# 4.17.1 Sequences
The early-next-pos procedure takes the current position and returns an updated position. This updated position is used for next-pos and continue-after-pos+val?, but not with continue-with-pos? (which uses the original current position). The intent of early-next-pos is to support a sequence where the position must be incremented to avoid keeping a value reachable while a loop processes the sequence value, so early-next-pos is applied just after pos  →  element. The continue-after-pos+val? function needs to be #f to avoid retaining values to supply to that function.
Each of the procedures listed above is called only once per position. Among the procedures continue-with-pos?, continue-with-val?, and continue-after-pos+val?, as soon as one of the procedures returns #f, the sequence ends, and none are called again. Typically, one of the functions determines the end condition, and #f is used in place of the other two functions.
Changed in version 6.7.0.4 of package base: Added support for the optional second result.
>
<colgroup><col style="width: 100%" /></colgroup>

value

prop:sequence : struct-type-property?

Associates a procedure to a structure type that takes an instance of the structure and returns a sequence. If v is an instance of a structure type with this property, then (sequence? v) produces #t.
Using a pre-existing sequence:
Examples:


# 4.17.1 Sequences
> ```racket
> > ( struct my-set ( table ) #:property prop:sequence ( lambda ( s ) ( in-hash-keys ( my-set-table s ) ) ) )
> > ( define ( make-set . xs ) ( my-set ( for/hash ( [ x ( in-list xs ) ] ) ( values x #t ) ) ) )
> > ( for/list ( [ c ( make-set ' celeriac ' carrot ' potato ) ] ) c )
> '(potato celeriac carrot)
> ```
Using make-do-sequence:
Examples:
> ```racket
> > ( require racket/sequence )
> > ( struct train ( car next ) #:property prop:sequence ( lambda ( t ) ( make-do-sequence ( lambda ( ) ( initiate-sequence #:pos  →  element train-car #:next-pos train-next #:init-pos t #:continue-with-pos? ( lambda ( t ) t ) ) ) ) ) )
> > ( for/list ( [ c ( train ' engine ( train ' boxcar ( train ' caboose #f ) ) ) ] ) c )
> '(engine boxcar caboose)
> ```


##### 4.17.1.2 Sequence Conversion
> ```
(sequence → stream seq) → stream?
  seq : sequence?```
