-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_2_part14.bak
251 lines (187 loc) · 6.02 KB
/
chapter_2_part14.bak
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#lang sicp
;;消息传递
;exericse-2.75
(define (make-from-real-imag r a)
(define (dispatch op)
(cond ((eq? op 'real-part)
(* r (cos a)))
((eq? op 'imag-part)
(* r (sin a)))
((eq? op 'magnitude)
r)
((eq? op 'angle)
a)
(else
(error "Unkonw op -- MAKE-FROM-MAG-ANG" op))))
dispatch)
;exercise-2.76
;https://sicp.readthedocs.io/en/latest/chp2/76.html
;2.5 带有通用型操作的系统
(define (make-table)
(let ((local-table (list '*table*)))
(define (lookup key-1 key-2)
(let ((subtable (assoc key-1 (cdr local-table))))
(if subtable
(let ((record (assoc key-2 (cdr subtable))))
(if record
(cdr record)
false))
false)))
(define (insert! key-1 key-2 value)
(let ((subtable (assoc key-1 (cdr local-table))))
(if subtable
(let ((record (assoc 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))
(define operation-table (make-table))
(define get (operation-table 'lookup-proc))
(define put (operation-table 'insert-proc!))
(define (attach-tag type-tag contents)
(cons type-tag contents))
(define (type-tag datum)
(if (pair? datum)
(car datum)
(error "Bad tagged datum TYPE-TAG --" datum)))
(define (contents datum)
(if (pair? datum)
(cdr datum)
(error "Bad tagged datum CONTENTS --" datum)))
(define (apply-generic op . args)
(let ((type-tags (map type-tag args)))
(write type-tags)
(let ((proc (get op type-tags)))
(if proc
(apply proc (map contents args))
(error
"No mathod for these types --- APPLY-GENERIC"
(list op type-tags))))))
;通用型算术运算
(define (add x y)
(apply-generic 'add x y))
(define (sub x y)
(apply-generic 'sub x y))
(define (mul x y)
(apply-generic 'mul x y))
(define (div x y)
(apply-generic 'div x y))
(define (install-scheme-number-package)
(define (tag x)
(attach-tag 'scheme-number x))
(put 'add '(scheme-number scheme-number)
(lambda (x y) (tag (+ x y))))
(put 'sub '(scheme-number scheme-number)
(lambda (x y) (tag (- x y))))
(put 'mul '(scheme-number scheme-number)
(lambda (x y) (tag (* x y))))
(put 'div '(scheme-number scheme-number)
(lambda (x y) (tag (/ x y))))
(put 'make 'scheme-number
(lambda (x y) (tag x)))
'done)
(define (make-scheme-number n)
((get 'make 'scheme-number) n))
;有理数程序包
(define (install-rational-package)
;;internal-procedures
(define (number x)
(car x))
(define (denom x)
(cdr x))
(define (make-rat n d)
(let ((g (gcd n d)))
(cons (/ n g) (/ d g))))
(define (add-rat x y)
(make-rat (+ (* (number x) (denom y))
(* (number y) (denom x)))
(* (denom x) (denom y))))
(define (sub-rat x y)
(make-rat (- (* (number x) (denom y))
(* (number y) (denom x)))
(* (denom x) (denom y))))
(define (mul-rat x y)
(make-rat (* (number x) (number y))
(* (denom y) (denom x))))
(define (div-rat x y)
(make-rat (* (number x) (denom y))
(* (denom x) (number y))))
;;inrerface to rest of the system
(define (tag x)
(attach-tag 'rantional x))
(put 'add '(rantional rantional)
(lambda (x y)
(tag (add-rat x y))))
(put 'sub '(rantional rantional)
(lambda (x y)
(tag (sub-rat x y))))
(put 'mul '(rantional rantional)
(lambda (x y)
(tag (mul-rat x y))))
(put 'div '(rantional rantional)
(lambda (x y)
(tag (div-rat x y))))
(put 'make 'rantional
(lambda (n d)
(tag (make-rat n d))))
'done)
(define (make-rational n d)
((get 'make 'rational) n d))
;;复数程序包
(define (install-complex-package)
;;imported procedures from rectangular and polar packages
(define (make-from-real-imag x y)
((get 'make-from-real-imag 'rectangular) x y))
(define (make-from-mag-ang r a)
((get 'make-from-mag-ang 'polar) r a))
;; internal procedures
(define (add-complex z1 z2)
(make-from-real-imag (+ (real-part z1) (real-part z2))
(+ (imag-part z1) (imag-part z2))))
(define (sub-complex z1 z2)
(make-from-real-imag (- (real-part z1) (real-part z2))
(- (imag-part z1) (imag-part z2))))
(define (mul-complex z1 z2)
(make-from-mag-ang (* (magnitude z1) (magnitude z2))
(+ (angle z1) (angle z2))))
(define (div-complex z1 z2)
(make-from-mag-ang (/ (magnitude z1) (magnitude z2))
(- (angle z1) (angle z2))))
;;interface to rest of system
(define (tag z)
(attach-tag 'complex z))
(put 'add '(complex complex)
(lambda (z1 z2)
(tag (add-complex z1 z2))))
(put 'sub '(complex complex)
(lambda (z1 z2)
(tag (sub-complex z1 z2))))
(put 'mul '(complex complex)
(lambda (z1 z2)
(tag (mul-complex z1 z2))))
(put 'div '(complex complex)
(lambda (z1 z2)
(tag (div-complex z1 z2))))
(put 'make-from-real-imag 'complex
(lambda (x y)
(tag (make-from-real-imag x y))))
(put 'make-from-mag-ang 'complex
(lambda (r a)
(tag (make-from-mag-ang r a))))
'done)
(define (make-complex-from-real-imag x y)
((get 'make-from-real-imag 'complex) x y))
(define (make-complex-from-mag-ang r a)
((get 'make-from-mag-ang 'complex) r a))
;;exercise-2.77