Add solution to exercise 3.4
This commit is contained in:
parent
6c31c289fa
commit
cc914f21a4
20
3_3.rkt
20
3_3.rkt
|
@ -1,6 +1,7 @@
|
|||
#lang sicp
|
||||
|
||||
(define (make-account balance account-password)
|
||||
(define bad-passwords 0)
|
||||
(define (withdraw amount)
|
||||
(if (>= balance amount)
|
||||
(begin (set! balance (- balance amount))
|
||||
|
@ -10,11 +11,20 @@
|
|||
(begin
|
||||
(set! balance (+ balance amount))
|
||||
balance))
|
||||
(define (call-the-cops)
|
||||
(error "Call the cops"))
|
||||
;;(let ((bad-passwords 0))
|
||||
(define (dispatch password m)
|
||||
(if (eq? password account-password)
|
||||
(cond ((eq? m 'withdraw) withdraw)
|
||||
((eq? m 'deposit) deposit)
|
||||
(else (error "Unknown request -- MAKE-ACCOUNT"
|
||||
m)))
|
||||
(error "Incorrect password")))
|
||||
(begin
|
||||
(set! bad-passwords 0)
|
||||
(cond ((eq? m 'withdraw) withdraw)
|
||||
((eq? m 'deposit) deposit)
|
||||
(else (error "Unknown request -- MAKE-ACCOUNT"
|
||||
m))))
|
||||
(begin
|
||||
(set! bad-passwords (+ bad-passwords 1))
|
||||
(if (> bad-passwords 7)
|
||||
(error "Call the cops")
|
||||
(error "Incorrect password")))))
|
||||
dispatch)
|
||||
|
|
Loading…
Reference in New Issue