sicp-ex-1.36


For the modification to print each value:

 (define tolerance 0.000001) 
  
 (define (fixed-point f first-guess) 
   (define (close-enough? v1 v2) 
     (< (abs (- v1 v2) tolerance)) 
     (define (try guess) 
       (display guess) 
       (newline) 
       (let ((next (f guess))) 
         (if (close-enough? guess next) 
             next 
             (try next)))) 
     (try first-guess))) 

Fixed-point above has an error, the line with (< (abs (- v1 v2) tolerance)) should be be (< (abs (- v1 v2)) tolerance)) and at the end it should be (try first-guess))

The correct version looks like this:

 (define tolerance 0.000001) 
  
 (define (fixed-point f first-guess) 
   (define (close-enough? v1 v2) 
     (< (abs (- v1 v2)) tolerance)) 
     (define (try guess) 
       (display guess) 
       (newline) 
       (let ((next (f guess))) 
         (if (close-enough? guess next) 
             next 
             (try next)))) 
     (try first-guess)) 

Note that it would be more idiomatic to extract the small function:

 (define (print-line value) 
    (display value) 
    (newline)) 

 (define (x-to-the-x y) 
   (fixed-point (lambda (x) (/ (log y) (log x))) 
     10.0)) 

Using my version of Scheme (Petite Chez Scheme), this takes 33 iterations to converge, printing out the final answer on the 34th line.

If we make the suggested change to use the average function,

 (define (x-to-the-x y) 
   (fixed-point (lambda (x) (average x (/ (log y) (log x)))) 
     10.0)) 

This converges in 10 iterations, printing the result on the 11th line.


<< Previous exercise (1.35) | Next exercise (1.37) >>