2015年3月8日日曜日

開発環境

計算機プログラムの構造と解釈[第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.4(値として返される手続き)、問題 1.44.を解いてみる。

その他参考書籍

問題 1.44.

コード(BBEdit, Emacs)

(define dx 0.00001)

(define compose
  (lambda (f g)
    (lambda (x)
      (f (g x)))))

(define repeated
  (lambda (f n)
    (if (= n 1)
        f
        (compose f
                 (repeated f (- n 1))))))

(define smooth
  (lambda (f)
    (lambda (x)
      (/ (+ (f (- x dx))
            (+ (f x)
               (f (+ x dx))))
         3))))

(define n-fold-smoothed
  (lambda (f n)
    ((repeated smooth n) f)))

(define f
  (lambda (x) x))

((n-fold-smoothed f 10) 0)
((n-fold-smoothed f 10) 1)

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

$ kscheme < sample44.scm
In : Out: dx
In : Out: compose
In : Out: repeated
In : Out: smooth
In : Out: n-fold-smoothed
In : Out: f
In : ;begin garbage collection: 1000000
;end garbage collection: 426
;begin garbage collection: 1000000
;end garbage collection: 407
Out: -0.264714662908082577744e-39
In : ;begin garbage collection: 1000000
;end garbage collection: 417
;begin garbage collection: 1000000
;end garbage collection: 411
Out: 0.999999999999999999729e0
In : $

0 コメント:

コメントを投稿