-
Notifications
You must be signed in to change notification settings - Fork 10
/
ex-3.4.scm
48 lines (43 loc) · 1.59 KB
/
ex-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
;;; Exercise 3.4. Modify the make-account procedure of exercise 3.3 by adding
;;; another local state variable so that, if an account is accessed more than
;;; seven consecutive times with an incorrect password, it invokes the
;;; procedure call-the-cops.
(load "./ex-3.3.scm")
(define make-account
(let ([%make-account make-account])
(lambda initial-args
(let ([acc (apply %make-account initial-args)]
[failed-count 0])
(lambda dispatched-args
(let* ([m (apply acc dispatched-args)]
[r (m 0)])
(if (equal? r "Incorrect password")
(begin
(set! failed-count (+ failed-count 1))
(if (> failed-count 7)
(call-the-cops)
m))
(begin
(set! failed-count 0)
m))))))))
(define (call-the-cops)
(error "wooooop wooooop"))
(define acc (make-account 100 'secret-password))
(print ((acc 'secret-password 'withdraw) 10))
;=> 90
(print ((acc 'wrong-password 'withdraw) 10))
;=> "Incorrect password"
(print ((acc 'wrong-password 'withdraw) 10))
;=> "Incorrect password"
(print ((acc 'wrong-password 'withdraw) 10))
;=> "Incorrect password"
(print ((acc 'wrong-password 'withdraw) 10))
;=> "Incorrect password"
(print ((acc 'wrong-password 'withdraw) 10))
;=> "Incorrect password"
(print ((acc 'wrong-password 'withdraw) 10))
;=> "Incorrect password"
(print ((acc 'wrong-password 'withdraw) 10))
;=> "Incorrect password"
(print ((acc 'wrong-password 'withdraw) 10))
;=> error: wooooop wooooop