-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-7.scm
77 lines (65 loc) · 2.39 KB
/
3-7.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#lang scheme
(require "modules/sicp/sicp.rkt")
(provide make-account)
(define (make-account balance secret)
(define (dispatch m secret password new-password)
(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))
(cond ((eq? secret password) (reset-attempts)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
((eq? m 'make-joint) (lambda (m password)
(dispatch m new-password password nil)))
(else (error "Unknown request - MAKE-ACCOUNT"
m))))
(else invalid-attempt))))
(lambda (m . passwords)
(dispatch m secret (car passwords) (if (null? (cdr passwords))
nil
(cadr passwords)))))
(define (make-joint account orig-pass joint-pass)
(account 'make-joint orig-pass joint-pass))
(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)
(define b (make-joint a 'qwerty 'abc))
((a 'deposit 'qwerty) 1)
(assert ((b 'deposit 'abc) 100)
2161)
(assert ((b 'deposit 'admin) 100)
"Wrong password – try again")
; tests that number of attempts is not shared
((a 'deposit 'nan) 100)
((a 'deposit 'nan) 100)
((a 'deposit 'nan) 100)
((b 'deposit 'nan) 100)
((b 'deposit 'nan) 100)
((b 'deposit 'nan) 100)
((b 'deposit 'nan) 100)
((b 'deposit 'nan) 100)