3.16 Sets

► |  | Reference: Racket

1 | Language Model
2 | Syntactic Forms
3 | Datatypes
4 | Structures
5 | Classes and Objects
6 | Units
7 | Contracts
8 | Pattern Matching
9 | Control Flow
10 | Concurrency
11 | Macros
12 | Input and Output
13 | Reflection and Security
14 | Operating System
15 | Memory Management
16 | Unsafe Operations
17 | Running Racket
| Bibliography
| Index

▼ | 3 | Datatypes

3.1 | Booleans and Equality
3.2 | Numbers
3.3 | Strings
3.4 | Byte Strings
3.5 | Characters
3.6 | Symbols
3.7 | Regular Expressions
3.8 | Keywords
3.9 | Pairs and Lists
3.10 | Mutable Pairs and Lists
3.11 | Vectors
3.12 | Boxes
3.13 | Hash Tables
3.14 | Sequences
3.15 | Dictionaries
3.16 | Sets
3.17 | Procedures
3.18 | Void and Undefined

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

top← prev up next →

#### 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.

( require racket/set)

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.
top← prev up next →