1.46 Exercise 1.46
(define (iterative-improve good-enough? improve-guess) | (define (iter guess) | (if (good-enough? guess) guess | (iter (improve-guess guess)))) | iter) |
|
The square root procedure is easy to rewrite using iterative-improve.
(define (sqrt x) | ((iterative-improve | (lambda (guess) (< (abs (- (square guess) x)) 0.001)) | (lambda (guess) (average guess (/ x guess)))) | 1.0)) |
|
fixed-point is also easy to rewrite, if you don’t need to get the exact
same result. The procedure relies on comparing the current guess with the next
guess, and returning the next guess if they are close enough. However,
iterative-improve returns the current guess if good-enough?
holds. The two results will be very close (as they have to be, if
close-enough? is going to return true), but not identical.
(define (fixed-point f first-guess) | (define tolerance #i1e-05) | (define (close-enough? v1 v2) | (< (abs (- v1 v2)) tolerance)) | ((iterative-improve | (lambda (guess) | (close-enough? guess (f guess))) | f) | first-guess)) |
|
At the moment, I don’t see a way around this.