sicp-ex-3.7


See sicp-ex-3.3-3.4 for the definition of make-account.

 (define (make-joint account old-pass new-pass) 
   (and (number? ((account old-pass 'withdraw) 0)) 
        (lambda (pass msg) 
          (if (eq? pass new-pass) 
              (account old-pass msg) 
              (account 'bad-pass 'foo))))) ;increment bad-passwords 

Alternative implementation:

 (define (call-the-cops) 
   (display "calling the cops\n")) 
  
 (define (password-protect password subject) 
   (let ((num-attempts 0)) 
     (lambda (provided-password msg) 
       (if (eq? provided-password password) 
           (begin (set! num-attempts 0) 
                  (subject msg)) 
           (begin (set! num-attempts (+ 1 num-attempts)) 
                  (when (>= num-attempts 7) 
                    (call-the-cops)) 
                  (lambda (arg . rest) "invalid password")))))) 
  
 (define (make-account password balance) 
   (define (withdraw amount) 
     (if (>= balance amount) 
         (begin (set! balance (- balance amount)) 
                balance) 
         "insufficient funds")) 
   (define (deposit amount) 
     (set! balance (+ balance amount)) 
     balance) 
   (define (dispatch msg) 
     (cond ((eq? msg 'withdraw) withdraw) 
           ((eq? msg 'deposit) deposit) 
           (else (error "Unknown request -- MAKE-ACCOUNT" 
                        msg)))) 
   (password-protect password dispatch)) 
  
 (define (make-joint-account account original-password password) 
   (define (dispatch msg) 
     (account original-password msg)) 
   (password-protect password dispatch)) 

<< Previous exercise (3.6) | Index | Next exercise (3.8) >>