Converts a sequence to a [stream](streams.html#%28tech._stream%29), which supports the [stream-first](streams.html#%28def._%28%28lib._racket%2Fstream..rkt%29._stream-first%29%29) and [stream-rest](streams.html#%28def._%28%28lib._racket%2Fstream..rkt%29._stream-rest%29%29) operations. Creation of the stream eagerly [initiates](#%28tech._initiate%29) the sequence, but the stream lazily draws elements from the sequence, caching each element so that [stream-first](streams.html#%28def._%28%28lib._racket%2Fstream..rkt%29._stream-first%29%29) produces the same result each time is applied to a stream.
If extracting an element from seq involves a side-effect, then the effect is performed each time that either [stream-first](streams.html#%28def._%28%28lib._racket%2Fstream..rkt%29._stream-first%29%29) or [stream-rest](streams.html#%28def._%28%28lib._racket%2Fstream..rkt%29._stream-rest%29%29) is first used to access or skip an element.
Note that a [sequence itself can have state](#%28elem._sequence-state%29), so multiple calls to [sequence->stream](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._sequence-~3estream%29%29) on the same seq are not necessarily independent.
Examples:


# 4.17.1 Sequences
> ```racket
> > ( define inport ( open-input-bytes ( bytes 1 2 3 4 5 ) ) )
> > ( define strm ( sequence->stream inport ) )
> > ( stream-first strm )
> 1
> > ( stream-first ( stream-rest strm ) )
> 2
> > ( stream-first strm )
> 1
> > ( define strm2 ( sequence->stream inport ) )
> > ( stream-first strm2 )
> 3
> > ( stream-first ( stream-rest strm2 ) )
> 4
> ```
> ```
(sequence-generate seq)    →    (  →   boolean?)   (  →   any)
  seq : sequence?
```
Initiates a sequence and returns two thunks to extract elements from the sequence. The first returns #t if more values are available for the sequence. The second returns the next element (which may be multiple values) from the sequence; if no more elements are available, the exn:fail:contract exception is raised.

Note that a sequence itself can have state, so multiple calls to sequence-generate on the same seq are not necessarily independent.

Examples:

> ```racket
> > ( define inport ( open-input-bytes ( bytes 1 2 3 4 5 ) ) )
> > ( define-values ( more? get ) ( sequence-generate inport ) )
> > ( more? )
> #t
> > ( get )
> 1
> > ( get )
> 2
> > ( define-values ( more2? get2 ) ( sequence-generate inport ) )
> > ( list ( get2 ) ( get2 ) ( get2 ) )
> '(3 4 5)
> > ( more2? )
> #f
> ```

> ```
(sequence-generate* seq)
   →    (or/c list? #f)
(  →   (values (or/c list? #f) procedure?))

  seq : sequence?```

Like [sequence-generate](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._sequence-generate%29%29), but avoids state (aside from any inherent in the sequence) by returning a list of values for the sequence’s first element—or #f if the sequence is empty—and a thunk to continue with the sequence; the result of the thunk is the same as the result of [sequence-generate*](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._sequence-generate%2A%29%29), but for the second element of the sequence, and so on. If the thunk is called when the element result is #f (indicating no further values in the sequence), the [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception is raised.

##### 4.17.1.3 Additional Sequence Operations

|                                                                                |                                                                                |
|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  ([require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/sequence](#%28mod-path._racket%2Fsequence%29)) |  package: [base](https://pkgs.racket-lang.org/package/base "Install this package using `raco pkg install base`") |

The bindings documented in this section are provided by the [racket/sequence](#%28mod-path._racket%2Fsequence%29) and [racket](index.html) libraries, but not [racket/base](index.html).

>
<colgroup><col style="width: 100%" /></colgroup>

value

[empty-sequence](#%28def._%28%28lib._racket%2Fsequence..rkt%29._empty-sequence%29%29) : [sequence?](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._sequence~3f%29%29)

A sequence with no elements.

> ```
(sequence  →  list s)  →  list?
  s : sequence?
```


# 4.17.1 Sequences
Returns a list whose elements are the elements of s, each of which must be a single value. If s is infinite, this function does not terminate.
> ```
(sequence-length s)  →  exact-nonnegative-integer?
  s : sequence?
```

Returns the number of elements of s by extracting and discarding all of them. If s is infinite, this function does not terminate.

> ```
(sequence-ref s i)  →  any
  s : sequence?
  i : exact-nonnegative-integer?
```
Returns the ith element of s (which may be multiple values).
> ```
(sequence-tail s i)  →  sequence?
  s : sequence?
  i : exact-nonnegative-integer?
```

Returns a sequence equivalent to s, except that the first i elements are omitted.

In case [initiating](#%28tech._initiate%29) s involves a side effect, the sequence s is not [initiate](#%28tech._initiate%29)d until the resulting sequence is [initiate](#%28tech._initiate%29)d, at which point the first i elements are extracted from the sequence.

> ```
(sequence-append s ...) → sequence?
  s : sequence?
```
Returns a sequence that contains all elements of each sequence in the order they appear in the original sequences. The new sequence is constructed lazily.
If all given ss are streams, the result is also a stream.
> ```
(sequence-map f s)   →   sequence?
  f : procedure?
  s : sequence?```
Returns a sequence that contains f applied to each element of s. The new sequence is constructed lazily.
If s is a [stream](streams.html#%28tech._stream%29), then the result is also a [stream](streams.html#%28tech._stream%29).
> ```


# 4.17.1 Sequences
(sequence-andmap f s) → boolean?
  f : (  →   any/c ... boolean?)
  s : sequence?
```
Returns #t if f returns a true result on every element of s. If s is infinite and f never returns a false result, this function does not terminate.

> ```
(sequence-ormap f s) → boolean?
  f : (  →   any/c ... boolean?)
  s : sequence?```

Returns #t if f returns a true result on some element of s. If s is infinite and f never returns a true result, this function does not terminate.

> ```
(parameter)	→
(-> any/c ... any)
```
Applies f to each element of s. If s is infinite, this function does not terminate.
> ```
(sequence-fold f i s) → any/c
  f : (  →   any/c any/c ... any/c)
  i : any/c
  s : sequence?```
Folds f over each element of s with i as the initial accumulator. If s is infinite, this function does not terminate. The f function takes the accumulator as its first argument and the next sequence element as its second.
> ```
(sequence-count f s)  →  exact-nonnegative-integer?
  f : procedure?
  s : sequence?


# 4.17.1 Sequences
```
Returns the number of elements in s for which f returns a true result. If s is infinite, this function does not terminate.

> ```
(sequence-filter f s) → sequence?
  f : (  →   any/c ... boolean?)
  s : sequence?```

Returns a sequence whose elements are the elements of s for which f returns a true result. Although the new sequence is constructed lazily, if s has an infinite number of elements where f returns a false result in between two elements where f returns a true result, then operations on this sequence will not terminate during the infinite sub-sequence.

If s is a [stream](streams.html#%28tech._stream%29), then the result is also a [stream](streams.html#%28tech._stream%29).

> ```
(sequence-add-between s e)  →  sequence?
  s : sequence?
  e : any/c
```
Returns a sequence whose elements are the elements of s, but with e between each pair of elements in s. The new sequence is constructed lazily.
If s is a stream, then the result is also a stream.
Examples:
> ```racket
> > ( let* ( [ all-reds ( in-cycle ' ( "red" ) ) ] [ red-and-blues ( sequence-add-between all-reds "blue" ) ] ) ( for/list ( [ n ( in-range 10 ) ] [ elt red-and-blues ] ) elt ) )
> '("red" "blue" "red" "blue" "red" "blue" "red" "blue" "red" "blue")
> > ( for ( [ text ( sequence-add-between ' ( "veni" "vidi" "duci" ) ", " ) ] ) ( display text ) )
> veni, vidi, duci
> ```
> ```
(sequence/c [#:min-count min-count]
    elem/c ...)    →    contract?
  min-count : (or/c #f exact-nonnegative-integer?) = #f
  elem/c : contract?```


# 4.17.1 Sequences
Wraps a [sequence](#%28tech._sequence%29), obligating it to produce elements with as many values as there are elem/c contracts, and obligating each value to satisfy the corresponding elem/c. The result is not guaranteed to be the same kind of sequence as the original value; for instance, a wrapped list is not guaranteed to satisfy [list?](pairs.html#%28def._%28%28quote._~23~25kernel%29._list~3f%29%29).
If min-count is a number, the stream is required to have at least that many elements in it.
Examples:
> ```racket
> > ( define/contract predicates ( sequence/c ( -> any/c boolean? ) ) ( in-list ( list integer? string->symbol ) ) )
> > ( for ( [ P predicates ] ) ( printf "~s\n" ( P "cat" ) ) )
> #f
> predicates: broke its own contract
> promised: boolean?
> produced: 'cat
> in: an element of
> (sequence/c (-> any/c boolean?))
> contract from: (definition predicates)
> blaming: (definition predicates)
> (assuming the contract is correct)
> at: eval:55:0
> > ( define/contract numbers&strings ( sequence/c number? string? ) ( in-dict ( list ( cons 1 "one" ) ( cons 2 "two" ) ( cons 3 ' three ) ) ) )
> > ( for ( [ ( N S ) numbers&strings ] ) ( printf "~s: ~a\n" N S ) )
> 1: one 2: two
> numbers&strings: broke its own contract
> promised: string?
> produced: 'three
> in: an element of
> (sequence/c number? string?)
> contract from: (definition numbers&strings)
> blaming: (definition numbers&strings)
> (assuming the contract is correct)
> at: eval:57:0
> > ( define/contract a-sequence ( sequence/c #:min-count 2 char? ) "x" )
> > ( for ( [ x a-sequence ] [ i ( in-naturals ) ] ) ( printf "~a is ~a\n" i x ) )
> 0 is x
> a-sequence: broke its own contract
> promised: a sequence that contains at least 2 values
> produced: "x"
> in: (sequence/c #:min-count 2 char?)
> contract from: (definition a-sequence)
> blaming: (definition a-sequence)
> (assuming the contract is correct)
> at: eval:59:0
> ```


##### 4.17.1.3.1 Additional Sequence Constructors and Functions
> ```
(in-syntax stx)  →  sequence?
  stx : syntax?
```
Produces a sequence whose elements are the successive subparts of stx. Equivalent to (stx  →  list lst).

An in-syntax application can provide better performance for syntax iteration when it appears directly in a for clause.

Example:

> ```racket
> > ( for/list ( [ x ( in-syntax #' ( 1 2 3 ) ) ] ) x )
> '(#<syntax:eval:61:0 1> #<syntax:eval:61:0 2> #<syntax:eval:61:0 3>)
> ```

Added in version 6.3 of package base.

> ```
(in-slice length seq) → sequence?
  length : exact-positive-integer?
  seq : sequence?```

Returns a sequence whose elements are lists with the first length elements of seq, then the next length and so on.

Example:

> ```racket
> > ( for/list ( [ e ( in-slice 3 ( in-range 8 ) ) ] ) e )
> '((0 1 2) (3 4 5) (6 7))
> ```

Added in version 6.3 of package base.

> ```
(initiate-sequence
    #:pos  →  element pos  →  element
   [ #:early-next-pos early-next-pos]
    #:next-pos next-pos
    #:init-pos init-pos
   [ #:continue-with-pos? continue-with-pos?
    #:continue-with-val? continue-with-val?
    #:continue-after-pos+val? continue-after-pos+val?)

   →    (any/c .  →  . any)
(or/c (any/c .  →  . any) #f)
(any/c .  →  . any/c)
any/c
(or/c (any/c .  →  . any/c) #f)
(or/c (any/c ... .  →  . any/c) #f)
(or/c (any/c any/c ... .  →  . any/c) #f)

  pos  →  element : (any/c .  →  . any)
  early-next-pos : (or/c (any/c .  →  . any) #f) = #f
  next-pos : (any/c .  →  . any/c)
  init-pos : any/c
  continue-with-pos? : (or/c (any/c .  →  . any/c) #f) = #f
  continue-with-val? : (or/c (any/c ... .  →  . any/c) #f) = #f
   continue-after-pos+val?   :   (or/c (any/c any/c ... .  →  . any/c) #f)
      =   #f
```


# 4.17.1 Sequences
Returns values suitable for the thunk argument in make-do-sequence. See make-do-sequence for the meaning of each argument.
Examples:
> ```racket
> > ( define ( in-alt-list xs ) ( make-do-sequence ( λ ( ) ( initiate-sequence #:pos  →  element car #:next-pos ( λ ( xs ) ( cdr ( cdr xs ) ) ) #:init-pos xs #:continue-with-pos? pair? #:continue-after-pos+val? ( λ ( xs _ ) ( pair? ( cdr xs ) ) ) ) ) ) )
> > ( sequence  →  list ( in-alt-list ' ( 1 2 3 4 5 6 ) ) )
> '(1 3 5)
> > ( sequence  →  list ( in-alt-list ' ( 1 2 3 4 5 6 7 ) ) )
> '(1 3 5 7)
> ```
Added in version 8.10.0.5 of package base.
------------------------------------------------------------------------


# 4.17.2 Streams


#### 4.17.2 Streams
A stream is a kind of sequence that supports functional iteration via stream-first and stream-rest. The stream-cons form constructs a lazy stream, but plain lists can be used as streams, and functions such as in-range and in-naturals also create streams.
|  |  |
|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  (require [racket/stream]()) |  package: base |
The bindings documented in this section are provided by the [racket/stream]() and racket libraries, but not racket/base.
> ```
(stream? v) → boolean?
  v : any/c```
Returns #t if v can be used as a [stream](#%28tech._stream%29), #f otherwise.
> ```
(stream-empty? s)  →  boolean?
  s : stream?
```

Returns #t if s has no elements, #f otherwise.

> ```
(stream-first s)  →  any
  s : (and/c stream? (not/c stream-empty?))
```
Returns the value(s) of the first element in s.
> ```
(stream-rest s)  →  stream?
  s : (and/c stream? (not/c stream-empty?))


# 4.17.2 Streams
```
Returns a stream that is equivalent to s without its first element.

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>(stream-cons first-expr rest-expr)</p></blockquote></td></tr><tr><td>(stream-cons #:eager first-expr rest-expr)</td></tr><tr><td>(stream-cons first-expr #:eager rest-expr)</td></tr><tr><td>(stream-cons #:eager first-expr #:eager rest-expr)</td></tr></tbody></table>

Produces a stream whose first element is determined by first-expr and whose rest is determined by rest-expr.

If first-expr is not preceded by #:eager, then first-expr is not evaluated immediately. Instead, stream-first on the result stream forces the evaluation of first-expr (once) to produce the first element of the stream. If evaluating first-expr raises an exception or tries to force itself, then an exn:fail:contract exception is raised, and future attempts to force evaluation will trigger another exception.

If rest-expr is not preceded by #:eager, then rest-expr is not evaluated immediately. Instead, stream-rest on the result stream produces another stream that is like the one produced by (stream-lazy rest-expr).

The first element of the stream as produced by first-expr can be multiple values. The rest-expr must produce a stream when it is evaluated, otherwise the exn:fail:contract? exception is raised.

Changed in version 8.0.0.12 of package base: Added #:eager options.
Changed in version 8.8.0.7: Changed to allow multiple values.

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>(stream-lazy stream-expr)</p></blockquote></td></tr><tr><td>(stream-lazy #:who who-expr stream-expr)</td></tr></tbody></table>

Similar to (delay stream-expr), but the result is a stream instead of a promise, and stream-expr must produce a stream when it is eventually forced. The stream produced by stream-lazy has the same content as the stream produced by stream-expr; that is, operations like stream-first on the result stream will force stream-expr and retry on its result.

If evaluating stream-expr raises an exception or tries to force itself, then an exn:fail:contract exception is raised, and future attempts to force evaluation will trigger another exception.

If who-expr is provided, it is evaluated when constructing the delayed stream. If stream-expr later produces a value that is not a stream, and if who-expr produced a symbol value, then the symbol is used for the error message.

Added in version 8.0.0.12 of package base.

> ```
(stream-force s) → stream?
  s : stream?```

Forces the evaluation of a delayed stream from [stream-lazy](#%28form._%28%28lib._racket%2Fstream..rkt%29._stream-lazy%29%29), from the [stream-rest](#%28def._%28%28lib._racket%2Fstream..rkt%29._stream-rest%29%29) of a [stream-cons](#%28form._%28%28lib._racket%2Fstream..rkt%29._stream-cons%29%29), etc., returning the forced stream. If s is not a delayed stream, then s is returned.

Normally, [stream-force](#%28def._%28%28lib._racket%2Fstream..rkt%29._stream-force%29%29) is not needed, because operations like [stream-first](#%28def._%28%28lib._racket%2Fstream..rkt%29._stream-first%29%29), [stream-rest](#%28def._%28%28lib._racket%2Fstream..rkt%29._stream-rest%29%29), and [stream-empty?](#%28def._%28%28lib._racket%2Fstream..rkt%29._stream-empty~3f%29%29) force a delayed stream as needed. In rare cases, [stream-force](#%28def._%28%28lib._racket%2Fstream..rkt%29._stream-force%29%29) can be useful to reveal the underlying implementation of a stream (e.g., a stream that is an instance of a structure type that has the [prop:stream](#%28def._%28%28lib._racket%2Fstream..rkt%29._prop~3astream%29%29) property).

Added in version 8.0.0.12 of package base.

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([stream](#%28form._%28%28lib._racket%2Fstream..rkt%29._stream%29%29) elem-expr ...)</p></blockquote></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td style="text-align: right;" data-valign="baseline">elem-expr</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([values](values.html#%28def._%28%28quote._~23~25kernel%29._values%29%29) single-expr ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">single-expr</td></tr></tbody></table></td></tr></tbody></table>

A shorthand for nested [stream-cons](#%28form._%28%28lib._racket%2Fstream..rkt%29._stream-cons%29%29)es ending with [empty-stream](#%28def._%28%28lib._racket%2Fstream..rkt%29._empty-stream%29%29). As a match pattern, [stream](#%28form._%28%28lib._racket%2Fstream..rkt%29._stream%29%29) matches a stream with as many elements as elem-exprs, and each element must match the corresponding elem-expr pattern. The pattern elem-expr can be ([values](values.html#%28def._%28%28quote._~23~25kernel%29._values%29%29) single-expr [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)), which matches against multiple valued elements in the stream.

Changed in version 8.8.0.7 of package base: Changed to allow multiple values.

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([stream*](#%28form._%28%28lib._racket%2Fstream..rkt%29._stream%2A%29%29) elem-expr ... tail-expr)</p></blockquote></td></tr></tbody></table>

A shorthand for nested [stream-cons](#%28form._%28%28lib._racket%2Fstream..rkt%29._stream-cons%29%29)es, but the tail-expr must produce a stream when it is forced, and that stream is used as the rest of the stream instead of [empty-stream](#%28def._%28%28lib._racket%2Fstream..rkt%29._empty-stream%29%29). Similar to [list*](pairs.html#%28def._%28%28quote._~23~25kernel%29._list%2A%29%29) but for streams. As a match pattern, [stream*](#%28form._%28%28lib._racket%2Fstream..rkt%29._stream%2A%29%29) is similar to a [stream](#%28form._%28%28lib._racket%2Fstream..rkt%29._stream%29%29) pattern, but the tail-expr pattern matches the “rest” of the stream after the last elem-expr.

Added in version 6.3 of package base.
Changed in version 8.0.0.12: Changed to delay rest-expr even if zero exprs are provided.
Changed in version 8.8.0.7: Changed to allow multiple values.

> ```
(in-stream s)  →  sequence?
  s : stream?
```


# 4.17.2 Streams
Returns a sequence that is equivalent to s.
An in-stream application can provide better performance for streams iteration when it appears directly in a for clause.
See for for information on the reachability of stream elements during an iteration.
Changed in version 6.7.0.4 of package base: Improved element-reachability guarantee for streams in for.
>
<colgroup><col style="width: 100%" /></colgroup>

value

empty-stream : stream?

A stream with no elements.
> ```
(stream → list s) → list?
  s : stream?```
Returns a list whose elements are the elements of s, each of which must be a single value. If s is infinite, this function does not terminate.
> ```
(stream-length s)  →  exact-nonnegative-integer?
  s : stream?
```

Returns the number of elements of s. If s is infinite, this function does not terminate.

In the case of lazy streams, this function forces evaluation only of the sub-streams, and not the stream’s elements.

> ```
(stream-ref s i)  →  any
  s : stream?
  i : exact-nonnegative-integer?
```
Returns the ith element of s (which may be multiple values).
> ```
(stream-tail s i)  →  stream?
  s : stream?
  i : exact-nonnegative-integer?


# 4.17.2 Streams
```

Returns a stream equivalent to s, except that the first i elements are omitted.

In case extracting elements from s involves a side effect, they will not be extracted until the first element is extracted from the resulting stream.

> ```
(stream-take s i)  →  stream?
  s : stream?
  i : exact-nonnegative-integer?
```
Returns a stream of the first i elements of s.
> ```
(stream-append s ...) → stream?
  s : stream?
```
Returns a stream that contains all elements of each stream in the order they appear in the original streams. The new stream is constructed lazily, while the last given stream is used in the tail of the result.

> ```
(stream-map f s)   →   stream?
  f : procedure?
  s : stream?```

Returns a stream that contains f applied to each element of s. The new stream is constructed lazily.

> ```
(stream-andmap f s) → boolean?
  f : (  →   any/c ... boolean?)
  s : stream?
```
Returns #t if f returns a true result on every element of s. If s is infinite and f never returns a false result, this function does not terminate.
> ```
(stream-ormap f s) → boolean?
  f : (  →   any/c ... boolean?)
  s : stream?```
Returns #t if f returns a true result on some element of s. If s is infinite and f never returns a true result, this function does not terminate.
> ```
(parameter)	→
(-> any/c ... any)


# 4.17.2 Streams
```
Applies f to each element of s. If s is infinite, this function does not terminate.

> ```
(stream-fold f i s) → any/c
  f : (  →   any/c any/c ... any/c)
  i : any/c
  s : stream?```

Folds f over each element of s with i as the initial accumulator. If s is infinite, this function does not terminate. The f function takes the accumulator as its first argument and the next stream element as its second.

> ```
(stream-count f s)  →  exact-nonnegative-integer?
  f : procedure?
  s : stream?
```
Returns the number of elements in s for which f returns a true result. If s is infinite, this function does not terminate.
> ```
(stream-filter f s) → stream?
  f : (  →   any/c ... boolean?)
  s : stream?```
Returns a stream whose elements are the elements of s for which f returns a true result. Although the new stream is constructed lazily, if s has an infinite number of elements where f returns a false result, then operations on this stream will not terminate during the infinite sub-stream.
> ```
(stream-add-between s e)  →  stream?
  s : stream?
  e : any/c


# 4.17.2 Streams
```
Returns a stream whose elements are the elements of s, but with e between each pair of elements in s. The new stream is constructed lazily.

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>(for/stream (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>(for*/stream (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table></td></tr></tbody></table>

Iterates like for/list and for*/list, respectively, but the results are lazily collected into a stream instead of a list.

Unlike most for forms, these forms are evaluated lazily, so each body will not be evaluated until the resulting stream is forced. This allows for/stream and for*/stream to iterate over infinite sequences, unlike their finite counterparts.

Examples:

> ```racket
> > ( for/stream ( [ i ' ( 1 2 3 ) ] ) ( * i i ) )
> #<stream>
> > ( stream  →  list ( for/stream ( [ i ' ( 1 2 3 ) ] ) ( * i i ) ) )
> '(1 4 9)
> > ( stream-ref ( for/stream ( [ i ' ( 1 2 3 ) ] ) ( displayln i ) ( * i i ) ) 1 )
> 2
> 4
> > ( stream-ref ( for/stream ( [ i ( in-naturals ) ] ) ( * i i ) ) 25 )
> 625
> > ( stream-ref ( for/stream ( [ i ( in-naturals ) ] ) ( values i ( add1 i ) ) ) 10 )
> 10 11
> ```

Added in version 6.3.0.9 of package base.
Changed in version 8.8.0.7: Changed to allow multiple values.

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>value</p>

<p>gen:stream : any/c</p></blockquote></td></tr></tbody></table>

Associates three methods to a structure type to implement the generic interface (see Generic Interfaces) for streams.

To supply method implementations, the #:methods keyword should be used in a structure type definition. The following three methods must be implemented:

-  stream-empty? : accepts one argument

-  stream-first : accepts one argument

-  stream-rest : accepts one argument

Examples:

> ```racket
> > ( struct list-stream ( v ) #:methods gen:stream [ ( define ( stream-empty? stream ) ( empty? ( list-stream-v stream ) ) ) ( define ( stream-first stream ) ( first ( list-stream-v stream ) ) ) ( define ( stream-rest stream ) ( list-stream ( rest ( list-stream-v stream ) ) ) ) ] )
> > ( define l1 ( list-stream ' ( 1 2 ) ) )
> > ( stream? l1 )
> #t
> > ( stream-first l1 )
> 1
> ```

Changed in version 8.7.0.5 of package base: Added a check so that omitting any of stream-empty?, stream-first, and stream-rest is now a syntax error.

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>value</p>

<p>prop:stream : struct-type-property?</p></blockquote></td></tr></tbody></table>

A structure type property used to define custom extensions to the stream API. Using the prop:stream property is discouraged; use the gen:stream generic interface instead. Accepts a vector of three procedures taking the same arguments as the methods in gen:stream.

> ```
(stream/c c) → contract?
  c : contract?```

Returns a contract that recognizes streams. All elements of the stream must match c.

If the c argument is a flat contract or a chaperone contract, then the result will be a chaperone contract. Otherwise, the result will be an impersonator contract.

When an [stream/c](#%28def._%28%28lib._racket%2Fstream..rkt%29._stream%2Fc%29%29) contract is applied to a stream, the result is not [eq?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29) to the input. The result will be either a [chaperone](chaperones.html#%28tech._chaperone%29) or [impersonator](chaperones.html#%28tech._impersonator%29) of the input depending on the type of contract.

Contracts on streams are evaluated lazily by necessity (since streams may be infinite). Contract violations will not be raised until the value in violation is retrieved from the stream. As an exception to this rule, streams that are lists are checked immediately, as if c had been used with [listof](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._listof%29%29).

If a contract is applied to a stream, and that stream is subsequently used as the tail of another stream (as the second parameter to [stream-cons](#%28form._%28%28lib._racket%2Fstream..rkt%29._stream-cons%29%29)), the new elements will not be checked with the contract, but the tail’s elements will still be enforced.

Added in version 6.1.1.8 of package base.

------------------------------------------------------------------------

# 4.17.3 Generators

#### 4.17.3 Generators

A generator is a procedure that returns a sequence of values, incrementing the sequence each time that the generator is called. In particular, the [generator](#%28form._%28%28lib._racket%2Fgenerator..rkt%29._generator%29%29) form implements a generator by evaluating a body that calls [yield](#%28def._%28%28lib._racket%2Fgenerator..rkt%29._yield%29%29) to return values from the generator.

|                                                                                |                                                                                |
|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  ([require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/generator]()) |  package: [base](https://pkgs.racket-lang.org/package/base "Install this package using `raco pkg install base`") |

> ```
(generator? v)  →  boolean?
  v : any/c
```


# 4.17.2 Streams
Return #t if v is a generator, #f otherwise.
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(generator formals body ...+)

formals	 	=	 	(id ...)

 	 	|	 	(id ...+ . rest-id)

 	 	|	 	rest-id

Creates a generator, where formals specify the arguments. Keyword and optional arguments are not supported. This is the same as the formals of a single case-lambda clause.


# 4.17.2 Streams
For the first call to a generator, the arguments are bound to the formals and evaluation of body starts. During the dynamic extent of body, the generator can return immediately using the yield function. A second call to the generator resumes at the yield call, producing the arguments of the second call as the results of the yield, and so on. The eventual results of body are supplied to an implicit final yield; after that final yield, calling the generator again returns the same values, but all such calls must provide 0 arguments to the generator.
Examples:
> ```racket
> > ( define g ( generator ( ) ( let loop ( [ x ' ( a b c ) ] ) ( if ( null? x ) 0 ( begin ( yield ( car x ) ) ( loop ( cdr x ) ) ) ) ) ) )
> > ( g )
> 'a
> > ( g )
> 'b
> > ( g )
> 'c
> > ( g )
> 0
> > ( g )
> 0
> ```
> ```
(yield v ...) → any
  v : any/c```
Returns vs from a generator, saving the point of execution inside a generator (i.e., within the [dynamic extent](eval-model.html#%28tech._dynamic._extent%29) of a [generator](#%28form._%28%28lib._racket%2Fgenerator..rkt%29._generator%29%29) body) to be resumed by the next call to the generator. The results of [yield](#%28def._%28%28lib._racket%2Fgenerator..rkt%29._yield%29%29) are the arguments that are provided to the next call of the generator.


# 4.17.2 Streams
When not in the [dynamic extent](eval-model.html#%28tech._dynamic._extent%29) of a [generator](#%28form._%28%28lib._racket%2Fgenerator..rkt%29._generator%29%29), [infinite-generator](#%28form._%28%28lib._racket%2Fgenerator..rkt%29._infinite-generator%29%29), or [in-generator](#%28form._%28%28lib._racket%2Fgenerator..rkt%29._in-generator%29%29) body, [yield](#%28def._%28%28lib._racket%2Fgenerator..rkt%29._yield%29%29) raises [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29).
Examples:
> ```racket
> > ( define my-generator ( generator ( ) ( yield 1 ) ( yield 2 3 4 ) ) )
> > ( my-generator )
> 1
> > ( my-generator )
> 2 3 4
> ```
Examples:
> ```racket
> > ( define pass-values-generator ( generator ( ) ( let* ( [ from-user ( yield 2 ) ] [ from-user-again ( yield ( add1 from-user ) ) ] ) ( yield from-user-again ) ) ) )
> > ( pass-values-generator )
> 2
> > ( pass-values-generator 5 )
> 6
> > ( pass-values-generator 12 )
> 12
> ```
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

([infinite-generator](#%28form._%28%28lib._racket%2Fgenerator..rkt%29._infinite-generator%29%29) body ...+)

Like [generator](#%28form._%28%28lib._racket%2Fgenerator..rkt%29._generator%29%29), but repeats evaluation of the bodys when the last body completes without implicitly [yield](#%28def._%28%28lib._racket%2Fgenerator..rkt%29._yield%29%29)ing.
Examples:


# 4.17.2 Streams
> ```racket
> > ( define welcome ( infinite-generator ( yield ' hello ) ( yield ' goodbye ) ) )
> > ( welcome )
> 'hello
> > ( welcome )
> 'goodbye
> > ( welcome )
> 'hello
> > ( welcome )
> 'goodbye
> ```
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

([in-generator](#%28form._%28%28lib._racket%2Fgenerator..rkt%29._in-generator%29%29) maybe-arity body ...+)

<colgroup><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /></colgroup>

maybe-arity	 	=

 	 	|	 	#:arity arity-k


# 4.17.2 Streams
Produces a [sequence](sequences.html#%28tech._sequence%29) that encapsulates the [generator](#%28tech._generator%29) formed by ([generator](#%28form._%28%28lib._racket%2Fgenerator..rkt%29._generator%29%29) () body [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)). The values produced by the generator form the elements of the sequence, except for the last value produced by the generator (i.e., the values produced by returning).
Example:
> ```racket
> > ( for/list ( [ i ( in-generator ( let loop ( [ x ' ( a b c ) ] ) ( when ( not ( null? x ) ) ( yield ( car x ) ) ( loop ( cdr x ) ) ) ) ) ] ) i )
> '(a b c)
> ```
If [in-generator](#%28form._%28%28lib._racket%2Fgenerator..rkt%29._in-generator%29%29) is used immediately with a [for](for.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) (or [for/list](for.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Flist%29%29), etc.) binding’s right-hand side, then its result arity (i.e., the number of values in each element of the sequence) can be inferred. Otherwise, if the generator produces multiple values for each element, its arity should be declared with an #:arity arity-k clause; the arity-k must be a literal, exact, non-negative integer.
Examples:


# 4.17.2 Streams
> ```racket
> > ( let ( [ g ( in-generator ( let loop ( [ n 3 ] ) ( unless ( zero? n ) ( yield n ( add1 n ) ) ( loop ( sub1 n ) ) ) ) ) ] ) ( let-values ( [ ( not-empty? next ) ( sequence-generate g ) ] ) ( let loop ( ) ( when ( not-empty? ) ( next ) ( loop ) ) ) ' done ) )
> stop?: arity mismatch;
> the expected number of arguments does not match the given
> number
> expected: 1
> given: 2
> > ( let ( [ g ( in-generator #:arity 2 ( let loop ( [ n 3 ] ) ( unless ( zero? n ) ( yield n ( add1 n ) ) ( loop ( sub1 n ) ) ) ) ) ] ) ( let-values ( [ ( not-empty? next ) ( sequence-generate g ) ] ) ( let loop ( ) ( when ( not-empty? ) ( next ) ( loop ) ) ) ' done ) )
> 'done
> ```
To use an existing generator as a sequence, use [in-producer](sequences.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._in-producer%29%29) with a stop-value known for the generator:
> ```racket
> > ( define abc-generator ( generator ( ) ( for ( [ x ' ( a b c ) ] ) ( yield x ) ) ) )
> > ( for/list ( [ i ( in-producer abc-generator ( void ) ) ] ) i )
> '(a b c)
> > ( define my-stop-value ( gensym ) )
> > ( define my-generator ( generator ( ) ( let loop ( [ x ( list ' a ( void ) ' c ) ] ) ( if ( null? x ) my-stop-value ( begin ( yield ( car x ) ) ( loop ( cdr x ) ) ) ) ) ) )
> > ( for/list ( [ i ( in-producer my-generator my-stop-value ) ] ) i )
> '(a #<void> c)
> ```
> ```
(parameter)	→


# 4.17.2 Streams
```
Returns a symbol that describes the state of the generator.

-  'fresh — The generator has been freshly created and has not been called yet.

-  'suspended — Control within the generator has been suspended due to a call to yield. The generator can be called.

-  'running — The generator is currently executing.

-  'done — The generator has executed its entire body and will continue to produce the same result as from the last call.

Examples:

> ```racket
> > ( define my-generator ( generator ( ) ( yield 1 ) ( yield 2 ) ) )
> > ( generator-state my-generator )
> 'fresh
> > ( my-generator )
> 1
> > ( generator-state my-generator )
> 'suspended
> > ( my-generator )
> 2
> > ( generator-state my-generator )
> 'suspended
> > ( my-generator )
> > ( generator-state my-generator )
> 'done
> > ( define introspective-generator ( generator ( ) ( ( yield 1 ) ) ) )
> > ( introspective-generator )
> 1
> > ( introspective-generator ( lambda ( ) ( generator-state introspective-generator ) ) )
> 'running
> > ( generator-state introspective-generator )
> 'done
> > ( introspective-generator )
> 'running
> ```

> ```
(sequence  →  generator s)   →   (  →   any)
  s : sequence?```

Converts a [sequence](sequences.html#%28tech._sequence%29) to a [generator](#%28tech._generator%29). The generator returns the next element of the sequence each time the generator is invoked, where each element of the sequence must be a single value. When the sequence ends, the generator returns [#<void>](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=void_undefined.html&version=8.18.0.13) as its final result.

> ```
(sequence  →  repeated-generator s)  →  (  →   any)
  s : sequence?
```


# 4.17.2 Streams
Like sequence  →  generator, but when s has no further values, the generator starts the sequence again (so that the generator never stops producing values).
------------------------------------------------------------------------


# 4.18 Dictionaries


### 4.18 Dictionaries
A dictionary is an instance of a datatype that maps keys to values. The following datatypes are all dictionaries:
-  hash tables;
-  vectors (using only exact integers as keys);
-  lists of pairs as an association list using equal? to compare keys, which must be distinct; and
-  structures whose types implement the gen:dict generic interface.
When list of pairs is used as association list but does not have distinct keys (so it’s not an association list), operations like dict-ref and dict-remove operate on the first instance of the key, while operations like dict-map and dict-keys produce an element for every instance of the key.
|  |  |
|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  (require [racket/dict]()) |  package: base |
The bindings documented in this section are provided by the [racket/dict]() and racket libraries, but not racket/base.


#### 4.18.1 Dictionary Predicates and Contracts
> ```
(dict? v) → boolean?
  v : any/c```
Returns #t if v is a [dictionary](#%28tech._dictionary%29), #f otherwise.
Beware that [dict?](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict~3f%29%29) is not a constant-time test on pairs, since checking that v is an [association list](#%28tech._association._list%29) may require traversing the list.
Examples:
> ```racket
> > ( dict? #hash ( ( a . "apple" ) ) )
> #t
> > ( dict? ' # ( "apple" "banana" ) )
> #t
> > ( dict? ' ( "apple" "banana" ) )
> #f
> > ( dict? ' ( ( a . "apple" ) ( b . "banana" ) ) )
> #t
> ```
> ```
(parameter)	→


# 4.18 Dictionaries
```
(parameter)	→```

Recognizes dictionaries that support all of the methods from [gen:dict](#%28form._%28%28lib._racket%2Fdict..rkt%29._gen~3adict%29%29) named by the syms. Note that the generated contract is not similar to [hash/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fhash..rkt%29._hash%2Fc%29%29), but closer to [dict-implements?](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-implements~3f%29%29).

Examples:

> ```racket
> > ( struct deformed-dict ( ) #:methods gen:dict [ ] )
> > ( define/contract good-dict ( dict-implements/c ) ( deformed-dict ) )
> > ( define/contract bad-dict ( dict-implements/c ' dict-ref ) ( deformed-dict ) )
> bad-dict: broke its own contract
> promised: (dict-implements/c dict-ref)
> produced: #<deformed-dict>
> in: (dict-implements/c dict-ref)
> contract from: (definition bad-dict)
> blaming: (definition bad-dict)
> (assuming the contract is correct)
> at: eval:14:0
> ```

> ```
(dict-mutable? d)  →  boolean?
  d : dict?
```
Returns #t if d is mutable via dict-set!, #f otherwise.
Equivalent to (dict-implements? d 'dict-set!).
Examples:
> ```racket
> > ( dict-mutable? #hash ( ( a . "apple" ) ) )
> #f
> > ( dict-mutable? ( make-hash ) )
> #t
> > ( dict-mutable? ' # ( "apple" "banana" ) )
> #f
> > ( dict-mutable? ( vector "apple" "banana" ) )
> #t
> > ( dict-mutable? ' ( ( a . "apple" ) ( b . "banana" ) ) )
> #f
> ```
> ```
(dict-can-remove-keys? d)   →   boolean?
  d : dict?```


# 4.18 Dictionaries
Returns #t if d supports removing mappings via [dict-remove!](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-remove%21%29%29) and/or [dict-remove](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-remove%29%29), #f otherwise.
Equivalent to ([or](if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29) ([dict-implements?](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-implements~3f%29%29) d 'dict-remove!) ([dict-implements?](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-implements~3f%29%29) d 'dict-remove)).
Examples:
> ```racket
> > ( dict-can-remove-keys? #hash ( ( a . "apple" ) ) )
> #t
> > ( dict-can-remove-keys? ' # ( "apple" "banana" ) )
> #f
> > ( dict-can-remove-keys? ' ( ( a . "apple" ) ( b . "banana" ) ) )
> #t
> ```
> ```
(dict-can-functional-set? d)  →  boolean?
  d : dict?


# 4.18 Dictionaries
```
Returns #t if d supports functional update via dict-set, #f otherwise.

Equivalent to (dict-implements? d 'dict-set).

Examples:

> ```racket
> > ( dict-can-functional-set? #hash ( ( a . "apple" ) ) )
> #t
> > ( dict-can-functional-set? ( make-hash ) )
> #f
> > ( dict-can-functional-set? ' # ( "apple" "banana" ) )
> #f
> > ( dict-can-functional-set? ' ( ( a . "apple" ) ( b . "banana" ) ) )
> #t
> ```

#### 4.18.2 Generic Dictionary Interface

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>gen:dict</p></blockquote></td></tr></tbody></table>

A generic interface (see Generic Interfaces) that supplies dictionary method implementations for a structure type via the #:methods option of struct definitions. This interface can be used to implement any of the methods documented as Primitive Dictionary Methods and Derived Dictionary Methods.

Examples:

> ```racket
> > ( struct alist ( v ) #:methods gen:dict [ ( define ( dict-ref dict key [ default ( lambda ( ) ( error "key not found" key ) ) ] ) ( cond [ ( assoc key ( alist-v dict ) ) => cdr ] [ else ( if ( procedure? default ) ( default ) default ) ] ) ) ( define ( dict-set dict key val ) ( alist ( cons ( cons key val ) ( alist-v dict ) ) ) ) ( define ( dict-remove dict key ) ( define al ( alist-v dict ) ) ( alist ( remove* ( filter ( λ ( p ) ( equal? ( car p ) key ) ) al ) al ) ) ) ( define ( dict-count dict ) ( length ( remove-duplicates ( alist-v dict ) #:key car ) ) ) ] )
> ; etc. other methods
> > ( define d1 ( alist ' ( ( 1 . a ) ( 2 . b ) ) ) )
> > ( dict? d1 )
> #t
> > ( dict-ref d1 1 )
> 'a
> > ( dict-remove d1 1 )
> #<alist>
> ```

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>value</p>

<p>prop:dict : struct-type-property?</p></blockquote></td></tr></tbody></table>

A structure type property used to define custom extensions to the dictionary API. Using the prop:dict property is discouraged; use the gen:dict generic interface instead. Accepts a vector of 10 method implementations:

-  dict-ref

-  dict-set!, or #f if unsupported

-  dict-set, or #f if unsupported

-  dict-remove!, or #f if unsupported

-  dict-remove, or #f if unsupported

-  dict-count

-  dict-iterate-first

-  dict-iterate-next

-  dict-iterate-key

-  dict-iterate-value

##### 4.18.2.1 Primitive Dictionary Methods

These methods of gen:dict have no fallback implementations; they are only supported for dictionary types that directly implement them.

> ```
(dict-ref dict key [failure-result) → any
  dict : dict?
  key : any/c
   failure-result   :   failure-result/c
      =   (lambda () (raise (make-exn:fail ....)))```

Returns the value for key in dict. If no value is found for key, then failure-result determines the result:

-   If failure-result is a procedure, it is called (through a tail call) with no arguments to produce the result.

-   Otherwise, failure-result is returned as the result.

Examples:

> ```racket
> > ( dict-ref #hash ( ( a . "apple" ) ( b . "beer" ) ) ' a )
> "apple"
> > ( dict-ref #hash ( ( a . "apple" ) ( b . "beer" ) ) ' c )
> hash-ref: no value found for key
> key: 'c
> > ( dict-ref #hash ( ( a . "apple" ) ( b . "beer" ) ) ' c #f )
> #f
> > ( dict-ref ' ( ( a . "apple" ) ( b . "banana" ) ) ' b )
> "banana"
> > ( dict-ref # ( "apple" "banana" ) 1 )
> "banana"
> > ( dict-ref # ( "apple" "banana" ) 3 #f )
> #f
> > ( dict-ref # ( "apple" "banana" ) -3 #f )
> dict-ref: contract violation
> expected: natural?
> given: -3
> in: the k argument of
> (->i
> ((d dict?) (k (d) (dict-key-contract d)))
> ((default any/c))
> any)
> contract from: <collects>/racket/dict.rkt
> blaming: top-level
> (assuming the contract is correct)
> at: <collects>/racket/dict.rkt:182:2
> ```

> ```
(parameter)	→
(-> and/c dict? (not/c immutable?))
```


# 4.18 Dictionaries
Maps key to v in dict, overwriting any existing mapping for key. The update can fail with a exn:fail:contract exception if dict is not mutable or if key is not an allowed key for the dictionary (e.g., not an exact integer in the appropriate range when dict is a vector).
Examples:
> ```racket
> > ( define h ( make-hash ) )
> > ( dict-set! h ' a "apple" )
> > h
> '#hash((a . "apple"))
> > ( define v ( vector #f #f #f ) )
> > ( dict-set! v 0 "apple" )
> > v
> '#("apple" #f #f)
> ```
> ```
(dict-set dict key v)   →   (and/c dict? immutable?)
  dict : (and/c dict? immutable?)
  key : any/c
  v : any/c```
Functionally extends dict by mapping key to v, overwriting any existing mapping for key, and returning an extended dictionary. The update can fail with a [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception if dict does not support functional extension or if key is not an allowed key for the dictionary.
Examples:
> ```racket
> > ( dict-set #hash ( ) ' a "apple" )
> '#hash((a . "apple"))
> > ( dict-set #hash ( ( a . "apple" ) ( b . "beer" ) ) ' b "banana" )
> '#hash((a . "apple") (b . "banana"))
> > ( dict-set ' ( ) ' a "apple" )
> '((a . "apple"))
> > ( dict-set ' ( ( a . "apple" ) ( b . "beer" ) ) ' b "banana" )
> '((a . "apple") (b . "banana"))
> ```
> ```
(parameter)	→
(-> and/c dict? (not/c immutable?))


# 4.18 Dictionaries
```
Removes any existing mapping for key in dict. The update can fail if dict is not mutable or does not support removing keys (as is the case for vectors, for example).

Examples:

> ```racket
> > ( define h ( make-hash ) )
> > ( dict-set! h ' a "apple" )
> > h
> '#hash((a . "apple"))
> > ( dict-remove! h ' a )
> > h
> '#hash()
> ```

> ```
(dict-remove dict key)   →   (and/c dict? immutable?)
  dict : (and/c dict? immutable?)
  key : any/c```

Functionally removes any existing mapping for key in dict, returning the fresh dictionary. The update can fail if dict does not support functional update or does not support removing keys.

Examples:

> ```racket
> > ( define h #hash ( ) )
> > ( define h ( dict-set h ' a "apple" ) )
> > h
> '#hash((a . "apple"))
> > ( dict-remove h ' a )
> '#hash()
> > h
> '#hash((a . "apple"))
> > ( dict-remove h ' z )
> '#hash((a . "apple"))
> > ( dict-remove ' ( ( a . "apple" ) ( b . "banana" ) ) ' a )
> '((b . "banana"))
> ```

> ```
(dict-iterate-first dict)  →  any/c
  dict : dict?
```
Returns #f if dict contains no elements, otherwise it returns a non-#f value that is an index to the first element in the dict table; “first” refers to an unspecified ordering of the dictionary elements. For a mutable dict, this index is guaranteed to refer to the first item only as long as no mappings are added to or removed from dict.
Examples:


# 4.18 Dictionaries
> ```racket
> > ( dict-iterate-first #hash ( ( a . "apple" ) ( b . "banana" ) ) )
> 0
> > ( dict-iterate-first #hash ( ) )
> #f
> > ( dict-iterate-first # ( "apple" "banana" ) )
> 0
> > ( dict-iterate-first ' ( ( a . "apple" ) ( b . "banana" ) ) )
> #<assoc-iter>
> ```
> ```
(dict-iterate-next dict pos)  →  any/c
  dict : dict?
  pos : any/c
```

Returns either a non-#f that is an index to the element in dict after the element indexed by pos or #f if pos refers to the last element in dict. If pos is not a valid index, then the [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception is raised. For a mutable dict, the result index is guaranteed to refer to its item only as long as no items are added to or removed from dict. The [dict-iterate-next](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-next%29%29) operation should take constant time.

Examples:

> ```racket
> > ( define h #hash ( ( a . "apple" ) ( b . "banana" ) ) )
> > ( define i ( dict-iterate-first h ) )
> > i
> 0
> > ( dict-iterate-next h i )
> 1
> > ( dict-iterate-next h ( dict-iterate-next h i ) )
> #f
> ```

> ```
(dict-iterate-key dict pos)  →  any
  dict : dict?
  pos : any/c
```
Returns the key for the element in dict at index pos. If pos is not a valid index for dict, the exn:fail:contract exception is raised. The dict-iterate-key operation should take constant time.
Examples:


# 4.18 Dictionaries
> ```racket
> > ( define h ' ( ( a . "apple" ) ( b . "banana" ) ) )
> > ( define i ( dict-iterate-first h ) )
> > ( dict-iterate-key h i )
> 'a
> > ( dict-iterate-key h ( dict-iterate-next h i ) )
> 'b
> ```
> ```
(dict-iterate-value dict pos)   →   any
  dict : dict?
  pos : any/c```
Returns the value for the element in dict at index pos. If pos is not a valid index for dict, the [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception is raised. The [dict-iterate-key](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-key%29%29) operation should take constant time.
Examples:
> ```racket
> > ( define h ' ( ( a . "apple" ) ( b . "banana" ) ) )
> > ( define i ( dict-iterate-first h ) )
> > ( dict-iterate-value h i )
> "apple"
> > ( dict-iterate-value h ( dict-iterate-next h i ) )
> "banana"
> ```


##### 4.18.2.2 Derived Dictionary Methods
These methods of [gen:dict](#%28form._%28%28lib._racket%2Fdict..rkt%29._gen~3adict%29%29) have fallback implementations in terms of the other methods; they may be supported even by dictionary types that do not directly implement them.
> ```
(dict-has-key? dict key)  →  boolean?
  dict : dict?
  key : any/c
```
(parameter)	→
(-> and/c dict? (not/c immutable?))```

Maps each key to each v in dict, overwriting any existing mapping for each key. The update can fail with a [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception if dict is not mutable or if any key is not an allowed key for the dictionary (e.g., not an exact integer in the appropriate range when dict is a [vector](vectors.html#%28tech._vector%29)). The update takes place from the left, so later mappings overwrite earlier mappings.

Supported for any dict that implements [dict-set!](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-set%21%29%29).

Examples:

> ```racket
> > ( define h ( make-hash ) )
> > ( dict-set*! h ' a "apple" ' b "banana" )
> > h
> '#hash((a . "apple") (b . "banana"))
> > ( define v1 ( vector #f #f #f ) )
> > ( dict-set*! v1 0 "apple" 1 "banana" )
> > v1
> '#("apple" "banana" #f)
> > ( define v2 ( vector #f #f #f ) )
> > ( dict-set*! v2 0 "apple" 0 "banana" )
> > v2
> '#("banana" #f #f)
> ```

> ```
(dict-set* dict key v ... ...) → (and/c dict? immutable?)
  dict : (and/c dict? immutable?)
  key : any/c
  v : any/c
```


# 4.18 Dictionaries
Functionally extends dict by mapping each key to each v, overwriting any existing mapping for each key, and returning an extended dictionary. The update can fail with a exn:fail:contract exception if dict does not support functional extension or if any key is not an allowed key for the dictionary. The update takes place from the left, so later mappings overwrite earlier mappings.
Supported for any dict that implements dict-set.
Examples:
> ```racket
> > ( dict-set* #hash ( ) ' a "apple" ' b "beer" )
> '#hash((a . "apple") (b . "beer"))
> > ( dict-set* #hash ( ( a . "apple" ) ( b . "beer" ) ) ' b "banana" ' a "anchor" )
> '#hash((a . "anchor") (b . "banana"))
> > ( dict-set* ' ( ) ' a "apple" ' b "beer" )
> '((a . "apple") (b . "beer"))
> > ( dict-set* ' ( ( a . "apple" ) ( b . "beer" ) ) ' b "banana" ' a "anchor" )
> '((a . "anchor") (b . "banana"))
> > ( dict-set* ' ( ( a . "apple" ) ( b . "beer" ) ) ' b "banana" ' b "ballistic" )
> '((a . "apple") (b . "ballistic"))
> ```
> ```
(dict-ref! dict key to-set)   →   any
  dict : dict?
  key : any/c
  to-set : any/c```
Returns the value for key in dict. If no value is found for key, then to-set determines the result as in [dict-ref](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-ref%29%29) (i.e., it is either a thunk that computes a value or a plain value), and this result is stored in dict for the key. (Note that if to-set is a thunk, it is not invoked in tail position.)


# 4.18 Dictionaries
Supported for any dict that implements [dict-ref](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-ref%29%29) and [dict-set!](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-set%21%29%29).
Examples:
> ```racket
> > ( dict-ref! ( make-hasheq ' ( ( a . "apple" ) ( b . "beer" ) ) ) ' a #f )
> "apple"
> > ( dict-ref! ( make-hasheq ' ( ( a . "apple" ) ( b . "beer" ) ) ) ' c ' cabbage )
> 'cabbage
> > ( define h ( make-hasheq ' ( ( a . "apple" ) ( b . "beer" ) ) ) )
> > ( dict-ref h ' c )
> hash-ref: no value found for key
> key: 'c
> > ( dict-ref! h ' c ( λ ( ) ' cabbage ) )
> 'cabbage
> > ( dict-ref h ' c )
> 'cabbage
> ```
> ```
(parameter)	→
(-> and/c dict? (not/c immutable?))


# 4.18 Dictionaries
```
Composes dict-ref and dict-set! to update an existing mapping in dict, where the optional failure-result argument is used as in dict-ref when no mapping exists for key already.

Supported for any dict that implements dict-ref and dict-set!.

Examples:

> ```racket
> > ( define h ( make-hash ) )
> > ( dict-update! h ' a add1 )
> hash-update!: no value found for key: 'a
> > ( dict-update! h ' a add1 0 )
> > h
> '#hash((a . 1))
> > ( define v ( vector #f #f #f ) )
> > ( dict-update! v 0 not )
> > v
> '#(#t #f #f)
> ```

> ```
(dict-update dict key updater [failure-result)
   →   (and/c dict? immutable?)
  dict : dict?
  key : any/c
  updater : (any/c .  →  . any/c)
   failure-result   :   failure-result/c
      =   (lambda () (raise (make-exn:fail ....)))```

Composes [dict-ref](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-ref%29%29) and [dict-set](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-set%29%29) to functionally update an existing mapping in dict, where the optional failure-result argument is used as in [dict-ref](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-ref%29%29) when no mapping exists for key already.

Supported for any dict that implements [dict-ref](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-ref%29%29) and [dict-set](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-set%29%29).

Examples:

> ```racket
> > ( dict-update #hash ( ) ' a add1 )
> hash-update: no value found for key: 'a
> > ( dict-update #hash ( ) ' a add1 0 )
> '#hash((a . 1))
> > ( dict-update #hash ( ( a . "apple" ) ( b . "beer" ) ) ' b string-length )
> '#hash((a . "apple") (b . 4))
> ```

> ```
(dict-map dict proc)  →  (listof any/c)
  dict : dict?
  proc : (any/c any/c .  →  . any/c)
```


# 4.18 Dictionaries
Applies the procedure proc to each element in dict in an unspecified order, accumulating the results into a list. The procedure proc is called each time with a key and its value.
Supported for any dict that implements dict-iterate-first, dict-iterate-next, dict-iterate-key, and dict-iterate-value.
Example:
> ```racket
> > ( dict-map #hash ( ( a . "apple" ) ( b . "banana" ) ) vector )
> '(#(b "banana") #(a "apple"))
> ```
> ```
(dict-map/copy dict proc)   →   dict?
  dict : dict?
  proc : (any/c any/c .  →  . (values any/c any/c))```
Applies the procedure proc to each element in dict in an unspecified order, accumulating the results into a dict of the same kind. The procedure proc is called each time with a key and its value, and must return a corresponding key and value.
Supported for any dict that implements [dict-iterate-first](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-first%29%29), [dict-iterate-next](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-next%29%29), [dict-iterate-key](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-key%29%29), and [dict-iterate-value](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-value%29%29), and either [dict-set](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-set%29%29) and [dict-clear](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-clear%29%29), or [dict-set!](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-set%21%29%29), [dict-copy](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-copy%29%29), and [dict-clear!](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-clear%21%29%29).


# 4.18 Dictionaries
Example:
> ```racket
> > ( dict-map/copy #hash ( ( a . "apple" ) ( b . "banana" ) ) ( lambda ( k v ) ( values k ( string-upcase v ) ) ) )
> '#hash((a . "APPLE") (b . "BANANA"))
> ```
Added in version 8.5.0.2 of package base.
> ```
(parameter)	→
(-> any/c any/c . → . any)
```
Applies proc to each element in dict (for the side-effects of proc) in an unspecified order. The procedure proc is called each time with a key and its value.

Supported for any dict that implements dict-iterate-first, dict-iterate-next, dict-iterate-key, and dict-iterate-value.

Example:

> ```racket
> > ( dict-for-each #hash ( ( a . "apple" ) ( b . "banana" ) ) ( lambda ( k v ) ( printf "~a = ~s\n" k v ) ) )
> b = "banana" a = "apple"
> ```

> ```
(dict-empty? dict) → boolean?
  dict : dict?```

Reports whether dict is empty.

Supported for any dict that implements [dict-iterate-first](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-first%29%29).

Examples:

> ```racket
> > ( dict-empty? #hash ( ( a . "apple" ) ( b . "banana" ) ) )
> #f
> > ( dict-empty? ( vector ) )
> #t
> ```

> ```
(dict-count dict)  →  exact-nonnegative-integer?
  dict : dict?
```
Returns the number of keys mapped by dict, usually in constant time.
Supported for any dict that implements dict-iterate-first and dict-iterate-next.
Examples:
> ```racket
> > ( dict-count #hash ( ( a . "apple" ) ( b . "banana" ) ) )
> 2
> > ( dict-count # ( "apple" "banana" ) )
> 2
> ```
> ```
(dict-copy dict)   →   dict?
  dict : dict?```


# 4.18 Dictionaries
Produces a new, mutable dictionary of the same type as dict and with the same key/value associations.
Supported for any dict that implements [dict-clear](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-clear%29%29), [dict-set!](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-set%21%29%29), [dict-iterate-first](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-first%29%29), [dict-iterate-next](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-next%29%29), [dict-iterate-key](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-key%29%29), and [dict-iterate-value](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-value%29%29).
Examples:
> ```racket
> > ( define original ( vector "apple" "banana" ) )
> > ( define copy ( dict-copy original ) )
> > original
> '#("apple" "banana")
> > copy
> '#("apple" "banana")
> > ( dict-set! copy 1 "carrot" )
> > original
> '#("apple" "banana")
> > copy
> '#("apple" "carrot")
> ```
> ```
(dict-clear dict)  →  dict?
  dict : dict?


# 4.18 Dictionaries
```
(parameter)	→```

Removes all of the key/value associations in dict.

Supported for any dict that supports [dict-remove!](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-remove%21%29%29), [dict-iterate-first](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-first%29%29), and [dict-iterate-key](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-key%29%29).

Examples:

> ```racket
> > ( define table ( make-hash ) )
> > ( dict-set! table ' a "apple" )
> > ( dict-set! table "banana" ' b )
> > table
> '#hash((a . "apple") ("banana" . b))
> > ( dict-clear! table )
> > table
> '#hash()
> ```

> ```
(dict-keys dict)  →  list?
  dict : dict?
```
Returns a list of the keys from dict in an unspecified order.
Supported for any dict that implements dict-iterate-first, dict-iterate-next, and dict-iterate-key.
Examples:
> ```racket
> > ( define h #hash ( ( a . "apple" ) ( b . "banana" ) ) )
> > ( dict-keys h )
> '(b a)
> ```
> ```
(dict-values dict)   →   list?
  dict : dict?```
Returns a list of the values from dict in an unspecified order.
Supported for any dict that implements [dict-iterate-first](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-first%29%29), [dict-iterate-next](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-next%29%29), and [dict-iterate-value](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-value%29%29).
Examples:
> ```racket
> > ( define h #hash ( ( a . "apple" ) ( b . "banana" ) ) )
> > ( dict-values h )
> '("banana" "apple")
> ```
> ```
(dict  →  list dict)  →  list?
  dict : dict?


# 4.18 Dictionaries
```
Returns a list of the associations from dict in an unspecified order.

Supported for any dict that implements dict-iterate-first, dict-iterate-next, dict-iterate-key, and dict-iterate-value.

Examples:

> ```racket
> > ( define h #hash ( ( a . "apple" ) ( b . "banana" ) ) )
> > ( dict  →  list h )
> '((b . "banana") (a . "apple"))
> ```

#### 4.18.3 Dictionary Sequences

> ```
(in-dict dict)   →   sequence?
  dict : dict?```

Returns a [sequence](sequences.html#%28tech._sequence%29) whose each element is two values: a key and corresponding value from dict.

Supported for any dict that implements [dict-iterate-first](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-first%29%29), [dict-iterate-next](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-next%29%29), [dict-iterate-key](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-key%29%29), and [dict-iterate-value](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-value%29%29).

Examples:

> ```racket
> > ( define h #hash ( ( a . "apple" ) ( b . "banana" ) ) )
> > ( for/list ( [ ( k v ) ( in-dict h ) ] ) ( format "~a = ~s" k v ) )
> '("b = \"banana\"" "a = \"apple\"")
> ```

> ```
(in-dict-keys dict)  →  sequence?
  dict : dict?
```
Returns a sequence whose elements are the keys of dict.
Supported for any dict that implements dict-iterate-first, dict-iterate-next, and dict-iterate-key.
Examples:
> ```racket
> > ( define h #hash ( ( a . "apple" ) ( b . "banana" ) ) )
> > ( for/list ( [ k ( in-dict-keys h ) ] ) k )
> '(b a)
> ```
> ```


# 4.18 Dictionaries
(in-dict-values dict) → sequence?
  dict : dict?```
Returns a sequence whose elements are the values of dict.
Supported for any dict that implements [dict-iterate-first](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-first%29%29), [dict-iterate-next](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-next%29%29), and [dict-iterate-value](#%28def._%28%28lib._racket%2Fdict..rkt%29._dict-iterate-value%29%29).
Examples:
> ```racket
> > ( define h #hash ( ( a . "apple" ) ( b . "banana" ) ) )
> > ( for/list ( [ v ( in-dict-values h ) ] ) v )
> '("banana" "apple")
> ```
> ```
(in-dict-pairs dict)  →  sequence?
  dict : dict?


# 4.18 Dictionaries
```
Returns a sequence whose elements are pairs, each containing a key and its value from dict (as opposed to using in-dict, which gets the key and value as separate values for each element).

Supported for any dict that implements dict-iterate-first, dict-iterate-next, dict-iterate-key, and dict-iterate-value.

Examples:

> ```racket
> > ( define h #hash ( ( a . "apple" ) ( b . "banana" ) ) )
> > ( for/list ( [ p ( in-dict-pairs h ) ] ) p )
> '((b . "banana") (a . "apple"))
> ```

#### 4.18.4 Contracted Dictionaries

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>value</p>

<p>prop:dict/contract : struct-type-property?</p></blockquote></td></tr></tbody></table>

A structure type property for defining dictionaries with contracts. The value associated with prop:dict/contract must be a list of two immutable vectors:

> ```racket
> ( list dict-vector
> ( vector type-key-contract
> type-value-contract
> type-iter-contract
> instance-key-contract
> instance-value-contract
> instance-iter-contract ) )
> ```

The first vector must be a vector of 10 procedures which match the gen:dict generic interface (in addition, it must be an immutable vector). The second vector must contain six elements; each of the first three is a contract for the dictionary type’s keys, values, and positions, respectively. Each of the second three is either #f or a procedure used to extract the contract from a dictionary instance.

> ```
(dict-key-contract d) → contract?
  d : dict?

(dict-value-contract d)   →   contract?
  d : dict?

(dict-iter-contract d)   →   contract?
  d : dict?```

Returns the contract that d imposes on its keys, values, or iterators, respectively, if d implements the [prop:dict/contract](#%28def._%28%28lib._racket%2Fdict..rkt%29._prop~3adict%2Fcontract%29%29) interface.

#### 4.18.5 Custom Hash Tables

> ```
syntax

(define-custom-hash-types name
> optional-predicate
> comparison-expr
> optional-hash-functions )

optional-predicate   =
    |   #:key? predicate-expr

optional-hash-functions   =
    |   hash1-expr
    |   hash1-expr hash2-expr
```


# 4.18 Dictionaries
(parameter)	→
(-> any/c . → . boolean?)```
Creates a new dictionary type based on the given comparison function eql?, hash functions hash1 and hash2, and predicate key?. The new dictionary type has variants that are immutable, mutable with strongly-held keys, and mutable with weakly-held keys. The given name is used when printing instances of the new dictionary type, and the symbol who is used for reporting errors.
The comparison function eql? may accept 2 or 3 arguments. If it accepts 2 arguments, it given two keys to compare them. If it accepts 3 arguments and does not accept 2 arguments, it is also given a recursive comparison function that handles data cycles when comparing sub-parts of the keys.
The hash functions hash1 and hash2 may accept 1 or 2 arguments. If either hash function accepts 1 argument, it is applied to a key to compute the corresponding hash value. If either hash function accepts 2 arguments and does not accept 1 argument, it is also given a recursive hash function that handles data cycles when computing hash values of sub-parts of the keys.
The predicate key? must accept 1 argument and is used to recognize valid keys for the new dictionary type.
Produces seven values:
-   a predicate recognizing all instances of the new dictionary type,
-   a predicate recognizing immutable instances,
-   a predicate recognizing mutable instances,
-   a predicate recognizing weak instances,
-   a constructor for immutable instances,
-   a constructor for mutable instances, and
-   a constructor for weak instances.


# 4.18 Dictionaries
See [define-custom-hash-types](#%28form._%28%28lib._racket%2Fdict..rkt%29._define-custom-hash-types%29%29) for an example.
> ```
(make-custom-hash eql?
   [ hash1
    hash2
    #:key? key?)    →    dict?
   eql?   :   ( or/c ( any/c any/c .  →  . any/c )
> ( any/c any/c ( any/c any/c .  →  . any/c ) .  →  . any/c ) )
   hash1   :   ( or/c ( any/c .  →  . exact-integer? )
> ( any/c ( any/c .  →  . exact-integer? ) .  →  . exact-integer? ) )
      =   (const 1)
   hash2   :   ( or/c ( any/c .  →  . exact-integer? )
> ( any/c ( any/c .  →  . exact-integer? ) .  →  . exact-integer? ) )
      =   (const 1)
  key? : (any/c .  →  . boolean?) = (λ (x) #true)
(make-weak-custom-hash   eql?
   [ hash1
    hash2
    #:key? key?)    →    dict?
   eql?   :   ( or/c ( any/c any/c .  →  . any/c )
> ( any/c any/c ( any/c any/c .  →  . any/c ) .  →  . any/c ) )
   hash1   :   ( or/c ( any/c .  →  . exact-integer? )
> ( any/c ( any/c .  →  . exact-integer? ) .  →  . exact-integer? ) )
      =   (const 1)
   hash2   :   ( or/c ( any/c .  →  . exact-integer? )
> ( any/c ( any/c .  →  . exact-integer? ) .  →  . exact-integer? ) )
      =   (const 1)
  key? : (any/c .  →  . boolean?) = (λ (x) #true)
(make-immutable-custom-hash   eql?
   [ hash1
    hash2
    #:key? key?)    →    dict?
   eql?   :   ( or/c ( any/c any/c .  →  . any/c )
> ( any/c any/c ( any/c any/c .  →  . any/c ) .  →  . any/c ) )
   hash1   :   ( or/c ( any/c .  →  . exact-integer? )


# 4.18 Dictionaries
> ( any/c ( any/c .  →  . exact-integer? ) .  →  . exact-integer? ) )
      =   (const 1)
   hash2   :   ( or/c ( any/c .  →  . exact-integer? )
> ( any/c ( any/c .  →  . exact-integer? ) .  →  . exact-integer? ) )
      =   (const 1)
  key? : (any/c .  →  . boolean?) = (λ (x) #true)
```
Creates an instance of a new dictionary type, implemented in terms of a hash table where keys are compared with eql?, hashed with hash1 and hash2, and where the key predicate is key?. See gen:equal-mode+hash and gen:equal+hash for information on suitable equality and hashing functions.

The make-custom-hash and make-weak-custom-hash functions create a mutable dictionary that does not support functional update, while make-immutable-custom-hash creates an immutable dictionary that supports functional update. The dictionary created by make-weak-custom-hash retains its keys weakly, like the result of make-weak-hash.

Dictionaries created by make-custom-hash and company are equal? when they have the same mutability and key strength, the associated procedures are equal?, and the key–value mappings are the same when keys and values are compared with equal?.

See also define-custom-hash-types.

Examples:

> ```racket
> > ( define h ( make-custom-hash ( lambda ( a b ) ( string=? ( format "~a" a ) ( format "~a" b ) ) ) ( lambda ( a ) ( equal-hash-code ( format "~a" a ) ) ) ) )
> > ( dict-set! h 1 ' one )
> > ( dict-ref h "1" )
> 'one
> ```

#### 4.18.6 Passing Keyword Arguments in Dictionaries

> ```
(keyword-apply/dict proc
    kw-dict
    pos-arg ...
    pos-args
    #:<kw> kw-arg ...)    →    any

  proc : procedure?
  kw-dict : dict?
  pos-arg : any/c
  pos-args : (listof any/c)
  kw-arg : any/c```

Applies the proc using the positional arguments from ([list*](pairs.html#%28def._%28%28quote._~23~25kernel%29._list%2A%29%29) pos-arg [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29) pos-args), and the keyword arguments from kw-dict in addition to the directly supplied keyword arguments in the #:\<kw> kw-arg sequence.

All the keys in kw-dict must be keywords. The keywords in the kw-dict do not have to be sorted. However, the keywords in kw-dict and the directly supplied #:\<kw> keywords must not overlap. The given proc must accept all of the keywords in kw-dict plus the #:\<kw>s.

Examples:

> ```racket
> > ( define ( sundae #:ice-cream [ ice-cream ' ( "vanilla" ) ] #:toppings [ toppings ' ( "brownie-bits" ) ] #:sprinkles [ sprinkles "chocolate" ] #:syrup [ syrup "caramel" ] ) ( format "A sundae with ~a ice cream, ~a, ~a sprinkles, and ~a syrup." ( string-join ice-cream #:before-last " and " ) ( string-join toppings #:before-last " and " ) sprinkles syrup ) )
> > ( keyword-apply/dict sundae ' ( ( #:ice-cream "chocolate" ) ) ' ( ) )
> "A sundae with chocolate ice cream, brownie-bits, chocolate sprinkles, and caramel syrup."
> > ( keyword-apply/dict sundae ( hash ' #:toppings ' ( "cookie-dough" ) ' #:sprinkles "rainbow" ' #:syrup "chocolate" ) ' ( ) )
> "A sundae with vanilla ice cream, cookie-dough, rainbow sprinkles, and chocolate syrup."
> > ( keyword-apply/dict sundae #:sprinkles "rainbow" ( hash ' #:toppings ' ( "cookie-dough" ) ' #:syrup "chocolate" ) ' ( ) )
> "A sundae with vanilla ice cream, cookie-dough, rainbow sprinkles, and chocolate syrup."
> ```

Added in version 7.9 of package base.

------------------------------------------------------------------------

# 4.19 Sets

### 4.19 Sets

A set represents a collection of distinct elements. The following datatypes are all sets:

-   [hash sets](#%28tech._hash._set%29);

-   [lists](pairs.html#%28tech._list%29) using [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) to compare elements; and

-   [structures](structures.html#%28tech._structure%29) whose types implement the [gen:set](#%28form._%28%28lib._racket%2Fset..rkt%29._gen~3aset%29%29) [generic interface](struct-generics.html#%28tech._generic._interface%29).

|                                                                                |                                                                                |
|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  ([require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/set]()) |  package: [base](https://pkgs.racket-lang.org/package/base "Install this package using `raco pkg install base`") |

The bindings documented in this section are provided by the [racket/set]() and [racket](index.html) libraries, but not [racket/base](index.html).

#### 4.19.1 Hash Sets

A hash set is a set whose elements are compared via [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29), [equal-always?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal-always~3f%29%29), [eqv?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29), or [eq?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29) and partitioned via [equal-hash-code](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal-hash-code%29%29), [equal-always-hash-code](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal-always-hash-code%29%29), [eqv-hash-code](Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv-hash-code%29%29), or [eq-hash-code](Equality.html#%28def._%28%28quote._~23~25kernel%29._eq-hash-code%29%29). A hash set is either immutable or mutable; mutable hash sets retain their elements either strongly or weakly.

> > > Like operations on immutable hash tables, “constant time” hash set operations actually require O(log N) time for a set of size N.

A hash set can be used as a [stream](streams.html#%28tech._stream%29) (see [Streams](streams.html)) and thus as a single-valued [sequence](sequences.html#%28tech._sequence%29) (see [Sequences](sequences.html)). The elements of the set serve as elements of the stream or sequence. If an element is added to or removed from the hash set during iteration, then an iteration step may fail with [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29), or the iteration may skip or duplicate elements. See also [in-set](#%28def._%28%28lib._racket%2Fset..rkt%29._in-set%29%29).

Two hash sets are [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) when they use the same element-comparison procedure ([equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29), [equal-always?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal-always~3f%29%29), [eqv?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29), or [eq?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29)), both hold elements strongly or weakly, have the same mutability, and have equivalent elements. Immutable hash sets support effectively constant-time access and update, just like mutable hash sets; the constant on immutable operations is usually larger, but the functional nature of immutable hash sets can pay off in certain algorithms.

All hash sets [implement](struct-generics.html#%28tech._implemented._generic._method%29) [set->stream](#%28def._%28%28lib._racket%2Fset..rkt%29._set-~3estream%29%29), [set-empty?](#%28def._%28%28lib._racket%2Fset..rkt%29._set-empty~3f%29%29), [set-member?](#%28def._%28%28lib._racket%2Fset..rkt%29._set-member~3f%29%29), [set-count](#%28def._%28%28lib._racket%2Fset..rkt%29._set-count%29%29), [subset?](#%28def._%28%28lib._racket%2Fset..rkt%29._subset~3f%29%29), [proper-subset?](#%28def._%28%28lib._racket%2Fset..rkt%29._proper-subset~3f%29%29), [set-map](#%28def._%28%28lib._racket%2Fset..rkt%29._set-map%29%29), [set-for-each](#%28def._%28%28lib._racket%2Fset..rkt%29._set-for-each%29%29), [set-copy](#%28def._%28%28lib._racket%2Fset..rkt%29._set-copy%29%29), [set-copy-clear](#%28def._%28%28lib._racket%2Fset..rkt%29._set-copy-clear%29%29), [set->list](#%28def._%28%28lib._racket%2Fset..rkt%29._set-~3elist%29%29), and [set-first](#%28def._%28%28lib._racket%2Fset..rkt%29._set-first%29%29). Immutable hash sets in addition [implement](struct-generics.html#%28tech._implemented._generic._method%29) [set-add](#%28def._%28%28lib._racket%2Fset..rkt%29._set-add%29%29), [set-remove](#%28def._%28%28lib._racket%2Fset..rkt%29._set-remove%29%29), [set-clear](#%28def._%28%28lib._racket%2Fset..rkt%29._set-clear%29%29), [set-union](#%28def._%28%28lib._racket%2Fset..rkt%29._set-union%29%29), [set-intersect](#%28def._%28%28lib._racket%2Fset..rkt%29._set-intersect%29%29), [set-subtract](#%28def._%28%28lib._racket%2Fset..rkt%29._set-subtract%29%29), and [set-symmetric-difference](#%28def._%28%28lib._racket%2Fset..rkt%29._set-symmetric-difference%29%29). Mutable hash sets in addition [implement](struct-generics.html#%28tech._implemented._generic._method%29) [set-add!](#%28def._%28%28lib._racket%2Fset..rkt%29._set-add%21%29%29), [set-remove!](#%28def._%28%28lib._racket%2Fset..rkt%29._set-remove%21%29%29), [set-clear!](#%28def._%28%28lib._racket%2Fset..rkt%29._set-clear%21%29%29), [set-union!](#%28def._%28%28lib._racket%2Fset..rkt%29._set-union%21%29%29), [set-intersect!](#%28def._%28%28lib._racket%2Fset..rkt%29._set-intersect%21%29%29), [set-subtract!](#%28def._%28%28lib._racket%2Fset..rkt%29._set-subtract%21%29%29), and [set-symmetric-difference!](#%28def._%28%28lib._racket%2Fset..rkt%29._set-symmetric-difference%21%29%29).

Operations on sets that contain elements that are mutated are unpredictable in much the same way that [hash table](hashtables.html#%28tech._hash._table%29) operations are unpredictable when keys are mutated.

> ```
(set-equal? x)  →  boolean?
  x : any/c

(set-equal-always? x)  →  boolean?
  x : any/c

(set-eqv? x)  →  boolean?
  x : any/c

(set-eq? x)  →  boolean?
  x : any/c
```


# 4.18 Dictionaries
Returns #t if x is a hash set that compares elements with equal?, equal-always?, eqv?, or eq?, respectively; returns #f otherwise.
Changed in version 8.5.0.3 of package base: Added set-equal-always?.
> ```
(set? x)   →   boolean?
  x : any/c
(set-mutable? x)   →   boolean?
  x : any/c
(set-weak? x)   →   boolean?
  x : any/c```
Returns #t if x is a [hash set](#%28tech._hash._set%29) that is respectively immutable, mutable with strongly-held keys, or mutable with weakly-held keys; returns #f otherwise.
> ```
(set v ...) → (and/c generic-set? set-equal? set?)
  v : any/c
(setalw v ...)  →  (and/c generic-set? set-equal-always? set?)
  v : any/c
(seteqv v ...)  →  (and/c generic-set? set-eqv? set?)
  v : any/c
(seteq v ...)  →  (and/c generic-set? set-eq? set?)
  v : any/c
(mutable-set v ...)
  →  (and/c generic-set? set-equal? set-mutable?)
  v : any/c
(mutable-setalw v ...)
  →  (and/c generic-set? set-equal-always? set-mutable?)
  v : any/c
(mutable-seteqv v ...)
  →  (and/c generic-set? set-eqv? set-mutable?)
  v : any/c
(mutable-seteq v ...)
  →  (and/c generic-set? set-eq? set-mutable?)
  v : any/c
(weak-set v ...)  →  (and/c generic-set? set-equal? set-weak?)
  v : any/c
(weak-setalw v ...)
  →  (and/c generic-set? set-equal-always? set-weak?)
  v : any/c
(weak-seteqv v ...)  →  (and/c generic-set? set-eqv? set-weak?)
  v : any/c
(weak-seteq v ...)  →  (and/c generic-set? set-eq? set-weak?)
  v : any/c


# 4.18 Dictionaries
```
Creates a hash set with the given vs as elements. The elements are added in the order that they appear as arguments, so in the case of sets that use equal?, equal-always?, or eqv?, an earlier element may be replaced by a later element that is equal?, equal-always?, or eqv?, but not eq?.

Changed in version 8.5.0.3 of package base: Added setalw, mutable-setalw, and weak-setalw.

> ```
(list  →  set lst)   →   (and/c generic-set? set-equal? set?)
  lst : list?

(list  →  setalw lst)
   →   (and/c generic-set? set-equal-always? set?)
  lst : list?

(list  →  seteqv lst)   →   (and/c generic-set? set-eqv? set?)
  lst : list?

(list  →  seteq lst)   →   (and/c generic-set? set-eq? set?)
  lst : list?

(list  →  mutable-set lst)
   →   (and/c generic-set? set-equal? set-mutable?)
  lst : list?

(list  →  mutable-setalw lst)
   →   (and/c generic-set? set-equal-always? set-mutable?)
  lst : list?

(list  →  mutable-seteqv lst)
   →   (and/c generic-set? set-eqv? set-mutable?)
  lst : list?

(list  →  mutable-seteq lst)
   →   (and/c generic-set? set-eq? set-mutable?)
  lst : list?

(list  →  weak-set lst)
   →   (and/c generic-set? set-equal? set-weak?)
  lst : list?

(list  →  weak-setalw lst)
   →   (and/c generic-set? set-equal-always? set-weak?)
  lst : list?

(list  →  weak-seteqv lst)
   →   (and/c generic-set? set-eqv? set-weak?)
  lst : list?

(list  →  weak-seteq lst)   →   (and/c generic-set? set-eq? set-weak?)
  lst : list?```

Creates a [hash set](#%28tech._hash._set%29) with the elements of the given lst as the elements of the set. Equivalent to ([apply](procedures.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._apply%29%29) [set](#%28def._%28%28lib._racket%2Fset..rkt%29._set%29%29) lst), ([apply](procedures.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._apply%29%29) [setalw](#%28def._%28%28lib._racket%2Fset..rkt%29._setalw%29%29) lst), ([apply](procedures.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._apply%29%29) [seteqv](#%28def._%28%28lib._racket%2Fset..rkt%29._seteqv%29%29) lst), ([apply](procedures.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._apply%29%29) [seteq](#%28def._%28%28lib._racket%2Fset..rkt%29._seteq%29%29) lst), and so on, respectively.

Changed in version 8.5.0.3 of package base: Added [list->setalw](#%28def._%28%28lib._racket%2Fset..rkt%29._list-~3esetalw%29%29), [list->mutable-setalw](#%28def._%28%28lib._racket%2Fset..rkt%29._list-~3emutable-setalw%29%29), and [list->weak-setalw](#%28def._%28%28lib._racket%2Fset..rkt%29._list-~3eweak-setalw%29%29).

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for/set](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fset%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for/seteq](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fseteq%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for/seteqv](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fseteqv%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for/setalw](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fsetalw%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for*/set](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2A%2Fset%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for*/seteq](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2A%2Fseteq%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for*/seteqv](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2A%2Fseteqv%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for*/setalw](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2A%2Fsetalw%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for/mutable-set](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fmutable-set%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for/mutable-seteq](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fmutable-seteq%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for/mutable-seteqv](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fmutable-seteqv%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for/mutable-setalw](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fmutable-setalw%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for*/mutable-set](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2A%2Fmutable-set%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for*/mutable-seteq](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2A%2Fmutable-seteq%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for*/mutable-seteqv](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2A%2Fmutable-seteqv%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for*/mutable-setalw](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2A%2Fmutable-setalw%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for/weak-set](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fweak-set%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for/weak-seteq](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fweak-seteq%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for/weak-seteqv](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fweak-seteqv%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for/weak-setalw](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fweak-setalw%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for*/weak-set](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2A%2Fweak-set%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for*/weak-seteq](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2A%2Fweak-seteq%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for*/weak-seteqv](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2A%2Fweak-seteqv%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>

<p>syntax</p>

<p>([for*/weak-setalw](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2A%2Fweak-setalw%29%29) (for-clause ...) body ...+)</p></blockquote></td></tr></tbody></table></td></tr></tbody></table>

Analogous to [for/list](for.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Flist%29%29) and [for*/list](for.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2A%2Flist%29%29), but to construct a [hash set](#%28tech._hash._set%29) instead of a list.

Changed in version 8.5.0.3 of package base: Added [for/setalw](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fsetalw%29%29), [for/mutable-setalw](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fmutable-setalw%29%29), and [for/weak-setalw](#%28form._%28%28lib._racket%2Fset..rkt%29._for%2Fweak-setalw%29%29).

> ```
(in-immutable-set st)  →  sequence?
  st : set?

(in-mutable-set st)  →  sequence?
  st : set-mutable?

(in-weak-set st)  →  sequence?
  st : set-weak?
```


# 4.18 Dictionaries
Explicitly converts a specific kind of hash set to a sequence for use with for forms.
As with in-list and some other sequence constructors, in-immutable-set performs better when it appears directly in a for clause.
These sequence constructors are compatible with Custom Hash Sets.
Added in version 6.4.0.7 of package base.


#### 4.19.2 Set Predicates and Contracts
> ```
(generic-set? v)   →   boolean?
  v : any/c```
Returns #t if v is a [set](#%28tech._set%29); returns #f otherwise.
Examples:
> ```racket
> > ( generic-set? ( list 1 2 3 ) )
> #t
> > ( generic-set? ( set 1 2 3 ) )
> #t
> > ( generic-set? ( mutable-seteq 1 2 3 ) )
> #t
> > ( generic-set? ( vector 1 2 3 ) )
> #f
> ```
> ```
(parameter)	→
```
(parameter)	→```

Recognizes sets that support all of the methods from [gen:set](#%28form._%28%28lib._racket%2Fset..rkt%29._gen~3aset%29%29) named by the syms.

> ```
(set/c elem/c
   [ #:cmp cmp
    #:kind kind
    #:lazy? lazy?
    #:equal-key/c equal-key/c)    →    contract?

  elem/c : chaperone-contract?
   cmp   :   (or/c 'dont-care 'equal 'equal-always 'eqv 'eq)
      =   'dont-care

   kind   :   (or/c 'dont-care 'immutable 'mutable 'weak 'mutable-or-weak)
      =   'immutable

   lazy?   :   any/c   =   ( not ( and ( equal? kind ' immutable )
> ( flat-contract? elem/c ) ) )

  equal-key/c : contract? = any/c
```
Constructs a contract that recognizes sets whose elements match elem/c.
If kind is 'immutable, 'mutable, or 'weak, the resulting contract accepts only hash sets that are respectively immutable, mutable with strongly-held keys, or mutable with weakly-held keys. If kind is 'mutable-or-weak, the resulting contract accepts any mutable hash sets, regardless of key-holding strength.
If cmp is 'equal, 'equal-always, 'eqv, or 'eq, the resulting contract accepts only hash sets that compare elements using equal?, equal-always?, eqv?, or eq?, respectively.


# 4.18 Dictionaries
If cmp is 'eqv or 'eq, then elem/c must be a flat contract.
If cmp and kind are both 'dont-care, then the resulting contract will accept any kind of set, not just hash sets.
If lazy? is not #f, then the elements of the set are not checked immediately by the contract and only the set itself is checked (according to the cmp and kind arguments). If lazy? is #f, then the elements are checked immediately by the contract. The lazy? argument is ignored when the set contract accepts generic sets (i.e., when cmp and kind are both 'dont-care); in that case, the value being checked in that case is a list?, then the contract is not lazy otherwise the contract is lazy.
If kind allows mutable sets (i.e., is 'dont-care, 'mutable, 'weak, or 'mutable-or-weak) and lazy? is #f, then the elements are checked both immediately and when they are accessed from the set.
The equal-key/c contract is used when values are passed to the comparison and hashing functions used internally.
The result contract will be a flat contract when elem/c and equal-key/c are both flat contracts, lazy? is #f, and kind is 'immutable. The result will be a chaperone contract when elem/c is a chaperone contract.
Changed in version 8.3.0.9 of package base: Added support for random generation.
Changed in version 8.5.0.3: Added 'equal-always support for cmp.