Exercise3-3 <---> Exercise3-5

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.


Ru: Русский текст упражнения


Scheme solution:

(define (make-secure-account balance secret-pwd)
  (define (withdraw amount)
    (if (>= balance amount)
        (begin
          (set! balance (- balance amount))
          balance)
        'insufficient-funds))
  (define (deposit amount)
    (set! balance (+ balance amount))
    balance)
  (let ((incorrect-pwd-count 0))
    (lambda (method pwd)
      (if (eq? pwd secret-pwd)
          (begin
            (set! incorrect-pwd-count 0)
            (cond
             ((eq? method 'withdraw) withdraw)
             ((eq? method 'deposit) deposit)))
          (begin
            (set! incorrect-pwd-count 
                  (+ 1 incorrect-pwd-count))
            (if (> incorrect-pwd-count 6)
                (call-the-cops)
                (lambda (x) 
                 'incorrect-password)))))))

(define (call-the-cops)
  (error 'cops-called))

Exercise3-3 <---> Exercise3-5


Comments


:) :)) :( ;) :\ |) X-( B) Markup

Exercise3-4 (last edited 2008-05-11 11:36:11 by localhost)