SICP-Exercise 1.5

Exercise 1.5.  Ben Bitdiddle has invented a test to determine whether the interpreterhe is faced with is using applicative-order evaluation or normal-orderevaluation. 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 usesapplicative-order evaluation? What behavior will he observe with aninterpreter 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 evaluatethe consequent or the alternative expression.)

1、无參数函数

(define (p) (+ 1 2) )
;Value p

p
;Value 18:#[compound-procedure 18 p]

(p)
;Value 3

比較一下:

(define x (+ 1 2) )
;Value x

x
;Value 3

2、无限循环

(define (p) (p))无限循环。前一个(p)定义无參数函数,后一个(p)表示调用自己。


3.应用序和正则序的差别

应用序:(test 0 (p))时,先求值(p)——无限循环,再将(p)的值带入

(if (= x 0)
      0
      y))

正则序:(test 0 (p))时。先带入/展开,即

(if (= x 0)
      0
      (p)))

依照if求值顺序,推断0=0,结果为#t。

注意:if为正则序

posted on 2017-08-11 08:35  slgkaifa  阅读(110)  评论(0编辑  收藏  举报

导航