On this page:
set
seteqv
seteq
set-empty?
set-count
set-member?
set-add
set-remove
set-union
set-intersect
set-subtract
subset?
set-map
set-for-each
set?
set-equal?
set-eqv?
set-eq?
set/ c
in-set
for/ set
for/ seteq
for/ seteqv
for*/ set
for*/ seteq
for*/ seteqv

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.

The bindings documented in this section are provided by the racket/set and racket libraries, but not racket/base.

(set v ...)  set?
  v : any/c
(seteqv v ...)  set?
  v : any/c
(seteq v ...)  set?
  v : any/c
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? st)  boolean?
  st : set?
Returns #t if st has no members, #f otherwise.

Returns the number of elements in st.

(set-member? st v)  boolean?
  st : set?
  v : any/c
Returns #t if v is in st, #f otherwise.

(set-add st v)  set?
  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.

(set-remove st v)  set?
  st : set?
  v : any/c
Produces a set that includes all elements of st except v. This operation runs in constant time.

(set-union st ...+)  set?
  st : set?
Produces a set that includes all elements of all given sts, 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 sts except for the largest.

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:

  > (set-union (set))

  #<set:>

  > (set-union (seteq))

  #<seteq:>

  > (set-union (set 1) (set 2))

  #<set: 1 2>

  > (set-union (set 1) (seteq 2))

  set-union: set's equivalence predicate is not the same as

  the first set: (seteq 2)

  ; Sets of different types cannot be unioned

(set-intersect st ...+)  set?
  st : set?
Produces a set that includes only the elements in all of the given sts, 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 sts except for the largest.

(set-subtract st ...+)  set?
  st : set?
Produces a set that includes all elements the first sts that are not present in any of the other given stss. All of the given sts must use the same equivalence predicate (equal?, eq?, or eqv?). This operation runs in time proportional to the total size of all given sts except the first one.

(subset? st st2)  boolean?
  st : set?
  st2 : set?
Returns #t if every member of st is in st2, #f otherwise. The st and st2 must use the same equivalence predicate (equal?, eq?, or eqv?). This operation runs in time proportional to the size of st.

(set-map st proc)  (listof any/c)
  st : set?
  proc : (any/c . -> . any/c)
Applies the procedure proc to each element in st in an unspecified order, accumulating the results into a list.

(set-for-each st proc)  void?
  st : set?
  proc : (any/c . -> . any)
Applies proc to each element in st (for the side-effects of proc) in an unspecified order.

(set? v)  boolean?
  v : any/c
Returns #t if v is a set, #f otherwise.

(set-equal? st)  boolean?
  st : set?
Returns #t if st compares elements with equal?, #f if it compares with eqv? or eq?.

(set-eqv? st)  boolean?
  st : set?
Returns #t if st compares elements with eqv?, #f if it compares with equal? or eq?.

(set-eq? st)  boolean?
  st : set?
Returns #t if st compares elements with eq?, #f if it compares with equal? or eqv?.

(set/c contract [#:cmp cmp])  contract?
  contract : contract?
  cmp : (or/c 'dont-care 'equal 'eqv 'eq) = 'dont-care
Constructs a contract that recognizes sets whose elements match contract.

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.

(in-set st)  sequence?
  st : set?
Explicitly converts a set to a sequence for use with for and other forms.

(for/set (for-clause ...) body ...+)
(for/seteq (for-clause ...) body ...+)
(for/seteqv (for-clause ...) body ...+)
(for*/set (for-clause ...) body ...+)
(for*/seteq (for-clause ...) body ...+)
(for*/seteqv (for-clause ...) body ...+)
Analogous to for/list and for*/list, but to construct a set instead of a list.