-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-4.scm
48 lines (42 loc) · 1.45 KB
/
3-4.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#lang scheme
(require "modules/sicp/sicp.rkt")
(define (make-account balance secret)
(let ((attempts 0))
(define (withdraw amount)
(cond ((>= balance amount) (begin (set! balance (- balance amount))
balance))
(else "Insufficient funds")))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (call-the-cops)
(error "calling the cops"))
(define (invalid-attempt x)
(set! attempts (+ attempts 1))
(if (> attempts 7)
(call-the-cops)
"Wrong password – try again"))
(define (reset-attempts)
(set! attempts 0))
(define (dispatch m password)
(cond ((eq? secret password) (reset-attempts)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request - MAKE-ACCOUNT"
m))))
(else invalid-attempt)))
dispatch))
(define a (make-account 100 'qwerty))
(assert ((a 'withdraw 'qwerty) 40)
60)
(assert ((a 'deposit 'qwerty) 1000)
1060)
((a 'deposit 'qerty) 1000)
((a 'deposit 'qerty) 1000)
((a 'deposit 'qerty) 1000)
((a 'deposit 'qerty) 1000)
((a 'deposit 'qerty) 1000)
((a 'deposit 'qerty) 1000)
((a 'deposit 'qerty) 1000)
((a 'deposit 'qwerty) 1000)
((a 'deposit 'qerty) 1000)