Exercise 3.7. Consider the bank account objects created by make-account, with the password modification described in exercise 3.3. Suppose that our banking system requires the ability to make joint accounts. Define a procedure make-joint that accomplishes this. Make-joint should take three arguments. The first is a password-protected account. The second argument must match the password with which the account was defined in order for the make-joint operation to proceed. The third argument is a new password. Make-joint is to create an additional access to the original account using the new password. For example, if peter-acc is a bank account with password open-sesame, then
(define paul-acc
(make-joint peter-acc 'open-sesame 'rosebud))
will allow one to make transactions on peter-acc using the name paul-acc and the password rosebud. You may wish to modify your solution to exercise 3.3 to accommodate this new feature.
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 (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) | |
| (cond | |
| ((eq? m 'withdraw) withdraw) | |
| ((eq? m 'deposit) deposit) | |
| (else (error "Unknown request — MAKE-ACCOUNT" | |
| m)))) | |
| (else (lambda (x) "Incorrect Password")))) | |
| dispatch) | |
| (define acc (make-account 100 'secret-password)) | |
| (define (make-joint old-account old-password new-password) | |
| (define (dispatch password m) | |
| (cond | |
| ((eq? password new-password) | |
| (old-account old-password m)) | |
| (else (old-account 'badpassword m)))) | |
| (if (number? ((old-account old-password 'withdraw) 0)) | |
| dispatch | |
| (lambda (x) "Incorrect 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) | |
| (make-joint acc 'secret-password 'new-secret) | |
| (define paul-acc | |
| (make-joint acc 'secret-password 'rosebud)) | |
| ((paul-acc 'rosebud 'withdraw) 10) | |