開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: MIT/GNU Scheme
計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の3(標準部品化力, オブジェクトおよび状態)、3.1(代入と局所状態)、3.1.1(局所状態変数)の問題 3.4を解いてみる。
その他参考書籍
問題 3.4
コード(BBEdit)
sample.scm
(define (make-account balance secret-password) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define count 0) (define (call-the-cops a) "Call the cops!") (define (dispatch password m) (if (eq? secret-password password) (begin (set! count 0) (cond ((eq? m 'withdraw) withdraw) ((eq? m 'deposit) deposit) (else (error "Unkown request -- MAKE-ACCOUNT" m)))) (begin (set! count (+ count 1)) (if (> count 6) call-the-cops (lambda (a) "Incorrect password"))))) dispatch) (define acc (make-account 100 'secret-password))
入出力結果(Terminal, REPL(Read, Eval, Print, Loop))
1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'secret-password 'withdraw) 40) ;Value: 60 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'secret-password 'withdraw) 40) ;Value: 20 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 2: "Incorrect password" 1 ]=> ((acc 'some-other-password 'deposit) 50) ;Value 3: "Call the cops!"
7回連続じゃないといけないことが確認できた。
0 コメント:
コメントを投稿