計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- Gauche (処理系)
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の3(標準部品化力、オブジェクトおよび状態)、3.2(評価の環境モデル)、3.2.4(内部定義)、問題 3.11.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 3.11.
環境構造。
;; 大域環境
make-account
;; (共有部分)
;; パラメーター
balance
;; 本体
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch m)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT"
m))))
dispatch
;; -> 大域環境
;; (共有部分)
(define acc (make-account 50))
;; E1 -> 大域環境
balance: 50
;; 手続き
withdraw
;; パラメーター
amount
;; 本体
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds")
;; -> E1
;; 手続き
deposit
;; パラメーター
amount
;; 本体
(set! balance (+ balance amount))
balance
;; -> E1
;; 手続き
dispatch
;; パラメーター
m
;; 本体
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT"
m)))
;; -> E1
;; 大域環境
;; 手続き
acc
;; パラメーター
m
;; 本体
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unkown request -- MAKE-ACCOUNT"
m)))
;; -> E1
;; E2 -> E1
m: 'deposit
deposit
;; -> E1
(deposit 40)
;; E3 -> E1
amount: 40
(set! balance (+ balance amount))
(set! balance (+ balance 40))
;; E1
balance: 90
90
;; 大域環境
((acc 'withdraw) 60)
;; E4 -> E1
m: 'withdraw
withdraw
;; -> E1
(withdraw 60)
;; E5 -> E1
amount: 60
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds")
(if (>= balance 60)
(begin (set! balance (- balance 60))
balance)
"Insufficient funds")
;; E1
balance: 30
(define acc2 (make-account 100))
;; E6
balance: 100
;; 大域環境
;; 手続き
acc2
;; 手続き
withdraw
;; パラメーター
amount
;; 本体
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds")
;; -> E6
;; 手続き
deposit
;; パラメーター
amount
;; 本体
(set! balance (+ balance amount))
balance
;; -> E6
;; 手続き
dispatch
;; パラメーター
m
;; 本体
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT"
m)))
;; -> E6
;; acc2
dispatch
;; 共有する部分は、make-acount手続きのパラメーター、本体
0 コメント:
コメントを投稿