题目定义了一个自己的if函数
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
问和if函数的区别。
首先根据练习1.5的程序
(define (p)(p))
(define (text x y)
(if (= x 0)
0
y))
用( text 0 (p) )测试解释器采用的求值顺序。假如为正则序求值,在x=0时直接输出结果0;如果为应用序求值,必须先求出(p)的值,会陷入无限循环。
测试后发现Racket编辑器为应用序求值。
参考答案后,Scheme所提供的if语句是一种特殊形式, new-if 只是一个普通函数,根据解释器所使用的应用序求值规则,每个函数的实际参数在传入的时候都会被求值,因此,当使用 new-if 函数时,无论 predicate 是真还是假, then-clause 和 else-clause 两个分支都会被求值。