2015年2月27日金曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の1(手続きによる抽象の構築)、1.3(高階手続きによる抽象)、1.3.3(一般方法としての手続き)、問題 1.37-b.を解いてみる。

その他参考書籍

問題 1.37-b.

コード(BBEdit, Emacs)

#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-

(define cont-frac
  (lambda (n d k)
    (define iter
      (lambda (i result)
        (if (= i 1)
            result
            (iter (- i 1)
                  (/ (n (- i 1))
                     (+ (d (- i 1))
                        result))))))
    (iter k (/ (n k)
               (d k)))))

(define x (/ 1 1.6180))
(define f
  (lambda (k)
    (define result (cont-frac (lambda (i) 1.0)
                              (lambda (i) 1.0)
                              k))
    (print k ": " result)
    (if (> (abs (- x result)) 0.00001)
        (f (+ k 1)))))

(f 2)

入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))

$ ./sample37_b.scm
2: 0.5
3: 0.6666666666666666
4: 0.6000000000000001
5: 0.625
6: 0.6153846153846154
7: 0.6190476190476191
8: 0.6176470588235294
9: 0.6181818181818182
10: 0.6179775280898876
11: 0.6180555555555556
$

0 コメント:

コメントを投稿