Resource > SICP Exersise 3_3
Exersise 3.3
make-account関数にパスワード機能を追加する。
make-account関数にパスワード機能を追加する。
(define (make-account balance password)
(let ((saved-password ""))
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch password m)
(if (equal? saved-password password)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT"
m)))
(error "Incorrect password")))
(begin (set! saved-password password)
dispatch)))
(use gauche.test)
(test-start "Exersise 3.3")
(define acc (make-account 100 'secret-password))
(test* "withdraw" 60 ((acc 'secret-password 'withdraw) 40))
(test* "deposit" 110 ((acc 'secret-password 'deposit) 50))
(test* "error" *test-error* ((acc 'some-other-password 'deposit) 50))
(test-end)