;; SICP 1.3.3
(define (search f neg-point pos-point)
(let ((midpoint (average neg-point pos-point)))
(if (close-enough? neg-point pos-point)
midpoint
(let ((test-value (f midpoint)))
(cond ((positive? test-value)
(search f neg-point midpoint))
((negative? test-value)
(search f midpoint pos-point))
(else midpoint))))))
;;(define (close-enough? x y)
;; (< (abs (- x y)) 1e-12))
(define (close-enough? x y)
(< (abs (- x y)) (/ 1 (expt 10 12))))
(define (average a b) (/ (+ a b) 2))
(define (half-interval-method f a b)
(let ((a-value (f a))
(b-value (f b)))
(cond ((and (negative? a-value) (positive? b-value))
(search f a b))
((and (negative? b-value) (positive? a-value))
(search f b a))
(else
(error "Values are not of opposite sign" a b)))))
(define (f x)
(- 2 (* x x)))
(let ((r (half-interval-method f 1 2)))
(format #t "~d = ~d\n" r (exact->inexact r)))
(let ((r (half-interval-method sin 2 4)))
(format #t "~d = ~d\n" r (exact->inexact r)))
(define tolerance 0.00001)
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
;;(print next)
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
(print (fixed-point cos 1.0))
(print (fixed-point (lambda (y) (+ (sin y) (cos y))) 1.0))
;; apply average-damping to prevent oscillations
(define (sqrt. x)
(fixed-point (lambda (y) (average y (/ x y))) 1.0))
(print (sqrt. 2))
(define (golden-ratio)
(fixed-point (lambda (x) (average x (+ 1 (/ 1 x)))) 1.0))
(print (golden-ratio))