Open Source WEB

##(link2sicp "book-Z-H-12.html#%_thm_1.35" "Exercise 1.35")

解答例

x |-> 1 + 1/x の不動点は x = 1 + 1/x すなわち x^2 =- x + 1 の解、すなわち、黄金比である。

(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))

実行結果

gosh> (fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0)
1.6180327868852458

コード

##(sicp-answer-code "ex-1.35.scm")

このサイトは、 IPA の「平成15年度オープンソフトウエア活用基盤整備事業」 の委託事業として開発されたKahuaで試験的に運用しております。

Copyright (c) 2004-2007 株式会社タイムインターメディア About Us