-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch3.1.1-suguni.rkt
215 lines (174 loc) · 5.62 KB
/
ch3.1.1-suguni.rkt
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#lang racket
;; 3.1 Assignment & Local state
;; 3.1.1 local state variable
;; p288
(define balance 100)
(define (withdraw amount)
(begin
(if (>= balance amount)
(begin
(set! balance (- balance amount)) ;; set!은 명령이다.
balance)
"Insufficient funds")))
;; [???]
;; set! 으로 하지말고 define 하면 어떻게 되는가?
;; (define x 100)
;; (define x (- x 50))
;; 이것도 덮어쓰기 아닌가?
;;
;; =>
;; ...
;; (begin
;; (set! balance (- balance amount))
;; balance)
;; ...
;; define: not allowed in an expression context 에러 난다.
;; begin
;; 아래랑 마찬가지다. 제일 마지막 식을 반환한다.
;; (define (f a)
;; (define (g x) (+ x 1))
;; (define (h y) (+ (g 5) 3))
;; (h (* a 2)))
;; 위 식은 아래와 동일하다.
;; (define (f a)
;; (begin
;; (define (g x) (+ x 1))
;; (define (h y) (+ (g 5) 3))
;; (h (* a 2))))
;; p289
;; 문제점: balance가 밖으로 드러나 있다.
;; p290 - 갇힌 변수로 만들기
;; 문제점: 처음에 무조건 100이 된다. - balance를 인자로 받아서 설정할 수 있게 만들어야 한다.
(define new-withdraw
(let ((balance 100))
(lambda (amount)
(if (>= balance amount)
(begin
(set! balance (- balance amount))
balance)
"Insufficient funds"))))
;; p291
(define (make-withdraw balance)
(lambda (amount)
(if (>= balance amount)
(begin
(set! balance (- balance amount))
balance)
"Insufficient funds")))
(define w1 (make-withdraw 100))
(define w2 (make-withdraw 100))
;; balance로 인해 갇힌 환경이 생기고, 내부 lambda 프로시저에서만 balance를 이용할 수 있다.
;; p292
(define (make-account balance)
(define (withdraw amount)
(if (>= balance amount)
(begin
(set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
;; message passing
(define (dispatch m)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT" m))))
dispatch)
;; ex 3.1 - make-accumulator 만들기
(define (make-accumulator init)
(let ((items (list init)))
(lambda (v)
(set! items (cons v items))
(apply + items))))
(define A (make-accumulator 5))
;; ex 3.2 - make-monitored 만들기
(define (make-monitored f)
(let ((c 0))
(lambda (m)
(cond ((eq? m 'how-many-calls?) c)
((eq? m 'reset-count) (begin (set! c 0) 0))
(else (begin
(set! c (+ c 1))
(f m)))))))
(define s (make-monitored sqrt))
;; (s 100) ;; => 10
;; (s 'how-many-calls?) ;; => 1
;; ex 3.3 - 암호걸린 make-account
(define (make-account-pwd balance pwd)
(define (withdraw amount)
(if (>= balance amount)
(begin
(set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch in-pwd m)
(if (eq? pwd in-pwd)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT" m)))
;; "Incorrect password"를 답하는게 아님.
;;(error "Incorrect password" in-pwd)))
;; 무조건 "Incorrect password"를 답하는 프로시저를 반환함
(lambda (v) "Incorrect password")))
dispatch)
(define acc1 (make-account-pwd 100 'secret-password))
(define (make-account-pwd-1 balance pwd)
(lambda (in-pwd m)
(define (withdraw amount)
(if (eq? pwd in-pwd)
(if (>= balance amount)
(begin
(set! balance (- balance amount))
balance)
"Insufficient funds")
"Incorrect Password"))
(define (deposit amount)
(if (eq? pwd in-pwd)
(begin
(set! balance (+ balance amount))
balance)
"Incorrect Password"))
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT" m))))
;; "Incorrect password"를 답하는게 아님.
;;(error "Incorrect password" in-pwd)))
;; 무조건 "Incorrect password"를 답하는 프로시저를 반환함
;;(lambda (v) "Incorrect password")))
)
;; ex 3.4 - 암호 + 경찰불러 make-account
(define (make-account-pwd-cops balance pwd)
(define (call-the-cops) "POLICE")
(let ((incorrect-count 0))
(define (withdraw amount)
(if (>= balance amount)
(begin
(set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch in-pwd m)
(if (eq? pwd in-pwd)
(begin
;; 성공하면 reset count
(set! incorrect-count 0)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT" m))))
(begin
;; 실패하면 count++
(set! incorrect-count (+ incorrect-count 1))
(lambda (v)
;; count가 7초과면 call-the-cops 호출
(if (> incorrect-count 7)
(call-the-cops)
;; "Incorrect password")))))
(list "Incorrect password" incorrect-count))))))
dispatch))
(define accp1 (make-account-pwd-cops 100 's1))