#### 4.19.3 Generic Set Interface
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

gen:set

A generic interface (see Generic Interfaces) that supplies set 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 Set Methods.
Examples:
> ```racket
> > ( struct binary-set [ integer ] #:transparent #:methods gen:set [ ( define ( set-member? st i ) ( bitwise-bit-set? ( binary-set-integer st ) i ) ) ( define ( set-add st i ) ( binary-set ( bitwise-ior ( binary-set-integer st ) ( arithmetic-shift 1 i ) ) ) ) ( define ( set-remove st i ) ( binary-set ( bitwise-and ( binary-set-integer st ) ( bitwise-not ( arithmetic-shift 1 i ) ) ) ) ) ] )
> > ( define bset ( binary-set 5 ) )
> > bset
> (binary-set 5)
> > ( generic-set? bset )
> #t
> > ( set-member? bset 0 )
> #t
> > ( set-member? bset 1 )
> #f
> > ( set-member? bset 2 )
> #t
> > ( set-add bset 4 )
> (binary-set 21)
> > ( set-remove bset 2 )
> (binary-set 1)
> ```


##### 4.19.3.1 Set Methods
The methods of gen:set can be classified into three categories, as determined by their fallback implementations:
1.  methods with no fallbacks,
2.  methods whose fallbacks depend on other, non-fallback methods,
3.  and methods whose fallbacks can depend on either fallback or non-fallback methods.
As an example, implementing the following methods would guarantee that all the methods in gen:set would at least have a fallback method:
-  set-member?
-  set-add
-  set-add!
-  set-remove
-  set-remove!
-  set-first
-  set-empty?
-  set-copy-clear
There may be other such subsets of methods that would guarantee at least a fallback for every method.
> ```
(set-member? st v) → boolean?
  st : generic-set?
  v : any/c```
Returns #t if v is in st, #f otherwise. Has no fallback.
> ```
(set-add st v)  →  generic-set?
  st : generic-set?
  v : any/c
```
(parameter)	→```

Adds the element v to st. This operation runs in constant time for [hash sets](#%28tech._hash._set%29). Has no fallback.

For [hash sets](#%28tech._hash._set%29), see also the [caveats concerning concurrent modification](hashtables.html#%28elem._%28caveat._concurrency%29%29) for hash tables, which applies to hash sets.

> ```
(set-remove st v)  →  generic-set?
  st : generic-set?
  v : any/c
