# 2.2 Simple Definitions and Expressions
> ( cond
> [ ( string-prefix? s "hello " )
> "hi!" ]
> [ ( string-prefix? s "goodbye " )
> "bye!" ]
> [ ( string-suffix? s "?" )
> "I don't know" ]
> [ else "huh?" ] ) )
> ( reply-more "hello racket" )
> "hi!"
> > ( reply-more "goodbye cruel world" )
> "bye!"
> > ( reply-more "what is your favorite color?" )
> "I don't know"
> > ( reply-more "mine is lime green" )
> "huh?"
```
The use of square brackets for cond clauses is a convention. In Racket, parentheses and square brackets are actually interchangeable, as long as ( is matched with ) and \[ is matched with \]. Using square brackets in a few key places makes Racket code even more readable.

#### 2.2.6 Function Calls, Again

In our earlier grammar of function calls, we oversimplified. The actual syntax of a function call allows an arbitrary expression for the function, instead of just an ‹id›:

> > > <img src="finger.png" width="24" height="24" alt="+" />Function Calls (later in this guide) explains more about function calls.

> ( ‹expr› ‹expr›\* )

The first ‹expr› is often an ‹id›, such as string-append or +, but it can be anything that evaluates to a function. For example, it can be a conditional expression:

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><pre><code>( define ( double v )
> ( ( if ( string? v ) string-append + ) v v ) )</code></pre></td></tr><tr><td><p> </p></td></tr><tr><td><pre><code>> ( double "mnah" )
> "mnahmnah"
> > ( double 5 )
> 10</code></pre></td></tr></tbody></table>

Syntactically, the first expression in a function call could even be a number—but that leads to an error, since a number is not a function.

> ```racket
> > ( 1 2 3 4 )
> application: not a procedure;
> expected a procedure that can be applied to arguments
> given: 1
> ```

When you accidentally omit a function name or when you use extra parentheses around an expression, you’ll most often get an “expected a procedure” error like this one.

#### 2.2.7 Anonymous Functions with lambda

Programming in Racket would be tedious if you had to name all of your numbers. Instead of writing (+ 1 2), you’d have to write

> > > <img src="finger.png" width="24" height="24" alt="+" />Functions: lambda (later in this guide) explains more about lambda.

> ```racket
> > ( define a 1 )
> > ( define b 2 )
> > ( + a b )
> 3
> ```

It turns out that having to name all your functions can be tedious, too. For example, you might have a function twice that takes a function and an argument. Using twice is convenient if you already have a name for the function, such as sqrt:

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><pre><code>( define ( twice f v )
> ( f ( f v ) ) )</code></pre></td></tr><tr><td><p> </p></td></tr><tr><td><pre><code>> ( twice sqrt 16 )
> 2</code></pre></td></tr></tbody></table>

If you want to call a function that is not yet defined, you could define it, and then pass it to twice:

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><pre><code>( define ( louder s )
> ( string-append s "!" ) )</code></pre></td></tr><tr><td><p> </p></td></tr><tr><td><pre><code>> ( twice louder "hello" )
> "hello!!"</code></pre></td></tr></tbody></table>

But if the call to twice is the only place where louder is used, it’s a shame to have to write a whole definition. In Racket, you can use a lambda expression to produce a function directly. The lambda form is followed by identifiers for the function’s arguments, and then the function’s body expressions:

> ( lambda ( ‹id›\* ) ‹expr›+ )

Evaluating a lambda form by itself produces a function:

> ```racket
> > ( lambda ( s ) ( string-append s "!" ) )
> #<procedure>
> ```

Using lambda, the above call to twice can be re-written as

> ```racket
> > ( twice ( lambda ( s ) ( string-append s "!" ) ) "hello" )
> "hello!!"
> > ( twice ( lambda ( s ) ( string-append s "?!" ) ) "hello" )
> "hello?!?!"
> ```

Another use of lambda is as a result for a function that generates functions:

> ```
(define (make-add-suffix s2)
> ( lambda ( s ) ( string-append s s2 ) ) )

