Sunday, July 4, 2010

SICP Exercise 1.8

SICP 1.8

 
 (define (square x)  
  (* x x))

(define (cube x)  
  (* x x x))  

 (define (improve guess x)  
  (/ (+ (/ x (square guess)) (* 2 guess)) 3))
  
 (define (good-enough? old-guess new-guess)  
  (< (/ (abs (- new-guess old-guess)) new-guess) 0.001))  

 (define (cube-iter old-guess new-guess x)  
  (if (good-enough? old-guess new-guess)  
    new-guess  
    (cube-iter new-guess (improve new-guess x) x)))  

 (define (cube-root x)  
  (cube-iter 0 1 x))  

No comments:

Post a Comment