text
stringlengths 12
127k
|
|---|
# 16.2 General Macro Transformers
| |
|----------------------------------------------------------------------------------------------------------------|
| [16.2.1 Syntax Objects](stx-obj.html) |
| [16.2.2 Macro Transformer Procedures](macro-transformers.html) |
| [16.2.3 Mixing Patterns and Expressions: syntax-case](syntax-case.html) |
| [16.2.4 with-syntax and generate-temporaries](with-syntax.html) |
| [16.2.5 Compile and Run-Time Phases](stx-phases.html) |
| [16.2.6 General Phase Levels](phases.html) |
| [16.2.6.1 Phases and Bindings](phases.html#%28part._.Phases_and_.Bindings%29) |
| [16.2.6.2 Phases and Modules](phases.html#%28part._.Phases_and_.Modules%29) |
| [16.2.7 Tainted Syntax](stx-certs.html) |
------------------------------------------------------------------------
|
# 16.2.1 Syntax Objects
|
#### 16.2.1 Syntax Objects
The input and output of a macro transformer (i.e., source and replacement forms) are represented as syntax objects. A syntax object contains symbols, lists, and constant values (such as numbers) that essentially correspond to the [quote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._quote%2529%2529&version=8.18.0.13)d form of the expression. For example, a representation of the expression ([+](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=8.18.0.13) 1 2) contains the symbol '+ and the numbers 1 and 2, all in a list. In addition to this quoted content, a syntax object associates source-location and lexical-binding information with each part of the form. The source-location information is used when reporting syntax errors (for example), and the lexical-binding information allows the macro system to maintain lexical scope. To accommodate this extra information, the representation of the expression ([+](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=8.18.0.13) 1 2) is not merely '(+ 1 2), but a packaging of '(+ 1 2) into a syntax object.
|
# 16.2.1 Syntax Objects
To create a literal syntax object, use the [syntax](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._syntax%2529%2529&version=8.18.0.13) form:
> ```racket
> > ( syntax ( + 1 2 ) )
> #<syntax:eval:1:0 (+ 1 2)>
> ```
In the same way that ' abbreviates [quote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._quote%2529%2529&version=8.18.0.13), #' abbreviates [syntax](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._syntax%2529%2529&version=8.18.0.13):
> ```racket
> > #' ( + 1 2 )
> #<syntax:eval:1:0 (+ 1 2)>
> ```
|
# 16.2.1 Syntax Objects
A syntax object that contains just a symbol is an identifier syntax object. Racket provides some additional operations specific to identifier syntax objects, including the [identifier?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fstx..rkt%2529._identifier%7E3f%2529%2529&version=8.18.0.13) operation to detect identifiers. Most notably, [free-identifier=?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxcmp.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._free-identifier%7E3d%7E3f%2529%2529&version=8.18.0.13) determines whether two identifiers refer to the same binding:
> ```racket
> > ( identifier? #' car )
> #t
> > ( identifier? #' ( + 1 2 ) )
> #f
> > ( free-identifier=? #' car #' cdr )
> #f
> > ( free-identifier=? #' car #' car )
> #t
> > ( require ( only-in racket/base [ car also-car ] ) )
> > ( free-identifier=? #' car #' also-car )
> #t
> ```
To see the lists, symbols, numbers, etc. within a syntax object, use [syntax->datum](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._syntax-%7E3edatum%2529%2529&version=8.18.0.13):
> ```racket
> > ( syntax->datum #' ( + 1 2 ) )
> '(+ 1 2)
> ```
|
# 16.2.1 Syntax Objects
The [syntax-e](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._syntax-e%2529%2529&version=8.18.0.13) function is similar to [syntax->datum](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._syntax-%7E3edatum%2529%2529&version=8.18.0.13), but it unwraps a single layer of source-location and lexical-context information, leaving sub-forms that have their own information wrapped as syntax objects:
> ```racket
> > ( syntax-e #' ( + 1 2 ) )
> '(#<syntax:eval:1:0 +> #<syntax:eval:1:0 1> #<syntax:eval:1:0 2>)
> ```
The [syntax-e](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._syntax-e%2529%2529&version=8.18.0.13) function always leaves syntax-object wrappers around sub-forms that are represented via symbols, numbers, and other literal values. The only time it unwraps extra sub-forms is when unwrapping a pair, in which case the [cdr](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cdr%2529%2529&version=8.18.0.13) of the pair may be recursively unwrapped, depending on how the syntax object was constructed.
|
# 16.2.1 Syntax Objects
The opposite of [syntax->datum](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._syntax-%7E3edatum%2529%2529&version=8.18.0.13) is, of course, [datum->syntax](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._datum-%7E3esyntax%2529%2529&version=8.18.0.13). In addition to a datum like '(+ 1 2), [datum->syntax](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._datum-%7E3esyntax%2529%2529&version=8.18.0.13) needs an existing syntax object to donate its lexical context, and optionally another syntax object to donate its source location:
> ```racket
> > ( datum->syntax #' lex ' ( + 1 2 ) #' srcloc )
> #<syntax:eval:1:0 (+ 1 2)>
> ```
In the above example, the lexical context of #'lex is used for the new syntax object, while the source location of #'[srcloc](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=exns.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._srcloc%2529%2529&version=8.18.0.13) is used.
|
# 16.2.1 Syntax Objects
When the second (i.e., the “datum”) argument to [datum->syntax](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._datum-%7E3esyntax%2529%2529&version=8.18.0.13) includes syntax objects, those syntax objects are preserved intact in the result. That is, deconstructing the result with [syntax-e](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._syntax-e%2529%2529&version=8.18.0.13) eventually produces the syntax objects that were given to [datum->syntax](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._datum-%7E3esyntax%2529%2529&version=8.18.0.13).
------------------------------------------------------------------------
|
# 16.2.2 Macro Transformer Procedures
|
#### 16.2.2 Macro Transformer Procedures
Any procedure of one argument can be a [macro transformer](proc-macros.html#%28tech._macro._transformer%29). As it turns out, the [syntax-rules](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._syntax-rules%2529%2529&version=8.18.0.13) form is a macro that expands to a procedure form. For example, if you evaluate a [syntax-rules](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._syntax-rules%2529%2529&version=8.18.0.13) form directly (instead of placing on the right-hand of a [define-syntax](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define-syntax%2529%2529&version=8.18.0.13) form), the result is a procedure:
> ```racket
> > ( syntax-rules ( ) [ ( nothing ) something ] )
> #<procedure>
> ```
|
# 16.2.2 Macro Transformer Procedures
Instead of using [syntax-rules](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._syntax-rules%2529%2529&version=8.18.0.13), you can write your own macro transformer procedure directly using [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13). The argument to the procedure is a [syntax object](stx-obj.html#%28tech._syntax._object%29) that represents the source form, and the result of the procedure must be a [syntax object](stx-obj.html#%28tech._syntax._object%29) that represents the replacement form:
> ```racket
> > ( define-syntax self-as-string ( lambda ( stx ) ( datum->syntax stx ( format "~s" ( syntax->datum stx ) ) ) ) )
> > ( self-as-string ( + 1 2 ) )
> "(self-as-string (+ 1 2))"
> ```
|
# 16.2.2 Macro Transformer Procedures
The source form passed to a macro transformer represents an expression in which its identifier is used in an application position (i.e., after a parenthesis that starts an expression), or it represents the identifier by itself if it is used as an expression position and not in an application position.The procedure produced by [syntax-rules](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._syntax-rules%2529%2529&version=8.18.0.13) raises a syntax error if its argument corresponds to a use of the identifier by itself, which is why [syntax-rules](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._syntax-rules%2529%2529&version=8.18.0.13) does not implement an [identifier macro](pattern-macros.html#%28tech._identifier._macro%29).
> ```racket
> > ( self-as-string ( + 1 2 ) )
> "(self-as-string (+ 1 2))"
> > self-as-string
> "self-as-string"
> ```
|
# 16.2.2 Macro Transformer Procedures
The [define-syntax](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define-syntax%2529%2529&version=8.18.0.13) form supports the same shortcut syntax for functions as [define](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.18.0.13), so that the following self-as-string definition is equivalent to the one that uses [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) explicitly:
> ```racket
> > ( define-syntax ( self-as-string stx ) ( datum->syntax stx ( format "~s" ( syntax->datum stx ) ) ) )
> > ( self-as-string ( + 1 2 ) )
> "(self-as-string (+ 1 2))"
> ```
------------------------------------------------------------------------
|
# 16.2.3 Mixing Patterns and Expressions: syntax-case
|
#### 16.2.3 Mixing Patterns and Expressions: [syntax-case](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._syntax-case%2529%2529&version=8.18.0.13)
The procedure generated by [syntax-rules](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._syntax-rules%2529%2529&version=8.18.0.13) internally uses [syntax-e](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._syntax-e%2529%2529&version=8.18.0.13) to deconstruct the given syntax object, and it uses [datum->syntax](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stxops.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._datum-%7E3esyntax%2529%2529&version=8.18.0.13) to construct the result. The [syntax-rules](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._syntax-rules%2529%2529&version=8.18.0.13) form doesn’t provide a way to escape from pattern-matching and template-construction mode into an arbitrary Racket expression.
|
# 16.2.3 Mixing Patterns and Expressions: syntax-case
The [syntax-case](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._syntax-case%2529%2529&version=8.18.0.13) form lets you mix pattern matching, template construction, and arbitrary expressions:
> > ```
(syntax-case stx-expr (literal-id ...)
> > [ pattern expr ]
> > ... )
```
(shifted) →
(-> syntax-case #' (+ 1 2) () [ (op n1 n2) #' (- n1 n2) ])```
(Simple) →
(-> Components)
```
(Parsing) →
(-> - 4 (+ 1 1))```
objects:
defined:
evaluate: ( begin ( define x ( vector 10 20 ) )
( define y x )
( vector-set! x 0 11 )
( vector-ref y 0 ) )
→ objects: (define <o1> (vector 10 20))
defined:
evaluate: ( begin ( define x <o1> )
( define y x )
( vector-set! x 0 11 )
( vector-ref y 0 ) )
→ objects: (define <o1> (vector 10 20))
defined: (define x <o1>)
evaluate: ( begin ( void )
( define y x )
( vector-set! x 0 11 )
( vector-ref y 0 ) )
→ objects: (define <o1> (vector 10 20))
defined: (define x <o1>)
evaluate: ( begin ( define y x )
( vector-set! x 0 11 )
( vector-ref y 0 ) )
→ objects: (define <o1> (vector 10 20))
defined: (define x <o1>)
evaluate: ( begin ( define y <o1> )
( vector-set! x 0 11 )
( vector-ref y 0 ) )
→ objects: (define <o1> (vector 10 20))
defined: ( define x <o1> )
( define y <o1> )
evaluate: ( begin ( void )
( vector-set! x 0 11 )
( vector-ref y 0 ) )
→ objects: (define <o1> (vector 10 20))
defined: ( define x <o1> )
( define y <o1> )
evaluate: ( begin ( vector-set! x 0 11 )
( vector-ref y 0 ) )
→ objects: (define <o1> (vector 10 20))
defined: ( define x <o1> )
( define y <o1> )
evaluate: ( begin ( vector-set! <o1> 0 11 )
( vector-ref y 0 ) )
→ objects: (define <o1> (vector 11 20))
defined: ( define x <o1> )
( define y <o1> )
evaluate: ( begin ( void )
( vector-ref y 0 ) )
→ objects: (define <o1> (vector 11 20))
defined: ( define x <o1> )
( define y <o1> )
evaluate: (vector-ref y 0)
→ objects: (define <o1> (vector 11 20))
defined: ( define x <o1> )
( define y <o1> )
evaluate: (vector-ref <o1> 0)
→ objects: (define <o1> (vector 11 20))
defined: ( define x <o1> )
( define y <o1> )
evaluate: 11
|
# 16.2.3 Mixing Patterns and Expressions: syntax-case
```
The distinction between a [top-level variable](#%28tech._top._level._variable%29) and an object reference is crucial. A [top-level variable](#%28tech._top._level._variable%29) is not a [value](#%28tech._value%29), so it must be evaluated. Each time a [variable](#%28tech._variable%29) expression is evaluated, the value of the variable is extracted from the current set of definitions. An object reference, in contrast, is a value and therefore needs no further evaluation. The evaluation steps above use angle-bracketed \<o1> for an object reference to distinguish it from a [variable](#%28tech._variable%29) name.
An object reference can never appear directly in a text-based source program. A program representation created with [datum->syntax](stxops.html#%28def._%28%28quote._~23~25kernel%29._datum-~3esyntax%29%29), however, can embed direct references to existing [objects](#%28tech._object%29).
#### 1.1.6 Garbage Collection
> > > <img src="magnify.png" width="24" height="24" alt="+" />See [Memory Management](memory.html) for functions related to garbage collection.
In the program state
```
objects: ( define <o1> ( vector 10 20 ) )
( define <o2> ( vector 0 ) )
defined: (define x <o1>)
evaluate: (+ 1 x)
|
# 16.2.3 Mixing Patterns and Expressions: syntax-case
```
(x) →
(-> define \<p1> (lambda (x) (+ x 10)))```
objects: (define <p1> (lambda (x) (+ x 10)))
defined: (define f <p1>)
evaluate: (<p1> 7)
→ objects: (define <p1> (lambda (x) (+ x 10)))
defined: ( define f <p1> )
( define xloc 7 )
evaluate: (+ xloc 10)
→ objects: (define <p1> (lambda (x) (+ x 10)))
defined: ( define f <p1> )
( define xloc 7 )
evaluate: (+ 7 10)
→ objects: (define <p1> (lambda (x) (+ x 10)))
defined: ( define f <p1> )
( define xloc 7 )
evaluate: 17
```
A [location](#%28tech._location%29) is the same as a [top-level variable](#%28tech._top._level._variable%29), but when a [location](#%28tech._location%29) is generated, it (conceptually) uses a name that has not been used before and that cannot be generated again or accessed directly.
Generating a [location](#%28tech._location%29) in this way means that [set!](set_.html#%28form._%28%28quote._~23~25kernel%29._set%21%29%29) evaluates for [local variables](#%28tech._local._variable%29), including argument variables, in the same way as for [top-level variables](#%28tech._top._level._variable%29), because the [local variable](#%28tech._local._variable%29) is always replaced with a [location](#%28tech._location%29) by the time the [set!](set_.html#%28form._%28%28quote._~23~25kernel%29._set%21%29%29) form is evaluated:
|
# 16.2.3 Mixing Patterns and Expressions: syntax-case
```
objects: (define <p1> (lambda (x) (begin (set! x 3) x)))
defined: (define f <p1>)
evaluate: (f 7)
→ objects: (define <p1> (lambda (x) (begin (set! x 3) x)))
defined: (define f <p1>)
evaluate: (<p1> 7)
→ objects: (define <p1> (lambda (x) (begin (set! x 3) x)))
defined: ( define f <p1> )
( define xloc 7 )
evaluate: (begin (set! xloc 3) xloc)
→ objects: (define <p1> (lambda (x) (begin (set! x 3) x)))
defined: ( define f <p1> )
( define xloc 3 )
evaluate: (begin (void) xloc)
→ objects: (define <p1> (lambda (x) (begin (set! x 3) x)))
defined: ( define f <p1> )
( define xloc 3 )
evaluate: xloc
→ objects: (define <p1> (lambda (x) (begin (set! x 3) x)))
defined: ( define f <p1> )
( define xloc 3 )
evaluate: 3
```
(x) →
(-> define x ' orig)```
(parameter) →
(-> #%plain-app variable-reference-from-unsafe?
(#%variable-reference))
```
(decimal) →
(-> see Weak Boxes)```
Examples:
(#1=100 #1# #1#) reads equal to (list 100 100 100)
#0=(1 . #0#) reads equal to ( let* ( [ ph ( make-placeholder #f ) ]
[ v ( cons 1 ph ) ] )
(placeholder-set! ph v)
( make-reader-graph v ) )
```
(unquoted) →
(-> ASCII 32)```
indicates that the result of each parameter-expr must be a value v for which ([parameter?](parameters.html#%28def._%28%28quote._~23~25kernel%29._parameter~3f%29%29) v) returns true.
|
### 2.3 Notation for Function Documentation
Procedures and other values are described using a notation based on [contract](contracts.html#%28tech._contract%29)s. In essence, these contracts describe the interfaces of the documented library using Racket predicates and expressions.
For example, the following is the header of the definition of a typical procedure:
> > ```
(char → integer char) → exact-integer?
char : char?
```
The function being defined, char → integer, is typeset as if it were being applied. The metavariables that come after the function name stand in for arguments. The white text in the corner identifies the kind of value that is being documented.
Each metavariable is described with a contract. In the preceding example, the metavariable char has the contract char?. This contract specifies that any argument char that answers true to the char? predicate is valid. The documented function may or may not actually check this property, but the contract signals the intent of the implementer.
The contract on the right of the arrow, exact-integer? in this case, specifies the expected result that is produced by the function.
Contract specifications can be more expressive than just names of predicates. Consider the following header for argmax:
> > ```
(argmax proc lst) → any
proc : ( → any/c real?)
lst : (and/c pair? list?)```
The contract ([->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29) [any/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29) [real?](number-types.html#%28def._%28%28quote._~23~25kernel%29._real~3f%29%29)) denotes a function contract specifying that proc’s argument can be any single value and the result should be a real number. The contract ([and/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._and%2Fc%29%29) [pair?](pairs.html#%28def._%28%28quote._~23~25kernel%29._pair~3f%29%29) [list?](pairs.html#%28def._%28%28quote._~23~25kernel%29._list~3f%29%29)) for lst specifies that lst should pass both [pair?](pairs.html#%28def._%28%28quote._~23~25kernel%29._pair~3f%29%29) and [list?](pairs.html#%28def._%28%28quote._~23~25kernel%29._list~3f%29%29) (i.e., that it is a non-empty list).
Both [->](function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%29%29) and [and/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._and%2Fc%29%29) are examples of [contract combinator](contracts.html#%28tech._contract._combinator%29)s. Contract combinators such as [or/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._or%2Fc%29%29), [cons/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._cons%2Fc%29%29), [listof](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._listof%29%29), and others are used throughout the documentation. Clicking on the hyperlinked combinator name will provide more information on its meaning.
A Racket function may be documented as having one or more optional arguments. The [read](Reading.html#%28def._%28%28quote._~23~25kernel%29._read%29%29) function is an example of such a function:
> > ```
(current-input-port) →
(-> current-input-port)
```
|
### 2.3 Notation for Function Documentation
(current-input-port) →
(-> any/c any/c . → . any/c)```
The brackets around the extract-key and cache-keys? arguments indicate that they are optional as before. The contract section of the header shows the default values that are provided for these keyword arguments.
|
### 2.4 Notation for Structure Type Documentation
A [structure type](structures.html#%28tech._structure._type%29) is also documented using contract notation:
> > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>struct</p>
<p>([struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29) color (red green blue alpha))</p></blockquote></td></tr><tr><td> red : ([and/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._and%2Fc%29%29) [natural-number/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._natural-number%2Fc%29%29) ([<=/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._~3c~3d%2Fc%29%29) 255))</td></tr><tr><td> green : ([and/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._and%2Fc%29%29) [natural-number/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._natural-number%2Fc%29%29) ([<=/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._~3c~3d%2Fc%29%29) 255))</td></tr><tr><td> blue : ([and/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._and%2Fc%29%29) [natural-number/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._natural-number%2Fc%29%29) ([<=/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._~3c~3d%2Fc%29%29) 255))</td></tr><tr><td> alpha : ([and/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._and%2Fc%29%29) [natural-number/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._natural-number%2Fc%29%29) ([<=/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._~3c~3d%2Fc%29%29) 255))</td></tr></tbody></table>
|
### 2.4 Notation for Structure Type Documentation
The structure type is typeset as it were declared in the source code of a program using the [struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29) form. Each field of the structure is documented with a corresponding contract that specifies the values that are accepted for that field.
In the example above, the structure type color has four fields: red, green, blue, and alpha. The constructor for the structure type accepts field values that satisfy ([and/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._and%2Fc%29%29) [natural-number/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._natural-number%2Fc%29%29) ([<=/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._~3c~3d%2Fc%29%29) 255)), i.e., non-negative exact integers up to 255.
Additional keywords may appear after the field names in the documentation for a structure type:
> > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>struct</p>
|
### 2.4 Notation for Structure Type Documentation
<table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td>([struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29)</td><td> </td><td>data-source (connector args extensions)</td></tr></tbody></table></blockquote></td></tr><tr><td> #:mutable)</td></tr><tr><td> connector : ([or/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._or%2Fc%29%29) 'postgresql 'mysql 'sqlite3 'odbc)</td></tr><tr><td> args : [list?](pairs.html#%28def._%28%28quote._~23~25kernel%29._list~3f%29%29)</td></tr><tr><td> extensions : ([listof](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._listof%29%29) ([list/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._list%2Fc%29%29) [symbol?](symbols.html#%28def._%28%28quote._~23~25kernel%29._symbol~3f%29%29) [any/c](data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fprivate%2Fmisc..rkt%29._any%2Fc%29%29)))</td></tr></tbody></table>
Here, the #:mutable keyword indicates that the fields of instances of the data-source structure type can be mutated with their respective setter functions.
|
### 2.5 Notation for Parameter Documentation
A [parameter](eval-model.html#%28tech._parameter%29) is documented the same way as a function:
> > ```
(current-command-line-arguments) →
(-> vectorof string?)
(current-command-line-arguments argv) → void?
argv :
(-> vectorof string?)
```
(current-module-declare-name) →
(-> module duck racket/base (provide num-eggs quack) (define num-eggs 2) (define (quack n) (unless (zero? n) (printf "quack\n") (quack (sub1 n)))))```
>
>
>
>
>
> Specifies a library available via the PLaneT server.
>
>
>
> The first form is a shorthand for the last one, where the id’s character sequence must match the following ‹spec› grammar:
>
> | | | | |
> |-----|-----------|--------|-------------------------------------------------|
> | | ‹spec› | ::= | ‹owner› / ‹pkg› ‹lib› |
> | | ‹owner› | ::= | ‹elem› |
> | | ‹pkg› | ::= | ‹elem› \| ‹elem› : ‹version› |
> | | ‹version› | ::= | ‹int› \| ‹int› : ‹minor› |
> | | ‹minor› | ::= | ‹int› \| \<= ‹int› \|>= ‹int› \| = ‹int› |
> | | | \| | ‹int› - ‹int› |
> | | ‹lib› | ::= | ‹empty› \| / ‹path› |
> | | ‹path› | ::= | ‹elem› \| ‹elem› / ‹path› |
>
> and where an ‹elem› is a non-empty sequence of characters that are ASCII letters, ASCII digits, -, +, \_, or % followed by lowercase hexadecimal digits (that do not encode one of the other allowed characters), and an ‹int› is a non-empty sequence of ASCII digits. As this shorthand is expended, a ".plt" extension is added to ‹pkg›, and a ".rkt" extension is added to ‹path›; if no ‹path› is included, "main.rkt" is used in the expansion.
>
> A ([planet](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._planet%29%29) string) form is like a ([planet](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._planet%29%29) id) form with the identifier converted to a string, except that the string can optionally end with a file extension (i.e., a .) for a ‹path›. A ".ss" file extension is converted to ".rkt".
>
> In the more general last form of a [planet](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._planet%29%29) module path, the rel-strings are similar to the [lib](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._lib%29%29) form, except that the (user-string pkg-string vers) names a PLaneT-based package instead of a [collection](collects.html#%28tech._collection%29). A version specification can include an optional major and minor version, where the minor version can be a specific number or a constraint: (nat nat) specifies an inclusive range, (= nat) specifies an exact match, (+ nat) specifies a minimum version and is equivalent to just nat, and (- nat) specifies a maximum version. The =, +, and - identifiers in a minor-version constraint are recognized symbolically.
>
>
>
> Examples:
>
>
>
>
>
> > ```racket
> > ; "main.rkt" in package "farm" by "mcdonald" :
> > > ( require ( planet mcdonald/farm ) )
> > ; "main.rkt" in version >= 2.0 of "farm" by "mcdonald" :
> > > ( require ( planet mcdonald/farm:2 ) )
> > ; "main.rkt" in version >= 2.5 of "farm" by "mcdonald" :
> > > ( require ( planet mcdonald/farm:2:5 ) )
> > ; "duck.rkt" in version >= 2.5 of "farm" by "mcdonald" :
> > > ( require ( planet mcdonald/farm:2:5/duck ) )
> > ```
>
>
>
>
> > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([submod](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._submod%29%29) root-module-path submod-path-element ...)</p></blockquote></td></tr><tr><td>([submod](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._submod%29%29) "." submod-path-element ...)</td></tr><tr><td>([submod](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._submod%29%29) ".." submod-path-element ...)</td></tr></tbody></table>
>
>
>
>
>
> Identifies a [submodule](eval-model.html#%28tech._submodule%29) within the module specified by root-module-path or relative to the current module in the case of ([submod](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._submod%29%29) "." ....), where ([submod](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._submod%29%29) ".." submod-path-element [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)) is equivalent to ([submod](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._submod%29%29) "." ".." submod-path-element [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)). Submodules have symbolic names, and a sequence of identifiers as submod-path-elements determine a path of successively nested submodules with the given names. A ".." as a submod-path-element names the enclosing module of a submodule, and it’s intended for use in ([submod](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._submod%29%29) "." ....) and ([submod](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._submod%29%29) ".." ....) forms.
>
>
As [require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) prepares to handle a sequence of require-specs, it logs a “prefetch” message to the [current logger](logging.html#%28tech._current._logger%29) at the 'info level, using the name 'module-prefetch, and including message data that is a list of two elements: a list of [module paths](Module_Names_and_Loading.html#%28tech._module._path%29) that appear to be imported, and a directory path to use for relative module paths. The logged list of module paths may be incomplete, but a compilation manager can use approximate prefetch information to start on compilations in parallel.
Changed in version 6.0.1.10 of package base: Added prefetch logging.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([local-require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._local-require%29%29) require-spec ...)</p></blockquote></td></tr></tbody></table>
Like [require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29), but for use in a [internal-definition context](syntax-model.html#%28tech._internal._definition._context%29) to import just into the local context. Only bindings from [phase level](syntax-model.html#%28tech._phase._level%29) 0 are imported.
Examples:
> ```racket
> > ( let ( ) ( local-require racket/control ) fcontrol )
> #<procedure:fcontrol>
> > fcontrol
> fcontrol: undefined;
> cannot reference an identifier before its definition
> in module: top-level
> ```
> > > <img src="finger.png" width="24" height="24" alt="+" />[Exports: provide](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=module-provide.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 [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29) provide-spec ...)</p></blockquote></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td style="text-align: right;" data-valign="baseline">provide-spec</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([all-defined-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._all-defined-out%29%29))</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([all-from-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._all-from-out%29%29) module-path ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([rename-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._rename-out%29%29) [orig-id export-id] ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([except-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._except-out%29%29) provide-spec provide-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([prefix-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._prefix-out%29%29) prefix-id provide-spec)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([struct-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct-out%29%29) id)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([combine-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._combine-out%29%29) provide-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([protect-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._protect-out%29%29) provide-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([for-meta](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-meta%29%29) phase-level provide-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-syntax%29%29) provide-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([for-template](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-template%29%29) provide-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([for-label](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-label%29%29) provide-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">([for-space](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-space%29%29) space provide-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">derived-provide-spec</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">phase-level</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">exact-integer</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#f</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">space</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#f</td></tr></tbody></table></td></tr></tbody></table>
Declares exports from a module. A [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29) form must appear in a [module context](syntax-model.html#%28tech._module._context%29) or a [module-begin context](syntax-model.html#%28tech._module._begin._context%29).
A provide-spec indicates one or more bindings to provide. For each exported binding, the external name is a symbol that can be different from the symbolic form of the identifier that is bound within the module. Also, each export is drawn from a particular [phase level](syntax-model.html#%28tech._phase._level%29) and exported at the same [phase level](syntax-model.html#%28tech._phase._level%29); by default, the relevant phase level is the number of [begin-for-syntax](begin.html#%28form._%28%28quote._~23~25kernel%29._begin-for-syntax%29%29) forms that enclose the [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29) form. Finally, each export is drawn from a [binding space](syntax-model.html#%28tech._binding._space%29) and exported at the same [binding space](syntax-model.html#%28tech._binding._space%29).
The syntax of provide-spec can be extended by bindings to [provide transformers](stxtrans.html#%28tech._provide._transformer%29) or [provide pre-transformers](stxtrans.html#%28tech._provide._pre._transformer%29), such as via [define-provide-syntax](define.html#%28form._%28%28lib._racket%2Fprovide-syntax..rkt%29._define-provide-syntax%29%29), but the pre-defined forms are as follows.
> > | |
> > |-----|
> > | id |
>
> Exports id, which must be [bound](syntax-model.html#%28tech._bound%29) within the module (i.e., either defined or imported) at the relevant [phase level](syntax-model.html#%28tech._phase._level%29) and [binding space](syntax-model.html#%28tech._binding._space%29). The symbolic form of id is used as the external name, and the symbolic form of the defined or imported identifier must match (otherwise, the external name could be ambiguous).
>
>
>
> Examples:
>
>
>
>
>
> > ```racket
> > > ( module nest racket ( provide num-eggs ) ( define num-eggs 2 ) )
> > > ( require ' nest )
> > > num-eggs
> > 2
> > ```
>
>
>
> If id has a transformer binding to a [rename transformer](syntax-model.html#%28tech._rename._transformer%29), then the transformer affects the exported binding. See [make-rename-transformer](stxtrans.html#%28def._%28%28quote._~23~25kernel%29._make-rename-transformer%29%29) for more information.
>
>
> > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([all-defined-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._all-defined-out%29%29))</p></blockquote></td></tr></tbody></table>
>
>
>
>
>
> Exports all identifiers that are defined at the relevant [phase level](syntax-model.html#%28tech._phase._level%29) within the exporting module, and that have the same lexical context as the ([all-defined-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._all-defined-out%29%29)) form, excluding bindings to [rename transformers](syntax-model.html#%28tech._rename._transformer%29) where the target identifier has the 'not-provide-all-defined [syntax property](stxprops.html#%28tech._syntax._property%29). The external name for each identifier is the symbolic form of the identifier. Only identifiers accessible from the lexical context of the ([all-defined-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._all-defined-out%29%29)) form are included; that is, macro-introduced imports are not re-exported, unless the ([all-defined-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._all-defined-out%29%29)) form was introduced at the same time.
>
>
>
>
>
> Examples:
>
>
>
>
>
> > ```racket
> > > ( module nest racket ( provide ( all-defined-out ) ) ( define num-eggs 2 ) )
> > > ( require ' nest )
> > > num-eggs
> > 2
> > ```
>
>
>
>
> > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([all-from-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._all-from-out%29%29) module-path ...)</p></blockquote></td></tr></tbody></table>
>
>
>
>
>
> Exports all identifiers that are imported into the exporting module using a require-spec built on each module-path (see [Importing and Exporting: require and provide]()) with no [phase-level](syntax-model.html#%28tech._phase._level%29) shift. The symbolic name for export is derived from the name that is bound within the module, as opposed to the symbolic name of the export from each module-path. Only identifiers accessible from the lexical context of the module-path are included; that is, macro-introduced imports are not re-exported, unless the module-path was introduced at the same time.
>
>
>
>
>
> Examples:
>
>
>
>
>
> > ```racket
> > > ( module nest racket ( provide num-eggs ) ( define num-eggs 2 ) )
> > > ( module hen-house racket ( require ' nest ) ( provide ( all-from-out ' nest ) ) )
> > > ( require ' hen-house )
> > > num-eggs
> > 2
> > ```
>
>
>
>
> > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([rename-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._rename-out%29%29) [orig-id export-id] ...)</p></blockquote></td></tr></tbody></table>
>
>
>
>
>
> Exports each orig-id, which must be [bound](syntax-model.html#%28tech._bound%29) within the module at the relevant [phase level](syntax-model.html#%28tech._phase._level%29) and [binding space](syntax-model.html#%28tech._binding._space%29). The symbolic name for each export is export-id instead of orig-id.
>
>
>
>
>
> Examples:
>
>
>
>
>
> > ```racket
> > > ( module nest racket ( provide ( rename-out [ count num-eggs ] ) ) ( define count 2 ) )
> > > ( require ' nest )
> > > num-eggs
> > 2
> > > count
> > count: undefined;
> > cannot reference an identifier before its definition
> > in module: top-level
> > ```
>
>
>
>
> > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([except-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._except-out%29%29) provide-spec provide-spec ...)</p></blockquote></td></tr></tbody></table>
>
>
>
>
>
> Like the first provide-spec, but omitting the bindings listed in each subsequent provide-spec. If one of the latter bindings is not included in the initial provide-spec, a syntax error is reported. The symbolic export name information in the latter provide-specs is ignored; only the bindings are used.
>
>
>
>
>
> Examples:
>
>
>
>
>
> > ```racket
> > > ( module nest racket ( provide ( except-out ( all-defined-out ) num-chicks ) ) ( define num-eggs 2 ) ( define num-chicks 3 ) )
> > > ( require ' nest )
> > > num-eggs
> > 2
> > > num-chicks
> > num-chicks: undefined;
> > cannot reference an identifier before its definition
> > in module: top-level
> > ```
>
>
>
>
> > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([prefix-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._prefix-out%29%29) prefix-id provide-spec)</p></blockquote></td></tr></tbody></table>
>
>
>
>
>
> Like provide-spec, but with each symbolic export name from provide-spec prefixed with prefix-id.
>
>
>
>
>
> Examples:
>
>
>
>
>
> > ```racket
> > > ( module nest racket ( provide ( prefix-out chicken: num-eggs ) ) ( define num-eggs 2 ) )
> > > ( require ' nest )
> > > chicken:num-eggs
> > 2
> > ```
>
>
>
> A [syntax property](stxprops.html#%28tech._syntax._property%29) with the key 'import-or-export-prefix-ranges is added to the exported identifier in the expanded form of [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29).
>
> Changed in version 8.9.0.5 of package base: Added the 'import-or-export-prefix-ranges syntax property.
>
>
> > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([struct-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct-out%29%29) id)</p></blockquote></td></tr></tbody></table>
>
>
>
>
>
> Exports the bindings associated with a structure type id. Typically, id is bound with ([struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29) id ....); more generally, id must have a [transformer](syntax-model.html#%28tech._transformer%29) binding of structure-type information at the relevant [phase level](syntax-model.html#%28tech._phase._level%29); see [Structure Type Transformer Binding](structinfo.html). Furthermore, for each identifier mentioned in the structure-type information, the enclosing module must define or import one identifier that is [free-identifier=?](stxcmp.html#%28def._%28%28quote._~23~25kernel%29._free-identifier~3d~3f%29%29). If the structure-type information includes a super-type identifier, and if the identifier has a [transformer](syntax-model.html#%28tech._transformer%29) binding of structure-type information, the accessor and mutator bindings of the super-type are not included by [struct-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct-out%29%29) for export.
>
>
>
>
>
> Examples:
>
>
>
>
>
> > ```racket
> > > ( module nest racket ( provide ( struct-out egg ) ) ( struct egg ( color wt ) ) )
> > > ( require ' nest )
> > > ( egg-color ( egg ' blue 10 ) )
> > 'blue
> > ```
>
>
>
>
> > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([combine-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._combine-out%29%29) provide-spec ...)</p></blockquote></td></tr></tbody></table>
>
>
>
>
>
> The union of the provide-specs.
>
>
>
>
>
> Examples:
>
>
>
>
>
> > ```racket
> > > ( module nest racket ( provide ( combine-out num-eggs num-chicks ) ) ( define num-eggs 2 ) ( define num-chicks 1 ) )
> > > ( require ' nest )
> > > num-eggs
> > 2
> > > num-chicks
> > 1
> > ```
>
>
>
>
> > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([protect-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._protect-out%29%29) provide-spec ...)</p></blockquote></td></tr></tbody></table>
>
>
>
>
>
> Like the union of the provide-specs, except that the exports are [protected](modprotect.html#%28tech._protected%29): requiring modules may refer to these bindings, but may not extract these bindings from macro expansions or access them via [eval](eval.html#%28def._%28%28quote._~23~25kernel%29._eval%29%29) without access privileges. For more details, see [Code Inspectors](modprotect.html). The provide-spec must specify only bindings that are defined within the exporting module.
>
>
>
>
>
> Examples:
>
>
>
>
>
> > ```racket
> > > ( module nest racket ( provide num-eggs ( protect-out num-chicks ) ) ( define num-eggs 2 ) ( define num-chicks 3 ) )
> > > ( define weak-inspector ( make-inspector ( current-code-inspector ) ) )
> > > ( define ( weak-eval x ) ( parameterize ( [ current-code-inspector weak-inspector ] ) ( define weak-ns ( make-base-namespace ) ) ( namespace-attach-module ( current-namespace ) ' ' nest weak-ns ) ( parameterize ( [ current-namespace weak-ns ] ) ( namespace-require ' ' nest ) ( eval x ) ) ) )
> > > ( require ' nest )
> > > ( list num-eggs num-chicks )
> > '(2 3)
> > > ( weak-eval ' num-eggs )
> > 2
> > > ( weak-eval ' num-chicks )
> > ?: access disallowed by code inspector to protected variable
> > from module: 'nest
> > at: num-chicks
> > ```
>
>
>
> See also [Code Inspectors for Trusted and Untrusted Code](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=code-inspectors_protect.html&version=8.18.0.13).
> > | |
> > |-----------------------------------------------------------------------------------------------------------------------------------------------|
> > | ([for-meta](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-meta%29%29) phase-level provide-spec ...) |
>
> Like the union of the provide-specs, but adjusted to apply to the [phase level](syntax-model.html#%28tech._phase._level%29) specified by phase-level relative to the current phase level (where #f corresponds to the [label phase level](syntax-model.html#%28tech._label._phase._level%29)). In particular, an id or [rename-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._rename-out%29%29) form as a provide-spec refers to a binding at phase-level relative to the current level, an [all-defined-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._all-defined-out%29%29) exports only definitions at phase-level relative to the current phase level, and an [all-from-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._all-from-out%29%29) exports bindings imported with a shift by phase-level.
>
>
>
> Examples:
>
>
>
>
>
> > ```racket
> > > ( module nest racket ( begin-for-syntax ( define eggs 2 ) ) ( define chickens 3 ) ( provide ( for-syntax eggs ) chickens ) )
> > > ( require ' nest )
> > > ( define-syntax ( test-eggs stx ) ( printf "Eggs are ~a\n" eggs ) #' 0 )
> > > ( test-eggs )
> > Eggs are 2
> > 0
> > > chickens
> > 3
> > > ( module broken-nest racket ( define eggs 2 ) ( define chickens 3 ) ( provide ( for-syntax eggs ) chickens ) )
> > eval:7:0: provide: provided identifier is not defined or
> > required
> > at: eggs
> > in: (provide (for-syntax eggs) chickens)
> > > ( module nest2 racket ( begin-for-syntax ( define eggs 2 ) ) ( provide ( for-syntax eggs ) ) )
> > > ( require ( for-meta 2 racket/base ) ( for-syntax ' nest2 ) )
> > > ( define-syntax ( test stx ) ( define-syntax ( show-eggs stx ) ( printf "Eggs are ~a\n" eggs ) #' 0 ) ( begin ( show-eggs ) #' 0 ) )
> > Eggs are 2
> > > ( test )
> > 0
> > ```
>
>
> > | |
> > |---------------------------------------------------------------------------------------------------------------------------------------|
> > | ([for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-syntax%29%29) provide-spec ...) |
>
> Same as ([for-meta](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-meta%29%29) 1 provide-spec [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)).
> > | |
> > |-------------------------------------------------------------------------------------------------------------------------------------------|
> > | ([for-template](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-template%29%29) provide-spec ...) |
>
> Same as ([for-meta](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-meta%29%29) -1 provide-spec [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)).
> > | |
> > |-------------------------------------------------------------------------------------------------------------------------------------|
> > | ([for-label](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-label%29%29) provide-spec ...) |
>
> Same as ([for-meta](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-meta%29%29) #f provide-spec [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)).
> > | |
> > |-------------------------------------------------------------------------------------------------------------------------------------------|
> > | ([for-space](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-space%29%29) space provide-spec ...) |
>
> Like the union of the provide-specs, but adjusted to apply to the [binding space](syntax-model.html#%28tech._binding._space%29) specified by space—where space is either an identifier or #f for the [default binding space](syntax-model.html#%28tech._default._binding._space%29). In particular, an id or [rename-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._rename-out%29%29) form as a provide-spec refers to a binding in space, an [all-defined-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._all-defined-out%29%29) exports only definitions in space, and an [all-from-out](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._all-from-out%29%29) exports bindings imported into space.
>
> When providing a binding for a non-default binding space, normally a module should also provide a binding for the default binding space, where the default-space binding represents the intended meaning of the identifier. When a module later imports the same name in different spaces from modules that adhere to this convention, then if the two modules also (re)export the same binding for the name in the default space, the imports are likely consistent. If the two modules export different bindings for the name in the default space, then attempting to import both modules will trigger an error about conflicting imports, and a programmer can explicitly resolve the mismatch.
>
> Added in version 8.2.0.3 of package base.
> > | |
> > |----------------------|
> > | derived-provide-spec |
>
> See [define-provide-syntax](define.html#%28form._%28%28lib._racket%2Fprovide-syntax..rkt%29._define-provide-syntax%29%29) for information on expanding the set of provide-spec forms.
Each export specified within a module must have a distinct symbolic export name, though the same binding can be specified with the multiple symbolic names.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for-meta](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-meta%29%29) phase-level require-spec ...)</p></blockquote></td></tr></tbody></table>
See [require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) and [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-syntax%29%29) require-spec ...)</p></blockquote></td></tr></tbody></table>
See [require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) and [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for-template](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-template%29%29) require-spec ...)</p></blockquote></td></tr></tbody></table>
See [require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) and [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for-label](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-label%29%29) require-spec ...)</p></blockquote></td></tr></tbody></table>
See [require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) and [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for-space](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-space%29%29) space require-spec ...)</p></blockquote></td></tr></tbody></table>
See [require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) and [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([#%require](#%28form._%28%28quote._~23~25kernel%29._~23~25require%29%29) raw-require-spec ...)</p></blockquote></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /></colgroup><tbody><tr><td style="text-align: right;" data-valign="baseline">raw-require-spec</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">phaseless-spec</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(for-meta phase-level raw-require-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(for-syntax raw-require-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(for-template raw-require-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(for-label raw-require-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(just-meta phase-level raw-require-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(portal portal-id content)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">phase-level</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">exact-integer</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#f</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">phaseless-spec</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">spaceless-spec</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(for-space space phaseless-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(just-space space spaceless-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">space</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#f</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">spaceless-spec</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">raw-module-path</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(only raw-module-path id ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(prefix prefix-id raw-module-path)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(all-except raw-module-path id ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"><pre><code>( prefix-all-except prefix-id
> raw-module-path id ... )</code></pre></td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(rename raw-module-path local-id exported-id)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">raw-module-path</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">raw-root-module-path</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(submod raw-root-module-path id ...+)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(submod "." id ...+)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">raw-root-module-path</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(quote id)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">rel-string</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(lib rel-string ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(file string)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"><pre><code>( planet rel-string
> ( user-string pkg-string vers ... ) )</code></pre></td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">literal-path</td></tr></tbody></table></td></tr></tbody></table>
The primitive import form, to which [require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) expands. A raw-require-spec is similar to a require-spec in a [require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) form, except that the syntax is more constrained, not composable, and not extensible. Also, sub-form names like for-syntax and lib are recognized symbolically, instead of via bindings. Some nested constraints are not formalized in the grammar above:
- a just-meta form cannot appear within a just-meta form;
- a for-meta, for-syntax, for-template, or for-label form cannot appear within a for-meta, for-syntax, for-template, or for-label form; and
- a for-space form cannot appear within a for-space form.
- a portal form cannot appear within a just-meta form.
Except for the portal form, each raw-require-spec corresponds to the obvious require-spec, but the rename sub-form has the identifiers in reverse order compared to [rename-in](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._rename-in%29%29).
For most raw-require-specs, the lexical context of the raw-require-spec determines the context of introduced identifiers. The exception is the rename sub-form, where the lexical context of the local-id is preserved.
A literal-path as a raw-root-module-path corresponds to a path in the sense of [path?](Manipulating_Paths.html#%28def._%28%28quote._~23~25kernel%29._path~3f%29%29). Since path values are never produced by [read-syntax](Reading.html#%28def._%28%28quote._~23~25kernel%29._read-syntax%29%29), they appear only in programmatically constructed expressions. They also appear naturally as arguments to functions such as [namespace-require](Namespaces.html#%28def._%28%28quote._~23~25kernel%29._namespace-require%29%29), with otherwise take a quoted raw-module-spec.
The portal form provides a way to define [portal syntax](stxtrans.html#%28tech._portal._syntax%29) at any phase level. A (portal portal-id content), defines portal-id to portal syntax with content effectively quoted to serve as its content.
Changed in version 8.2.0.3 of package base: Added for-space and just-space.
Changed in version 8.3.0.8: Added portal.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([#%provide](#%28form._%28%28quote._~23~25kernel%29._~23~25provide%29%29) raw-provide-spec ...)</p></blockquote></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td style="text-align: right;" data-valign="baseline">raw-provide-spec</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">phaseless-spec</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(for-meta phase-level phaseless-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(for-syntax phaseless-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(for-label phaseless-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(protect raw-provide-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">phase-level</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">exact-integer</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#f</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">phaseless-spec</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">spaceless-spec</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(for-space space spaceless-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(protect phaseless-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">space</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#f</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">spaceless-spec</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(rename local-id export-id)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(struct struct-id (field-id ...))</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(all-from raw-module-path)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(all-from-except raw-module-path id ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(all-defined)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(all-defined-except id ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(prefix-all-defined prefix-id)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(prefix-all-defined-except prefix-id id ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(protect spaceless-spec ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(expand (id . datum))</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(expand (id . datum) orig-form)</td></tr></tbody></table></td></tr></tbody></table>
The primitive export form, to which [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29) expands. A raw-module-path is as for [#%require](#%28form._%28%28quote._~23~25kernel%29._~23~25require%29%29). A protect sub-form cannot appear within a protect sub-form.
Like [#%require](#%28form._%28%28quote._~23~25kernel%29._~23~25require%29%29), the sub-form keywords for [#%provide](#%28form._%28%28quote._~23~25kernel%29._~23~25provide%29%29) are recognized symbolically, and nearly every raw-provide-spec has an obvious equivalent provide-spec via [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29), with the exception of the struct and expand sub-forms.
A (struct struct-id (field-id [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29))) sub-form expands to struct-id, make-struct-id, struct:struct-id, struct-id?, struct-id-field-id for each field-id, and set-struct-id-field-id! for each field-id. The lexical context of the struct-id is used for all generated identifiers.
Unlike [#%require](#%28form._%28%28quote._~23~25kernel%29._~23~25require%29%29), the [#%provide](#%28form._%28%28quote._~23~25kernel%29._~23~25provide%29%29) form is macro-extensible via an explicit expand sub-form; the (id . datum) part is locally expanded as an expression (even though it is not actually an expression), stopping when a [begin](begin.html#%28form._%28%28quote._~23~25kernel%29._begin%29%29) form is produced; if the expansion result is ([begin](begin.html#%28form._%28%28quote._~23~25kernel%29._begin%29%29) raw-provide-spec [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)), it is spliced in place of the expand form, otherwise a syntax error is reported. If an orig-form part is provided, then it is used instead of the [#%provide](#%28form._%28%28quote._~23~25kernel%29._~23~25provide%29%29) form when raising syntax errors, such as a “provide identifier is not defined” error. The expand sub-form is not normally used directly; it provides a hook for implementing [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29) and [provide transformers](stxtrans.html#%28tech._provide._transformer%29).
The all-from and all-from-except forms re-export only identifiers that are accessible in lexical context of the all-from or all-from-except form itself. That is, macro-introduced imports are not re-exported, unless the all-from or all-from-except form was introduced at the same time. Similarly, all-defined and its variants export only definitions accessible from the lexical context of the spaceless-spec form.
Changed in version 8.2.0.3 of package base: Added for-space.
Changed in version 8.2.0.5: Added orig-form support to expand.
#### 3.2.1 Additional [require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) Forms
| | |
|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
| ([require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/require](#%28mod-path._racket%2Frequire%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/require](#%28mod-path._racket%2Frequire%29) library, not [racket/base](index.html) or [racket](index.html).
The following forms support more complex selection and manipulation of sets of imported identifiers.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([matching-identifiers-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._matching-identifiers-in%29%29) regexp require-spec)</p></blockquote></td></tr></tbody></table>
Like require-spec, but including only imports whose names match regexp. The regexp must be a literal regular expression (see [Regular Expressions](regexp.html)).
Examples:
> ```racket
> > ( module zoo racket/base ( provide tunafish swordfish blowfish monkey lizard ant ) ( define tunafish 1 ) ( define swordfish 2 ) ( define blowfish 3 ) ( define monkey 4 ) ( define lizard 5 ) ( define ant 6 ) )
> > ( require racket/require )
> > ( require ( matching-identifiers-in #rx"\\w*fish" ' zoo ) )
> > tunafish
> 1
> > swordfish
> 2
> > blowfish
> 3
> > monkey
> monkey: undefined;
> cannot reference an identifier before its definition
> in module: top-level
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([subtract-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._subtract-in%29%29) require-spec subtracted-spec ...)</p></blockquote></td></tr></tbody></table>
Like require-spec, but omitting those imports that would be imported by one of the subtracted-specs.
Examples:
> ```racket
> > ( module earth racket ( provide land sea air ) ( define land 1 ) ( define sea 2 ) ( define air 3 ) )
> > ( module mars racket ( provide aliens ) ( define aliens 4 ) )
> > ( module solar-system racket ( require ' earth ' mars ) ( provide ( all-from-out ' earth ) ( all-from-out ' mars ) ) )
> > ( require racket/require )
> > ( require ( subtract-in ' solar-system ' earth ) )
> > land
> land: undefined;
> cannot reference an identifier before its definition
> in module: top-level
> > aliens
> 4
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([filtered-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._filtered-in%29%29) proc-expr require-spec)</p></blockquote></td></tr></tbody></table>
Applies an arbitrary transformation on the import names (as strings) of require-spec. The proc-expr must evaluate at expansion time to a single-argument procedure, which is applied on each of the names from require-spec. For each name, the procedure must return either a string for the import’s new name or #f to exclude the import.
> > > The second part of [filtered-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._filtered-in%29%29) is expand-time code evaluated in the scope of the enclosing module. Accordingly, most uses need ([require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) ([for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-syntax%29%29) racket/base)) if [racket/base](index.html) is not already imported [for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-syntax%29%29). For example, [#lang](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=Module_Syntax.html%23%2528part._hash-lang%2529&version=8.18.0.13) [racket](index.html) establishes this import automatically, while [#lang](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=Module_Syntax.html%23%2528part._hash-lang%2529&version=8.18.0.13) [racket/base](index.html) does not.
For example,
> ```racket
> ( require ( filtered-in
> ( lambda ( name )
> ( and ( regexp-match? #rx"^[a-z-]+$" name )
> ( regexp-replace #rx"-" ( string-titlecase name ) "" ) ) )
> racket/base ) )
> ```
imports only bindings from [racket/base](index.html) that match the pattern #rx"^\[a-z-\]+$", and it converts the names to “camel case.”
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([path-up](#%28form._%28%28lib._racket%2Frequire..rkt%29._path-up%29%29) rel-string ...)</p></blockquote></td></tr></tbody></table>
Specifies paths to modules named by the rel-strings similar to using the rel-strings directly, except that if a required module file is not found relative to the enclosing source, it is searched for in the parent directory, and then in the grand-parent directory, etc., all the way to the root directory. The discovered path relative to the enclosing source becomes part of the expanded form.
This form is useful in setting up a “project environment.” For example, using the following "config.rkt" file in the root directory of your project:
> ```racket
> #lang racket/base
> ( require racket/require-syntax
> ( for-syntax "utils/in-here.rkt" ) )
> ( provide utils-in )
> ( define-require-syntax utils-in in-here-transformer )
> ```
and using "utils/in-here.rkt" under the same root directory:
> ```racket
> #lang racket/base
> ( require racket/runtime-path )
> ( provide in-here-transformer )
> ( define-runtime-path here "." )
> ( define ( in-here-transformer stx )
> ( syntax-case stx ( )
> [ ( _ sym )
> ( identifier? #' sym )
> ( let ( [ path ( build-path here ( format "~a.rkt" ( syntax-e #' sym ) ) ) ] )
> ( datum->syntax stx ` ( file , ( path->string path ) ) stx ) ) ] ) )
> ```
then [path-up](#%28form._%28%28lib._racket%2Frequire..rkt%29._path-up%29%29) works for any other module under the project directory to find "config.rkt":
> ```racket
> ( require racket/require
> ( path-up "config.rkt" )
> ( utils-in foo ) )
> ```
Note that the order of requires in the example is important, as each of the first two bind the identifier used in the following.
An alternative in this scenario is to use [path-up](#%28form._%28%28lib._racket%2Frequire..rkt%29._path-up%29%29) directly to find the utility module:
> ```racket
> ( require racket/require
> ( path-up "utils/foo.rkt" ) )
> ```
but then sub-directories that are called "utils" override the one in the project’s root. In other words, the previous method requires only a single unique name.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([multi-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._multi-in%29%29) subs ...+)</p></blockquote></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td style="text-align: right;" data-valign="baseline">subs</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">sub-path</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(sub-path ...)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">sub-path</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">rel-string</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">id</td></tr></tbody></table></td></tr></tbody></table>
Specifies multiple files to be required from a hierarchy of directories or collections. The set of required module paths is computed as the Cartesian product of the subs groups, where each sub-path is combined with other sub-paths in order using a / separator. A sub-path as a subs is equivalent to (sub-path). All sub-paths in a given [multi-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._multi-in%29%29) form must be either strings or identifiers.
Examples:
<table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote><p>([require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) ([multi-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._multi-in%29%29) racket (dict list)))</p></blockquote></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td data-valign="baseline"><p> is equivalent to </p></td><td data-valign="baseline">([require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) racket/dict racket/list)</td></tr></tbody></table></td></tr><tr><td><p> </p></td></tr><tr><td><blockquote><p>([require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) ([multi-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._multi-in%29%29) "math" "matrix" "utils.rkt"))</p></blockquote></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td data-valign="baseline"><p> is equivalent to </p></td><td data-valign="baseline">([require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) "math/matrix/utils.rkt")</td></tr></tbody></table></td></tr><tr><td><p> </p></td></tr><tr><td><blockquote><p>([require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) ([multi-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._multi-in%29%29) "utils" ("math.rkt" "matrix.rkt")))</p></blockquote></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td data-valign="baseline"><p> is equivalent to </p></td><td data-valign="baseline">([require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) "utils/math.rkt" "utils/matrix.rkt")</td></tr></tbody></table></td></tr><tr><td><p> </p></td></tr><tr><td><blockquote><p>([require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) ([multi-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._multi-in%29%29) ("math" "matrix") "utils.rkt"))</p></blockquote></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td data-valign="baseline"><p> is equivalent to </p></td><td data-valign="baseline">([require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) "math/utils.rkt" "matrix/utils.rkt")</td></tr></tbody></table></td></tr><tr><td><p> </p></td></tr><tr><td><blockquote><p>([require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) ([multi-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._multi-in%29%29) ("math" "matrix") ("utils.rkt" "helpers.rkt")))</p></blockquote></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 50%" /><col style="width: 50%" /></colgroup><tbody><tr><td data-valign="baseline"><p> is equivalent to </p></td><td data-valign="baseline"><pre><code>( require "math/utils.rkt" "math/helpers.rkt"
"matrix/utils.rkt" "matrix/helpers.rkt" )</code></pre></td></tr></tbody></table></td></tr></tbody></table>
#### 3.2.2 Additional [provide](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29) Forms
| | |
|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
| ([require](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/provide](#%28mod-path._racket%2Fprovide%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/provide](#%28mod-path._racket%2Fprovide%29) library, not [racket/base](index.html) or [racket](index.html).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([matching-identifiers-out](#%28form._%28%28lib._racket%2Fprovide..rkt%29._matching-identifiers-out%29%29) regexp provide-spec)</p></blockquote></td></tr></tbody></table>
Like provide-spec, but including only exports of bindings with an external name that matches regexp. The regexp must be a literal regular expression (see [Regular Expressions](regexp.html)).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([filtered-out](#%28form._%28%28lib._racket%2Fprovide..rkt%29._filtered-out%29%29) proc-expr provide-spec)</p></blockquote></td></tr></tbody></table>
Analogous to [filtered-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._filtered-in%29%29), but for filtering and renaming exports.
> > > See the documentation of [filtered-in](#%28form._%28%28lib._racket%2Frequire..rkt%29._filtered-in%29%29) for use with [#lang](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=Module_Syntax.html%23%2528part._hash-lang%2529&version=8.18.0.13) [racket/base](index.html).
For example,
> ```racket
> ( provide ( filtered-out
> ( lambda ( name )
> ( and ( regexp-match? #rx"^[a-z-]+$" name )
> ( regexp-replace
> #rx"-" ( string-titlecase name ) "" ) ) )
> ( all-defined-out ) ) )
> ```
exports only bindings that match the pattern #rx"^\[a-z-\]+$", and it converts the names to “camel case.”
------------------------------------------------------------------------
# 3.3 Literals: quote and #%datum
### 3.3 Literals: [quote](#%28form._%28%28quote._~23~25kernel%29._quote%29%29) and [#%datum](#%28form._%28%28quote._~23~25kernel%29._~23~25datum%29%29)
Many forms are implicitly quoted (via [#%datum](#%28form._%28%28quote._~23~25kernel%29._~23~25datum%29%29)) as literals. See [Expansion Steps](syntax-model.html#%28part._expand-steps%29) for more information.
> > > <img src="finger.png" width="24" height="24" alt="+" />[Quoting: quote and ’](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=quote.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 [quote](#%28form._%28%28quote._~23~25kernel%29._quote%29%29).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([quote](#%28form._%28%28quote._~23~25kernel%29._quote%29%29) datum)</p></blockquote></td></tr></tbody></table>
Produces a constant value corresponding to datum (i.e., the representation of the program fragment) without its [lexical information](syntax-model.html#%28tech._lexical._information%29), source location, etc. Quoted pairs, vectors, and boxes are immutable.
Examples:
> ```racket
> > ( quote x )
> 'x
> > ( quote ( + 1 2 ) )
> '(+ 1 2)
> > ( + 1 2 )
> 3
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([#%datum](#%28form._%28%28quote._~23~25kernel%29._~23~25datum%29%29) . datum)</p></blockquote></td></tr></tbody></table>
Expands to (quote datum), as long as datum is not a keyword. If datum is a keyword, a syntax error is reported.
See also [Expansion Steps](syntax-model.html#%28part._expand-steps%29) for information on how the expander introduces #%datum identifiers.
Examples:
> ```racket
> > ( #%datum . 10 )
> 10
> > ( #%datum . x )
> 'x
> > ( #%datum . #:x )
> eval:6:0: #%datum: keyword misused as an expression
> at: #:x
> ```
------------------------------------------------------------------------
# 3.4 Expression Wrapper: #%expression
### 3.4 Expression Wrapper: [#%expression](#%28form._%28%28quote._~23~25kernel%29._~23~25expression%29%29)
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([#%expression](#%28form._%28%28quote._~23~25kernel%29._~23~25expression%29%29) expr)</p></blockquote></td></tr></tbody></table>
Produces the same result as expr. Using [#%expression](#%28form._%28%28quote._~23~25kernel%29._~23~25expression%29%29) forces the parsing of a form as an expression.
Examples:
> ```racket
> > ( #%expression ( + 1 2 ) )
> 3
> > ( #%expression ( define x 10 ) )
> eval:8:0: define: not allowed in an expression context
> in: (define x 10)
> ```
The [#%expression](#%28form._%28%28quote._~23~25kernel%29._~23~25expression%29%29) form is helpful in recursive definition contexts where expanding a subsequent definition can provide compile-time information for the current expression. For example, consider a define-sym-case macro that simply records some symbols at compile-time in a given identifier.
> ```racket
> ( define-syntax ( define-sym-case stx ) ( syntax-case stx ( ) [ ( _ id sym ... ) ( andmap identifier? ( syntax->list #' ( sym ... ) ) ) #' ( define-syntax id ' ( sym ... ) ) ] ) )
> ```
and then a variant of [case](case.html#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29) that checks to make sure the symbols used in the expression match those given in the earlier definition:
> ```racket
> ( define-syntax ( sym-case stx ) ( syntax-case stx ( ) [ ( _ id val-expr [ ( sym ) expr ] ... ) ( let ( ) ( define expected-ids ( syntax-local-value #' id ( λ ( ) ( raise-syntax-error ' sym-case "expected an identifier bound via define-sym-case" stx #' id ) ) ) ) ( define actual-ids ( syntax->datum #' ( sym ... ) ) ) ( unless ( equal? expected-ids actual-ids ) ( raise-syntax-error ' sym-case ( format "expected the symbols ~s" expected-ids ) stx ) ) #' ( case val-expr [ ( sym ) expr ] ... ) ) ] ) )
> ```
If the definition follows the use like this, then the define-sym-case macro does not have a chance to bind [id](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=syntax&rel=Library_Syntax_Classes_and_Literal_Sets.html%23%2528form._%2528%2528lib._syntax%252Fparse..rkt%2529._id%2529%2529&version=8.18.0.13) and the sym-case macro signals an error:
> ```racket
> > ( let ( ) ( sym-case land-creatures ' bear [ ( bear ) 1 ] [ ( fox ) 2 ] ) ( define-sym-case land-creatures bear fox ) )
> eval:11:0: sym-case: expected an identifier bound via
> define-sym-case
> at: land-creatures
> in: (sym-case land-creatures (quote bear) ((bear) 1)
> ((fox) 2))
> ```
But if the sym-case is wrapped in an [#%expression](#%28form._%28%28quote._~23~25kernel%29._~23~25expression%29%29), then the expander does not need to expand it to know it is an expression and it moves on to the define-sym-case expression.
> ```racket
> > ( let ( ) ( #%expression ( sym-case sea-creatures ' whale [ ( whale ) 1 ] [ ( squid ) 2 ] ) ) ( define-sym-case sea-creatures whale squid ) ' more... )
> 'more...
> ```
Of course, a macro like sym-case should not require its clients to add [#%expression](#%28form._%28%28quote._~23~25kernel%29._~23~25expression%29%29); instead it should check the basic shape of its arguments and then expand to [#%expression](#%28form._%28%28quote._~23~25kernel%29._~23~25expression%29%29) wrapped around a helper macro that calls [syntax-local-value](stxtrans.html#%28def._%28%28quote._~23~25kernel%29._syntax-local-value%29%29) and finishes the expansion.
------------------------------------------------------------------------
# 3.5 Variable References and #%top
### 3.5 Variable References and [#%top](#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29)
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>id</p></blockquote></td></tr></tbody></table>
Refers to a top-level, module-level, or local binding, when id is not bound as a transformer (see [Expansion](syntax-model.html#%28part._expansion%29)). At run-time, the reference evaluates to the value in the [location](eval-model.html#%28tech._location%29) associated with the binding.
When the expander encounters an id that is not bound by a module-level or local binding, it converts the expression to (#%top . id) giving #%top the lexical context of the id; typically, that context refers to [#%top](#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29). See also [Expansion Steps](syntax-model.html#%28part._expand-steps%29).
Examples:
> ```racket
> > ( define x 10 )
> > x
> 10
> > ( let ( [ x 5 ] ) x )
> 5
> > ( ( lambda ( x ) x ) 2 )
> 2
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([#%top](#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29) . id)</p></blockquote></td></tr></tbody></table>
Equivalent to id when id is bound to a module-level or top-level variable. In a top-level context, ([#%top](#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29) . id) always refers to a top-level variable, even if id is [unbound](syntax-model.html#%28tech._unbound%29) or bound to syntax, as long as id does not have a local binding. In all contexts, ([#%top](#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29) . id) is a syntax error if id has a local binding.
Within a [module](module.html#%28form._%28%28quote._~23~25kernel%29._module%29%29) form, ([#%top](#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29) . id) expands to just id as long as id is defined within the module and has no local binding in its context. At [phase level](syntax-model.html#%28tech._phase._level%29) 0, ([#%top](#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29) . id) is an immediate syntax error if id is not bound. At [phase level](syntax-model.html#%28tech._phase._level%29) 1 and higher, a syntax error is reported if id is not defined at the corresponding phase by the end of [module](module.html#%28form._%28%28quote._~23~25kernel%29._module%29%29)-body [partial expansion](syntax-model.html#%28tech._partial._expansion%29).
See also [Expansion Steps](syntax-model.html#%28part._expand-steps%29) for information on how the expander introduces #%top identifiers.
Examples:
> ```racket
> > ( define x 12 )
> > ( #%top . x )
> 12
> ```
Changed in version 6.3 of package base: Changed the introduction of [#%top](#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29) in a top-level context to [unbound](syntax-model.html#%28tech._unbound%29) identifiers only.
Changed in version 8.2.0.7: Changed treatment of locally bound id to always report a syntax error, even outside of a module.
------------------------------------------------------------------------
# 3.6 Locations: #%variable-reference
### 3.6 Locations: [#%variable-reference](#%28form._%28%28quote._~23~25kernel%29._~23~25variable-reference%29%29)
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([#%variable-reference](#%28form._%28%28quote._~23~25kernel%29._~23~25variable-reference%29%29) id)</p></blockquote></td></tr><tr><td>([#%variable-reference](#%28form._%28%28quote._~23~25kernel%29._~23~25variable-reference%29%29) ([#%top](__top.html#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29) . id))</td></tr><tr><td>([#%variable-reference](#%28form._%28%28quote._~23~25kernel%29._~23~25variable-reference%29%29))</td></tr></tbody></table>
Produces an opaque variable reference value representing the [location](eval-model.html#%28tech._location%29) of id, which must be bound as a variable. If no id is supplied, the resulting value refers to an “anonymous” variable defined within the enclosing context (i.e., within the enclosing module, or at the top level if the form is not inside a module).
When ([#%top](__top.html#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29) . id) is used, then the variable reference refers to the same variable as ([#%top](__top.html#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29) . id). Note that ([#%top](__top.html#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29) . id) is not allowed if id is locally bound or within a module if id is bound as a transformer.
A [variable reference](#%28tech._variable._reference%29) can be used with [variable-reference->empty-namespace](Namespaces.html#%28def._%28%28quote._~23~25kernel%29._variable-reference-~3eempty-namespace%29%29), [variable-reference->resolved-module-path](Namespaces.html#%28def._%28%28quote._~23~25kernel%29._variable-reference-~3eresolved-module-path%29%29), and [variable-reference->namespace](Namespaces.html#%28def._%28%28quote._~23~25kernel%29._variable-reference-~3enamespace%29%29), but facilities like [define-namespace-anchor](Namespaces.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-namespace-anchor%29%29) and [namespace-anchor->namespace](Namespaces.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._namespace-anchor-~3enamespace%29%29) wrap those to provide a clearer interface. A [variable reference](#%28tech._variable._reference%29) is also useful to low-level extensions; see [Inside: Racket C API](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=inside&rel=index.html&version=8.18.0.13).
Changed in version 8.2.0.7 of package base: Changed [#%top](__top.html#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29) treatment to be consistent with [#%top](__top.html#%28form._%28%28quote._~23~25kernel%29._~23~25top%29%29) by itself.
------------------------------------------------------------------------
# 3.7 Procedure Applications and #%app
### 3.7 Procedure Applications and [#%app](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._~23~25app%29%29)
> > > <img src="finger.png" width="24" height="24" alt="+" />[Function Calls](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=application.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 procedure applications.
> ```
syntax
(proc-expr arg ...)
```
|
### 2.5 Notation for Parameter Documentation
(parameter) →```
Combines [letrec-syntaxes](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._letrec-syntaxes%29%29) with a variant of [letrec-values](#%28form._%28%28quote._~23~25kernel%29._letrec-values%29%29): each trans-id and val-id is bound in all trans-exprs and val-exprs.
The [letrec-syntaxes+values](#%28form._%28%28quote._~23~25kernel%29._letrec-syntaxes%2Bvalues%29%29) form is the core form for local compile-time bindings, since forms like [letrec-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._letrec-syntax%29%29) and [internal-definition contexts](syntax-model.html#%28tech._internal._definition._context%29) expand to it. In a fully expanded expression (see [Fully Expanded Programs](syntax-model.html#%28part._fully-expanded%29)), the trans-id bindings are discarded and the form reduces to a combination of [letrec-values](#%28form._%28%28quote._~23~25kernel%29._letrec-values%29%29) or [let-values](#%28form._%28%28quote._~23~25kernel%29._let-values%29%29).
|
### 2.5 Notation for Parameter Documentation
For variables bound by [letrec-syntaxes+values](#%28form._%28%28quote._~23~25kernel%29._letrec-syntaxes%2Bvalues%29%29), the [location](eval-model.html#%28tech._location%29)-creation rules differ slightly from [letrec-values](#%28form._%28%28quote._~23~25kernel%29._letrec-values%29%29). The \[(val-id [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)) val-expr\] binding clauses are partitioned into minimal sets of clauses that satisfy the following rule: if a clause has a val-id binding that is referenced (in a full expansion) by the val-expr of an earlier clause, the two clauses and all in between are in the same set. If a set consists of a single clause whose val-expr does not refer to any of the clause’s val-ids, then [locations](eval-model.html#%28tech._location%29) for the val-ids are created after the val-expr is evaluated. Otherwise, [locations](eval-model.html#%28tech._location%29) for all val-ids in a set are created just before the first val-expr in the set is evaluated. For the purposes of forming sets, a ([quote-syntax](Syntax_Quoting__quote-syntax.html#%28form._%28%28quote._~23~25kernel%29._quote-syntax%29%29) datum #:local) form counts as a reference to all bindings in the [letrec-syntaxes+values](#%28form._%28%28quote._~23~25kernel%29._letrec-syntaxes%2Bvalues%29%29) form
|
### 2.5 Notation for Parameter Documentation
The end result of the [location](eval-model.html#%28tech._location%29)-creation rules is that scoping and evaluation order are the same as for [letrec-values](#%28form._%28%28quote._~23~25kernel%29._letrec-values%29%29), but the compiler has more freedom to optimize away [location](eval-model.html#%28tech._location%29) creation. The rules also correspond to a nesting of [let-values](#%28form._%28%28quote._~23~25kernel%29._let-values%29%29) and [letrec-values](#%28form._%28%28quote._~23~25kernel%29._letrec-values%29%29), which is how [letrec-syntaxes+values](#%28form._%28%28quote._~23~25kernel%29._letrec-syntaxes%2Bvalues%29%29) for a fully-expanded expression.
See also [local](local.html#%28form._%28%28lib._racket%2Flocal..rkt%29._local%29%29), which supports local bindings with [define](define.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](define.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), and more.
------------------------------------------------------------------------
|
# 3.10 Local Definitions: local
|
### 3.10 Local Definitions: [local](#%28form._%28%28lib._racket%2Flocal..rkt%29._local%29%29)
| | |
|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
| ([require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/local]()) | 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/local]() and [racket](index.html) libraries, but not [racket/base](index.html).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([local](#%28form._%28%28lib._racket%2Flocal..rkt%29._local%29%29) [definition ...] body ...+)</p></blockquote></td></tr></tbody></table>
|
### 3.10 Local Definitions: [local](#%28form._%28%28lib._racket%2Flocal..rkt%29._local%29%29)
Like [letrec-syntaxes+values](let.html#%28form._%28%28quote._~23~25kernel%29._letrec-syntaxes%2Bvalues%29%29), except that the bindings are expressed in the same way as in the top-level or in a module body: using [define](define.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-values](define.html#%28form._%28%28quote._~23~25kernel%29._define-values%29%29), [define-syntax](define.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), [struct](define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29), etc. Definitions are distinguished from non-definitions by partially expanding definition forms (see [Partial Expansion](syntax-model.html#%28part._partial-expansion%29)). As in the top-level or in a module body, a [begin](begin.html#%28form._%28%28quote._~23~25kernel%29._begin%29%29)-wrapped sequence is spliced into the sequence of definitions.
------------------------------------------------------------------------
|
# 3.11 Constructing Graphs: shared
|
### 3.11 Constructing Graphs: [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29)
| | |
|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
| ([require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/shared]()) | 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/shared]() and [racket](index.html) libraries, but not [racket/base](index.html).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29) ([id expr] ...) body ...+)</p></blockquote></td></tr></tbody></table>
Binds ids with shared structure according to exprs and then evaluates the bodys, returning the result of the last expression.
|
### 3.11 Constructing Graphs: [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29)
The [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29) form is similar to [letrec](let.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._letrec%29%29), except that special forms of expr are recognized (after partial macro expansion) to construct graph-structured data, where the corresponding [letrec](let.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._letrec%29%29) would instead produce a use-before-initialization error.
Each expr (after partial expansion) is matched against the following shared-expr grammar, where earlier variants in a production take precedence over later variants:
|
### 3.11 Constructing Graphs: [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29)
| | | | | |
|--------------------:|:----|:---:|:----|:----------------------------------------------------------------------------------------------------------------------------------------------------|
| shared-expr | | = | | shell-expr |
| | | \| | | plain-expr |
| | | | | |
| shell-expr | | = | | ([cons](pairs.html#%28def._%28%28quote._~23~25kernel%29._cons%29%29) in-immutable-expr in-immutable-expr) |
| | | \| | | ([list](pairs.html#%28def._%28%28quote._~23~25kernel%29._list%29%29) in-immutable-expr ...) |
| | | \| | | ([list*](pairs.html#%28def._%28%28quote._~23~25kernel%29._list%2A%29%29) in-immutable-expr ...) |
| | | \| | | ([append](pairs.html#%28def._%28%28quote._~23~25kernel%29._append%29%29) early-expr ... in-immutable-expr) |
| | | \| | | ([vector-immutable](vectors.html#%28def._%28%28quote._~23~25kernel%29._vector-immutable%29%29) in-immutable-expr ...) |
| | | \| | | ([box-immutable](boxes.html#%28def._%28%28quote._~23~25kernel%29._box-immutable%29%29) in-immutable-expr) |
| | | \| | | ([mcons](mpairs.html#%28def._%28%28quote._~23~25kernel%29._mcons%29%29) patchable-expr patchable-expr) |
| | | \| | | ([vector](vectors.html#%28def._%28%28quote._~23~25kernel%29._vector%29%29) patchable-expr ...) |
| | | \| | | ([box](boxes.html#%28def._%28%28quote._~23~25kernel%29._box%29%29) patchable-expr) |
| | | \| | | (prefix:make-id patchable-expr ...) |
| | | | | |
| in-immutable-expr | | = | | shell-id |
| | | \| | | shell-expr |
| | | \| | | early-expr |
| | | | | |
| shell-id | | = | | id |
| | | | | |
| patchable-expr | | = | | expr |
| | | | | |
| early-expr | | = | | expr |
| | | | | |
| plain-expr | | = | | expr |
|
### 3.11 Constructing Graphs: [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29)
The prefix:make-id identifier above matches three kinds of references. The first kind is any binding whose name has make- in the middle, and where prefix:id has a [transformer](syntax-model.html#%28tech._transformer%29) binding to structure information with a full set of mutator bindings; see [Structure Type Transformer Binding](structinfo.html). The second kind is an identifier that itself has a [transformer](syntax-model.html#%28tech._transformer%29) binding to structure information. The third kind is an identifier that has a 'constructor-for [syntax property](stxprops.html#%28tech._syntax._property%29) whose value is an identifier with a [transformer](syntax-model.html#%28tech._transformer%29) binding to structure information. A shell-id, meanwhile, must be one of the ids bound by the [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29) form to a shell-expr.
|
### 3.11 Constructing Graphs: [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29)
When the exprs of the [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29) form are parsed as shared-expr (taking into account the order of the variants for parsing precedence), the sub-expressions that were parsed via early-expr will be evaluated first when the [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29) form is evaluated. Among such expressions, they are evaluated in the order as they appear within the [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29) form. However, any reference to an id bound by [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29) produces a use-before-initialization errror, even if the binding for the id appears before the corresponding early-expr within the [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29) form.
The shell-ids and shell-exprs (not counting patchable-expr and early-expr sub-expressions) are effectively evaluated next:
- A shell-id reference produces the same value as the corresponding id will produce within the bodys, assuming that id is never mutated with [set!](set_.html#%28form._%28%28quote._~23~25kernel%29._set%21%29%29). This special handling of a shell-id reference is one way in which [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29) supports the creation of cyclic data, including immutable cyclic data.
|
### 3.11 Constructing Graphs: [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29)
- A shell-expr of the form ([mcons](mpairs.html#%28def._%28%28quote._~23~25kernel%29._mcons%29%29) patchable-expr patchable-expr), ([vector](vectors.html#%28def._%28%28quote._~23~25kernel%29._vector%29%29) patchable-expr [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)), ([box](boxes.html#%28def._%28%28quote._~23~25kernel%29._box%29%29) patchable-expr), or (prefix:make-id patchable-expr [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)) produces a mutable value whose content positions are initialized to [undefined](undefined.html#%28def._%28%28lib._racket%2Fundefined..rkt%29._undefined%29%29). Each content position is patched (i.e., updated) after the corresponding patchable-expr expression is later evaluated.
Next, the plain-exprs are evaluated as for [letrec](let.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._letrec%29%29), where a reference to an id raises [exn:fail:contract:variable](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract~3avariable%29%29) if it is evaluated before the right-hand side of the id binding.
|
### 3.11 Constructing Graphs: [shared](#%28form._%28%28lib._racket%2Fshared..rkt%29._shared%29%29)
Finally, the patchable-exprs are evaluated and their values replace [undefined](undefined.html#%28def._%28%28lib._racket%2Fundefined..rkt%29._undefined%29%29)s in the results of shell-exprs. At this point, all ids are bound, so patchable-exprs can create data cycles (but only with cycles that can be created via mutation).
Examples:
> ```racket
> > ( shared ( [ a ( cons 1 a ) ] ) a )
> #0='(1 . #0#)
> > ( shared ( [ a ( cons 1 b ) ] [ b ( cons 2 a ) ] ) a )
> #0='(1 2 . #0#)
> > ( shared ( [ a ( cons 1 b ) ] [ b 7 ] ) a )
> '(1 . 7)
> > ( shared ( [ a a ] ) ; no indirection... a )
> a: undefined;
> cannot use before initialization
> > ( shared ( [ a ( cons 1 b ) ] ; b is early... [ b a ] ) a )
> a: undefined;
> cannot use before initialization
> > ( shared ( [ a ( mcons 1 b ) ] ; b is patchable... [ b a ] ) a )
> #0=(mcons 1 #0#)
> > ( shared ( [ a ( vector b b b ) ] [ b ( box 1 ) ] ) ( set-box! b 5 ) a )
> '#(#&5 #&5 #&5)
> > ( shared ( [ a ( box b ) ] [ b ( vector ( unbox a ) ; unbox after a is patched ( unbox c ) ) ] ; unbox before c is patched [ c ( box b ) ] ) b )
> #0='#(#0# #<undefined>)
> ```
------------------------------------------------------------------------
|
# 3.12 Conditionals: if, cond, and, and or
|
### 3.12 Conditionals: [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29), [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29), [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29), and [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29)
> > > <img src="finger.png" width="24" height="24" alt="+" />[Conditionals](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=conditionals.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 conditionals.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([if](#%28form._%28%28quote._~23~25kernel%29._if%29%29) test-expr then-expr else-expr)</p></blockquote></td></tr></tbody></table>
Evaluates test-expr. If it produces any value other than #f, then then-expr is evaluated, and its results are the result for the [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29) form. Otherwise, else-expr is evaluated, and its results are the result for the [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29) form. The then-expr and else-expr are in tail position with respect to the [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29) form.
Examples:
|
### 3.12 Conditionals: [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29), [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29), [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29), and [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29)
> ```racket
> > ( if ( positive? -5 ) ( error "doesn't get here" ) 2 )
> 2
> > ( if ( positive? 5 ) 1 ( error "doesn't get here" ) )
> 1
> > ( if ' we-have-no-bananas "yes" "no" )
> "yes"
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
|
### 3.12 Conditionals: [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29), [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29), [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29), and [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29)
<p>([cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29) cond-clause ...)</p></blockquote></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td style="text-align: right;" data-valign="baseline">cond-clause</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">[test-expr then-body ...+]</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">[[else](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._else%29%29) then-body ...+]</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">[test-expr [=>](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._~3d~3e%29%29) proc-expr]</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">[test-expr]</td></tr></tbody></table></td></tr></tbody></table>
|
### 3.12 Conditionals: [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29), [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29), [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29), and [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29)
> > > <img src="finger.png" width="24" height="24" alt="+" />[Chaining Tests: cond](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=conditionals.html%23%2528part._cond%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 [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29).
A cond-clause that starts with [else](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._else%29%29) must be the last cond-clause.
If no cond-clauses are present, the result is [#<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).
|
### 3.12 Conditionals: [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29), [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29), [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29), and [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29)
If only a \[[else](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._else%29%29) then-body [...+](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=syntax&rel=stxparse-patterns.html%23%2528form._%2528%2528lib._syntax%252Fparse..rkt%2529._......%252B%2529%2529&version=8.18.0.13)\] is present, then the then-bodys are evaluated. The results from all but the last then-body are ignored. The results of the last then-body, which is in tail position with respect to the [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29) form, are the results for the whole [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29) form.
Otherwise, the first test-expr is evaluated. If it produces #f, then the result is the same as a [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29) form with the remaining cond-clauses, in tail position with respect to the original [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29) form. Otherwise, evaluation depends on the form of the cond-clause:
|
### 3.12 Conditionals: [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29), [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29), [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29), and [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29)
> > | |
> > |------------------------------|
> > | \[test-expr then-body ...+\] |
>
> The then-bodys are evaluated in order, and the results from all but the last then-body are ignored. The results of the last then-body, which is in tail position with respect to the [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29) form, provides the result for the whole [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29) form.
|
### 3.12 Conditionals: [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29), [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29), [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29), and [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29)
> > | |
> > |--------------------------------------------------------------------------------------------------------------------------------------------|
> > | \[test-expr [=>](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._~3d~3e%29%29) proc-expr\] |
>
> The proc-expr is evaluated, and it must produce a procedure that accepts one argument, otherwise the [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception is raised. The procedure is applied to the result of test-expr in tail position with respect to the [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29) expression.
> > | |
> > |---------------|
> > | \[test-expr\] |
>
> The result of the test-expr is returned as the result of the [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29) form. The test-expr is not in tail position.
Examples:
|
### 3.12 Conditionals: [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29), [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29), [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29), and [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29)
> ```racket
> > ( cond )
> > ( cond [ else 5 ] )
> 5
> > ( cond [ ( positive? -5 ) ( error "doesn't get here" ) ] [ ( zero? -5 ) ( error "doesn't get here, either" ) ] [ ( positive? 5 ) ' here ] )
> 'here
> > ( cond [ ( member 2 ' ( 1 2 3 ) ) => ( lambda ( l ) ( map - l ) ) ] )
> '(-2 -3)
> > ( cond [ ( member 2 ' ( 1 2 3 ) ) ] )
> '(2 3)
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>[else](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._else%29%29)</p></blockquote></td></tr></tbody></table>
Recognized specially within forms like [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29). An [else](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._else%29%29) form as an expression is a syntax error.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>[=>](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._~3d~3e%29%29)</p></blockquote></td></tr></tbody></table>
|
### 3.12 Conditionals: [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29), [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29), [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29), and [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29)
Recognized specially within forms like [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29). A [=>](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._~3d~3e%29%29) form as an expression is a syntax error.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29) expr ...)</p></blockquote></td></tr></tbody></table>
> > > <img src="finger.png" width="24" height="24" alt="+" />[Combining Tests: and and or](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=conditionals.html%23%2528part._and%252Bor%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 [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29).
If no exprs are provided, then result is #t.
|
### 3.12 Conditionals: [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29), [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29), [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29), and [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29)
If a single expr is provided, then it is in tail position, so the results of the [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29) expression are the results of the expr.
Otherwise, the first expr is evaluated. If it produces #f, the result of the [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29) expression is #f. Otherwise, the result is the same as an [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29) expression with the remaining exprs in tail position with respect to the original [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29) form.
Examples:
> ```racket
> > ( and )
> #t
> > ( and 1 )
> 1
> > ( and ( values 1 2 ) )
> 1 2
> > ( and #f ( error "doesn't get here" ) )
> #f
> > ( and #t 5 )
> 5
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29) expr ...)</p></blockquote></td></tr></tbody></table>
|
### 3.12 Conditionals: [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29), [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29), [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29), and [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29)
> > > <img src="finger.png" width="24" height="24" alt="+" />[Combining Tests: and and or](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=conditionals.html%23%2528part._and%252Bor%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 [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29).
If no exprs are provided, then result is #f.
If a single expr is provided, then it is in tail position, so the results of the [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29) expression are the results of the expr.
Otherwise, the first expr is evaluated. If it produces a value other than #f, that result is the result of the [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29) expression. Otherwise, the result is the same as an [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29) expression with the remaining exprs in tail position with respect to the original [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29) form.
Examples:
|
### 3.12 Conditionals: [if](#%28form._%28%28quote._~23~25kernel%29._if%29%29), [cond](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._cond%29%29), [and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29), and [or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29)
> ```racket
> > ( or )
> #f
> > ( or 1 )
> 1
> > ( or ( values 1 2 ) )
> 1 2
> > ( or 5 ( error "doesn't get here" ) )
> 5
> > ( or #f 5 )
> 5
> ```
------------------------------------------------------------------------
|
# 3.13 Dispatch: case
|
### 3.13 Dispatch: [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29)
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29) val-expr case-clause ...)</p></blockquote></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td style="text-align: right;" data-valign="baseline">case-clause</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">[(datum ...) then-body ...+]</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">[[else](if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._else%29%29) then-body ...+]</td></tr></tbody></table></td></tr></tbody></table>
|
### 3.13 Dispatch: [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29)
Evaluates val-expr and uses the result to select a case-clause. The selected clause is the first one with a datum whose [quote](quote.html#%28form._%28%28quote._~23~25kernel%29._quote%29%29)d form is [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) to the result of val-expr. If no such datum is present, the [else](if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._else%29%29) case-clause is selected; if no [else](if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._else%29%29) case-clause is present, either, then the result of the [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29) form is [#<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).
|
### 3.13 Dispatch: [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29)
> > > The [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29) form of [racket](index.html) differs from that of [R6RS](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?tag=%28part._%28.%27%28lib._r6rs%2Fscribblings%2Fr6rs..scrbl%29.%27._.%27top.%27%29%29&version=8.18.0.13) or [R5RS](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?tag=%28part._%28.%27%28lib._r5rs%2Fr5rs..scrbl%29.%27._.%27top.%27%29%29&version=8.18.0.13) by being based on [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) instead of [eqv?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29) (in addition to allowing internal definitions).
For the selected case-clause, the results of the last then-body, which is in tail position with respect to the [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29) form, are the results for the whole [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29) form.
A case-clause that starts with [else](if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._else%29%29) must be the last case-clause.
The [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29) form can dispatch to a matching case-clause in O(log N) time for N datums.
Examples:
|
### 3.13 Dispatch: [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29)
> ```racket
> > ( case ( + 7 5 ) [ ( 1 2 3 ) ' small ] [ ( 10 11 12 ) ' big ] )
> 'big
> > ( case ( - 7 5 ) [ ( 1 2 3 ) ' small ] [ ( 10 11 12 ) ' big ] )
> 'small
> > ( case ( string-append "do" "g" ) [ ( "cat" "dog" "mouse" ) "animal" ] [ else "mineral or vegetable" ] )
> "animal"
> > ( case ( list ' y ' x ) [ ( ( a b ) ( x y ) ) ' forwards ] [ ( ( b a ) ( y x ) ) ' backwards ] )
> 'backwards
> > ( case ' x [ ( x ) "ex" ] [ ( ' x ) "quoted ex" ] )
> "ex"
> > ( case ( list ' quote ' x ) [ ( x ) "ex" ] [ ( ' x ) "quoted ex" ] )
> "quoted ex"
> ( define ( classify c ) ( case ( char-general-category c ) [ ( ll lu lt ln lo ) "letter" ] [ ( nd nl no ) "number" ] [ else "other" ] ) )
> > ( classify #\A )
> "letter"
> > ( classify #\1 )
> "number"
> > ( classify #\! )
> "other"
> ```
|
#### 3.13.1 Variants of [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29)
| | |
|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
| ([require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/case](#%28mod-path._racket%2Fcase%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/case](#%28mod-path._racket%2Fcase%29) library, not [racket/base](index.html) or [racket](index.html).
Added in version 8.11.1.8 of package base.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
|
### 3.13 Dispatch: [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29)
<p>([case/equal](#%28form._%28%28lib._racket%2Fcase..rkt%29._case%2Fequal%29%29) val-expr case-clause ...)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([case/equal-always](#%28form._%28%28lib._racket%2Fcase..rkt%29._case%2Fequal-always%29%29) val-expr case-clause ...)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([case/eq](#%28form._%28%28lib._racket%2Fcase..rkt%29._case%2Feq%29%29) val-expr case-clause ...)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([case/eqv](#%28form._%28%28lib._racket%2Fcase..rkt%29._case%2Feqv%29%29) val-expr case-clause ...)</p></blockquote></td></tr></tbody></table></td></tr></tbody></table>
|
### 3.13 Dispatch: [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29)
Like [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29), but using [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), [eq?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29), or [eqv?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29) for comparing the result of val-expr to the literals in the case-clauses. The [case/equal](#%28form._%28%28lib._racket%2Fcase..rkt%29._case%2Fequal%29%29) form is equivalent to [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29).
------------------------------------------------------------------------
|
# 3.14 Definitions: define, define-syntax, ...
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
> > > <img src="finger.png" width="24" height="24" alt="+" />[Definitions: define](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=define.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 definitions.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
<p>([define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29) id expr)</p></blockquote></td></tr><tr><td>([define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29) (head args) body ...+)</td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td style="text-align: right;" data-valign="baseline">head</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(head args)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">args</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">arg ...</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">arg ... . rest-id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">arg</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">arg-id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">[arg-id default-expr]</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">keyword arg-id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">keyword [arg-id default-expr]</td></tr></tbody></table></td></tr></tbody></table>
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
The first form [bind](syntax-model.html#%28tech._bind%29)s id to the result of expr, and the second form [bind](syntax-model.html#%28tech._bind%29)s id to a procedure. In the second case, the generated procedure is (CVT (head args) body [...+](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=syntax&rel=stxparse-patterns.html%23%2528form._%2528%2528lib._syntax%252Fparse..rkt%2529._......%252B%2529%2529&version=8.18.0.13)), using the CVT meta-function defined as follows:
> ```racket
> ( CVT ( id . kw-formals ) . datum ) = ( lambda kw-formals . datum )
> ( CVT ( head . kw-formals ) . datum ) = ( lambda kw-formals expr )
> if ( CVT head . datum ) = expr
> ```
In an [internal-definition context](syntax-model.html#%28tech._internal._definition._context%29), a [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29) form introduces a local binding; see [Internal Definitions](syntax-model.html#%28part._intdef-body%29). At the top level, the top-level binding for id is created after evaluating expr, if it does not exist already, and the top-level mapping of id (in the [namespace](syntax-model.html#%28tech._namespace%29) linked with the compiled definition) is set to the binding at the same time.
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
In a context that allows [liberal expansion](stxtrans.html#%28tech._liberal._expansion%29) of [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), id is bound as syntax if expr is an immediate [lambda](lambda.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._lambda%29%29) form with keyword arguments or args include keyword arguments.
Examples:
> ```racket
> ( define x 10 )
> > x
> 10
> ( define ( f x ) ( + x 1 ) )
> > ( f 10 )
> 11
> ( define ( ( f x ) [ y 20 ] ) ( + x y ) )
> > ( ( f 10 ) 30 )
> 40
> > ( ( f 10 ) )
> 30
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([define-values](#%28form._%28%28quote._~23~25kernel%29._define-values%29%29) (id ...) expr)</p></blockquote></td></tr></tbody></table>
Evaluates the expr, and [bind](syntax-model.html#%28tech._bind%29)s the results to the ids, in order, if the number of results matches the number of ids; if expr produces a different number of results, the [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception is raised.
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
In an [internal-definition context](syntax-model.html#%28tech._internal._definition._context%29) (see [Internal Definitions](syntax-model.html#%28part._intdef-body%29)), a [define-values](#%28form._%28%28quote._~23~25kernel%29._define-values%29%29) form introduces local bindings. At the top level, the top-level binding for each id is created after evaluating expr, if it does not exist already, and the top-level mapping of each id (in the [namespace](syntax-model.html#%28tech._namespace%29) linked with the compiled definition) is set to the binding at the same time.
Examples:
> ```racket
> > ( define-values ( ) ( values ) )
> > ( define-values ( x y z ) ( values 1 2 3 ) )
> > z
> 3
> ```
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
If a [define-values](#%28form._%28%28quote._~23~25kernel%29._define-values%29%29) form for a function definition in a module body has a 'compiler-hint:cross-module-inline [syntax property](stxprops.html#%28tech._syntax._property%29) with a true value, then the Racket treats the property as a performance hint. See [Function-Call Optimizations](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=performance.html%23%2528part._func-call-performance%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) for more information, and see also [begin-encourage-inline](performance-hint.html#%28form._%28%28lib._racket%2Fperformance-hint..rkt%29._begin-encourage-inline%29%29).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29) id expr)</p></blockquote></td></tr><tr><td>([define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29) (head args) body ...+)</td></tr></tbody></table>
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
The first form creates a [transformer](syntax-model.html#%28tech._transformer%29) binding (see [Transformer Bindings](syntax-model.html#%28part._transformer-model%29)) of id with the value of expr, which is an expression at [phase level](syntax-model.html#%28tech._phase._level%29) 1 relative to the surrounding context. (See [Identifiers, Binding, and Scopes](syntax-model.html#%28part._id-model%29) for information on [phase levels](syntax-model.html#%28tech._phase._level%29).) Evaluation of expr side is [parameterize](parameters.html#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._parameterize%29%29)d to set [current-namespace](Namespaces.html#%28def._%28%28quote._~23~25kernel%29._current-namespace%29%29) as in [let-syntax](let.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._let-syntax%29%29).
The second form is a shorthand the same as for [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29); it expands to a definition of the first form where the expr is a [lambda](lambda.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._lambda%29%29) form.
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
In an [internal-definition context](syntax-model.html#%28tech._internal._definition._context%29) (see [Internal Definitions](syntax-model.html#%28part._intdef-body%29)), a [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29) form introduces a local binding.
Examples:
> ```racket
> > ( define-syntax foo ( syntax-rules ( ) ( ( _ a ... ) ( printf "~a\n" ( list a ... ) ) ) ) )
> > ( foo 1 2 3 4 )
> (1 2 3 4)
> > ( define-syntax ( bar syntax-object ) ( syntax-case syntax-object ( ) ( ( _ a ... ) #' ( printf "~a\n" ( list a ... ) ) ) ) )
> > ( bar 1 2 3 4 )
> (1 2 3 4)
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([define-syntaxes](#%28form._%28%28quote._~23~25kernel%29._define-syntaxes%29%29) (id ...) expr)</p></blockquote></td></tr></tbody></table>
Like [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), but creates a [transformer](syntax-model.html#%28tech._transformer%29) binding for each id. The expr should produce as many values as ids, and each value is bound to the corresponding id.
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
When expr produces zero values for a top-level [define-syntaxes](#%28form._%28%28quote._~23~25kernel%29._define-syntaxes%29%29) (i.e., not in a module or internal-definition position), then the ids are effectively declared without binding; see [Macro-Introduced Bindings](syntax-model.html#%28part._macro-introduced-bindings%29).
In an [internal-definition context](syntax-model.html#%28tech._internal._definition._context%29) (see [Internal Definitions](syntax-model.html#%28part._intdef-body%29)), a [define-syntaxes](#%28form._%28%28quote._~23~25kernel%29._define-syntaxes%29%29) form introduces local bindings.
Examples:
> ```racket
> > ( define-syntaxes ( foo1 foo2 foo3 ) ( let ( [ transformer1 ( lambda ( syntax-object ) ( syntax-case syntax-object ( ) [ ( _ ) #' 1 ] ) ) ] [ transformer2 ( lambda ( syntax-object ) ( syntax-case syntax-object ( ) [ ( _ ) #' 2 ] ) ) ] [ transformer3 ( lambda ( syntax-object ) ( syntax-case syntax-object ( ) [ ( _ ) #' 3 ] ) ) ] ) ( values transformer1 transformer2 transformer3 ) ) )
> > ( foo1 )
> 1
> > ( foo2 )
> 2
> > ( foo3 )
> 3
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
<p>([define-for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-for-syntax%29%29) id expr)</p></blockquote></td></tr><tr><td>([define-for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-for-syntax%29%29) (head args) body ...+)</td></tr></tbody></table>
Like [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), except that the binding is at [phase level](syntax-model.html#%28tech._phase._level%29) 1 instead of [phase level](syntax-model.html#%28tech._phase._level%29) 0 relative to its context. The expression for the binding is also at [phase level](syntax-model.html#%28tech._phase._level%29) 1. (See [Identifiers, Binding, and Scopes](syntax-model.html#%28part._id-model%29) for information on [phase levels](syntax-model.html#%28tech._phase._level%29).) The form is a shorthand for ([begin-for-syntax](begin.html#%28form._%28%28quote._~23~25kernel%29._begin-for-syntax%29%29) ([define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29) id expr)) or ([begin-for-syntax](begin.html#%28form._%28%28quote._~23~25kernel%29._begin-for-syntax%29%29) ([define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29) (head args) body [...+](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=syntax&rel=stxparse-patterns.html%23%2528form._%2528%2528lib._syntax%252Fparse..rkt%2529._......%252B%2529%2529&version=8.18.0.13))).
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
Within a module, bindings introduced by [define-for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-for-syntax%29%29) must appear before their uses or in the same [define-for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-for-syntax%29%29) form (i.e., the [define-for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-for-syntax%29%29) form must be expanded before the use is expanded). In particular, mutually recursive functions bound by [define-for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-for-syntax%29%29) must be defined by the same [define-for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-for-syntax%29%29) form.
Examples:
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
> ```racket
> > ( define-for-syntax helper 2 )
> > ( define-syntax ( make-two syntax-object ) ( printf "helper is ~a\n" helper ) #' 2 )
> > ( make-two )
> helper is 2
> 2
> ; ‘ helper ' is not bound in the runtime phase
> > helper
> helper: undefined;
> cannot reference an identifier before its definition
> in module: top-level
> > ( define-for-syntax ( filter-ids ids ) ( filter identifier? ids ) )
> > ( define-syntax ( show-variables syntax-object ) ( syntax-case syntax-object ( ) [ ( _ expr ... ) ( with-syntax ( [ ( only-ids ... ) ( filter-ids ( syntax->list #' ( expr ... ) ) ) ] ) #' ( list only-ids ... ) ) ] ) )
> > ( let ( [ a 1 ] [ b 2 ] [ c 3 ] ) ( show-variables a 5 2 b c ) )
> '(1 2 3)
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([define-values-for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fdefine..rkt%29._define-values-for-syntax%29%29) (id ...) expr)</p></blockquote></td></tr></tbody></table>
Like [define-for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-for-syntax%29%29), but expr must produce as many values as supplied ids, and all of the ids are bound (at [phase level](syntax-model.html#%28tech._phase._level%29) 1).
Examples:
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
> ```racket
> > ( define-values-for-syntax ( foo1 foo2 ) ( values 1 2 ) )
> > ( define-syntax ( bar syntax-object ) ( printf "foo1 is ~a foo2 is ~a\n" foo1 foo2 ) #' 2 )
> > ( bar )
> foo1 is 1 foo2 is 2
> 2
> ```
|
#### 3.14.1 [require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) Macros
| | |
|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
| ([require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) [racket/require-syntax](#%28mod-path._racket%2Frequire-syntax%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/require-syntax](#%28mod-path._racket%2Frequire-syntax%29) library, not [racket/base](index.html) or [racket](index.html).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
<p>([define-require-syntax](#%28form._%28%28lib._racket%2Frequire-syntax..rkt%29._define-require-syntax%29%29) id proc-expr)</p></blockquote></td></tr><tr><td>([define-require-syntax](#%28form._%28%28lib._racket%2Frequire-syntax..rkt%29._define-require-syntax%29%29) (id args ...) body ...+)</td></tr></tbody></table>
The first form is like [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), but for a [require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) sub-form. The proc-expr must produce a procedure that accepts and returns a syntax object representing a [require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) sub-form.
This form expands to [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29) with a use of [make-require-transformer](stxtrans.html#%28def._%28%28lib._racket%2Frequire-transform..rkt%29._make-require-transformer%29%29) (see [require Transformers](stxtrans.html#%28part._require-trans%29) for more information).
The second form is a shorthand the same as for [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29); it expands to a definition of the first form where the proc-expr is a [lambda](lambda.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._lambda%29%29) form.
> ```
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
(syntax-local-require-introduce stx) → syntax?
stx : syntax?
```
For backward compatibility only; equivalent to syntax-local-introduce.
Changed in version 6.90.0.29 of package base: Made equivalent to syntax-local-introduce.
#### 3.14.2 provide Macros
| | |
|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
| (require racket/provide-syntax) | package: base |
The bindings documented in this section are provided by the racket/provide-syntax library, not racket/base or racket.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(define-provide-syntax id proc-expr)</p></blockquote></td></tr><tr><td>(define-provide-syntax (id args ...) body ...+)</td></tr></tbody></table>
The first form is like define-syntax, but for a provide sub-form. The proc-expr must produce a procedure that accepts and returns a syntax object representing a provide sub-form.
This form expands to define-syntax with a use of make-provide-transformer (see provide Transformers for more information).
The second form is a shorthand the same as for define-syntax; it expands to a definition of the first form where the expr is a lambda form.
> ```
(syntax-local-provide-introduce stx) → syntax?
stx : syntax?```
For backward compatibility only; equivalent to [syntax-local-introduce](stxtrans.html#%28def._%28%28quote._~23~25kernel%29._syntax-local-introduce%29%29).
Changed in version 6.90.0.29 of package base: Made equivalent to [syntax-local-introduce](stxtrans.html#%28def._%28%28quote._~23~25kernel%29._syntax-local-introduce%29%29).
------------------------------------------------------------------------
# 3.15 Sequencing: begin, begin0, and begin-for-syntax
### 3.15 Sequencing: [begin](#%28form._%28%28quote._~23~25kernel%29._begin%29%29), [begin0](#%28form._%28%28quote._~23~25kernel%29._begin0%29%29), and [begin-for-syntax](#%28form._%28%28quote._~23~25kernel%29._begin-for-syntax%29%29)
> > > <img src="finger.png" width="24" height="24" alt="+" />[Sequencing](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=begin.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 [begin](#%28form._%28%28quote._~23~25kernel%29._begin%29%29) and [begin0](#%28form._%28%28quote._~23~25kernel%29._begin0%29%29).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([begin](#%28form._%28%28quote._~23~25kernel%29._begin%29%29) form ...)</p></blockquote></td></tr><tr><td>([begin](#%28form._%28%28quote._~23~25kernel%29._begin%29%29) expr ...+)</td></tr></tbody></table>
The first form applies when [begin](#%28form._%28%28quote._~23~25kernel%29._begin%29%29) appears at the top level, at module level, or in an internal-definition position. In that case, the [begin](#%28form._%28%28quote._~23~25kernel%29._begin%29%29) form is equivalent to splicing the forms into the enclosing context.
The second form applies for [begin](#%28form._%28%28quote._~23~25kernel%29._begin%29%29) in an expression position. In that case, the exprs are evaluated in order, and the results are ignored for all but the last expr. The last expr is in tail position with respect to the [begin](#%28form._%28%28quote._~23~25kernel%29._begin%29%29) form.
Examples:
> ```racket
> > ( begin ( define x 10 ) x )
> 10
> > ( + 1 ( begin ( printf "hi\n" ) 2 ) )
> hi
> 3
> > ( let-values ( [ ( x y ) ( begin ( values 1 2 3 ) ( values 1 2 ) ) ] ) ( list x y ) )
> '(1 2)
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([begin0](#%28form._%28%28quote._~23~25kernel%29._begin0%29%29) expr ...+)</p></blockquote></td></tr></tbody></table>
Evaluates the first expr, then evaluates the other exprss in order, ignoring their results. The results of the first expr are the results of the [begin0](#%28form._%28%28quote._~23~25kernel%29._begin0%29%29) form; the first expr is in tail position only if no other exprs are present.
Example:
> ```racket
> > ( begin0 ( values 1 2 ) ( printf "hi\n" ) )
> hi
> 1 2
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([begin-for-syntax](#%28form._%28%28quote._~23~25kernel%29._begin-for-syntax%29%29) form ...)</p></blockquote></td></tr></tbody></table>
Allowed only in a [top-level context](syntax-model.html#%28tech._top._level._context%29) or [module context](syntax-model.html#%28tech._module._context%29), shifts the [phase level](syntax-model.html#%28tech._phase._level%29) of each form by one:
- expressions reference bindings at a [phase level](syntax-model.html#%28tech._phase._level%29) one greater than in the context of the [begin-for-syntax](#%28form._%28%28quote._~23~25kernel%29._begin-for-syntax%29%29) form;
- [define](define.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-values](define.html#%28form._%28%28quote._~23~25kernel%29._define-values%29%29), [define-syntax](define.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), and [define-syntaxes](define.html#%28form._%28%28quote._~23~25kernel%29._define-syntaxes%29%29) forms bind at a [phase level](syntax-model.html#%28tech._phase._level%29) one greater than in the context of the [begin-for-syntax](#%28form._%28%28quote._~23~25kernel%29._begin-for-syntax%29%29) form;
- in [require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) and [provide](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._provide%29%29) forms, the default [phase level](syntax-model.html#%28tech._phase._level%29) is greater, which is roughly like wrapping the content of the [require](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) form with [for-syntax](require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-syntax%29%29);
- expression form expr: converted to ([define-values-for-syntax](define.html#%28form._%28%28lib._racket%2Fprivate%2Fdefine..rkt%29._define-values-for-syntax%29%29) () ([begin](#%28form._%28%28quote._~23~25kernel%29._begin%29%29) expr ([values](values.html#%28def._%28%28quote._~23~25kernel%29._values%29%29)))), which effectively evaluates the expression at expansion time and, in the case of a [module context](syntax-model.html#%28tech._module._context%29), preserves the expression for future [visit](syntax-model.html#%28tech._visit%29)s of the module.
See also [module](module.html#%28form._%28%28quote._~23~25kernel%29._module%29%29) for information about expansion order and partial expansion for [begin-for-syntax](#%28form._%28%28quote._~23~25kernel%29._begin-for-syntax%29%29) within a module context. Evaluation of an [expr](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=syntax&rel=Library_Syntax_Classes_and_Literal_Sets.html%23%2528form._%2528%2528lib._syntax%252Fparse..rkt%2529._expr%2529%2529&version=8.18.0.13) within [begin-for-syntax](#%28form._%28%28quote._~23~25kernel%29._begin-for-syntax%29%29) is [parameterize](parameters.html#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._parameterize%29%29)d to set [current-namespace](Namespaces.html#%28def._%28%28quote._~23~25kernel%29._current-namespace%29%29) as in [let-syntax](let.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._let-syntax%29%29).
------------------------------------------------------------------------
# 3.16 Guarded Evaluation: when and unless
### 3.16 Guarded Evaluation: [when](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._when%29%29) and [unless](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._unless%29%29)
> > > <img src="finger.png" width="24" height="24" alt="+" />[Effects If...: when and unless](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=begin.html%23%2528part._when%252Bunless%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 [when](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._when%29%29) and [unless](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._unless%29%29).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([when](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._when%29%29) test-expr body ...+)</p></blockquote></td></tr></tbody></table>
Evaluates test-expr. If the result is #f, then the result of the [when](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._when%29%29) expression is [#<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). Otherwise, the bodys are evaluated, and the last body is in tail position with respect to the [when](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._when%29%29) form.
Examples:
> ```racket
> > ( when ( positive? -5 ) ( display "hi" ) )
> > ( when ( positive? 5 ) ( display "hi" ) ( display " there" ) )
> hi there
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([unless](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._unless%29%29) test-expr body ...+)</p></blockquote></td></tr></tbody></table>
Equivalent to ([when](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._when%29%29) ([not](booleans.html#%28def._%28%28quote._~23~25kernel%29._not%29%29) test-expr) body [...+](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=syntax&rel=stxparse-patterns.html%23%2528form._%2528%2528lib._syntax%252Fparse..rkt%2529._......%252B%2529%2529&version=8.18.0.13)).
Examples:
> ```racket
> > ( unless ( positive? 5 ) ( display "hi" ) )
> > ( unless ( positive? -5 ) ( display "hi" ) ( display " there" ) )
> hi there
> ```
------------------------------------------------------------------------
# 3.17 Assignment: set! and set!-values
### 3.17 Assignment: [set!](#%28form._%28%28quote._~23~25kernel%29._set%21%29%29) and [set!-values](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._set%21-values%29%29)
> > > <img src="finger.png" width="24" height="24" alt="+" />[Assignment: set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=set_.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 [set!](#%28form._%28%28quote._~23~25kernel%29._set%21%29%29).
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([set!](#%28form._%28%28quote._~23~25kernel%29._set%21%29%29) id expr)</p></blockquote></td></tr></tbody></table>
If id has a [transformer](syntax-model.html#%28tech._transformer%29) binding to an [assignment transformer](syntax-model.html#%28tech._assignment._transformer%29), as produced by [make-set!-transformer](stxtrans.html#%28def._%28%28quote._~23~25kernel%29._make-set%21-transformer%29%29) or as an instance of a structure type with the [prop:set!-transformer](stxtrans.html#%28def._%28%28quote._~23~25kernel%29._prop~3aset%21-transformer%29%29) property, then this form is expanded by calling the assignment transformer with the full expressions. If id has a [transformer](syntax-model.html#%28tech._transformer%29) binding to a [rename transformer](syntax-model.html#%28tech._rename._transformer%29) as produced by [make-rename-transformer](stxtrans.html#%28def._%28%28quote._~23~25kernel%29._make-rename-transformer%29%29) or as an instance of a structure type with the [prop:rename-transformer](stxtrans.html#%28def._%28%28quote._~23~25kernel%29._prop~3arename-transformer%29%29) property, then this form is expanded by replacing id with the target identifier (e.g., the one provided to [make-rename-transformer](stxtrans.html#%28def._%28%28quote._~23~25kernel%29._make-rename-transformer%29%29)). If a transformer binding has both [prop:set!-transformer](stxtrans.html#%28def._%28%28quote._~23~25kernel%29._prop~3aset%21-transformer%29%29) and [prop:rename-transformer](stxtrans.html#%28def._%28%28quote._~23~25kernel%29._prop~3arename-transformer%29%29) properties, the latter takes precedence.
Otherwise, evaluates expr and installs the result into the location for id, which must be bound as a local variable or defined as a [top-level variable](eval-model.html#%28tech._top._level._variable%29) or [module-level variable](eval-model.html#%28tech._module._level._variable%29). If id refers to an imported binding, a syntax error is reported. If id refers to a [top-level variable](eval-model.html#%28tech._top._level._variable%29) that has not been defined, the [exn:fail:contract](exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29) exception is raised.
See also [compile-allow-set!-undefined](eval.html#%28def._%28%28quote._~23~25kernel%29._compile-allow-set%21-undefined%29%29).
Examples:
> ```racket
> > ( define x 12 )
> > ( set! x ( add1 x ) )
> > x
> 13
> > ( let ( [ x 5 ] ) ( set! x ( add1 x ) ) x )
> 6
> > ( set! i-am-not-defined 10 )
> set!: assignment disallowed;
> cannot set variable before its definition
> variable: i-am-not-defined
> in module: top-level
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([set!-values](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._set%21-values%29%29) (id ...) expr)</p></blockquote></td></tr></tbody></table>
Assuming that all ids refer to variables, this form evaluates expr, which must produce as many values as supplied ids. The location of each id is filled with the corresponding value from expr in the same way as for [set!](#%28form._%28%28quote._~23~25kernel%29._set%21%29%29).
Example:
> ```racket
> > ( let ( [ a 1 ] [ b 2 ] ) ( set!-values ( a b ) ( values b a ) ) ( list a b ) )
> '(2 1)
> ```
More generally, the [set!-values](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._set%21-values%29%29) form is expanded to
> ```racket
> ( let-values ( [ ( tmp-id ... ) expr ] )
> ( set! id tmp-id ) ... )
> ```
which triggers further expansion if any id has a transformer binding to an [assignment transformer](syntax-model.html#%28tech._assignment._transformer%29).
------------------------------------------------------------------------
# 3.18 Iterations and Comprehensions: for, for/list, ...
### 3.18 Iterations and Comprehensions: [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29), [for/list](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Flist%29%29), ...
> > > <img src="finger.png" width="24" height="24" alt="+" />[Iterations and Comprehensions](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=guide&rel=for.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 iterations and comprehensions.
The [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) iteration forms are based on SRFI-42 \[[SRFI-42](doc-bibliography.html#%28cite._.S.R.F.I-42%29)\].
#### 3.18.1 Iteration and Comprehension Forms
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) (for-clause ...) body-or-break ... body)</p></blockquote></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td style="text-align: right;" data-valign="baseline">for-clause</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">[id seq-expr]</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">[(id ...) seq-expr]</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#:when guard-expr</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#:unless guard-expr</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#:do [do-body ...]</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">break-clause</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#:splice (splicing-id . form)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">break-clause</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#:break guard-expr</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#:final guard-expr</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">body-or-break</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">body</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">break-clause</td></tr></tbody></table></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td> </td><td>seq-expr</td><td> </td><td>:</td><td> </td><td>[sequence?](sequences.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._sequence~3f%29%29)</td></tr></tbody></table></td></tr></tbody></table>
Iteratively evaluates bodys. The for-clauses introduce bindings whose scope includes body and that determine the number of times that body is evaluated. A break-clause either among the for-clauses or bodys stops further iteration.
In the simple case, each for-clause has one of its first two forms, where \[id seq-expr\] is a shorthand for \[(id) seq-expr\]. In this simple case, the seq-exprs are evaluated left-to-right, and each must produce a sequence value (see [Sequences](sequences.html)).
The [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) form iterates by drawing an element from each sequence; if any sequence is empty, then the iteration stops, and [#<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 the result of the [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) expression. Otherwise a location is created for each id to hold the values of each element; the sequence produced by a seq-expr must return as many values for each iteration as corresponding ids.
The ids are then bound in the body, which is evaluated, and whose results are ignored. Iteration continues with the next element in each sequence and with fresh locations for each id.
A [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) form with zero for-clauses is equivalent to a single for-clause that binds an unreferenced id to a sequence containing a single element. All of the ids must be distinct according to [bound-identifier=?](stxcmp.html#%28def._%28%28quote._~23~25kernel%29._bound-identifier~3d~3f%29%29).
If any for-clause has the form #:when guard-expr, then only the preceding clauses (containing no #:when, #:unless, or #:do) determine iteration as above, and the body is effectively wrapped as
> ```racket
> ( when guard-expr
> ( for ( for-clause ... ) body ...+ ) )
> ```
using the remaining for-clauses. A for-clause of the form #:unless guard-expr corresponds to the same transformation with [unless](when_unless.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._unless%29%29) in place of [when](when_unless.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._when%29%29). A for-clause of the form #:do \[do-body [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)\] similarly creates nesting and corresponds to
> ```racket
> ( let ( )
> do-body ...
> ( for ( for-clause ... ) body ...+ ) )
> ```
where the do-body forms may introduce definitions that are visible in the remaining for-clauses.
A #:break guard-expr clause is similar to a #:unless guard-expr clause, but when #:break avoids evaluation of the bodys, it also effectively ends all sequences within the [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) form. A #:final guard-expr clause is similar to #:break guard-expr, but instead of immediately ending sequences and skipping the bodys, it allows at most one more element from each later sequence and at most one more evaluation of the following bodys. Among the bodys, besides stopping the iteration and preventing later body evaluations, a #:break guard-expr or #:final guard-expr clause starts a new internal-definition context.
A #:splice (splicing-id . form) clause is replaced by the sequence of forms that are produced by expanding (splicing-id . form), where splicing-id is bound using [define-splicing-for-clause-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-splicing-for-clause-syntax%29%29). The binding context of that expansion includes previous binding from any clause preceding both the #:splice form and a #:when, #:unless, #:do, #:break, or #:final form. The result of a #:splice expansion can include more #:splice forms to further interleave clause binding and expansion. Support for #:splice clauses is intended less for direct use in source [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) forms than for building new forms that expand to [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29).
In the case of [list](pairs.html#%28tech._list%29) and [stream](streams.html#%28tech._stream%29) sequences, the [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) form itself does not keep each element reachable. If a list or stream produced by a seq-expr is otherwise unreachable, and if the [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) body can no longer reference an id for a list element, then the element is subject to [garbage collection](eval-model.html#%28tech._garbage._collection%29). The [make-do-sequence](sequences.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._make-do-sequence%29%29) sequence constructor supports additional sequences that behave like lists and streams in this way.
If a seq-expr is a quoted literal list, vector, exact integer, string, byte string, immutable hash, or expands to such a literal, then it may be treated as if a sequence transformer such as [in-list](sequences.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._in-list%29%29) was used, unless the seq-expr has a true value for the 'for:no-implicit-optimization syntax property; in most cases this improves performance.
Examples:
> ```racket
> > ( for ( [ i ' ( 1 2 3 ) ] [ j "abc" ] #:when ( odd? i ) [ k # ( #t #f ) ] ) ( display ( list i j k ) ) )
> (1 a #t)(1 a #f)(3 c #t)(3 c #f)
> > ( for ( [ i ' ( 1 2 3 ) ] #:do [ ( define neg-i ( * i -1 ) ) ] [ j ( list neg-i 0 i ) ] ) ( display ( list j ) ) )
> (-1)(0)(1)(-2)(0)(2)(-3)(0)(3)
> > ( for ( [ ( i j ) #hash ( ( "a" . 1 ) ( "b" . 20 ) ) ] ) ( display ( list i j ) ) )
> (a 1)(b 20)
> > ( for ( [ i ' ( 1 2 3 ) ] [ j "abc" ] #:break ( not ( odd? i ) ) [ k # ( #t #f ) ] ) ( display ( list i j k ) ) )
> (1 a #t)(1 a #f)
> > ( for ( [ i ' ( 1 2 3 ) ] [ j "abc" ] #:final ( not ( odd? i ) ) [ k # ( #t #f ) ] ) ( display ( list i j k ) ) )
> (1 a #t)(1 a #f)(2 b #t)
> > ( for ( [ i ' ( 1 2 3 ) ] [ j "abc" ] [ k # ( #t #f ) ] ) #:break ( not ( or ( odd? i ) k ) ) ( display ( list i j k ) ) )
> (1 a #t)
> > ( for ( ) ( display "here" ) )
> here
> > ( for ( [ i ' ( ) ] ) ( error "doesn't get here" ) )
> ```
Changed in version 6.7.0.4 of package base: Added support for the optional second result.
Changed in version 7.8.0.11: Added support for implicit optimization.
Changed in version 8.4.0.2: Added #:do.
Changed in version 8.4.0.3: Added #:splice.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for/list](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Flist%29%29) (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table>
Iterates like [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29), but that the last expression in the bodys must produce a single value, and the result of the [for/list](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Flist%29%29) expression is a list of the results in order. When evaluation of a body is skipped due to a #:when or #:unless clause, the result list includes no corresponding element.
Examples:
> ```racket
> > ( for/list ( [ i ' ( 1 2 3 ) ] [ j "abc" ] #:when ( odd? i ) [ k # ( #t #f ) ] ) ( list i j k ) )
> '((1 #\a #t) (1 #\a #f) (3 #\c #t) (3 #\c #f))
> > ( for/list ( [ i ' ( 1 2 3 ) ] [ j "abc" ] #:break ( not ( odd? i ) ) [ k # ( #t #f ) ] ) ( list i j k ) )
> '((1 #\a #t) (1 #\a #f))
> > ( for/list ( ) ' any )
> '(any)
> > ( for/list ( [ i ' ( ) ] ) ( error "doesn't get here" ) )
> '()
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for/vector](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fvector%29%29) maybe-length (for-clause ...) body-or-break ... body)</p></blockquote></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /></colgroup><tbody><tr><td style="text-align: right;" data-valign="baseline">maybe-length</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"><pre><code></code></pre></td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#:length length-expr</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">#:length length-expr #:fill fill-expr</td></tr></tbody></table></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td> </td><td>length-expr</td><td> </td><td>:</td><td> </td><td>[exact-nonnegative-integer?](number-types.html#%28def._%28%28quote._~23~25kernel%29._exact-nonnegative-integer~3f%29%29)</td></tr></tbody></table></td></tr></tbody></table>
Iterates like [for/list](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Flist%29%29), but results are accumulated into a vector instead of a list.
If the optional #:length clause is specified, the result of length-expr determines the length of the result vector. In that case, the iteration can be performed more efficiently, and it terminates when the vector is full or the requested number of iterations have been performed, whichever comes first. If length-expr specifies a length longer than the number of iterations, then the remaining slots of the vector are initialized to the value of fill-expr, which defaults to 0 (i.e., the default argument of [make-vector](vectors.html#%28def._%28%28quote._~23~25kernel%29._make-vector%29%29)).
Examples:
> ```racket
> > ( for/vector ( [ i ' ( 1 2 3 ) ] ) ( number->string i ) )
> '#("1" "2" "3")
> > ( for/vector #:length 2 ( [ i ' ( 1 2 3 ) ] ) ( number->string i ) )
> '#("1" "2")
> > ( for/vector #:length 4 ( [ i ' ( 1 2 3 ) ] ) ( number->string i ) )
> '#("1" "2" "3" 0)
> > ( for/vector #:length 4 #:fill "?" ( [ i ' ( 1 2 3 ) ] ) ( number->string i ) )
> '#("1" "2" "3" "?")
> ```
The [for/vector](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fvector%29%29) form may allocate a vector and mutate it after each iteration of body, which means that capturing a continuation during body and applying it multiple times may mutate a shared vector.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for/hash](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fhash%29%29) (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for/hasheq](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fhasheq%29%29) (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for/hasheqv](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fhasheqv%29%29) (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table></td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for/hashalw](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fhashalw%29%29) (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table></td></tr></tbody></table>
Like [for/list](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Flist%29%29), but the result is an immutable [hash table](hashtables.html#%28tech._hash._table%29); [for/hash](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fhash%29%29) creates a table using [equal?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) to distinguish keys, [for/hasheq](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fhasheq%29%29) produces a table using [eq?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29), [for/hasheqv](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fhasheqv%29%29) produces a table using [eqv?](Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29), and [for/hashalw](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fhashalw%29%29) produces a table using [equal-always?](Equality.html#%28def._%28%28quote._~23~25kernel%29._equal-always~3f%29%29). The last expression in the bodys must return two values: a key and a value to extend the hash table accumulated by the iteration.
Example:
> ```racket
> > ( for/hash ( [ i ' ( 1 2 3 ) ] ) ( values i ( number->string i ) ) )
> '#hash((1 . "1") (2 . "2") (3 . "3"))
> ```
Changed in version 8.5.0.3 of package base: Added the [for/hashalw](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fhashalw%29%29) form.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for/and](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fand%29%29) (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table>
Iterates like [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29), but when last expression of body produces #f, then iteration terminates, and the result of the [for/and](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fand%29%29) expression is #f. If the body is never evaluated, then the result of the [for/and](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fand%29%29) expression is #t. Otherwise, the result is the (single) result from the last evaluation of body.
Examples:
> ```racket
> > ( for/and ( [ i ' ( 1 2 3 "x" ) ] ) ( i . < . 3 ) )> #f
> > ( for/and ( [ i ' ( 1 2 3 4 ) ] ) i )
> 4
> > ( for/and ( [ i ' ( 1 2 3 4 ) ] ) #:break ( = i 3 ) i )
> 2
> > ( for/and ( [ i ' ( ) ] ) ( error "doesn't get here" ) )
> #t
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for/or](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2For%29%29) (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table>
Iterates like [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29), but when last expression of body produces a value other than #f, then iteration terminates, and the result of the [for/or](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2For%29%29) expression is the same (single) value. If the body is never evaluated, then the result of the [for/or](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2For%29%29) expression is #f. Otherwise, the result is #f.
Examples:
> ```racket
> > ( for/or ( [ i ' ( 1 2 3 "x" ) ] ) ( i . < . 3 ) )> #t
> > ( for/or ( [ i ' ( 1 2 3 4 ) ] ) i )
> 1
> > ( for/or ( [ i ' ( ) ] ) ( error "doesn't get here" ) )
> #f
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for/sum](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fsum%29%29) (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table></td></tr></tbody></table>
Iterates like [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29), but each result of the last body is accumulated into a result with [+](generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._%2B%29%29).
Example:
> ```racket
> > ( for/sum ( [ i ' ( 1 2 3 4 ) ] ) i )
> 10
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>([for/product](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Fproduct%29%29) (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table></td></tr></tbody></table>
Iterates like [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29), but each result of the last body is accumulated into a result with [*](generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._%2A%29%29).
Example:
> ```racket
> > ( for/product ( [ i ' ( 1 2 3 4 ) ] ) i )
> 24
> ```
> ```
syntax
(for/lists (id ... maybe-result)
> ( for-clause ... )
> body-or-break ... body )
maybe-result =
| #:result result-expr
```
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
Similar to for/list, but the last body expression should produce as many values as given ids. The ids are bound to the reversed lists accumulated so far in the for-clauses and bodys.
If a result-expr is provided, it is used as with for/fold when iteration terminates; otherwise, the result is as many lists as supplied ids.
The scope of id bindings is the same as for accumulator identifiers in for/fold. Mutating a id affects the accumulated lists, and mutating it in a way that produces a non-list can cause a final reverse for each id to fail.
Examples:
> ```racket
> > ( for/lists ( l1 l2 l3 ) ( [ i ' ( 1 2 3 ) ] [ j "abc" ] #:when ( odd? i ) [ k # ( #t #f ) ] ) ( values i j k ) )
> '(1 1 3 3) '(#\a #\a #\c #\c) '(#t #f #t #f)
> > ( for/lists ( acc ) ( [ x ' ( tvp tofu seitan tvp tofu ) ] #:unless ( member x acc ) ) x )
> '(tvp tofu seitan)
> > ( for/lists ( firsts seconds #:result ( list firsts seconds ) ) ( [ pr ' ( ( 1 . 2 ) ( 3 . 4 ) ( 5 . 6 ) ) ] ) ( values ( car pr ) ( cdr pr ) ) )
> '((1 3 5) (2 4 6))
> ```
Changed in version 7.1.0.2 of package base: Added the #:result form.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(for/first (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table>
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
Iterates like for, but after body is evaluated the first time, then the iteration terminates, and the for/first result is the (single) result of body. If the body is never evaluated, then the result of the for/first expression is #f.
Examples:
> ```racket
> > ( for/first ( [ i ' ( 1 2 3 "x" ) ] #:when ( even? i ) ) ( number → string i ) )
> "2"
> > ( for/first ( [ i ' ( ) ] ) ( error "doesn't get here" ) )
> #f
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(for/last (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table>
Iterates like for, but the for/last result is the (single) result of the last evaluation of body. If the body is never evaluated, then the result of the for/last expression is #f.
Examples:
> ```racket
> > ( for/last ( [ i ' ( 1 2 3 4 5 ) ] #:when ( even? i ) ) ( number → string i ) )
> "4"
> > ( for/last ( [ i ' ( ) ] ) ( error "doesn't get here" ) )
> #f
> ```
> ```
syntax
(for/fold ([accum-id init-expr] ... maybe-result) (for-clause ...)
> body-or-break ... body )
maybe-result =
| #:result result-expr```
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
Iterates like [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29). Before iteration starts, the init-exprs are evaluated to produce initial accumulator values. At the start of each iteration, a location is generated for each accum-id, and the corresponding current accumulator value is placed into the location. The last expression in body must produce as many values as accum-ids, and those values become the current accumulator values. When iteration terminates, if a result-expr is provided then the result of the [for/fold](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Ffold%29%29) is the result of evaluating result-expr (with accum-ids in scope and bound to their final values), otherwise the results of the [for/fold](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Ffold%29%29) expression are the accumulator values.
Examples:
> ```racket
> > ( for/fold ( [ sum 0 ] [ rev-roots null ] ) ( [ i ' ( 1 2 3 4 ) ] ) ( values ( + sum i ) ( cons ( sqrt i ) rev-roots ) ) )
> 10 '(2 1.7320508075688772 1.4142135623730951 1)
> > ( for/fold ( [ acc ' ( ) ] [ seen ( hash ) ] #:result ( reverse acc ) ) ( [ x ( in-list ' ( 0 1 1 2 3 4 4 4 ) ) ] ) ( cond [ ( hash-ref seen x #f ) ( values acc seen ) ] [ else ( values ( cons x acc ) ( hash-set seen x #t ) ) ] ) )
> '(0 1 2 3 4)
> ```
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
The binding and evaluation order of accum-ids and init-exprs follow the textual, left-to-right order relative to the for-clauses, except that (for historical reasons) accum-ids are not available in the for-clauses for the outermost iteration. The lifetimes of variables are not quite the same as the lexical nesting, however: the variable referenced by a accum-id has a fresh location in each iteration.
Changed in version 6.11.0.1 of package base: Added the #:result form.
Changed in version 8.11.1.3: Changed evaluation order to match textual left-to-right order, including evaluating init-exprs before the first for-clause’s right-hand side and fixing shadowing of accum-id.
> ```
syntax
(for/foldr ([accum-id init-expr] ... accum-option ...)
> ( for-clause ... )
> body-or-break ... body )
accum-option = #:result result-expr
| #:delay
| #:delay-as delayed-id
| #:delay-with delayer-id
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
```
Like for/fold, but analogous to foldr rather than foldl: the given sequences are still iterated in the same order, but the loop body is evaluated in reverse order. Evaluation of a for/foldr expression uses space proportional to the number of iterations it performs, and all elements produced by the given sequences are retained until backwards evaluation of the loop body begins (assuming the element is, in fact, referenced in the body).
Examples:
> ```racket
> > ( define ( in-printing seq ) ( sequence-map ( lambda ( v ) ( println v ) v ) seq ) )
> > ( for/foldr ( [ acc ' ( ) ] ) ( [ v ( in-printing ( in-range 1 4 ) ) ] ) ( println v ) ( cons v acc ) )
> 1 2 3 3 2 1
> '(1 2 3)
> ```
Furthermore, unlike for/fold, the accum-ids are not bound within guard-exprs or body-or-break forms that appear before a break-clause.
While the aforementioned limitations make for/foldr less generally useful than for/fold, for/foldr provides the additional capability to iterate lazily via the #:delay, #:delay-as, and #:delay-with options, which can mitigate many of for/foldr’s disadvantages. If at least one such option is specified, the loop body is given explicit control over when iteration continues: by default, each accum-id is bound to a promise that, when forced, produces the accum-id’s current value.
In this mode, iteration does not continue until one such promise is forced, which triggers any additional iteration necessary to produce a value. If the loop body is lazy in its accum-ids—that is, it returns a value without forcing any of them—then the loop (or any of its iterations) will produce a value before iteration has completely finished. If a reference to at least one such promise is retained, then forcing it will resume iteration from the point at which it was suspended, even if control has left the dynamic extent of the loop body.
Examples:
> ```racket
> > ( for/foldr ( [ acc ' ( ) ] #:delay ) ( [ v ( in-range 1 4 ) ] ) ( printf "- → ~v\n" v ) ( begin0 ( cons v ( force acc ) ) ( printf "<-- ~v\n" v ) ) )> - → 1 - → 2 - → 3 <-- 3 <-- 2 <-- 1> '(1 2 3)
> > ( define resume ( for/foldr ( [ acc ' ( ) ] #:delay ) ( [ v ( in-range 1 5 ) ] ) ( printf "- → ~v\n" v ) ( begin0 ( cond [ ( = v 1 ) ( force acc ) ] [ ( = v 2 ) acc ] [ else ( cons v ( force acc ) ) ] ) ( printf "<-- ~v\n" v ) ) ) )> - → 1 - → 2 <-- 2 <-- 1> > ( force resume )
> - → 3 - → 4 <-- 4 <-- 3> '(3 4)
> ```
This extra control over iteration order allows for/foldr to both consume and construct infinite sequences, so long as it is at least sometimes lazy in its accumulators.
> > > <img src="magnify.png" width="24" height="24" alt="+" />See also for/stream for a more convenient (albeit less flexible) way to lazily transform infinite sequences. (Internally, for/stream is defined in terms of for/foldr.)
Examples:
> ```racket
> > ( define squares ( for/foldr ( [ s empty-stream ] #:delay ) ( [ n ( in-naturals ) ] ) ( stream-cons ( * n n ) ( force s ) ) ) )
> > ( stream → list ( stream-take squares 10 ) )
> '(0 1 4 9 16 25 36 49 64 81)
> ```
The suspension introduced by the #:delay option does not ordinarily affect the loop’s eventual return value, but if #:delay and #:result are combined, the accum-ids will be delayed in the scope of the result-expr in the same way they are delayed within the loop body. This can be used to introduce an additional layer of suspension around the evaluation of the entire loop, if desired.
Examples:
> ```racket
> > ( define evaluated-yet? #f )
> > ( for/foldr ( [ acc ( set! evaluated-yet? #t ) ] #:delay ) ( ) ( force acc ) )
> > evaluated-yet?
> #t
> ```
> ```racket
> > ( define evaluated-yet? #f )
> > ( define start ( for/foldr ( [ acc ( set! evaluated-yet? #t ) ] #:delay #:result acc ) ( ) ( force acc ) ) )
> > evaluated-yet?
> #f
> > ( force start )
> > evaluated-yet?
> #t
> ```
If the #:delay-as option is provided, then delayed-id is bound to an additional promise that returns the values of all accum-ids at once. When multiple accum-ids are provided, forcing this promise can be slightly more efficient than forcing the promises bound to the accum-ids individually.
If the #:delay-with option is provided, the given delayer-id is used to suspend nested iterations (instead of the default, delay). A form of the shape (delayer-id recur-expr) is constructed and placed in expression position, where recur-expr is an expression that, when evaluated, will perform the next iteration and return its result (or results). Sensible choices for delayer-id include lazy, delay/sync, delay/thread, or any of the other promise constructors from racket/promise, as well as thunk from racket/function. However, beware that choices such as thunk or delay/name may evaluate their subexpression multiple times, which can lead to nonsensical results for sequences that have state, as the state will be shared between all evaluations of the recur-expr.
If multiple accum-ids are given, the #:delay-with option is provided, and delayer-id is not bound to one of delay, lazy, delay/strict, delay/sync, delay/thread, or delay/idle, the accum-ids will not be bound at all, even within the loop body. Instead, the #:delay-as option must be specified to access the accumulator values via delayed-id.
Added in version 7.3.0.3 of package base.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(for* (for-clause ...) body-or-break ... body)</p></blockquote></td></tr></tbody></table>
Like for, but with an implicit #:when #t between each pair of for-clauses, so that all sequence iterations are nested.
Example:
> ```racket
> > ( for* ( [ i ' ( 1 2 ) ] [ j "ab" ] ) ( display ( list i j ) ) )
> (1 a)(1 b)(2 a)(2 b)
> ```
> ```
syntax
(for*/list (for-clause ...) body-or-break ... body)
syntax
( for*/lists ( id ... maybe-result ) ( for-clause ... )
> body-or-break ... body )
syntax
(for*/vector maybe-length (for-clause ...) body-or-break ... body)
syntax
(for*/hash (for-clause ...) body-or-break ... body)
syntax
(for*/hasheq (for-clause ...) body-or-break ... body)
syntax
(for*/hasheqv (for-clause ...) body-or-break ... body)
syntax
(for*/hashalw (for-clause ...) body-or-break ... body)
syntax
(for*/and (for-clause ...) body-or-break ... body)
syntax
(for*/or (for-clause ...) body-or-break ... body)
syntax
(for*/sum (for-clause ...) body-or-break ... body)
syntax
(for*/product (for-clause ...) body-or-break ... body)
syntax
(for*/first (for-clause ...) body-or-break ... body)
syntax
(for*/last (for-clause ...) body-or-break ... body)
syntax
( for*/fold ( [ accum-id init-expr ] ... maybe-result ) ( for-clause ... )
> body-or-break ... body )
syntax
( for*/foldr ( [ accum-id init-expr ] ... accum-option ... )
> ( for-clause ... )
> body-or-break ... body )```
Like [for/list](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Flist%29%29), etc., but with the implicit nesting of [for*](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2A%29%29).
Example:
> ```racket
> > ( for*/list ( [ i ' ( 1 2 ) ] [ j "ab" ] ) ( list i j ) )
> '((1 #\a) (1 #\b) (2 #\a) (2 #\b))
> ```
Changed in version 7.3.0.3 of package base: Added the [for*/foldr](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2A%2Ffoldr%29%29) form.
Changed in version 8.5.0.3: Added the [for*/hashalw](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2A%2Fhashalw%29%29) form.
#### 3.18.2 Deriving New Iteration Forms
> ```
syntax
(for/fold/derived orig-datum
> ( [ accum-id init-expr ] ... maybe-result ) ( for-clause ... )
> body-or-break ... body )
```
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
(parameter) →```
Like [for*/fold](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2A%2Ffold%29%29), but the extra orig-datum is used as the source for all syntax errors.
Examples:
> ```racket
> > ( require ( for-syntax syntax/for-body ) syntax/parse/define )
> > ( define-syntax-parse-rule ( for*/digits clauses body ... tail-expr ) #:with original this-syntax #:with ( ( pre-body ... ) ( post-body ... ) ) ( split-for-body this-syntax #' ( body ... tail-expr ) ) ( for*/fold/derived original ( [ n 0 ] [ k 1 ] #:result n ) clauses pre-body ... ( values ( + n ( * ( let ( ) post-body ... ) k ) ) ( * k 10 ) ) ) )
> > ( for*/digits [ ds ( in-list ' ( ( 8 3 ) ( 1 1 ) ) ) ] [ d ( in-list ds ) ] d )
> eval:10:0: for*/digits: bad sequence binding clause
> at: ds
> in: (for*/digits (ds (in-list (quote ((8 3) (1 1))))) (d
> (in-list ds)) d)
> > ( for*/digits ( [ ds ( in-list ' ( ( 8 3 ) ( 1 1 ) ) ) ] [ d ( in-list ds ) ] ) d )
> 1138
> ```
Changed in version 6.11.0.1 of package base: Added the #:result form.
> ```
syntax
(for/foldr/derived orig-datum
> ( [ accum-id init-expr ] ... accum-option ... ) ( for-clause ... )
> body-or-break ... body )
syntax
( for*/foldr/derived orig-datum
> ( [ accum-id init-expr ] ... accum-option ... ) ( for-clause ... )
> body-or-break ... body )
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
```
Like for/foldr and for*/foldr, but the extra orig-datum is used as the source for all syntax errors as in for/fold/derived and for*/fold/derived.
Added in version 7.3.0.3 of package base.
> ```
syntax
( define-sequence-syntax id
> expr-transform-expr
> clause-transform-expr )
expr-transform-expr : ( or/c ( → identifier? )
> ( syntax? . → . syntax? ) )
clause-transform-expr : (syntax? . → . syntax?)```
Defines id as syntax. An (id . rest) form is treated specially when used to generate a sequence in a for-clause of [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) (or one of its variants). In that case, the procedure result of clause-transform-expr is called to transform the clause.
When id is used in any other expression position, the result of expr-transform-expr is used. If it is a procedure of zero arguments, then the result must be an identifier other-id, and any use of id is converted to a use of other-id. Otherwise, expr-transform-expr must produce a procedure (of one argument) that is used as a macro transformer.
When the clause-transform-expr transformer is used, it is given a for-clause as an argument, where the clause’s form is normalized so that the left-hand side is a parenthesized sequence of identifiers. The right-hand side is of the form (id . rest). The result can be either #f, to indicate that the forms should not be treated specially (perhaps because the number of bound identifiers is inconsistent with the (id . rest) form), or a new for-clause to replace the given one. The new clause might use [:do-in](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._~3ado-in%29%29). To protect identifiers in the result of clause-transform-expr, use [for-clause-syntax-protect](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for-clause-syntax-protect%29%29) instead of [syntax-protect](stxcerts.html#%28def._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._syntax-protect%29%29).
Examples:
> ```racket
> > ( define ( check-nat n ) ( unless ( exact-nonnegative-integer? n ) ( raise-argument-error ' in-digits "exact-nonnegative-integer?" n ) ) )
> > ( define-sequence-syntax in-digits ( lambda ( ) #' in-digits/proc ) ( lambda ( stx ) ( syntax-case stx ( ) [ [ ( d ) ( _ nat ) ] #' [ ( d ) ( :do-in ( [ ( n ) nat ] ) ( check-nat n ) ( [ i n ] ) ( not ( zero? i ) ) ( [ ( j d ) ( quotient/remainder i 10 ) ] ) #t #t [ j ] ) ] ] [ _ #f ] ) ) )
> > ( define ( in-digits/proc n ) ( for/list ( [ d ( in-digits n ) ] ) d ) )
> > ( for/list ( [ d ( in-digits 1138 ) ] ) d )
> '(8 3 1 1)
> > ( map in-digits ( list 137 216 ) )
> '((7 3 1) (6 1 2))
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<pre><code>( :do-in ( [ ( outer-id ... ) outer-expr ] ... )
> outer-defn-or-expr
> ( [ loop-id loop-expr ] ... )
> pos-guard
> ( [ ( inner-id ... ) inner-expr ] ... )
> maybe-inner-defn-or-expr
> pre-guard
> post-guard
> ( loop-arg ... ) )</code></pre></blockquote></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /><col style="width: 20%" /></colgroup><tbody><tr><td style="text-align: right;" data-valign="baseline">maybe-inner-defn/expr</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"><pre><code></code></pre></td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">inner-defn-or-expr</td></tr></tbody></table></td></tr></tbody></table>
A form that can only be used as a seq-expr in a for-clause of [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) (or one of its variants).
Within a [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29), the pieces of the [:do-in](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._~3ado-in%29%29) form are spliced into the iteration essentially as follows:
> ```racket
> ( let-values ( [ ( outer-id ... ) outer-expr ] ... )
> outer-defn-or-expr
> ( let loop ( [ loop-id loop-expr ] ... )
> ( if pos-guard
> ( let-values ( [ ( inner-id ... ) inner-expr ] ... )
> inner-defn-or-expr
> ( if pre-guard
> ( let body-bindings
> ( if post-guard
> ( loop loop-arg ... )
> done-expr ) )
> done-expr ) )
> done-expr ) ) )
> ```
where body-bindings and done-expr are from the context of the [:do-in](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._~3ado-in%29%29) use. The identifiers bound by the [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) clause are typically part of the (\[(inner-id [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)) inner-expr\] [...](stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._......%29%29)) section. When inner-defn-or-expr is not provided ([begin](begin.html#%28form._%28%28quote._~23~25kernel%29._begin%29%29)) is used in its place.
Beware that body-bindings and done-expr can contain arbitrary expressions, potentially including [set!](set_.html#%28form._%28%28quote._~23~25kernel%29._set%21%29%29) on outer-id or inner-id identifiers if they are visible in the original [for](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%29%29) form, so beware of depending on such identifiers in post-guard and loop-arg.
The actual loop binding and call has additional loop arguments to support iterations in parallel with the [:do-in](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._~3ado-in%29%29) form, and the other pieces are similarly accompanied by pieces from parallel iterations.
For an example of [:do-in](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._~3ado-in%29%29), see [define-sequence-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-sequence-syntax%29%29).
Changed in version 8.10.0.3 of package base: Added support for non-empty maybe-inner-defn-or-expr.
> ```
(for-clause-syntax-protect stx) → syntax?
stx : syntax?
```
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
Provided for-syntax: Like syntax-protect, just returns its argument.
Changed in version 8.2.0.4 of package base: Changed to just return stx instead of returning “armed” syntax.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(define-splicing-for-clause-syntax id proc-expr)</p></blockquote></td></tr></tbody></table>
Binds id for reference via a #:splice clause in a for form. The proc-expr expression is evaluated in phase level 1, and it must produce a procedure that accepts a syntax object and returns a syntax object.
The procedure’s input is a syntax object that appears after #:splice. The result syntax object must be a parenthesized sequence of forms, and the forms are spliced in place of the #:splice clause in the enclosing for form.
Examples:
> ```racket
> > ( define-splicing-for-clause-syntax cross3 ( lambda ( stx ) ( syntax-case stx ( ) [ ( _ n m ) #' ( [ n ( in-range 3 ) ] #:when #t [ m ( in-range 3 ) ] ) ] ) ) )
> > ( for ( #:splice ( cross3 n m ) ) ( println ( list n m ) ) )
> '(0 0) '(0 1) '(0 2) '(1 0) '(1 1) '(1 2) '(2 0) '(2 1) '(2 2)
> ```
Added in version 8.4.0.3 of package base.
|
#### 3.18.3 Iteration Expansion
| | |
|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
| (require racket/for-clause) | package: base |
The bindings documented in this section are provided by the racket/for-clause library, not racket/base or racket.
> ```
(syntax-local-splicing-for-clause-introduce stx) → syntax?
stx : syntax?```
Analogous to [syntax-local-introduce](stxtrans.html#%28def._%28%28quote._~23~25kernel%29._syntax-local-introduce%29%29), but for use in an expander bound with [define-splicing-for-clause-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-splicing-for-clause-syntax%29%29).
Added in version 8.11.1.4 of package base.
|
#### 3.18.4 Do Loops
> ```
syntax
(do ([id init-expr step-expr-maybe] ...)
> ( stop?-expr finish-expr ... )
> expr ... )
step-expr-maybe =
| step-expr
```
Iteratively evaluates the exprs for as long as stop?-expr returns #f.
To initialize the loop, the init-exprs are evaluated in order and bound to the corresponding ids. The ids are bound in all expressions within the form other than the init-exprs.
After the ids have been bound, the stop?-expr is evaluated. If it produces #f, each expr is evaluated for its side-effect. The ids are then effectively updated with the values of the step-exprs, where the default step-expr for id is just id; more precisely, iteration continues with fresh locations for the ids that are initialized with the values of the corresponding step-exprs.
When stop?-expr produces a true value, then the finish-exprs are evaluated in order, and the last one is evaluated in tail position to produce the overall value for the do form. If no finish-expr is provided, the value of the do form is #<void>.
------------------------------------------------------------------------
# 3.19 Continuation Marks: with-continuation-mark
### 3.19 Continuation Marks: with-continuation-mark
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(with-continuation-mark key-expr val-expr result-expr)</p></blockquote></td></tr></tbody></table>
The key-expr, val-expr, and result-expr expressions are evaluated in order. After key-expr is evaluated to obtain a key and val-expr is evaluated to obtain a value, the key is mapped to the value as a continuation mark in the current continuation’s initial continuation frame. If the frame already has a mark for the key, the mark is replaced. Finally, the result-expr is evaluated; the continuation for evaluating result-expr is the continuation of the with-continuation-mark expression (so the result of the result-expr is the result of the with-continuation-mark expression, and result-expr is in tail position for the with-continuation-mark expression).
> > > <img src="magnify.png" width="24" height="24" alt="+" />Continuation Marks provides more information on continuation marks.
------------------------------------------------------------------------
# 3.20 Quasiquoting: quasiquote, unquote, and unquote-splicing
### 3.20 Quasiquoting: quasiquote, unquote, and unquote-splicing
> > > <img src="finger.png" width="24" height="24" alt="+" />Quasiquoting: quasiquote and ‘ in The Racket Guide introduces quasiquote.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(quasiquote datum)</p></blockquote></td></tr></tbody></table>
The same as 'datum if datum does not include (unquote expr) or (unquote-splicing expr). An (unquote expr) form escapes from the quote, however, and the result of the expr takes the place of the (unquote expr) form in the quasiquote result. An (unquote-splicing expr) similarly escapes, but the expr produces a list whose elements are spliced as multiple values place of the (unquote-splicing expr).
An unquote or unquote-splicing form is recognized in any of the following escaping positions within datum: in a pair, in a vector, in a box, in a prefab structure field after the name position, and in hash table value position (but not in a hash table key position). Such escaping positions can be nested to an arbitrary depth.
An unquote-splicing form must appear as the car of a quoted pair, as an element of a quoted vector, or as an element of a quoted prefab structure. In the case of a pair, if the cdr of the relevant quoted pair is empty, then expr need not produce a list, and its result is used directly in place of the quoted pair (in the same way that append accepts a non-list final argument).
If unquote or unquote-splicing appears within quasiquote in an escaping position but in a way other than as (unquote expr) or (unquote-splicing expr), a syntax error is reported.
Examples:
> ```racket
> > ( quasiquote ( 0 1 2 ) )
> '(0 1 2)
> > ( quasiquote ( 0 ( unquote ( + 1 2 ) ) 4 ) )
> '(0 3 4)
> > ( quasiquote ( 0 ( unquote-splicing ( list 1 2 ) ) 4 ) )
> '(0 1 2 4)
> > ( quasiquote ( 0 ( unquote-splicing 1 ) 4 ) )
> unquote-splicing: contract violation
> expected: list?
> given: 1
> > ( quasiquote ( 0 ( unquote-splicing 1 ) ) )
> '(0 . 1)
> ```
A quasiquote, unquote, or unquote-splicing form is typically abbreviated with \`, ,, or ,@, respectively. See also Reading Quotes.
Examples:
> ```racket
> > ` ( 0 1 2 )
> '(0 1 2)
> > ` ( 1 , ( + 1 2 ) 4 )
> '(1 3 4)
> > ` #s ( stuff 1 , ( + 1 2 ) 4 )
> '#s(stuff 1 3 4)
> > ` #hash ( ( "a" . , ( + 1 2 ) ) )
> '#hash(("a" . 3))
> > ` #hash ( ( , ( + 1 2 ) . "a" ) )
> '#hash((,(+ 1 2) . "a"))
> > ` ( 1 ,@ ( list 1 2 ) 4 )
> '(1 1 2 4)
> > ` # ( 1 ,@ ( list 1 2 ) 4 )
> '#(1 1 2 4)
> ```
A quasiquote form within the original datum increments the level of quasiquotation: within the quasiquote form, each unquote or unquote-splicing is preserved, but a further nested unquote or unquote-splicing escapes. Multiple nestings of quasiquote require multiple nestings of unquote or unquote-splicing to escape.
Examples:
> ```racket
> > ` ( 1 ` , ( + 1 , ( + 2 3 ) ) 4 )
> '(1 `,(+ 1 5) 4)
> > ` ( 1 ` ` ` , ,@ , ,@ ( list ( + 1 2 ) ) 4 )
> '(1 ```,,@,3 4)
> ```
The quasiquote form allocates only as many fresh cons cells, vectors, and boxes as are needed without analyzing unquote and unquote-splicing expressions. For example, in
> \`(,1 2 3)
a single tail '(2 3) is used for every evaluation of the quasiquote expression. When allocating fresh data, the quasiquote form allocates mutable vectors, mutable boxes and immutable hashes.
Examples:
> ```racket
> > ( immutable? ` # ( , 0 ) )
> #f
> > ( immutable? ` #hash ( ( a . , 0 ) ) )
> #t
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>unquote</p></blockquote></td></tr></tbody></table>
See quasiquote, where unquote is recognized as an escape. An unquote form as an expression is a syntax error.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>unquote-splicing</p></blockquote></td></tr></tbody></table>
See quasiquote, where unquote-splicing is recognized as an escape. An unquote-splicing form as an expression is a syntax error.
------------------------------------------------------------------------
# 3.21 Syntax Quoting: quote-syntax
### 3.21 Syntax Quoting: quote-syntax
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(quote-syntax datum)</p></blockquote></td></tr><tr><td>(quote-syntax datum #:local)</td></tr></tbody></table>
Similar to quote, but produces a syntax object that preserves the lexical information and source-location information attached to datum at expansion time.
When #:local is specified, then all scopes in the syntax object’s lexical information are preserved. When #:local is omitted, then the scope sets within datum are pruned to omit the scope for any binding form that appears between the quote-syntax form and the enclosing top-level context, module body, or phase level crossing, whichever is closer.
Unlike syntax (#'), quote-syntax does not substitute pattern variables bound by with-syntax, syntax-parse, or syntax-case.
Examples:
> ```racket
> > ( syntax? ( quote-syntax x ) )
> #t
> > ( quote-syntax ( 1 2 3 ) )
> #<syntax:eval:78:0 (1 2 3)>
> > ( with-syntax ( [ a #' 5 ] ) ( quote-syntax ( a b c ) ) )
> #<syntax:eval:79:0 (a b c)>
> > ( free-identifier=? ( let ( [ x 1 ] ) ( quote-syntax x ) ) ( quote-syntax x ) )
> #t
> > ( free-identifier=? ( let ( [ x 1 ] ) ( quote-syntax x #:local ) ) ( quote-syntax x ) )
> #f
> ```
Changed in version 6.3 of package base: Added scope pruning and support for #:local.
------------------------------------------------------------------------
# 3.22 Interaction Wrapper: #%top-interaction
### 3.22 Interaction Wrapper: #%top-interaction
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(#%top-interaction . form)</p></blockquote></td></tr></tbody></table>
Expands to simply form. The #%top-interaction form is similar to #%app and #%module-begin, in that it provides a hook to control interactive evaluation through load (more precisely, the default load handler) or read-eval-print-loop.
------------------------------------------------------------------------
# 3.23 Blocks: block
### 3.23 Blocks: block
| | |
|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
| (require [racket/block]()) | package: base |
The bindings documented in this section are provided by the [racket/block]() library, not racket/base or racket.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(block defn-or-expr ...)</p></blockquote></td></tr></tbody></table>
Supports a mixture of expressions and mutually recursive definitions, as in a module body. Unlike an internal-definition context, the last defn-or-expr need not be an expression.
The result of the block form is the result of the last defn-or-expr if it is an expression, #<void> otherwise. If no defn-or-expr is provided (after flattening begin forms), the result is #<void>.
The final defn-or-expr is executed in tail position, if it is an expression.
Examples:
> ```racket
> > ( define ( f x ) ( block ( define y ( add1 x ) ) ( displayln y ) ( define z ( * 2 y ) ) ( + 3 z ) ) )
> > ( f 12 )
> 13
> 29
> ```
------------------------------------------------------------------------
# 3.24 Internal-Definition Limiting: #%stratified-body
### 3.24 Internal-Definition Limiting: #%stratified-body
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(#%stratified-body defn-or-expr ...)</p></blockquote></td></tr></tbody></table>
Like (let () defn-or-expr ...) for an internal-definition context sequence, except that an expression is not allowed to precede a definition, and all definitions are treated as referring to all other definitions (i.e., locations for variables are all allocated first, like letrec and unlike letrec-syntaxes+values).
The #%stratified-body form is useful for implementing syntactic forms or languages that supply a more limited kind of internal-definition context.
------------------------------------------------------------------------
# 3.25 Performance Hints: begin-encourage-inline
### 3.25 Performance Hints: begin-encourage-inline
| | |
|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
| (require [racket/performance-hint]()) | package: base |
The bindings documented in this section are provided by the [racket/performance-hint]() library, not racket/base or racket.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(begin-encourage-inline form ...)</p></blockquote></td></tr></tbody></table>
Attaches a 'compiler-hint:cross-module-inline syntax property to each form, which is useful when a form is a function definition. See define-values.
The begin-encourage-inline form is also provided by the (submod racket/performance-hint begin-encourage-inline) module, which has fewer dependencies than [racket/performance-hint]().
Changed in version 6.2 of package base: Added the (submod racket/performance-hint begin-encourage-inline) submodule.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(define-inline id expr)</p></blockquote></td></tr><tr><td>(define-inline (head args) body ...+)</td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td style="text-align: right;" data-valign="baseline">head</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(head args)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">args</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">arg ...</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">arg ... . rest-id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">arg</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">arg-id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">[arg-id default-expr]</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">keyword arg-id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">keyword [arg-id default-expr]</td></tr></tbody></table></td></tr></tbody></table>
Like define, but ensures that the definition will be inlined at its call sites. Recursive calls are not inlined, to avoid infinite inlining. Higher-order uses are supported, but also not inlined. Misapplication (by supplying the wrong number of arguments or incorrect keyword arguments) is also not inlined and left as a run-time error.
The define-inline form may interfere with the Racket compiler’s own inlining heuristics, and should only be used when other inlining attempts (such as begin-encourage-inline) fail.
Changed in version 8.1.0.5 of package base: Changed to treat misapplication as a run-time error.
------------------------------------------------------------------------
# 3.26 Importing Modules Lazily: lazy-require
### 3.26 Importing Modules Lazily: lazy-require
| | |
|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------:|
| (require [racket/lazy-require]()) | package: base |
The bindings documented in this section are provided by the [racket/lazy-require]() library, not racket/base or racket.
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(lazy-require [module-path (fun-import ...)] ...)</p></blockquote></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td style="text-align: right;" data-valign="baseline">fun-import</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">fun-id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(orig-fun-id fun-id)</td></tr></tbody></table></td></tr></tbody></table>
Defines each fun-id as a function that, when called, dynamically requires the export named orig-fun-id from the module specified by module-path and calls it with the same arguments. If orig-fun-id is not given, it defaults to fun-id.
If the enclosing relative phase level is not 0, then module-path is also placed in a submodule (with a use of define-runtime-module-path-index at phase level 0 within the submodule). Introduced submodules have the names lazy-require-auxn-m, where n is a phase-level number and m is a number.
When the use of a lazily-required function triggers module loading, it also triggers a use of register-external-module to declare an indirect compilation dependency (in case the function is used in the process of compiling a module).
Examples:
> ```racket
> > ( lazy-require [ racket/list ( partition ) ] )
> > ( partition even? ' ( 1 2 3 4 5 ) )
> '(2 4) '(1 3 5)
> > ( module hello racket/base ( provide hello ) ( printf "starting hello server\n" ) ( define ( hello ) ( printf "hello!\n" ) ) )
> > ( lazy-require [ ' hello ( [ hello greet ] ) ] )
> > ( greet )
> starting hello server hello!
> ```
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>syntax</p>
<p>(lazy-require-syntax [module-path (macro-import ...)] ...)</p></blockquote></td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td style="text-align: right;" data-valign="baseline">macro-import</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">macro-id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(orig-macro-id macro-id)</td></tr></tbody></table></td></tr></tbody></table>
Like lazy-require but for macros. That is, it defines each macro-id as a macro that, when used, dynamically loads the macro’s implementation from the given module-path. If orig-macro-id is not given, it defaults to macro-id.
Use lazy-require-syntax in the implementation of a library with large, complicated macros to avoid a dependence from clients of the library on the macro “compilers.” Note that only macros with exceptionally large compile-time components (such as Typed Racket, which includes a type checker and optimizer) benefit from lazy-require-syntax; typical macros do not.
Warning: lazy-require-syntax breaks the invariants that Racket’s module loader and linker rely on; these invariants normally ensure that the references in code produced by a macro are loaded before the code runs. Safe use of lazy-require-syntax requires a particular structure in the macro implementation. (In particular, lazy-require-syntax cannot simply be introduced in the client code.) The macro implementation must follow these rules:
1. the interface module must require the runtime-support module
2. the compiler module must require the runtime-support module via an absolute module path rather than a relative path
To explain the concepts of “interface, compiler, and runtime-support modules”, here is an example module that exports a macro:
> ```racket
> ( module original racket/base
> ( define ( ntimes-proc n thunk )
> ( for ( [ i ( in-range n ) ] ) ( thunk ) ) )
> ( define-syntax-rule ( ntimes n expr )
> ( ntimes-proc n ( lambda ( ) expr ) ) )
> ( provide ntimes ) )
> ```
Suppose we want to use lazy-require-syntax to lazily load the implementation of the ntimes macro transformer. The original module must be split into three parts:
> ```racket
> ( module runtime-support racket/base
> ( define ( ntimes-proc n thunk )
> ( for ( [ i ( in-range n ) ] ) ( thunk ) ) )
> ( provide ntimes-proc ) )
> ( module compiler racket/base
> ( require ' runtime-support )
> ( define-syntax-rule ( ntimes n expr )
> ( ntimes-proc n ( lambda ( ) expr ) ) )
> ( provide ntimes ) )
> ( module interface racket/base
> ( require racket/lazy-require )
> ( require ' runtime-support )
> ( lazy-require-syntax [ ' compiler ( ntimes ) ] )
> ( provide ntimes ) )
> ```
The runtime support module contains the function and value definitions that the macro refers to. The compiler module contains the macro definition(s) themselves—the part of the code that “disappears” after compile time. The interface module lazily loads the macro transformer, but it makes sure the runtime support module is defined at run time by requiring it normally. In a larger example, of course, the runtime support and compiler may both consist of multiple modules.
Here what happens when we don’t separate the runtime support into a separate module:
> ```racket
> > ( module bad-no-runtime racket/base ( define ( ntimes-proc n thunk ) ( for ( [ i ( in-range n ) ] ) ( thunk ) ) ) ( define-syntax-rule ( ntimes n expr ) ( ntimes-proc n ( lambda ( ) expr ) ) ) ( provide ntimes ) )
> > ( module bad-client racket/base ( require racket/lazy-require ) ( lazy-require-syntax [ ' bad-no-runtime ( ntimes ) ] ) ( ntimes 3 ( printf "hello?\n" ) ) )
> > ( require ' bad-client )
> require: namespace mismatch;
> reference to a module that is not instantiated
> module: 'bad-no-runtime
> phase: 0
> ```
A similar error occurs when the interface module doesn’t introduce a dependency on the runtime support module.
------------------------------------------------------------------------
# 4 Datatypes
## 4 Datatypes
> > > <img src="finger.png" width="24" height="24" alt="+" />Built-In Datatypes in The Racket Guide introduces Datatypes.
Each pre-defined datatype comes with a set of procedures for manipulating instances of the datatype.
| |
|-------------------------------------------------------------------------------------|
| 4.1 Equality |
| 4.2 Booleans |
| 4.3 Numbers |
| 4.4 Strings |
| 4.5 Byte Strings |
| 4.6 Characters |
| 4.7 Symbols |
| 4.8 Regular Expressions |
| 4.9 Keywords |
| 4.10 Pairs and Lists |
| 4.11 Mutable Pairs and Lists |
| 4.12 Vectors |
| 4.13 Stencil Vectors |
| 4.14 Boxes |
| 4.15 Hash Tables |
| 4.16 Treelists |
| 4.17 Sequences and Streams |
| 4.18 Dictionaries |
| 4.19 Sets |
| 4.20 Procedures |
| 4.21 Void |
| 4.22 Undefined |
------------------------------------------------------------------------
# 4.1 Equality
8.18.0.13
### 4.1 Equality
Equality is the concept of whether two values are “the same.” Racket supports a few different kinds of equality by default, although equal? is preferred for most uses.
> ```
(equal? v1 v2) → boolean?
v1 : any/c
v2 : any/c```
Two values are [equal?](#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) if and only if they are [eqv?](#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29), unless otherwise specified for a particular datatype.
Datatypes with further specification of [equal?](#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) include strings, byte strings, pairs, mutable pairs, vectors, boxes, hash tables, and inspectable structures. In the last six cases, equality is recursively defined; if both v1 and v2 contain reference cycles, they are equal when the infinite unfoldings of the values would be equal. See also [gen:equal+hash](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._gen~3aequal%2Bhash%29%29) and [prop:impersonator-of](chaperones.html#%28def._%28%28quote._~23~25kernel%29._prop~3aimpersonator-of%29%29).
Examples:
> ```racket
> > ( equal? ' yes ' yes )
> #t
> > ( equal? ' yes ' no )
> #f
> > ( equal? ( * 6 7 ) 42 )
> #t
> > ( equal? ( expt 2 100 ) ( expt 2 100 ) )
> #t
> > ( equal? 2 2.0 )
> #f
> > ( let ( [ v ( mcons 1 2 ) ] ) ( equal? v v ) )
> #t
> > ( equal? ( mcons 1 2 ) ( mcons 1 2 ) )
> #t
> > ( equal? ( integer->char 955 ) ( integer->char 955 ) )
> #t
> > ( equal? ( make-string 3 #\z ) ( make-string 3 #\z ) )
> #t
> > ( equal? #t #t )
> #t
> ```
> ```
(equal-always? v1 v2) → boolean?
v1 : any/c
v2 : any/c
```
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
Indicates whether v1 and v2 are equal and will always stay equal independent of mutations. Generally, for two values to be equal-always, corresponding immutable values within v1 and v2 must be equal?, while corresponding mutable values within them must be eq?. Precedents for this operator in other languages include egal \[Baker93\].
Two values v1 and v2 are equal-always? if and only if there exists a third value v3 such that v1 and v2 are both chaperones of v3, meaning (chaperone-of? v1 v3) and (chaperone-of? v2 v3) are both true.
For values that include no chaperones or other impersonators, v1 and v2 can be considered equal-always if they are equal?, except that corresponding mutable vectors, boxes, hash tables, strings, byte strings, mutable pairs, and mutable structures within v1 and v2 must be eq?, and equality on structures can be specialized for equal-always? through gen:equal-mode+hash.
Examples:
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
> ```racket
> > ( equal-always? ' yes ' yes )
> #t
> > ( equal-always? ' yes ' no )
> #f
> > ( equal-always? ( * 6 7 ) 42 )
> #t
> > ( equal-always? ( expt 2 100 ) ( expt 2 100 ) )
> #t
> > ( equal-always? 2 2.0 )
> #f
> > ( equal-always? ( list 1 2 ) ( list 1 2 ) )
> #t
> > ( let ( [ v ( mcons 1 2 ) ] ) ( equal-always? v v ) )
> #t
> > ( equal-always? ( mcons 1 2 ) ( mcons 1 2 ) )
> #f
> > ( equal-always? ( integer → char 955 ) ( integer → char 955 ) )
> #t
> > ( equal-always? ( make-string 3 #\z ) ( make-string 3 #\z ) )
> #f
> > ( equal-always? ( string → immutable-string ( make-string 3 #\z ) ) ( string → immutable-string ( make-string 3 #\z ) ) )
> #t
> > ( equal-always? #t #t )
> #t
> ```
Added in version 8.5.0.3 of package base.
> ```
(eqv? v1 v2) → boolean?
v1 : any/c
v2 : any/c```
Two values are [eqv?](#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29) if and only if they are [eq?](#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29), unless otherwise specified for a particular datatype.
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
The [number](numbers.html#%28tech._number%29) and [character](characters.html#%28tech._character%29) datatypes are the only ones for which [eqv?](#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29) differs from [eq?](#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29). Two numbers are [eqv?](#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29) when they have the same exactness, precision, and are both equal and non-zero, both +0.0, both +0.0f0, both -0.0, both -0.0f0, both +nan.0, or both +nan.f—considering real and imaginary components separately in the case of [complex numbers](numbers.html#%28tech._complex._number%29). Two characters are [eqv?](#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29) when their [char->integer](characters.html#%28def._%28%28quote._~23~25kernel%29._char-~3einteger%29%29) results are equal.
Generally, [eqv?](#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29) is identical to [equal?](#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) except that the former cannot recursively compare the contents of compound data types (such as lists and structs) and cannot be customized by user-defined data types. The use of [eqv?](#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29) is lightly discouraged in favor of [equal?](#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29).
Examples:
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
> ```racket
> > ( eqv? ' yes ' yes )
> #t
> > ( eqv? ' yes ' no )
> #f
> > ( eqv? ( * 6 7 ) 42 )
> #t
> > ( eqv? ( expt 2 100 ) ( expt 2 100 ) )
> #t
> > ( eqv? 2 2.0 )
> #f
> > ( let ( [ v ( mcons 1 2 ) ] ) ( eqv? v v ) )
> #t
> > ( eqv? ( mcons 1 2 ) ( mcons 1 2 ) )
> #f
> > ( eqv? ( integer->char 955 ) ( integer->char 955 ) )
> #t
> > ( eqv? ( make-string 3 #\z ) ( make-string 3 #\z ) )
> #f
> > ( eqv? #t #t )
> #t
> ```
> ```
(eq? v1 v2) → boolean?
v1 : any/c
v2 : any/c
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
```
Return #t if v1 and v2 refer to the same object, #f otherwise. As a special case among numbers, two fixnums that are = are also the same according to eq?. See also Object Identity and Comparisons.
Examples:
> ```racket
> > ( eq? ' yes ' yes )
> #t
> > ( eq? ' yes ' no )
> #f
> > ( eq? ( * 6 7 ) 42 )
> #t
> > ( eq? ( expt 2 100 ) ( expt 2 100 ) )
> #f
> > ( eq? 2 2.0 )
> #f
> > ( let ( [ v ( mcons 1 2 ) ] ) ( eq? v v ) )
> #t
> > ( eq? ( mcons 1 2 ) ( mcons 1 2 ) )
> #f
> > ( eq? ( integer → char 955 ) ( integer → char 955 ) )
> #t
> > ( eq? ( make-string 3 #\z ) ( make-string 3 #\z ) )
> #f
> > ( eq? #t #t )
> #t
> ```
> ```
(equal?/recur v1 v2 recur-proc) → boolean?
v1 : any/c
v2 : any/c
recur-proc : (any/c any/c . → . any/c)```
Like [equal?](#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29), but using recur-proc for recursive comparisons (which means that reference cycles are not handled automatically). Non-#f results from recur-proc are converted to #t before being returned by [equal?/recur](#%28def._%28%28quote._~23~25kernel%29._equal~3f%2Frecur%29%29).
Examples:
> ```racket
> > ( equal?/recur 1 1 ( lambda ( a b ) #f ) )
> #t
> > ( equal?/recur ' ( 1 ) ' ( 1 ) ( lambda ( a b ) #f ) )
> #f
> > ( equal?/recur ' # ( 1 1 1 ) ' # ( 1 1.2 3/4 ) ( lambda ( a b ) ( <= ( abs ( - a b ) ) 0.25 ) ) )> #t
> ```
> ```
(equal-always?/recur v1 v2 recur-proc) → boolean?
v1 : any/c
v2 : any/c
recur-proc : (any/c any/c . → . any/c)
```
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
Like equal-always?, but using recur-proc for recursive comparisons (which means that reference cycles are not handled automatically). Non-#f results from recur-proc are converted to #t before being returned by equal-always?/recur.
Examples:
> ```racket
> > ( equal-always?/recur 1 1 ( lambda ( a b ) #f ) )
> #t
> > ( equal-always?/recur ' ( 1 ) ' ( 1 ) ( lambda ( a b ) #f ) )
> #f
> > ( equal-always?/recur ( vector-immutable 1 1 1 ) ( vector-immutable 1 1.2 3/4 ) ( lambda ( a b ) ( <= ( abs ( - a b ) ) 0.25 ) ) )> #t
> ```
|
#### 4.1.1 Object Identity and Comparisons
The eq? operator compares two values, returning #t when the values refer to the same object. This form of equality is suitable for comparing objects that support imperative update (e.g., to determine that the effect of modifying an object through one reference is visible through another reference). Also, an eq? test evaluates quickly, and eq?-based hashing is more lightweight than equal?-based hashing in hash tables.
In some cases, however, eq? is unsuitable as a comparison operator, because the generation of objects is not clearly defined. In particular, two applications of + to the same two exact integers may or may not produce results that are eq?, although the results are always equal?. Similarly, evaluation of a lambda form typically generates a new procedure object, but it may re-use a procedure object previously generated by the same source lambda form.
The behavior of a datatype with respect to eq? is generally specified with the datatype and its associated procedures.
|
#### 4.1.2 Equality and Hashing
All comparable values have at least one hash code — an arbitrary integer (more specifically a fixnum) computed by applying a hash function to the value. The defining property of these hash codes is that equal values have equal hash codes. Note that the reverse is not true: two unequal values can still have equal hash codes. Hash codes are useful for various indexing and comparison operations, especially in the implementation of hash tables. See Hash Tables for more information.
> ```
(equal-hash-code v) → fixnum?
v : any/c```
Returns a [hash code](#%28tech._hash._code%29) consistent with [equal?](#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29). For any two calls with [equal?](#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29) values, the returned number is the same. A hash code is computed even when v contains a cycle through pairs, vectors, boxes, and/or inspectable structure fields. Additionally, user-defined data types can customize how this hash code is computed by implementing [gen:equal+hash](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._gen~3aequal%2Bhash%29%29) or [gen:equal-mode+hash](#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._gen~3aequal-mode%2Bhash%29%29).
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
For any v that could be produced by [read](Reading.html#%28def._%28%28quote._~23~25kernel%29._read%29%29), if v2 is produced by [read](Reading.html#%28def._%28%28quote._~23~25kernel%29._read%29%29) for the same input characters, the ([equal-hash-code](#%28def._%28%28quote._~23~25kernel%29._equal-hash-code%29%29) v) is the same as ([equal-hash-code](#%28def._%28%28quote._~23~25kernel%29._equal-hash-code%29%29) v2) — even if v and v2 do not exist at the same time (and therefore could not be compared by calling [equal?](#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29)).
Changed in version 6.4.0.12 of package base: Strengthened guarantee for [read](Reading.html#%28def._%28%28quote._~23~25kernel%29._read%29%29)able values.
> ```
(equal-hash-code/recur v recur-proc) → fixnum?
v : any/c
recur-proc : ( → any/c exact-integer?)
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
```
Like equal-hash-code, but using recur-proc for recursive hashing within v.
Examples:
> ```racket
> > ( define ( rational-hash x ) ( cond [ ( rational? x ) ( equal-hash-code ( inexact → exact x ) ) ] [ else ( equal-hash-code/recur x rational-hash ) ] ) )
> > ( = ( rational-hash 0.0 ) ( rational-hash -0 .0 ) )
> #t
> > ( = ( rational-hash 1.0 ) ( rational-hash -1 .0 ) )
> #f
> > ( = ( rational-hash ( list ( list ( list 4.0 0.0 ) 9.0 ) 6.0 ) ) ( rational-hash ( list ( list ( list 4 0 ) 9 ) 6 ) ) )
> #t
> ```
Added in version 8.8.0.9 of package base.
> ```
(equal-secondary-hash-code v) → fixnum?
v : any/c```
Like [equal-hash-code](#%28def._%28%28quote._~23~25kernel%29._equal-hash-code%29%29), but computes a secondary [hash code](#%28tech._hash._code%29) suitable for use in double hashing.
> ```
(equal-always-hash-code v) → fixnum?
v : any/c
```
Returns a hash code consistent with equal-always?. For any two calls with equal-always? values, the returned number is the same.
As equal-always-hash-code traverses v, immutable values within v are hashed with equal-hash-code, while mutable values within v are hashed with eq-hash-code.
> ```
(equal-always-hash-code/recur v recur-proc) → fixnum?
v : any/c
recur-proc : ( → any/c exact-integer?)```
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
Like [equal-always-hash-code](#%28def._%28%28quote._~23~25kernel%29._equal-always-hash-code%29%29), but using recur-proc for recursive hashing within v.
Added in version 8.8.0.9 of package base.
> ```
(equal-always-secondary-hash-code v) → fixnum?
v : any/c
```
Like equal-always-hash-code, but computes a secondary hash code suitable for use in double hashing.
> ```
(eq-hash-code v) → fixnum?
v : any/c```
Returns a [hash code](#%28tech._hash._code%29) consistent with [eq?](#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29). For any two calls with [eq?](#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29) values, the returned number is the same.
> > > Equal [fixnums](numbers.html#%28tech._fixnum%29) are always [eq?](#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29).
> ```
(eqv-hash-code v) → fixnum?
v : any/c
```
Returns a hash code consistent with eqv?. For any two calls with eqv? values, the returned number is the same.
|
#### 4.1.3 Implementing Equality for Custom Types
> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><blockquote>
<p>value</p>
<p>gen:equal+hash : any/c</p></blockquote></td></tr></tbody></table>
A generic interface (see Generic Interfaces) for types that can be compared for equality using equal?. The following methods must be implemented:
- equal-proc : ( → any/c any/c ( → any/c any/c boolean?) any/c) — tests whether the first two arguments are equal, where both values are instances of the structure type to which the generic interface is associated (or a subtype of the structure type).
The third argument is an equal? predicate to use for recursive equality checks; use the given predicate instead of equal? to ensure that data cycles are handled properly and to work with equal?/recur (but beware that an arbitrary function can be provided to equal?/recur for recursive checks, which means that arguments provided to the predicate might be exposed to arbitrary code).
The equal-proc is called for a pair of structures only when they are not eq?, and only when they both have a gen:equal+hash value inherited from the same structure type. With this strategy, the order in which equal? receives two structures does not matter. It also means that, by default, a structure sub-type inherits the equality predicate of its parent, if any.
|
### 3.14 Definitions: [define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29), [define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29), ...
- hash-proc : ( → any/c ( → any/c exact-integer?) exact-integer?) — computes a hash code for the given structure, like equal-hash-code. The first argument is an instance of the structure type (or one of its subtypes) to which the generic interface is associated.
The second argument is an equal-hash-code-like procedure to use for recursive hash-code computation; use the given procedure instead of equal-hash-code to ensure that data cycles are handled properly.
Although the result of hash-proc can be any exact integer, it will be truncated for most purposes to a fixnum (e.g., for the result of equal-hash-code). Roughly, truncation uses bitwise-and to take the lower bits of the number. Thus, variation in the hash-code computation should be reflected in the fixnum-compatible bits of hash-proc’s result. Consumers of a hash code are expected to use variation within the fixnum range appropriately, and producers are not responsible to reflect variation in hash codes across the full range of bits that fit within a fixnum.
- hash2-proc : ( → any/c ( → any/c exact-integer?) exact-integer?) — computes a secondary hash code for the given structure. This procedure is like hash-proc, but analogous to equal-secondary-hash-code.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.