2008. 8. 6.

연습문제 3.4

(define (make-account balance password)
  (let ((invalid-count 0))
    (define (withdraw amount)
      (if (>= balance amount)
          (begin (set! balance (- balance amount))
                 balance)
          "Insufficient funds"))
    (define (call-the-cops msg)
      (error "Invalid password" msg))
    (define (deposit amount)
      (set! balance (+ balance amount))
      balance)
    (define (dispatch pass m)
      (cond ((eq? password pass)
             (cond ((eq? m 'withdraw) withdraw)
                   ((eq? m 'deposit) deposit)
                   (else (error "Unknown request -- MAKE-ACCOUNT" m))))
            (else
             (begin
               (set! invalid-count(+ invalid-count 1))
               (if (> invalid-count 7)
                   (call-the-cops "help")
                   (lambda (x) "Bad Password"))))))
    dispatch))

댓글 없음:

댓글 쓰기