1 day ago
my attempt to do the exercises in sicp.
Monday, August 17, 2009
sicp exercise 3.4
;; Exercise 3.4. Modify the make-account procedure of exercise 3.3 by adding another local state variable so that, if an account is accessed more than seven consecutive times with an incorrect password, it invokes the procedure call-the-cops.
(define (make-account balance password)
(define wrong-pass-count 0)
(define (call-the-cops) "you are under arrest")
(define (wrong-passwd arg)
(begin (set! wrong-pass-count (+ 1 wrong-pass-count))
(if (> wrong-pass-count 7) (call-the-cops)
"wrong 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 passwrd m)
(if (eq? passwrd password)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT"
m)))
wrong-passwd))
dispatch)
(define acc (make-account 100 'secret-password))
(display ((acc 'secret-password 'withdraw) 40)) (newline)
(display ((acc 'secret-password 'withdraw) 40)) (newline)
(display ((acc 'secret-password 'withdraw) 40)) (newline)
(display ((acc 'some-other-password 'deposit) 50)) (newline)
(display ((acc 'some-other-password 'deposit) 50)) (newline)
(display ((acc 'some-other-password 'deposit) 50)) (newline)
(display ((acc 'some-other-password 'deposit) 50)) (newline)
(display ((acc 'some-other-password 'deposit) 50)) (newline)
(display ((acc 'some-other-password 'deposit) 50)) (newline)
(display ((acc 'some-other-password 'deposit) 50)) (newline)
(display ((acc 'some-other-password 'deposit) 50)) (newline)
(display ((acc 'some-other-password 'deposit) 50)) (newline)
(display ((acc 'some-other-password 'deposit) 50)) (newline)
(display ((acc 'some-other-password 'deposit) 50)) (newline)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment