On this page:
2.2.1 Definitions
2.2.2 An Aside on Indenting Code
2.2.3 Identifiers
2.2.4 Function Calls
2.2.5 Conditionals with if, and, or, and cond
2.2.6 Function Calls, Again
2.2.7 Anonymous Functions with lambda
2.2.8 Local Binding with define, let, and let*

2.2 Simple Definitions and Expressions🔗ℹ

A program module is written as

#lang langname topform*

where a topform is either a definition or an expr. The REPL also evaluates topforms.

In syntax specifications, text with a gray background, such as #lang, represents literal text. Whitespace must appear between such literals and nonterminals like id, except that whitespace is not required before or after (, ), [, or ]. A comment, which starts with ; and runs until the end of the line, is treated the same as whitespace.

+Reading Comments in The Racket Reference provides more on different forms of comments.

Following the usual conventions, * in a grammar means zero or more repetitions of the preceding element, + means one or more repetitions of the preceding element, and {} groups a sequence as an element for repetition.

2.2.1 Definitions🔗ℹ

A definition of the form

+Definitions: define (later in this guide) explains more about definitions.

( define id expr )

binds id to the result of expr, while

( define ( id id* ) expr+ )

binds the first id to a function (also called a procedure) that takes arguments as named by the remaining ids. In the function case, the exprs are the body of the function. When the function is called, it returns the result of the last expr.

Examples:
(define pie 3)             ; defines pie to be 3
(define (piece str)        ; defines piece as a function
  (substring str 0 pie))   ;  of one argument
> pie

3

> (piece "key lime")

"key"

Under the hood, a function definition is really the same as a non-function definition, and a function name does not have to be used in a function call. A function is just another kind of value, though the printed form is necessarily less complete than the printed form of a number or string.

Examples:
> piece

#<procedure:piece>

> substring

#<procedure:substring>

A function definition can include multiple expressions for the function’s body. In that case, only the value of the last expression is returned when the function is called. The other expressions are evaluated only for some side-effect, such as printing.

Examples:
(define (bake flavor)
  (printf "preheating oven...\n")
  (string-append flavor " pie"))
> (bake "apple")

preheating oven...

"apple pie"

Racket programmers prefer to avoid side-effects, so a definition usually has just one expression in its body. It’s important, though, to understand that multiple expressions are allowed in a definition body, because it explains why the following nobake function fails to include its argument in its result:

(define (nobake flavor)
  string-append flavor "jello")

 

> (nobake "green")

"jello"

Within nobake, there are no parentheses around string-append flavor "jello", so they are three separate expressions instead of one function-call expression. The expressions string-append and flavor are evaluated, but the results are never used. Instead, the result of the function is just the result of the final expression, "jello".

2.2.2 An Aside on Indenting Code🔗ℹ

Line breaks and indentation are not significant for parsing Racket programs, but most Racket programmers use a standard set of conventions to make code more readable. For example, the body of a definition is typically indented under the first line of the definition. Identifiers are written immediately after an open parenthesis with no extra space, and closing parentheses never go on their own line.

