# The Racket Guide
8.18.0.13


# The Racket Guide
This guide is intended for programmers who are new to Racket or new to some part of Racket. It assumes programming experience, so if you are new to programming, consider instead reading [How to Design Programs](https://htdp.org). If you want an especially quick introduction to Racket, start with [Quick: An Introduction to Racket with Pictures](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?tag=%28part._%28.%27%28lib._scribblings%2Fquick%2Fquick..scrbl%29.%27._.%27top.%27%29%29&version=8.18.0.13).
[Chapter 2](to-scheme.html) provides a brief introduction to Racket. From [Chapter 3](datatypes.html) on, this guide dives into details—covering much of the Racket toolbox, but leaving precise details to [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) and other reference manuals.
> > > The source of this manual is available on [GitHub](https://github.com/racket/racket/tree/master/pkgs/racket-doc/scribblings/guide).


# The Racket Guide
|                                                                                |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|     [1 Welcome to Racket](intro.html)                                                                                |
|       [1.1 Interacting with Racket](intro.html#%28part._.Interacting_with_.Racket%29)                                                                   |
|       [1.2 Definitions and Interactions](intro.html#%28part._.Definitions_and_.Interactions%29)                                                         |
|       [1.3 Creating Executables](intro.html#%28part._.Creating_.Executables%29)                                                                         |
|       [1.4 A Note to Readers with Lisp/Scheme Experience](intro.html#%28part._use-module%29)                                                            |
|                                                                                |
|     [2 Racket Essentials](to-scheme.html)                                                                                |
|       [2.1 Simple Values](Simple_Values.html)                                                                                |
|       [2.2 Simple Definitions and Expressions](syntax-overview.html)                                                                                |
|         [2.2.1 Definitions](syntax-overview.html#%28part._.Definitions%29)                                                                              |
|         [2.2.2 An Aside on Indenting Code](syntax-overview.html#%28part._indentation%29)                                                                |
|         [2.2.3 Identifiers](syntax-overview.html#%28part._.Identifiers%29)                                                                              |
|         [2.2.4 Function Calls (Procedure Applications)](syntax-overview.html#%28part._.Function_.Calls__.Procedure_.Applications_%29)                   |
|         [2.2.5 Conditionals with if, and, or, and cond](syntax-overview.html#%28part._.Conditionals_with_if__and__or__and_cond%29)                      |
|         [2.2.6 Function Calls, Again](syntax-overview.html#%28part._.Function_.Calls__.Again%29)                                                        |
|         [2.2.7 Anonymous Functions with lambda](syntax-overview.html#%28part._.Anonymous_.Functions_with_lambda%29)                                     |
|         [2.2.8 Local Binding with define, let, and let*](syntax-overview.html#%28part._local-binding-intro%29)                                          |
|       [2.3 Lists, Iteration, and Recursion](Lists__Iteration__and_Recursion.html)                                                                       |
|         [2.3.1 Predefined List Loops](Lists__Iteration__and_Recursion.html#%28part._.Predefined_.List_.Loops%29)                                        |
|         [2.3.2 List Iteration from Scratch](Lists__Iteration__and_Recursion.html#%28part._.List_.Iteration_from_.Scratch%29)                            |
|         [2.3.3 Tail Recursion](Lists__Iteration__and_Recursion.html#%28part._tail-recursion%29)                                                         |
|         [2.3.4 Recursion versus Iteration](Lists__Iteration__and_Recursion.html#%28part._.Recursion_versus_.Iteration%29)                               |
|       [2.4 Pairs, Lists, and Racket Syntax](Pairs__Lists__and_Racket_Syntax.html)                                                                       |
|         [2.4.1 Quoting Pairs and Symbols with quote](Pairs__Lists__and_Racket_Syntax.html#%28part._quoting-lists%29)                                    |
|         [2.4.2 Abbreviating quote with '](Pairs__Lists__and_Racket_Syntax.html#%28part._.Abbreviating_quote_with__%29)                                  |
|         [2.4.3 Lists and Racket Syntax](Pairs__Lists__and_Racket_Syntax.html#%28part._lists-and-syntax%29)                                              |
|                                                                                |
|     [3 Built-In Datatypes](datatypes.html)                                                                                |
|       [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)                                                                                |
|                                                                                |
|     [4 Expressions and Definitions](scheme-forms.html)                                                                                |
|       [4.1 Notation](syntax-notation.html)                                                                                |
|       [4.2 Identifiers and Binding](binding.html)                                                                                |
|       [4.3 Function Calls (Procedure Applications)](application.html)                                                                                |
|         [4.3.1 Evaluation Order and Arity](application.html#%28part._.Evaluation_.Order_and_.Arity%29)                                                  |
|         [4.3.2 Keyword Arguments](application.html#%28part._keyword-args%29)                                                                            |
|         [4.3.3 The apply Function](application.html#%28part._apply%29)                                                                                |
|       [4.4 Functions (Procedures): lambda](lambda.html)                                                                                |
|         [4.4.1 Declaring a Rest Argument](lambda.html#%28part._rest-args%29)                                                                            |
|         [4.4.2 Declaring Optional Arguments](lambda.html#%28part._.Declaring_.Optional_.Arguments%29)                                                   |
|         [4.4.3 Declaring Keyword Arguments](lambda.html#%28part._lambda-keywords%29)                                                                    |
|         [4.4.4 Arity-Sensitive Functions: case-lambda](lambda.html#%28part._case-lambda%29)                                                             |
|       [4.5 Definitions: define](define.html)                                                                                |
|         [4.5.1 Function Shorthand](define.html#%28part._.Function_.Shorthand%29)                                                                        |
|         [4.5.2 Curried Function Shorthand](define.html#%28part._.Curried_.Function_.Shorthand%29)                                                       |
|         [4.5.3 Multiple Values and define-values](define.html#%28part._multiple-values%29)                                                              |
|         [4.5.4 Internal Definitions](define.html#%28part._intdefs%29)                                                                                |
|       [4.6 Local Binding](let.html)                                                                                |
|         [4.6.1 Parallel Binding: let](let.html#%28part._.Parallel_.Binding__let%29)                                                                     |
|         [4.6.2 Sequential Binding: let*](let.html#%28part._.Sequential_.Binding__let_%29)                                                               |
|         [4.6.3 Recursive Binding: letrec](let.html#%28part._.Recursive_.Binding__letrec%29)                                                             |
|         [4.6.4 Named let](let.html#%28part._.Named_let%29)                                                                                |
|         [4.6.5 Multiple Values: let-values, let*-values, letrec-values](let.html#%28part._.Multiple_.Values__let-values__let_-values__letrec-values%29) |
|       [4.7 Conditionals](conditionals.html)                                                                                |
|         [4.7.1 Simple Branching: if](conditionals.html#%28part._.Simple_.Branching__if%29)                                                              |
|         [4.7.2 Combining Tests: and and or](conditionals.html#%28part._and%2Bor%29)                                                                     |
|         [4.7.3 Chaining Tests: cond](conditionals.html#%28part._cond%29)                                                                                |
|       [4.8 Sequencing](begin.html)                                                                                |
|         [4.8.1 Effects Before: begin](begin.html#%28part._.Effects_.Before__begin%29)                                                                   |
|         [4.8.2 Effects After: begin0](begin.html#%28part._.Effects_.After__begin0%29)                                                                   |
|         [4.8.3 Effects If...: when and unless](begin.html#%28part._when%2Bunless%29)                                                                    |
|       [4.9 Assignment: set!](set_.html)                                                                                |
|         [4.9.1 Guidelines for Using Assignment](set_.html#%28part._using-set%21%29)                                                                     |
|         [4.9.2 Multiple Values: set!-values](set_.html#%28part._.Multiple_.Values__set_-values%29)                                                      |
|       [4.10 Quoting: quote and ’](quote.html)                                                                                |
|       [4.11 Quasiquoting: quasiquote and ‘](qq.html)                                                                                |
|       [4.12 Simple Dispatch: case](case.html)                                                                                |
|       [4.13 Dynamic Binding: parameterize](parameterize.html)                                                                                |
|                                                                                |
|     [5 Programmer-Defined Datatypes](define-struct.html)                                                                                |
|       [5.1 Simple Structure Types: struct](define-struct.html#%28part._.Simple_.Structure_.Types__struct%29)                                            |
|       [5.2 Copying and Update](define-struct.html#%28part._struct-copy%29)                                                                              |
|       [5.3 Structure Subtypes](define-struct.html#%28part._struct-subtypes%29)                                                                          |
|       [5.4 Opaque versus Transparent Structure Types](define-struct.html#%28part._trans-struct%29)                                                      |
|       [5.5 Structure Comparisons](define-struct.html#%28part._struct-equal%29)                                                                          |
|       [5.6 Structure Type Generativity](define-struct.html#%28part._.Structure_.Type_.Generativity%29)                                                  |
|       [5.7 Prefab Structure Types](define-struct.html#%28part._prefab-struct%29)                                                                        |
|       [5.8 More Structure Type Options](define-struct.html#%28part._struct-options%29)                                                                  |
|                                                                                |
|     [6 Modules](modules.html)                                                                                |
|       [6.1 Module Basics](module-basics.html)                                                                                |
|         [6.1.1 Organizing Modules](module-basics.html#%28part._module-org%29)                                                                           |
|         [6.1.2 Library Collections](module-basics.html#%28part._.Library_.Collections%29)                                                               |
|         [6.1.3 Packages and Collections](module-basics.html#%28part._packages-and-collections%29)                                                       |
|         [6.1.4 Adding Collections](module-basics.html#%28part._link-collection%29)                                                                      |
|         [6.1.5 Module References Within a Collection](module-basics.html#%28part._intracollection%29)                                                   |
|       [6.2 Module Syntax](Module_Syntax.html)                                                                                |
|         [6.2.1 The module Form](Module_Syntax.html#%28part._module-syntax%29)                                                                           |
|         [6.2.2 The #lang Shorthand](Module_Syntax.html#%28part._hash-lang%29)                                                                           |
|         [6.2.3 Submodules](Module_Syntax.html#%28part._submodules%29)                                                                                |
|         [6.2.4 Main and Test Submodules](Module_Syntax.html#%28part._main-and-test%29)                                                                  |
|       [6.3 Module Paths](module-paths.html)                                                                                |
|       [6.4 Imports: require](module-require.html)                                                                                |
|       [6.5 Exports: provide](module-provide.html)                                                                                |
|       [6.6 Assignment and Redefinition](module-set.html)                                                                                |
|       [6.7 Modules and Macros](module-macro.html)                                                                                |
|       [6.8 Protected Exports](protect-out.html)                                                                                |
|                                                                                |
|     [7 Contracts](contracts.html)                                                                                |
|       [7.1 Contracts and Boundaries](contract-boundaries.html)                                                                                |
|         [7.1.1 Contract Violations](contract-boundaries.html#%28part._contracts-amount0%29)                                                             |
|         [7.1.2 Experimenting with Contracts and Modules](contract-boundaries.html#%28part._.Experimenting_with_.Contracts_and_.Modules%29)              |
|         [7.1.3 Experimenting with Nested Contract Boundaries](contract-boundaries.html#%28part._contracts-intro-nested%29)                              |
|       [7.2 Simple Contracts on Functions](contract-func.html)                                                                                |
|         [7.2.1 Styles of ->](contract-func.html#%28part._.Styles_of_-_%29)                                                                           |
|         [7.2.2 Using define/contract and ->](contract-func.html#%28part._simple-nested%29)                                                           |
|         [7.2.3 any and any/c](contract-func.html#%28part._any_and_any_c%29)                                                                             |
|         [7.2.4 Rolling Your Own Contracts](contract-func.html#%28part._contracts-own%29)                                                                |
|         [7.2.5 Contracts on Higher-order Functions](contract-func.html#%28part._.Contracts_on_.Higher-order_.Functions%29)                              |
|         [7.2.6 Contract Messages with “???”](contract-func.html#%28part._contracts-flat-named-contracts%29)                                             |
|         [7.2.7 Dissecting a contract error message](contract-func.html#%28part._contracts-dissecting-contract-errors%29)                                |
|       [7.3 Contracts on Functions in General](contracts-general-functions.html)                                                                         |
|         [7.3.1 Optional Arguments](contracts-general-functions.html#%28part._contracts-optional%29)                                                     |
|         [7.3.2 Rest Arguments](contracts-general-functions.html#%28part._contracts-rest-args%29)                                                        |
|         [7.3.3 Keyword Arguments](contracts-general-functions.html#%28part._contracts-keywords%29)                                                      |
|         [7.3.4 Optional Keyword Arguments](contracts-general-functions.html#%28part._contracts-optional-keywords%29)                                    |
|         [7.3.5 Contracts for case-lambda](contracts-general-functions.html#%28part._contracts-case-lambda%29)                                           |
|         [7.3.6 Argument and Result Dependencies](contracts-general-functions.html#%28part._contracts-arrow-d%29)                                        |
|         [7.3.7 Checking State Changes](contracts-general-functions.html#%28part._contracts-arrow-d-eval-order%29)                                       |
|         [7.3.8 Multiple Result Values](contracts-general-functions.html#%28part._contracts-multiple%29)                                                 |
|         [7.3.9 Fixed but Statically Unknown Arities](contracts-general-functions.html#%28part._contracts-no-domain%29)                                  |
|       [7.4 Contracts: A Thorough Example](contracts-first.html)                                                                                |
|       [7.5 Contracts on Structures](contracts-struct.html)                                                                                |
|         [7.5.1 Guarantees for a Specific Value](contracts-struct.html#%28part._contracts-single-struct%29)                                              |
|         [7.5.2 Guarantees for All Values](contracts-struct.html#%28part._contracts-define-struct%29)                                                    |
|         [7.5.3 Checking Properties of Data Structures](contracts-struct.html#%28part._contracts-lazy-contracts%29)                                      |
|       [7.6 Abstract Contracts using #:exists and #:∃](contracts-exists.html)                                                                            |
|       [7.7 Additional Examples](contracts-examples.html)                                                                                |
|         [7.7.1 A Customer-Manager Component](contracts-examples.html#%28part._.A_.Customer-.Manager_.Component%29)                                      |
|         [7.7.2 A Parameteric (Simple) Stack](contracts-examples.html#%28part._.A_.Parameteric__.Simple__.Stack%29)                                      |
|         [7.7.3 A Dictionary](contracts-examples.html#%28part._.A_.Dictionary%29)                                                                        |
|         [7.7.4 A Queue](contracts-examples.html#%28part._.A_.Queue%29)                                                                                |
|       [7.8 Building New Contracts](Building_New_Contracts.html)                                                                                |
|         [7.8.1 Contract Struct Properties](Building_New_Contracts.html#%28part._.Contract_.Struct_.Properties%29)                                       |
|         [7.8.2 With all the Bells and Whistles](Building_New_Contracts.html#%28part._.With_all_the_.Bells_and_.Whistles%29)                             |
|       [7.9 Gotchas](contracts-gotchas.html)                                                                                |
|         [7.9.1 Contracts and eq?](contracts-gotchas.html#%28part._.Contracts_and_eq_%29)                                                                |
|         [7.9.2 Contract boundaries and define/contract](contracts-gotchas.html#%28part._contracts-gotcha-nested%29)                                     |
|         [7.9.3 Exists Contracts and Predicates](contracts-gotchas.html#%28part._contracts-exists-gotcha%29)                                             |
|         [7.9.4 Defining Recursive Contracts](contracts-gotchas.html#%28part._.Defining_.Recursive_.Contracts%29)                                        |
|         [7.9.5 Mixing set! and contract-out](contracts-gotchas.html#%28part._.Mixing_set__and_contract-out%29)                                          |
|                                                                                |
|     [8 Input and Output](i_o.html)                                                                                |
|       [8.1 Varieties of Ports](ports.html)                                                                                |
|       [8.2 Default Ports](default-ports.html)                                                                                |
|       [8.3 Reading and Writing Racket Data](read-write.html)                                                                                |
|       [8.4 Datatypes and Serialization](serialization.html)                                                                                |
|       [8.5 Bytes, Characters, and Encodings](encodings.html)                                                                                |
|       [8.6 I/O Patterns](io-patterns.html)                                                                                |
|                                                                                |
|     [9 Regular Expressions](regexp.html)                                                                                |
|       [9.1 Writing Regexp Patterns](regexp-intro.html)                                                                                |
|       [9.2 Matching Regexp Patterns](regexp-match.html)                                                                                |
|       [9.3 Basic Assertions](regexp-assert.html)                                                                                |
|       [9.4 Characters and Character Classes](regexp-chars.html)                                                                                |
|         [9.4.1 Some Frequently Used Character Classes](regexp-chars.html#%28part._.Some_.Frequently_.Used_.Character_.Classes%29)                       |
|         [9.4.2 POSIX character classes](regexp-chars.html#%28part._.P.O.S.I.X_character_classes%29)                                                     |
|       [9.5 Quantifiers](regexp-quant.html)                                                                                |
|       [9.6 Clusters](regexp-clusters.html)                                                                                |
|         [9.6.1 Backreferences](regexp-clusters.html#%28part._.Backreferences%29)                                                                        |
|         [9.6.2 Non-capturing Clusters](regexp-clusters.html#%28part._.Non-capturing_.Clusters%29)                                                       |
|         [9.6.3 Cloisters](regexp-clusters.html#%28part._regexp-cloister%29)                                                                             |
|       [9.7 Alternation](regexp-alternation.html)                                                                                |
|       [9.8 Backtracking](Backtracking.html)                                                                                |
|       [9.9 Looking Ahead and Behind](Looking_Ahead_and_Behind.html)                                                                                |
|         [9.9.1 Lookahead](Looking_Ahead_and_Behind.html#%28part._.Lookahead%29)                                                                         |
|         [9.9.2 Lookbehind](Looking_Ahead_and_Behind.html#%28part._.Lookbehind%29)                                                                       |
|       [9.10 An Extended Example](An_Extended_Example.html)                                                                                |
|                                                                                |
|     [10 Exceptions and Control](control.html)                                                                                |
|       [10.1 Exceptions](exns.html)                                                                                |
|       [10.2 Prompts and Aborts](prompt.html)                                                                                |
|       [10.3 Continuations](conts.html)                                                                                |
|                                                                                |
|     [11 Iterations and Comprehensions](for.html)                                                                                |
|       [11.1 Sequence Constructors](for.html#%28part._sequences%29)                                                                                |
|       [11.2 for and for*](for.html#%28part._for_and_for_%29)                                                                                |
|       [11.3 for/list and for*/list](for.html#%28part._for_list_and_for__list%29)                                                                        |
|       [11.4 for/vector and for*/vector](for.html#%28part._for_vector_and_for__vector%29)                                                                |
|       [11.5 for/and and for/or](for.html#%28part._for_and_and_for_or%29)                                                                                |
|       [11.6 for/first and for/last](for.html#%28part._for_first_and_for_last%29)                                                                        |
|       [11.7 for/fold and for*/fold](for.html#%28part._for%2Ffold%29)                                                                                |
|       [11.8 Multiple-Valued Sequences](for.html#%28part._.Multiple-.Valued_.Sequences%29)                                                               |
|       [11.9 Breaking an Iteration](for.html#%28part._.Breaking_an_.Iteration%29)                                                                        |
|       [11.10 Iteration Performance](for.html#%28part._for-performance%29)                                                                               |
|                                                                                |
|     [12 Pattern Matching](match.html)                                                                                |
|                                                                                |
|     [13 Classes and Objects](classes.html)                                                                                |
|       [13.1 Methods](classes.html#%28part._methods%29)                                                                                |
|       [13.2 Initialization Arguments](classes.html#%28part._initargs%29)                                                                                |
|       [13.3 Internal and External Names](classes.html#%28part._intnames%29)                                                                             |
|       [13.4 Interfaces](classes.html#%28part._.Interfaces%29)                                                                                |
|       [13.5 Final, Augment, and Inner](classes.html#%28part._inner%29)                                                                                |
|       [13.6 Controlling the Scope of External Names](classes.html#%28part._extnames%29)                                                                 |
|       [13.7 Mixins](classes.html#%28part._.Mixins%29)                                                                                |
|         [13.7.1 Mixins and Interfaces](classes.html#%28part._.Mixins_and_.Interfaces%29)                                                                |
|         [13.7.2 The mixin Form](classes.html#%28part._.The_mixin_.Form%29)                                                                              |
|         [13.7.3 Parameterized Mixins](classes.html#%28part._parammixins%29)                                                                             |
|       [13.8 Traits](classes.html#%28part._.Traits%29)                                                                                |
|         [13.8.1 Traits as Sets of Mixins](classes.html#%28part._.Traits_as_.Sets_of_.Mixins%29)                                                         |
|         [13.8.2 Inherit and Super in Traits](classes.html#%28part._.Inherit_and_.Super_in_.Traits%29)                                                   |
|         [13.8.3 The trait Form](classes.html#%28part._.The_trait_.Form%29)                                                                              |
|       [13.9 Class Contracts](classes.html#%28part._.Class_.Contracts%29)                                                                                |
|         [13.9.1 External Class Contracts](classes.html#%28part._.External_.Class_.Contracts%29)                                                         |
|         [13.9.2 Internal Class Contracts](classes.html#%28part._.Internal_.Class_.Contracts%29)                                                         |
|                                                                                |
|     [14 Units (Components)](units.html)                                                                                |
|       [14.1 Signatures and Units](Signatures_and_Units.html)                                                                                |
|       [14.2 Invoking Units](Invoking_Units.html)                                                                                |
|       [14.3 Linking Units](Linking_Units.html)                                                                                |
|       [14.4 First-Class Units](firstclassunits.html)                                                                                |
|       [14.5 Whole-module Signatures and Units](Whole-module_Signatures_and_Units.html)                                                                  |
|       [14.6 Contracts for Units](Contracts_for_Units.html)                                                                                |
|         [14.6.1 Adding Contracts to Signatures](Contracts_for_Units.html#%28part._.Adding_.Contracts_to_.Signatures%29)                                 |
|         [14.6.2 Adding Contracts to Units](Contracts_for_Units.html#%28part._.Adding_.Contracts_to_.Units%29)                                           |
|       [14.7 unit versus module](unit_versus_module.html)                                                                                |
|                                                                                |
|     [15 Reflection and Dynamic Evaluation](reflection.html)                                                                                |
|       [15.1 eval](eval.html)                                                                                |
|         [15.1.1 Local Scopes](eval.html#%28part._.Local_.Scopes%29)                                                                                |
|         [15.1.2 Namespaces](eval.html#%28part._namespaces%29)                                                                                |
|         [15.1.3 Namespaces and Modules](eval.html#%28part._.Namespaces_and_.Modules%29)                                                                 |
|       [15.2 Manipulating Namespaces](mk-namespace.html)                                                                                |
|         [15.2.1 Creating and Installing Namespaces](mk-namespace.html#%28part._.Creating_and_.Installing_.Namespaces%29)                                |
|         [15.2.2 Sharing Data and Code Across Namespaces](mk-namespace.html#%28part._.Sharing_.Data_and_.Code_.Across_.Namespaces%29)                    |
|       [15.3 Scripting Evaluation and Using load](load.html)                                                                                |
|       [15.4 Code Inspectors for Trusted and Untrusted Code](code-inspectors_protect.html)                                                               |
|                                                                                |
|     [16 Macros](macros.html)                                                                                |
|       [16.1 Pattern-Based Macros](pattern-macros.html)                                                                                |
|         [16.1.1 define-syntax-rule](pattern-macros.html#%28part._define-syntax-rule%29)                                                                 |
|         [16.1.2 Lexical Scope](pattern-macros.html#%28part._.Lexical_.Scope%29)                                                                         |
|         [16.1.3 define-syntax and syntax-rules](pattern-macros.html#%28part._define-syntax_and_syntax-rules%29)                                         |
|         [16.1.4 Matching Sequences](pattern-macros.html#%28part._.Matching_.Sequences%29)                                                               |
|         [16.1.5 Identifier Macros](pattern-macros.html#%28part._.Identifier_.Macros%29)                                                                 |
|         [16.1.6 set! Transformers](pattern-macros.html#%28part._set__.Transformers%29)                                                                  |
|         [16.1.7 Macro-Generating Macros](pattern-macros.html#%28part._.Macro-.Generating_.Macros%29)                                                    |
|         [16.1.8 Extended Example: Call-by-Reference Functions](pattern-macros.html#%28part._pattern-macro-example%29)                                   |
|       [16.2 General Macro Transformers](proc-macros.html)                                                                                |
|         [16.2.1 Syntax Objects](stx-obj.html)                                                                                |
|         [16.2.2 Macro Transformer Procedures](macro-transformers.html)                                                                                |
|         [16.2.3 Mixing Patterns and Expressions: syntax-case](syntax-case.html)                                                                         |
|         [16.2.4 with-syntax and generate-temporaries](with-syntax.html)                                                                                |
|         [16.2.5 Compile and Run-Time Phases](stx-phases.html)                                                                                |
|         [16.2.6 General Phase Levels](phases.html)                                                                                |
|           [16.2.6.1 Phases and Bindings](phases.html#%28part._.Phases_and_.Bindings%29)                                                                 |
|           [16.2.6.2 Phases and Modules](phases.html#%28part._.Phases_and_.Modules%29)                                                                   |
|         [16.2.7 Tainted Syntax](stx-certs.html)                                                                                |
|       [16.3 Module Instantiations and Visits](macro-module.html)                                                                                |
|         [16.3.1 Declaration versus Instantiation](macro-module.html#%28part._.Declaration_versus_.Instantiation%29)                                     |
|         [16.3.2 Compile-Time Instantiation](macro-module.html#%28part._compile-time-instantiation%29)                                                   |
|         [16.3.3 Visiting Modules](macro-module.html#%28part._.Visiting_.Modules%29)                                                                     |
|         [16.3.4 Lazy Visits via Available Modules](macro-module.html#%28part._stx-available-module%29)                                                  |
|                                                                                |
|     [17 Creating Languages](languages.html)                                                                                |
|       [17.1 Module Languages](module-languages.html)                                                                                |
|         [17.1.1 Implicit Form Bindings](module-languages.html#%28part._implicit-forms%29)                                                               |
|         [17.1.2 Using #lang s-exp](module-languages.html#%28part._s-exp%29)                                                                             |
|       [17.2 Reader Extensions](hash-reader.html)                                                                                |
|         [17.2.1 Source Locations](hash-reader.html#%28part._.Source_.Locations%29)                                                                      |
|         [17.2.2 Readtables](hash-reader.html#%28part._readtable%29)                                                                                |
|       [17.3 Defining new #lang Languages](hash-languages.html)                                                                                |
|         [17.3.1 Designating a #lang Language](hash-lang_syntax.html)                                                                                |
|         [17.3.2 Using #lang reader](hash-lang_reader.html)                                                                                |
|         [17.3.3 Using #lang s-exp syntax/module-reader](syntax_module-reader.html)                                                                      |
|         [17.3.4 Installing a Language](language-collection.html)                                                                                |
|         [17.3.5 Source-Handling Configuration](language-get-info.html)                                                                                |
|         [17.3.6 Module-Handling Configuration](module-runtime-config.html)                                                                              |
|                                                                                |
|     [18 Concurrency and Synchronization](concurrency.html)                                                                                |
|       [18.1 Threads](concurrency.html#%28part._.Threads%29)                                                                                |
|       [18.2 Thread Mailboxes](concurrency.html#%28part._.Thread_.Mailboxes%29)                                                                          |
|       [18.3 Semaphores](concurrency.html#%28part._.Semaphores%29)                                                                                |
|       [18.4 Channels](concurrency.html#%28part._.Channels%29)                                                                                |
|       [18.5 Buffered Asynchronous Channels](concurrency.html#%28part._.Buffered_.Asynchronous_.Channels%29)                                             |
|       [18.6 Synchronizable Events and sync](concurrency.html#%28part._.Synchronizable_.Events_and_sync%29)                                              |
|       [18.7 Building Your Own Synchronization Patterns](concurrency.html#%28part._.Building_.Your_.Own_.Synchronization_.Patterns%29)                   |
|                                                                                |
|     [19 Performance](performance.html)                                                                                |
|       [19.1 Performance in DrRacket](performance.html#%28part._.Dr.Racket-perf%29)                                                                      |
|       [19.2 Racket Virtual Machine Implementations](performance.html#%28part._virtual-machines%29)                                                      |
|       [19.3 Bytecode, Machine Code, and Just-in-Time (JIT) Compilers](performance.html#%28part._.J.I.T%29)                                              |
|       [19.4 Modules and Performance](performance.html#%28part._modules-performance%29)                                                                  |
|       [19.5 Function-Call Optimizations](performance.html#%28part._func-call-performance%29)                                                            |
|       [19.6 Mutation and Performance](performance.html#%28part._.Mutation_and_.Performance%29)                                                          |
|       [19.7 letrec Performance](performance.html#%28part._letrec-performance%29)                                                                        |
|       [19.8 Fixnum and Flonum Optimizations](performance.html#%28part._fixnums%2Bflonums%29)                                                            |
|       [19.9 Unchecked, Unsafe Operations](performance.html#%28part._unchecked-unsafe%29)                                                                |
|       [19.10 Foreign Pointers](performance.html#%28part._ffi-pointer-access%29)                                                                         |
|       [19.11 Regular Expression Performance](performance.html#%28part._regexp-perf%29)                                                                  |
|       [19.12 Memory Management](performance.html#%28part._gc-perf%29)                                                                                |
|       [19.13 Reachability and Garbage Collection](performance.html#%28part._.Reachability._and._.Garbage._.Collection%29)                               |
|       [19.14 Weak Boxes and Testing](performance.html#%28part._.Weak_.Boxes_and_.Testing%29)                                                            |
|       [19.15 Reducing Garbage Collection Pauses](performance.html#%28part._.Reducing_.Garbage_.Collection_.Pauses%29)                                   |
|                                                                                |
|     [20 Parallelism](parallelism.html)                                                                                |
|       [20.1 Parallel Threads](parallelism.html#%28part._parallel-threads%29)                                                                            |
|       [20.2 Parallelism with Futures](parallelism.html#%28part._effective-futures%29)                                                                   |
|       [20.3 Parallelism with Places](parallelism.html#%28part._effective-places%29)                                                                     |
|       [20.4 Distributed Places](parallelism.html#%28part._distributed-places%29)                                                                        |
|                                                                                |
|     [21 Running and Creating Executables](running.html)                                                                                |
|       [21.1 Running racket and gracket](racket.html)                                                                                |
|         [21.1.1 Interactive Mode](racket.html#%28part._start-interactive-mode%29)                                                                       |
|         [21.1.2 Module Mode](racket.html#%28part._start-module-mode%29)                                                                                |
|         [21.1.3 Load Mode](racket.html#%28part._start-load-mode%29)                                                                                |
|       [21.2 Scripts](scripts.html)                                                                                |
|         [21.2.1 Unix Scripts](scripts.html#%28part._.Unix_.Scripts%29)                                                                                |
|         [21.2.2 Windows Batch Files](scripts.html#%28part._.Windows_.Batch_.Files%29)                                                                   |
|       [21.3 Creating Stand-Alone Executables](exe.html)                                                                                |
|                                                                                |
|     [22 More Libraries](More_Libraries.html)                                                                                |
|       [22.1 Graphics and GUIs](More_Libraries.html#%28part._graphics%29)                                                                                |
|       [22.2 The Web Server](More_Libraries.html#%28part._.The_.Web_.Server%29)                                                                          |
|       [22.3 Using Foreign Libraries](More_Libraries.html#%28part._.Using_.Foreign_.Libraries%29)                                                        |
|       [22.4 And More](More_Libraries.html#%28part._.And_.More%29)                                                                                |
|                                                                                |
|     [23 Dialects of Racket and Scheme](dialects.html)                                                                                |
|       [23.1 More Rackets](more-hash-lang.html)                                                                                |
|       [23.2 Standards](standards.html)                                                                                |
|         [23.2.1 R5RS](standards.html#%28part._r5rs%29)                                                                                |
|         [23.2.2 R6RS](standards.html#%28part._.R6.R.S%29)                                                                                |
|       [23.3 Teaching](teaching-langs.html)                                                                                |
|                                                                                |
|     [24 Command-Line Tools and Your Editor of Choice](other-editors.html)                                                                            |
|       [24.1 Command-Line Tools](cmdline-tools.html)                                                                                |
|         [24.1.1 Compilation and Configuration: raco](cmdline-tools.html#%28part._compile%29)                                                            |
|         [24.1.2 Interactive evaluation](cmdline-tools.html#%28part._.Interactive_evaluation%29)                                                         |
|         [24.1.3 Shell completion](cmdline-tools.html#%28part._.Shell_completion%29)                                                                     |
|       [24.2 Emacs](Emacs.html)                                                                                |
|         [24.2.1 Major Modes](Emacs.html#%28part._.Major_.Modes%29)                                                                                |
|         [24.2.2 Minor Modes](Emacs.html#%28part._.Minor_.Modes%29)                                                                                |
|         [24.2.3 Packages specific to Evil Mode](Emacs.html#%28part._.Packages_specific_to_.Evil_.Mode%29)                                               |
|       [24.3 Vim](Vim.html)                                                                                |
|         [24.3.1 Enhanced Racket Support](Vim.html#%28part._vim-racket%29)                                                                               |
|         [24.3.2 Indentation](Vim.html#%28part._.Indentation%29)                                                                                |
|         [24.3.3 Highlighting](Vim.html#%28part._.Highlighting%29)                                                                                |
|         [24.3.4 Structured Editing](Vim.html#%28part._.Structured_.Editing%29)                                                                          |
|         [24.3.5 REPLs](Vim.html#%28part._.R.E.P.Ls%29)                                                                                |
|         [24.3.6 Scribble](Vim.html#%28part._.Scribble%29)                                                                                |
|         [24.3.7 Miscellaneous](Vim.html#%28part._.Miscellaneous%29)                                                                                |
|         [24.3.8 Older Versions of Vim](Vim.html#%28part._vim-versions%29)                                                                               |
|       [24.4 Sublime Text](Sublime_Text.html)                                                                                |
|       [24.5 Visual Studio Code](Visual_Studio_Code.html)                                                                                |
|                                                                                |
|     [Bibliography](doc-bibliography.html)                                                                                |
|                                                                                |
|     [Index](doc-index.html)                                                                                |


# The Racket Guide
------------------------------------------------------------------------


# 1 Welcome to Racket


## 1 Welcome to Racket
Depending on how you look at it, Racket is
-   a programming language—a dialect of Lisp and a descendant of Scheme;
    > > > See [Dialects of Racket and Scheme](dialects.html) for more information on other dialects of Lisp and how they relate to Racket.
-   a family of programming languages—variants of Racket, and more; or
-   a set of tools—for using a family of programming languages.
Where there is no room for confusion, we use simply Racket.
Racket’s main tools are
-   racket, the core compiler, interpreter, and run-time system;
-   DrRacket, the programming environment; and
-   raco, a command-line tool for executing Racket commands that install packages, build libraries, and more.
Most likely, you’ll want to explore the Racket language using DrRacket, especially at the beginning. If you prefer, you can also work with the command-line racket interpreter (see [Running racket and gracket](racket.html)) and your favorite text editor (see [Command-Line Tools and Your Editor of Choice](other-editors.html)). The rest of this guide presents the language mostly independent of your choice of editor.
If you’re using DrRacket, you’ll need to choose the proper language, because DrRacket accommodates many different variants of Racket, as well as other languages. Assuming that you’ve never used DrRacket before, start it up, type the line
> [#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)


# 1 Welcome to Racket
in DrRacket’s top text area, and then click the Run button that’s above the text area. DrRacket then understands that you mean to work in the normal variant of Racket (as opposed to the smaller [racket/base](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13) or many other possibilities).
> > > [More Rackets](more-hash-lang.html) describes some of the other possibilities.
If you’ve used DrRacket before with something other than a program that starts [#lang](Module_Syntax.html#%28part._hash-lang%29), DrRacket will remember the last language that you used, instead of inferring the language from the [#lang](Module_Syntax.html#%28part._hash-lang%29) line. In that case, use the Language\|Choose Language... menu item. In the dialog that appears, select the first item, which tells DrRacket to use the language that is declared in a source program via [#lang](Module_Syntax.html#%28part._hash-lang%29). Put the [#lang](Module_Syntax.html#%28part._hash-lang%29) line above in the top text area, still.


### 1.1 Interacting with Racket
DrRacket’s bottom text area and the racket command-line program (when started with no options) both act as a kind of calculator. You type a Racket expression, hit the Return key, and the answer is printed. In the terminology of Racket, this kind of calculator is called a read-eval-print loop or REPL.
A number by itself is an expression, and the answer is just the number:
> ```racket
> > 5
> 5
> ```
A string is also an expression that evaluates to itself. A string is written with double quotes at the start and end of the string:
> ```racket
> > "Hello, world!"
> "Hello, world!"
> ```
Racket uses parentheses to wrap larger expressions—almost any kind of expression, other than simple constants. For example, a function call is written: open parenthesis, function name, argument expression, and closing parenthesis. The following expression calls the built-in function [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) with the arguments "the boy out of the country", 4, and 7:
> ```racket
> > ( substring "the boy out of the country" 4 7 )
> "boy"
> ```


### 1.2 Definitions and Interactions
You can define your own functions that work like [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) by using 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, like this:
>
<colgroup><col style="width: 100%" /></colgroup>

( define ( extract str )
> ( substring str 4 7 ) )

> ( extract "the boy out of the country" )
> "boy"
> > ( extract "the country out of the boy" )
> "cou"

Although you can evaluate 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 in the [REPL](#%28tech._repl%29), definitions are normally a part of a program that you want to keep and use later. So, in DrRacket, you’d normally put the definition in the top text area—called the definitions area—along with the [#lang](Module_Syntax.html#%28part._hash-lang%29) prefix:


### 1.2 Definitions and Interactions
> ```racket
> #lang racket
> ( define ( extract str )
> ( substring str 4 7 ) )
> ```
If calling (extract "the boy") is part of the main action of your program, that would go in the [definitions area](#%28tech._definitions._area%29), too. But if it was just an example expression that you were using to explore extract, then you’d more likely leave the [definitions area](#%28tech._definitions._area%29) as above, click Run, and then evaluate (extract "the boy") in the [REPL](#%28tech._repl%29).
When using command-line racket instead of DrRacket, you’d save the above text in a file using your favorite editor. If you save it as "extract.rkt", then after starting racket in the same directory, you’d evaluate the following sequence:
> > > If you use [xrepl](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=xrepl&rel=index.html&version=8.18.0.13), you can use [,enter extract.rkt](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=xrepl&rel=index.html%23%2528xrepl._enter%2529&version=8.18.0.13).
> ```racket
> > ( enter! "extract.rkt" )
> > ( extract "the gal out of the city" )
> "gal"
> ```
The [enter!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=interactive.html%23%2528form._%2528%2528lib._racket%252Fenter..rkt%2529._enter%2521%2529%2529&version=8.18.0.13) form both loads the code and switches the evaluation context to the inside of the module, just like DrRacket’s Run button.


### 1.3 Creating Executables
If your file (or [definitions area](#%28tech._definitions._area%29) in DrRacket) contains
> ```racket
> #lang racket
> ( define ( extract str )
> ( substring str 4 7 ) )
> ( extract "the cat out of the bag" )
> ```
then it is a complete program that prints “cat” when run. You can run the program within DrRacket or using [enter!](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=interactive.html%23%2528form._%2528%2528lib._racket%252Fenter..rkt%2529._enter%2521%2529%2529&version=8.18.0.13) in racket, but if the program is saved in ‹src-filename›, you can also run it from a command line with
  racket ‹src-filename›
To package the program as an executable, you have a few options:
-   In DrRacket, you can select the Racket\|Create Executable... menu item.
-   From a command-line prompt, run raco exe ‹src-filename›, where ‹src-filename› contains the program. See [raco exe: Creating Stand-Alone Executables](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=raco&rel=exe.html&version=8.18.0.13) for more information.
-   With Unix or Mac OS, you can turn the program file into an executable script by inserting the line
    > > > See [Scripts](scripts.html) for more information on script files.
    |                          |
    |--------------------------|
    |   #! /usr/bin/env racket |
    at the very beginning of the file. Also, change the file permissions to executable using chmod +x ‹filename› on the command line.


### 1.3 Creating Executables
    The script works as long as racket is in the user’s executable search path. Alternately, use a full path to racket after #! (with a space between #! and the path), in which case the user’s executable search path does not matter.


### 1.4 A Note to Readers with Lisp/Scheme Experience
If you already know something about Scheme or Lisp, you might be tempted to put just
> ```racket
> ( define ( extract str )
> ( substring str 4 7 ) )
> ```
into "extract.rktl" and run racket with
> ```racket
> > ( load "extract.rktl" )
> > ( extract "the dog out" )
> "dog"
> ```
That will work, because racket is willing to imitate a traditional Lisp environment, but we strongly recommend against using [load](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=eval.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._load%2529%2529&version=8.18.0.13) or writing programs outside of a module.
Writing definitions outside of a module leads to bad error messages, bad performance, and awkward scripting to combine and run programs. The problems are not specific to racket; they’re fundamental limitations of the traditional top-level environment, which Scheme and Lisp implementations have historically fought with ad hoc command-line flags, compiler directives, and build tools. The module system is designed to avoid these problems, so start with [#lang](Module_Syntax.html#%28part._hash-lang%29), and you’ll be happier with Racket in the long run.
------------------------------------------------------------------------


# 2 Racket Essentials


## 2 Racket Essentials
This chapter provides a quick introduction to Racket as background for the rest of the guide. Readers with some Racket experience can safely skip to [Built-In Datatypes](datatypes.html).
|                                                                                |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
|     [2.1 Simple Values](Simple_Values.html)                                                                                |
|     [2.2 Simple Definitions and Expressions](syntax-overview.html)                                                                  |
|       [2.2.1 Definitions](syntax-overview.html#%28part._.Definitions%29)                                                            |
|       [2.2.2 An Aside on Indenting Code](syntax-overview.html#%28part._indentation%29)                                              |
|       [2.2.3 Identifiers](syntax-overview.html#%28part._.Identifiers%29)                                                            |
|       [2.2.4 Function Calls (Procedure Applications)](syntax-overview.html#%28part._.Function_.Calls__.Procedure_.Applications_%29) |
|       [2.2.5 Conditionals with if, and, or, and cond](syntax-overview.html#%28part._.Conditionals_with_if__and__or__and_cond%29)    |
|       [2.2.6 Function Calls, Again](syntax-overview.html#%28part._.Function_.Calls__.Again%29)                                      |
|       [2.2.7 Anonymous Functions with lambda](syntax-overview.html#%28part._.Anonymous_.Functions_with_lambda%29)                   |
|       [2.2.8 Local Binding with define, let, and let*](syntax-overview.html#%28part._local-binding-intro%29)                        |
|     [2.3 Lists, Iteration, and Recursion](Lists__Iteration__and_Recursion.html)                                                     |
|       [2.3.1 Predefined List Loops](Lists__Iteration__and_Recursion.html#%28part._.Predefined_.List_.Loops%29)                      |
|       [2.3.2 List Iteration from Scratch](Lists__Iteration__and_Recursion.html#%28part._.List_.Iteration_from_.Scratch%29)          |
|       [2.3.3 Tail Recursion](Lists__Iteration__and_Recursion.html#%28part._tail-recursion%29)                                       |
|       [2.3.4 Recursion versus Iteration](Lists__Iteration__and_Recursion.html#%28part._.Recursion_versus_.Iteration%29)             |
|     [2.4 Pairs, Lists, and Racket Syntax](Pairs__Lists__and_Racket_Syntax.html)                                                     |
|       [2.4.1 Quoting Pairs and Symbols with quote](Pairs__Lists__and_Racket_Syntax.html#%28part._quoting-lists%29)                  |
|       [2.4.2 Abbreviating quote with '](Pairs__Lists__and_Racket_Syntax.html#%28part._.Abbreviating_quote_with__%29)                |
|       [2.4.3 Lists and Racket Syntax](Pairs__Lists__and_Racket_Syntax.html#%28part._lists-and-syntax%29)                            |


# 2 Racket Essentials
------------------------------------------------------------------------


# 2.1 Simple Values


### 2.1 Simple Values
Racket values include numbers, booleans, strings, and byte strings. In DrRacket and documentation examples (when you read the documentation in color), value expressions are shown in green.
Numbers are written in the usual way, including fractions and imaginary numbers:
> > > [Numbers](numbers.html) (later in this guide) explains more about numbers.
> ```racket
> 1 3.14
> 1/2 6.02e+23
> 1+2i 9999999999999999999999
> ```
Booleans are #t for true and #f for false. In conditionals, however, all non-#f values are treated as true.
> > > [Booleans](booleans.html) (later in this guide) explains more about booleans.
Strings are written between doublequotes. Within a string, backslash is an escaping character; for example, a backslash followed by a doublequote includes a literal doublequote in the string. Except for an unescaped doublequote or backslash, any Unicode character can appear in a string constant.
> > > [Strings (Unicode)](strings.html) (later in this guide) explains more about strings.
> ```racket
> "Hello, world!"
> "Benjamin \"Bugsy\" Siegel"
> "λx:(μα.α→α).xx"
> ```


# 2.1 Simple Values
When a constant is evaluated in the [REPL](intro.html#%28tech._repl%29), it typically prints the same as its input syntax. In some cases, the printed form is a normalized version of the input syntax. In documentation and in DrRacket’s [REPL](intro.html#%28tech._repl%29), results are printed in blue instead of green to highlight the difference between an input expression and a printed result.
Examples:
> ```racket
> > 1.0000
> 1.0
> > "Bugs \u0022Figaro\u0022 Bunny"
> "Bugs \"Figaro\" Bunny"
> ```
------------------------------------------------------------------------


# 2.2 Simple Definitions and Expressions


### 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](intro.html#%28tech._repl%29) also evaluates ‹topform›s.
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.
> > > <img src="magnify.png" width="24" height="24" alt="+" />[Reading Comments](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=reader.html%23%2528part._parse-comment%2529&version=8.18.0.13) in [The Racket Reference](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.18.0.13) provides more on 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
> > > <img src="finger.png" width="24" height="24" alt="+" />[Definitions: define](define.html) (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 ‹id›s. In the function case, the ‹expr›s are the body of the function. When the function is called, it returns the result of the last ‹expr›.
Examples:
> ```racket
> ( 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:
> ```racket
> > 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:
> ```racket
> ( define ( bake flavor ) ( printf "preheating oven...\n" ) ( string-append flavor " pie" ) )
> > ( bake "apple" )
> preheating oven...
> "apple pie"
> ```


# 2.2 Simple Definitions and Expressions
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:
>
<colgroup><col style="width: 100%" /></colgroup>

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

> ( nobake "green" )
> "jello"

Within nobake, there are no parentheses around [string-append](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._string-append%2529%2529&version=8.18.0.13) flavor "jello", so they are three separate expressions instead of one function-call expression. The expressions [string-append](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._string-append%2529%2529&version=8.18.0.13) 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](intro.html#%28tech._repl%29) 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](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) keyword:


# 2.2 Simple Definitions and Expressions
> ```racket
> ( 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
> > > <img src="finger.png" width="24" height="24" alt="+" />[Identifiers and Binding](binding.html) (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:
> ```racket
> +
> 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
> > > <img src="finger.png" width="24" height="24" alt="+" />[Function Calls](application.html) (later in this guide) explains more about function calls.
> ( ‹id› ‹expr›\* )
where the number of ‹expr›s determines the number of arguments supplied to the function named by ‹id›.
The [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) language pre-defines many function identifiers, such as [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) and [string-append](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._string-append%2529%2529&version=8.18.0.13). 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.


# 2.2 Simple Definitions and Expressions
> ```racket
> > ( 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 -1 6 )
> 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](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), [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), and [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)
The next simplest kind of expression is an [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) conditional:
> ( if ‹expr› ‹expr› ‹expr› )
> > > <img src="finger.png" width="24" height="24" alt="+" />[Conditionals](conditionals.html) (later in this guide) explains more about conditionals.


# 2.2 Simple Definitions and Expressions
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](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) expression, otherwise the third ‹expr› is evaluated for the result.
Example:
> ```racket
> > ( if ( > 2 3 ) "2 is bigger than 3" "2 is smaller than 3" )
> "2 is smaller than 3"
> ```
>
<colgroup><col style="width: 100%" /></colgroup>

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

> ( reply "hello racket" )
> "hi!"
> > ( reply "λx:(μα.α→α).xx" )
> "huh?"


# 2.2 Simple Definitions and Expressions
Complex conditionals can be formed by nesting [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) expressions. For example, in the previous reply example, the input must be a string because [string-prefix?](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528lib._racket%252Fstring..rkt%2529._string-prefix%7E3f%2529%2529&version=8.18.0.13) would error when given non-strings. You can remove this restriction by adding another [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) to check first if the input is a string:
> ```racket
> ( 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
> ```racket
> ( define ( reply-non-string s )
> ( if ( if ( string? s )
> ( string-prefix? s "hello " )
> #f )
> "hi!"
> "huh?" ) )
> ```


# 2.2 Simple Definitions and Expressions
but these kinds of nested [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)s are difficult to read. Racket provides more readable shortcuts through the [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) and [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) forms:
> > > <img src="finger.png" width="24" height="24" alt="+" />[Combining Tests: and and or](conditionals.html#%28part._and%2Bor%29) (later in this guide) explains more about [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) and [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).
> ```racket
> ( and ‹ expr › * )
> ( or ‹ expr › * )
> ```


# 2.2 Simple Definitions and Expressions
The [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) form short-circuits: it stops and returns #f when an expression produces #f, otherwise it keeps going. The [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) form similarly short-circuits when it encounters a true result.
Examples:
> ```racket
> ( 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](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) and [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) forms work with any number of expressions.
Examples:


# 2.2 Simple Definitions and Expressions
> ```racket
> ( 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 [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)s involves a sequence of tests, each with its own result:
> ```racket
> ( 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](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=8.18.0.13) form:
> > > <img src="finger.png" width="24" height="24" alt="+" />[Chaining Tests: cond](conditionals.html#%28part._cond%29) (later in this guide) explains more about [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).
> ( cond {\[ ‹expr› ‹expr›\* \]}\* )


# 2.2 Simple Definitions and Expressions
A [cond](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=8.18.0.13) 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 ‹expr›s are evaluated, and the last one in the clause provides the answer for the entire [cond](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._cond%2529%2529&version=8.18.0.13) expression; the rest of the clauses are ignored. If the test ‹expr› produces #f, then the clause’s remaining ‹expr›s are ignored, and evaluation continues with the next clause. The last clause can use [else](https://plt.cs.northwestern.edu/new-snapshots/new-snapshots/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._else%2529%2529&version=8.18.0.13) as a synonym for a #t test expression.
Using [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), the reply-more function can be more clearly written as follows:
> ```
(define (reply-more s)