Exercise 3.8. When we defined the evaluation model in section 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.
我们可以使用 scheme 的内建的求值顺序来得到答案。
mit-scheme 参数的求值顺序是从右到左。所以可以如下来进行测验:
(define f
(let ((x 1))
(lambda (z)
(set! x (* x z))
x)))
;; evalution from right to left, which is the order of the scheme
;; 1. x = (f 0)
;; 2. y = (f 1)
;; 3. z = x + y
(+ (f 1) (f 0))
; => 0
; reload again, rename it to f1
(define f1
(let ((x 1))
(lambda (z)
(set! x (* x z))
x)))
;; 1. x = (f 1)
;; 2. y = (f 0)
;; 3. z = x + y
(+ (f1 0) (f1 1))
; => 1