DrRacket automatically indents according to the standard style when you type Enter in a program or REPL expression. For example, if you hit Enter after typing (define (greet name), then DrRacket automatically inserts two spaces for the next line. If you change a region of code, you can select it in DrRacket and hit Tab, and DrRacket will re-indent the code (without inserting any line breaks). Editors like Emacs offer a Racket or Scheme mode with similar indentation support.

Re-indenting not only makes the code easier to read, it gives you extra feedback that your parentheses match in the way that you intended. For example, if you leave out a closing parenthesis after the last argument to a function, automatic indentation starts the next line under the first argument, instead of under the define keyword:

(define (halfbake flavor
                  (string-append flavor " creme brulee")))

In this case, indentation helps highlight the mistake. In other cases, where the indentation may be normal while an open parenthesis has no matching close parenthesis, both racket and DrRacket use the source’s indentation to suggest where a parenthesis might be missing.

2.2.3 Identifiers🔗ℹ

Racket’s syntax for identifiers is especially liberal. Excluding the special characters

+Identifiers and Binding (later in this guide) explains more about identifiers.

   ( ) [ ] { } " , ' ` ; # | \

and except for the sequences of characters that make number constants, almost any sequence of non-whitespace characters forms an id. For example substring is an identifier. Also, string-append and a+b are identifiers, as opposed to arithmetic expressions. Here are several more examples:

+
integer?
pass/fail
Hfuhruhurr&Uumellmahaye
john-jacob-jingleheimer-schmidt
a-b-c+1-2-3
2.2.4 Function Calls (Procedure Applications)🔗ℹ

We have already seen many function calls, which are called procedure applications in more traditional terminology. The syntax of a function call is

+Function Calls (later in this guide) explains more about function calls.

( id expr* )

where the number of exprs determines the number of arguments supplied to the function named by id.

The racket language pre-defines many function identifiers, such as substring and string-append. More examples are below.

In example Racket code throughout the documentation, uses of pre-defined names are hyperlinked to the reference manual. So, you can click on an identifier to get full details about its use.

> (string-append "rope" "twine" "yarn")  ; append strings

"ropetwineyarn"

> (substring "corduroys" 0 4)            ; extract a substring

"cord"

> (string-prefix? "shoelace" "shoe")     ; recognize string prefix/suffix

#t

> (string-suffix? "shoelace" "shoe")

#f

> (string? "Ceci n'est pas une string.") ; recognize strings

#t

> (string? 1)

#f

> (sqrt 16)                              ; find a square root

4

> (sqrt -16)

0+4i

> (+ 1 2)                                ; add numbers

3

> (- 2 1)                                ; subtract numbers

1

> (< 2 1)                                ; compare numbers

#f

> (>= 2 1)

#t

> (number? "c'est une number")           ; recognize numbers

#f

> (number? 1)

#t

> (equal? 6 "half dozen")                ; compare anything

#f

> (equal? 6 6)

#t

> (equal? "half dozen" "half dozen")

#t

2.2.5 Conditionals with if, and, or, and cond🔗ℹ

The next simplest kind of expression is an if conditional:

( if expr expr expr )

+Conditionals (later in this guide) explains more about conditionals.

The first expr is always evaluated. If it produces a non-#f value, then the second expr is evaluated for the result of the whole if expression, otherwise the third expr is evaluated for the result.

Example:
> (if (> 2 3)
      "2 is bigger than 3"
      "2 is smaller than 3")

"2 is smaller than 3"

(define (reply s)
  (if (string-prefix? s "hello ")
      "hi!"
      "huh?"))

 

> (reply "hello racket")

"hi!"

> (reply "λx:(μα.α→α).xx")

"huh?"

Complex conditionals can be formed by nesting if expressions. For example, in the previous reply example, the input must be a string because string-prefix? would error when given non-strings. You can remove this restriction by adding another if to check first if the input is a string:

(define (reply-non-string s)
  (if (string? s)
      (if (string-prefix? s "hello ")
          "hi!"
          "huh?")
      "huh?"))

Instead of duplicating the "huh?" case, this function is better written as

(define (reply-non-string s)
  (if (if (string? s)
          (string-prefix? s "hello ")
          #f)
      "hi!"
      "huh?"))

but these kinds of nested ifs are difficult to read. Racket provides more readable shortcuts through the and and or forms:

+Combining Tests: and and or (later in this guide) explains more about and and or.

( and expr* )
( or expr* )

The and form short-circuits: it stops and returns #f when an expression produces #f, otherwise it keeps going. The or form similarly short-circuits when it encounters a true result.

Examples:
(define (reply-non-string s)
  (if (and (string? s) (string-prefix? s "hello "))
      "hi!"
      "huh?"))
> (reply-non-string "hello racket")

"hi!"

> (reply-non-string 17)

"huh?"

Note that in the above grammar, the and and or forms work with any number of expressions.

Examples:
(define (reply-only-enthusiastic s)
  (if (and (string? s)
           (string-prefix? s "hello ")
           (string-suffix? s "!"))
      "hi!"
      "huh?"))
> (reply-only-enthusiastic "hello racket!")

"hi!"

> (reply-only-enthusiastic "hello racket")

"huh?"

Another common pattern of nested ifs involves a sequence of tests, each with its own result:

(define (reply-more s)
  (if (string-prefix? s "hello ")
      "hi!"
      (if (string-prefix? s "goodbye ")
          "bye!"
          (if (string-suffix? s "?")
              "I don't know"
              "huh?"))))

The shorthand for a sequence of tests is the cond form:

+Chaining Tests: cond (later in this guide) explains more about cond.

( cond {[ expr expr* ]}* )

A cond form contains a sequence of clauses between square brackets. In each clause, the first expr is a test expression. If it produces true, then the clause’s remaining exprs are evaluated, and the last one in the clause provides the answer for the entire cond expression; the rest of the clauses are ignored. If the test expr produces #f, then the clause’s remaining exprs are ignored, and evaluation continues with the next clause. The last clause can use else as a synonym for a #t test expression.

Using cond, the reply-more function can be more clearly written as follows:

(define (reply-more s)
  (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:

+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:

(define (double v)
  ((if (string? v) string-append +) v v))

 

> (double "mnah")

"mnahmnah"

> (double 5)

10

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.

> (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

+Functions: lambda (later in this guide) explains more about lambda.

> (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:

(define (twice f v)
  (f (f v)))

 

> (twice sqrt 16)

2

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

(define (louder s)
  (string-append s "!"))

 

> (twice louder "hello")

"hello!!"

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:

> (lambda (s) (string-append s "!"))

#<procedure>

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

> (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-generated function “remembers” the right s2:

> (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 id expr) as “non-function definitions.” This characterization is misleading, because the expr could be a lambda form, in which case the definition is equivalent to using the “function” definition form. For example, the following two definitions of louder are equivalent:

(define (louder s)
  (string-append s "!"))
 
(define louder
  (lambda (s)
    (string-append s "!")))

 

> louder

#<procedure:louder>

Note that the expression for louder in the second case is an “anonymous” function written with lambda, 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, let, and let*🔗ℹ

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:

+Internal Definitions (later in this guide) explains more about local (internal) definitions.

( define ( id id* ) definition* expr+ )
( lambda ( id* ) definition* expr+ )

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

Examples:
(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 form. An advantage of let is that it can be used in any expression position. Also, let binds many identifiers at once, instead of requiring a separate define for each identifier.

+Internal Definitions (later in this guide) explains more about let and let*.

( 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. In each clause, the id is bound to the result of the expr for use in the body.

> (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 form are available only in the body of the let, so the binding clauses cannot refer to each other. The let* form, in contrast, allows later clauses to use earlier bindings:

> (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 1"