#### 4.6.4 Named let
A named let is an iteration and recursion form. It uses the same syntactic keyword let as for local binding, but an identifier after the let (instead of an immediate open parenthesis) triggers a different parsing.
> > ```
(let proc-id ([arg-id init-expr] ...)
> > body ...+ )```
A named [let](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.18.0.13) form is equivalent to
> ```racket
> ( ( letrec ( [ proc-id ( lambda ( arg-id ... )
> body ...+ ) ] )
> proc-id )
> init-expr ... )
> ```
That is, a named [let](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.18.0.13) binds a function identifier that is visible only in the function’s body, and it implicitly calls the function with the values of some initial expressions.
Examples:
> ```racket
> ( define ( duplicate pos lst ) ( let dup ( [ i 0 ] [ lst lst ] ) ( cond [ ( = i pos ) ( cons ( car lst ) lst ) ] [ else ( cons ( car lst ) ( dup ( + i 1 ) ( cdr lst ) ) ) ] ) ) )
> > ( duplicate 1 ( list "apple" "cheese burger!" "banana" ) )
> '("apple" "cheese burger!" "cheese burger!" "banana")
> ```


#### 4.6.5 Multiple Values: [let-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._let-values%2529%2529&version=8.18.0.13), [let*-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%252A-values%2529%2529&version=8.18.0.13), [letrec-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._letrec-values%2529%2529&version=8.18.0.13)
> > > <img src="magnify.png" width="24" height="24" alt="+" />[Local Binding: let, let*, letrec, ...](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html&version=8.18.0.13) in [The Racket Reference](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13) also documents multiple-value binding forms.