> ( twice ( make-add-suffix "!" ) "hello" )
> "hello!!"
> > ( twice ( make-add-suffix "?!" ) "hello" )
> "hello?!?!"
> > ( twice ( make-add-suffix "..." ) "hello" )
> "hello......"```

Racket is a lexically scoped language, which means that s2 in the function returned by make-add-suffix always refers to the argument for the call that created the function. In other words, the [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13)-generated function “remembers” the right s2:

> ```racket
> > ( define louder ( make-add-suffix "!" ) )
> > ( define less-sure ( make-add-suffix "?" ) )
> > ( twice less-sure "really" )
> "really??"
> > ( twice louder "really" )
> "really!!"
> ```

We have so far referred to definitions of the form ([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) ‹id› ‹expr›) as “non-function definitions.” This characterization is misleading, because the ‹expr› could be a [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) form, in which case the definition is equivalent to using the “function” definition form. For example, the following two definitions of louder are equivalent:

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><pre><code>( define ( louder s )
> ( string-append s "!" ) )
> ( define louder
> ( lambda ( s )
> ( string-append s "!" ) ) )</code></pre></td></tr><tr><td><p> </p></td></tr><tr><td><pre><code>> louder
> #<procedure:louder></code></pre></td></tr></tbody></table>

Note that the expression for louder in the second case is an “anonymous” function written with [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13), but, if possible, the compiler infers a name, anyway, to make printing and error reporting as informative as possible.

#### 2.2.8 Local Binding with [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), [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), and [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%252A%2529%2529&version=8.18.0.13)

It’s time to retract another simplification in our grammar of Racket. In the body of a function, definitions can appear before the body expressions:

> > > <img src="finger.png" width="24" height="24" alt="+" />[Internal Definitions](define.html#%28part._intdefs%29) (later in this guide) explains more about local (internal) definitions.

> ```racket
> ( define ( ‹ id › ‹ id › * ) ‹ definition › * ‹ expr › + )
> ( lambda ( ‹ id › * ) ‹ definition › * ‹ expr › + )
> ```

Definitions at the start of a function body are local to the function body.

Examples:

> ```racket
> ( define ( converse s ) ( define ( starts? s2 ) ; local to converse ( define spaced-s2 ( string-append s2 " " ) ) ; local to starts? ( string-prefix? s spaced-s2 ) ) ( cond [ ( starts? "hello" ) "hi!" ] [ ( starts? "goodbye" ) "bye!" ] [ else "huh?" ] ) )
> > ( converse "hello world" )
> "hi!"
> > ( converse "hellonearth" )
> "huh?"
> > ( converse "goodbye friends" )
> "bye!"
> > ( converse "urp" )
> "huh?"
> > starts? ; outside of converse , so...
> starts?: undefined;
> cannot reference an identifier before its definition
> in module: top-level
> ```

Another way to create local bindings is the [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. An advantage of [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 that it can be used in any expression position. Also, [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 many identifiers at once, instead of requiring a separate [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) for each identifier.

> > > <img src="finger.png" width="24" height="24" alt="+" />[Internal Definitions](define.html#%28part._intdefs%29) (later in this guide) explains more about [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) and [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%252A%2529%2529&version=8.18.0.13).

> ( let ( {\[ ‹id› ‹expr› \]}\* ) ‹expr›+ )

Each binding clause is an ‹id› and an ‹expr› surrounded by square brackets, and the expressions after the clauses are the body of the [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). In each clause, the ‹id› is bound to the result of the ‹expr› for use in the body.

> ```racket
> > ( let ( [ x ( random 4 ) ] [ o ( random 4 ) ] ) ( cond [ ( > x o ) "X wins" ] [ ( > o x ) "O wins" ] [ else "cat's game" ] ) )
> "cat's game"
> ```

The bindings of a [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 are available only in the body of the [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), so the binding clauses cannot refer to each other. The [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%252A%2529%2529&version=8.18.0.13) form, in contrast, allows later clauses to use earlier bindings:

> ```racket
> > ( let* ( [ x ( random 4 ) ] [ o ( random 4 ) ] [ diff ( number->string ( abs ( - x o ) ) ) ] ) ( cond [ ( > x o ) ( string-append "X wins by " diff ) ] [ ( > o x ) ( string-append "O wins by " diff ) ] [ else "cat's game" ] ) )
> "O wins by 2"
> ```

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

# 2.3 Lists, Iteration, and Recursion

### 2.3 Lists, Iteration, and Recursion

Racket is a dialect of the language Lisp, whose name originally stood for “LISt Processor.” The built-in list datatype remains a prominent feature of the language.

The [list](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=8.18.0.13) function takes any number of values and returns a list containing the values:

> ```racket
> > ( list "red" "green" "blue" )
> '("red" "green" "blue")
> > ( list 1 2 3 4 5 )
> '(1 2 3 4 5)
> ```

> > > A list usually prints with ', but the printed form of a list depends on its content. See [Pairs and Lists](pairs.html) for more information.

As you can see, a list result prints in the [REPL](intro.html#%28tech._repl%29) as a quote ' and then a pair of parentheses wrapped around the printed form of the list elements. There’s an opportunity for confusion here, because parentheses are used for both expressions, such as ([list](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=8.18.0.13) "red" "green" "blue"), and printed results, such as '("red" "green" "blue"). In addition to the quote, parentheses for results are printed in blue in the documentation and in DrRacket, whereas parentheses for expressions are brown.

Many predefined functions operate on lists. Here are a few examples:

> ```racket
> > ( length ( list "hop" "skip" "jump" ) ) ; count the elements
> 3
> > ( list-ref ( list "hop" "skip" "jump" ) 0 ) ; extract by position
> "hop"
> > ( list-ref ( list "hop" "skip" "jump" ) 1 )
> "skip"
> > ( append ( list "hop" "skip" ) ( list "jump" ) ) ; combine lists
> '("hop" "skip" "jump")
> > ( reverse ( list "hop" "skip" "jump" ) ) ; reverse order
> '("jump" "skip" "hop")
> > ( member "fall" ( list "hop" "skip" "jump" ) ) ; check for an element
> #f
> ```

#### 2.3.1 Predefined List Loops

In addition to simple operations like [append](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._append%2529%2529&version=8.18.0.13), Racket includes functions that iterate over the elements of a list. These iteration functions play a role similar to [for](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=for.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._for%2529%2529&version=8.18.0.13) in Java, Racket, and other languages. The body of a Racket iteration is packaged into a function to be applied to each element, so the [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) form becomes particularly handy in combination with iteration functions.

Different list-iteration functions combine iteration results in different ways. The [map](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fmap..rkt%2529._map%2529%2529&version=8.18.0.13) function uses the per-element results to create a new list:

> ```racket
> > ( map sqrt ( list 1 4 9 16 ) )
> '(1 2 3 4)
> > ( map ( lambda ( i ) ( string-append i "!" ) ) ( list "peanuts" "popcorn" "crackerjack" ) )
> '("peanuts!" "popcorn!" "crackerjack!")
> ```

The [andmap](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fmap..rkt%2529._andmap%2529%2529&version=8.18.0.13) and [ormap](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fmap..rkt%2529._ormap%2529%2529&version=8.18.0.13) functions combine the results by [and](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._and%2529%2529&version=8.18.0.13)ing or [or](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._or%2529%2529&version=8.18.0.13)ing:

> ```racket
> > ( andmap string? ( list "a" "b" "c" ) )
> #t
> > ( andmap string? ( list "a" "b" 6 ) )
> #f
> > ( ormap number? ( list "a" "b" 6 ) )
> #t
> ```

The [map](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fmap..rkt%2529._map%2529%2529&version=8.18.0.13), [andmap](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fmap..rkt%2529._andmap%2529%2529&version=8.18.0.13), and [ormap](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fmap..rkt%2529._ormap%2529%2529&version=8.18.0.13) functions can all handle multiple lists, instead of just a single list. The lists must all have the same length, and the given function must accept one argument for each list:

> ```racket
> > ( map ( lambda ( s n ) ( substring s 0 n ) ) ( list "peanuts" "popcorn" "crackerjack" ) ( list 6 3 7 ) )
> '("peanut" "pop" "cracker")
> ```

The [filter](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Flist..rkt%2529._filter%2529%2529&version=8.18.0.13) function keeps elements for which the body result is true, and discards elements for which it is #f:

> ```racket
> > ( filter string? ( list "a" "b" 6 ) )
> '("a" "b")
> > ( filter positive? ( list 1 -2 6 7 0 ) )
> '(1 6 7)
> ```

The [foldl](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Flist..rkt%2529._foldl%2529%2529&version=8.18.0.13) function generalizes some iteration functions. It uses the per-element function to both process an element and combine it with the “current” value, so the per-element function takes an extra first argument. Also, a starting “current” value must be provided before the lists:

> ```racket
> > ( foldl ( lambda ( elem v ) ( + v ( * elem elem ) ) ) 0 ' ( 1 2 3 ) )
> 14
> ```

Despite its generality, [foldl](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Flist..rkt%2529._foldl%2529%2529&version=8.18.0.13) is not as popular as the other functions. One reason is that [map](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fmap..rkt%2529._map%2529%2529&version=8.18.0.13), [ormap](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fmap..rkt%2529._ormap%2529%2529&version=8.18.0.13), [andmap](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fmap..rkt%2529._andmap%2529%2529&version=8.18.0.13), and [filter](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Flist..rkt%2529._filter%2529%2529&version=8.18.0.13) cover the most common kinds of list loops.

Racket provides a general list comprehension form [for/list](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=for.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._for%252Flist%2529%2529&version=8.18.0.13), which builds a list by iterating through sequences. List comprehensions and related iteration forms are described in [Iterations and Comprehensions](for.html).

#### 2.3.2 List Iteration from Scratch

Although [map](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fmap..rkt%2529._map%2529%2529&version=8.18.0.13) and other iteration functions are predefined, they are not primitive in any interesting sense. You can write equivalent iterations using a handful of list primitives.

Since a Racket list is a linked list, the two core operations on a non-empty list are

-   [first](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._first%2529%2529&version=8.18.0.13): get the first thing in the list; and

-   [rest](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._rest%2529%2529&version=8.18.0.13): get the rest of the list.

Examples:

> ```racket
> > ( first ( list 1 2 3 ) )
> 1
> > ( rest ( list 1 2 3 ) )
> '(2 3)
> ```

To create a new node for a linked list—that is, to add to the front of the list—use the [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13) function, which is short for “construct.” To get an empty list to start with, use the [empty](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._empty%2529%2529&version=8.18.0.13) constant:

> ```racket
> > empty
> '()
> > ( cons "head" empty )
> '("head")
> > ( cons "dead" ( cons "head" empty ) )
> '("dead" "head")
> ```

To process a list, you need to be able to distinguish empty lists from non-empty lists, because [first](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._first%2529%2529&version=8.18.0.13) and [rest](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._rest%2529%2529&version=8.18.0.13) work only on non-empty lists. The [empty?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._empty%7E3f%2529%2529&version=8.18.0.13) function detects empty lists, and [cons?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._cons%7E3f%2529%2529&version=8.18.0.13) detects non-empty lists:

> ```racket
> > ( empty? empty )
> #t
> > ( empty? ( cons "head" empty ) )
> #f
> > ( cons? empty )
> #f
> > ( cons? ( cons "head" empty ) )
> #t
> ```

With these pieces, you can write your own versions of the [length](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._length%2529%2529&version=8.18.0.13) function, [map](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fmap..rkt%2529._map%2529%2529&version=8.18.0.13) function, and more.

Examples:

> ```racket
> ( define ( my-length lst ) ( cond [ ( empty? lst ) 0 ] [ else ( + 1 ( my-length ( rest lst ) ) ) ] ) )
> > ( my-length empty )
> 0
> > ( my-length ( list "a" "b" "c" ) )
> 3
> ```

> ```
(define (my-map f lst)
> ( cond
> [ ( empty? lst ) empty ]
> [ else ( cons ( f ( first lst ) )
> ( my-map f ( rest lst ) ) ) ] ) )

> ( my-map string-upcase ( list "ready" "set" "go" ) )
> '("READY" "SET" "GO")
```


# 2.2 Simple Definitions and Expressions
If the derivation of the above definitions is mysterious to you, consider reading How to Design Programs. If you are merely suspicious of the use of recursive calls instead of a looping construct, then read on.


#### 2.3.3 Tail Recursion
Both the my-length and my-map functions run in O(n) space for a list of length n. This is easy to see by imagining how (my-length (list "a" "b" "c")) must evaluate:
> ```racket
> ( my-length ( list "a" "b" "c" ) )
> = ( + 1 ( my-length ( list "b" "c" ) ) )
> = ( + 1 ( + 1 ( my-length ( list "c" ) ) ) )
> = ( + 1 ( + 1 ( + 1 ( my-length ( list ) ) ) ) )
> = ( + 1 ( + 1 ( + 1 0 ) ) )
> = ( + 1 ( + 1 1 ) )
> = ( + 1 2 )
> = 3
> ```
For a list with n elements, evaluation will stack up n (+ 1 ...) additions, and then finally add them up when the list is exhausted.
You can avoid piling up additions by adding along the way. To accumulate a length this way, we need a function that takes both a list and the length of the list seen so far; the code below uses a local function iter that accumulates the length in an argument len:
> ```racket
> ( define ( my-length lst )
> ; local function iter :
> ( define ( iter lst len )
> ( cond
> [ ( empty? lst ) len ]
> [ else ( iter ( rest lst ) ( + len 1 ) ) ] ) )
> ; body of my-length calls iter :
> ( iter lst 0 ) )
> ```
Now evaluation looks like this:
> ```racket
> ( my-length ( list "a" "b" "c" ) )
> = ( iter ( list "a" "b" "c" ) 0 )
> = ( iter ( list "b" "c" ) 1 )
> = ( iter ( list "c" ) 2 )
> = ( iter ( list ) 3 )
> 3
> ```


# 2.2 Simple Definitions and Expressions
The revised my-length runs in constant space, just as the evaluation steps above suggest. That is, when the result of a function call, like (iter (list "b" "c") 1), is exactly the result of some other function call, like (iter (list "c") 2), then the first one doesn’t have to wait around for the second one, because that takes up space for no good reason.
This evaluation behavior is sometimes called tail-call optimization, but it’s not merely an “optimization” in Racket; it’s a guarantee about the way the code will run. More precisely, an expression in tail position with respect to another expression does not take extra computation space over the other expression.
In the case of my-map, O(n) space complexity is reasonable, since it has to generate a result of size O(n). Nevertheless, you can reduce the constant factor by accumulating the result list. The only catch is that the accumulated list will be backwards, so you’ll have to reverse it at the very end:
> > > Attempting to reduce a constant factor like this is usually not worthwhile, as discussed below.
> ```racket
> ( define ( my-map f lst )
> ( define ( iter lst backward-result )
> ( cond
> [ ( empty? lst ) ( reverse backward-result ) ]
> [ else ( iter ( rest lst )
> ( cons ( f ( first lst ) )
> backward-result ) ) ] ) )
> ( iter lst empty ) )
> ```
It turns out that if you write
> ```racket
> ( define ( my-map f lst )
> ( for/list ( [ i lst ] )
> ( f i ) ) )
> ```


# 2.2 Simple Definitions and Expressions
then the for/list form in the function is expanded to essentially the same code as the iter local definition and use. The difference is merely syntactic convenience.


#### 2.3.4 Recursion versus Iteration
The my-length and my-map examples demonstrate that iteration is just a special case of recursion. In many languages, it’s important to try to fit as many computations as possible into iteration form. Otherwise, performance will be bad, and moderately large inputs can lead to stack overflow. Similarly, in Racket, it is sometimes important to make sure that tail recursion is used to avoid O(n) space consumption when the computation is easily performed in constant space.
At the same time, recursion does not lead to particularly bad performance in Racket, and there is no such thing as stack overflow; you can run out of memory if a computation involves too much context, but exhausting memory typically requires orders of magnitude deeper recursion than would trigger a stack overflow in other languages. These considerations, combined with the fact that tail-recursive programs automatically run the same as a loop, lead Racket programmers to embrace recursive forms rather than avoid them.
Suppose, for example, that you want to remove consecutive duplicates from a list. While such a function can be written as a loop that remembers the previous element for each iteration, a Racket programmer would more likely just write the following:
> ```
(define (remove-dups l)
> ( cond
> [ ( empty? l ) empty ]
> [ ( empty? ( rest l ) ) l ]
> [ else
> ( let ( [ i ( first l ) ] )
> ( if ( equal? i ( first ( rest l ) ) )
> ( remove-dups ( rest l ) )
> ( cons i ( remove-dups ( rest l ) ) ) ) ) ] ) )


# 2.2 Simple Definitions and Expressions
> ( remove-dups ( list "a" "b" "b" "b" "c" "c" ) )
> '("a" "b" "c")```
In general, this function consumes O(n) space for an input list of length n, but that’s fine, since it produces an O(n) result. If the input list happens to be mostly consecutive duplicates, then the resulting list can be much smaller than O(n)—and remove-dups will also use much less than O(n) space! The reason is that when the function discards duplicates, it returns the result of a remove-dups call directly, so the tail-call “optimization” kicks in:
> ```racket
> ( remove-dups ( list "a" "b" "b" "b" "b" "b" ) )
> = ( cons "a" ( remove-dups ( list "b" "b" "b" "b" "b" ) ) )
> = ( cons "a" ( remove-dups ( list "b" "b" "b" "b" ) ) )
> = ( cons "a" ( remove-dups ( list "b" "b" "b" ) ) )
> = ( cons "a" ( remove-dups ( list "b" "b" ) ) )
> = ( cons "a" ( remove-dups ( list "b" ) ) )
> = ( cons "a" ( list "b" ) )
> = ( list "a" "b" )
> ```
------------------------------------------------------------------------


# 2.4 Pairs, Lists, and Racket Syntax


### 2.4 Pairs, Lists, and Racket Syntax
The [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13) function actually accepts any two values, not just a list for the second argument. When the second argument is not [empty](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._empty%2529%2529&version=8.18.0.13) and not itself produced by [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13), the result prints in a special way. The two values joined with [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13) are printed between parentheses, but with a dot (i.e., a period surrounded by whitespace) in between:
> ```racket
> > ( cons 1 2 )
> '(1 . 2)
> > ( cons "banana" "split" )
> '("banana" . "split")
> ```


# 2.4 Pairs, Lists, and Racket Syntax
Thus, a value produced by [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13) is not always a list. In general, the result of [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13) is a pair. The more traditional name for the [cons?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._cons%7E3f%2529%2529&version=8.18.0.13) function is [pair?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._pair%7E3f%2529%2529&version=8.18.0.13), and we’ll use the traditional name from now on.


# 2.4 Pairs, Lists, and Racket Syntax
The name [rest](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._rest%2529%2529&version=8.18.0.13) also makes less sense for non-list pairs; the more traditional names for [first](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._first%2529%2529&version=8.18.0.13) and [rest](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Flist..rkt%2529._rest%2529%2529&version=8.18.0.13) are [car](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._car%2529%2529&version=8.18.0.13) and [cdr](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cdr%2529%2529&version=8.18.0.13), respectively. (Granted, the traditional names are also nonsense. Just remember that “a” comes before “d,” and [cdr](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cdr%2529%2529&version=8.18.0.13) is pronounced “could-er.”)
Examples:


# 2.4 Pairs, Lists, and Racket Syntax
> ```racket
> > ( car ( cons 1 2 ) )
> 1
> > ( cdr ( cons 1 2 ) )
> 2
> > ( pair? empty )
> #f
> > ( pair? ( cons 1 2 ) )
> #t
> > ( pair? ( list 1 2 3 ) )
> #t
> ```
Racket’s pair datatype and its relation to lists is essentially a historical curiosity, along with the dot notation for printing and the funny names [car](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._car%2529%2529&version=8.18.0.13) and [cdr](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cdr%2529%2529&version=8.18.0.13). Pairs are deeply wired into the culture, specification, and implementation of Racket, however, so they survive in the language.
You are perhaps most likely to encounter a non-list pair when making a mistake, such as accidentally reversing the arguments to [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13):
> ```racket
> > ( cons ( list 2 3 ) 1 )
> '((2 3) . 1)
> > ( cons 1 ( list 2 3 ) )
> '(1 2 3)
> ```


# 2.4 Pairs, Lists, and Racket Syntax
Non-list pairs are used intentionally, sometimes. For example, the [make-hash](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=hashtables.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._make-hash%2529%2529&version=8.18.0.13) function takes a list of pairs, where the [car](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._car%2529%2529&version=8.18.0.13) of each pair is a key and the [cdr](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cdr%2529%2529&version=8.18.0.13) is an arbitrary value.
The only thing more confusing to new Racketeers than non-list pairs is the printing convention for pairs where the second element is a pair, but is not a list:
> ```racket
> > ( cons 0 ( cons 1 2 ) )
> '(0 1 . 2)
> ```
In general, the rule for printing a pair is as follows: use the dot notation unless the dot is immediately followed by an open parenthesis. In that case, remove the dot, the open parenthesis, and the matching close parenthesis. Thus, '(0 . (1 . 2)) becomes '(0 1 . 2), and '(1 . (2 . (3 . ()))) becomes '(1 2 3).


#### 2.4.1 Quoting Pairs and Symbols with [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)
A list prints with a quote mark before it, but if an element of a list is itself a list, then no quote mark is printed for the inner list:
> ```racket
> > ( list ( list 1 ) ( list 2 3 ) ( list 4 ) )
> '((1) (2 3) (4))
> ```
For nested lists, especially, 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 lets you write a list as an expression in essentially the same way that the list prints:
> ```racket
> > ( quote ( "red" "green" "blue" ) )
> '("red" "green" "blue")
> > ( quote ( ( 1 ) ( 2 3 ) ( 4 ) ) )
> '((1) (2 3) (4))
> > ( quote ( ) )
> '()
> ```
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 works with the dot notation, too, whether the quoted form is normalized by the dot-parenthesis elimination rule or not:
> ```racket
> > ( quote ( 1 . 2 ) )
> '(1 . 2)
> > ( quote ( 0 . ( 1 . 2 ) ) )
> '(0 1 . 2)
> ```
Naturally, lists of any kind can be nested:


# 2.4 Pairs, Lists, and Racket Syntax
> ```racket
> > ( list ( list 1 2 3 ) 5 ( list "a" "b" "c" ) )
> '((1 2 3) 5 ("a" "b" "c"))
> > ( quote ( ( 1 2 3 ) 5 ( "a" "b" "c" ) ) )
> '((1 2 3) 5 ("a" "b" "c"))
> ```
If you wrap an identifier with [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), then you get output that looks like an identifier, but with a ' prefix:
> ```racket
> > ( quote jane-doe )
> 'jane-doe
> ```
A value that prints like a quoted identifier is a symbol. In the same way that parenthesized output should not be confused with expressions, a printed symbol should not be confused with an identifier. In particular, the symbol ([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) map) has nothing to do with the map identifier or the predefined function that is bound to [map](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fmap..rkt%2529._map%2529%2529&version=8.18.0.13), except that the symbol and the identifier happen to be made up of the same letters.


# 2.4 Pairs, Lists, and Racket Syntax
Indeed, the intrinsic value of a symbol is nothing more than its character content. In this sense, symbols and strings are almost the same thing, and the main difference is how they print. The functions [symbol->string](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=symbols.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._symbol-%7E3estring%2529%2529&version=8.18.0.13) and [string->symbol](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=symbols.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-%7E3esymbol%2529%2529&version=8.18.0.13) convert between them.
Examples:
> ```racket
> > map
> #<procedure:map>
> > ( quote map )
> 'map
> > ( symbol? ( quote map ) )
> #t
> > ( symbol? map )
> #f
> > ( procedure? map )
> #t
> > ( string->symbol "map" )
> 'map
> > ( symbol->string ( quote map ) )
> "map"
> ```


# 2.4 Pairs, Lists, and Racket Syntax
In the same way that [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) for a list automatically applies itself to nested lists, [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) on a parenthesized sequence of identifiers automatically applies itself to the identifiers to create a list of symbols:
> ```racket
> > ( car ( quote ( road map ) ) )
> 'road
> > ( symbol? ( car ( quote ( road map ) ) ) )
> #t
> ```
When a symbol is inside a list that is printed with ', the ' on the symbol is omitted, since ' is doing the job already:
> ```racket
> > ( quote ( road map ) )
> '(road map)
> ```
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 has no effect on a literal expression such as a number or string:
> ```racket
> > ( quote 42 )
> 42
> > ( quote "on the record" )
> "on the record"
> ```


#### 2.4.2 Abbreviating [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) with '
As you may have guessed, you can abbreviate a use 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) by just putting ' in front of a form to quote:
> ```racket
> > ' ( 1 2 3 )
> '(1 2 3)
> > ' road
> 'road
> > ' ( ( 1 2 3 ) road ( "a" "b" "c" ) )
> '((1 2 3) road ("a" "b" "c"))
> ```
In the documentation, ' within an expression is printed in green along with the form after it, since the combination is an expression that is a constant. In DrRacket, only the ' is colored green. DrRacket is more precisely correct, because the meaning 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) can vary depending on the context of an expression. In the documentation, however, we routinely assume that standard bindings are in scope, and so we paint quoted forms in green for extra clarity.


