2016年10月1日土曜日

開発環境

計算機プログラムの構造と解釈[第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.40.を取り組んでみる。

その他参考書籍

問題1.40.

コード(Emacs)

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

  (define tolerance 0.00001)
  (define (fixed-point f first-guess)
    (define (close-enough? v1 v2)
      (< (abs (- v1 v2)) tolerance))
    (define (try guess)
      ((lambda (next)
         (if (close-enough? guess next)
             next
             (try next)))
       (f guess)))
    (try first-guess))
  
  (define dx 0.00001)
  (define (deriv g)
    (lambda (x)
      (/ (- (g (+ x dx)) (g x))
         dx)))
  (define (newton-transform g)
    (lambda (x)
      (- x (/ (g x) ((deriv g) x)))))
  (define (newton-method g guess)
    (fixed-point (newton-transform g) guess))

  (define (cubic a b c)
    (lambda (x)
      (+ (* x x x) (* a x x) (* b x) c)))
  
  (define x (newton-method (cubic 1 2 3) 1))
  (p x)
  (p (+ (* x x x) (* 1 x x) (* 2 x) 3))
  'done)

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

$ ksi < sample40.scm
ksi> 
-1.275682203649845
4.935607478273596e-12
=> done
ksi> $

0 コメント:

コメントを投稿