2016年10月6日木曜日

開発環境

計算機プログラムの構造と解釈[第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.

コード(Emacs)

(begin
  (load "procedures.scm")
  (newline)
  (define (p x) (display x) (newline))

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

  (define (repeated proc n)
    (if (= n 1)
        (lambda (x) (proc x))
        (compose proc (repeated proc (- n 1)))))

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

  (define (n-smooth f n)
    ((repeated smooth n) f))

  (define (f x) (abs x))
  (define g (smooth f))
  (define h (n-smooth f 5))


  (for-each (lambda (proc)
              (p (proc 0)))
            (list f g h))
  'done)

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

$ ksi < sample44.scm
ksi> 
0
6.666666666666667e-06
1.440329218106996e-05
=> done
ksi> $

0 コメント:

コメントを投稿