Exercise 1.46
Several of the numerical methods described in this chapter are instances of an extremely general computational strategy known as iterative improvement. Iterative improvement says that to compute something, we start with aninitial guess for the answer, test if the guess is good enough, and otherwise improve the guess and continue the process using the improved guess as the new guess. Write a procedure iterative-improve that takes two procedures as arguments: a method for telling whether a guess is good enough and a method for improving a guess. iterative improve should return as its value a procedure that takes a guess as argument and keeps improving the guess until it is good enough. Rewrite the sqrt procedure of Section 1.1.7 and the fixed-point procedure of Section 1.3.3 in terms of iterative-improve.
(define tolerance 0.00001)
(define (good-enough? guess target tolerance)
(< (abs (- guess target)) tolerance))
; 注意:這個函式的兩個引數以及返回值都是函式,所以呼叫的語句外還要傳一個引數作為 first-guess
(define (iterative-improve good-enough? improve-guess)
(lambda (first-guess)
(define (iter guess)
(if (good-enough? guess)
guess
(iter (improve-guess guess))))
(iter first-guess)))
; Rewrite Version
; 1.1.7 Square Roots by Newton's Method
(define (sqrt x)
((iterative-improve (lambda (guess) (good-enough? (square guess) x tolerance))
(lambda (guess) (average guess (/ x guess))))
1.0))
; 1.3.3 Procedures as General Methods
(define (fixed-point f first-guess)
((iterative-improve (lambda (guess) (good-enough? guess (f guess) tolerance))
f)
first-guess))
(sqrt 2)
(sqrt 10)
(fixed-point cos 1.0)
(fixed-point (lambda (x) (+ (sin x) (cos x))) 1.0)
; 執行結果
1.4142156862745097
3.162277665175675
0.7390893414033927
1.2587228743052672