# 2.4 Pairs, Lists, and Racket Syntax
A ' expands to a [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 in quite a literal way. You can see this if you put a ' in front of a form that has a ':
> ```racket
> > ( car ' ' road )
> 'quote
> > ( car ' ( quote road ) )
> 'quote
> ```
The ' abbreviation works in output as well as input. The [REPL](intro.html#%28tech._repl%29)’s printer recognizes the symbol 'quote as the first element of a two-element list when printing output, in which case it uses ’ to print the output:
> ```racket
> > ( quote ( quote road ) )
> ''road
> > ' ( quote road )
> ''road
> > ' ' road
> ''road
> ```


#### 2.4.3 Lists and Racket Syntax
Now that you know the truth about pairs and lists, and now that you’ve seen [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), you’re ready to understand the main way in which we have been simplifying Racket’s true syntax.
The syntax of Racket is not defined directly in terms of character streams. Instead, the syntax is determined by two layers:
-   a reader layer, which turns a sequence of characters into lists, symbols, and other constants; and
-   an expander layer, which processes the lists, symbols, and other constants to parse them as an expression.
The rules for printing and reading go together. For example, a list is printed with parentheses, and reading a pair of parentheses produces a list. Similarly, a non-list pair is printed with the dot notation, and a dot on input effectively runs the dot-notation rules in reverse to obtain a pair.
One consequence of the read layer for expressions is that you can use the dot notation in expressions that are not quoted forms:
> ```racket
> > ( + 1 . ( 2 ) )
> 3
> ```


