Show that the golden ratio ϕ (section 1.2.2) is a fixed point of the transformation x ↦ 1 + 1/x, and use this fact to compute ϕ by means of the fixed-point procedure.

(define ϕ (/ (+ 1 (sqrt 5)) 2))
(+ 1 (/ 1 ϕ)) ; => ϕ = 1.618033988749895
 
(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))
 
(fixed-point (lambda (x) (+ 1 (/ 1 x))) 1) ; => 987 / 610 = 1.6180327869
 
exercise_1.35.txt · Last modified: 2009/03/02 01:46 by 204.14.159.185
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki