#lang racket (provide make-account) ;; ---------------------------------------------------------------------- ;; Another way to implement message passing. Our method selector is ;; created inside a let that defines the methods as local functions. ;; ---------------------------------------------------------------------- (define make-account ;; Creates a procedure that creates a (lambda (balance) ;; closure around balance. All of the (let ((withdraw ;; procedures share the balance variable. (lambda (amount) (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)) (error (lambda args (error "Unknown request -- ACCOUNT")))) (lambda (transaction) (case transaction ('withdraw withdraw) ('deposit deposit) ('balance get-balance) (else error)))))) ;; ----------------------------------------------------------------------