# 2.4 Pairs, Lists, and Racket Syntax
This works because ([+](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=8.18.0.13) 1 . (2)) is just another way of writing ([+](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=8.18.0.13) 1 2). It is practically never a good idea to write application expressions using this dot notation; it’s just a consequence of the way Racket’s syntax is defined.
Normally, . is allowed by the reader only with a parenthesized sequence, and only before the last element of the sequence. However, a pair of .s can also appear around a single element in a parenthesized sequence, as long as the element is not first or last. Such a pair triggers a reader conversion that moves the element between .s to the front of the list. The conversion enables a kind of general infix notation:
> ```racket
> > ( 1 . < . 2 )> #t
> > ' ( 1 . < . 2 )> '(< 1 2)> ```


# 2.4 Pairs, Lists, and Racket Syntax
This two-dot convention is non-traditional, and it has essentially nothing to do with the dot notation for non-list pairs. Racket programmers use the infix convention sparingly—mostly for asymmetric binary operators such as [<](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._%7E3c%2529%2529&version=8.18.0.13) and [is-a?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=objectutils.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fclass-internal..rkt%2529._is-a%7E3f%2529%2529&version=8.18.0.13).
------------------------------------------------------------------------


# 3 Built-In Datatypes


## 3 Built-In Datatypes
The [previous chapter](to-scheme.html) introduced some of Racket’s built-in datatypes: numbers, booleans, strings, lists, and procedures. This section provides a more complete coverage of the built-in datatypes for simple forms of data.
|                                                                               |
|-------------------------------------------------------------------------------|
|     [3.1 Booleans](booleans.html)                  |
|     [3.2 Numbers](numbers.html)                    |
|     [3.3 Characters](characters.html)              |
|     [3.4 Strings (Unicode)](strings.html)          |
|     [3.5 Bytes and Byte Strings](bytestrings.html) |
|     [3.6 Symbols](symbols.html)                    |
|     [3.7 Keywords](keywords.html)                  |
|     [3.8 Pairs and Lists](pairs.html)              |
|     [3.9 Vectors](vectors.html)                    |
|     [3.10 Hash Tables](hash-tables.html)           |
|     [3.11 Boxes](boxes.html)                       |
|     [3.12 Void and Undefined](void_undefined.html) |
------------------------------------------------------------------------


