On this page:
set?
set-eqv?
set-eq?
set
seteqv
seteq
set-empty?
set-member?
set-add
set-remove
set-union
set-intersect
set-subtract
subset?
set-map
set-for-each
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 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.

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

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

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

(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? 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.

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

(set-union set ...+)  set?
  set : set?
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.

(subset? set set2)  boolean?
  set : set?
  set2 : set?
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.

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

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

(in-set set)  sequence?
  set : 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.