2015年9月24日木曜日

開発環境

計算機プログラムの構造と解釈[第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.4.を解いてみる。

その他参考書籍

問題3.4.

コード(Emacs)

(begin
  (define print (lambda (x) (display x) (newline)))

  (define (make-account balance password)
    (let ((wrong 0))
      (define (withdraw amount)
        (if (>= balance amount)
            (begin (set! balance (- balance amount))
                   balance)
            "Insufficient funds"))
      (define (deposit amount)
        (set! balance (+ balance amount))
        balance)
      (define (call-the-cops) (quote call-the-cops))
      (define (dispatch pwd m)
        (if (eq? pwd password)
            (begin (set! wrong 0)
                   (cond ((eq? m (quote withdraw)) withdraw)
                         ((eq? m (quote deposit)) deposit)
                         (else (lambda ()
                                 (display "Unknown request -- MAKE-ACCOUNT ")
                                 (print m)))))
            (lambda (x)
              (set! wrong (+ wrong 1))
              (if (>= wrong 7)
                  (call-the-cops)
                  "Incorrect password"))))
      dispatch))
  

  (define acc1 (make-account 100 (quote secret-password1)))
  (define acc2 (make-account 100 (quote secret-password2)))

  (print "acc1")
  (print ((acc1 (quote secret-password1) (quote withdraw)) 40))
  (print ((acc1 (quote some-other-password) (quote deposit)) 50))

  (print "acc2")
  (print ((acc2 (quote secret-password2) (quote withdraw)) 40))
  (print ((acc2 (quote secret-password2) (quote deposit)) 50))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote secret-password2) (quote deposit)) 50))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1))
  (print ((acc2 (quote wrong-password) (quote withdraw)) 1)))

入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))

$ ./kscheme sample4.scm
acc1
60
Incorrect password
acc2
60
110
Incorrect password
Incorrect password
Incorrect password
Incorrect password
Incorrect password
160
Incorrect password
Incorrect password
Incorrect password
Incorrect password
Incorrect password
Incorrect password
call-the-cops
call-the-cops
call-the-cops
$

0 コメント:

コメントを投稿

Comments on Google+: