2015/10/08

SICP 問題1.35

f:id:wat-aro:20151008050935j:plain

x → 1 + 1/xを使い,fixed-point手続きにより黄金比を計算する

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

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

計算機プログラムの構造と解釈[第2版]

計算機プログラムの構造と解釈[第2版]


© 2022 wat-aro