```
(parameter)	→```
Removes the element v from st. This operation runs in constant time for [hash sets](#%28tech._hash._set%29). Has no fallback.


# 4.18 Dictionaries
For [hash sets](#%28tech._hash._set%29), see also the [caveats concerning concurrent modification](hashtables.html#%28elem._%28caveat._concurrency%29%29) for hash tables, which applies to hash sets.
> ```
(set-empty? st)  →  boolean?
  st : generic-set?
```
Returns #t if st has no members; returns #f otherwise.

Supported for any st that implements set  →  stream or set-count.

> ```
(set-count st)   →   exact-nonnegative-integer?
  st : generic-set?```

Returns the number of elements in st.

Supported for any st that [supports](struct-generics.html#%28tech._supported._generic._method%29) [set->stream](#%28def._%28%28lib._racket%2Fset..rkt%29._set-~3estream%29%29).

> ```
(set-first st)  →  any/c
  st : (and/c generic-set? (not/c set-empty?))
```
Produces an unspecified element of st. Multiple uses of set-first on st produce the same result.
Supported for any st that implements set  →  stream.
> ```
(set-rest st)   →   generic-set?
  st : (and/c generic-set? (not/c set-empty?))```
Produces a set that includes all elements of st except ([set-first](#%28def._%28%28lib._racket%2Fset..rkt%29._set-first%29%29) st).
Supported for any st that [implements](struct-generics.html#%28tech._implemented._generic._method%29) [set-remove](#%28def._%28%28lib._racket%2Fset..rkt%29._set-remove%29%29) and either [set-first](#%28def._%28%28lib._racket%2Fset..rkt%29._set-first%29%29) or [set->stream](#%28def._%28%28lib._racket%2Fset..rkt%29._set-~3estream%29%29).
> ```
(set  →  stream st)  →  stream?
  st : generic-set?


# 4.18 Dictionaries
```
Produces a stream containing the elements of st.

Supported for any st that implements:

-  set  →  list

-  in-set

-  set-empty?, set-first, set-rest

-  set-empty?, set-first, set-remove

-  set-count, set-first, set-rest

-  set-count, set-first, set-remove

> ```
(set-copy st)   →   generic-set?
  st : generic-set?```

Produces a new, mutable set of the same type and with the same elements as st.

Supported for any st that [supports](struct-generics.html#%28tech._supported._generic._method%29) [set->stream](#%28def._%28%28lib._racket%2Fset..rkt%29._set-~3estream%29%29) and [implements](struct-generics.html#%28tech._implemented._generic._method%29) [set-copy-clear](#%28def._%28%28lib._racket%2Fset..rkt%29._set-copy-clear%29%29) and [set-add!](#%28def._%28%28lib._racket%2Fset..rkt%29._set-add%21%29%29).

> ```
(set-copy-clear st)  →  (and/c generic-set? set-empty?)
  st : generic-set?
```
Produces a new, empty set of the same type, mutability, and key strength as st.
A difference between set-copy-clear and set-clear is that the latter conceptually iterates set-remove on the given set, and so it preserves any contract on the given set. The set-copy-clear function produces a new set without any contracts.
The set-copy-clear function must call concrete set constructors and thus has no generic fallback.
> ```
(set-clear st)   →   (and/c generic-set? set-empty?)
  st : generic-set?```
Produces a set like st but with all elements removed.


# 4.18 Dictionaries
Supported for any st that [implements](struct-generics.html#%28tech._implemented._generic._method%29) [set-remove](#%28def._%28%28lib._racket%2Fset..rkt%29._set-remove%29%29) and [supports](struct-generics.html#%28tech._supported._generic._method%29) [set->stream](#%28def._%28%28lib._racket%2Fset..rkt%29._set-~3estream%29%29).
> ```
(parameter)	→
```
Removes all elements from st.

Supported for any st that implements set-remove! and either supports set  →  stream or implements set-first and either set-count or set-empty?.

For hash sets, see also the caveats concerning concurrent modification for hash tables, which applies to hash sets.

> ```
(set-union st0 st ...) → generic-set?
  st0 : generic-set?
  st : generic-set?```

Produces a set of the same type as st0 that includes the elements from st0 and all of the sts.

If st0 is a list, each st must also be a list. This operation runs on lists in time proportional to the total size of the sts times the size of the result.

If st0 is a [hash set](#%28tech._hash._set%29), each st must also be a [hash set](#%28tech._hash._set%29) that uses the same comparison function ([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)). The mutability and key strength of the hash sets may differ. This operation runs on hash sets in time proportional to the total size of all of the sets except the largest immutable set.

At least one set must be provided to [set-union](#%28def._%28%28lib._racket%2Fset..rkt%29._set-union%29%29) to determine the type of the resulting set (list, hash set, etc.). If there is a case where [set-union](#%28def._%28%28lib._racket%2Fset..rkt%29._set-union%29%29) may be applied to zero arguments, instead pass an empty set of the intended type as the first argument.

Supported for any st that [implements](struct-generics.html#%28tech._implemented._generic._method%29) [set-add](#%28def._%28%28lib._racket%2Fset..rkt%29._set-add%29%29) and [supports](struct-generics.html#%28tech._supported._generic._method%29) [set->stream](#%28def._%28%28lib._racket%2Fset..rkt%29._set-~3estream%29%29).

Examples:

> ```racket
> > ( set-union ( set ) )
> (set)
> > ( set-union ( seteq ) )
> (seteq)
> > ( set-union ( set 1 2 ) ( set 2 3 ) )
> (set 1 2 3)
> > ( set-union ( list 1 2 ) ( list 2 3 ) )
> '(3 1 2)
> > ( set-union ( set 1 2 ) ( seteq 2 3 ) )
> set-union: set arguments have incompatible equivalence
> predicates
> first set: (set 1 2)
> incompatible set: (seteq 2 3)
> ; Sets of different types cannot be unioned
> ```

> ```
(parameter)	→
```


# 4.18 Dictionaries
Adds the elements from all of the sts to st0.
If st0 is a hash set, each st must also be a hash set that uses the same comparison function (equal?, equal-always?, eqv?, or eq?). The mutability and key strength of the hash sets may differ. This operation runs on hash sets in time proportional to the total size of the sts.
Supported for any st that implements set-add! and supports set  →  stream.
For hash sets, see also the caveats concerning concurrent modification for hash tables, which applies to hash sets.
> ```
(set-intersect st0 st ...) → generic-set?
  st0 : generic-set?
  st : generic-set?```
Produces a set of the same type as st0 that includes the elements from st0 that are also contained by all of the sts.
If st0 is a list, each st must also be a list. This operation runs on lists in time proportional to the total size of the sts times the size of st0.
If st0 is a [hash set](#%28tech._hash._set%29), each st must also be a [hash set](#%28tech._hash._set%29) that uses the same comparison function ([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)). The mutability and key strength of the hash sets may differ. This operation runs on hash sets in time proportional to the size of the smallest immutable set.


# 4.18 Dictionaries
Supported for any st that [implements](struct-generics.html#%28tech._implemented._generic._method%29) either [set-remove](#%28def._%28%28lib._racket%2Fset..rkt%29._set-remove%29%29) or both [set-clear](#%28def._%28%28lib._racket%2Fset..rkt%29._set-clear%29%29) and [set-add](#%28def._%28%28lib._racket%2Fset..rkt%29._set-add%29%29), and [supports](struct-generics.html#%28tech._supported._generic._method%29) [set->stream](#%28def._%28%28lib._racket%2Fset..rkt%29._set-~3estream%29%29).
> ```
(parameter)	→
```
Removes every element from st0 that is not contained by all of the sts.

If st0 is a hash set, each st must also be a hash set that uses the same comparison function (equal?, equal-always?, eqv?, or eq?). The mutability and key strength of the hash sets may differ. This operation runs on hash sets in time proportional to the size of st0.

Supported for any st that implements set-remove! and supports set  →  stream.

For hash sets, see also the caveats concerning concurrent modification for hash tables, which applies to hash sets.

> ```
(set-subtract st0 st ...) → generic-set?
  st0 : generic-set?
  st : generic-set?```

Produces a set of the same type as st0 that includes the elements from st0 that are not contained by any of the sts.

If st0 is a list, each st must also be a list. This operation runs on lists in time proportional to the total size of the sts times the size of st0.

If st0 is a [hash set](#%28tech._hash._set%29), each st must also be a [hash set](#%28tech._hash._set%29) that uses the same comparison function ([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)). The mutability and key strength of the hash sets may differ. This operation runs on hash sets in time proportional to the size of st0.

Supported for any st that [implements](struct-generics.html#%28tech._implemented._generic._method%29) either [set-remove](#%28def._%28%28lib._racket%2Fset..rkt%29._set-remove%29%29) or both [set-clear](#%28def._%28%28lib._racket%2Fset..rkt%29._set-clear%29%29) and [set-add](#%28def._%28%28lib._racket%2Fset..rkt%29._set-add%29%29), and [supports](struct-generics.html#%28tech._supported._generic._method%29) [set->stream](#%28def._%28%28lib._racket%2Fset..rkt%29._set-~3estream%29%29).

> ```
(parameter)	→
```


# 4.18 Dictionaries
Removes every element from st0 that is contained by any of the sts.
If st0 is a hash set, each st must also be a hash set that uses the same comparison function (equal?, equal-always?, eqv?, or eq?). The mutability and key strength of the hash sets may differ. This operation runs on hash sets in time proportional to the size of st0.
Supported for any st that implements set-remove! and supports set  →  stream.
For hash sets, see also the caveats concerning concurrent modification for hash tables, which applies to hash sets.
> ```
(set-symmetric-difference st0 st ...) → generic-set?
  st0 : generic-set?
  st : generic-set?```
Produces a set of the same type as st0 that includes all of the elements contained an odd number of times in st0 and the sts.
If st0 is a list, each st must also be a list. This operation runs on lists in time proportional to the total size of the sts times the size of st0.
If st0 is a [hash set](#%28tech._hash._set%29), each st must also be a [hash set](#%28tech._hash._set%29) that uses the same comparison function ([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)). The mutability and key strength of the hash sets may differ. This operation runs on hash sets in time proportional to the total size of all of the sets except the largest immutable set.


# 4.18 Dictionaries
Supported for any st that [implements](struct-generics.html#%28tech._implemented._generic._method%29) [set-remove](#%28def._%28%28lib._racket%2Fset..rkt%29._set-remove%29%29) or both [set-clear](#%28def._%28%28lib._racket%2Fset..rkt%29._set-clear%29%29) and [set-add](#%28def._%28%28lib._racket%2Fset..rkt%29._set-add%29%29), and [supports](struct-generics.html#%28tech._supported._generic._method%29) [set->stream](#%28def._%28%28lib._racket%2Fset..rkt%29._set-~3estream%29%29).
Example:
> ```racket
> > ( set-symmetric-difference ( set 1 ) ( set 1 2 ) ( set 1 2 3 ) )
> (set 1 3)
> ```
> ```
(parameter)	→


# 4.18 Dictionaries
```
Adds and removes elements of st0 so that it includes all of the elements contained an odd number of times in the sts and the original contents of st0.

If st0 is a hash set, each st must also be a hash set that uses the same comparison function (equal?, equal-always?, eqv?, or eq?). The mutability and key strength of the hash sets may differ. This operation runs on hash sets in time proportional to the total size of the sts.

Supported for any st that implements set-remove! and supports set  →  stream.

For hash sets, see also the caveats concerning concurrent modification for hash tables, which applies to hash sets.

> ```
(set=? st st2) → boolean?
  st : generic-set?
  st2 : generic-set?```

Returns #t if st and st2 contain the same members; returns #f otherwise.

If st is a list, then st2 must also be a list. This operation runs on lists in time proportional to the size of st times the size of st2.

If st is a [hash set](#%28tech._hash._set%29), then st2 must also be a [hash set](#%28tech._hash._set%29) that uses the same comparison function ([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)). The mutability and key strength of the hash sets may differ. This operation runs on hash sets in time proportional to the size of st plus the size of st2.

Supported for any st and st2 that both [support](struct-generics.html#%28tech._supported._generic._method%29) [subset?](#%28def._%28%28lib._racket%2Fset..rkt%29._subset~3f%29%29); also supported for any if st2 that [implements](struct-generics.html#%28tech._implemented._generic._method%29) [set=?](#%28def._%28%28lib._racket%2Fset..rkt%29._set~3d~3f%29%29) regardless of st.

Examples:

> ```racket
> > ( set=? ( list 1 2 ) ( list 2 1 ) )
> #t
> > ( set=? ( set 1 ) ( set 1 2 3 ) )
> #f
> > ( set=? ( set 1 2 3 ) ( set 1 ) )
> #f
> > ( set=? ( set 1 2 3 ) ( set 1 2 3 ) )
> #t
> > ( set=? ( seteq 1 2 ) ( mutable-seteq 2 1 ) )
> #t
> > ( set=? ( seteq 1 2 ) ( seteqv 1 2 ) )
> set=?: set arguments have incompatible equivalence
> predicates
> first set: (seteq 1 2)
> incompatible set: (seteqv 1 2)
> ; Sets of different types cannot be compared
> ```

> ```
(subset? st st2)  →  boolean?
  st : generic-set?
  st2 : generic-set?
```


# 4.18 Dictionaries
Returns #t if st2 contains every member of st; returns #f otherwise.
If st is a list, then st2 must also be a list. This operation runs on lists in time proportional to the size of st times the size of st2.
If st is a hash set, then st2 must also be a hash set that uses the same comparison function (equal?, equal-always?, eqv?, or eq?). The mutability and key strength of the hash sets may differ. This operation runs on hash sets in time proportional to the size of st.
Supported for any st that supports set  →  stream.
Examples:
> ```racket
> > ( subset? ( set 1 ) ( set 1 2 3 ) )
> #t
> > ( subset? ( set 1 2 3 ) ( set 1 ) )
> #f
> > ( subset? ( set 1 2 3 ) ( set 1 2 3 ) )
> #t
> ```
> ```
(proper-subset? st st2)   →   boolean?
  st : generic-set?
  st2 : generic-set?```
Returns #t if st2 contains every member of st and at least one additional element; returns #f otherwise.
If st is a list, then st2 must also be a list. This operation runs on lists in time proportional to the size of st times the size of st2.


# 4.18 Dictionaries
If st is a [hash set](#%28tech._hash._set%29), then st2 must also be a [hash set](#%28tech._hash._set%29) that uses the same comparison function ([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)). The mutability and key strength of the hash sets may differ. This operation runs on hash sets in time proportional to the size of st plus the size of st2.
Supported for any st and st2 that both [support](struct-generics.html#%28tech._supported._generic._method%29) [subset?](#%28def._%28%28lib._racket%2Fset..rkt%29._subset~3f%29%29).
Examples:
> ```racket
> > ( proper-subset? ( set 1 ) ( set 1 2 3 ) )
> #t
> > ( proper-subset? ( set 1 2 3 ) ( set 1 ) )
> #f
> > ( proper-subset? ( set 1 2 3 ) ( set 1 2 3 ) )
> #f
> ```
> ```
(set  →  list st)  →  list?
  st : generic-set?


# 4.18 Dictionaries
```
Produces a list containing the elements of st.

Supported for any st that supports set  →  stream.

> ```
(set-map st proc)   →   (listof any/c)
  st : generic-set?
  proc : (any/c .  →  . any/c)```

Applies the procedure proc to each element in st in an unspecified order, accumulating the results into a list.

Supported for any st that [supports](struct-generics.html#%28tech._supported._generic._method%29) [set->stream](#%28def._%28%28lib._racket%2Fset..rkt%29._set-~3estream%29%29).

> ```
(parameter)	→
(-> any/c . → . any)
```
Applies proc to each element in st (for the side-effects of proc) in an unspecified order.
Supported for any st that supports set  →  stream.
> ```
(in-set st)   →   sequence?
  st : generic-set?```
Explicitly converts a set to a sequence for use with [for](for.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) and other forms.
Supported for any st that [supports](struct-generics.html#%28tech._supported._generic._method%29) [set->stream](#%28def._%28%28lib._racket%2Fset..rkt%29._set-~3estream%29%29).
> ```
(impersonate-hash-set st
    inject-proc
    add-proc
    shrink-proc
    extract-proc
   [ clear-proc
    equal-key-proc]
    prop
    prop-val ...
    ...)


# 4.18 Dictionaries
  →  (and/c (or/c set-mutable? set-weak?) impersonator?)
  st : (or/c set-mutable? set-weak?)
  inject-proc : (or/c #f (  →   set? any/c any/c))
  add-proc : (or/c #f (  →   set? any/c any/c))
  shrink-proc : (or/c #f (  →   set? any/c any/c))
  extract-proc : (or/c #f (  →   set? any/c any/c))
  clear-proc : (or/c #f (  →   set? any)) = #f
  equal-key-proc : (or/c #f (  →   set? any/c any/c)) = #f
  prop : impersonator-property?
  prop-val : any/c
```
Impersonates st, redirecting various set operations via the given procedures.

The inject-proc procedure is called whenever an element is temporarily put into the set for the purposes of comparing it with other elements that may already be in the set. For example, when evaluating (set-member? s e), e will be passed to the inject-proc before comparing it with other elements of s.

The add-proc procedure is called when adding an element to a set, e.g., via set-add or set-add!. The result of the add-proc is stored in the set.

The shrink-proc procedure is called when building a new set with one fewer element. For example, when evaluating (set-remove s e) or (set-remove! s e), an element is removed from a set, e.g., via set-remove or set-remove!. The result of the shrink-proc is the element actually removed from the set.

The extract-proc procedure is called when an element is pulled out of a set, e.g., by set-first. The result of the extract-proc is the element actually produced by from the set.

The clear-proc is called by set-clear and set-clear! and if it returns (as opposed to escaping, perhaps via raising an exception), the clearing operation is permitted. Its result is ignored. If clear-proc is #f, then clearing is done element by element (via calls into the other supplied procedures).

The equal-key-proc is called when an element’s hash code is needed of when an element is supplied to the underlying equality in the set. The result of equal-key-proc is used when computing the hash or comparing for equality.

If any of the inject-proc, add-proc, shrink-proc, or extract-proc arguments are #f, then they all must be #f, the clear-proc and equal-key-proc must also be #f, and there must be at least one property supplied.

Pairs of prop and prop-val (the number of arguments to impersonate-hash-set must be odd) add impersonator properties or override impersonator property values of st.

> ```
(chaperone-hash-set st
    inject-proc
    add-proc
    shrink-proc
    extract-proc
   [ clear-proc
    equal-key-proc]
    prop
    prop-val ...
    ...)

   →   (and/c (or/c set? set-mutable? set-weak?) chaperone?)
  st : (or/c set? set-mutable? set-weak?)
  inject-proc : (or/c #f (  →   set? any/c any/c))
  add-proc : (or/c #f (  →   set? any/c any/c))
  shrink-proc : (or/c #f (  →   set? any/c any/c))
  extract-proc : (or/c #f (  →   set? any/c any/c))
  clear-proc : (or/c #f (  →   set? any)) = #f
  equal-key-proc : (or/c #f (  →   set? any/c any/c)) = #f
  prop : impersonator-property?
  prop-val : any/c```

Chaperones st. Like [impersonate-hash-set](#%28def._%28%28lib._racket%2Fset..rkt%29._impersonate-hash-set%29%29) but with the constraints that the results of the inject-proc, add-proc, shrink-proc, extract-proc, and equal-key-proc must be [chaperone-of?](chaperones.html#%28def._%28%28quote._~23~25kernel%29._chaperone-of~3f%29%29) their second arguments. Also, the input may be an [immutable?](booleans.html#%28def._%28%28quote._~23~25kernel%29._immutable~3f%29%29) set.

#### 4.19.4 Custom Hash Sets

> ```
syntax

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

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

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


# 4.18 Dictionaries
(parameter)	→
(-> any/c . → . boolean?)```
Creates a new set type based on the given comparison function eql?, hash functions hash1 and hash2, and predicate elem?. The new set type has variants that are immutable, mutable with strongly-held elements, and mutable with weakly-held elements. The given name is used when printing instances of the new set 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 elements 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 elements.
The hash functions hash1 and hash2 may accept 1 or 2 arguments. If either hash function accepts 1 argument, it is applied to a element 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 elements.
The predicate elem? must accept 1 argument and is used to recognize valid elements for the new set type.
Produces seven values:
-   a predicate recognizing all instances of the new set type,
-   a predicate recognizing weak instances,
-   a predicate recognizing mutable instances,
-   a predicate recognizing immutable instances,
-   a constructor for weak instances,
-   a constructor for mutable instances, and
-   a constructor for immutable instances.


# 4.18 Dictionaries
See [define-custom-set-types](#%28form._%28%28lib._racket%2Fset..rkt%29._define-custom-set-types%29%29) for an example.
------------------------------------------------------------------------


# 4.20 Procedures


### 4.20 Procedures
> ```
(procedure? v)  →  boolean?
  v : any/c
```
Returns #t if v is a procedure, #f otherwise.

> ```
(apply proc v ... lst #:<kw> kw-arg ...) → any
  proc : procedure?
  v : any/c
  lst : list?
  kw-arg : any/c```

> > > <img src="finger.png" width="24" height="24" alt="+" />[The apply Function](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=application.html%23%2528part._apply%2529&version=8.18.0.13) in [The Racket Guide](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=index.html&version=8.18.0.13) introduces [apply](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._apply%29%29).

Applies proc using the content of ([list*](pairs.html#%28def._%28%28quote._~23~25kernel%29._list%2A%29%29) v [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29) lst) as the (by-position) arguments. The #:\<kw> kw-arg sequence is also supplied as keyword arguments to proc, where #:\<kw> stands for any keyword.

The given proc must accept as many arguments as the number of vs plus length of lst, it must accept the supplied keyword arguments, and it must not require any other keyword arguments; otherwise, the [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception is raised. The given proc is called in tail position with respect to the [apply](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._apply%29%29) call.

Examples:

> ```racket
> > ( apply + ' ( 1 2 3 ) )
> 6
> > ( apply + 1 2 ' ( 3 ) )
> 6
> > ( apply + ' ( ) )
> 0
> > ( apply sort ( list ( list ' ( 2 ) ' ( 1 ) ) < ) #:key car )> '((1) (2))
> ```

> ```
(compose proc ...) → procedure?
  proc : procedure?

(compose1 proc ...)  →  procedure?
  proc : procedure?
```


# 4.20 Procedures
(parameter)	→
(-> #<path:/> #<path:a> #f)```
Returns a procedure that is like proc, except that its name as returned by [object-name](inspectors.html#%28def._%28%28quote._~23~25kernel%29._object-name%29%29) (and as printed for debugging) is name and its [realm](exns.html#%28tech._realm%29) (potentially used for adjusting error messages) is realm.
The given name and realm are used for printing and adjusting an error message if the resulting procedure is applied to the wrong number of arguments. In addition, if proc is an [accessor](define-struct.html#%28tech._accessor%29) or [mutator](define-struct.html#%28tech._mutator%29) produced by [struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29), [make-struct-field-accessor](creatingmorestructs.html#%28def._%28%28quote._~23~25kernel%29._make-struct-field-accessor%29%29), or [make-struct-field-mutator](creatingmorestructs.html#%28def._%28%28quote._~23~25kernel%29._make-struct-field-mutator%29%29), the resulting procedure also uses name when its (first) argument has the wrong type. More typically, however, name is not used for reporting errors, since the procedure name is typically hard-wired into an internal check.
Changed in version 8.4.0.2 of package base: Added the realm argument.
> ```
(parameter)	→


# 4.20 Procedures
```
(parameter)	→```

Returns a procedure that is like proc except that, when applied to the wrong number of arguments, the resulting error hides the first argument as if the procedure had been compiled with the 'method-arity-error syntax property.

> ```
(procedure-closure-contents-eq?   proc1
    proc2)    →    boolean?

  proc1 : procedure?
  proc2 : procedure?
```
(parameter)	→
(-> listof keyword?)```
> > > <img src="finger.png" width="24" height="24" alt="+" />[The apply Function](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=application.html%23%2528part._apply%2529&version=8.18.0.13) in [The Racket Guide](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=index.html&version=8.18.0.13) introduces [keyword-apply](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._keyword-apply%29%29).
Like [apply](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._apply%29%29), but kw-lst and kw-val-lst supply by-keyword arguments in addition to the by-position arguments of the vs and lst, and in addition to the directly supplied keyword arguments in the #:\<kw> kw-arg sequence, where #:\<kw> stands for any keyword.


# 4.20 Procedures
The given kw-lst must be sorted using [keyword<?](keywords.html#%28def._%28%28quote._~23~25kernel%29._keyword~3c~3f%29%29). No keyword can appear twice in kw-lst or both in kw-lst and as a #:\<kw>, otherwise, the [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception is raised. The given kw-val-lst must have the same length as kw-lst, otherwise, the [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception is raised. The given proc must accept all of the keywords in kw-lst plus the #:\<kw>s, it must not require any other keywords, and it must accept as many by-position arguments as supplied via the vs and lst; otherwise, the [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception is raised.
Examples:
> ```racket
> ( define ( f x #:y y #:z [ z 10 ] ) ( list x y z ) )
> > ( keyword-apply f ' ( #:y ) ' ( 2 ) ' ( 1 ) )
> '(1 2 10)
> > ( keyword-apply f ' ( #:y #:z ) ' ( 2 3 ) ' ( 1 ) )
> '(1 2 3)
> > ( keyword-apply f #:z 7 ' ( #:y ) ' ( 2 ) ' ( 1 ) )
> '(1 2 7)
> ```
> ```
(procedure-arity proc)  →  normalized-arity?
  proc : procedure?


# 4.20 Procedures
```
Returns information about the number of by-position arguments accepted by proc. See also procedure-arity?, normalized-arity?, and procedure-arity-mask.

> ```
(procedure-arity? v)   →   boolean?
  v : any/c```

A valid arity a is one of the following:

-   An exact non-negative integer, which means that the procedure accepts a arguments, only.

-   A [arity-at-least](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._arity-at-least%29%29) instance, which means that the procedure accepts ([arity-at-least-value](#%28def._%28%28quote._~23~25kernel%29._arity-at-least-value%29%29) a) or more arguments.

-   A list containing integers and [arity-at-least](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._arity-at-least%29%29) instances, which means that the procedure accepts any number of arguments that can match one of the elements of a.

The result of [procedure-arity](#%28def._%28%28quote._~23~25kernel%29._procedure-arity%29%29) is always normalized in the sense of [normalized-arity?](#%28def._%28%28lib._racket%2Ffunction..rkt%29._normalized-arity~3f%29%29).

Examples:

> ```racket
> > ( procedure-arity cons )
> 2
> > ( procedure-arity list )
> (arity-at-least 0)
> > ( arity-at-least? ( procedure-arity list ) )
> #t
> > ( arity-at-least-value ( procedure-arity list ) )
> 0
> > ( arity-at-least-value ( procedure-arity ( lambda ( x . y ) x ) ) )
> 1
> > ( procedure-arity ( case-lambda [ ( x ) 0 ] [ ( x y ) 1 ] ) )
> '(1 2)
> ```

> ```
(procedure-arity-mask proc)  →  exact-integer?
  proc : procedure?
```


# 4.20 Procedures
Returns the same information as procedure-arity, but encoded differently. The arity is encoded as an exact integer mask where (bitwise-bit-set? mask n) returns true if proc accepts n arguments.
The mask encoding of an arity is often easier to test and manipulate, and procedure-arity-mask is sometimes faster than procedure-arity while always being at least as fast.
Added in version 7.0.0.11 of package base.
> ```
(procedure-arity-includes? proc k [kws-ok?) → boolean?
  proc : procedure?
  k : exact-nonnegative-integer?
  kws-ok? : any/c = #f```
Returns #t if the procedure can accept k by-position arguments, #f otherwise. If kws-ok? is #f, the result is #t only if proc has no required keyword arguments.
Examples:
> ```racket
> > ( procedure-arity-includes? cons 2 )
> #t
> > ( procedure-arity-includes? display 3 )
> #f
> > ( procedure-arity-includes? ( lambda ( x #:y y ) x ) 1 )
> #f
> > ( procedure-arity-includes? ( lambda ( x #:y y ) x ) 1 #t )
> #t
> ```
> ```
(parameter)	→
(-> or/c symbol? #f)


# 4.20 Procedures
```
(parameter)	→
(-> or/c symbol? #f)```

The same as [procedure-reduce-arity](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._procedure-reduce-arity%29%29), but using the representation of arity described with [procedure-arity-mask](#%28def._%28%28quote._~23~25kernel%29._procedure-arity-mask%29%29).

The mask encoding of an arity is often easier to test and manipulate, and [procedure-reduce-arity-mask](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._procedure-reduce-arity-mask%29%29) is sometimes faster than [procedure-reduce-arity](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._procedure-reduce-arity%29%29) while always being at least as fast.

Added in version 7.0.0.11 of package base.
Changed in version 8.4.0.2: Added the realm argument.

> ```
(parameter)	→
(-> listof keyword?)
```
Returns information about the keyword arguments required and accepted by a procedure. The first result is a list of distinct keywords (sorted by keyword<?) that are required when applying proc. The second result is a list of distinct accepted keywords (sorted by keyword<?), or #f to mean that any keyword is accepted. When the second result is a list, every element in the first list is also in the second list.
Examples:
> ```racket
> > ( procedure-keywords + )
> '() '()
> > ( procedure-keywords ( lambda ( #:tag t #:mode m ) t ) )
> '(#:mode #:tag) '(#:mode #:tag)
> > ( procedure-keywords ( lambda ( #:tag t #:mode [ m #f ] ) t ) )
> '(#:tag) '(#:mode #:tag)
> ```
> ```
(procedure-result-arity proc) → (or/c #f procedure-arity?)
  proc : procedure?```


# 4.20 Procedures
Returns the arity of the result of the procedure proc or #f if the number of results are not known, perhaps due to shortcomings in the implementation of [procedure-result-arity](#%28def._%28%28quote._~23~25kernel%29._procedure-result-arity%29%29) or because proc’s behavior is not sufficiently simple.
Examples:
> ```racket
> > ( procedure-result-arity car )
> 1
> > ( procedure-result-arity values )
> #f
> > ( procedure-result-arity ( λ ( x ) ( apply values ( let loop ( ) ( cond [ ( zero? ( random 10 ) ) ' ( ) ] [ else ( cons 1 ( loop ) ) ] ) ) ) ) )
> #f
> ```
Added in version 6.4.0.3 of package base.
> ```
(parameter)	→
(-> (listof keyword?) list? any/c ... . → . any)


# 4.20 Procedures
```
(1)	→
(-> show2 1)```

Like [procedure-reduce-arity](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._procedure-reduce-arity%29%29), but constrains the keyword arguments according to required-kws and allowed-kws, which must be sorted using [keyword<?](keywords.html#%28def._%28%28quote._~23~25kernel%29._keyword~3c~3f%29%29) and contain no duplicates. If allowed-kws is #f, then the resulting procedure still accepts any keyword, otherwise the keywords in required-kws must be a subset of those in allowed-kws. The original proc must require no more keywords than the ones listed in required-kws, and it must allow at least the keywords in allowed-kws (or it must allow all keywords if allowed-kws is #f).

Examples:

> ```racket
> ( define orig-show ( make-keyword-procedure ( lambda ( kws kw-args . rest ) ( list kws kw-args rest ) ) ) ) ( define show ( procedure-reduce-keyword-arity orig-show 3 ' ( #:init ) ' ( #:extra #:init ) ) )
> > ( show #:init 0 1 2 3 #:extra 4 )
> '((#:extra #:init) (4 0) (1 2 3))
> > ( show 1 )
> arity mismatch;
> the expected number of arguments does not match the given
> number
> expected: 3 plus an argument with keyword #:init plus an
> optional argument with keyword #:extra
> given: 1
> arguments...:
> 1
> > ( show #:init 0 1 2 3 #:extra 4 #:more 7 )
> application: procedure does not expect an argument with
> given keyword
> procedure: #<procedure>
> given keyword: #:more
> arguments...:
> 1
> 2
> 3
> #:extra 4
> #:init 0
> #:more 7
> ```

Changed in version 8.4.0.2 of package base: Added the realm argument.

> ```
(parameter)	→
(-> listof keyword?)
```


# 4.20 Procedures
The same as procedure-reduce-keyword-arity, but using the representation of arity described with procedure-arity-mask.
Added in version 7.0.0.11 of package base.
Changed in version 8.4.0.2: Added the realm argument.
>
<colgroup><col style="width: 100%" /></colgroup>

struct

(struct	 	arity-at-least (value)

    #:extra-constructor-name make-arity-at-least)

  value : exact-nonnegative-integer?

A structure type used for the result of procedure-arity. See also procedure-arity?.
>
<colgroup><col style="width: 100%" /></colgroup>

value

prop:procedure : struct-type-property?

A structure type property to identify structure types whose instances can be applied as procedures. In particular, when procedure? is applied to the instance, the result will be #t, and when an instance is used in the function position of an application expression, a procedure is extracted from the instance and used to complete the procedure call.


# 4.20 Procedures
If the prop:procedure property value is an exact non-negative integer, it designates a field within the structure that should contain a procedure. The integer must be between 0 (inclusive) and the number of non-automatic fields in the structure type (exclusive, not counting supertype fields). The designated field must also be specified as immutable, so that after an instance of the structure is created, its procedure cannot be changed. (Otherwise, the arity and name of the instance could change, and such mutations are generally not allowed for procedures.) When the instance is used as the procedure in an application expression, the value of the designated field in the instance is used to complete the procedure call. (This procedure can be another structure that acts as a procedure; the immutability of procedure fields disallows cycles in the procedure graph, so that the procedure call will eventually continue with a non-structure procedure.) That procedure receives all of the arguments from the application expression. The procedure’s name (see object-name), arity (see procedure-arity), and keyword protocol (see procedure-keywords) are also used for the name, arity, and keyword protocol of the structure. If the value in the designated field is not a procedure, then the instance behaves like (case-lambda) (i.e., a procedure which does not accept any number of arguments). See also procedure-extract-target.


# 4.20 Procedures
Providing an integer proc-spec argument to make-struct-type is the same as both supplying the value with the prop:procedure property and designating the field as immutable (so that a property binding or immutable designation is redundant and disallowed).
Examples:
> ```racket
> > ( struct annotated-proc ( base note ) #:property prop:procedure ( struct-field-index base ) )
> > ( define plus1 ( annotated-proc ( lambda ( x ) ( + x 1 ) ) "adds 1 to its argument" ) )
> > ( procedure? plus1 )
> #t
> > ( annotated-proc? plus1 )
> #t
> > ( plus1 10 )
> 11
> > ( annotated-proc-note plus1 )
> "adds 1 to its argument"
> ```
When the prop:procedure value is a procedure, it should accept at least one non-keyword argument. When an instance of the structure is used in an application expression, the property-value procedure is called with the instance as the first argument. The remaining arguments to the property-value procedure are the arguments from the application expression (including keyword arguments). Thus, if the application expression provides five non-keyword arguments, the property-value procedure is called with six non-keyword arguments. The name of the instance (see object-name) and its keyword protocol (see procedure-keywords) are unaffected by the property-value procedure, but the instance’s arity is determined by subtracting one from every possible non-keyword argument count of the property-value procedure. If the property-value procedure cannot accept at least one argument, then the instance behaves like (case-lambda).


# 4.20 Procedures
Providing a procedure proc-spec argument to make-struct-type is the same as supplying the value with the prop:procedure property (so that a specific property binding is disallowed).
Examples:
> ```racket
> > ( struct fish ( weight color ) #:mutable #:property prop:procedure ( lambda ( f n ) ( let ( [ w ( fish-weight f ) ] ) ( set-fish-weight! f ( + n w ) ) ) ) )
> > ( define wanda ( fish 12 ' red ) )
> > ( fish? wanda )
> #t
> > ( procedure? wanda )
> #t
> > ( fish-weight wanda )
> 12
> > ( for-each wanda ' ( 1 2 3 ) )
> > ( fish-weight wanda )
> 18
> ```
If the value supplied for the prop:procedure property is not an exact non-negative integer or a procedure, the exn:fail:contract exception is raised.
> ```
(procedure-struct-type? type) → boolean?
  type : struct-type?```
Returns #t if instances of the structure type represented by type are procedures (according to [procedure?](#%28def._%28%28quote._~23~25kernel%29._procedure~3f%29%29)), #f otherwise.
> ```
(procedure-extract-target proc)  →  (or/c #f procedure?)
  proc : procedure?


# 4.20 Procedures
```
If proc is an instance of a structure type with property prop:procedure, and if the property value indicates a field of the structure, and if the field value is a procedure, then procedure-extract-target returns the field value. Otherwise, the result is #f.

When a prop:procedure property value is a procedure, the procedure is not returned by procedure-extract-target. Such a procedure is different from one accessed through a structure field, because it consumes an extra argument, which is always the structure that was applied as a procedure. Keeping the procedure private ensures that is it always called with a suitable first argument.

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

<p>value</p>

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

A structure type property that is used for reporting arity-mismatch errors when a structure type with the prop:procedure property is applied to the wrong number of arguments. The value of the prop:arity-string property must be a procedure that takes a single argument, which is the misapplied structure, and returns a string. The result string is used after the word “expects,” and it is followed in the error message by the number of actual arguments.

Arity-mismatch reporting automatically uses procedure-extract-target when the prop:arity-string property is not associated with a procedure structure type.

Examples:

> ```racket
> > ( struct evens ( proc ) #:property prop:procedure ( struct-field-index proc ) #:property prop:arity-string ( lambda ( p ) "an even number of arguments" ) )
> > ( define pairs ( evens ( case-lambda [ ( ) null ] [ ( a b . more ) ( cons ( cons a b ) ( apply pairs more ) ) ] ) ) )
> > ( pairs 1 2 3 4 )
> '((1 . 2) (3 . 4))
> > ( pairs 5 )
> arity mismatch;
> the expected number of arguments does not match the given
> number
> expected: an even number of arguments
> given: 1
> arguments...:
> 5
> ```

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

<p>value</p>

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

A structure type property that is used with checked-procedure-check-and-extract, which is a hook to allow the compiler to improve the performance of keyword arguments. The property can only be attached to a structure type without a supertype and with at least two fields.

> ```
(checked-procedure-check-and-extract type
    v
    proc
    v1
    v2)    →    any/c

  type : struct-type?
  v : any/c
  proc : (any/c any/c any/c .  →  . any/c)
  v1 : any/c
  v2 : any/c```

Extracts a value from v if it is an instance of type, which must have the property [prop:checked-procedure](#%28def._%28%28quote._~23~25kernel%29._prop~3achecked-procedure%29%29). If v is such an instance, then the first field of v is extracted and applied to v1 and v2; if the result is a true value, the result is the value of the second field of v.

If v is not an instance of type, or if the first field of v applied to v1 and v2 produces #f, then proc is applied to v, v1, and v2, and its result is returned by [checked-procedure-check-and-extract](#%28def._%28%28quote._~23~25kernel%29._checked-procedure-check-and-extract%29%29).

> ```
(procedure-specialize proc)  →  procedure?
  proc : procedure?
```


# 4.20 Procedures
Returns proc or its equivalent, but provides a hint to the run-time system that it should spend extra time and memory to specialize the implementation of proc.
The hint is currently used when proc is the value of a lambda or case-lambda form that references variables bound outside of the lambda or case-lambda, and when proc has not been previously applied.
Added in version 6.3.0.10 of package base.


#### 4.20.2 Reflecting on Primitives
A primitive procedure is a built-in procedure that may be implemented in a lower-level language. Not all procedures of racket/base are primitives, but many are. The distinction between primitives and other procedures may be useful to other low-level code.
> ```
(primitive? v)   →   boolean?
  v : any/c```
Returns #t if v is a primitive procedure, #f otherwise.
> ```
(primitive-closure? v)  →  boolean?
  v : any/c
```
Returns #t if v is internally implemented as a primitive closure rather than a simple primitive procedure, #f otherwise.

> ```
(primitive-result-arity prim)   →   procedure-arity?
  prim : primitive?```

Returns the arity of the result of the primitive procedure prim (as opposed to the procedure’s input arity as returned by [procedure-arity](#%28def._%28%28quote._~23~25kernel%29._procedure-arity%29%29)). For most primitives, this procedure returns 1, since most primitives return a single value when applied.

#### 4.20.3 Additional Higher-Order Functions

|                                                                                |                                                                                |
|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  ([require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/function](#%28mod-path._racket%2Ffunction%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/function](#%28mod-path._racket%2Ffunction%29) and [racket](index.html) libraries, but not [racket/base](index.html).

> ```
(identity v)  →  any/c
  v : any/c
```


# 4.20 Procedures
Returns v.
> ```
(const v)   →   procedure?
  v : any/c```
Returns a procedure that accepts any arguments (including keyword arguments) and returns v.
Examples:
> ```racket
> > ( ( const ' foo ) )
> 'foo
> > ( ( const ' foo ) 1 2 3 )
> 'foo
> > ( ( const ' foo ) ' a ' b #:c ' c )
> 'foo
> ```
> ```
(const* v ...) → procedure?
  v : any/c
```
Similar to const, except it returns vs.

Examples:

> ```racket
> > ( ( const* ) )
> > ( ( const* ) 1 2 3 )
> > ( ( const* ) ' a ' b #:c ' c )
> > ( ( const* ' foo ) )
> 'foo
> > ( ( const* ' foo ) 1 2 3 )
> 'foo
> > ( ( const* ' foo ) ' a ' b #:c ' c )
> 'foo
> > ( ( const* ' foo ' foo ) )
> 'foo 'foo
> > ( ( const* ' foo ' foo ) 1 2 3 )
> 'foo 'foo
> > ( ( const* ' foo ' foo ) ' a ' b #:c ' c )
> 'foo 'foo
> ```

Added in version 8.7.0.5 of package base.

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

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

syntax

(thunk  body ...+)

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

syntax

(thunk* body ...+)

The thunk form creates a nullary function that evaluates the given body. The thunk* form is similar, except that the resulting function accepts any arguments (including keyword arguments).

Examples:

> ```racket
> ( define th1 ( thunk ( define x 1 ) ( printf "~a\n" x ) ) )
> > ( th1 )
> 1
> > ( th1 ' x )
> th1: arity mismatch;
> the expected number of arguments does not match the given
> number
> expected: 0
> given: 1
> > ( th1 #:y ' z )
> application: procedure does not accept keyword arguments
> procedure: th1
> arguments...:
> #:y 'z
> ( define th2 ( thunk* ( define x 1 ) ( printf "~a\n" x ) ) )
> > ( th2 )
> 1
> > ( th2 ' x )
> 1
> > ( th2 #:y ' z )
> 1
> ```

> ```
(negate proc) → procedure?
  proc : procedure?```

Returns a procedure that is just like proc, except that it returns the [not](booleans.html#%28def._%28%28quote._~23~25kernel%29._not%29%29) of proc’s result.

Examples:

> ```racket
> > ( filter ( negate symbol? ) ' ( 1 a 2 b 3 c ) )
> '(1 2 3)
> > ( map ( negate = ) ' ( 1 2 3 ) ' ( 1 1 1 ) )
> '(#f #t #t)
> ```

> ```
((conjoin f ...) x ...)  →  any
  f : procedure?
  x : any/c
```


# 4.20 Procedures
Combines calls to each function with and. Equivalent to (and (f x ...) ...)
Examples:
> ```racket
> ( define f ( conjoin exact? integer? ) )
> > ( f 1 )
> #t
> > ( f 1.0 )
> #f
> > ( f 1/2 )
> #f
> > ( f 0.5 )
> #f
> > ( ( conjoin ( λ ( x ) ( values 1 2 ) ) ) 0 )
> 1 2
> ```
> ```
((disjoin f ...) x ...)   →   any
  f : procedure?
  x : any/c```
Combines calls to each function with [or](if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29). Equivalent to ([or](if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29) (f x [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)) [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29))
Examples:
> ```racket
> ( define f ( disjoin exact? integer? ) )
> > ( f 1 )
> #t
> > ( f 1.0 )
> #t
> > ( f 1/2 )
> #t
> > ( f 0.5 )
> #f
> > ( ( disjoin ( λ ( x ) ( values 1 2 ) ) ) 0 )
> 1 2
> ```
> ```
(curry proc) → procedure?
  proc : procedure?
(curry proc v ...+)  →  any
  proc : procedure?
  v : any/c


# 4.20 Procedures
```
The result of (curry proc) is a procedure that is a curried version of proc. When the resulting procedure is first applied, unless it is given the maximum number of arguments that it can accept according to (procedure-arity proc), the result is a procedure to accept additional arguments.

Examples:

> ```racket
> > ( ( curry list ) 1 2 )
> #<procedure:curried:list>
> > ( ( curry cons ) 1 )
> #<procedure:curried:cons>
> > ( ( curry cons ) 1 2 )
> '(1 . 2)
> ```

After the first application of the result of (curry proc), each further application accumulates arguments until an acceptable number of arguments according to (procedure-arity proc) have been accumulated, at which point the original proc is called.

Examples:

> ```racket
> > ( ( ( curry list ) 1 2 ) 3 )
> '(1 2 3)
> > ( ( ( curry list ) 1 ) 3 )
> '(1 3)
> > ( ( ( ( curry foldl ) + ) 0 ) ' ( 1 2 3 ) )
> 6
> > ( define foo ( curry ( lambda ( x y z ) ( list x y z ) ) ) )
> > ( foo 1 2 3 )
> '(1 2 3)
> > ( ( ( ( ( foo ) 1 ) 2 ) ) 3 )
> '(1 2 3)
> ```

A function call (curry proc v ...) is equivalent to ((curry proc) v ...). In other words, curry itself is curried.

Examples:

> ```racket
> > ( map ( ( curry + ) 10 ) ' ( 1 2 3 ) )
> '(11 12 13)
> > ( map ( curry + 10 ) ' ( 1 2 3 ) )
> '(11 12 13)
> > ( map ( compose ( curry * 2 ) ( curry + 10 ) ) ' ( 1 2 3 ) )
> '(22 24 26)
> ```

The curry function also supports functions with keyword arguments: keyword arguments will be accumulated in the same way as positional arguments until all required keyword arguments according to (procedure-keywords proc) have been supplied.

Examples:

> ```racket
> ( define ( f #:a a #:b b #:c c ) ( list a b c ) )
> > ( ( ( ( curry f ) #:a 1 ) #:b 2 ) #:c 3 )
> '(1 2 3)
> > ( ( ( ( curry f ) #:b 1 ) #:c 2 ) #:a 3 )
> '(3 1 2)
> > ( ( curry f #:a 1 #:c 2 ) #:b 3 )
> '(1 3 2)
> ```

Changed in version 7.0.0.7 of package base: Added support for keyword arguments.

> ```
(curryr proc) → procedure?
  proc : procedure?
(curryr proc v ...+)   →   any
  proc : procedure?
  v : any/c```

Like [curry](#%28def._%28%28lib._racket%2Ffunction..rkt%29._curry%29%29), except that the arguments are collected in the opposite direction: the first step collects the rightmost group of arguments, and following steps add arguments to the left of these.

Example:

> ```racket
> > ( map ( curryr list ' foo ) ' ( 1 2 3 ) )
> '((1 foo) (2 foo) (3 foo))
> ```

> ```
(normalized-arity? arity)  →  boolean?
  arity : any/c
```


# 4.20 Procedures
A normalized arity has one of the following forms:
-  the empty list;
-  an exact non-negative integer;
-  an arity-at-least instance;
-  a list of two or more strictly increasing, exact non-negative integers; or
-  a list of one or more strictly increasing, exact non-negative integers followed by a single arity-at-least instance whose value is greater than the preceding integer by at least 2.
Every normalized arity is a valid procedure arity and satisfies procedure-arity?. Any two normalized arity values that are arity=? must also be equal?.
Examples:
> ```racket
> > ( normalized-arity? ( arity-at-least 1 ) )
> #t
> > ( normalized-arity? ( list ( arity-at-least 1 ) ) )
> #f
> > ( normalized-arity? ( list 0 ( arity-at-least 2 ) ) )
> #t
> > ( normalized-arity? ( list ( arity-at-least 2 ) 0 ) )
> #f
> > ( normalized-arity? ( list 0 2 ( arity-at-least 3 ) ) )
> #f
> ```
> ```
(normalize-arity arity)
   →   (and/c normalized-arity? (lambda (x) (arity=? x arity)))
  arity : procedure-arity?```
Produces a normalized form of arity. See also [normalized-arity?](#%28def._%28%28lib._racket%2Ffunction..rkt%29._normalized-arity~3f%29%29) and [arity=?](#%28def._%28%28lib._racket%2Ffunction..rkt%29._arity~3d~3f%29%29).
Examples:


# 4.20 Procedures
> ```racket
> > ( normalize-arity 1 )
> 1
> > ( normalize-arity ( list 1 ) )
> 1
> > ( normalize-arity ( arity-at-least 2 ) )
> (arity-at-least 2)
> > ( normalize-arity ( list ( arity-at-least 2 ) ) )
> (arity-at-least 2)
> > ( normalize-arity ( list 1 ( arity-at-least 2 ) ) )
> (arity-at-least 1)
> > ( normalize-arity ( list ( arity-at-least 2 ) 1 ) )
> (arity-at-least 1)
> > ( normalize-arity ( list ( arity-at-least 2 ) 3 ) )
> (arity-at-least 2)
> > ( normalize-arity ( list 3 ( arity-at-least 2 ) ) )
> (arity-at-least 2)
> > ( normalize-arity ( list ( arity-at-least 6 ) 0 2 ( arity-at-least 4 ) ) )
> (list 0 2 (arity-at-least 4))
> ```
> ```
(arity=? a b) → boolean?
  a : procedure-arity?
  b : procedure-arity?


# 4.20 Procedures
```
Returns #true if procedures with arity a and b accept the same numbers of arguments, and #false otherwise. Equivalent to both (and (arity-includes? a b) (arity-includes? b a)) and (equal? (normalize-arity a) (normalize-arity b)).

Examples:

> ```racket
> > ( arity=? 1 1 )
> #t
> > ( arity=? ( list 1 ) 1 )
> #t
> > ( arity=? 1 ( list 1 ) )
> #t
> > ( arity=? 1 ( arity-at-least 1 ) )
> #f
> > ( arity=? ( arity-at-least 1 ) 1 )
> #f
> > ( arity=? ( arity-at-least 1 ) ( list 1 ( arity-at-least 2 ) ) )
> #t
> > ( arity=? ( list 1 ( arity-at-least 2 ) ) ( arity-at-least 1 ) )
> #t
> > ( arity=? ( arity-at-least 1 ) ( list 1 ( arity-at-least 3 ) ) )
> #f
> > ( arity=? ( list 1 ( arity-at-least 3 ) ) ( arity-at-least 1 ) )
> #f
> > ( arity=? ( list 0 1 2 ( arity-at-least 3 ) ) ( list ( arity-at-least 0 ) ) )
> #t
> > ( arity=? ( list ( arity-at-least 0 ) ) ( list 0 1 2 ( arity-at-least 3 ) ) )
> #t
> > ( arity=? ( list 0 2 ( arity-at-least 3 ) ) ( list ( arity-at-least 0 ) ) )
> #f
> > ( arity=? ( list ( arity-at-least 0 ) ) ( list 0 2 ( arity-at-least 3 ) ) )
> #f
> ```

> ```
(arity-includes? a b) → boolean?
  a : procedure-arity?
  b : procedure-arity?```

Returns #true if procedures with arity a accept any number of arguments that procedures with arity b accept.

Examples:

> ```racket
> > ( arity-includes? 1 1 )
> #t
> > ( arity-includes? ( list 1 ) 1 )
> #t
> > ( arity-includes? 1 ( list 1 ) )
> #t
> > ( arity-includes? 1 ( arity-at-least 1 ) )
> #f
> > ( arity-includes? ( arity-at-least 1 ) 1 )
> #t
> > ( arity-includes? ( arity-at-least 1 ) ( list 1 ( arity-at-least 2 ) ) )
> #t
> > ( arity-includes? ( list 1 ( arity-at-least 2 ) ) ( arity-at-least 1 ) )
> #t
> > ( arity-includes? ( arity-at-least 1 ) ( list 1 ( arity-at-least 3 ) ) )
> #t
> > ( arity-includes? ( list 1 ( arity-at-least 3 ) ) ( arity-at-least 1 ) )
> #f
> > ( arity-includes? ( list 0 1 2 ( arity-at-least 3 ) ) ( list ( arity-at-least 0 ) ) )
> #t
> > ( arity-includes? ( list ( arity-at-least 0 ) ) ( list 0 1 2 ( arity-at-least 3 ) ) )
> #t
> > ( arity-includes? ( list 0 2 ( arity-at-least 3 ) ) ( list ( arity-at-least 0 ) ) )
> #f
> > ( arity-includes? ( list ( arity-at-least 0 ) ) ( list 0 2 ( arity-at-least 3 ) ) )
> #t
> ```

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

# 4.21 Void

### 4.21 Void

The constant [#<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) is returned by most forms and procedures that have a side-effect and no useful result.

The [#<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) value is always [eq?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29) to itself.

> ```
(parameter)	→
```


# 4.20 Procedures
(parameter)	→```
Returns the constant [#<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). Each v argument is ignored.
------------------------------------------------------------------------


# 4.22 Undefined


### 4.22 Undefined
|                                                                                |                                                                                |
|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  ([require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/undefined]()) |  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/undefined]() library, not [racket/base](index.html) or [racket](index.html).
The constant [undefined](#%28def._%28%28lib._racket%2Fundefined..rkt%29._undefined%29%29) can be used as a placeholder value for a value to be installed later, especially for cases where premature access of the value is either difficult or impossible to detect or prevent.
The [undefined](#%28def._%28%28lib._racket%2Fundefined..rkt%29._undefined%29%29) value is always [eq?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29) to itself.
Added in version 6.0.0.6 of package base.


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

value

[undefined](#%28def._%28%28lib._racket%2Fundefined..rkt%29._undefined%29%29) : [any/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29)

The “undefined” constant.
------------------------------------------------------------------------


# 5 Structures


## 5 Structures
> > > <img src="finger.png" width="24" height="24" alt="+" />[Programmer-Defined Datatypes](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=define-struct.html&version=8.18.0.13) in [The Racket Guide](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=index.html&version=8.18.0.13) introduces structure types via [struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29).
A structure type is a record datatype composing a number of fields. A structure, an instance of a structure type, is a first-class value that contains a value for each field of the structure type. A structure instance is created with a type-specific [constructor](define-struct.html#%28tech._constructor%29) procedure, and its field values are accessed and changed with type-specific [accessor](define-struct.html#%28tech._accessor%29) and [mutator](define-struct.html#%28tech._mutator%29) procedures. In addition, each structure type has a [predicate](define-struct.html#%28tech._predicate%29) procedure that answers #t for instances of the structure type and #f for any other value.


# 5 Structures
A structure type’s fields are essentially unnamed, though names are supported for error-reporting purposes. The constructor procedure takes one value for each field of the structure type, except that some of the fields of a structure type can be automatic fields; the [automatic fields](#%28tech._automatic._field%29) are initialized to a constant that is associated with the structure type, and the corresponding arguments are omitted from the constructor procedure. All automatic fields in a structure type follow the non-automatic fields.
A structure type can be created as a structure subtype of an existing base structure type. An instance of a structure subtype can always be used as an instance of the base structure type, but the subtype gets its own predicate procedure, and it may have its own fields in addition to the fields of the base type.
A structure subtype “inherits” the fields of its base type. If the base type has m fields, and if n fields are specified for the new structure subtype, then the resulting structure type has m+n fields. The value for automatic fields can be different in a subtype than in its base type.


# 5 Structures
If m′ of the original m fields are non-automatic (where m′\<m), and n′ of the new fields are non-automatic (where n′\<n), then m′+n′ field values must be provided to the subtype’s constructor procedure. Values for the first m fields of a subtype instance are accessed with selector procedures for the original base type (or its supertypes), and the last n are accessed with subtype-specific selectors. Subtype-specific [accessors](define-struct.html#%28tech._accessor%29) and [mutators](define-struct.html#%28tech._mutator%29) for the first m fields do not exist.
The [struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29) form and [make-struct-type](creatingmorestructs.html#%28def._%28%28quote._~23~25kernel%29._make-struct-type%29%29) procedure typically create a new structure type, but they can also access prefab (i.e., previously fabricated) structure types that are globally shared, and whose instances can be parsed and written by the default reader (see [The Reader](reader.html)) and printer (see [The Printer](printing.html)). Prefab structure types can inherit only from other prefab structure types, and they cannot have guards (see [Creating Structure Types](creatingmorestructs.html)) or properties (see [Structure Type Properties](structprops.html)). Exactly one prefab structure type exists for each combination of name, supertype, field count, automatic field count, automatic field value (when there is at least one automatic field), and field mutability.


# 5 Structures
> > > <img src="magnify.png" width="24" height="24" alt="+" />[Serialization](serialization.html) also provides information on reading and writing structures.
Two structure values are [eqv?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29) if and only if they are [eq?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29). Two structure values are [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) if they are [eq?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29). By default, two structure values are also [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) if they are instances of the same structure type, no fields are opaque, and the results of applying [struct->vector](structutils.html#%28def._%28%28quote._~23~25kernel%29._struct-~3evector%29%29) to the structs are [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29). (Consequently, [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) testing for structures may depend on the current inspector.) A structure type can override the default [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) definition through the [gen:equal+hash](Equality.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._gen~3aequal%2Bhash%29%29) or [gen:equal-mode+hash](Equality.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._gen~3aequal-mode%2Bhash%29%29) [generic interface](struct-generics.html#%28tech._generic._interface%29).


# 5 Structures
|                                                                                |
|-----------------------------------------------------------------------------------------------------------------------------------------|
|     [5.1 Defining Structure Types: struct](define-struct.html)                                               |
|     [5.2 Creating Structure Types](creatingmorestructs.html)                                                 |
|     [5.3 Structure Type Properties](structprops.html)                                                        |
|     [5.4 Generic Interfaces](struct-generics.html)                                                           |
|     [5.5 Copying and Updating Structures](struct-copy.html)                                                  |
|     [5.6 Structure Utilities](structutils.html)                                                              |
|       [5.6.1 Additional Structure Utilities](structutils.html#%28part._.Additional_.Structure_.Utilities%29) |
|     [5.7 Structure Type Transformer Binding](structinfo.html)                                                |
------------------------------------------------------------------------


# 5.1 Defining Structure Types: struct


### 5.1 Defining Structure Types: [struct](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29)
> > > <img src="finger.png" width="24" height="24" alt="+" />[Programmer-Defined Datatypes](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=define-struct.html&version=8.18.0.13) in [The Racket Guide](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=index.html&version=8.18.0.13) introduces [struct](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29).
> ```
syntax
(struct id maybe-super (field ...)
> struct-option ... )
maybe-super   =
    |   super-id
field   =   field-id
    |   [field-id field-option ...]
struct-option   =   #:mutable
    |   #:super super-expr
    |   #:inspector inspector-expr
    |   #:auto-value auto-expr
    |   #:guard guard-expr
    |   #:property prop-expr val-expr
    |   #:properties prop-list-expr
    |   #:transparent
    |   #:prefab
    |   #:sealed
    |   #:authentic
    |   #:name name-id
    |   #:extra-name name-id
    |   #:constructor-name constructor-id
    |   #:extra-constructor-name constructor-id
    |   #:reflection-name symbol-expr
    |   #:methods gen:name-id method-defs
    |   #:omit-define-syntaxes
    |   #:omit-define-values
field-option   =   #:mutable
    |   #:auto
method-defs   =   (definition ...)


### 5.1 Defining Structure Types: [struct](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29)
```
Creates a new structure type (or uses a pre-existing structure type if #:prefab is specified), and binds transformers and variables related to the structure type.

A struct form with n fields defines up to 4+2n names:

-  struct:id, a structure type descriptor value that represents the structure type.

-  constructor-id (which defaults to id), a constructor procedure that takes m arguments and returns a new instance of the structure type, where m is the number of fields that do not include an #:auto option.

-  name-id (which defaults to id), a transformer binding that encapsulates information about the structure type declaration. This binding is used to define subtypes, and it also works with the shared and match forms. For detailed information about the binding of name-id, see Structure Type Transformer Binding.

The constructor-id and name-id can be the same, in which case name-id performs both roles. In that case, the expansion of name-id as an expression produces an otherwise inaccessible identifier that is bound to the constructor procedure; the expanded identifier has a 'constructor-for property whose value is an identifier that is free-identifier=? to name-id as well as a syntax property accessible via syntax-procedure-alias-property with an identifier that is free-identifier=? to name-id.

-  id?, a predicate procedure that returns #t for instances of the structure type (constructed by constructor-id or the constructor for a subtype) and #f for any other value.

-  id-field-id, for each field; an accessor procedure that takes an instance of the structure type and extracts the value for the corresponding field.

-  set-id-field-id!, for each field that includes a #:mutable option, or when the #:mutable option is specified as a struct-option; a mutator procedure that takes an instance of the structure type and a new field value. The structure is destructively updated with the new value, and #<void> is returned.

If super-id is provided, it must have a transformer binding of the same sort bound to name-id (see Structure Type Transformer Binding), and it specifies a supertype for the structure type. Alternately, the #:super option can be used to specify an expression that must produce a structure type descriptor. See Structures for more information on structure subtypes and supertypes. If both super-id and #:super are provided, a syntax error is reported.

Examples:

> ```racket
> > ( struct document ( author title content ) )
> > ( struct book document ( publisher ) )
> > ( struct paper ( journal ) #:super struct:document )
> ```

If the #:mutable option is specified for an individual field, then the field can be mutated in instances of the structure type, and a mutator procedure is bound. Supplying #:mutable as a struct-option is the same as supplying it for all fields. If #:mutable is specified as both a field-option and struct-option, a syntax error is reported.

Examples:

> ```racket
> > ( struct cell ( [ content #:mutable ] ) #:transparent )
> > ( define a-cell ( cell 0 ) )
> > ( set-cell-content! a-cell 1 )
> ```

The #:inspector, #:auto-value, and #:guard options specify an inspector, value for automatic fields, and guard procedure, respectively. See make-struct-type for more information on these attributes of a structure type. The #:property option, which can be supplied multiple times, attaches a property value to the structure type; see Structure Type Properties for more information on properties. The #:properties option, which can be supplied multiple times, accepts multiple properties and their values as an association list. The #:transparent option is a shorthand for #:inspector #f.

Examples:

> ```racket
> > ( struct point ( x y ) #:inspector #f )
> > ( point 3 5 )
> (point 3 5)
> > ( struct celsius ( temp ) #:guard ( λ ( temp name ) ( unless ( and ( real? temp ) ( >= temp -2 73.15 ) ) ( error "not a valid temperature" ) ) temp ) )
> > ( celsius -2 75 )
> not a valid temperature
> ```

> > > Use the prop:procedure property to implement an applicable structure, use prop:evt to create a structure type whose instances are synchronizable events, and so on. By convention, property names start with prop:.

The #:prefab option obtains a prefab (pre-defined, globally shared) structure type, as opposed to creating a new structure type. Such a structure type is inherently transparent and non-sealed, and it cannot have a guard or properties, so using #:prefab with #:transparent, #:inspector, #:guard, #:property, #:sealed, #:authentic, or #:methods is a syntax error. If a supertype is specified, it must also be a prefab structure type.

Examples:

> ```racket
> > ( struct prefab-point ( x y ) #:prefab )
> > ( prefab-point 1 2 )
> '#s(prefab-point 1 2)
> > ( prefab-point? #s ( prefab-point 1 2 ) )
> #t
> ```

The #:sealed option is a shorthand for #:property prop:sealed #t, which prevents the structure type from being used as the supertype of another structure type. See prop:sealed for more information.

The #:authentic option is a shorthand for #:property prop:authentic #t, which prevents instances of the structure type from being impersonated (see impersonate-struct), chaperoned (see chaperone-struct), or acquiring a non-flat contract (see struct/c). See prop:authentic for more information. If a supertype is specified, it must also have the prop:authentic property.

If name-id is supplied via #:extra-name and it is not id, then both name-id and id are bound to information about the structure type. Only one of #:extra-name and #:name can be provided within a struct form, and #:extra-name cannot be combined with #:omit-define-syntaxes.

Examples:

> ```racket
> > ( struct ghost ( color name ) #:prefab #:extra-name GHOST )
> > ( match ( ghost ' red ' blinky ) [ ( GHOST c n ) c ] )
> 'red
> ```

If constructor-id is supplied, then the transformer binding of name-id records constructor-id as the constructor binding; as a result, for example, struct-out includes constructor-id as an export. If constructor-id is supplied via #:extra-constructor-name and it is not id, applying object-name on the constructor produces the symbolic form of id rather than constructor-id. If constructor-id is supplied via #:constructor-name and it is not the same as name-id, then name-id does not serve as a constructor, and object-name on the constructor produces the symbolic form of constructor-id. Only one of #:extra-constructor-name and #:constructor-name can be provided within a struct form.

Examples:

> ```racket
> > ( struct color ( r g b ) #:constructor-name -c olor )
> > ( struct rectangle ( w h color ) #:extra-constructor-name rect )
> > ( rectangle 13 50 ( -c olor 192 157 235 ) )
> #<rectangle>
> > ( rect 50 37 ( -c olor 35 183 252 ) )
> #<rectangle>
> ```

If #:reflection-name symbol-expr is provided, then symbol-expr must produce a symbol that is used to identify the structure type in reflective operations such as struct-type-info. It corresponds to the first argument of make-struct-type. Structure printing uses the reflective name, as do the various procedures that are bound by struct.

Examples:

> ```racket
> > ( struct circle ( radius ) #:reflection-name ' <circle> )
> > ( circle 15 )
> #<<circle>>
> > ( circle-radius "bad" )
> <circle>-radius: contract violation
> expected: <circle>?
> given: "bad"
> ```

If #:methods gen:name-id method-defs is provided (potentially multiple times), then gen:name-id must be a transformer binding for the static information about a generic interface produced by define-generics. The method-defs define the methods of the gen:name-id interface. A define/generic form or auxiliary definitions and expressions may also appear in method-defs.

Examples:

> ```racket
> > ( struct constant-stream ( val ) #:methods gen:stream [ ( define ( stream-empty? stream ) #f ) ( define ( stream-first stream ) ( constant-stream-val stream ) ) ( define ( stream-rest stream ) stream ) ] )
> > ( stream-ref ( constant-stream ' forever ) 0 )
> 'forever
> > ( stream-ref ( constant-stream ' forever ) 50 )
> 'forever
> ```

If the #:omit-define-syntaxes option is supplied, then name-id (and id, if #:extra-name is specified) is not bound as a transformer. If the #:omit-define-values option is supplied, then none of the usual variables are bound, but id is bound. If both are supplied, then the struct form is equivalent to (begin).

Examples:

> ```racket
> > ( struct square ( side ) #:omit-define-syntaxes )
> > ( match ( square 5 ) ; fails to match because syntax is omitted [ ( struct square x ) x ] )
> eval:28:0: match: square does not refer to a structure
> definition
> at: square
> in: (struct square x)
> > ( struct ellipse ( width height ) #:omit-define-values )
> > ellipse-width
> ellipse-width: undefined;
> cannot reference an identifier before its definition
> in module: top-level
> ```

> > > Expressions supplied to #:auto-value are evaluated once and shared between every instance of the structure type. In particular, updates to a mutable #:auto-value affect all current and future instances.

If #:auto is supplied as a field-option, then the constructor procedure for the structure type does not accept an argument corresponding to the field. Instead, the structure type’s automatic value is used for the field, as specified by the #:auto-value option, or as defaults to #f when #:auto-value is not supplied. The field is mutable (e.g., through reflective operations), but a mutator procedure is bound only if #:mutable is specified.

If a field includes the #:auto option, then all fields after it must also include #:auto, otherwise a syntax error is reported. If any field-option or struct-option keyword is repeated, other than #:property, a syntax error is reported.

Examples:

> ```racket
> ( struct posn ( x y [ z #:auto #:mutable ] ) #:auto-value 0 #:transparent )
> > ( posn 1 2 )
> (posn 1 2 0)
> > ( posn? ( posn 1 2 ) )
> #t
> > ( posn-y ( posn 1 2 ) )
> 2
> > ( posn-z ( posn 1 2 ) )
> 0
> ( struct color-posn posn ( hue ) #:mutable ) ( define cp ( color-posn 1 2 "blue" ) )
> > ( color-posn-hue cp )
> "blue"
> > cp
> (color-posn 1 2 0 ...)
> > ( set-posn-z! cp 3 )
> ```

For serialization, see define-serializable-struct.

Changed in version 6.9.0.4 of package base: Added #:authentic.
Changed in version 8.0.0.7: Added #:sealed.
Changed in version 8.17.0.4: Added #:properties.

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

<p>syntax</p>

<p>(struct-field-index field-id)</p></blockquote></td></tr></tbody></table>

This form can only appear as an expression within a struct form; normally, it is used with #:property, especially for a property like prop:procedure. The result of a struct-field-index expression is an exact, non-negative integer that corresponds to the position within the structure declaration of the field named by field-id.

Examples:

> ```racket
> ( struct mood-procedure ( base rating ) #:property prop:procedure ( struct-field-index base ) ) ( define happy+ ( mood-procedure add1 10 ) )
> > ( happy+ 2 )
> 3
> > ( mood-procedure-rating happy+ )
> 10
> ```

> ```
syntax

(define-struct id-maybe-super (field ...)
> struct-option ... )

id-maybe-super   =   id
    |   (id super-id)```

Like [struct](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29), except that the syntax for supplying a super-id is different, and a constructor-id that has a make- prefix on id is implicitly supplied via #:extra-constructor-name if neither #:extra-constructor-name nor #:constructor-name is provided.

This form is provided for backwards compatibility; [struct](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29) is preferred.

Examples:

> ```racket
> ( define-struct posn ( x y [ z #:auto ] ) #:auto-value 0 #:transparent )
> > ( make-posn 1 2 )
> (posn 1 2 0)
> > ( posn? ( make-posn 1 2 ) )
> #t
> > ( posn-y ( make-posn 1 2 ) )
> 2
> ```

> ```
syntax

(struct/derived (id . rest-form)
> id ( field ... ) struct-option ... )
( struct/derived ( id . rest-form )
> id super-id ( field ... ) struct-option ... )
```


### 5.1 Defining Structure Types: [struct](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29)
The same as struct, but with an extra (id . rest-form) sub-form that is treated as the overall form for syntax-error reporting and otherwise ignored. The only constraint on the sub-form for error reporting is that it starts with id. The struct/derived form is intended for use by macros that expand to struct.
Examples:
> ```racket
> ( define-syntax ( fruit-struct stx ) ( syntax-case stx ( ) [ ( ds name . rest ) ( with-syntax ( [ orig stx ] ) #' ( struct/derived orig name ( seeds color ) . rest ) ) ] ) )
> > ( fruit-struct apple )
> > ( apple-seeds ( apple 12 "red" ) )
> 12
> > ( fruit-struct apple #:mutable )
> > ( set-apple-seeds! ( apple 12 "red" ) 8 )
> ; this next line will cause an error due to a bad keyword
> > ( fruit-struct apple #:bad-option )
> eval:54:0: fruit-struct: unrecognized struct-specification
> keyword
> at: #:bad-option
> in: (fruit-struct apple #:bad-option)
> ```
Added in version 7.5.0.16 of package base.
> ```
syntax
(define-struct/derived (id . rest-form)
> id-maybe-super ( field ... ) struct-option ... )```


### 5.1 Defining Structure Types: [struct](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29)
Like [struct/derived](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%2Fderived%29%29), except that the syntax for supplying a super-id is different, and a constructor-id that has a make- prefix on id is implicitly supplied via #:extra-constructor-name if neither #:extra-constructor-name nor #:constructor-name is provided. The [define-struct/derived](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-struct%2Fderived%29%29) form is intended for use by macros that expand to [define-struct](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-struct%29%29).
Examples:
> ```racket
> ( define-syntax ( define-xy-struct stx ) ( syntax-case stx ( ) [ ( ds name . rest ) ( with-syntax ( [ orig stx ] ) #' ( define-struct/derived orig name ( x y ) . rest ) ) ] ) )
> > ( define-xy-struct posn )
> > ( posn-x ( make-posn 1 2 ) )
> 1
> > ( define-xy-struct posn #:mutable )
> > ( set-posn-x! ( make-posn 1 2 ) 0 )
> ; this next line will cause an error due to a bad keyword
> > ( define-xy-struct posn #:bad-option )
> eval:60:0: define-xy-struct: unrecognized
> struct-specification keyword
> at: #:bad-option
> in: (define-xy-struct posn #:bad-option)
> ```
Changed in version 7.5.0.16 of package base: Moved main description to [struct/derived](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%2Fderived%29%29) and replaced with differences.
------------------------------------------------------------------------


# 5.2 Creating Structure Types


### 5.2 Creating Structure Types
> ```
(current-inspector)	→
(-> or/c struct-type? #f)
```
(inclusive)	→
(-> format "make-~a" name)```

Returns a field accessor that is equivalent to ([lambda](lambda.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._lambda%29%29) (s) (accessor-proc s field-pos)). The accessor-proc must be an [accessor](define-struct.html#%28tech._accessor%29) returned by [make-struct-type](#%28def._%28%28quote._~23~25kernel%29._make-struct-type%29%29).

The field/proc-name argument determines the name of the resulting procedure for error reporting and debugging purposes. If field/proc-name is a symbol and arg-contract-str is not #f, then field/proc-name is used as the procedure name. If field/proc-name is a symbol and arg-contract-str is #f, then field/proc-name is combined with the name of accessor-proc’s structure type to form the procedure name. If field/proc-name is #f, then 'accessor is used as the procedure name.

The arg-contract-str argument determines how the accessor procedure reports an error when it is applied to a value that is not an instance of the accessor-proc’s structure type. If it is a string or symbol, the text of the string or symbol is used as a contract for error reporting. Otherwise, contract text is synthesized from the name of accessor-proc’s structure type.

The realm argument is also used for error reporting. It specifies a [realm](exns.html#%28tech._realm%29) that an error-message adjuster may use to determine how to adjust an error message. The realm argument also determines the result of [procedure-realm](procedures.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._procedure-realm%29%29) for the accessor procedure.

For examples, see [make-struct-type](#%28def._%28%28quote._~23~25kernel%29._make-struct-type%29%29).

Changed in version 8.4.0.2 of package base: Added the arg-contract-str and realm arguments.

> ```
(parameter)	→
(-> or/c symbol? #f)
```


# 5.2 Creating Structure Types
(parameter)	→
(-> any/c . → . boolean?)```
Creates a new structure type property and returns three values:
-   a structure type property descriptor, for use with [make-struct-type](creatingmorestructs.html#%28def._%28%28quote._~23~25kernel%29._make-struct-type%29%29) and [struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29);
-   a property predicate procedure, which takes an arbitrary value and returns #t if the value is a descriptor or instance of a structure type that has a value for the property, #f otherwise;
-   a property accessor procedure, which returns the value associated with the structure type given its descriptor or one of its instances; if the structure type does not have a value for the property, or if any other kind of value is provided, the [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception is raised unless a second argument, failure-result, is supplied to the procedure. In that case, if failure-result is a procedure, it is called (through a tail call) with no arguments to produce the result of the property accessor procedure; otherwise, failure-result is itself returned as the result.


# 5.2 Creating Structure Types
If the optional guard is supplied as a procedure, it is called by [make-struct-type](creatingmorestructs.html#%28def._%28%28quote._~23~25kernel%29._make-struct-type%29%29) before attaching the property to a new structure type. The guard must accept two arguments: a value for the property supplied to [make-struct-type](creatingmorestructs.html#%28def._%28%28quote._~23~25kernel%29._make-struct-type%29%29), and a list containing information about the new structure type. The list contains the values that [struct-type-info](inspectors.html#%28def._%28%28quote._~23~25kernel%29._struct-type-info%29%29) would return for the new structure type if it skipped the current-inspector control checks.
The result of calling guard is associated with the property in the target structure type, instead of the value supplied to [make-struct-type](creatingmorestructs.html#%28def._%28%28quote._~23~25kernel%29._make-struct-type%29%29). To reject a property association (e.g., because the value supplied to [make-struct-type](creatingmorestructs.html#%28def._%28%28quote._~23~25kernel%29._make-struct-type%29%29) is inappropriate for the property), the guard can raise an exception. Such an exception prevents [make-struct-type](creatingmorestructs.html#%28def._%28%28quote._~23~25kernel%29._make-struct-type%29%29) from returning a structure type descriptor.


# 5.2 Creating Structure Types
If guard is 'can-impersonate, then the property’s accessor can be redirected through [impersonate-struct](chaperones.html#%28def._%28%28quote._~23~25kernel%29._impersonate-struct%29%29). This option is identical to supplying #t as the can-impersonate? argument and is provided for backwards compatibility.
The optional supers argument is a list of properties that are automatically associated with some structure type when the newly created property is associated to the structure type. Each property in supers is paired with a procedure that receives the value supplied for the new property (after it is processed by guard) and returns a value for the associated property (which is then sent to that property’s guard, of any).
The optional can-impersonate? argument determines if the structure type property can be redirected through [impersonate-struct](chaperones.html#%28def._%28%28quote._~23~25kernel%29._impersonate-struct%29%29). If the argument is #f, then redirection is not allowed. Otherwise, the property accessor may be redirected by a struct impersonator.
The optional accessor-name argument supplies a name (in the sense of [object-name](inspectors.html#%28def._%28%28quote._~23~25kernel%29._object-name%29%29)) to use for the returned accessor function. If accessor-name is #f, a name is created by adding -accessor to the end of name.


# 5.2 Creating Structure Types
The optional contract-str argument supplies a contract that is included in an error message with the returned accessor is applied to a value that is not an instance of the property (and where a failure-result argument is not supplied to the accessor). If contract-str is #f, a contract is created by adding ? to the end of name.
The optional realm argument supplies a [realm](exns.html#%28tech._realm%29) (in the sense of [procedure-realm](procedures.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._procedure-realm%29%29)) to associate with the returned accessor.
Examples:
> ```racket
> > ( define-values ( prop:p p? p-ref ) ( make-struct-type-property ' p ) )
> > ( define-values ( struct:a make-a a? a-ref a-set! ) ( make-struct-type ' a #f 2 1 ' uninitialized ( list ( cons prop:p 8 ) ) ) )
> > ( p? struct:a )
> #t
> > ( p? 13 )
> #f
> > ( define an-a ( make-a ' x ' y ) )
> > ( p? an-a )
> #t
> > ( p-ref an-a )
> 8
> > ( define-values ( struct:b make-b b? b-ref b-set! ) ( make-struct-type ' b #f 0 0 #f ) )
> > ( p? struct:b )
> #f
> > ( define-values ( prop:q q? q-ref ) ( make-struct-type-property ' q ( lambda ( v si ) ( add1 v ) ) ( list ( cons prop:p sqrt ) ) ) )
> > ( define-values ( struct:c make-c c? c-ref c-set! ) ( make-struct-type ' c #f 0 0 ' uninit ( list ( cons prop:q 8 ) ) ) )
> > ( q-ref struct:c )
> 9
> > ( p-ref struct:c )
> 3
> ```


# 5.2 Creating Structure Types
Changed in version 7.0 of package base: The [CS](implementations.html#%28tech._c%29) implementation of Racket skips the inspector check for exposing an ancestor structure type, if any, in information provided to a guard procedure.
Changed in version 8.4.0.2: Added the accessor-name, contract-str, and realm arguments.
Changed in version 8.5.0.2: Changed the [BC](implementations.html#%28tech._bc%29) implementation of Racket to skip the inspector check, the same as the [CS](implementations.html#%28tech._c%29) implementation, for ancestor information provided to a guard procedure.
> ```
(struct-type-property? v)  →  boolean?
  v : any/c
```
Returns #t if v is a structure type property descriptor value, #f otherwise.

> ```
(struct-type-property-accessor-procedure? v)   →   boolean?
  v : any/c```

Returns #t if v is an accessor procedure produced by [make-struct-type-property](#%28def._%28%28quote._~23~25kernel%29._make-struct-type-property%29%29), #f otherwise.

> ```
(struct-type-property-predicate-procedure? v
   [ prop)    →    boolean?

  v : any/c
  prop : (or/c struct-type-property? #f) = #f
```
Returns #t if v is a predicate procedure produced by make-struct-type-property and either prop is #f or it was produced by the same call to make-struct-type-property, #f otherwise.
Added in version 7.5.0.11 of package base.
------------------------------------------------------------------------


# 5.4 Generic Interfaces
8.18.0.13


### 5.4 Generic Interfaces
|  |  |
|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
|  (require [racket/generic]()) |  package: base |
A generic interface allows per-type methods to be associated with generic functions. Generic functions are defined using a define-generics form. Method implementations for a structure type are defined using the #:methods keyword (see Defining Structure Types: struct).
> ```
syntax
(define-generics id
> generics-opt ...
> [ method-id . kw-formals* ] ...
> generics-opt ... )
generics-opt   =   #:defaults ([default-pred? default-impl ...] ...)
    |   #:fast-defaults ([fast-pred? fast-impl ...] ...)
    |   #:fallbacks [fallback-impl ...]
    |   #:defined-predicate defined-pred-id
    |   #:defined-table defined-table-id
    |   #:derive-property prop-expr prop-value-expr
    |   #:requires [required-method-id ...]
kw-formals*   =   (arg* ...)
    |   (arg* ...+ . rest-id)
    |   rest-id
arg*   =   arg-id
    |   [arg-id]
    |   keyword arg-id
    |   keyword [arg-id]```
Defines the following names, plus any specified by keyword options.
-   gen:id as a transformer binding for the static information about a new generic interface;
-   id? as a predicate identifying instances of structure types that implement this generic group; and


# 5.4 Generic Interfaces
-   each method-id as a generic method that calls the corresponding method on values where id? is true. Each method-id’s kw-formals\* must include a required by-position argument that is [free-identifier=?](stxcmp.html#%28def._%28%28quote._~23~25kernel%29._free-identifier~3d~3f%29%29) to id. That argument is used in the generic definition to locate the specialization.
-   id/c as a contract combinator that recognizes instances of structure types which implement the gen:id generic interface. The combinator takes pairs of method-ids and contracts. The contracts will be applied to each of the corresponding method implementations. The id/c combinator is intended to be used to contract the range of a constructor procedure for a struct type that implements the generic interface.
The #:defaults option may be provided at most once. When it is provided, each generic function uses default-pred?s to dispatch to the given default method implementations, default-impls, if dispatching to the generic method table fails. The syntax of the default-impls is the same as the methods provided for the #:methods keyword for [struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29).
The #:fast-defaults option may be provided at most once. It works the same as #:defaults, except the fast-pred?s are checked before dispatching to the generic method table. This option is intended to provide a fast path for dispatching to built-in datatypes, such as lists and vectors, that do not overlap with structures implementing gen:id.


# 5.4 Generic Interfaces
The #:fallbacks option may be provided at most once. When it is provided, the fallback-impls define fallback method implementations that are used for any instance of the generic interface that does not supply a specific implementation. The syntax of the fallback-impls is the same as the methods provided for the #:methods keyword for [struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29).
The #:defined-predicate option may be provided at most once. When it is provided, defined-pred-id is defined as a procedure that reports whether a specific instance of the generic interface implements a given set of methods. Specifically, (defined-pred-id v 'name [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)) produces #t if v has implementations for each method name, not counting #:fallbacks implementations, and produces #f otherwise. This procedure is intended for use by higher-level APIs to adapt their behavior depending on method availability.
The #:defined-table option may be provided at most once. When it is provided, defined-table-id is defined as a procedure that takes an instance of the generic interface and returns an immutable [hash table](hashtables.html#%28tech._hash._table%29) that maps symbols corresponding to method names to booleans representing whether or not that method is implemented by the instance. This option is deprecated; use #:defined-predicate instead.