SICP-2nd-Edition-Exercise-1.35

From Boozled

Jump to: navigation, search

Contents

Exercise 1.35 From SICP

Details from SICP are here

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

Attempt

This was a cut and paste exercise. Although if they wanted us to prove that φ is the fixed point that may be a different matter.

Scheme

  1. (define tolerance 0.00001)
  2. (define (fixed-point f first-guess)
  3.   (define (close-enough? v1 v2)
  4.     (< (abs (- v1 v2)) tolerance))
  5.   (define (try guess)
  6.     (let ((next (f guess)))
  7.       (if (close-enough? guess next)
  8.           next
  9.           (try next))))
  10.   (try first-guess))
  11.  
  12. (define (phi x) (+ 1 (/ 1 x)))
  13.  
  14. (display (fixed-point phi 1.0))
  15. (newline)
  16. (display (fixed-point cos 1.0))
  17. (newline)
  18. (display (fixed-point sin 0.5))
  19. (newline)
  20. (fixed-point (lambda (y) (+ (sin y) (cos y))) 1.0)
  21. (newline)

Parent Course

6.001_Structure_and_Interpretation_of_Computer_Programs

Further Reading

Personal tools