(wat-aro)

無職から有職者にランクアップしました

SICP 問題 3.3

(define (make-account balance password)
  (define (withdraw amount)
    (if (>= balance amount)
        (begin (set! balance (- balance amount))
               balance)
        "Insufficient funds"))
  (define (deposit amount)
    (set! balance (+ balance amount))
    balance)
  (define (login-error amount) "Incorrect password")
  (define (dispatch pass m)
    (if (eq? pass password)
        (cond ((eq? m 'withdraw) withdraw)
              ((eq? m 'deposit) deposit)
              (else (error "Unknown request: MAKE-ACCOUNT"
                           m)))
        login-error))
  dispatch)
gosh> (define acc (make-account 100 'secret-password))
acc
gosh> ((acc 'secret-password 'withdraw) 50)
50
gosh> ((acc 'some-password 'deposit) 40)
"Incorrect password"