開発環境
- 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.1(局所状態変数)、問題3.1、3.2、3.3、3.4.を取り組んでみる。
その他参考書籍
問題3.1、3.2、3.3、3.4.
コード(Emacs)
((lambda () (load "procedures.scm") (newline) (define (p obj) (display obj) (newline)) (p 3.1) (define (make-accumulator sum) (lambda (n) (set! sum (+ sum n)) sum)) (define A (make-accumulator 5)) (define B (make-accumulator 5)) (p (A 10)) (p (A 10)) (p (B 10)) (p (B 10)) (p 3.2) (define (make-monitored f) (define counter 0) (define (mf o) (if (eq? o 'how-many-calls?) counter (if (eq? o 'reset-count) (set! counter 0) ((lambda () (set! counter (+ counter 1)) (f o)))))) mf) (define s (make-monitored sqrt)) (p (s 100)) (p (s 'how-many-calls?)) (p (s 100)) (p (s 'how-many-calls?)) (s 'reset-count) (p (s 'how-many-calls?)) (p (s 100)) (p (s 'how-many-calls?)) (p 3.3) (define (make-account balance password) (define (withdraw amount) (if (>= balance amount) ((lambda () (set! balance (- balance amount)) balance)) "insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch pwd m) (if (eq? pwd password) (if (eq? m 'withdraw) withdraw (if (eq? m 'deposit) deposit (error "Unknown request -- MAKE-ACCOUNT" m))) (lambda (o) (p "Incorrect password")))) dispatch) (define acc (make-account 100 'secret-password)) (p ((acc 'secret-password 'withdraw) 40)) (p ((acc 'some-other-password 'deposit) 50)) (p 3.4) (define (call-the-cops o) 'call-the-cops) (define (make-account balance password) (define counter 0) (define (withdraw amount) (if (>= balance amount) ((lambda () (set! balance (- balance amount)) balance)) "insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch pwd m) (if (eq? pwd password) ((lambda () (set! counter 0) (if (eq? m 'withdraw) withdraw (if (eq? m 'deposit) deposit (error "Unknown request -- MAKE-ACCOUNT" m))))) (if (= counter 6) call-the-cops ((lambda () (set! counter (+ counter 1)) (lambda (o) (p "Incorrect password"))))))) dispatch) (define acc (make-account 100 'secret-password)) (p ((acc 'secret-password 'withdraw) 40)) (p ((acc 'some-other-password 'deposit) 50)) (p ((acc 'some-other-password 'deposit) 50)) (p ((acc 'some-other-password 'deposit) 50)) (p ((acc 'some-other-password 'deposit) 50)) (p ((acc 'some-other-password 'deposit) 50)) (p ((acc 'some-other-password 'deposit) 50)) (p ((acc 'secret-password 'withdraw) 40)) (p ((acc 'some-other-password 'deposit) 50)) (p ((acc 'some-other-password 'deposit) 50)) (p ((acc 'some-other-password 'deposit) 50)) (p ((acc 'some-other-password 'deposit) 50)) (p ((acc 'some-other-password 'deposit) 50)) (p ((acc 'some-other-password 'deposit) 50)) (p ((acc 'some-other-password 'deposit) 50)) 'done))
入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))
$ ksi < sample1.scm ksi> 3.1 15 25 15 25 3.2 10 1 10 2 0 10 1 3.3 60 Incorrect password #<unspecified> 3.4 60 Incorrect password #<unspecified> Incorrect password #<unspecified> Incorrect password #<unspecified> Incorrect password #<unspecified> Incorrect password #<unspecified> Incorrect password #<unspecified> 20 Incorrect password #<unspecified> Incorrect password #<unspecified> Incorrect password #<unspecified> Incorrect password #<unspecified> Incorrect password #<unspecified> Incorrect password #<unspecified> call-the-cops => done ksi> $
0 コメント:
コメントを投稿