24 lines
664 B
Scheme
24 lines
664 B
Scheme
#lang sicp
|
|
|
|
(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)))
|
|
(if (close-enough? guess next)
|
|
next
|
|
(try next))))
|
|
(try first-guess))
|
|
|
|
; below is phi
|
|
;
|
|
; proof: f(x) = 1 + 1/x
|
|
; f(phi) = 1 + 1/((1+sqrt(5))/2) = 1 + 2/(1+sqrt(5)) =
|
|
; (1+sqrt(5))/(1+sqrt(5)) + 2/(1+sqrt(5)) =
|
|
; ( 3+sqrt(5) ) / (1+sqrt(5)) (multiply and divide by (1-sqrt(5)))
|
|
; you get: (1+sqrt(5))/2 qed
|
|
;
|
|
(define phi (fixed-point (lambda (x) (+ 1 (/ 1 x))) 2.0))
|
|
(define solution (lambda (x) (+ 1 (/ 1 x))))
|