Take Home Quiz #6

Due: At the start of lecture, Wednesday, 1 August 2007
Name
 
Student ID
 
Score            / 20

  1. (10 pts.) Recall from your reading (Section 3.1.1) the make-account procedure:

    ;; A procedure that returns a "bank-account object" with a specified
    ;; initial balance:
    (define (make-account 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 m)
        (cond ((eq? m 'withdraw) withdraw)
              ((eq? m 'deposit) deposit)
              (else (error "Unknown request -- MAKE-ACCOUNT" m))))
      dispatch)
    

    Modify make-account 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"
    









  2. (10 pts.) Scheme leaves unspecified the order in which the subexpressions should be evaluated (e.g., left to right or right to left). Because Scheme has assignment, the order in which the arguments to a procedure are evaluated can make a difference to the result. Define a simple procedure f such that evaluating

    (+ (f 0) (f 1))
    

    will return 0 if the arguments to + are evaluated from left to right but will return 1 if the arguments are evaluated from right to left.