2015年3月27日金曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の2(データによる抽象の構築)、2.1(データ抽象入門)、2.1.4(拡張問題: 区間算術演算)、問題 2.12.を解いてみる。

その他参考書籍

問題 2.12.

コード(BBEdit, Emacs)

(define abs (lambda (x) (if (< x 0)
                            (* -1 x)
                            x)))

(define make-interval (lambda (a b) (cons a b)))
(define lower-bound (lambda (interval) (car interval)))
(define upper-bound (lambda (interval) (cdr interval)))

(define make-center-percent
  (lambda (center percent)
    (make-interval (- center (abs (* center (/ percent 100))))
                   (+ center (abs (* center (/ percent 100)))))))

(define center
  (lambda (i)
    (/ (+ (lower-bound i) (upper-bound i)) 2)))

(define width
  (lambda (i)
    (/ (- upper-bound i) (lower-bound i)) 2))

(define percent
  (lambda (i)
    (* (abs (/ (width i) (center i)))
       100)))              

(define print-interval
  (lambda (interval)
    (display "(")
    (display (lower-bound interval))
    (display ", ")
    (display (upper-bound interval))
    (display ")")
    (newline)))

(print-interval (make-center-percent 100 10))

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

$ kscheme < sample12.scm
In : Out: abs
;(total-pushes = 3 maximum-depth = 3)
In : Out: make-interval
;(total-pushes = 3 maximum-depth = 3)
In : Out: lower-bound
;(total-pushes = 3 maximum-depth = 3)
In : Out: upper-bound
;(total-pushes = 3 maximum-depth = 3)
In : Out: make-center-percent
;(total-pushes = 3 maximum-depth = 3)
In : Out: center
;(total-pushes = 3 maximum-depth = 3)
In : Out: width
;(total-pushes = 3 maximum-depth = 3)
In : Out: percent
;(total-pushes = 3 maximum-depth = 3)
In : Out: print-interval
;(total-pushes = 3 maximum-depth = 3)
In : (90, 110)
Out: ;undefined
;(total-pushes = 96 maximum-depth = 18)
In : $

0 コメント:

コメントを投稿