2016年11月21日月曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原著: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の第2章(データによる抽象の構築)、2.4(抽象データの多重表現)、2.4.3(データ主導プログラミングと加法性)、メッセージパッシング、問題2.75.を取り組んでみる。

その他参考書籍

問題2.75.

コード(Emacs)

((lambda ()
  (load "procedures.scm")
  (newline)
  (define (p obj) (display obj) (newline))

  (p 2.75)

  (define (make-from-mag-ang m a)
    (define (dispatch op)
      (if (eq? op 'real-part)
          (* m (cos a))
          (if (eq? op 'imag-part)
              (* m (sin a))
              (if (eq? op 'magnitude)
                  m
                  (if (eq? op 'angle)
                      a
                      (error "unkown op -- make-from-real-imag" op))))))
    dispatch)

  (define pi 3.14)
  
  (define z (make-from-mag-ang 2 (/ pi 3)))
  (define ops '(real-part imag-part magnitude angle))
  (for-each (lambda (z)
              (p z))
            (map z ops))
  'done))

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

$ ksi < sample75.scm
ksi> 
2.75
1.000919378016412
1.731519678984689
2
1.046666666666667
=> done
ksi> $

0 コメント:

コメントを投稿