hw8 - CS 61A -- Week 8 solutions HOMEWORK: 3.3 Accounts...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 61A -- Week 8 solutions HOMEWORK: 3.3 Accounts with passwords (define (make-account balance password) (define (withdraw amount) ; Starting here exactly as in p. 223 (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch pw m) ; Starting here different because of pw (cond ((not (eq? pw password)) (lambda (x) "Incorrect password")) ((eq? m 'withdraw) withdraw) ; Now the same again ((eq? m 'deposit) deposit) (else (error "Unknown request -- MAKE-ACCOUNT" m)))) dispatch) The big question here is why withdraw can get away with returning "Insufficient funds" while dispatch has to return this complicated (lambda (x) "Incorrect password") The answer is that ordinarily the result returned by withdraw is supposed to be a number, which is just printed. In case of an error, withdraw can return a string instead, and that string will just get printed. But dispatch is ordinarily supposed to return a PROCEDURE. In the example ((acc 'some-other-password 'deposit) 50) if dispatch just returned the string, it would be as if we'd typed ("Incorrect password" 50) which makes no sense. Instead this version is as if we typed ((lambda (x) "Incorrect password") 50) which does, as desired, print the string. A simpler solution would be to say (error "Incorrect password") because the ERROR primitive stops the computation and returns to toplevel after printing its argument(s). But you should understand the version above! 3.4 call-the-cops (define (make-account balance password) (define error-count 0) ; THIS LINE ADDED (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch pw m) (cond ((eq? pw password) ; REARRANGED STARTING HERE (set! error-count 0) (cond ((eq? m 'withdraw) withdraw) ((eq? m 'deposit) deposit) (else (error "Unknown request -- MAKE-ACCOUNT" m)) )) (else (set! error-count (+ error-count 1)) (if (> error-count 7) (call-the-cops)) (lambda (x) "Incorrect password") ))) dispatch) In this version, call-the-cops will be invoked before the dispatch procedure goes on to return the nameless procedure that will, eventually, be invoked and print the string "Incorrect password", so whatever call-the-cops prints will appear before that message. If you'd like it to appear instead of the string, change the last few lines to (lambda (x) (if (> error-count 7) (call-the-cops) "Incorrect password")) 3.7 Joint accounts What we want here is a new dispatch procedure that has access to the same environment frame containing the balance of the original account. You could imagine a complicated scheme in which we teach make-account's dispatch procedure a new message, make-joint, such that ((acc 'old-password 'make-joint) 'new-password) will return a new dispatch procedure in a new frame with its own password...
View Full Document

Page1 / 9

hw8 - CS 61A -- Week 8 solutions HOMEWORK: 3.3 Accounts...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online