-
Notifications
You must be signed in to change notification settings - Fork 1
/
library.rkt
351 lines (310 loc) · 9.29 KB
/
library.rkt
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
#lang scheme/base
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Misc useful functions
(require ;; also srfi/1
(only-in racket/list flatten filter-map take last)
(only-in racket/base filter)
(only-in srfi/1 list-index list-tabulate)
;; also srfi/43
(only-in racket/vector vector-map vector-append)
(only-in srfi/43 vector-for-each)
racket/match)
(provide set-vector!
set-string!
;; known list functions
flatten
foldr ormap andmap list-index filter filter-map
(rename-out [take sublist]
[last rac]
[map mapLR]
[list-tabulate tabulate])
;; rolled list functions
mapRL foldl1 foldr1 foldr-map foldl-map
rdc map-with-n
natural-foldl natural-map iota
;; known vector functions
vector-for-each vector-map vector-append
;; rolled vector functions
vector-foldl vector-foldr vector-tabulate
set-vector!
;; imperative functions
natural-for-each set-string!
;; set implementation
empty-set empty-set? set list->set cardinality element-of?
set<= set-eq?
union2 union
setdiff2 setdiff
intersect2 intersect
;; others
readfile repeated generate-counter symbol-append
memo memo-hashed memo* memo-rec)
(define set-vector! vector-set!)
(define set-string! string-set!)
(define symbol-append
(λ args (string->symbol (apply string-append (map symbol->string args)))))
;; Map from right to left.
(define mapRL
(lambda (f l)
(match l
['() '()]
[(cons x y) (let ((v (mapRL f y))) (cons (f x) v))])))
;; The foldl in this old code has swapped arguments to the function
(define foldl
(λ (f b l)
(for/fold ([acc b]) ([x (in-list l)])
(f acc x))))
;; Fold a binary function down a non-empty list, left to right.
;; (left operand of f is accumulator)
(define foldl1
(lambda (f l)
(let loop ([l (cdr l)]
[acc (car l)])
(if (null? l)
acc
(loop (cdr l) (f acc (car l)))))))
;; Fold a binary function down a non-empty list, right to left.
;; (right operand of f is accumulator)
(define foldr1
(lambda (f l)
(if (null? (cdr l))
(car l)
(f (car l) (foldr1 f (cdr l))))))
;; Foldl composed appropriately with map
(define foldl-map
(lambda (foldf mapf i l)
(if (pair? l)
(foldl-map foldf mapf (foldf i (mapf (car l))) (cdr l))
i)))
;; Foldr composed appropriately with map
(define foldr-map
(lambda (foldf mapf i l)
(if (pair? l)
(foldf (mapf (car l)) (foldr-map foldf mapf i (cdr l)))
i)))
;; All but the last element of a list.
(define rdc
(match-lambda
[(list _) '()]
[(cons x rest) (cons x (rdc rest))]))
;; Map left to right, but also pass f a 0-based index.
(define map-with-n
(lambda (f l)
(let loop ([l l]
[n 0])
(match l
['() '()]
[(cons x y) (let ((v (f x n))) (cons v (loop y (+ 1 n))))]))))
;; Return a list of the s-expressions in a file.
(define readfile
(lambda (f)
(letrec ((rf (lambda ()
(let ((sexp (read)))
(if (eof-object? sexp)
'()
(cons sexp (rf)))))))
(with-input-from-file f rf))))
;; Find a repeated element in a list
(define repeated
(lambda (l)
(cond [(null? l) #f]
[(memq (car l) (cdr l)) (car l)]
[else (repeated (cdr l))])))
;; Build a thunk that counts up from an initial value.
(define generate-counter
(lambda (n)
(lambda ()
(let ([m n])
(set! n (+ 1 n))
m))))
;; Memoize a function, using equal? to compare args
(define memo
(lambda (f)
(let ((q '()))
(lambda args
(let ((r (assoc args q)))
(if r
(cdr r)
(let ((v (apply f args)))
(set! q (cons (cons args v) q))
v)))))))
;; Memoize a function, using equal? on hashed args
(define memo-hashed
(lambda (f hash)
(let ((q '()))
(lambda args
(let* ((h (hash args))
(r (assoc h q)))
(if r
(cdr r)
(let ((v (apply f args)))
(set! q (cons (cons h v) q))
v)))))))
;; Memoize a function, passing comparison function
(define memo*
(lambda (f compare)
(let ((q '()))
(lambda args
(let loop ((s q))
(cond [(null? s)
(let ((v (apply f args)))
(set! q (cons (cons args v) q))
v)]
[(compare args (caar s))
(cdar s)]
[else
(loop (cdr s))]))))))
;; Memoize a recursive function that returns no interesting result,
;; passing a comparison function
(define memo-rec
(lambda (f compare)
(let ((q '()))
(lambda args
(let loop ((s q))
(cond [(null? s)
(set! q (cons args q))
(apply f args)
(void)]
[(compare args (car s))
(void)]
[else
(loop (cdr s))]))))))
;; Create a new vector from a function.
(define vector-tabulate
(lambda (size f)
(if (zero? size)
(vector)
(let ((x (make-vector size (f 0))))
(let loop ((i 1))
(when (< i size)
(vector-set! x i (f i))
(loop (+ i 1))))
x))))
;; Foldl for vectors
(define vector-foldl
(lambda (f i v)
(let ([n (vector-length v)])
(let loop ([j 0] [acc i])
(if (< j n)
(loop (+ j 1) (f acc (vector-ref v j)))
acc)))))
;; Foldr for vectors
(define vector-foldr
(lambda (f i v)
(let ([n (vector-length v)])
(let loop ([j (- n 1)] [acc i])
(if (<= 0 j)
(loop (- j 1) (f (vector-ref v j) acc))
acc)))))
;; For-each over the integers 0 .. n-1
(define natural-for-each
(lambda (f n) (for ([i (in-range n)]) (f i))))
;; Map over the integers 0 .. n-1
(define natural-map (lambda (f n) (build-list n f)))
;; Fold left over the integers 0 .. n-1
(define natural-foldl
(lambda (f i n)
(let loop ((j 0) (acc i))
(if (< j n)
(loop (+ j 1) (f acc j))
acc))))
;; Build a list of length n, with optional values for elements
(define iota
(lambda (n . val)
(let loop ((n n) (acc '()))
(if (< 0 n)
(loop (- n 1)
(cons (if (null? val) (- n 1) (car val)) acc))
acc))))
;; Format a number into a simple decimal
;; d - number of digits after the decimal point
;; w - minimum with of number in characters
(define (format-num d w num)
(let* ((expt-10-d (expt 10 d))
(n (floor (inexact->exact (round (* (abs num) expt-10-d)))))
(i (quotient n expt-10-d))
(f (modulo n expt-10-d))
(si (string-append
(if (< num 0) "-" "")
(number->string i 10)))
(sf (number->string (+ f expt-10-d) 10))
(sf (if (> d 0)
(string-append "." (substring sf 1 (string-length sf)))
""))
(lsi (string-length si))
(lsf (string-length sf))
(blanks (- w (+ lsi lsf))))
(string-append (make-string (max blanks 0) #\space) si sf)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Set operations implemented by lists.
;; Identity of elements is based on eq?.
;; These should probably be sped up some day.
(define empty-set '())
(define empty-set? null?)
;; construct a set
(define set
(lambda l
(list->set l)))
;; construct a set from a list by removing duplicates
(define list->set
(match-lambda
['() '()]
[(cons x y) (if (memq x y)
(list->set y)
(cons x (list->set y)))]))
;; test for membership
(define element-of?
(lambda (x set)
(and (memq x set) #t)))
(define cardinality length)
;; does s2 contain s1?
(define set<=
(lambda (a b)
(andmap (lambda (x) (memq x b)) a)))
;; are two sets equal? (mutually containing)
(define set-eq?
(lambda (a b)
(and (= (cardinality a) (cardinality b)) (set<= a b))))
;; unite two sets
(define union2
(lambda (a b)
(if (null? b)
a
(for/fold ([acc b]) ([x (in-list a)]
#:unless (memq x acc))
(cons x acc)))))
;; unite any number of sets
(define union
(lambda l
(for/fold ([set '()]) ([s (in-list l)])
(union2 s set))))
;; take set b from set a
(define setdiff2
(lambda (a b)
(if (null? b)
a
(for/fold ([c '()]) ([x (in-list a)]
#:unless (memq x b))
(cons x c)))))
;; take 2nd and other sets from first set
(define setdiff
(lambda l
(if (null? l)
'()
(setdiff2 (car l)
(for/fold ([c '()]) ([x (in-list (cdr l))])
(union2 x c))))))
;; intersect two sets
(define intersect2
(lambda (a b)
(if (null? b)
'()
(for/fold ([c '()]) ([x (in-list a)]
#:when (memq x b))
(cons x c)))))
;; intersect several sets
(define intersect
(lambda l
(if (null? l)
'()
(for/fold ([acc (car l)]) ([s (in-list l)])
(intersect2 s acc)))))