week9 - CS 61A - Week 9 solutions LAB ACTIVITIES: 1. Use a...

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

View Full Document Right Arrow Icon
CS 61A -- Week 9 solutions LAB ACTIVITIES: 1. Use a LET to keep both initial and current balance (define (make-account init-amount) (let ((BALANCE INIT-AMOUNT)) ;;; This is the change. (define (withdraw amount) (set! balance (- balance amount)) balance) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch msg) (cond ((eq? msg 'withdraw) withdraw) ((eq? msg 'deposit) deposit))) dispatch)) 2. Add messages to read those variables. (define (make-account init-amount) (let ((balance init-amount)) (define (withdraw amount) (set! balance (- balance amount)) balance) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch msg) (cond ((eq? msg 'withdraw) withdraw) ((eq? msg 'deposit) deposit) ((EQ? MSG 'BALANCE) BALANCE) ;; two lines added here ((EQ? MSG 'INIT-BALANCE) INIT-AMOUNT))) dispatch)) 3. Add transaction history. (define (make-account init-amount) (let ((balance init-amount) (TRANSACTIONS '())) ;; add local state var (define (withdraw amount) (SET! TRANSACTIONS (APPEND TRANSACTIONS (LIST (LIST 'WITHDRAW AMOUNT)))) ;; update (set! balance (- balance amount)) balance) (define (deposit amount) (SET! TRANSACTIONS (APPEND TRANSACTIONS (LIST (LIST 'DEPOSIT AMOUNT)))) ;; update (set! balance (+ balance amount)) balance) (define (dispatch msg) (cond ((eq? msg 'withdraw) withdraw) ((eq? msg 'deposit) deposit) ((eq? msg 'balance) balance) ((eq? msg 'init-balance) init-amount) ((EQ? MSG 'TRANSACTIONS) TRANSACTIONS))) ;; message to examine it dispatch))
Background image of page 1

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

View Full DocumentRight Arrow Icon
4. Why substitution doesn't work. (plus1 5) becomes (set! 5 (+ 5 1)) 5 The first line (the SET!) is syntactically wrong; "5" isn't a variable and it doesn't make sense to substitute into an unevaluated part of a special form. The second line (returning the value 5) is syntactically okay but gives the wrong answer; it ignores the fact that the SET! was supposed to change the result. 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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This homework help was uploaded on 04/09/2008 for the course CS 61A taught by Professor Harvey during the Spring '08 term at University of California, Berkeley.

Page1 / 10

week9 - CS 61A - Week 9 solutions LAB ACTIVITIES: 1. Use a...

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