<一>
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(new-if (= 2 3) 0 5)
(new-if (= 1 1) 0 5)
new-if在两种环境下均运行正常.
(if (= 2 3) 0 5)
(if (= 1 1) 0 5)
if 在R5RS下输出正常,在Lazy Racket下为(delay ...)
<二>
(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)))
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (sqrta x)
(sqrt-iter 1.0 x))
(define (square x)
(* x x))
(sqrta 9)
在R5RS下内存被耗尽,而在Lazy Racket下正常输出.
原因是new-if在R5RS,也就是应用序下,传入的每一个分支都会被求值.
<三>
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(new-if #t (display "good") (display "bad"))
R5RS输出:goodbad
Lazy Racket输出:good