3 (define (interval n m) ; returns the list (n n+1 n+2 ... m)
4 (if (<= n m) (cons n (interval (+ n 1) m)) '()))
7 (cond ((null? lst) '())
8 ((f (car lst)) (cons (car lst) (keep f (cdr lst))))
9 (else (keep f (cdr lst)))))
11 ;; sets using hash tables
12 (define (new-empty-set) (make-table hash: eq?-hash test: eq?))
14 (let ((s (new-empty-set)))
17 (define (set-member? s x) (table-ref s x #f))
18 (define (set-equal? s1 s2) (equal? s1 s2))
20 (let ((s (table-copy s1)))
21 (table-for-each (lambda (key val) (table-set! s key))
24 (define (intersection s1 s2)
25 (define (inters s1 s2)
26 (let ((t (table-copy s1)))
27 (table-for-each (lambda (k v) (if (not (table-ref s2 k #f))
31 (if (< (table-length s1) (table-length s2))
34 (define (set-add s1 x)
35 (let ((s2 (table-copy s1)))
38 (define (union s1 s2) (table-merge s1 s2))
39 (define (union-multi sets) (foldl union (new-empty-set) sets))
40 (define (list->set l) (list->table (map (lambda (x) (cons x #t)) l)))
41 (define (set->list s) (map car (table->list s)))
42 (define (set-filter p s1)
43 (let ((s2 (new-empty-set)))
44 (table-for-each (lambda (key value)
46 (table-set! s2 key #t)))
50 (define (foldl f base lst)
53 (foldl f (f base (car lst)) (cdr lst))))
55 (define (pos-in-list x lst)
56 (let loop ((lst lst) (i 0))
57 (cond ((not (pair? lst)) #f)
59 (else (loop (cdr lst) (+ i 1))))))
61 (define (remove x lst)
62 (cond ((null? lst) '())
63 ((eq? x (car lst)) (cdr lst))
65 (remove x (cdr lst))))))
67 (define (replace x y lst)
68 (cond ((null? lst) '())
69 ((eq? x (car lst)) (cons y (cdr lst)))
71 (replace x y (cdr lst))))))
74 (cond ((null? lst) #f)
75 ((null? (cdr lst)) (car lst))
76 (else (last (cdr lst)))))
78 (define (all-but-last lst)
81 (cond ((null? lst) #f)
82 ((null? (cdr lst)) (reverse new))
84 (cons (car lst) new))))))
89 (else (memp p (cdr l)))))