; initialize the balance in the account (define balance 1000) ; takes an amount, if there is enough balance ; in the account to cover the amount, then return ; the new balance (after withdraw is made). Otherwise, ; return "insufficient funds" (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "insufficient funds")) ; make-withdraw takes a balance as an argument ; it returns a function that expects an amount to be ; withdrawn as its argument -- resets the balance ; accordingly ; Think of this function as establishing a balance ; in an account -- and returning a procedure that ; implements withdraws (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "insufficient funds"))) (define checking-withdraw (make-withdraw 100)) (define savings-withdraw (make-withdraw 500)) ; make-account takes a balance and returns ; a procedure which is able to dispatch ; calls to withdraw or deposit both ; of which takes an amount and either withdraws ; or deposits the amount from the balance (define (make-account balance) ; takes an amount and sets balance to balance minus ; the amount if balance is high enough (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "insufficient funds")) ; takes an amount and increases balance by that amount (define (deposit amount) (set! balance (+ balance amount)) balance) ; dispatches either withdraw or deposit as appropriate (define (dispatch msg) (cond ((eq? msg 'withdraw) withdraw) ((eq? msg 'deposit) deposit) (else (error "You can't do that here:" msg)))) dispatch)