オブジェクト指向風
(define (make-account balance)
  (define (withdraw amount)       ;  引き出し
    (if (>= balance amount)
        (begin (set! balance (- balance amount))
               balance)
        "Insufficient funds"))
  (define (deposit amount)        ;  預金
    (set! balance (+ balance amount))
    balance)
  (define (dispatch m amount)
    (cond ((eq? m 'withdraw) (withdraw amount))
          ((eq? m 'deposit) (deposit amount))
          (else "Unknown request")))
  dispatch)
(define acc (make-account 10000))
> (acc 'withdraw 5000)
5000
> (acc 'withdraw 6000)
"Insufficient funds"
> (acc 'deposit 4000)
9000
> (acc 'withdraw 6000)
3000

一覧 前へ 次へ