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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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) | |