Log in Page Discussion History Go to the site toolbox


From BluWiki

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.

(define (call-the-cops)
  (display "Forgot your password feature, Ouch")(newline))
(define (make-account balance password)  
  (let ((auth-token (sha-digest password))
        (failed-pass-count 0))
    (define (good? pwd)
      (if (equal? (sha-digest pwd) auth-token)
          #t #f))
    (define (bad-password-message)
      (set! failed-pass-count (+ failed-pass-count 1))
      (if (>= failed-pass-count 7)
      "Incorrect password")
    (define (withdraw amount) 
      (if (>= balance amount) 
          (begin (set! balance (- balance amount)) 
          "Insufficient funds"))  
    (define (deposit amount)
        (set! balance (+ balance amount)) 
    (define (dispatch pwd m) 
      (if (not (good? pwd))
          (lambda (arg)
          (cond ((eq? m 'withdraw) withdraw) 
                ((eq? m 'deposit) deposit) 
                (else (error "Unknown request -- MAKE-ACCOUNT" 
    (if (symbol? password)
        dispatch (error "Error, password must be a symbol"))))
(define a (make-account 3 'unforgettable))
((a 'doh 'widthdraw) 1)
((a 'doh 'widthdraw) 1)
((a 'doh 'widthdraw) 1)
((a 'doh 'widthdraw) 1)
((a 'doh 'widthdraw) 1)
((a 'doh 'widthdraw) 1)
((a 'doh 'widthdraw) 1)

Site Toolbox:

Personal tools
GNU Free Documentation License 1.2
This page was last modified on 28 September 2007, at 16:19.
Disclaimers - About BluWiki