-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.scm
319 lines (274 loc) · 6.03 KB
/
12.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
315
316
317
318
319
;;; -*- buffer-read-only:t -*-
(load "11.scm")
(define multirember
(lambda (a lat)
((Y (lambda (mr)
(lambda (lat)
(cond ((null? lat) '())
((eq? a (car lat))
(mr (cdr lat)))
(else (cons (car lat)
(mr (cdr lat))))))))
lat)))
;;; (multirember 'a '(a b c a d))
(define length
((lambda (k)
"this function is called Y-combinator"
((lambda (f)
(f f))
(lambda (f)
(k (lambda (x)
((f f) x))))))
(lambda (length)
"this is the length function"
(lambda (l)
(cond ((null? l) 0)
(else (add1 (length (cdr l)))))))))
;;; (length '(a b c))
(define multirember
(lambda (a lat)
((letrec
((mr (lambda (lat)
(cond ((null? lat) '())
((eq? a (car lat))
(mr (cdr lat)))
(else (cons (car lat)
(mr (cdr lat))))))))
mr)
lat)))
;;; (multirember 'a '(a b c a d))
(define mr
(lambda (lat)
(cond ((null? lat) '())
((eq? a (car lat))
(mr (cdr lat)))
(else (cons (car lat)
(mr (cdr lat)))))))
(define multirember-bad
(lambda (a lat)
"this does not work"
(mr lat)))
;;; (multirember-bad 'a '(a b c a d))
(define multirember
(lambda (a lat)
(letrec
((mr (lambda (lat)
(cond ((null? lat) '())
((eq? a (car lat))
(mr (cdr lat)))
(else (cons (car lat)
(mr (cdr lat))))))))
(mr lat))))
;;; (multirember 'a '(a b c a d))
(define rember-f
(lambda (test?)
"from little"
(lambda (a l)
(cond ((null? l) '())
((test? a (car l))
(cdr l))
(else (cons (car l)
(rember-f test? a (cdr l))))))))
(define rember-eq?
(rember-f eq?))
(define multirember-f
(lambda (test?)
(lambda (a lat)
"from little"
(cond ((null? lat) '())
((test? (car lat) a)
((multirember-f test?) a (cdr lat)))
(else (cons (car lat)
((multirember-f test?) a (cdr lat))))))))
(define multirember-eq?
(multirember-f eq?))
(define multirember-f
(lambda (test?)
(letrec ((m-f (lambda (a lat)
"from little"
(cond ((null? lat) '())
((test? (car lat) a)
(m-f a (cdr lat)))
(else (cons (car lat)
(m-f a (cdr lat))))))))
m-f)))
(define multirember-eq?
(multirember-f eq?))
;;; (multirember-eq? 'a '(a b c a d))
(define (member? a lat)
"from little"
(cond ((null? lat) false)
(else
(or
(equal? a (car lat))
(member? a (cdr lat))))))
(define member?
(lambda (a lat)
((letrec ((yes? (lambda (l)
(cond ((null? l) false)
(else
(or
(eq? a (car l))
(yes? (cdr l))))))))
yes?)
lat)))
;;; (member? 'a '(b c))
(define member?
(lambda (a lat)
(letrec ((yes? (lambda (l)
(cond ((null? l) false)
(else
(or
(eq? a (car l))
(yes? (cdr l))))))))
(yes? lat))))
;;; (member? 'a '(b c a))
(define (union set1 set2)
"from little"
(cond ((null? set1) set2)
((member? (car set1) set2)
(union (cdr set1) set2))
(else
(cons (car set1)
(union (cdr set1)
set2)))))
(define union
(lambda (set1 set2)
(letrec ((u (lambda (s1)
(cond ((null? s1) set2)
((member? (car s1) set2)
(u (cdr s1)))
(else
(cons (car s1)
(u (cdr s1))))))))
(u set1))))
;;; (union '(a c w) '(a b y w))
(define union
(lambda (set1 set2)
(letrec ((u (lambda (s1)
(cond ((null? s1) set2)
((member? (car s1) set2)
(u (cdr s1)))
(else
(cons (car s1)
(u (cdr s1)))))))
(member? (lambda (a lat)
(cond ((null? lat) false)
(else
(or
(equal? a (car lat))
(member? a (cdr lat))))))))
(u set1))))
;;; (union '(a c w) '(a b y w))
(define union
(lambda (set1 set2)
(letrec ((u (lambda (s1)
(cond ((null? s1) set2)
((member? (car s1) set2)
(u (cdr s1)))
(else
(cons (car s1)
(u (cdr s1)))))))
(member? (lambda (a lat)
(letrec ((m? (lambda (l)
(cond ((null? l) false)
(else
(or
(eq? a (car l))
(m? (cdr l))))))))
(m? lat)))))
(u set1))))
;;; (union '(a c w) '(a b y w))
(define two-in-a-row?
(lambda (lat)
(letrec ((w? (lambda (preceding lat)
(cond ((null? lat)
false)
(else
(or (eq? (car lat) preceding)
(w? (car lat) (cdr lat))))))
))
(cond ((null? lat) false)
(else
(w? (car lat) (cdr lat)))))))
;;; (two-in-a-row? '(b a b c b a b a))
(define two-in-a-row?
(letrec ((w? (lambda (preceding lat)
(cond ((null? lat)
false)
(else
(or (eq? (car lat) preceding)
(w? (car lat) (cdr lat))))))
))
(lambda (lat)
(cond ((null? lat) false)
(else
(w? (car lat) (cdr lat)))))))
;;; (two-in-a-row? '(b a b c b a b c a))
(define (sum-of-prefixes-b sonssf tup)
"other variant"
(cond ((null? tup) '())
(else
(cons (o+ sonssf
(car tup))
(sum-of-prefixes-b
(o+ sonssf
(car tup))
(cdr tup))))))
(define sum-of-prefixes
(lambda (tup)
(letrec ((s (lambda (sonssf tup)
(cond ((null? tup) '())
(else
(cons (o+ sonssf
(car tup))
(s
(o+ sonssf
(car tup))
(cdr tup)))))
)))
(cond ((null? tup) ())
(else
(s 0
tup))))))
;;; (sum-of-prefixes '(1 1 1))
(define sum-of-prefixes
(letrec ((s (lambda (sonssf tup)
(cond ((null? tup) '())
(else
(cons (o+ sonssf
(car tup))
(s
(o+ sonssf
(car tup))
(cdr tup)))))
)))
(lambda (tup)
(cond ((null? tup) ())
(else
(s 0
tup))))))
;;; (sum-of-prefixes '(1 1 1))
(define (scramble-b tup rev-pre)
(cond ((null? tup) '())
(else
(cons (pick (car tup)
(cons (car tup)
rev-pre))
(scramble-b (cdr tup)
(cons (car tup)
rev-pre))))))
(define scramble
(lambda (tup)
(letrec ((s (lambda (tup rev-pre)
(cond ((null? tup) '())
(else
(cons (pick (car tup)
(cons (car tup)
rev-pre))
(s (cdr tup)
(cons (car tup)
rev-pre))))))))
(s tup '()))))
;;; (scramble '(1 2 3))
;;; (scramble '(1 1 1 3 4 2))