3 (define (list-set! l i x)
4 (cond ((null? l) (error "list-set!: out of bounds"))
5 ((= i 0) (set-car! l x))
6 (else (list-set! (cdr l) (- i 1) x))))
8 (define (interval n m) ; returns the list (n n+1 n+2 ... m)
9 (if (<= n m) (cons n (interval (+ n 1) m)) '()))
11 (define (iota n) (interval 0 (- n 1)))
13 (define (filter f lst)
14 (cond ((null? lst) '())
15 ((f (car lst)) (cons (car lst) (filter f (cdr lst))))
16 (else (filter f (cdr lst)))))
18 (define (identity x) x)
21 ;; sets using hash tables
22 (define (new-empty-set) (make-table hash: eq?-hash test: eq?))
24 (let ((s (new-empty-set)))
27 (define (set-member? s x) (table-ref s x #f))
28 (define (set-length s) (table-length s))
29 (define (set-equal? s1 s2) (equal? s1 s2))
30 (define (set-diff s1 s2)
31 (let ((s (table-copy s1)))
32 (table-for-each (lambda (key val) (table-set! s key))
35 (define (set-intersection s1 s2)
36 (define (inters s1 s2)
37 (let ((t (table-copy s1)))
38 (table-for-each (lambda (k v) (if (not (table-ref s2 k #f))
42 (if (< (table-length s1) (table-length s2))
45 (define (set-union s1 s2)
46 (if (> (table-length s1) (table-length s2))
49 (define (set-union! s1 s2) (table-merge! s1 s2)) ; side-effects s1
50 (define (set-union-multi sets)
53 (let loop ((l (cdr sets))
54 (s (set-copy (car sets))))
58 (if (> (set-length s) (set-length s2))
59 (begin (set-union! s s2)
61 (let ((s2 (set-copy s2)))
63 (loop (cdr l) s2))))))))
64 (define (set-add s1 x)
65 (let ((s2 (table-copy s1)))
68 (define (set-add! s x) (table-set! s x #t)) ; faster, but side-effecting
69 (define (set-remove! s x) (table-set! s x))
70 (define (set-empty? s) (= (table-length s) 0))
71 (define (list->set l) (list->table (map (lambda (x) (cons x #t)) l)))
72 (define (set->list s) (map car (table->list s)))
73 (define (set-filter p s1)
74 (let ((s2 (new-empty-set)))
75 (table-for-each (lambda (key value)
77 (table-set! s2 key #t)))
80 (define (set-for-each f s) (table-for-each (lambda (x dummy) (f x)) s))
81 (define (set-subset? s1 s2) ; is s2 a subset of s1 ?
82 (if (> (set-length s2) (set-length s1))
84 (let loop ((l (set->list s2)))
87 ((set-member? s1 (car l))
91 (define set-copy table-copy)
93 (define (foldl f base lst)
96 (foldl f (f base (car lst)) (cdr lst))))
98 (define (pos-in-list x lst)
99 (let loop ((lst lst) (i 0))
100 (cond ((not (pair? lst)) #f)
101 ((eq? (car lst) x) i)
102 (else (loop (cdr lst) (+ i 1))))))
104 (define (remove x lst)
105 (cond ((null? lst) '())
106 ((eq? x (car lst)) (cdr lst))
107 (else (cons (car lst)
108 (remove x (cdr lst))))))
110 (define (replace x y lst)
111 (cond ((null? lst) '())
112 ((eq? x (car lst)) (cons y (cdr lst)))
113 (else (cons (car lst)
114 (replace x y (cdr lst))))))
117 (cond ((null? lst) #f)
118 ((null? (cdr lst)) (car lst))
119 (else (last (cdr lst)))))
121 (define (all-but-last lst)
124 (cond ((null? lst) #f)
125 ((null? (cdr lst)) (reverse new))
126 (else (loop (cdr lst)
127 (cons (car lst) new))))))
132 (else (memp p (cdr l)))))
134 (define (intersperse x l)
135 (cond ((or (null? l) (null? (cdr l))) l)
136 (else (cons (car l) (cons x (intersperse x (cdr l)))))))
141 (rest (unique (cdr l))))
142 (if (member head rest)
145 (define (string-append-with-separator sep . strings)
146 (apply string-append (intersperse sep (unique strings))))
148 (define (split-string s delimiter) ; delimiter is a char
149 (let loop ((s (string->list s))
153 (reverse (map (lambda (x) (list->string (reverse x)))
154 (if (null? acc) res (cons acc res)))))
155 ((eq? (car s) delimiter)
172 (define (make-bitset n)
173 (let ((len (fxarithmetic-shift-right (+ n 7) 3)))
174 (make-u8vector len)))
176 (define (bitset-add! bs i)
177 (let* ((j (fxarithmetic-shift-right i 3))
181 (fxior (u8vector-ref bs j)
182 (fxarithmetic-shift-left 1 k)))))
183 (define (bitset-remove! bs i)
184 (let* ((j (fxarithmetic-shift-right i 3))
188 (fxand (u8vector-ref bs j)
189 (fxnot (fxarithmetic-shift-left 1 k))))))
191 (define (bitset-member? bs i)
192 (let* ((j (fxarithmetic-shift-right i 3))
194 (not (fx= 0 (fxand (u8vector-ref bs j)
195 (fxarithmetic-shift-left 1 k))))))
197 (define (bitset-intersection b1 b2)
198 (let* ((l (u8vector-length b1)) ; both should have the same length
199 (b3 (make-u8vector l 0))) ;; TODO abstract with diff and union!
200 (let loop ((l (- l 1)))
202 (begin (u8vector-set! b3 l (fxand (u8vector-ref b1 l)
203 (u8vector-ref b2 l)))
207 (define (bitset-diff b1 b2)
208 (let* ((l (u8vector-length b1)) ; both should have the same length
209 (b3 (make-u8vector l 0)))
210 (let loop ((l (- l 1)))
212 (begin (u8vector-set! b3 l (fxand (u8vector-ref b1 l)
213 (fxnot (u8vector-ref b2 l))))
217 (define (bitset-union! b1 b2)
218 (let* ((l (u8vector-length b1))) ; both should have the same length
219 (let loop ((l (- l 1)))
221 (begin (u8vector-set! b1 l (fxior (u8vector-ref b1 l)
222 (u8vector-ref b2 l)))
226 (define (bitset-empty? bs)
227 (let loop ((l (- (u8vector-length bs) 1)))
230 ((= (u8vector-ref bs l) 0)
234 (define (bitset-length bs)
235 (let loop ((l (- (u8vector-length bs) 1))
242 (let ((b (u8vector-ref bs l)))
243 (let loop2 ((i 0) (n 0)) ;; TODO is there a better way ?
248 (if (= (fxand (fxarithmetic-shift-left 1 i) b) 0)
252 (define (list->bitset n lst)
253 (let ((bs (make-bitset n)))
254 (let loop ((lst lst))
261 (define (bitset->list bs)
262 (let ((n (fxarithmetic-shift-left (u8vector-length bs) 3)))
263 (let loop ((i (- n 1)) (lst '()))
266 (if (bitset-member? bs i)
271 (define bitset-copy u8vector-copy)
273 (define (bitset-union-multi n bitsets) ; n is necessary is bitsets is null
274 (let ((bs (make-bitset n)))
275 (let loop ((l bitsets))
278 (begin (bitset-union! bs (car l))
281 (define (bitset-subset? b1 b2) ; is b2 a subset of b1 ?
282 (equal? (bitset-intersection b1 b2) b2))