# 4.6 Local Binding
In the same way that [define-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._define-values%2529%2529&version=8.18.0.13) binds multiple results in a definition (see [Multiple Values and define-values](define.html#%28part._multiple-values%29)), [let-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._let-values%2529%2529&version=8.18.0.13), [let*-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%252A-values%2529%2529&version=8.18.0.13), and [letrec-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._letrec-values%2529%2529&version=8.18.0.13) bind multiple results locally.
> > ```
(let-values ([(id ...) expr] ...)
> > body ...+ )
```
> > ```
(let*-values ([(id ...) expr] ...)
> > body ...+ )```

> > ```
(letrec-values ([(id ...) expr] ...)
> > body ...+ )
```


# 4.6 Local Binding
Each expr must produce as many values as corresponding ids. The binding rules are the same for the forms without -values forms: the ids of let-values are bound only in the bodys, the ids of let*-valuess are bound in exprs of later clauses, and the ids of letrec-values are bound for all exprs.
Example:
> ```racket
> > ( let-values ( [ ( q r ) ( quotient/remainder 14 3 ) ] ) ( list q r ) )
> '(4 2)
> ```
------------------------------------------------------------------------


# 4.7 Conditionals


### 4.7 Conditionals
Most functions used for branching, such as < and string?, produce either #t or #f. Racket’s branching forms, however, treat any value other than #f as true. We say a true value to mean any value other than #f.
This convention for “true value” meshes well with protocols where #f can serve as failure or to indicate that an optional value is not supplied. (Beware of overusing this trick, and remember that an exception is usually a better mechanism to report failure.)
For example, the member function serves double duty; it can be used to find the tail of a list that starts with a particular item, or it can be used to simply check whether an item is present in a list:
> ```racket
> > ( member "Groucho" ' ( "Harpo" "Zeppo" ) )
> #f
> > ( member "Groucho" ' ( "Harpo" "Groucho" "Zeppo" ) )
> '("Groucho" "Zeppo")
> > ( if ( member "Groucho" ' ( "Harpo" "Zeppo" ) ) ' yep ' nope )
> 'nope
> > ( if ( member "Groucho" ' ( "Harpo" "Groucho" "Zeppo" ) ) ' yep ' nope )
> 'yep
> ```


#### 4.7.1 Simple Branching: if
> > > <img src="magnify.png" width="24" height="24" alt="+" />Conditionals: if, cond, and, and or in The Racket Reference also documents if.
In an if form,
> > |  |
> > |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | (if test-expr then-expr else-expr) |
the test-expr is always evaluated. If it produces any value other than #f, then then-expr is evaluated. Otherwise, else-expr is evaluated.
An if form must have both a then-expr and an else-expr; the latter is not optional. To perform (or skip) side-effects based on a test-expr, use when or unless, which we describe later in Sequencing.


#### 4.7.2 Combining Tests: and and or
> > > <img src="magnify.png" width="24" height="24" alt="+" />Conditionals: if, cond, and, and or in The Racket Reference also documents and and or.
Racket’s and and or are syntactic forms, rather than functions. Unlike a function, the and and or forms can skip evaluation of later expressions if an earlier one determines the answer.
> > |  |
> > |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | (and expr ...) |
An and form produces #f if any of its exprs produces #f. Otherwise, it produces the value of its last expr. As a special case, (and) produces #t.
> > |  |
> > |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | (or expr ...) |
The or form produces #f if all of its exprs produce #f. Otherwise, it produces the first non-#f value from its exprs. As a special case, (or) produces #f.
Examples:
> ```racket
> > ( define ( got-milk? lst ) ( and ( not ( null? lst ) ) ( or ( eq? ' milk ( car lst ) ) ( got-milk? ( cdr lst ) ) ) ) ) ; recurs only if needed
> > ( got-milk? ' ( apple banana ) )
> #f
> > ( got-milk? ' ( apple milk banana ) )
> #t
> ```


# 4.7 Conditionals
If evaluation reaches the last expr of an and or or form, then the expr’s value directly determines the and or or result. Therefore, the last expr is in tail position, which means that the above got-milk? function runs in constant space.
> > > <img src="finger.png" width="24" height="24" alt="+" />Tail Recursion introduces tail calls and tail positions.


#### 4.7.3 Chaining Tests: cond
The cond form chains a series of tests to select a result expression. To a first approximation, the syntax of cond is as follows:
> > > <img src="magnify.png" width="24" height="24" alt="+" />Conditionals: if, cond, and, and or in The Racket Reference also documents cond.
> > ```
(cond [test-expr body ...+]
> > ... )```
Each test-expr is evaluated in order. If it produces #f, the corresponding bodys are ignored, and evaluation proceeds to the next test-expr. As soon as a test-expr produces a true value, the associated bodys are evaluated to produce the result for the [cond](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=8.18.0.13) form, and no further test-exprs are evaluated.


# 4.7 Conditionals
The last test-expr in a [cond](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=8.18.0.13) can be replaced by [else](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._else%2529%2529&version=8.18.0.13). In terms of evaluation, [else](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._else%2529%2529&version=8.18.0.13) serves as a synonym for #t, but it clarifies that the last clause is meant to catch all remaining cases. If [else](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._else%2529%2529&version=8.18.0.13) is not used, then it is possible that no test-exprs produce a true value; in that case, the result of the [cond](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=8.18.0.13) expression is [#<void>](void_undefined.html).
Examples:


# 4.7 Conditionals
> ```racket
> > ( cond [ ( = 2 3 ) ( error "wrong!" ) ] [ ( = 2 2 ) ' ok ] )
> 'ok
> > ( cond [ ( = 2 3 ) ( error "wrong!" ) ] )
> > ( cond [ ( = 2 3 ) ( error "wrong!" ) ] [ else ' ok ] )
> 'ok
> ```
> ```
(define (got-milk? lst)
> ( cond
> [ ( null? lst ) #f ]
> [ ( eq? ' milk ( car lst ) ) #t ]
> [ else ( got-milk? ( cdr lst ) ) ] ) )
> ( got-milk? ' ( apple banana ) )
> #f
> > ( got-milk? ' ( apple milk banana ) )
> #t
```
(make-running-total)	→```

#### 4.9.1 Guidelines for Using Assignment

Although using [set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._set%2521%2529%2529&version=8.18.0.13) is sometimes appropriate, Racket style generally discourages the use of [set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._set%2521%2529%2529&version=8.18.0.13). The following guidelines may help explain when using [set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._set%2521%2529%2529&version=8.18.0.13) is appropriate.

-   As in any modern language, assigning to a shared identifier is no substitute for passing an argument to a procedure or getting its result.

    Really awful example:

    > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><pre><code>( define name "unknown" )
    > ( define result "unknown" )
    > ( define ( greet )
    > ( set! result ( string-append "Hello, " name ) ) )</code></pre></td></tr><tr><td><p> </p></td></tr><tr><td><pre><code>> ( set! name "John" )
    > > ( greet )
    > > result
    > "Hello, John"</code></pre></td></tr></tbody></table>

    Ok example:

    > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><pre><code>( define ( greet name )
    > ( string-append "Hello, " name ) )</code></pre></td></tr><tr><td><p> </p></td></tr><tr><td><pre><code>> ( greet "John" )
    > "Hello, John"
    > > ( greet "Anna" )
    > "Hello, Anna"</code></pre></td></tr></tbody></table>

-   A sequence of assignments to a local variable is far inferior to nested bindings.

    Bad example:

    > ```racket
    > > ( let ( [ tree 0 ] ) ( set! tree ( list tree 1 tree ) ) ( set! tree ( list tree 2 tree ) ) ( set! tree ( list tree 3 tree ) ) tree )
    > '(((0 1 0) 2 (0 1 0)) 3 ((0 1 0) 2 (0 1 0)))
    > ```

    Ok example:

    > ```racket
    > > ( let* ( [ tree 0 ] [ tree ( list tree 1 tree ) ] [ tree ( list tree 2 tree ) ] [ tree ( list tree 3 tree ) ] ) tree )
    > '(((0 1 0) 2 (0 1 0)) 3 ((0 1 0) 2 (0 1 0)))
    > ```

-   Using assignment to accumulate results from an iteration is bad style. Accumulating through a loop argument is better.

    Somewhat bad example:

    > ```
(define (sum lst)
> ( let ( [ s 0 ] )
> ( for-each ( lambda ( i ) ( set! s ( + i s ) ) )
> lst )
> s ) )

> ( sum ' ( 1 2 3 ) )
> 6
```


# 4.7 Conditionals
Ok example:
> ```
(define (sum lst)
> ( let loop ( [ lst lst ] [ s 0 ] )
> ( if ( null? lst )
> s
> ( loop ( cdr lst ) ( + s ( car lst ) ) ) ) ) )
> ( sum ' ( 1 2 3 ) )
> 6```
    Better (use an existing function) example:
    >
<colgroup><col style="width: 100%" /></colgroup>

( define ( sum lst )
    > ( apply + lst ) )

> ( sum ' ( 1 2 3 ) )
    > 6

    Good (a general approach) example:
    > ```
(define (sum lst)
> ( for/fold ( [ s 0 ] )
> ( [ i ( in-list lst ) ] )
> ( + s i ) ) )
> ( sum ' ( 1 2 3 ) )
> 6


# 4.7 Conditionals
```
-  For cases where stateful objects are necessary or appropriate, then implementing the object’s state with set! is fine.

Ok example:

> ```
(define next-number!
> ( let ( [ n 0 ] )
> ( lambda ( )
> ( set! n ( add1 n ) )
> n ) ) )

> ( next-number! )
> 1
> > ( next-number! )
> 2
> > ( next-number! )
> 3```

All else being equal, a program that uses no assignments or mutation is always preferable to one that uses assignments or mutation. While side effects are to be avoided, however, they should be used if the resulting code is significantly more readable or if it implements a significantly better algorithm.

The use of mutable values, such as vectors and hash tables, raises fewer suspicions about the style of a program than using [set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._set%2521%2529%2529&version=8.18.0.13) directly. Nevertheless, simply replacing [set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._set%2521%2529%2529&version=8.18.0.13)s in a program with [vector-set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=vectors.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._vector-set%2521%2529%2529&version=8.18.0.13)s obviously does not improve the style of the program.

#### 4.9.2 Multiple Values: [set!-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._set%2521-values%2529%2529&version=8.18.0.13)

> > > <img src="magnify.png" width="24" height="24" alt="+" />[Assignment: set! and set!-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html&version=8.18.0.13) in [The Racket Reference](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13) also documents [set!-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._set%2521-values%2529%2529&version=8.18.0.13).

The [set!-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._set%2521-values%2529%2529&version=8.18.0.13) form assigns to multiple variables at once, given an expression that produces an appropriate number of values:

> > |                                                                                |
> > |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | ([set!-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._set%2521-values%2529%2529&version=8.18.0.13) (id ...) expr) |

This form is equivalent to using [let-values](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._let-values%2529%2529&version=8.18.0.13) to receive multiple results from expr, and then assigning the results individually to the ids using [set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._set%2521%2529%2529&version=8.18.0.13).

Examples:

> ```racket
> ( define game ( let ( [ w 0 ] [ l 0 ] ) ( lambda ( win? ) ( if win? ( set! w ( + w 1 ) ) ( set! l ( + l 1 ) ) ) ( begin0 ( values w l ) ; swap sides... ( set!-values ( w l ) ( values l w ) ) ) ) ) )
> > ( game #t )
> 1 0
> > ( game #t )
> 1 1
> > ( game #f )
> 1 2
> ```

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

# 4.10 Quoting: quote and '

### 4.10 Quoting: [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) and ’

> > > <img src="magnify.png" width="24" height="24" alt="+" />[Literals: quote and #%datum](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quote.html&version=8.18.0.13) in [The Racket Reference](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13) also documents [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).

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) form produces a constant:

> > |               |
> > |---------------|
> > | (quote datum) |

The syntax of a datum is technically specified as anything that the [read](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.18.0.13) function parses as a single element. The value of 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) form is the same value that [read](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=Reading.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._read%2529%2529&version=8.18.0.13) would produce given datum.

The datum can be a symbol, a boolean, a number, a (character or byte) string, a character, a keyword, an empty list, a pair (or list) containing more such values, a vector containing more such values, a hash table containing more such values, or a box containing another such value.

Examples:

> ```racket
> > ( quote apple )
> 'apple
> > ( quote #t )
> #t
> > ( quote 42 )
> 42
> > ( quote "hello" )
> "hello"
> > ( quote ( ) )
> '()
> > ( quote ( ( 1 2 3 ) # ( "z" x ) . the-end ) )
> '((1 2 3) #("z" x) . the-end)
> > ( quote ( 1 2 . ( 3 ) ) )
> '(1 2 3)
> ```

As the last example above shows, the datum does not have to match the normalized printed form of a value. A datum cannot be a printed representation that starts with #\<, so it cannot be [#<void>](void_undefined.html), [#<undefined>](void_undefined.html), or a procedure.

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) form is rarely used for a datum that is a boolean, number, or string by itself, since the printed forms of those values can already be used as constants. 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) form is more typically used for symbols and lists, which have other meanings (identifiers, function calls, etc.) when not quoted.

An expression

> > |        |
> > |--------|
> > | 'datum |

is a shorthand for

> (quote datum)

and this shorthand is almost always used instead of [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). The shorthand applies even within the datum, so it can produce a list containing [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).

> > > <img src="magnify.png" width="24" height="24" alt="+" />[Reading Quotes](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=reader.html%23%2528part._parse-quote%2529&version=8.18.0.13) in [The Racket Reference](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13) provides more on the ' shorthand.

Examples:

> ```racket
> > ' apple
> 'apple
> > ' "hello"
> "hello"
> > ' ( 1 2 3 )
> '(1 2 3)
> > ( display ' ( you can ' me ) )
> (you can (quote me))
> ```

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

# 4.11 Quasiquoting: quasiquote and lsquo

### 4.11 Quasiquoting: [quasiquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._quasiquote%2529%2529&version=8.18.0.13) and ‘

> > > <img src="magnify.png" width="24" height="24" alt="+" />[Quasiquoting: quasiquote, unquote, and unquote-splicing](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html&version=8.18.0.13) in [The Racket Reference](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13) also documents [quasiquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._quasiquote%2529%2529&version=8.18.0.13).

The [quasiquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._quasiquote%2529%2529&version=8.18.0.13) form is similar to [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):

> > |                                                                                |
> > |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | ([quasiquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._quasiquote%2529%2529&version=8.18.0.13) datum) |

However, for each ([unquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._unquote%2529%2529&version=8.18.0.13) expr) that appears within the datum, the expr is evaluated to produce a value that takes the place of the [unquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._unquote%2529%2529&version=8.18.0.13) sub-form.

Example:

> ```racket
> > ( quasiquote ( 1 2 ( unquote ( + 1 2 ) ) ( unquote ( - 5 1 ) ) ) )
> '(1 2 3 4)
> ```

This form can be used to write functions that build lists according to certain patterns.

Examples:

> ```racket
> > ( define ( deep n ) ( cond [ ( zero? n ) 0 ] [ else ( quasiquote ( ( unquote n ) ( unquote ( deep ( - n 1 ) ) ) ) ) ] ) )
> > ( deep 8 )
> '(8 (7 (6 (5 (4 (3 (2 (1 0))))))))
> ```

Or even to cheaply construct expressions programmatically. (Of course, 9 times out of 10, you should be using a [macro](macros.html) to do this (the 10th time being when you’re working through a textbook like [PLAI](https://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/)).)

Examples:

> ```racket
> > ( define ( build-exp n ) ( add-lets n ( make-sum n ) ) )
> > ( define ( add-lets n body ) ( cond [ ( zero? n ) body ] [ else ( quasiquote ( let ( [ ( unquote ( n->var n ) ) ( unquote n ) ] ) ( unquote ( add-lets ( - n 1 ) body ) ) ) ) ] ) )
> > ( define ( make-sum n ) ( cond [ ( = n 1 ) ( n->var 1 ) ] [ else ( quasiquote ( + ( unquote ( n->var n ) ) ( unquote ( make-sum ( - n 1 ) ) ) ) ) ] ) )
> > ( define ( n->var n ) ( string->symbol ( format "x~a" n ) ) )
> > ( build-exp 3 )
> '(let ((x3 3)) (let ((x2 2)) (let ((x1 1)) (+ x3 (+ x2 x1)))))
> ```

The [unquote-splicing](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._unquote-splicing%2529%2529&version=8.18.0.13) form is similar to [unquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._unquote%2529%2529&version=8.18.0.13), but its expr must produce a list, and the [unquote-splicing](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._unquote-splicing%2529%2529&version=8.18.0.13) form must appear in a context that produces either a list or a vector. As the name suggests, the resulting list is spliced into the context of its use.

Example:

> ```racket
> > ( quasiquote ( 1 2 ( unquote-splicing ( list ( + 1 2 ) ( - 5 1 ) ) ) 5 ) )
> '(1 2 3 4 5)
> ```

Using splicing we can revise the construction of our example expressions above to have just a single [let](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.18.0.13) expression and a single [+](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) expression.

Examples:

> ```racket
> > ( define ( build-exp n ) ( add-lets n ( quasiquote ( + ( unquote-splicing ( build-list n ( λ ( x ) ( n->var ( + x 1 ) ) ) ) ) ) ) ) )
> > ( define ( add-lets n body ) ( quasiquote ( let ( unquote ( build-list n ( λ ( n ) ( quasiquote [ ( unquote ( n->var ( + n 1 ) ) ) ( unquote ( + n 1 ) ) ] ) ) ) ) ( unquote body ) ) ) )
> > ( define ( n->var n ) ( string->symbol ( format "x~a" n ) ) )
> > ( build-exp 3 )
> '(let ((x1 1) (x2 2) (x3 3)) (+ x1 x2 x3))
> ```

If a [quasiquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._quasiquote%2529%2529&version=8.18.0.13) form appears within an enclosing [quasiquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._quasiquote%2529%2529&version=8.18.0.13) form, then the inner [quasiquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._quasiquote%2529%2529&version=8.18.0.13) effectively cancels one layer of [unquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._unquote%2529%2529&version=8.18.0.13) and [unquote-splicing](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._unquote-splicing%2529%2529&version=8.18.0.13) forms, so that a second [unquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._unquote%2529%2529&version=8.18.0.13) or [unquote-splicing](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._unquote-splicing%2529%2529&version=8.18.0.13) is needed.

Examples:

> ```racket
> > ( quasiquote ( 1 2 ( quasiquote ( unquote ( + 1 2 ) ) ) ) )
> '(1 2 (quasiquote (unquote (+ 1 2))))
> > ( quasiquote ( 1 2 ( quasiquote ( unquote ( unquote ( + 1 2 ) ) ) ) ) )
> '(1 2 (quasiquote (unquote 3)))
> > ( quasiquote ( 1 2 ( quasiquote ( ( unquote ( + 1 2 ) ) ( unquote ( unquote ( - 5 1 ) ) ) ) ) ) )
> '(1 2 (quasiquote ((unquote (+ 1 2)) (unquote 4))))
> ```

The evaluations above will not actually print as shown. Instead, the shorthand form of [quasiquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._quasiquote%2529%2529&version=8.18.0.13) and [unquote](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._unquote%2529%2529&version=8.18.0.13) will be used: \` (i.e., a backquote) and , (i.e., a comma). The same shorthands can be used in expressions:

Example:

> ```racket
> > ` ( 1 2 ` ( , ( + 1 2 ) , , ( - 5 1 ) ) )
> '(1 2 `(,(+ 1 2) ,4))
> ```

The shorthand form of [unquote-splicing](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=quasiquote.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._unquote-splicing%2529%2529&version=8.18.0.13) is ,@:

Example:

> ```racket
> > ` ( 1 2 ,@ ( list ( + 1 2 ) ( - 5 1 ) ) )
> '(1 2 3 4)
> ```

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

# 4.12 Simple Dispatch: case

### 4.12 Simple Dispatch: [case](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=case.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._case%2529%2529&version=8.18.0.13)

The [case](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=case.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._case%2529%2529&version=8.18.0.13) form dispatches to a clause by matching the result of an expression to the values for the clause:

> > ```
(case expr
> > [ ( datum ...+ ) body ...+ ]
> > ... )
```


# 4.7 Conditionals
(parameter)	→```
> > > The term “parameter” is sometimes used to refer to the arguments of a function, but “parameter” in Racket has the more specific meaning described here.
For example, the [error-print-width](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=exns.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._error-print-width%2529%2529&version=8.18.0.13) parameter controls how many characters of a value are printed in an error message:
> ```racket
> > ( parameterize ( [ error-print-width 5 ] ) ( car ( expt 10 1024 ) ) )
> car: contract violation
> expected: pair?
> given: 10...
> > ( parameterize ( [ error-print-width 10 ] ) ( car ( expt 10 1024 ) ) )
> car: contract violation
> expected: pair?
> given: 1000000...
> ```
More generally, parameters implement a kind of dynamic binding. The [make-parameter](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._make-parameter%2529%2529&version=8.18.0.13) function takes any value and returns a new parameter that is initialized to the given value. Applying the parameter as a function returns its current value:
> ```racket
> > ( define location ( make-parameter "here" ) )
> > ( location )
> "here"
> ```


# 4.7 Conditionals
In a [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) form, each parameter-expr must produce a parameter. During the evaluation of the bodys, each specified parameter is given the result of the corresponding value-expr. When control leaves the [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) form—either through a normal return, an exception, or some other escape—the parameter reverts to its earlier value:
> ```racket
> > ( parameterize ( [ location "there" ] ) ( location ) )
> "there"
> > ( location )
> "here"
> > ( parameterize ( [ location "in a house" ] ) ( list ( location ) ( parameterize ( [ location "with a mouse" ] ) ( location ) ) ( location ) ) )
> '("in a house" "with a mouse" "in a house")
> > ( parameterize ( [ location "in a box" ] ) ( car ( location ) ) )
> car: contract violation
> expected: pair?
> given: "in a box"
> > ( location )
> "here"
> ```


# 4.7 Conditionals
The [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) form is not a binding form like [let](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.18.0.13); each use of location above refers directly to the original definition. A [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) form adjusts the value of a parameter during the whole time that the [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) body is evaluated, even for uses of the parameter that are textually outside of the [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) body:


# 4.7 Conditionals
> ```racket
> > ( define ( would-you-could-you? ) ( and ( not ( equal? ( location ) "here" ) ) ( not ( equal? ( location ) "there" ) ) ) )
> > ( would-you-could-you? )
> #f
> > ( parameterize ( [ location "on a bus" ] ) ( would-you-could-you? ) )
> #t
> ```
If a use of a parameter is textually inside the body of a [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) but not evaluated before the [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) form produces a value, then the use does not see the value installed by the [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) form:
> ```racket
> > ( let ( [ get ( parameterize ( [ location "with a fox" ] ) ( lambda ( ) ( location ) ) ) ] ) ( get ) )
> "here"
> ```


# 4.7 Conditionals
The current binding of a parameter can be adjusted imperatively by calling the parameter as a function with a value. If a [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) has adjusted the value of the parameter, then directly applying the parameter procedure affects only the value associated with the active [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13):
> ```racket
> > ( define ( try-again! where ) ( location where ) )
> > ( location )
> "here"
> > ( parameterize ( [ location "on a train" ] ) ( list ( location ) ( begin ( try-again! "in a boat" ) ( location ) ) ) )
> '("on a train" "in a boat")
> > ( location )
> "here"
> ```


# 4.7 Conditionals
Using [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) is generally preferable to updating a parameter value imperatively—for much the same reasons that binding a fresh variable with [let](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.18.0.13) is preferable to using [set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._set%2521%2529%2529&version=8.18.0.13) (see [Assignment: set!](set_.html)).
It may seem that variables and [set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._set%2521%2529%2529&version=8.18.0.13) can solve many of the same problems that parameters solve. For example, lokation could be defined as a string, and [set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._set%2521%2529%2529&version=8.18.0.13) could be used to adjust its value:


# 4.7 Conditionals
> ```racket
> > ( define lokation "here" )
> > ( define ( would-ya-could-ya? ) ( and ( not ( equal? lokation "here" ) ) ( not ( equal? lokation "there" ) ) ) )
> > ( set! lokation "on a bus" )
> > ( would-ya-could-ya? )
> #t
> ```
Parameters, however, offer several crucial advantages over [set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._set%2521%2529%2529&version=8.18.0.13):
-   The [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) form helps automatically reset the value of a parameter when control escapes due to an exception. Adding exception handlers and other forms to rewind a [set!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=set_.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._set%2521%2529%2529&version=8.18.0.13) is relatively tedious.


# 4.7 Conditionals
-   Parameters work nicely with tail calls (see [Tail Recursion](Lists__Iteration__and_Recursion.html#%28part._tail-recursion%29)). The last body in a [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) form is in [tail position](Lists__Iteration__and_Recursion.html#%28tech._tail._position%29) with respect to the [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) form.
-   Parameters work properly with threads (see [Threads](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=threads.html&version=8.18.0.13)). The [parameterize](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=parameters.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fmore-scheme..rkt%2529._parameterize%2529%2529&version=8.18.0.13) form adjusts the value of a parameter only for evaluation in the current thread, which avoids race conditions with other threads.
------------------------------------------------------------------------


# 5 Programmer-Defined Datatypes


## 5 Programmer-Defined Datatypes
> > > <img src="magnify.png" width="24" height="24" alt="+" />[Structures](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=structures.html&version=8.18.0.13) in [The Racket Reference](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13) also documents structure types.
New datatypes are normally created with the [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) form, which is the topic of this chapter. The class-based object system, which we defer to [Classes and Objects](classes.html), offers an alternate mechanism for creating new datatypes, but even classes and objects are implemented in terms of structure types.


### 5.1 Simple Structure Types: [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13)
> > > <img src="magnify.png" width="24" height="24" alt="+" />[Defining Structure Types: struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html&version=8.18.0.13) in [The Racket Reference](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13) also documents [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13).
To a first approximation, the syntax of [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) is


### 5.1 Simple Structure Types: [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13)
> > |                                                                                |
> > |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | ([struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) struct-id (field-id ...)) |
Examples:
> ([struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) posn (x y))


### 5.1 Simple Structure Types: [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13)
The [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) form binds struct-id and a number of identifiers that are built from struct-id and the field-ids:
-   struct-id : a constructor function that takes as many arguments as the number of field-ids, and returns an instance of the structure type.
    Example:
    > ```racket
    > > ( posn 1 2 )
    > #<posn>
    > ```
-   struct-id? : a predicate function that takes a single argument and returns #t if it is an instance of the structure type, #f otherwise.
    Examples:
    > ```racket
    > > ( posn? 3 )
    > #f
    > > ( posn? ( posn 1 2 ) )
    > #t
    > ```
-   struct-id-field-id : for each field-id, an accessor that extracts the value of the corresponding field from an instance of the structure type.
    Examples:
    > ```racket
    > > ( posn-x ( posn 1 2 ) )
    > 1
    > > ( posn-y ( posn 1 2 ) )
    > 2
    > ```
-   struct:struct-id : a structure type descriptor, which is a value that represents the structure type as a first-class value (with #:super, as discussed later in [More Structure Type Options](#%28part._struct-options%29)).


### 5.1 Simple Structure Types: [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13)
A [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) form places no constraints on the kinds of values that can appear for fields in an instance of the structure type. For example, (posn "apple" #f) produces an instance of posn, even though "apple" and #f are not valid coordinates for the obvious uses of posn instances. Enforcing constraints on field values, such as requiring them to be numbers, is normally the job of a contract, as discussed later in [Contracts](contracts.html).


### 5.2 Copying and Update
The [struct-copy](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=struct-copy.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct-copy%2529%2529&version=8.18.0.13) form clones a structure and optionally updates specified fields in the clone. This process is sometimes called a functional update, because the result is a structure with updated field values. but the original structure is not modified.
> > |                                                                                |
> > |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | ([struct-copy](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=struct-copy.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct-copy%2529%2529&version=8.18.0.13) struct-id struct-expr \[field-id expr\] ...) |


### 5.2 Copying and Update
The struct-id that appears after [struct-copy](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=struct-copy.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct-copy%2529%2529&version=8.18.0.13) must be a structure type name bound by [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13). The struct-expr must produce an instance of the structure type. The result is a new instance of the structure type that is like the old one, except that the field indicated by each field-id gets the value of the corresponding expr.
Examples:
> ```racket
> > ( define p1 ( posn 1 2 ) )
> > ( define p2 ( struct-copy posn p1 [ x 3 ] ) )
> > ( list ( posn-x p2 ) ( posn-y p2 ) )
> '(3 2)
> > ( list ( posn-x p1 ) ( posn-y p1 ) )
> '(1 2)
> ```


### 5.3 Structure Subtypes
An extended form of [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) can be used to define a structure subtype, which is a structure type that extends an existing structure type:
> > |                                                                                |
> > |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | ([struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) struct-id super-id (field-id ...)) |


### 5.3 Structure Subtypes
The super-id must be a structure type name bound by [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) (i.e., the name that cannot be used directly as an expression).
Examples:
> ```racket
> ( struct posn ( x y ) )
> ( struct 3d-posn posn ( z ) )
> ```
A structure subtype inherits the fields of its supertype, and the subtype constructor accepts the values for the subtype fields after values for the supertype fields. An instance of a structure subtype can be used with the predicate and accessors of the supertype.
Examples:
> ```racket
> > ( define p ( 3d-posn 1 2 3 ) )
> > p
> #<3d-posn>
> > ( posn? p )
> #t
> > ( 3d-posn-z p )
> 3
> ; a 3d-posn has an x field, but there is no 3d-posn-x selector:
> > ( 3d-posn-x p )
> 3d-posn-x: undefined;
> cannot reference an identifier before its definition
> in module: top-level
> ; use the supertype's posn-x selector to access the x field:
> > ( posn-x p )
> 1
> ```


### 5.4 Opaque versus Transparent Structure Types
With a structure type definition like
> ([struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) posn (x y))
an instance of the structure type prints in a way that does not show any information about the fields’ values. That is, structure types by default are opaque. If the accessors and mutators of a structure type are kept private to a module, then no other module can rely on the representation of the type’s instances.
To make a structure type transparent, use the #:transparent keyword after the field-name sequence:
>
<colgroup><col style="width: 100%" /></colgroup>

( struct posn ( x y )
> #:transparent )

> ( posn 1 2 )
> (posn 1 2)


### 5.4 Opaque versus Transparent Structure Types
An instance of a transparent structure type prints like a call to the constructor, so that it shows the structures field values. A transparent structure type also allows reflective operations, such as [struct?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=structutils.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._struct%7E3f%2529%2529&version=8.18.0.13) and [struct-info](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=inspectors.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._struct-info%2529%2529&version=8.18.0.13), to be used on its instances (see [Reflection and Dynamic Evaluation](reflection.html)).
Structure types are opaque by default, because opaque structure instances provide more encapsulation guarantees. That is, a library can use an opaque structure to encapsulate data, and clients of the library cannot manipulate the data in the structure except as allowed by the library.


### 5.5 Structure Comparisons
A generic [equal?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=Equality.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._equal%7E3f%2529%2529&version=8.18.0.13) comparison automatically recurs on the fields of a transparent structure type, but [equal?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=Equality.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._equal%7E3f%2529%2529&version=8.18.0.13) defaults to mere instance identity for opaque structure types:
>
<colgroup><col style="width: 100%" /></colgroup>

([struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) glass (width height) #:transparent)

> ( equal? ( glass 1 2 ) ( glass 1 2 ) )
> #t


### 5.5 Structure Comparisons
>
<colgroup><col style="width: 100%" /></colgroup>

([struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) lead (width height))

> ( define slab ( lead 1 2 ) )
> > ( equal? slab slab )
> #t
> > ( equal? slab ( lead 1 2 ) )
> #f

To support instances comparisons via [equal?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=Equality.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._equal%7E3f%2529%2529&version=8.18.0.13) without making the structure type transparent, you can use the #:methods keyword, [gen:equal+hash](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=Equality.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._gen%7E3aequal%252Bhash%2529%2529&version=8.18.0.13), and implement three methods:
> ```
(struct lead (width height)


### 5.5 Structure Comparisons
> #:methods
> gen:equal+hash
> [ ( define ( equal-proc a b equal?-recur )
> ; compare a and b
> ( and ( equal?-recur ( lead-width a ) ( lead-width b ) )
> ( equal?-recur ( lead-height a ) ( lead-height b ) ) ) )
> ( define ( hash-proc a hash-recur )
> ; compute primary hash code of a
> ( + ( hash-recur ( lead-width a ) )
> ( * 3 ( hash-recur ( lead-height a ) ) ) ) )
> ( define ( hash2-proc a hash2-recur )
> ; compute secondary hash code of a
> ( + ( hash2-recur ( lead-width a ) )
> ( hash2-recur ( lead-height a ) ) ) ) ] )
> ( equal? ( lead 1 2 ) ( lead 1 2 ) )
> #t


### 5.5 Structure Comparisons
```
The first function in the list implements the equal? test on two leads; the third argument to the function is used instead of equal? for recursive equality testing, so that data cycles can be handled correctly. The other two functions compute primary and secondary hash codes for use with hash tables:

> ```racket
> > ( define h ( make-hash ) )
> > ( hash-set! h ( lead 1 2 ) 3 )
> > ( hash-ref h ( lead 1 2 ) )
> 3
> > ( hash-ref h ( lead 2 1 ) )
> hash-ref: no value found for key
> key: #<lead>
> ```

The first function provided with gen:equal+hash is not required to recursively compare the fields of the structure. For example, a structure type representing a set might implement equality by checking that the members of the set are the same, independent of the order of elements in the internal representation. Just take care that the hash functions produce the same value for any two structure types that are supposed to be equivalent.

### 5.6 Structure Type Generativity

Each time that a struct form is evaluated, it generates a structure type that is distinct from all existing structure types, even if some other structure type has the same name and fields.

This generativity is useful for enforcing abstractions and implementing programs such as interpreters, but beware of placing a struct form in positions that are evaluated multiple times.

Examples:

> ```racket
> ( define ( add-bigger-fish lst ) ( struct fish ( size ) #:transparent ) ; new every time ( cond [ ( null? lst ) ( list ( fish 1 ) ) ] [ else ( cons ( fish ( * 2 ( fish-size ( car lst ) ) ) ) lst ) ] ) )
> > ( add-bigger-fish null )
> (list (fish 1))
> > ( add-bigger-fish ( add-bigger-fish null ) )
> fish-size: contract violation
> expected: fish?
> given: (fish 1)
> ```

> ```
(struct fish (size) #:transparent)
> ( define ( add-bigger-fish lst )
> ( cond
> [ ( null? lst ) ( list ( fish 1 ) ) ]
> [ else ( cons ( fish ( * 2 ( fish-size ( car lst ) ) ) )
> lst ) ] ) )

> ( add-bigger-fish ( add-bigger-fish null ) )
> (list (fish 2) (fish 1))```

### 5.7 Prefab Structure Types

Although a [transparent](#%28tech._transparent%29) structure type prints in a way that shows its content, the printed form of the structure cannot be used in an expression to get the structure back, unlike the printed form of a number, string, symbol, or list.

A prefab (“previously fabricated”) structure type is a built-in type that is known to the Racket printer and expression reader. Infinitely many such types exist, and they are indexed by name, field count, supertype, and other such details. The printed form of a prefab structure is similar to a vector, but it starts #s instead of just #, and the first element in the printed form is the prefab structure type’s name.

The following examples show instances of the sprout prefab structure type that has one field. The first instance has a field value 'bean, and the second has field value 'alfalfa:

> ```racket
> > ' #s ( sprout bean )
> '#s(sprout bean)
> > ' #s ( sprout alfalfa )
> '#s(sprout alfalfa)
> ```

Like numbers and strings, prefab structures are “self-quoting,” so the quotes above are optional:

> ```racket
> > #s ( sprout bean )
> '#s(sprout bean)
> ```

When you use the #:prefab keyword with [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13), instead of generating a new structure type, you obtain bindings that work with the existing prefab structure type:

> ```racket
> > ( define lunch ' #s ( sprout bean ) )
> > ( struct sprout ( kind ) #:prefab )
> > ( sprout? lunch )
> #t
> > ( sprout-kind lunch )
> 'bean
> > ( sprout ' garlic )
> '#s(sprout garlic)
> ```

The field name kind above does not matter for finding the prefab structure type; only the name sprout and the number of fields matter. At the same time, the prefab structure type sprout with three fields is a different structure type than the one with a single field:

> ```racket
> > ( sprout? #s ( sprout bean #f 17 ) )
> #f
> > ( struct sprout ( kind yummy? count ) #:prefab ) ; redefine
> > ( sprout? #s ( sprout bean #f 17 ) )
> #t
> > ( sprout? lunch )
> #f
> ```

A prefab structure type can have another prefab structure type as its supertype, it can have mutable fields, and it can have auto fields. Variations in any of these dimensions correspond to different prefab structure types, and the printed form of the structure type’s name encodes all of the relevant details.

> ```racket
> > ( struct building ( rooms [ location #:mutable ] ) #:prefab )
> > ( struct house building ( [ occupied #:auto ] ) #:prefab #:auto-value ' no )
> > ( house 5 ' factory )
> '#s((house (1 no) building 2 #(1)) 5 factory no)
> ```

Every [prefab](#%28tech._prefab%29) structure type is [transparent](#%28tech._transparent%29)—but even less abstract than a [transparent](#%28tech._transparent%29) type, because instances can be created without any access to a particular structure-type declaration or existing examples. Overall, the different options for structure types offer a spectrum of possibilities from more abstract to more convenient:

-   [Opaque](#%28tech._opaque%29) (the default) : Instances cannot be inspected or forged without access to the structure-type declaration. As discussed in the next section, [constructor guards](#%28tech._constructor._guard%29) and [properties](#%28tech._property%29) can be attached to the structure type to further protect or to specialize the behavior of its instances.

-   [Transparent](#%28tech._transparent%29) : Anyone can inspect or create an instance without access to the structure-type declaration, which means that the value printer can show the content of an instance. All instance creation passes through a [constructor guard](#%28tech._constructor._guard%29), however, so that the content of an instance can be controlled, and the behavior of instances can be specialized through [properties](#%28tech._property%29). Since the structure type is generated by its definition, instances cannot be manufactured simply through the name of the structure type, and therefore cannot be generated automatically by the expression reader.

-   [Prefab](#%28tech._prefab%29) : Anyone can inspect or create an instance at any time, without prior access to a structure-type declaration or an example instance. Consequently, the expression reader can manufacture instances directly. The instance cannot have a [constructor guard](#%28tech._constructor._guard%29) or [properties](#%28tech._property%29).

Since the expression reader can generate [prefab](#%28tech._prefab%29) instances, they are useful when convenient [serialization](serialization.html#%28tech._serialization%29) is more important than abstraction. [Opaque](#%28tech._opaque%29) and [transparent](#%28tech._transparent%29) structures also can be serialized, however, if they are defined with [serializable-struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=serialization.html%23%2528form._%2528%2528lib._racket%252Fserialize..rkt%2529._serializable-struct%2529%2529&version=8.18.0.13) as described in [Datatypes and Serialization](serialization.html).

### 5.8 More Structure Type Options

The full syntax of [struct](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define-struct.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._struct%2529%2529&version=8.18.0.13) supports many options, both at the structure-type level and at the level of individual fields:

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

maybe-super   =
    |   super-id

field   =   field-id
    |   [field-id field-option ...]
```


### 5.5 Structure Comparisons
(find-user-collects-dir)	→
(-> error type-name "bad name: ~e" name)```
where the name-id is a name for the module, initial-module-path is an initial import, and each decl is an import, export, definition, or expression. In the case of a file, name-id normally matches the name of the containing file, minus its directory path or file extension, but name-id is ignored when the module is [require](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._require%2529%2529&version=8.18.0.13)d through its file’s path.


### 5.5 Structure Comparisons
The initial-module-path is needed because even the [require](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._require%2529%2529&version=8.18.0.13) form must be imported for further use in the module body. In other words, the initial-module-path import bootstraps the syntax that is available in the body. The most commonly used initial-module-path is [racket](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13), which supplies most of the bindings described in this guide, including [require](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._require%2529%2529&version=8.18.0.13), [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), and [provide](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._provide%2529%2529&version=8.18.0.13). Another commonly used initial-module-path is [racket/base](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13), which provides less functionality, but still much of the most commonly needed functions and syntax.


### 5.5 Structure Comparisons
For example, the "cake.rkt" example of the [previous section](module-basics.html) could be written as
> ```racket
> ( module cake racket
> ( provide print-cake )
> ( define ( print-cake n )
> ( show " ~a " n #\. )
> ( show " .-~a-. " n #\| )
> ( show " | ~a | " n #\space )
> ( show "---~a---" n #\- ) )
> ( define ( show fmt n ch )
> ( printf fmt ( make-string n ch ) )
> ( newline ) ) )
> ```
Furthermore, this [module](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=module.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._module%2529%2529&version=8.18.0.13) form can be evaluated in a [REPL](intro.html#%28tech._repl%29) to declare a cake module that is not associated with any file. To refer to such an unassociated module, quote the module name:
Examples:
> ```racket
> > ( require ' cake )
> > ( print-cake 3 )
> ... .-|||-. | | ---------
> ```


### 5.5 Structure Comparisons
Declaring a module does not immediately evaluate the body definitions and expressions of the module. The module must be explicitly [require](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._require%2529%2529&version=8.18.0.13)d at the top level to trigger evaluation. After evaluation is triggered once, later [require](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._require%2529%2529&version=8.18.0.13)s do not re-evaluate the module body.
Examples:
> ```racket
> > ( module hi racket ( printf "Hello\n" ) )
> > ( require ' hi )
> Hello
> > ( require ' hi )
> ```


#### 6.2.2 The #lang Shorthand
The body of a #lang shorthand has no specific syntax, because the syntax is determined by the language name that follows #lang.
In the case of #lang [racket](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13), the syntax is
> ```racket
> #lang racket
> decl ...
> ```
which [reads](hash-lang_reader.html) the same as
> ```racket
> ( module name racket
> decl ... )
> ```
where name is derived from the name of the file that contains the #lang form.
The #lang [racket/base](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13) form has the same syntax as #lang [racket](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13), except that the longhand expansion uses [racket/base](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13) instead of [racket](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13). The #lang [scribble/manual](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=scribble&rel=manual.html&version=8.18.0.13) form, in contrast, has a completely different syntax that doesn’t even look like Racket, and which we do not attempt to describe in this guide.


### 5.5 Structure Comparisons
Unless otherwise specified, a module that is documented as a “language” using the #lang notation will expand to [module](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=module.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._module%2529%2529&version=8.18.0.13) in the same way as #lang [racket](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13). The documented language name can be used directly with [module](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=module.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._module%2529%2529&version=8.18.0.13) or [require](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._require%2529%2529&version=8.18.0.13), too.


#### 6.2.3 Submodules
A [module](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=module.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._module%2529%2529&version=8.18.0.13) form can be nested within a module, in which case the nested [module](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=module.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._module%2529%2529&version=8.18.0.13) form declares a submodule. Submodules can be referenced directly by the enclosing module using a quoted name. The following example prints "Tony" by importing tiger from the zoo submodule:
> > "park.rkt"
> >
> > > ```racket
> > > #lang racket
> > > ( module zoo racket
> > > ( provide tiger )
> > > ( define tiger "Tony" ) )
> > > ( require ' zoo )
> > > tiger
> > > ```
Running a module does not necessarily run its submodules. In the above example, running "park.rkt" runs its submodule zoo only because the "park.rkt" module [require](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=require.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._require%2529%2529&version=8.18.0.13)s the zoo submodule. Otherwise, a module and each of its submodules can be run independently. Furthermore, if "park.rkt" is compiled to a bytecode file (via raco make), then the code for "park.rkt" or the code for zoo can be loaded independently.


### 5.5 Structure Comparisons
Submodules can be nested within submodules, and a submodule can be referenced directly by a module other than its enclosing module by using a [submodule path](module-paths.html#%28elem._submod%29).
A [module*](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=module.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._module%252A%2529%2529&version=8.18.0.13) form is similar to a nested [module](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=module.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._module%2529%2529&version=8.18.0.13) form:
> > ```
(module* name-id initial-module-path-or-#f
> > decl ... )