# 3.1 Booleans


### 3.1 Booleans
Racket has two distinguished constants to represent boolean values: #t for true and #f for false. Uppercase #T and #F are parsed as the same values, but the lowercase forms are preferred.
The [boolean?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=booleans.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._boolean%7E3f%2529%2529&version=8.18.0.13) procedure recognizes the two boolean constants. In the result of a test expression for [if](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.18.0.13), [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), [and](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._and%2529%2529&version=8.18.0.13), [or](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._or%2529%2529&version=8.18.0.13), etc., however, any value other than #f counts as true.
Examples:


# 3.1 Booleans
> ```racket
> > ( = 2 ( + 1 1 ) )
> #t
> > ( boolean? #t )
> #t
> > ( boolean? #f )
> #t
> > ( boolean? "no" )
> #f
> > ( if "no" 1 0 )
> 1
> ```
------------------------------------------------------------------------


# 3.2 Numbers


### 3.2 Numbers
A Racket number is either exact or inexact:
-   An exact number is either
    -   an arbitrarily large or small integer, such as 5, 99999999999999999, or -17;
    -   a rational that is exactly the ratio of two arbitrarily small or large integers, such as 1/2, 99999999999999999/2, or -3/4; or
    -   a complex number with exact real and imaginary parts (where the imaginary part is not zero), such as 1+2i or 1/2+3/4i.
-   An inexact number is either
    -   an IEEE floating-point representation of a number, such as 2.0 or 3.14e+87, where the IEEE infinities and not-a-number are written +inf.0, -inf.0, and +nan.0 (or -nan.0); or
    -   a complex number with real and imaginary parts that are IEEE floating-point representations, such as 2.0+3.0i or -inf.0+nan.0i; as a special case, an inexact complex number can have an exact zero real part with an inexact imaginary part.
Inexact numbers print with a decimal point or exponent specifier, and exact numbers print as integers and fractions. The same conventions apply for reading number constants, but #e or #i can prefix a number to force its parsing as an exact or inexact number. The prefixes #b, #o, and #x specify binary, octal, and hexadecimal interpretation of digits.


# 3.2 Numbers
> > > <img src="magnify.png" width="24" height="24" alt="+" />[Reading Numbers](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=reader.html%23%2528part._parse-number%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) documents the fine points of the syntax of numbers.
Examples:
> ```racket
> > 0.5
> 0.5
> > #e0.5
> 1/2
> > #x03BB
> 955
> ```
Computations that involve an inexact number produce inexact results, so that inexactness acts as a kind of taint on numbers. Beware, however, that Racket offers no “inexact booleans,” so computations that branch on the comparison of inexact numbers can nevertheless produce exact results. The procedures [exact->inexact](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._exact-%7E3einexact%2529%2529&version=8.18.0.13) and [inexact->exact](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._inexact-%7E3eexact%2529%2529&version=8.18.0.13) convert between the two types of numbers.
Examples:
> ```racket
> > ( / 1 2 )
> 1/2
> > ( / 1 2.0 )
> 0.5
> > ( if ( = 3.0 2.999 ) 1 2 )
> 2
> > ( inexact->exact 0.1 )
> 3602879701896397/36028797018963968
> ```


