my attempt to do the exercises in sicp.

Saturday, July 5, 2008

sicp exercise 1.8


;  Exercise 1.8.  Newton's method for cube roots is based on the fact that if y is an approximation to
;  the cube root of x, then a better approximation is given by the value

;  Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In section
;  1.3.4 we will see how to implement Newton's method in general as an abstraction of these square-root
;  and cube-root procedures.)



(begin
(define ( cube-root x )
  (define (good-enough? guess p)
  ( < (abs (- p ( * guess guess guess) )) 0.000001)
  )
  (define (improve guess x)
     (/ ( + (/ x (* guess guess)) (* 2 guess)) 3)
  )
  (define (cube-root-impl x guess)
  ( if (good-enough? guess x)
     guess
     (cube-root-impl x (improve guess x))
  )
  )
  (cube-root-impl  x 1.0 )
)
(display (cube-root 125))
(newline)
)

No comments: