sicp-ex-1.46



<< Previous exercise (1.45) | Index | Next exercise (2.1) >>


Note that the arguments to iterative-improve must:

  1. tell if a guess is good enough
  2. improve a guess
  
 (define (iterative-improve good-enough? improve) 
   (lambda (x) 
     (define (iter n) 
       (if (good-enough? n) 
           n 
           (iter (improve n)))) 
     (iter x))) 
  
  
 (define (close-enough? v1 v2) 
   (< (abs (- v1 v2)) tolerance)) 
  
 (define (fixed-point f first-guess) 
   ((iterative-improve 
     (lambda (x) (close-enough? x (f x))) 
     f) 
    first-guess)) 
  
 (define (sqrt x) 
   ((iterative-improve 
     (lambda (y) 
       (< (abs (- (square y) x)) 
          0.0001)) 
     (lambda (y) 
       (average y (/ x y)))) 
    1.0))