Exercise 1.40 of SICP

Exercise 1.40: Define a procedure cubic that can be used together with the newtons-method procedure in expressions of the form

(newtons-method (cubic a b c) 1)

to approximate zeros of the cubic x3 + ax2 + bx + c.

(define (cubic a b c)
  (lambda (x)
      (* x x x)
      (* a x x)
      (* b x)

(define dx 0.00001)
(define (deriv g)
  (lambda (x)
    (/ (- (g (+ x dx)) (g x))

(define (newton-transform g)
  (lambda (x)
    (- x
       (/ (g x) ((deriv g) x)))))

(define (newtons-method g guess)
  (fixed-point (newton-transform g) guess))

(define (fixed-point f first-guess)
  (define tolerance 0.00001)
  (define (close-enough? v1 v2)
    (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (let ((next (f guess)))
      (if (close-enough? guess next)
          (try next))))
  (try first-guess))
> (newtons-method (cubic -2 -9 18) -2)

> (newtons-method (cubic 8 3 6) 1)

Note that Newton’s Method only finds a root not all roots or even all real roots.