計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- kscheme, Gauche (処理系)
計算機プログラムの構造と解釈[第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.9.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
- Scheme手習い
問題 2.9.
コード(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 add-interval
(lambda (x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y)))))
(define width
(lambda (interval)
(/ (- (upper-bound interval)
(lower-bound interval))
2)))
(define add-interval-width
(lambda (x-width y-width) (+ x-width y-width)))
(define i0 (make-interval 1 1))
(define i1 (make-interval 0 1))
(define i2 (make-interval 1 2))
(= (width (add-interval i0 i0))
(add-interval-width (width i0) (width i0)))
(= (width (add-interval i0 i1))
(add-interval-width (width i0) (width i1)))
(= (width (add-interval i0 i2))
(add-interval-width (width i0) (width i2)))
(= (width (add-interval i1 i0))
(add-interval-width (width i1) (width i0)))
(= (width (add-interval i1 i1))
(add-interval-width (width i1) (width i1)))
(= (width (add-interval i1 i2))
(add-interval-width (width i1) (width i2)))
(= (width (add-interval i2 i0))
(add-interval-width (width i2) (width i0)))
(= (width (add-interval i2 i1))
(add-interval-width (width i2) (width i1)))
(= (width (add-interval i2 i2))
(add-interval-width (width i2) (width i2)))
入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))
$ kscheme < sample9.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: add-interval ;(total-pushes = 3 maximum-depth = 3) In : Out: width ;(total-pushes = 3 maximum-depth = 3) In : Out: add-interval-width ;(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: #t ;(total-pushes = 190 maximum-depth = 18) In : Out: #t ;(total-pushes = 190 maximum-depth = 18) In : Out: #t ;(total-pushes = 190 maximum-depth = 18) In : Out: #t ;(total-pushes = 190 maximum-depth = 18) In : Out: #t ;(total-pushes = 190 maximum-depth = 18) In : Out: #t ;(total-pushes = 190 maximum-depth = 18) In : Out: #t ;(total-pushes = 190 maximum-depth = 18) In : Out: #t ;(total-pushes = 190 maximum-depth = 18) In : Out: #t ;(total-pushes = 190 maximum-depth = 18) In : $
0 コメント:
コメントを投稿