### 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.
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

([and](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29) expr ...)

> > > [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
> ```
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

([or](#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._or%29%29) expr ...)


### 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)
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

([case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29) val-expr case-clause ...)

case-clause	 	=	 	[(datum ...) then-body ...+]

 	 	|	 	[[else](if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._else%29%29) then-body ...+]


### 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.
>
<colgroup><col style="width: 100%" /></colgroup>

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

syntax


### 3.13 Dispatch: [case](#%28form._%28%28lib._racket%2Fprivate%2Fmore-scheme..rkt%29._case%29%29)

([case/equal](#%28form._%28%28lib._racket%2Fcase..rkt%29._case%2Fequal%29%29) val-expr case-clause ...)

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

syntax

([case/equal-always](#%28form._%28%28lib._racket%2Fcase..rkt%29._case%2Fequal-always%29%29) val-expr case-clause ...)

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

syntax

([case/eq](#%28form._%28%28lib._racket%2Fcase..rkt%29._case%2Feq%29%29) val-expr case-clause ...)

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

syntax

([case/eqv](#%28form._%28%28lib._racket%2Fcase..rkt%29._case%2Feqv%29%29) val-expr case-clause ...)


### 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), ...
> > > [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.
>
<colgroup><col style="width: 100%" /></colgroup>

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), ...

([define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29) id expr)

([define](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define%29%29) (head args) body ...+)

head	 	=	 	id

 	 	|	 	(head args)

args	 	=	 	arg ...

 	 	|	 	arg ... . rest-id

arg	 	=	 	arg-id

 	 	|	 	[arg-id default-expr]

 	 	|	 	keyword arg-id

 	 	|	 	keyword [arg-id default-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), ...
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
> ```
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

([define-values](#%28form._%28%28quote._~23~25kernel%29._define-values%29%29) (id ...) expr)

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).
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

([define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29) id expr)

([define-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29) (head args) 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), ...
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)
> ```
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

([define-syntaxes](#%28form._%28%28quote._~23~25kernel%29._define-syntaxes%29%29) (id ...) expr)

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
> ```
>
<colgroup><col style="width: 100%" /></colgroup>

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), ...

([define-for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-for-syntax%29%29) id expr)

([define-for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-for-syntax%29%29) (head args) body ...+)

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)
> ```
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

([define-values-for-syntax](#%28form._%28%28lib._racket%2Fprivate%2Fdefine..rkt%29._define-values-for-syntax%29%29) (id ...) expr)

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).
>
<colgroup><col style="width: 100%" /></colgroup>

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), ...

([define-require-syntax](#%28form._%28%28lib._racket%2Frequire-syntax..rkt%29._define-require-syntax%29%29) id proc-expr)

([define-require-syntax](#%28form._%28%28lib._racket%2Frequire-syntax..rkt%29._define-require-syntax%29%29) (id args ...) body ...+)

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.
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(for/first (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), ...
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
> ```
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(for/last (for-clause ...) body-or-break ... body)

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.
>
<colgroup><col style="width: 100%" /></colgroup>

syntax

(define-splicing-for-clause-syntax id proc-expr)

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.