開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Scheme (プログラミング言語)
- kscheme (ksi)(github) (処理系)
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原著: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の第3章(標準部品化力、オブジェクト及び状態)、3.1(代入と局所状態)、3.1.3(代入を取り入れた対価)、問題3.7.を取り組んでみる。
その他参考書籍
問題3.7.
コード(Emacs)
((lambda () (load "procedures.scm") (newline) (define (p obj) (display obj) (newline)) (p 3.7) (define (make-account balance password) (define passwords (list password)) (define (withdraw amount) (if (>= balance amount) ((lambda () (set! balance (- balance amount)) balance)) "insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (add-password pass) (set! passwords (cons pass passwords))) (define (dispatch pwd m) (if (memq pwd passwords) (if (eq? m 'withdraw) withdraw (if (eq? m 'deposit) deposit (if (eq? m 'add-password) add-password (error "Unknown request -- MAKE-ACCOUNT" m)))) (lambda (o) (p "Incorrect password")))) dispatch) (define (make-joint acc pass new-pass) ((acc pass 'add-password) new-pass) acc) (define peter-acc (make-account 100 'open-sesame)) (p ((peter-acc 'open-sesame 'withdraw) 10)) (p ((peter-acc 'open-sesame 'deposit) 10)) (define paul-acc (make-joint peter-acc 'open-sesame 'rosebud)) (p ((paul-acc 'open-sesame 'withdraw) 10)) (p ((paul-acc 'open-sesame 'deposit) 10)) (p ((paul-acc 'rosebud 'withdraw) 10)) (p ((paul-acc 'rosebud 'deposit) 10)) (p ((peter-acc 'open-sesame 'withdraw) 10)) (p ((peter-acc 'open-sesame 'deposit) 10)) (p ((peter-acc 'rosebud 'withdraw) 10)) (p ((peter-acc 'rosebud 'deposit) 10)) 'done))
入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))
$ ksi < sample7.scm ksi> 3.7 90 100 90 100 90 100 90 100 90 100 => done ksi> $
0 コメント:
コメントを投稿