#lang racket (provide make-account) ;; ---------------------------------------------------------------------- ;; This account object responds to messages on its own! ;; ---------------------------------------------------------------------- (define make-account ;; Defines a procedure that creates (lambda (balance) ;; a closure around balance. (let ((withdraw ;; These procedures share (lambda (amount) ;; the balance variable. (if (>= balance amount) (begin (set! balance (- balance amount)) balance) (error "Insufficient funds" balance)))) (deposit (lambda (amount) (set! balance (+ balance amount)) balance)) (get-balance (lambda () balance))) (lambda (transaction . args) ;; This procedure *applies* (case transaction ;; the "method" corresponding ('withdraw ;; to the transaction to its (apply withdraw args)) ;; arguments. ('deposit (apply deposit args)) ('balance (apply get-balance args)) (else (error "Unknown request -- ACCOUNT" transaction))))))) ;; ---------------------------------------------------------------------- ;; How can we make the repeated (apply args) go away? ;; ----------------------------------------------------------------------