-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc3.lisp
More file actions
145 lines (128 loc) · 3.6 KB
/
c3.lisp
File metadata and controls
145 lines (128 loc) · 3.6 KB
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
#lang racket
(define (make-monitor f)
(let ((count 0))
(lambda (x)
(cond ((eq? x 'how-many-calls?) count)
((eq? x 'reset-count) (set! count 0))
(else (begin (set! count (+ count 1))
(f x)))))))
(define s (make-monitor sqrt))
(s 100)
(s 49)
(s 'how-many-calls?)
(define (make-accumulator x)
(lambda (y)
(begin (set! x (+ x y))
x)))
(define acc (make-accumulator 1))
(acc 2)
(define (make-account balance original-pw)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch input-pw msg)
(cond ((eq? input-pw original-pw)
(cond ((eq? msg 'withdraw) withdraw)
((eq? msg 'deposit) deposit)
(else (error "Unknown function" msg))))
(else (display "Incorrect password"))))
dispatch)
(define account1 (make-account 100 'passwd))
((account1 'passwd 'withdraw) 50) ; 50
;((account1 'wrong-passwd 'deposit) 100)
(define (make-account1 balance ori-pw)
(let ((pw-err-cnt 0))
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (call-the-cops) (error "call-the-cops"))
(define (dispatch input-pw func)
(if (eq? input-pw ori-pw)
(cond ((eq? func 'withdraw) withdraw)
((eq? func 'deposit) deposit)
(else (error "Unknown function")))
(begin (set! pw-err-cnt (+ 1 pw-err-cnt))
(if (> pw-err-cnt 7)
(call-the-cops)
"Wrong passwd"))))
dispatch))
(define account2 (make-account1 100 'passwd))
((account2 'passwd 'withdraw) 50)
((account2 'passwd 'withdraw) 80)
((account2 'passwd 'deposit) 200)
;((account2 'wrong-pw 'deposit) 30)
(define (make-joint acc ori-pw new-pw)
(lambda (input-pw func)
(if (eq? new-pw input-pw)
(acc ori-pw func)
(error "Wrong password"))))
(define account3 (make-joint account2 'passwd 'new-passwd))
((account3 'new-passwd 'deposit) 100)
;((account3 'passwd 'withdraw) 100)
(define (f x)
(let ((call-count 0)
(return-val 0))
(begin (+ call-count 1)
(if (= call-count 1)
(if (= x 0)
(set! return-val 0)
(set! return-val 0.5))
'())
return-val)))
(+ (f 0) (f 1))
(define (last-pair x)
(if (null? (mcdr x))
x
(last-pair (mcdr x))))
(require scheme/mpair)
(define (append! x y)
(set-mcdr! (last-pair x) y)
x)
(define x (mlist 'a 'b))
(define y (mlist 'c 'd))
(define z (mappend x y))
(mcdr x)
(define w (append! x y))
(mcdr x)
(define (assoc-old key records)
(cond ((null? records) false)
((equal? key (caar records)) (car records))
(else (assoc-old key (cdr records)))))
(define (make-table-old)
(let ((local-table (list '*table*)))
(define (lookup key-1 key-2)
(let ((subtable (assoc-old key-1 (cdr local-table))))
(if subtable
(let ((record (assoc-old key-2 (cdr subtable))))
(if record
(cdr record)
false))
false)))
(define (insert! key-1 key-2 value)
(let ((subtable (assoc-old key-1 (cdr local-table))))
(if subtable
(let ((record (assoc-old key-2 (cdr subtable))))
(if record
(set-cdr! record value)
(set-cdr! subtable
(cons (cons key-2 value)
(cdr subtable)))))
(set-cdr! local-table
(cons (list key-1 (cons key-2 value))
(cdr local-table)))))
'ok)
(define (dispatch m)
(cond ((eq? m 'lookup-proc) lookup)
((eq? m 'insert-proc) insert!)
(else (error "Unknown operation -- TABLE" m))))
dispatch))