-
Notifications
You must be signed in to change notification settings - Fork 0
/
sets.sls
207 lines (178 loc) · 5.63 KB
/
sets.sls
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
#!r6rs
;;; sets.sls --- Purely Functional Sets
;; Copyright (C) 2012 Ian Price <[email protected]>
;; Author: Ian Price <[email protected]>
;; This program is free software, you can redistribute it and/or
;; modify it under the terms of the new-style BSD license.
;; You should have received a copy of the BSD license along with this
;; program. If not, see <http://www.debian.org/misc/bsd.license>.
;; Documentation:
;;
;; set? : any -> boolean
;; returns #t if the object is a set, #f otherwise
;;
;; make-set : (any any -> boolean) -> set
;; returns a new empty set ordered by the < procedure
;;
;; set-member? : set any -> boolean
;; returns true if element is in the set
;;
;; set-insert : set any -> set
;; returns a new set created by inserting element into the set argument
;;
;; set-remove : set element -> set
;; returns a new set created by removing element from the set
;;
;; set-size : set -> non-negative integer
;; returns the number of elements in the set
;;
;; set<? : set set -> boolean
;; returns #t if set1 is a proper subset of set2, #f otherwise. That
;; is, if all elements of set1 are in set2, and there is at least one
;; element of set2 not in set1.
;;
;; set<=? : set set -> boolean
;; returns #t if set1 is a subset of set2, #f otherwise, i.e. if all
;; elements of set1 are in set2.
;;
;; set=? : set set -> boolean
;; returns #t if every element of set1 is in set2, and vice versa, #f
;; otherwise.
;;
;; set>=? : set set -> boolean
;; returns #t if set2 is a subset of set1, #f otherwise.
;;
;; set>? : set set -> boolean
;; returns #t if set2 is a proper subset of set1, #f otherwise.
;;
;; subset? : set set -> boolean
;; same as set<=?
;;
;; proper-subset? : set set -> boolean
;; same as set<?
;;
;; set-map : (any -> any) set -> set
;; returns the new set created by applying proc to each element of the set
;;
;; set-fold : (any any -> any) any set -> any
;; returns the value obtained by iterating the procedure over each
;; element of the set and an accumulator value. The value of the
;; accumulator is initially base, and the return value of proc is used
;; as the accumulator for the next iteration.
;;
;; list->set : Listof(any) (any any -> any) -> set
;; returns the set containing all the elements of the list, ordered by <.
;;
;; set->list : set -> Listof(any)
;; returns all the elements of the set as a list
;;
;; set-union : set set -> set
;; returns the union of set1 and set2, i.e. contains all elements of
;; set1 and set2.
;;
;; set-intersection : set set -> set
;; returns the intersection of set1 and set2, i.e. the set of all
;; items that are in both set1 and set2.
;;
;; set-difference : set set -> set
;; returns the difference of set1 and set2, i.e. the set of all items
;; in set1 that are not in set2.
;;
;; set-ordering-procedure : set -> (any any -> boolean)
;; returns the ordering procedure used internall by the set.
(library (pfds sets)
(export set?
make-set
set-member?
set-insert
set-remove
set-size
set<?
set<=?
set=?
set>=?
set>?
subset?
proper-subset?
set-map
set-fold
list->set
set->list
set-union
set-intersection
set-difference
set-ordering-procedure
)
(import (rnrs)
(pfds bbtrees))
(define dummy #f)
;;; basic sets
(define-record-type (set %make-set set?)
(fields tree))
(define (set-ordering-procedure set)
(bbtree-ordering-procedure (set-tree set)))
(define (make-set <)
(%make-set (make-bbtree <)))
;; provide a (make-equal-set) function?
(define (set-member? set element)
(bbtree-contains? (set-tree set) element))
(define (set-insert set element)
(%make-set (bbtree-set (set-tree set) element dummy)))
(define (set-remove set element)
(%make-set (bbtree-delete (set-tree set) element)))
(define (set-size set)
(bbtree-size (set-tree set)))
;;; set equality
(define (set<=? set1 set2)
(let ((t (set-tree set2)))
(bbtree-traverse (lambda (k _ l r b)
(and (bbtree-contains? t k)
(l #t)
(r #t)))
#t
(set-tree set1))))
(define (set<? set1 set2)
(and (< (set-size set1)
(set-size set2))
(set<=? set1 set2)))
(define (set>=? set1 set2)
(set<=? set2 set1))
(define (set>? set1 set2)
(set<? set2 set1))
(define (set=? set1 set2)
(and (set<=? set1 set2)
(set>=? set1 set2)))
(define subset? set<=?)
(define proper-subset? set<?)
;;; iterators
(define (set-map proc set)
;; currently restricted to returning a set with the same ordering, I
;; could weaken this to, say, comparing with < on the object-hash,
;; or I make it take a < argument for the result set.
(let ((tree (set-tree set)))
(%make-set
(bbtree-fold (lambda (key _ tree)
(bbtree-set tree (proc key) dummy))
(make-bbtree (bbtree-ordering-procedure tree))
tree))))
(define (set-fold proc base set)
(bbtree-fold (lambda (key value base)
(proc key base))
base
(set-tree set)))
;;; conversion
(define (list->set list <)
(fold-left (lambda (tree element)
(set-insert tree element))
(make-set <)
list))
(define (set->list set)
(set-fold cons '() set))
;;; set operations
(define (set-union set1 set2)
(%make-set (bbtree-union (set-tree set1) (set-tree set2))))
(define (set-intersection set1 set2)
(%make-set (bbtree-intersection (set-tree set1) (set-tree set2))))
(define (set-difference set1 set2)
(%make-set (bbtree-difference (set-tree set1) (set-tree set2))))
)