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 secret-password)
(define count 0)
(define limit 7)
(define (call-the-cops) "Calling the police..")
(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)
(cond
((eq? password secret-password)
(begin
(set! count 0)
(cond
((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request — MAKE-ACCOUNT"
m)))))
(else
(lambda (x)
(if (> count limit)
(call-the-cops)
(begin
(set! count (+ count 1))
"Incorrect Password"))))))
dispatch)
(define acc (make-account 100 'secret-password))
((acc 'secret-password 'withdraw) 50)
((acc 'secret-password 'withdraw) 60)
((acc 'secret-password 'deposit) 40)
((acc 'secret-password 'withdraw) 60)
((acc 'secret 'withdraw) 60)
((acc 'secret 'withdraw) 60)
((acc 'secret 'withdraw) 60)
((acc 'secret 'withdraw) 60)
((acc 'secret 'withdraw) 60)
((acc 'secret 'withdraw) 60)
((acc 'secret 'withdraw) 60)
((acc 'secret 'withdraw) 60)
((acc 'secret 'withdraw) 60)

view raw

s304.scm

hosted with ❤ by GitHub

 

Discover more from Gaurav Sharma's Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading