This shows you the differences between two versions of the page.
|
exercise_1.46 [2009/03/07 09:30] inimino created |
exercise_1.46 [2009/03/07 09:39] (current) inimino |
||
|---|---|---|---|
| Line 14: | Line 14: | ||
| ====== Solutions ====== | ====== Solutions ====== | ||
| + | |||
| + | <code scheme> | ||
| + | (define (iterative-improve good-enough? improve) | ||
| + | (define (iter guess) | ||
| + | (if (good-enough? guess) | ||
| + | guess | ||
| + | (iter (improve guess)))) | ||
| + | iter) | ||
| + | |||
| + | (define (sqrt x) | ||
| + | (iterative-improve (lambda (guess) (< (abs (- (square guess) x)) 0.001)) | ||
| + | (lambda (guess) (average guess (/ x guess))))) | ||
| + | |||
| + | (define (fixed-point f first-guess) | ||
| + | (iterative-improve (lambda (guess) (< (abs (- (f guess) guess)) tolerance)) | ||
| + | (lambda (guess) (f guess)))) | ||
| + | </code> | ||
| + | |||
| + | This fixed-point implementation is deeply unsatisfying as it calculates | ||
| + | (f guess) twice as many times as necessary. However, without mutable state | ||
| + | or changing the interface of iterative-improve, is there any way around it? | ||