-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.scm
314 lines (242 loc) · 7.38 KB
/
tests.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#lang scheme
(require "schemer.scm")
; micro unit testing -____-
(define (assert msg test)
(if (not test) (error msg) (displayln (string-append "ok: " msg))))
; print helpers
(define (msg before message after) (displayln
(string-append before message after)))
(define (block b) (msg "-- " b " --"))
(define (title t) (displayln "") (msg "** " t " **"))
(define (body b) (msg " " b ""))
; sample data
(define xs-null '())
(define xs-five (list 'one 'two 'three 'four 'five))
(define xs-five-mix (list 'one 1 'two 2 3 4 'three 'four 5 'five))
(define xs-five-nested
(list
(list 'one 'one-point-five)
(list 'two)
(list 'three)
(list 'four)
(list 'five)))
(define food
(list 'chips 'fish 'cream 'fish 'potato 'hummus 'leek 'chips))
(define tup-one (list 1 2))
(define tup-two (list (list 1 2) (list 3 4)))
; atom?
(assert "is atom?" (atom? 'test-atom))
; quick list test
(block "List test")
(title "The law of car")
(body "The primitive car is defined")
(body "only for non-empty lists.")
(car xs-five)
(title "The law of cdr")
(body "The primitive cdr is defined only for")
(body "not-empty lists. The cdr of any non-empty")
(body "list is always another list.")
(cdr xs-five)
(car (cdr (cdr xs-five)))
(title "The law of cons")
(body "The primitive cons takes two arguments.")
(body "The second argument to cons must be a list.")
(body "The result is a list.")
(cons 'first xs-five)
(cons 'firster xs-five)
(cons 'one (car xs-five))
(cons 'new-one (cdr xs-five))
(title "The law of null?")
(body "The primitive null? is defined only for lists.")
(null? xs-null)
(null? xs-five)
(title "The law of eq?")
(body "The primitive eq? takes two arguments.")
(body "Each must be a non-numeric atom.")
(eq? 'two 'two)
(eq? 'two 'three)
(title "The First Commandment (preliminary)")
(body "Always ask null? as the first question")
(body "in expressing any function.")
(rember 'two xs-five)
(rember 'two xs-five-nested)
(title "The Second Commandment")
(body "Use cons to build lists.")
(firsts xs-five-nested)
(seconds xs-five-nested)
(title "The Third Commandment")
(body "When building a list, describe the first typical")
(body "element, and then cons it onto the natural recursion.")
(insertR 'three-point-five 'three xs-five)
(insertL 'two-point-five 'three xs-five)
(subst 'three 'better-three xs-five)
(subst2 'six 'five 'four xs-five)
(multirember 'four (insertL 'four 'four xs-five))
(multiinsertR 'tomato 'fish food)
(multiinsertL 'fried 'fish food)
(title "The Fourth Commandment")
(body "Always change at least one argument while recurring.")
(body "It must be changed to be closer to termination.")
(body "The changing argument must be tested in the termination")
(body "condition: when using cdr, test termination with null?.")
(multisubst 'crabs 'fish food)
(title "Numbers")
(add1 67)
(sub1 5)
(sub1 0)
(zero? 0)
(zero? 1492)
(o+ 4 4)
(o- 17 7)
(title "Tuples")
;;(tup? (1 32 312))
(addtup '(12 12 12))
(o* 5 5)
;; (x 5 5) = 5 + (x 5 4)
;; = 5 + 5 + (x 5 3)
;; = 5 + 5 + 5 + (x 5 2)
;; = 5 + 5 + 5 + 5 + (x 5 1)
;; = 5 + 5 + 5 + 5 + 5 + (x 5 0)
;; = 5 + 5 + 5 + 5 + 5 + 0
(tup+ '(2 2) '(4 4 4))
(> 12 133)
(o> 134 133)
(o< 6 5)
(o=? 4 4)
(o= 4 4)
(o** 5 3)
(o/ 15 4)
;; (/ 15 4) = 1 + (/ 11 4)
;; = 1 + 1 + (/ 7 4)
;; = 1 + 1 + 1 (/ 3 4)
;; = 1 + 1 + 1 + 0
(length xs-five)
(pick 1 '(1 2 3 4 5 6))
(pick 1 xs-five)
(rempick 2 '(1 2 3))
(rempick 2 xs-five)
(no-nums xs-five-mix)
(all-nums xs-five-mix)
(eqan? 2 2)
(eqan? 2 'two)
(eqan? 'two 'two)
(occur 2 '(1 2 3 4 5 2 2))
(one? 1)
(rempick2 2 '(one two three four))
(rember* 2 '(2 3 (4 5) 6 8 9 (2 1 2 (2 3 3))))
(insertR* 'sir 'yes '(oh yes (oh yes (oh yes) oh yes)))
(occur* 2 '(2 2 2 (2 3 4 (43 2))))
(subst* 'yesterday 'today '(today was a nice day (yes today)))
(insertL* 'before 'today '(today was a nice day (yes today)))
(member* 'yes '(today was a nice day (yes today)))
(leftmost '(((maybe) try this for a change (today) please)))
(eqlist? '(a b (aa)) '(a b (aa)))
(equal? '(a) '(ab))
(eqlist?r '(a b (aa)) '(a b (aa)))
(remberr 'test '(this is a big test))
(title "The First Commandment (final version)")
(body "When recurring on a list of atoms, lat, ask two questions")
(body "about it: (null? lat) and else.")
(body "When recurring on a number, n, ask two questions about")
(body "it: (zero? n) and else.")
(numbered? '(2 o+ 2))
(title "The Seventh Commandment")
(body "Recur on the subparts that are of the same nature")
(body "- On the sublists of a list.")
(body "- On the subexpressions of an artihmetic expressions.")
(1st-sub-exp '('one 'two 'three))
(2nd-sub-exp '('one 'two 'three))
(value_v1 '(o+ 4 4))
(value_v1 '(o* 4 4))
(value_v1 '(o** 4 4))
(title "The Eighth Commandment")
(body "Use help functions to abstract from representations.")
(set? xs-five)
(set? food)
(set? xs-five-mix)
(makeset-old food)
(set? (makeset food))
(makeset food)
(subset? '(chips fish) (makeset food))
(eqset? (makeset food) (makeset food))
(intersect? (makeset food) food)
(intersect (makeset food) food)
(intersect '(seven eight one five) (makeset xs-five))
(union '(seven eight one five) xs-five)
(xxx '(seven eight one five) xs-five)
(intersect-all '((one two) (seven one three)))
(a-pair? tup-one)
(a-pair? '(1 2 3))
(a-pair? tup-two)
(first tup-two)
(second tup-two)
(build xs-five food)
(fun? tup-two)
(revrel tup-two)
(fullfun? tup-two)
(one-to-one? tup-two)
(firsts tup-two)
(seconds tup-two)
((rember-f eq?) 'one xs-five)
((insertL-f eq?) 'eleventy 'one xs-five)
((insertR-f eq?) 'eleventy 'one xs-five)
(insertLr 'eleventy 'one xs-five)
(insertRr 'eleventy 'one xs-five)
(insertLrr 'smementy 'one xs-five)
(subst_r 'smementy 'one xs-five)
(title "The Ninth Commandment")
(body "Abstract common patterns with a new function")
((atom-to-function 'o*) 2 2)
(value_v2 '(o+ 4 4))
(value_v2 '(o* 4 4))
(value_v2 '(o** 4 4))
((multirember_r eq?) 'fish food)
(multirember-eq? 'fish food)
(multiremberT (lambda (a) (eq? a 'fish)) food)
(multirember&co 'fish food a-friend)
(multirember&co 'chips food last-friend)
(title "The Tenth Commandment")
(body "Build functions to collect more than one value at a time")
(multiinsertLR 'salty 'fish 'chips food)
(multiinsertLR&co 'salty 'fish 'chips food (lambda (lat l r) (= l r)))
(even? 2)
(even? 3)
(evens-only* '((9 1 2 8) 3 10 ((9 9) 7 6) 2))
(evens-only*&co '((9 1 2 8) 3 10 ((9 9) 7 6) 2) (lambda (n p s)
(cons s (cons p n))))
(shift '((a b) (c d)))
(align '((a b) (c d)))
(length* '((a b) (c d)))
(weight* '((a b) (c d)))
(weight* '(a (c d)))
(shuffle '(a b))
; following procedures are non-total
;(shuffle '((a b) (c d)))
;(C 8721)
;(A 2 2)
; what is the value of:
;(((lambda (mk-length) (mk-length mk-length))
; (lambda (mk-length) (lambda (l)
; (cond
; ((null? l) 0)
; (else (addl ((mk-length eternity) (cdr l)))))))))
; Y comb
;((lambda (le)
; ((lambda (mk-length)
; (mk-length mk-length)) (lambda (mk-length)
; (le (lambda (x)
; ((mk-length mk-length) x))))))
; (lambda (length) (lambda (l)
; (cond
; ((null? l) 0)
; (else (add1 (length (cdr l))))))))
(define xs-entry (new-entry
'(one two three) '((couple of things) (other things) (last things))))
(displayln xs-entry)
(lookup-in-entry 'two xs-entry displayln)
(lookup-in-entry 'four xs-entry displayln)
(define xs-table (extend-table xs-entry '()))
(value_v2 (car (quote (a b c))))
(value_v2 (add1 6))
;(meaning (lambda (x) (cons x x)) '(((y z) ((8) 9))))
(apply '(primitive cons) '(6 (a b c)))