Exercise 1.5. Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
Then he evaluates the expression
(test 0 (p))
What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.)
- applicative-order evaluation 即先计算再传递
- normal-order evaluation 即先传递再计算
对于 Ben 的程序,applicative-order 将会先去计算过程调用时候的参数,这使得
(test 0 (p))
先去计算 (p),于是程序进入了死循环。
如果这个语言是 normal-order,则这个过程的调用被替代成了:
(if (= x 0)
0
(p))
返回结果 0。
这两个求值顺序都有什么作用呢?为什么要设计这两种求值顺序呢?
在练习 1.6中可以看出一些答案。而在第三章 “stream” 中,可以看到某些时候我们也会刻意地去模仿 normal-order evalutation。