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为正则序

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值