灯下 登录
计算机科学 / SICP / 3.1.3 The Costs of Introducing Assignment

Exercise 3.8 · 习题

Exercise 3.8: When we defined the evaluation
model in 1.1.3, we said that the first step in evaluating an
expression is to evaluate its subexpressions. But we never specified the order
in which the subexpressions should be evaluated (e.g., left to right or right
to left). When we introduce assignment, the order in which the arguments to a
procedure are evaluated can make a difference to the result. Define a simple
procedure f such that evaluating

(+ (f 0) (f 1))

will return 0 if

the arguments to + are evaluated from left to right but will return 1 if

the arguments are evaluated from right to left.

练习 3.8:在 1.1.3 中定义求值模型时,我们说过对表达式求值的第一步是对其子表达式求值。但我们从未规定子表达式的求值顺序(例如,是从左到右还是从右到左)。当我们引入赋值之后,过程参数的求值顺序会对结果产生影响。请定义一个简单的过程 f,使得对

(+ (f 0) (f 1))

求值时,若 + 的参数从左到右求值则返回 0,若从右到左求值则返回 1。

Racket #lang sicp
(+ (f 0) (f 1))