2015/11/06

SICP 問題 3.4

(define (make-account balance password)
  (let ((counter 0))
    (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)
      (set! counter (+ 1 counter))
      (if (>= counter 7)
          (coll-the-cop)
          "Incorrect password"))
    (define (call-the-cops)
      "110")
    (define (dispatch pass m)
      (if (eq? pass password)
          (cond ((eq? m 'withdraw)
                 (set! counter 0)
                 withdraw)
                ((eq? m 'deposit)
                 (set! counter 0)
                 deposit)
                (else (error "Unknown request: MAKE-ACCOUNT"
                             m)))
          login-error))
    dispatch))

© 2022 wat-aro