; Note the password variable added from the original
; make-account that is given in the book.
(define (make-account balance pwd)
(define (withdraw amount)
(if (> balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch m pass)
(cond ((not (eq? pass pwd)) (error "WRONG PASSWORD"))
((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT"
m))))
dispatch)
; (define paul-acc
; (make-joint peter-acc 'open-sesame 'rosebud))
; Note: If the old password is incorrect, then the dispatch procedure in make-account
; produces an error (see above). If it doesn't produce an error, then the lambda,
; below, is returned. It takes a message and the new password and:
; *** Makes sure the new password is correct
; *** Uses the old account and old password to pass the message to get the job done
; Ain't Scheme grand? Of course it is!
(define (make-joint existing-acc existing-pass new-pass)
(existing-acc 'deposit existing-pass) ; will produce error if wrong existing-pass
(lambda (msg pass)
(cond ((eq? pass new-pass) (existing-acc msg existing-pass))
(else (error "WRONG PASSWORD")))))
; Examples:
> (define jaxon (make-account 1000 'hi))
> ((jaxon 'deposit 'hi) 70)
1070
> ((jaxon 'deposit 'him) 70)
. WRONG PASSWORD
> (define neil (make-joint jaxon 'hi 'there))
> ((neil 'withdraw 'there) 850)
220
> ((neil 'withdraw 'the) 10)
. WRONG PASSWORD