3.3 Exercise 3.3
I shouldn’t need to mention that handling plaintext passwords is a bad idea.
There is one subtlety in this code: Because the inner dispatch procedure is always expected to return a procedure, we have to wrap the sending of the "Incorrect password" message in a dummy procedure. I have done this in a rather terse way.
(define (make-account balance password) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (if (>= amount 0) (begin (set! balance (+ balance amount)) balance) "Must deposit a non-negative amount")) (define (dispatch p m) (if (eq? p password) (cond ((eq? m 'withdraw) withdraw) ((eq? m 'deposit) deposit) (else (error "Unknown request -- MAKE-ACCOUNT" m))) (lambda args "Incorrect password"))) dispatch)