Exercise3-2 <---> Exercise3-4

Exercise 3.3

Modify the make-account procedure so that it creates password-protected accounts. That is, make-account should take a symbol as an additional argument, as in

(define acc (make-account 100 'secret-password))

The resulting account object should process a request only if it is accompanied by the password with which the account was created, and should otherwise return a complaint:

((acc 'secret-password 'withdraw) 40)

60

((acc 'some-other-password 'deposit) 50)

"Incorrect password"


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


Scheme solution:

(define (make-pwd-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)
  (define (dispatch method pwd)
    (cond
     ((not (eq? pwd secret-pwd)) 
      (lambda (x) "Incorrect password"))
     ((eq? method 'withdraw) withdraw)
     ((eq? method 'deposit) deposit)))
  dispatch)

Exercise3-2 <---> Exercise3-4


Comments


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

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