3.16 Sets
A set represents a set of distinct elements. For a given set, elements are equivalent via equal?, eqv?, or eq?. Two sets are equal? when they use the same element-comparison procedure (equal?, eqv?, or eq?) and have equivalent elements. A set can be used as a sequence (see Sequences).
Operations on sets that contain elements that are mutated are unpredictable in much the same way that hash table operations are unpredictable when keys are mutated.
The bindings documented in this section are provided by the racket/set and racket libraries, but not racket/base.
Creates a set that uses equal?, eq?, or
eqv?, respectively, to compare elements. The given
vs are added to the set. The elements are added in the order
that they appear as vs, so in the first two cases, an earlier
element that is equal? or eqv? but not eq?
to a later element takes precedence over the later element.
(set-empty? set) → boolean? |
set : set? |
Returns #t if set has no members, #f
otherwise.
(set-member? set v) → boolean? |
set : set? |
v : any/c |
Returns #t if v is in set, #f
otherwise.
Like operations on immutable hash tables, “constant time” set operations actually require O(log N) time for a set of size N.
Produces a set that includes v plus all elements of set. This operation runs in constant time.
(set-remove set v) → set? |
set : set? |
v : any/c |
Produces a set that includes all elements of set except
v. This operation runs in constant time.
Produces a set that includes all elements of all given sets,
which must all use the same equivalence predicate (equal?,
eq?, or eqv?). This operation runs in time
proportional to the total size of all given sets except for
the largest.
(set-intersect set ...+) → set? |
set : set? |
Produces a set that includes only the elements in all of the given
sets, which must all use the same equivalence predicate
(equal?, eq?, or eqv?). This operation
runs in time proportional to the total size of all given
sets except for the largest.
(set-subtract set ...+) → set? |
set : set? |
Produces a set that includes all elements the first sets that
are not present in any of the other given setss. All of the
given sets must use the same equivalence predicate
(equal?, eq?, or eqv?). This operation
runs in time proportional to the total size of all given
sets except the first one.
Returns #t if every member of set is in
set2, #f otherwise. The set and
set2 must use the same equivalence predicate
(equal?, eq?, or eqv?). This operation
runs in time proportional to the size of set.
Applies the procedure proc to each element in
set in an unspecified order, accumulating the results
into a list.
Applies proc to each element in set (for the
side-effects of proc) in an unspecified order.
Explicitly converts a set to a sequence for use with for and
other forms.
| |
| |
| |
| |
| |
|