SICP 全笔记

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

$$\frac{\frac{x}{y^2}+2y}{3} $$ 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.)

只需要更改 improve 就可以了

(define (cube-root x)
  (define (cube-root-iter last-guess guess x)
    (if (good-enough? last-guess guess)
        guess
        (cube-root-iter guess (improve guess x)
                        x)))

  (define (improve guess x)
    (average guess (/ (+ (/ x (* guess guess)) (* 2 guess))
                      3)))

  (define (average x y)
    (/ (+ x y) 2))

  (define (good-enough? last-guess guess)
    (< (/ (abs (- last-guess guess)) guess) 0.001))

  (cube-root-iter 0 1.0 x))