My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
;;;;; PROBLEMS 1 - 28: WORKING WITH LISTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define test
(lambda (expected actual-exp)
(let ((actual (eval actual-exp)))
(if (equal? expected actual)
(let ()
(display (car actual-exp))
(display " matched expected value of ")
expected)
(let ()
(display (car actual-exp))
(display " FAILED: expected ")
(display expected)
(display ", actual ")
actual)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P01: Find the last box of a list.
(define p01-last
(lambda (lst)
(cond
((null? lst) '())
((null? (cdr lst)) lst)
(else (p01-last (cdr lst))))))


(test '(d) '(p01-last '(a b c d)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P02: Find the last but one box of a list.
(define p02-last-but-one
(lambda (lst)
(cond
((null? lst) '())
((null? (cddr lst)) lst)
(else (p02-last-but-one (cdr lst))))))


(test '(c d) '(p02-last-but-one '(a b c d)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P03: Find the K'th element of a list. The first element in the list is
; number 1.
(define p03-element-at
(lambda (lst k)
(if (null? lst)
'ERROR
(if (equal? k 1)
(car lst)
(p03-element-at (cdr lst) (- k 1))))))


(test 'c '(p03-element-at '(a b c d e) 3))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P04: Find the number of elements of a list.
(define p04-count
(lambda (lst)
(if (null? lst)
0
(+ 1 (p04-count (cdr lst))))))


(test '4 '(p04-count '(a b c d)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P05: Reverse a list.
(define p05-reverse
(lambda (lst)
(p05-reverse.2 lst '())))

(define p05-reverse.2
(lambda (todo done)
(if (null? todo)
done
(p05-reverse.2 (cdr todo) (cons (car todo) done)))))


(test '(a b c d) '(p05-reverse '(d c b a)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P06: Find out whether a list is a palindrome. A palindrome can be read
; forward or backward; e.g. (x a m a x).
(define p06-palindrome?
(lambda (lst)
(null? (p06-palindrome?.2 lst lst))))

(define p06-palindrome?.2
(lambda (orig lst)
(if (null? lst)
#t ; safe guard
(if (null? (cdr lst))
(if (equal? (car orig) (car lst)) ; base case
(cdr orig)
#f)
(let ((induc (p06-palindrome?.2 orig (cdr lst)))) ; induction case
(if (not (list? induc))
induc
(if (equal? (car induc) (car lst))
(cdr induc)
#f)))))))


(test '#t '(p06-palindrome? '(x a m a x)))(test '#f '(p06-palindrome? '(x a m b x)))(test '#f '(p06-palindrome? '(x b m a x)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P07: Flatten a nested list structure. Transform a list, possibly holding
; lists as elements into a "flat" list by replacing each list with its
; elements (recursively).
(define p07-flatten
(lambda (lst)
(cond
((null? lst) '())
((not (list? (car lst))) (cons (car lst) (p07-flatten (cdr lst))))
(else
(let ((flat (p07-flatten (car lst))))
(if (null? flat)
(p07-flatten (cdr lst))
(cons (car flat) (p07-flatten.2 (cdr flat) (cdr lst)))))))))

(define p07-flatten.2
(lambda (sublist lst)
(if (null? sublist)
(p07-flatten lst)
(cons (car sublist) (p07-flatten.2 (cdr sublist) lst)))))


(test '(a b c d e) '(p07-flatten '(a (b (c d) e))))(test '(a b c d e) '(p07-flatten '(a (()) (b ()) (((c))) (() (()) d) ((() ((e) ()))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P08: Eliminate consecutive duplicates of list elements. If a list contains
; repeated elements they should be replaced with a single copy of the
; element. The order of the elements should not be changed.
(define p08-compress
(lambda (lst)
(if (null? lst)
'()
(p08-compress.2 (cdr lst) (car lst)))))

(define p08-compress.2
(lambda (lst curr)
(if (null? lst)
(list curr)
(if (equal? curr (car lst))
(p08-compress.2 (cdr lst) curr)
(cons curr (p08-compress.2 (cdr lst) (car lst)))))))


(test '(a b c a d e) '(p08-compress '(a a a a b c c a a d e e e e)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P09: Pack consecutive duplicates of list elements into sublists. If a list
; contains repeated elements they should be placed in separate sublists.
(define p09-pack
(lambda (lst)
(if (null? lst)
'()
(p09-pack.2 (cdr lst) (list (car lst))))))

(define p09-pack.2
(lambda (lst curr)
(if (null? lst)
(list curr)
(if (equal? (car lst) (car curr))
(p09-pack.2 (cdr lst) (cons (car lst) curr))
(cons curr (p09-pack lst))))))


(test '((a a a a) (b) (c c) (a a) (d) (e e e e)) '(p09-pack '(a a a a b c c a a d e e e e)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P10: Run-length encoding of a list. Use the result of problem P09 to
; implement the so-called run-length encoding data compression method.
; Consecutive duplicates of elements are encoded as lists (N E) where N is
; the number of duplicates of the element E.
(define p10-encode
(lambda (lst)
(let ((lst (p09-pack lst)))
(if (null? lst)
'()
(p10-encode.2 (cdr lst) (car lst) 1)))))

(define p10-encode.2
(lambda (lst curr cnt)
(if (null? (cdr curr))
(cons (list cnt (car curr))
(if (null? lst)
'()
(p10-encode.2 (cdr lst) (car lst) 1)))
(p10-encode.2 lst (cdr curr) (+ cnt 1)))))


(test '((4 a) (1 b) (2 c) (2 a) (1 d) (4 e)) '(p10-encode '(a a a a b c c a a d e e e e)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P11: Modified run-length encoding. Modify the result of problem P10 in
; such a way that if an element has no duplicates it is simply copied into
; the result list. Only elements with duplicates are transferred as (N E)
; lists.
(define p11-encode-modified
(lambda (lst)
(p11-encode-modified.2 (p10-encode lst))))

(define p11-encode-modified.2
(lambda (lst)
(if (null? lst)
'()
(cons
(if (= 1 (caar lst)) (cadar lst) (car lst))
(p11-encode-modified.2 (cdr lst))))))


(test '((4 a) b (2 c) (2 a) d (4 e)) '(p11-encode-modified '(a a a a b c c a a d e e e e)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P12: Decode a run-length encoded list. Given a run-length code list
; generated as specified in problem P11, construct its uncompressed version.
(define p12-decode
(lambda (lst)
(if (null? lst)
'()
(if (list? (car lst))
(p12-decode.2 (cdr lst) (cadar lst) (caar lst))
(cons (car lst) (p12-decode (cdr lst)))))))

(define p12-decode.2
(lambda (lst curr cnt)
(if (zero? cnt)
(p12-decode lst)
(cons curr (p12-decode.2 lst curr (- cnt 1))))))


(test '(a a a a b c c a a d e e e e) '(p12-decode '((4 a) b (2 c) (2 a) d (4 e))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P13: Run-length encoding of a list (direct solution). Implement the
; run-length encoding from problem P11 directly, i.e. don't create the
; sublists containing the duplicates as in problem P09.
(define p13-encode-direct
(lambda (lst)
(if (null? lst)
'()
(p13-encode-direct.2 (cdr lst) (car lst) 1))))

(define p13-encode-direct.2
(lambda (lst curr cnt)
(if (null? lst)
(list (if (= cnt 1) curr (list cnt curr)))
(if (equal? (car lst) curr)
(p13-encode-direct.2 (cdr lst) curr (+ cnt 1))
(cons (if (= cnt 1) curr (list cnt curr)) (p13-encode-direct lst))))))


(test '((4 a) b (2 c) (2 a) d (4 e)) '(p13-encode-direct '(a a a a b c c a a d e e e e)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P14: Duplicate the elements of a list.
(define p14-duplicate
(lambda (lst)
(if (null? lst)
'()
(cons (car lst) (cons (car lst) (p14-duplicate (cdr lst)))))))


(test '(a a b b c c c c d d) '(p14-duplicate '(a b c c d)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P15: Replicate the elements of a list a given number of times.
(define p15-replicate
(lambda (lst n)
(if (null? lst)
'()
(p15-replicate.2 (cdr lst) n (car lst) n))))

(define p15-replicate.2
(lambda (lst n curr cnt)
(if (zero? cnt)
(p15-replicate lst n)
(cons curr (p15-replicate.2 lst n curr (- cnt 1))))))


(test '(a a a b b b c c c) '(p15-replicate '(a b c) 3))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P16: Drop every N'th element from a list.
(define p16-drop
(lambda (lst n)
(p16-drop.2 lst n n)))

(define p16-drop.2
(lambda (lst n cnt)
(if (null? lst)
'()
(if (= cnt 1)
(p16-drop (cdr lst) n)
(cons (car lst) (p16-drop.2 (cdr lst) n (- cnt 1)))))))


(test '(a b d e g h k) '(p16-drop '(a b c d e f g h i k) 3))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P17: Split a list into two parts; the length of the first part is given.
(define p17-split
(lambda (lst n)
(if (null? lst)
(list '() '())
(if (zero? n)
(list '() lst)
(let ((induc (p17-split (cdr lst) (- n 1))))
(cons (cons (car lst) (car induc)) (cdr induc)))))))


(test '((a b c) (d e f g h i k)) '(p17-split '(a b c d e f g h i k) 3))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P18: Extract a slice from a list. Given two indices, I and K, the slice
; includes the I'th and K'th elements of the original list and the elements
; between them. Start counting the elements with 1.
(define p18-slice
(lambda (lst i k)
(if (or (null? lst) (zero? k))
'()
(if (<= i 1)
(cons (car lst) (p18-slice (cdr lst) 0 (- k 1)))
(p18-slice (cdr lst) (- i 1) (- k 1))))))


(test '(c d e f g) '(p18-slice '(a b c d e f g h i k) 3 7))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P19: Rotate a list N places to the left.
(define p19-rotate
(lambda (lst n)
(let ((halves (p17-split lst (if (> n 0) n (+ (length lst) n)))))
(append (cadr halves) (car halves)))))

(test '(d e f g h a b c) '(p19-rotate '(a b c d e f g h) 3))(test '(g h a b c d e f) '(p19-rotate '(a b c d e f g h) -2))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P20: Remove the K'th element from a list.
(define p20-remove-at
(lambda (lst k)
(if (null? lst)
'()
(if (> k 1)
(cons (car lst) (p20-remove-at (cdr lst) (- k 1)))
(cdr lst)))))


(test '(a c d) '(p20-remove-at '(a b c d) 2))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P21: Insert an element at a given position into a list.
(define p21-insert-at
(lambda (el lst i)
(if (null? lst)
(list el)
(if (> i 1)
(cons (car lst) (p21-insert-at el (cdr lst) (- i 1)))
(cons el lst)))))


(test '(a alfa b c d) '(p21-insert-at 'alfa '(a b c d) 2))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P22: Create a list containing all integers within a given range. If first
; argument is smaller than second, produce a list in decreasing order.
(define p22-range
(lambda (i j)
(cond
((= i j) (list i))
((< i j) (cons i (p22-range (+ i 1) j)))
((> i j) (cons i (p22-range (- i 1) j))))))


(test '(4 5 6 7 8 9) '(p22-range 4 9))(test '(9 8 7 6 5 4) '(p22-range 9 4))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P23: Extract a given number of randomly selected elements from a list.
(define p23-rand-select
(lambda (lst n)
(if (or (null? lst) (zero? n))
'()
(let ((k (+ (random (length lst) 1))))
(cons (p03-element-at lst k) (p23-rand-select (p20-remove-at lst k) (- n 1)))))))



; (test '(e d a) '(p23-rand-select '(a b c d e f g h) 3))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P24: Lotto: Draw N different random numbers from the set 1..M.
(define p24-lotto-select
(lambda (n m)
(p23-rand-select (p22-range 1 m) n)))



; (test '(23 1 17 33 21 37) '(p24-lotto-select 6 49))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P25: Generate a random permutation of the elements of a list.
(define p25-rand-permutation
(lambda (lst)
(p23-rand-select lst (length lst))))



; (test '(b a d c e f) '(p25-rand-permutation '(a b c d e f)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P26: Generate the combinations of K distinct objects chosen from the N
; elements of a list
(define p26-combination
(lambda (k lst)
(cond
((or (> k (length lst)) (< k 1)) '())
((= k 1) (cons (list (car lst)) (p26-combination k (cdr lst))))
(else
(append
(p26-combination.combine (car lst) (p26-combination (- k 1) (cdr lst)))
(p26-combination k (cdr lst)))))))

(define p26-combination.combine
(lambda (head tuples)
(if (null? tuples)
'()
(cons (cons head (car tuples)) (p26-combination.combine head (cdr tuples))))))


(test '((a) (b) (c) (d)) '(p26-combination 1 '(a b c d)))(test '((a b) (a c) (a d) (b c) (b d) (c d)) '(p26-combination 2 '(a b c d)))(test '((a b c) (a b d) (a c d) (b c d)) '(p26-combination 3 '(a b c d)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P27: Group the elements of a set into disjoint subsets. Note that we do
; not want permutations of the group members; i.e. ((a b) ...) is the same
; solution as ((b a) ...). However, we make a difference between
; ((a b) (c d) ...) and ((c d) (a b) ...). You may find more about this
; combinatorial problem in a good book on discrete mathematics under the term
; "multinomial coefficients".
(define p27-group
(lambda (lst ks)
(if (null? ks)
'()
(p27-group.2 lst (cdr ks) (p26-combination (car ks) lst)))))

(define p27-group.2
(lambda (lst ks heads)
(if (null? heads)
'()
(let ((tails (p27-group (p27-remove-multi lst (car heads)) ks)))
(if (null? tails)
(p26-combination 1 heads) ; ex: turns ((a b) (a c) (b c)) into (((a b)) ((a c)) ((b c)))
(append ; instead of a append, for efficiency, could define a function like p26-combination.combine, but that adds on to a running result of p27-group.2
(p26-combination.combine (car heads) tails)
(p27-group.2 lst ks (cdr heads))))))))

(define p27-remove-multi ; removes first instance of each `els` from `lst`
(lambda (lst els)
(if (null? els)
lst
(p27-remove-multi (p27-remove lst (car els)) (cdr els)))))

(define p27-remove ; removes first instance `el` from `lst`
(lambda (lst el)
(if (null? lst)
'()
(if (equal? (car lst) el)
(cdr lst)
(cons (car lst) (p27-remove (cdr lst) el))))))


(test '(((a b) (c d))
((a c) (b d))
((a d) (b c))
((b c) (a d))
((b d) (a c))
((c d) (a b)))
'(p27-group '(a b c d) '(2 2)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P28A: Sort a list of lists according to length of sublists, i.e. short
; lists first, longer lists later.

(define p28-sort
(lambda (lst pred)
(let ((result (p28-merge-listlists (p26-combination 1 lst) pred)))
(if (null? result)
result
(car result)))))

(define p28-merge
(lambda (lst1 lst2 pred)
(cond
((null? lst1) lst2)
((null? lst2) lst1)
((pred (car lst1) (car lst2)) (cons (car lst1) (p28-merge (cdr lst1) lst2 pred)))
(else (cons (car lst2) (p28-merge lst1 (cdr lst2) pred))))))

(define p28-merge-listlists ; takes a list of lists and binary merges them, ex: ((c) (a) (b) (d)) --> ((a c) (b d)) --> ((a b c d))
(lambda (lolst pred)
(if (or (null? lolst) (null? (cdr lolst)))
lolst
(p28-merge-listlists (cons
(p28-merge (car lolst) (cadr lolst) pred)
(p28-merge-listlists (cddr lolst) pred))
pred))))

(define p28a-sort
(lambda (lolst)
(p28a-decode (p28-sort (p28a-encode lolst) p28a-first-el-lt?))))

(define p28a-first-el-lt?
(lambda (lst1 lst2)
(< (car lst1) (car lst2))))

(define p28a-encode
(lambda (lolst)
(if (null? lolst)
'()
(cons
(cons (length (car lolst)) (car lolst))
(p28a-encode (cdr lolst))))))

(define p28a-decode
(lambda (lolst)
(if (null? lolst)
'()
(cons (cdar lolst) (p28a-decode (cdr lolst))))))


(test '((o) (m n) (d e) (d e) (f g h) (a b c) (i j k l)) '(p28a-sort '((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; P28B: Sort a list of lists according to frequency of the lengths of the
; sublists, i.e. lists with rare lengths are placed first, others with a more
; frequent length come later.

(define p28b-sort
(lambda (lolst)
(let ((sorted-lenenc-lolst (p28-sort (p28a-encode lolst) p28a-first-el-lt?)))
(p28a-decode (p28-sort (p28b-reencode sorted-lenenc-lolst) p28a-first-el-lt?)))))

(define p28b-reencode
(lambda (lolst)
(cdr (p28b-reencode.2 lolst -1 0))))

(define p28b-reencode.2
(lambda (lolst len freq)
(if (null? lolst)
(list freq)
(let ((lst (cdar lolst))
(lstlen (caar lolst))
(rest (cdr lolst)))
(let ((rest-result (p28b-reencode.2 rest lstlen (if (= len lstlen) (+ freq 1) 1)))) ; first element of rest-result is total freq for lstlen
(cons (if (= len lstlen) (car rest-result) freq) ; put the total freq for len onto the result...
(cons (cons (car rest-result) lst) ; ...followed by lst with lstlen's total freq as lst's first element...
(cdr rest-result)))))))) ; ...followed by the rest


(test '((i j k l) (o) (a b c) (f g h) (d e) (d e) (m n)) '(p28b-sort '((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o))))

Change log

r24 by jonathan.hefner on Aug 13, 2008   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r23 by jonathan.hefner on Aug 13, 2008   Diff
[No log message]
All revisions of this file

File info

Size: 19591 bytes, 589 lines
Powered by Google Project Hosting