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 single-valued sequence (see Sequences). The elements of the set serve as elements of the sequence. See also in-set.
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.
procedure
(set-empty? st) → boolean?
st : set?
procedure
st : set?
procedure
(set-member? st v) → boolean?
st : set? v : any/c
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 st. This operation runs in constant time.
procedure
(set-remove st v) → set?
st : set? v : any/c
At least one set must be provided to set-union even though mathematically set-union could accept zero arguments. Since there are multiple types of sets (eq?, eqv?, and equal?) there is no obvious choice for a default empty set to be returned. If there is a case where set-union may be applied to zero arguments, instead pass an empty set of the type you desire.
Examples: | ||||||||||||
|
procedure
(set-intersect st ...+) → set?
st : set?
procedure
(set-subtract st ...+) → set?
st : set?
procedure
(set-symmetric-difference st ...+) → set?
st : set?
Example: | ||
|
Equivalent to (equal? st st2).
Examples: | ||||||
|
Examples: | ||||||
|
procedure
(proper-subset? st st2) → boolean?
st : set? st2 : set?
Examples: | ||||||
|
procedure
(set-equal? st) → boolean?
st : set?
procedure
contract : chaperone-contract? cmp : (or/c 'dont-care 'equal 'eqv 'eq) = 'dont-care
If cmp is 'dont-care, then the equality notion of the set is not considered when checking the contract. Otherwise, the contract accepts only sets with the corresponding notion of equality.
If cmp is 'eq or 'eqv, then contract must be a flat contract. If contract is not a flat contract, then cmp cannot be 'eq or 'eqv and the resulting contract can only be applied to sets that use equal? for equality.
syntax
(for/set (for-clause ...) body ...+)
syntax
(for/seteq (for-clause ...) body ...+)
syntax
(for/seteqv (for-clause ...) body ...+)
syntax
(for*/set (for-clause ...) body ...+)
syntax
(for*/seteq (for-clause ...) body ...+)
syntax
(for*/seteqv (for-clause ...) body ...+)
procedure
lst : list?
procedure
(list->seteq lst) → set?
lst : list?
procedure
(list->seteqv lst) → set?
lst : list?