# 3.2 Numbers
Inexact results are also produced by procedures such as [sqrt](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._sqrt%2529%2529&version=8.18.0.13), [log](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._log%2529%2529&version=8.18.0.13), and [sin](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._sin%2529%2529&version=8.18.0.13) when an exact result would require representing real numbers that are not rational. Racket can represent only rational numbers and complex numbers with rational parts.
Examples:
> ```racket
> > ( sin 0 ) ; rational...
> 0
> > ( sin 1/2 ) ; not rational...
> 0.479425538604203
> ```
In terms of performance, computations with small integers are typically the fastest, where “small” means that the number fits into one bit less than the machine’s word-sized representation for signed numbers. Computation with very large exact integers or with non-integer exact numbers can be much more expensive than computation with inexact numbers.
> ```
(define (sigma f a b)
> ( if ( = a b )
> 0
> ( + ( f a ) ( sigma f ( + a 1 ) b ) ) ) )


# 3.2 Numbers
> ( time ( round ( sigma ( lambda ( x ) ( / 1 x ) ) 1 2000 ) ) )
> cpu time: 21 real time: 3 gc time: 0
> 8
> > ( time ( round ( sigma ( lambda ( x ) ( / 1.0 x ) ) 1 2000 ) ) )
> cpu time: 0 real time: 0 gc time: 0
> 8.0
```
(Unicode)	→
(-> integer → char 65)```

The above example says that, within a something-else form, a thing is either an identifier or a keyword.

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

# 4.2 Identifiers and Binding

### 4.2 Identifiers and Binding

The context of an expression determines the meaning of identifiers that appear in the expression. In particular, starting a module with the language [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), as in

> [#lang](Module_Syntax.html#%28part._hash-lang%29) [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)

means that, within the module, the identifiers described in this guide start with the meaning described here: [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13) refers to the function that creates a pair, [car](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._car%2529%2529&version=8.18.0.13) refers to the function that extracts the first element of a pair, and so on.

> > > [Symbols](symbols.html) introduces the syntax of identifiers.

Forms like [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), [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13), and [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) associate a meaning with one or more identifiers; that is, they bind identifiers. The part of the program for which the binding applies is the scope of the binding. The set of bindings in effect for a given expression is the expression’s environment.

For example, in

> ```racket
> #lang racket
> ( define f
> ( lambda ( x )
> ( let ( [ y 5 ] )
> ( + x y ) ) ) )
> ( f 10 )
> ```

the [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) is a binding of f, the [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) has a binding for x, and the [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) has a binding for y. The scope of the binding for f is the entire module; the scope of the x binding is ([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) (\[y 5\]) ([+](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) x y)); and the scope of the y binding is just ([+](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) x y). The environment of ([+](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) x y) includes bindings for y, x, and f, as well as everything in [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).

A module-level [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) can bind only identifiers that are not already defined 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)d into the module. A local [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) or other binding forms, however, can give a new local binding for an identifier that already has a binding; such a binding shadows the existing binding.

Examples:

> ```racket
> ( define f ( lambda ( append ) ( define cons ( append "ugly" "confusing" ) ) ( let ( [ append ' this-was ] ) ( list append cons ) ) ) )
> > ( f list )
> '(this-was ("ugly" "confusing"))
> ```

Similarly, a module-level [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) can [shadow](#%28tech._shadow%29) a binding from the module’s language. For example, ([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) [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13) 1) in a [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) module shadows the [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13) that is provided by [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). Intentionally shadowing a language binding is rarely a good idea—especially for widely used bindings like [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13)—but shadowing relieves a programmer from having to avoid every obscure binding that is provided by a language.

Even identifiers like [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 [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) get their meanings from bindings, though they have transformer bindings (which means that they indicate syntactic forms) instead of value bindings. Since [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) has a transformer binding, the identifier define cannot be used by itself to get a value. However, the normal binding for define can be shadowed.

Examples:

> ```racket
> > define
> eval:1:0: define: bad syntax
> in: define
> > ( let ( [ define 5 ] ) define )
> 5
> ```

Again, shadowing standard bindings in this way is rarely a good idea, but the possibility is an inherent part of Racket’s flexibility.

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

# 4.3 Function Calls

### 4.3 Function Calls (Procedure Applications)

An expression of the form

> > |                          |
> > |--------------------------|
> > | (proc-expr arg-expr ...) |

is a function call—also known as a procedure application—when proc-expr is not an identifier that is bound as a syntax transformer (such as [if](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.18.0.13) or [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)).

#### 4.3.1 Evaluation Order and Arity

A function call is evaluated by first evaluating the proc-expr and all arg-exprs in order (left to right). Then, if proc-expr produces a function that accepts as many arguments as supplied arg-exprs, the function is called. Otherwise, an exception is raised.

Examples:

> ```racket
> > ( cons 1 null )
> '(1)
> > ( + 1 2 3 )
> 6
> > ( cons 1 2 3 )
> cons: arity mismatch;
> the expected number of arguments does not match the given
> number
> expected: 2
> given: 3
> > ( 1 2 3 )
> application: not a procedure;
> expected a procedure that can be applied to arguments
> given: 1
> ```

Some functions, such as [cons](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._cons%2529%2529&version=8.18.0.13), accept a fixed number of arguments. Some functions, such as [+](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) or [list](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=pairs.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._list%2529%2529&version=8.18.0.13), accept any number of arguments. Some functions accept a range of argument counts; for example [substring](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._substring%2529%2529&version=8.18.0.13) accepts either two or three arguments. A function’s arity is the number of arguments that it accepts.

#### 4.3.2 Keyword Arguments

Some functions accept keyword arguments in addition to by-position arguments. For that case, an arg can be an arg-keyword arg-expr sequence instead of just a arg-expr:

> > > [Keywords](keywords.html) introduces keywords.

> > ```
(proc-expr arg ...)

arg   =   arg-expr
    |   arg-keyword arg-expr
```


# 3.2 Numbers
For example,
> (go "super.rkt" #:mode 'fast)
calls the function bound to go with "super.rkt" as a by-position argument, and with 'fast as an argument associated with the #:mode keyword. A keyword is implicitly paired with the expression that follows it.
Since a keyword by itself is not an expression, then
> (go "super.rkt" #:mode #:fast)
is a syntax error. The #:mode keyword must be followed by an expression to produce an argument value, and #:fast is not an expression.
The order of keyword args determines the order in which arg-exprs are evaluated, but a function accepts keyword arguments independent of their position in the argument list. The above call to go can be equivalently written
> (go #:mode 'fast "super.rkt")
> > > <img src="magnify.png" width="24" height="24" alt="+" />Procedure Applications and #%app in The Racket Reference provides more on procedure applications.


#### 4.3.3 The apply Function
The syntax for function calls supports any number of arguments, but a specific call always specifies a fixed number of arguments. As a result, a function that takes a list of arguments cannot directly apply a function like + to all of the items in a list:
> ```
(define (avg lst) ; doesn’t work...
> ( / ( + lst ) ( length lst ) ) )
> ( avg ' ( 1 2 3 ) )
> +: contract violation
> expected: number?
> given: '(1 2 3)```
> ```
(define (avg lst) ; doesn’t always work...
> ( / ( + ( list-ref lst 0 ) ( list-ref lst 1 ) ( list-ref lst 2 ) )
> ( length lst ) ) )
> ( avg ' ( 1 2 3 ) )
> 2
> > ( avg ' ( 1 2 ) )
> list-ref: index too large for list
> index: 2
> in: '(1 2)


# 3.2 Numbers
```
The apply function offers a way around this restriction. It takes a function and a list argument, and it applies the function to the values in the list:

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><pre><code>( define ( avg lst )
> ( / ( apply + lst ) ( length lst ) ) )</code></pre></td></tr><tr><td><p> </p></td></tr><tr><td><pre><code>> ( avg ' ( 1 2 3 ) )
> 2
> > ( avg ' ( 1 2 ) )
> 3/2
> > ( avg ' ( 1 2 3 4 ) )
> 5/2</code></pre></td></tr></tbody></table>

As a convenience, the apply function accepts additional arguments between the function and the list. The additional arguments are effectively consed onto the argument list:

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><pre><code>( define ( anti-sum lst )
> ( apply - 0 lst ) )</code></pre></td></tr><tr><td><p> </p></td></tr><tr><td><pre><code>> ( anti-sum ' ( 1 2 3 ) )
> -6</code></pre></td></tr></tbody></table>

The apply function accepts keyword arguments, too, and it passes them along to the called function:

> ```racket
> ( apply go #:mode ' fast ' ( "super.rkt" ) )
> ( apply go ' ( "super.rkt" ) #:mode ' fast )
> ```

Keywords that are included in apply’s list argument do not count as keyword arguments for the called function; instead, all arguments in this list are treated as by-position arguments. To pass a list of keyword arguments to a function, use the keyword-apply function, which accepts a function to apply and three lists. The first two lists are in parallel, where the first list contains keywords (sorted by keyword<?), and the second list contains a corresponding argument for each keyword. The third list contains by-position function arguments, as for apply.

> ```racket
> ( keyword-apply go
> ' ( #:mode )
> ' ( fast )
> ' ( "super.rkt" ) )
> ```

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

# 4.4 Functions: lambda

### 4.4 Functions (Procedures): lambda

A lambda expression creates a function. In the simplest case, a lambda expression has the form

> > ```
(lambda (arg-id ...)
> > body ...+ )```

A [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) form with n arg-ids accepts n arguments:

> ```racket
> > ( ( lambda ( x ) x ) 1 )
> 1
> > ( ( lambda ( x y ) ( + x y ) ) 1 2 )
> 3
> > ( ( lambda ( x y ) ( + x y ) ) 1 )
> arity mismatch;
> the expected number of arguments does not match the given
> number
> expected: 2
> given: 1
> ```

#### 4.4.1 Declaring a Rest Argument

A [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) expression can also have the form

> > ```
(lambda rest-id
> > body ...+ )
```


# 3.2 Numbers
That is, a lambda expression can have a single rest-id that is not surrounded by parentheses. The resulting function accepts any number of arguments, and the arguments are put into a list bound to rest-id.
Examples:
> ```racket
> > ( ( lambda x x ) 1 2 3 )
> '(1 2 3)
> > ( ( lambda x x ) )
> '()
> > ( ( lambda x ( car x ) ) 1 2 3 )
> 1
> ```
Functions with a rest-id often use apply to call another function that accepts any number of arguments.
> > > <img src="finger.png" width="24" height="24" alt="+" />The apply Function describes apply.
Examples:
> ```racket
> ( define max-mag ( lambda nums ( apply max ( map magnitude nums ) ) ) )
> > ( max 1 -2 0 )
> 1
> > ( max-mag 1 -2 0 )
> 2
> ```
The lambda form also supports required arguments combined with a rest-id:
> > ```
(lambda (arg-id ...+ . rest-id)
> > body ...+ )```
The result of this form is a function that requires at least as many arguments as arg-ids, and also accepts any number of additional arguments.
Examples:
> ```racket
> ( define max-mag ( lambda ( num . nums ) ( apply max ( map magnitude ( cons num nums ) ) ) ) )
> > ( max-mag 1 -2 0 )
> 2
> > ( max-mag )
> max-mag: arity mismatch;
> the expected number of arguments does not match the given
> number
> expected: at least 1
> given: 0
> ```
A rest-id variable is sometimes called a rest argument, because it accepts the “rest” of the function arguments. A function with a rest argument is sometimes called a variadic function, with elements in the rest argument called variadic arguments.


#### 4.4.2 Declaring Optional Arguments
Instead of just an identifier, an argument (other than a rest argument) in a [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) form can be specified with an identifier and a default value:
> > ```
(lambda gen-formals
> > body ...+ )
gen-formals   =   (arg ...)
    |   rest-id
    |   (arg ...+ . rest-id)
arg   =   arg-id
    |   [arg-id default-expr]
```
An argument of the form \[arg-id default-expr\] is optional. When the argument is not supplied in an application, default-expr produces the default value. The default-expr can refer to any preceding arg-id, and every following arg-id must have a default as well.

Examples:

> ```racket
> ( define greet ( lambda ( given [ surname "Smith" ] ) ( string-append "Hello, " given " " surname ) ) )
> > ( greet "John" )
> "Hello, John Smith"
> > ( greet "John" "Doe" )
> "Hello, John Doe"
> ```

> ```
(define greet
> ( lambda ( given [ surname ( if ( equal? given "John" )
> "Doe"
> "Smith" ) ] )
> ( string-append "Hello, " given " " surname ) ) )

> ( greet "John" )
> "Hello, John Doe"
> > ( greet "Adam" )
> "Hello, Adam Smith"```

#### 4.4.3 Declaring Keyword Arguments

A [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) form can declare an argument to be passed by keyword, instead of position. Keyword arguments can be mixed with by-position arguments, and default-value expressions can be supplied for either kind of argument:

> > > <img src="finger.png" width="24" height="24" alt="+" />[Keyword Arguments](application.html#%28part._keyword-args%29) introduces function calls with keywords.

> > ```
(lambda gen-formals
> > body ...+ )

gen-formals   =   (arg ...)
    |   rest-id
    |   (arg ...+ . rest-id)

arg   =   arg-id
    |   [arg-id default-expr]
    |   arg-keyword arg-id
    |   arg-keyword [arg-id default-expr]
```


# 3.2 Numbers
An argument specified as arg-keyword arg-id is supplied by an application using the same arg-keyword. The position of the keyword–identifier pair in the argument list does not matter for matching with arguments in an application, because it will be matched to an argument value by keyword instead of by position.
>
<colgroup><col style="width: 100%" /></colgroup>

( define greet
> ( lambda ( given #:last surname )
> ( string-append "Hello, " given " " surname ) ) )

> ( greet "John" #:last "Smith" )
> "Hello, John Smith"
> > ( greet #:last "Doe" "John" )
> "Hello, John Doe"

An arg-keyword \[arg-id default-expr\] argument specifies a keyword-based argument with a default value.
Examples:
> ```racket
> ( define greet ( lambda ( #:hi [ hi "Hello" ] given #:last [ surname "Smith" ] ) ( string-append hi ", " given " " surname ) ) )
> > ( greet "John" )
> "Hello, John Smith"
> > ( greet "Karl" #:last "Marx" )
> "Hello, Karl Marx"
> > ( greet "John" #:hi "Howdy" )
> "Howdy, John Smith"
> > ( greet "Karl" #:last "Marx" #:hi "Guten Tag" )
> "Guten Tag, Karl Marx"
> ```


# 3.2 Numbers
The lambda form does not directly support the creation of a function that accepts “rest” keywords. To construct a function that accepts all keyword arguments, use make-keyword-procedure. The function supplied to make-keyword-procedure receives keyword arguments through parallel lists in the first two (by-position) arguments, and then all by-position arguments from an application as the remaining by-position arguments.
> > > The apply Function introduces keyword-apply.
Examples:
> ```racket
> ( define ( trace-wrap f ) ( make-keyword-procedure ( lambda ( kws kw-args . rest ) ( printf "Called with ~s ~s ~s\n" kws kw-args rest ) ( keyword-apply f kws kw-args rest ) ) ) )
> > ( ( trace-wrap greet ) "John" #:hi "Howdy" )
> Called with (#:hi) ("Howdy") ("John")
> "Howdy, John Smith"
> ```
> > > Procedure Expressions: lambda and case-lambda in The Racket Reference provides more on function expressions.


#### 4.4.4 Arity-Sensitive Functions: case-lambda
The case-lambda form creates a function that can have completely different behaviors depending on the number of arguments that are supplied. A case-lambda expression has the form
> > ```
(case-lambda
> > [ formals body ...+ ]
> > ... )
formals   =   (arg-id ...)
    |   rest-id
    |   (arg-id ...+ . rest-id)```
where each \[formals body ...+\] is analogous to ([lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) formals body ...+). Applying a function produced by [case-lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._case-lambda%2529%2529&version=8.18.0.13) is like applying a [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) for the first case that matches the number of given arguments.
Examples:


# 3.2 Numbers
> ```racket
> ( define greet ( case-lambda [ ( name ) ( string-append "Hello, " name ) ] [ ( given surname ) ( string-append "Hello, " given " " surname ) ] ) )
> > ( greet "John" )
> "Hello, John"
> > ( greet "John" "Smith" )
> "Hello, John Smith"
> > ( greet )
> greet: arity mismatch;
> the expected number of arguments does not match the given
> number
> given: 0
> ```
A [case-lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._case-lambda%2529%2529&version=8.18.0.13) function cannot directly support optional or keyword arguments.
------------------------------------------------------------------------


# 4.5 Definitions: define


### 4.5 Definitions: [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)
A basic definition has the form
> > |                                                                                |
> > |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | ([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) id expr) |
in which case id is bound to the result of expr.
Examples:
> ```racket
> ( define salutation ( list-ref ' ( "Hi" "Hello" ) ( random 2 ) ) )
> > salutation
> "Hi"
> ```


#### 4.5.1 Function Shorthand
The [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) form also supports a shorthand for function definitions:
> > |                                                                                |
> > |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | ([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) (id arg ...) body ...+) |
which is a shorthand for


### 4.5 Definitions: [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)
> ([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) id ([lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) (arg [...](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=stx-patterns.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fstxcase-scheme..rkt%2529._......%2529%2529&version=8.18.0.13)) body ...+))
Examples:
> ```racket
> ( define ( greet name ) ( string-append salutation ", " name ) )
> > ( greet "John" )
> "Hi, John"
> ```
> ```
(define (greet first [surname "Smith"] #:hi [hi salutation])
> ( string-append hi ", " first " " surname ) )
> ( greet "John" )
> "Hi, John Smith"
> > ( greet "John" #:hi "Hey" )
> "Hey, John Smith"
> > ( greet "John" "Doe" )
> "Hi, John Doe"


### 4.5 Definitions: [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)
```
The function shorthand via define also supports a rest argument (i.e., a final argument to collect extra arguments in a list):

> > |  |
> > |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | (define (id arg ... . rest-id) body ...+) |

which is a shorthand

> (define id (lambda (arg ... . rest-id) body ...+))

Examples:

> ```racket
> ( define ( avg . l ) ( / ( apply + l ) ( length l ) ) )
> > ( avg 1 2 3 )
> 2
> ```

#### 4.5.2 Curried Function Shorthand

Consider the following make-add-suffix function that takes a string and returns another function that takes a string:

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><pre><code>( define make-add-suffix
> ( lambda ( s2 )
> ( lambda ( s ) ( string-append s s2 ) ) ) )</code></pre></td></tr><tr><td><p> </p></td></tr><tr><td><pre><code></code></pre></td></tr></tbody></table>

Although it’s not common, the result of make-add-suffix could be called directly, like this:

> ```racket
> > ( ( make-add-suffix "!" ) "hello" )
> "hello!"
> ```

In a sense, make-add-suffix is a function that takes two arguments, but it takes them one at a time. A function that takes some of its arguments and returns a function to consume more is sometimes called a curried function.

Using the function-shorthand form of define, make-add-suffix can be written equivalently as

> ```racket
> ( define ( make-add-suffix s2 )
> ( lambda ( s ) ( string-append s s2 ) ) )
> ```

This shorthand reflects the shape of the function call (make-add-suffix "!"). The define form further supports a shorthand for defining curried functions that reflects nested function calls:

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><pre><code>( define ( ( make-add-suffix s2 ) s )
> ( string-append s s2 ) )</code></pre></td></tr><tr><td><p> </p></td></tr><tr><td><pre><code>> ( ( make-add-suffix "!" ) "hello" )
> "hello!"</code></pre></td></tr></tbody></table>

> <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td><pre><code>( define louder ( make-add-suffix "!" ) )
> ( define less-sure ( make-add-suffix "?" ) )</code></pre></td></tr><tr><td><p> </p></td></tr><tr><td><pre><code>> ( less-sure "really" )
> "really?"
> > ( louder "really" )
> "really!"</code></pre></td></tr></tbody></table>

The full syntax of the function shorthand for define is as follows:

> > <table data-cellpadding="0" data-cellspacing="0"><colgroup><col style="width: 100%" /></colgroup><tbody><tr><td>(define (head args) body ...+)</td></tr><tr><td> </td></tr><tr><td><table data-cellpadding="0" data-cellspacing="0"><tbody><tr><td style="text-align: right;" data-valign="baseline">head</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">id</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">(head args)</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td></tr><tr><td style="text-align: right;" data-valign="baseline">args</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">=</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">arg ...</td></tr><tr><td style="text-align: right;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: center;" data-valign="baseline">|</td><td style="text-align: left;" data-valign="baseline"> </td><td style="text-align: left;" data-valign="baseline">arg ... . rest-id</td></tr></tbody></table></td></tr></tbody></table>

The expansion of this shorthand has one nested lambda form for each head in the definition, where the innermost head corresponds to the outermost lambda.

#### 4.5.3 Multiple Values and define-values

A Racket expression normally produces a single result, but some expressions can produce multiple results. For example, quotient and remainder each produce a single value, but quotient/remainder produces the same two values at once:

> ```racket
> > ( quotient 13 3 )
> 4
> > ( remainder 13 3 )
> 1
> > ( quotient/remainder 13 3 )
> 4 1
> ```

As shown above, the REPL prints each result value on its own line.

Multiple-valued functions can be implemented in terms of the values function, which takes any number of values and returns them as the results:

> ```racket
> > ( values 1 2 3 )
> 1 2 3
> ```

> ```
(define (split-name name)
> ( let ( [ parts ( regexp-split " " name ) ] )
> ( if ( = ( length parts ) 2 )
> ( values ( list-ref parts 0 ) ( list-ref parts 1 ) )
> ( error "not a <first> <last> name" ) ) ) )

> ( split-name "Adam Smith" )
> "Adam" "Smith"```

The [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) form binds multiple identifiers at once to multiple results produced from a single expression:

> > |                                                                                |
> > |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | ([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) (id ...) expr) |

The number of results produced by the expr must match the number of ids.

Examples:

> ```racket
> ( define-values ( given surname ) ( split-name "Adam Smith" ) )
> > given
> "Adam"
> > surname
> "Smith"
> ```

A [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) form (that is not a function shorthand) is equivalent to a [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) form with a single id.

> > > <img src="magnify.png" width="24" height="24" alt="+" />[Definitions: define, define-syntax, ...](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define.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) provides more on definitions.

#### 4.5.4 Internal Definitions

When the grammar for a syntactic form specifies body, then the corresponding form can be either a definition or an expression. A definition as a body is an internal definition.

Expressions and internal definitions in a body sequence can be mixed, as long as the last body is an expression.

For example, the syntax of [lambda](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.18.0.13) is

> > ```
(lambda gen-formals
> > body ...+ )
```


### 4.5 Definitions: [define](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.18.0.13)
so the following are valid instances of the grammar:
> ```racket
> ( lambda ( f ) ; no definitions
> ( printf "running\n" )
> ( f 0 ) )
> ( lambda ( f ) ; one definition
> ( define ( log-it what )
> ( printf "~a\n" what ) )
> ( log-it "running" )
> ( f 0 )
> ( log-it "done" ) )
> ( lambda ( f n ) ; two definitions
> ( define ( call n )
> ( if ( zero? n )
> ( log-it "done" )
> ( begin
> ( log-it "running" )
> ( f n )
> ( call ( - n 1 ) ) ) ) )
> ( define ( log-it what )
> ( printf "~a\n" what ) )
> ( call n ) )
> ```
Internal definitions in a particular body sequence are mutually recursive; that is, any definition can refer to any other definition—as long as the reference isn’t actually evaluated before its definition takes place. If a definition is referenced too early, an error occurs.
Examples:
> ```racket
> ( define ( weird ) ( define x x ) x )
> > ( weird )
> x: undefined;
> cannot use before initialization
> ```
A sequence of internal definitions using just define is easily translated to an equivalent letrec form (as introduced in the next section). However, other definition forms can appear as a body, including define-values, struct (see Programmer-Defined Datatypes) or define-syntax (see Macros).


### 4.5 Definitions: [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)
> > > <img src="magnify.png" width="24" height="24" alt="+" />Internal Definitions in The Racket Reference documents the fine points of internal definitions.
------------------------------------------------------------------------


# 4.6 Local Binding


### 4.6 Local Binding
Although internal defines can be used for local binding, Racket provides three forms that give the programmer more control over bindings: let, let*, and letrec.


#### 4.6.1 Parallel Binding: let
> > > <img src="magnify.png" width="24" height="24" alt="+" />Local Binding: let, let*, letrec, ... in The Racket Reference also documents let.
A let form binds a set of identifiers, each to the result of some expression, for use in the let body:
> > |  |
> > |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | (let (\[id expr\] ...) body ...+) |
The ids are bound “in parallel.” That is, no id is bound in the right-hand side expr for any id, but all are available in the body. The ids must be different from each other.
Examples:
> ```racket
> > ( let ( [ me "Bob" ] ) me )
> "Bob"
> > ( let ( [ me "Bob" ] [ myself "Robert" ] [ I "Bobby" ] ) ( list me myself I ) )
> '("Bob" "Robert" "Bobby")
> > ( let ( [ me "Bob" ] [ me "Robert" ] ) me )
> eval:3:0: let: duplicate identifier
> at: me
> in: (let ((me "Bob") (me "Robert")) me)
> ```
The fact that an id’s expr does not see its own binding is often useful for wrappers that must refer back to the old value:
> ```racket
> > ( let ( [ + ( lambda ( x y ) ( if ( string? x ) ( string-append x y ) ( + x y ) ) ) ] ) ; use original + ( list ( + 1 2 ) ( + "see" "saw" ) ) )
> '(3 "seesaw")
> ```
Occasionally, the parallel nature of let bindings is convenient for swapping or rearranging a set of bindings:


# 4.6 Local Binding
> ```racket
> > ( let ( [ me "Tarzan" ] [ you "Jane" ] ) ( let ( [ me you ] [ you me ] ) ( list me you ) ) )
> '("Jane" "Tarzan")
> ```
The characterization of let bindings as “parallel” is not meant to imply concurrent evaluation. The exprs are evaluated in order, even though the bindings are delayed until all exprs are evaluated.


#### 4.6.2 Sequential Binding: let*
> > > <img src="magnify.png" width="24" height="24" alt="+" />Local Binding: let, let*, letrec, ... in The Racket Reference also documents let*.
The syntax of let* is the same as let:
> > |  |
> > |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | (let* (\[id expr\] ...) body ...+) |
The difference is that each id is available for use in later exprs, as well as in the body. Furthermore, the ids need not be distinct, and the most recent binding is the visible one.
Examples:
> ```racket
> > ( let* ( [ x ( list "Burroughs" ) ] [ y ( cons "Rice" x ) ] [ z ( cons "Edgar" y ) ] ) ( list x y z ) )
> '(("Burroughs") ("Rice" "Burroughs") ("Edgar" "Rice" "Burroughs"))
> > ( let* ( [ name ( list "Burroughs" ) ] [ name ( cons "Rice" name ) ] [ name ( cons "Edgar" name ) ] ) name )
> '("Edgar" "Rice" "Burroughs")
> ```
In other words, a let* form is equivalent to nested let forms, each with a single binding:
> ```racket
> > ( let ( [ name ( list "Burroughs" ) ] ) ( let ( [ name ( cons "Rice" name ) ] ) ( let ( [ name ( cons "Edgar" name ) ] ) name ) ) )
> '("Edgar" "Rice" "Burroughs")
> ```


#### 4.6.3 Recursive Binding: letrec
> > > <img src="magnify.png" width="24" height="24" alt="+" />Local Binding: let, let*, letrec, ... in The Racket Reference also documents letrec.
The syntax of letrec is also the same as let:
> > |  |
> > |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> > | (letrec (\[id expr\] ...) body ...+) |
While let makes its bindings available only in the bodys, and let* makes its bindings available to any later binding expr, letrec makes its bindings available to all other exprs—even earlier ones. In other words, letrec bindings are recursive.
The exprs in a letrec form are most often lambda forms for recursive and mutually recursive functions:
> ```racket
> > ( letrec ( [ swing ( lambda ( t ) ( if ( eq? ( car t ) ' tarzan ) ( cons ' vine ( cons ' tarzan ( cddr t ) ) ) ( cons ( car t ) ( swing ( cdr t ) ) ) ) ) ] ) ( swing ' ( vine tarzan vine vine ) ) )
> '(vine vine tarzan vine)
> ```


# 4.6 Local Binding
> ```racket
> > ( letrec ( [ tarzan-near-top-of-tree? ( lambda ( name path depth ) ( or ( equal? name "tarzan" ) ( and ( directory-exists? path ) ( tarzan-in-directory? path depth ) ) ) ) ] [ tarzan-in-directory? ( lambda ( dir depth ) ( cond [ ( zero? depth ) #f ] [ else ( ormap ( λ ( elem ) ( tarzan-near-top-of-tree? ( path-element  →  string elem ) ( build-path dir elem ) ( - depth 1 ) ) ) ( directory-list dir ) ) ] ) ) ] ) ( tarzan-near-top-of-tree? "tmp" ( find-system-path ' temp-dir ) 4 ) )
> #f
> ```
While the exprs of a letrec form are typically lambda expressions, they can be any expression. The expressions are evaluated in order, and after each value is obtained, it is immediately associated with its corresponding id. If an id is referenced before its value is ready, an error is raised, just as for internal definitions.
> ```racket
> > ( letrec ( [ quicksand quicksand ] ) quicksand )
> quicksand: undefined;
> cannot use before initialization
> ```