sicp 1.6

xiaoxiao2026-06-14  5

if的新版本,用cond实现的new-if:

 

(define (new-if predicate then-clause else-clause) (cond (predicate then-clause) (else else-clause)))

 

重写的求平方根的程序:

 

(define (sqrt-iter guess x) (new-if (good-enough? guess x) guess (sqrt-iter (improve guess x) x)))

 

如果是正则序求值,if的新版本可以正确运行。

但是应用序求值,则会陷入无限递归,不断调用(sqrt-iter (improve guess x) x)。作为特殊的语法形式if,和函数new-if的求值顺序是不同的。new-if会先对参数求值。

转载请注明原文地址: https://www.6miu.com/read-5050139.html

最新回复(0)