需要lazy evaluation的一个例子
why functional programming matters里看来的例子,改成了Scheme版本。
占个位子。等SICP做到第四章,搞出LE,这个就可以解决了。
(define (within eps lst)
(let ((a (car lst))
(b (cadr lst)))
(if (<= (abs (- a b)) eps)
b
(within eps (cdr lst)))))
(define (repeat f a)
(cons a (repeat f (f a))))
(define (square-root guess eps n)
(within esp (repeat
(lambda (x) (/ (+ x (/ n x)) 2))
guess)))
0 条评论: