2015年3月22日日曜日

開発環境

計算機プログラムの構造と解釈[第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.8.を解いてみる。

その他参考書籍

問題 2.8.

コード(BBEdit, Emacs)

(define make-interval
  (lambda (a b) (cons a b)))

(define lower-bound
  (lambda (interval) (car interval)))

(define upper-bound
  (lambda (interval) (cdr interval)))

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

(define interval (make-interval 1 2))

(define sub-interval
  (lambda (x y)
    (make-interval (- (lower-bound x) (upper-bound y))
                   (- (upper-bound x) (lower-bound y)))))
                      

(define i0 (make-interval 1 1))
(define i1 (make-interval 0 1))
(define i2 (make-interval 1 2))

(sub-interval i0 i1)
(sub-interval i1 i1)
(sub-interval i1 i0)
(sub-interval i2 i0)
(sub-interval i1 i2)
(sub-interval i2 i1)

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

$ kscheme  < sample8.scm
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: print-interval
;(total-pushes = 3 maximum-depth = 3)
In : Out: interval
;(total-pushes = 17 maximum-depth = 7)
In : Out: sub-interval
;(total-pushes = 3 maximum-depth = 3)
In : Out: i0
;(total-pushes = 17 maximum-depth = 7)
In : Out: i1
;(total-pushes = 17 maximum-depth = 7)
In : Out: i2
;(total-pushes = 17 maximum-depth = 7)
In : Out: (0 . 1)
;(total-pushes = 67 maximum-depth = 11)
In : Out: (-1 . 1)
;(total-pushes = 67 maximum-depth = 11)
In : Out: (-1 . 0)
;(total-pushes = 67 maximum-depth = 11)
In : Out: (0 . 1)
;(total-pushes = 67 maximum-depth = 11)
In : Out: (-2 . 0)
;(total-pushes = 67 maximum-depth = 11)
In : Out: (0 . 2)
;(total-pushes = 67 maximum-depth = 11)
$

0 コメント:

コメントを投稿