2015/10/09

SICP 問題1.46

(define (iterative-improve enough? improve)
  (lambda (guess)
    (define (iter guess)
      (if (enough? guess)
          (improve guess)
          (iter (improve guess))))
    (iter guess)))

(define (sqrt x)
  (define (improve guess)
    (average guess (/ x guess)))
  (define (good-enough? guess)
    (< (abs (- 1 (/ guess (improve guess)))) 0.001))
  ((iterative-improve good-enough? improve) 1.0))

(define (fixed-point f first-guess)
  (define (improve guess)
    (f guess))
  (define (close-enough? guess)
    (< (abs (- guess (improve guess))) 0.00001))
  ((iterative-improve close-enough? improve) first-guess))

© 2022 wat-aro