-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex311.lisp
181 lines (149 loc) · 4.77 KB
/
ex311.lisp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
; Answers for 3-1-1
(defvar *balance* 100)
(defun withdraw (amount)
(if (>= *balance* amount)
(setf *balance* (- *balance* amount))
"Insufficient funds"))
(let ((balance 100))
(defun new-withdraw (amount)
(if (>= balance amount)
(setf balance (- balance amount))
"Insufficient funds")))
(defun make-withdraw (balance)
(lambda (amount)
(if (>= balance amount)
(setf balance (- balance amount))
"Insufficient funds")))
(defvar w1 (make-withdraw 100))
(defvar w2 (make-withdraw 200))
;; CL-USER> (funcall w1 10)
;; 90
;; CL-USER> (funcall w2 1)
;; 199
;; CL-USER> (funcall w1 10)
;; 80
(defun make-account (balance)
(labels ((withdraw (amount)
(if (>= balance amount)
(decf balance amount)
"Insufficient funds"))
(deposit (amount)
(incf balance amount))
(dispatch (m)
(cond ((eq m 'withdraw) #'withdraw)
((eq m 'deposit) #'deposit)
(t (error "Unknown request -- MAKE-ACCOUNT ~a" m)))))
#'dispatch))
(defvar acc (make-account 100))
;; CL-USER> (funcall (funcall acc 'withdraw) 10)
;; 90
;; CL-USER> (funcall (funcall acc 'withdraw) 100)
;; "Insufficient funds"
;; CL-USER> (funcall (funcall acc 'deposit) 400)
;; 490
;; CL-USER> (funcall (funcall acc 'withdraw) 50)
;; 440
; Scheme is nicer :(
; Exercise 3.1
(defun make-accumulator (sum)
(lambda (n)
(incf sum n)))
(defvar a (make-accumulator 5))
;; CL-USER> (funcall a 10)
;; 15
;; CL-USER> (funcall a 10)
;; 25
; Exercise 3.2
(defun make-monitored (f)
(let ((times-called 0))
(lambda (x)
(labels ((exec ()
(incf times-called)
(funcall f x)))
(if (symbolp x)
(case x
(how-many-calls? times-called)
(reset-count (setf times-called 0))
(otherwise (exec)))
(exec))))))
(defvar s (make-monitored #'sqrt))
;; CL-USER> (funcall s 100)
;; 10.0
;; CL-USER> (funcall s 'how-many-calls?)
;; 1
;; CL-USER> (funcall s 100)
;; 10.0
;; CL-USER> (funcall s 100)
;; 10.0
;; CL-USER> (funcall s 'how-many-calls?)
;; 3
;; CL-USER> (funcall s 'reset-count)
;; 0
;; CL-USER> (funcall s 100)
;; 10.0
;; CL-USER> (funcall s 'how-many-calls?)
;; 1
; Exercise 3.3
(defun make-account-2 (balance password)
(labels ((withdraw (amount)
(if (>= balance amount)
(decf balance amount)
"Insufficient funds"))
(deposit (amount)
(incf balance amount))
(dispatch (pw-attempt m)
(if (eq password pw-attempt)
(cond ((eq m 'withdraw) #'withdraw)
((eq m 'deposit) #'deposit)
(t (error "Unknown request -- MAKE-ACCOUNT ~a" m)))
(lambda (x) "Incorrect password"))))
#'dispatch))
(defvar acc-2 (make-account-2 100 'checkers))
;; CL-USER> (funcall (funcall acc-2 'draughts 'withdraw) 10)
;; "Incorrect password"
;; CL-USER> (funcall (funcall acc-2 'checkers 'withdraw) 10)
;; 90
; Exercise 3.4
(defun call-the-cops ()
(lambda (x)
(format t "--o o--")))
(defun make-account-3 (balance password)
(let ((incorrect-attempts 0))
(labels ((withdraw (amount)
(if (>= balance amount)
(decf balance amount)
"Insufficient funds"))
(deposit (amount)
(incf balance amount))
(dispatch (pw-attempt m)
(if (eq password pw-attempt)
(cond ((eq m 'withdraw) #'withdraw)
((eq m 'deposit) #'deposit)
(t (error "Unknown request -- MAKE-ACCOUNT ~a" m)))
(if (> incorrect-attempts 7)
(call-the-cops)
(let nil
(incf incorrect-attempts)
(lambda (x) "Incorrect password"))))))
#'dispatch)))
(defvar acc-3 (make-account-3 100 'checkers))
;; CL-USER> (funcall (funcall acc-3 'checkers 'withdraw) 10)
;; 90
;; CL-USER> (funcall (funcall acc-3 'draughts 'withdraw) 10)
;; "Incorrect password"
;; CL-USER> (funcall (funcall acc-3 'draughts 'withdraw) 10)
;; "Incorrect password"
;; CL-USER> (funcall (funcall acc-3 'draughts 'withdraw) 10)
;; "Incorrect password"
;; CL-USER> (funcall (funcall acc-3 'draughts 'withdraw) 10)
;; "Incorrect password"
;; CL-USER> (funcall (funcall acc-3 'draughts 'withdraw) 10)
;; "Incorrect password"
;; CL-USER> (funcall (funcall acc-3 'draughts 'withdraw) 10)
;; "Incorrect password"
;; CL-USER> (funcall (funcall acc-3 'draughts 'withdraw) 10)
;; "Incorrect password"
;; CL-USER> (funcall (funcall acc-3 'draughts 'withdraw) 10)
;; "Incorrect password"
;; CL-USER> (funcall (funcall acc-3 'draughts 'withdraw) 10)
;; --o o--