练习1.46
这道题要求我们写一个过程iterative-improve,其接受两个过程为参数,一个是判断检测是否足够好的good-enough?和另一个改进猜测的improve。其有一个猜测的数字作为参数,然后返回的是一个过程。
我们先来写出这个iterative-improve过程。
(define (iterative-improveclose-enough? improve)
(lambda (first-guess)
(define (try guess)
(let ((next (improve guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess)))
如果这道题不会的话就要再重新温习一下第一章了,其思路在于不断的抽象书中的fixed-point函数。
接下来就是重写sqrt和fixed-point了。
(define (sqrt x)
(define dx 0.00001)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) dx))
(define (improve guess)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
((iterative-improve close-enough? improve)1.0))
(define (fixed-point ffirst-guess)
(define tolerance 0.00001)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (improve guess)
(f guess))
((iterative-improve close-enough? improve)first-guess))
其实现在已经深夜了,最后一题就比较简陋了。如果不会的话好好琢磨啦。