145 lines
No EOL
151 KiB
HTML
145 lines
No EOL
151 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=0.8"/><title>2.2 Simple Definitions and Expressions</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="icons.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="../local-redirect/local-redirect.js"></script><script type="text/javascript" src="../local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_0");">►</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">The Racket Guide</a></td></tr></table></div><div class="tocviewsublisttop" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1 </td><td><a href="intro.html" class="tocviewlink" data-pltdoc="x">Welcome to Racket</a></td></tr><tr><td align="right">2 </td><td><a href="to-scheme.html" class="tocviewselflink" data-pltdoc="x">Racket Essentials</a></td></tr><tr><td align="right">3 </td><td><a href="datatypes.html" class="tocviewlink" data-pltdoc="x">Built-<wbr></wbr>In Datatypes</a></td></tr><tr><td align="right">4 </td><td><a href="scheme-forms.html" class="tocviewlink" data-pltdoc="x">Expressions and Definitions</a></td></tr><tr><td align="right">5 </td><td><a href="define-struct.html" class="tocviewlink" data-pltdoc="x">Programmer-<wbr></wbr>Defined Datatypes</a></td></tr><tr><td align="right">6 </td><td><a href="modules.html" class="tocviewlink" data-pltdoc="x">Modules</a></td></tr><tr><td align="right">7 </td><td><a href="contracts.html" class="tocviewlink" data-pltdoc="x">Contracts</a></td></tr><tr><td align="right">8 </td><td><a href="i_o.html" class="tocviewlink" data-pltdoc="x">Input and Output</a></td></tr><tr><td align="right">9 </td><td><a href="regexp.html" class="tocviewlink" data-pltdoc="x">Regular Expressions</a></td></tr><tr><td align="right">10 </td><td><a href="control.html" class="tocviewlink" data-pltdoc="x">Exceptions and Control</a></td></tr><tr><td align="right">11 </td><td><a href="for.html" class="tocviewlink" data-pltdoc="x">Iterations and Comprehensions</a></td></tr><tr><td align="right">12 </td><td><a href="match.html" class="tocviewlink" data-pltdoc="x">Pattern Matching</a></td></tr><tr><td align="right">13 </td><td><a href="classes.html" class="tocviewlink" data-pltdoc="x">Classes and Objects</a></td></tr><tr><td align="right">14 </td><td><a href="units.html" class="tocviewlink" data-pltdoc="x">Units</a></td></tr><tr><td align="right">15 </td><td><a href="reflection.html" class="tocviewlink" data-pltdoc="x">Reflection and Dynamic Evaluation</a></td></tr><tr><td align="right">16 </td><td><a href="macros.html" class="tocviewlink" data-pltdoc="x">Macros</a></td></tr><tr><td align="right">17 </td><td><a href="languages.html" class="tocviewlink" data-pltdoc="x">Creating Languages</a></td></tr><tr><td align="right">18 </td><td><a href="concurrency.html" class="tocviewlink" data-pltdoc="x">Concurrency and Synchronization</a></td></tr><tr><td align="right">19 </td><td><a href="performance.html" class="tocviewlink" data-pltdoc="x">Performance</a></td></tr><tr><td align="right">20 </td><td><a href="parallelism.html" class="tocviewlink" data-pltdoc="x">Parallelism</a></td></tr><tr><td align="right">21 </td><td><a href="running.html" class="tocviewlink" data-pltdoc="x">Running and Creating Executables</a></td></tr><tr><td align="right">22 </td><td><a href="More_Libraries.html" class="tocviewlink" data-pltdoc="x">More Libraries</a></td></tr><tr><td align="right">23 </td><td><a href="dialects.html" class="tocviewlink" data-pltdoc="x">Dialects of Racket and Scheme</a></td></tr><tr><td align="right">24 </td><td><a href="other-editors.html" class="tocviewlink" data-pltdoc="x">Command-<wbr></wbr>Line Tools and Your Editor of Choice</a></td></tr><tr><td align="right"></td><td><a href="doc-bibliography.html" class="tocviewlink" data-pltdoc="x">Bibliography</a></td></tr><tr><td align="right"></td><td><a href="doc-index.html" class="tocviewlink" data-pltdoc="x">Index</a></td></tr></table></div></div><div class="tocviewlist"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_1");">▼</a></td><td>2 </td><td><a href="to-scheme.html" class="tocviewlink" data-pltdoc="x">Racket Essentials</a></td></tr></table><div class="tocviewsublist" style="display: block;" id="tocview_1"><table cellspacing="0" cellpadding="0"><tr><td align="right">2.1 </td><td><a href="Simple_Values.html" class="tocviewlink" data-pltdoc="x">Simple Values</a></td></tr><tr><td align="right">2.2 </td><td><a href="syntax-overview.html" class="tocviewselflink" data-pltdoc="x">Simple Definitions and Expressions</a></td></tr><tr><td align="right">2.3 </td><td><a href="Lists__Iteration__and_Recursion.html" class="tocviewlink" data-pltdoc="x">Lists, Iteration, and Recursion</a></td></tr><tr><td align="right">2.4 </td><td><a href="Pairs__Lists__and_Racket_Syntax.html" class="tocviewlink" data-pltdoc="x">Pairs, Lists, and Racket Syntax</a></td></tr></table></div></div><div class="tocviewlist"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_2");">►</a></td><td>2.2 </td><td><a href="syntax-overview.html" class="tocviewselflink" data-pltdoc="x">Simple Definitions and Expressions</a></td></tr></table><div class="tocviewsublistbottom" style="display: none;" id="tocview_2"><table cellspacing="0" cellpadding="0"><tr><td align="right">2.2.1 </td><td><a href="syntax-overview.html#%28part._.Definitions%29" class="tocviewlink" data-pltdoc="x">Definitions</a></td></tr><tr><td align="right">2.2.2 </td><td><a href="syntax-overview.html#%28part._indentation%29" class="tocviewlink" data-pltdoc="x">An Aside on Indenting Code</a></td></tr><tr><td align="right">2.2.3 </td><td><a href="syntax-overview.html#%28part._.Identifiers%29" class="tocviewlink" data-pltdoc="x">Identifiers</a></td></tr><tr><td align="right">2.2.4 </td><td><a href="syntax-overview.html#%28part._.Function_.Calls__.Procedure_.Applications_%29" class="tocviewlink" data-pltdoc="x">Function Calls</a></td></tr><tr><td align="right">2.2.5 </td><td><a href="syntax-overview.html#%28part._.Conditionals_with_if__and__or__and_cond%29" class="tocviewlink" data-pltdoc="x">Conditionals with <span class="RktSym"><span class="RktStxLink">if</span></span>, <span class="RktSym"><span class="RktStxLink">and</span></span>, <span class="RktSym"><span class="RktStxLink">or</span></span>, and <span class="RktSym"><span class="RktStxLink">cond</span></span></a></td></tr><tr><td align="right">2.2.6 </td><td><a href="syntax-overview.html#%28part._.Function_.Calls__.Again%29" class="tocviewlink" data-pltdoc="x">Function Calls, Again</a></td></tr><tr><td align="right">2.2.7 </td><td><a href="syntax-overview.html#%28part._.Anonymous_.Functions_with_lambda%29" class="tocviewlink" data-pltdoc="x">Anonymous Functions with <span class="RktSym"><span class="RktStxLink">lambda</span></span></a></td></tr><tr><td align="right">2.2.8 </td><td><a href="syntax-overview.html#%28part._local-binding-intro%29" class="tocviewlink" data-pltdoc="x">Local Binding with
|
|
<span class="RktSym"><span class="RktStxLink">define</span></span>, <span class="RktSym"><span class="RktStxLink">let</span></span>, and <span class="RktSym"><span class="RktStxLink">let*</span></span></a></td></tr></table></div></div></div><div class="tocsub"><div class="tocsubtitle">On this page:</div><table class="tocsublist" cellspacing="0"><tr><td><span class="tocsublinknumber">2.2.1<tt> </tt></span><a href="syntax-overview.html#%28part._.Definitions%29" class="tocsubseclink" data-pltdoc="x">Definitions</a></td></tr><tr><td><span class="tocsublinknumber">2.2.2<tt> </tt></span><a href="syntax-overview.html#%28part._indentation%29" class="tocsubseclink" data-pltdoc="x">An Aside on Indenting Code</a></td></tr><tr><td><span class="tocsublinknumber">2.2.3<tt> </tt></span><a href="syntax-overview.html#%28part._.Identifiers%29" class="tocsubseclink" data-pltdoc="x">Identifiers</a></td></tr><tr><td><span class="tocsublinknumber">2.2.4<tt> </tt></span><a href="syntax-overview.html#%28part._.Function_.Calls__.Procedure_.Applications_%29" class="tocsubseclink" data-pltdoc="x">Function Calls</a></td></tr><tr><td><span class="tocsublinknumber">2.2.5<tt> </tt></span><a href="syntax-overview.html#%28part._.Conditionals_with_if__and__or__and_cond%29" class="tocsubseclink" data-pltdoc="x">Conditionals with <span class="RktSym"><span class="RktStxLink">if</span></span>, <span class="RktSym"><span class="RktStxLink">and</span></span>, <span class="RktSym"><span class="RktStxLink">or</span></span>, and <span class="RktSym"><span class="RktStxLink">cond</span></span></a></td></tr><tr><td><span class="tocsublinknumber">2.2.6<tt> </tt></span><a href="syntax-overview.html#%28part._.Function_.Calls__.Again%29" class="tocsubseclink" data-pltdoc="x">Function Calls, Again</a></td></tr><tr><td><span class="tocsublinknumber">2.2.7<tt> </tt></span><a href="syntax-overview.html#%28part._.Anonymous_.Functions_with_lambda%29" class="tocsubseclink" data-pltdoc="x">Anonymous Functions with <span class="RktSym"><span class="RktStxLink">lambda</span></span></a></td></tr><tr><td><span class="tocsublinknumber">2.2.8<tt> </tt></span><a href="syntax-overview.html#%28part._local-binding-intro%29" class="tocsubseclink" data-pltdoc="x">Local Binding with
|
|
<span class="RktSym"><span class="RktStxLink">define</span></span>, <span class="RktSym"><span class="RktStxLink">let</span></span>, and <span class="RktSym"><span class="RktStxLink">let*</span></span></a></td></tr></table></div></div><div class="maincolumn"><div class="main"><div class="navsettop"><span class="navleft"><form class="searchform"><input class="searchbox" id="searchbox" type="text" tabindex="1" placeholder="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, "8.6", "../");"/></form> <a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("8.6");">top</a><span class="tocsettoggle"> <a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright"> <a href="Simple_Values.html" title="backward to "2.1 Simple Values"" data-pltdoc="x">← prev</a> <a href="to-scheme.html" title="up to "2 Racket Essentials"" data-pltdoc="x">up</a> <a href="Lists__Iteration__and_Recursion.html" title="forward to "2.3 Lists, Iteration, and Recursion"" data-pltdoc="x">next →</a></span> </div><h4 x-source-module="(lib "scribblings/guide/guide.scrbl")" x-source-pkg="racket-doc" x-part-tag=""syntax-overview"">2.2<tt> </tt><a name="(part._syntax-overview)"></a>Simple Definitions and Expressions</h4><p>A program module is written as</p><blockquote class="SCodeFlow"><p><span class="RktInBG"><span class="hspace"></span><span class="RktIn">#lang</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">langname</span>›</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">topform</span>›</span><span class="sroman">*</span></p></blockquote><p>where a <span class="sroman">‹<span style="font-style: italic">topform</span>›</span> is either a <span class="sroman">‹<span style="font-style: italic">definition</span>›</span> or an
|
|
<span class="sroman">‹<span style="font-style: italic">expr</span>›</span>. The <a href="intro.html#%28tech._repl%29" class="techoutside" data-pltdoc="x"><span class="techinside">REPL</span></a> also evaluates <span class="sroman">‹<span style="font-style: italic">topform</span>›</span>s.</p><p>In syntax specifications, text with a gray background, such as
|
|
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">#lang</span><span class="hspace"></span></span>, represents literal text. Whitespace must appear
|
|
between such literals and nonterminals like <span class="sroman">‹<span style="font-style: italic">id</span>›</span>,
|
|
except that whitespace is not required before or after <span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span>,
|
|
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span>, <span class="RktInBG"><span class="hspace"></span><span class="RktIn">[</span><span class="hspace"></span></span>, or <span class="RktInBG"><span class="hspace"></span><span class="RktIn">]</span><span class="hspace"></span></span>. A <a name="(idx._(gentag._0._(lib._scribblings/guide/guide..scrbl)))"></a>comment, which starts
|
|
with <span class="RktInBG"><span class="hspace"></span><span class="RktIn">;</span><span class="hspace"></span></span> and runs until the end of the line, is treated the
|
|
same as whitespace.</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="magnify.png" alt="+" width="24" height="24"/></span><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=reader.html%23%2528part._parse-comment%2529&version=8.6" class="Sq" data-pltdoc="x">Reading Comments</a> in <a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.6" class="Sq" data-pltdoc="x">The Racket Reference</a> provides more on different forms of comments.</p></blockquote></blockquote></blockquote><p>Following the usual conventions, <span class="sroman">*</span> in a grammar means zero
|
|
or more repetitions of the preceding element, <span style="vertical-align: super; font-size: 80%">+</span> means one
|
|
or more repetitions of the preceding element, and <span class="sroman">{</span><span class="sroman">}</span> groups
|
|
a sequence as an element for repetition.</p><h5 x-source-module="(lib "scribblings/guide/guide.scrbl")" x-source-pkg="racket-doc" x-part-tag=""Definitions"">2.2.1<tt> </tt><a name="(part._.Definitions)"></a>Definitions</h5><p>A definition of the form</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="finger.png" alt="+" width="24" height="24"/></span><a href="define.html" data-pltdoc="x">Definitions: <span class="RktSym"><span class="RktStxLink">define</span></span></a> (later in this guide) explains more about definitions.</p></blockquote></blockquote></blockquote><blockquote class="SCodeFlow"><p><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">define</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">id</span>›</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span></p></blockquote><p>binds <span class="sroman">‹<span style="font-style: italic">id</span>›</span> to the result of <span class="sroman">‹<span style="font-style: italic">expr</span>›</span>, while</p><blockquote class="SCodeFlow"><p><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">define</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">id</span>›</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">id</span>›</span><span class="sroman">*</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span style="vertical-align: super; font-size: 80%">+</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span></p></blockquote><p>binds the first <span class="sroman">‹<span style="font-style: italic">id</span>›</span> to a function (also called a
|
|
<span style="font-style: italic">procedure</span>) that takes arguments as named by the remaining
|
|
<span class="sroman">‹<span style="font-style: italic">id</span>›</span>s. In the function case, the <span class="sroman">‹<span style="font-style: italic">expr</span>›</span>s are the body
|
|
of the function. When the function is called, it returns the result of
|
|
the last <span class="sroman">‹<span style="font-style: italic">expr</span>›</span>.</p><p><div class="SIntrapara">Examples:</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">pie</span><span class="hspace"> </span><span class="RktVal">3</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">defines </span><span class="RktSym">pie</span><span class="RktCmt"> to be </span><span class="RktVal">3</span></td></tr><tr><td> </td></tr></table></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">piece</span><span class="hspace"> </span><span class="RktSym">str</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">defines </span><span class="RktSym">piece</span><span class="RktCmt"> as a function</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._substring%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">substring</a></span><span class="hspace"> </span><span class="RktSym">str</span><span class="hspace"> </span><span class="RktVal">0</span><span class="hspace"> </span><span class="RktSym">pie</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt"> of one argument</span></td></tr><tr><td> </td></tr></table></td></tr><tr><td><span class="stt">> </span><span class="RktSym">pie</span></td></tr><tr><td><p><span class="RktRes">3</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">piece</span><span class="hspace"> </span><span class="RktVal">"key lime"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"key"</span></p></td></tr></table></blockquote></div></p><p>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.</p><p><div class="SIntrapara">Examples:</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktSym">piece</span></td></tr><tr><td><p><span class="RktRes">#<procedure:piece></span></p></td></tr><tr><td><span class="stt">> </span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._substring%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">substring</a></span></td></tr><tr><td><p><span class="RktRes">#<procedure:substring></span></p></td></tr></table></blockquote></div></p><p>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.</p><p><div class="SIntrapara">Examples:</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">bake</span><span class="hspace"> </span><span class="RktSym">flavor</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Writing.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._printf%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">printf</a></span><span class="hspace"> </span><span class="RktVal">"preheating oven...\n"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktSym">flavor</span><span class="hspace"> </span><span class="RktVal">" pie"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td> </td></tr></table></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">bake</span><span class="hspace"> </span><span class="RktVal">"apple"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktOut">preheating oven...</span></p></td></tr><tr><td><p><span class="RktRes">"apple pie"</span></p></td></tr></table></blockquote></div></p><p>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
|
|
<span class="RktSym">nobake</span> function fails to include its argument in its result:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">nobake</span><span class="hspace"> </span><span class="RktSym">flavor</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktSym">flavor</span><span class="hspace"> </span><span class="RktVal">"jello"</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p> </p></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">nobake</span><span class="hspace"> </span><span class="RktVal">"green"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"jello"</span></p></td></tr></table></td></tr></table></blockquote><p>Within <span class="RktSym">nobake</span>, there are no parentheses around
|
|
<span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="stt"> </span><span class="RktSym">flavor</span><span class="stt"> </span><span class="RktVal">"jello"</span>, so they are three separate
|
|
expressions instead of one function-call expression. The expressions
|
|
<span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span> and <span class="RktSym">flavor</span> are evaluated, but the
|
|
results are never used. Instead, the result of the function is just
|
|
the result of the final expression, <span class="RktVal">"jello"</span>.</p><h5 x-source-module="(lib "scribblings/guide/guide.scrbl")" x-source-pkg="racket-doc" x-part-tag=""indentation"">2.2.2<tt> </tt><a name="(part._indentation)"></a>An Aside on Indenting Code</h5><p>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.</p><p>DrRacket automatically indents according to the standard style when
|
|
you type Enter in a program or <a href="intro.html#%28tech._repl%29" class="techoutside" data-pltdoc="x"><span class="techinside">REPL</span></a> expression. For example, if you
|
|
hit Enter after typing <span class="RktInBG"><span class="hspace"></span><span class="RktIn">(define (greet name)</span><span class="hspace"></span></span>, 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.</p><p>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
|
|
<span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span> keyword:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">halfbake</span><span class="hspace"> </span><span class="RktSym">flavor</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktSym">flavor</span><span class="hspace"> </span><span class="RktVal">" creme brulee"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>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 <span class="stt">racket</span> and DrRacket use the
|
|
source’s indentation to suggest where a parenthesis might be missing.</p><h5 x-source-module="(lib "scribblings/guide/guide.scrbl")" x-source-pkg="racket-doc" x-part-tag=""Identifiers"">2.2.3<tt> </tt><a name="(part._.Identifiers)"></a>Identifiers</h5><p>Racket’s syntax for identifiers is especially liberal. Excluding the
|
|
special characters</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="finger.png" alt="+" width="24" height="24"/></span><a href="binding.html" data-pltdoc="x">Identifiers and Binding</a> (later in this guide) explains more about identifiers.</p></blockquote></blockquote></blockquote><p><span class="hspace"> </span> <span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span> <span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span> <span class="RktInBG"><span class="hspace"></span><span class="RktIn">[</span><span class="hspace"></span></span> <span class="RktInBG"><span class="hspace"></span><span class="RktIn">]</span><span class="hspace"></span></span>
|
|
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">{</span><span class="hspace"></span></span> <span class="RktInBG"><span class="hspace"></span><span class="RktIn">}</span><span class="hspace"></span></span>
|
|
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">"</span><span class="hspace"></span></span> <span class="RktInBG"><span class="hspace"></span><span class="RktIn">,</span><span class="hspace"></span></span> <span class="RktInBG"><span class="hspace"></span><span class="RktIn">'</span><span class="hspace"></span></span> <span class="RktInBG"><span class="hspace"></span><span class="RktIn">`</span><span class="hspace"></span></span>
|
|
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">;</span><span class="hspace"></span></span> <span class="RktInBG"><span class="hspace"></span><span class="RktIn">#</span><span class="hspace"></span></span> <span class="RktInBG"><span class="hspace"></span><span class="RktIn">|</span><span class="hspace"></span></span> <span class="RktInBG"><span class="hspace"></span><span class="RktIn">\</span><span class="hspace"></span></span></p><p>and except for the sequences of characters that make number constants,
|
|
almost any sequence of non-whitespace characters forms an
|
|
<span class="sroman">‹<span style="font-style: italic">id</span>›</span>. For example <span class="RktSym">substring</span> is an
|
|
identifier. Also, <span class="RktSym">string-append</span> and <span class="RktSym">a+b</span> are
|
|
identifiers, as opposed to arithmetic expressions. Here are several
|
|
more examples:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktSym">+</span></td></tr><tr><td><span class="RktSym">integer?</span></td></tr><tr><td><span class="RktSym">pass/fail</span></td></tr><tr><td><span class="RktSym">Hfuhruhurr&Uumellmahaye</span></td></tr><tr><td><span class="RktSym">john-jacob-jingleheimer-schmidt</span></td></tr><tr><td><span class="RktSym">a-b-c+1-2-3</span></td></tr></table></blockquote><h5 x-source-module="(lib "scribblings/guide/guide.scrbl")" x-source-pkg="racket-doc" x-part-tag=""Function_Calls__Procedure_Applications_"">2.2.4<tt> </tt><a name="(part._.Function_.Calls__.Procedure_.Applications_)"></a>Function Calls (Procedure Applications)</h5><p>We have already seen many function calls, which are called
|
|
<span style="font-style: italic">procedure applications</span> in more traditional
|
|
terminology. The syntax of a function call is</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="finger.png" alt="+" width="24" height="24"/></span><a href="application.html" data-pltdoc="x">Function Calls</a> (later in this guide) explains more about function calls.</p></blockquote></blockquote></blockquote><blockquote class="SCodeFlow"><p><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">id</span>›</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="sroman">*</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span></p></blockquote><p>where the number of <span class="sroman">‹<span style="font-style: italic">expr</span>›</span>s determines the number of
|
|
arguments supplied to the function named by <span class="sroman">‹<span style="font-style: italic">id</span>›</span>.</p><p>The <a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=index.html&version=8.6" class="RktModLink Sq" data-pltdoc="x"><span class="RktSym">racket</span></a> language pre-defines many function
|
|
identifiers, such as <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._substring%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">substring</a></span> and
|
|
<span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span>. More examples are below.</p><p>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.</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktVal">"rope"</span><span class="hspace"> </span><span class="RktVal">"twine"</span><span class="hspace"> </span><span class="RktVal">"yarn"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">append strings</span></td></tr><tr><td><p><span class="RktRes">"ropetwineyarn"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._substring%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">substring</a></span><span class="hspace"> </span><span class="RktVal">"corduroys"</span><span class="hspace"> </span><span class="RktVal">0</span><span class="hspace"> </span><span class="RktVal">4</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">extract a substring</span></td></tr><tr><td><p><span class="RktRes">"cord"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktValLink Sq" data-pltdoc="x">string-prefix?</a></span><span class="hspace"> </span><span class="RktVal">"shoelace"</span><span class="hspace"> </span><span class="RktVal">"shoe"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">recognize string prefix/suffix</span></td></tr><tr><td><p><span class="RktRes">#t</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528lib._racket%252Fstring..rkt%2529._string-suffix%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-suffix?</a></span><span class="hspace"> </span><span class="RktVal">"shoelace"</span><span class="hspace"> </span><span class="RktVal">"shoe"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#f</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string?</a></span><span class="hspace"> </span><span class="RktVal">"Ceci n'est pas une string."</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">recognize strings</span></td></tr><tr><td><p><span class="RktRes">#t</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string?</a></span><span class="hspace"> </span><span class="RktVal">1</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#f</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._sqrt%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">sqrt</a></span><span class="hspace"> </span><span class="RktVal">16</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">find a square root</span></td></tr><tr><td><p><span class="RktRes">4</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._sqrt%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">sqrt</a></span><span class="hspace"> </span><span class="RktVal"><span class="nobreak">-1</span>6</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">0+4i</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="hspace"> </span><span class="RktVal">1</span><span class="hspace"> </span><span class="RktVal">2</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">add numbers</span></td></tr><tr><td><p><span class="RktRes">3</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktVal">2</span><span class="hspace"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">subtract numbers</span></td></tr><tr><td><p><span class="RktRes">1</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%7E3c%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x"><</a></span><span class="hspace"> </span><span class="RktVal">2</span><span class="hspace"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">compare numbers</span></td></tr><tr><td><p><span class="RktRes">#f</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%7E3e%7E3d%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">>=</a></span><span class="hspace"> </span><span class="RktVal">2</span><span class="hspace"> </span><span class="RktVal">1</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#t</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._number%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">number?</a></span><span class="hspace"> </span><span class="RktVal">"c'est une number"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">recognize numbers</span></td></tr><tr><td><p><span class="RktRes">#f</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=number-types.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._number%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">number?</a></span><span class="hspace"> </span><span class="RktVal">1</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#t</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Equality.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._equal%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">equal?</a></span><span class="hspace"> </span><span class="RktVal">6</span><span class="hspace"> </span><span class="RktVal">"half dozen"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">compare anything</span></td></tr><tr><td><p><span class="RktRes">#f</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Equality.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._equal%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">equal?</a></span><span class="hspace"> </span><span class="RktVal">6</span><span class="hspace"> </span><span class="RktVal">6</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#t</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=Equality.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._equal%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">equal?</a></span><span class="hspace"> </span><span class="RktVal">"half dozen"</span><span class="hspace"> </span><span class="RktVal">"half dozen"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#t</span></p></td></tr></table></blockquote><h5 x-source-module="(lib "scribblings/guide/guide.scrbl")" x-source-pkg="racket-doc" x-part-tag=""Conditionals_with_if__and__or__and_cond"">2.2.5<tt> </tt><a name="(part._.Conditionals_with_if__and__or__and_cond)"></a>Conditionals with <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span>, <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">and</a></span>, <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">or</a></span>, and <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">cond</a></span></h5><p>The next simplest kind of expression is an <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span> conditional:</p><blockquote class="SCodeFlow"><p><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">if</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span></p></blockquote><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="finger.png" alt="+" width="24" height="24"/></span><a href="conditionals.html" data-pltdoc="x">Conditionals</a> (later in this guide) explains more about conditionals.</p></blockquote></blockquote></blockquote><p>The first <span class="sroman">‹<span style="font-style: italic">expr</span>›</span> is always evaluated. If it produces a
|
|
non-<span class="RktVal">#f</span> value, then the second <span class="sroman">‹<span style="font-style: italic">expr</span>›</span> is
|
|
evaluated for the result of the whole <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span> expression, otherwise
|
|
the third <span class="sroman">‹<span style="font-style: italic">expr</span>›</span> is evaluated for the result.</p><p><div class="SIntrapara">Example:</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%7E3e%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">></a></span><span class="hspace"> </span><span class="RktVal">2</span><span class="hspace"> </span><span class="RktVal">3</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktVal">"2 is bigger than 3"</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktVal">"2 is smaller than 3"</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p><span class="RktRes">"2 is smaller than 3"</span></p></td></tr></table></blockquote></div></p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">reply</span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktValLink Sq" data-pltdoc="x">string-prefix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"hello "</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"hi!"</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"huh?"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p> </p></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">reply</span><span class="hspace"> </span><span class="RktVal">"hello racket"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"hi!"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">reply</span><span class="hspace"> </span><span class="RktVal">"λx:(μα.α→α).xx"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"huh?"</span></p></td></tr></table></td></tr></table></blockquote><p>Complex conditionals can be formed by nesting <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span>
|
|
expressions. For example, in the previous <span class="RktSym">reply</span> example,
|
|
the input must be a string because <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktValLink Sq" data-pltdoc="x">string-prefix?</a></span>
|
|
would error when given non-strings. You can remove this restriction
|
|
by adding another <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span> to check first if the input is a string:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">reply-non-string</span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktValLink Sq" data-pltdoc="x">string-prefix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"hello "</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"hi!"</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"huh?"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"huh?"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>Instead of duplicating the <span class="RktVal">"huh?"</span> case, this function is
|
|
better written as</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">reply-non-string</span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktValLink Sq" data-pltdoc="x">string-prefix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"hello "</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">#f</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"hi!"</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"huh?"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>but these kinds of nested <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span>s are difficult to read. Racket
|
|
provides more readable shortcuts through the <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">and</a></span> and
|
|
<span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">or</a></span> forms:</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="finger.png" alt="+" width="24" height="24"/></span><a href="conditionals.html#%28part._and%2Bor%29" data-pltdoc="x">Combining Tests: <span class="RktSym"><span class="RktStxLink">and</span></span> and <span class="RktSym"><span class="RktStxLink">or</span></span></a> (later in this guide) explains more about <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">and</a></span> and <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">or</a></span>.</p></blockquote></blockquote></blockquote><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">and</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="sroman">*</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span></td></tr><tr><td><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">or</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="sroman">*</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span></td></tr></table></blockquote><p>The <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">and</a></span> form short-circuits: it stops and returns <span class="RktVal">#f</span>
|
|
when an expression produces <span class="RktVal">#f</span>, otherwise it keeps
|
|
going. The <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">or</a></span> form similarly short-circuits when it
|
|
encounters a true result.</p><p><div class="SIntrapara">Examples:</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">reply-non-string</span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">and</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktValLink Sq" data-pltdoc="x">string-prefix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"hello "</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"hi!"</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"huh?"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td> </td></tr></table></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">reply-non-string</span><span class="hspace"> </span><span class="RktVal">"hello racket"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"hi!"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">reply-non-string</span><span class="hspace"> </span><span class="RktVal">17</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"huh?"</span></p></td></tr></table></blockquote></div></p><p>Note that in the above grammar, the <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">and</a></span> and <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">or</a></span> forms
|
|
work with any number of expressions.</p><p><div class="SIntrapara">Examples:</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">reply-only-enthusiastic</span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">and</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktValLink Sq" data-pltdoc="x">string-prefix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"hello "</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528lib._racket%252Fstring..rkt%2529._string-suffix%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-suffix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"!"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"hi!"</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"huh?"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td> </td></tr></table></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">reply-only-enthusiastic</span><span class="hspace"> </span><span class="RktVal">"hello racket!"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"hi!"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">reply-only-enthusiastic</span><span class="hspace"> </span><span class="RktVal">"hello racket"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"huh?"</span></p></td></tr></table></blockquote></div></p><p>Another common pattern of nested <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span>s involves a sequence of
|
|
tests, each with its own result:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">reply-more</span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktValLink Sq" data-pltdoc="x">string-prefix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"hello "</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"hi!"</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktValLink Sq" data-pltdoc="x">string-prefix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"goodbye "</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"bye!"</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528lib._racket%252Fstring..rkt%2529._string-suffix%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-suffix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"?"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"I don't know"</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"huh?"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>The shorthand for a sequence of tests is the <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">cond</a></span> form:</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="finger.png" alt="+" width="24" height="24"/></span><a href="conditionals.html#%28part._cond%29" data-pltdoc="x">Chaining Tests: <span class="RktSym"><span class="RktStxLink">cond</span></span></a> (later in this guide) explains more about <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">cond</a></span>.</p></blockquote></blockquote></blockquote><blockquote class="SCodeFlow"><p><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">cond</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">{</span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">[</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="sroman">*</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">]</span><span class="hspace"></span></span><span class="sroman">}</span><span class="sroman">*</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span></p></blockquote><p>A <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">cond</a></span> form contains a sequence of clauses between square
|
|
brackets. In each clause, the first <span class="sroman">‹<span style="font-style: italic">expr</span>›</span> is a test
|
|
expression. If it produces true, then the clause’s remaining
|
|
<span class="sroman">‹<span style="font-style: italic">expr</span>›</span>s are evaluated, and the last one in the clause provides
|
|
the answer for the entire <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">cond</a></span> expression; the rest of the
|
|
clauses are ignored. If the test <span class="sroman">‹<span style="font-style: italic">expr</span>›</span> produces <span class="RktVal">#f</span>,
|
|
then the clause’s remaining <span class="sroman">‹<span style="font-style: italic">expr</span>›</span>s are ignored, and
|
|
evaluation continues with the next clause. The last clause can use
|
|
<span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">else</a></span> as a synonym for a <span class="RktVal">#t</span> test expression.</p><p>Using <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">cond</a></span>, the <span class="RktSym">reply-more</span> function can be more
|
|
clearly written as follows:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">reply-more</span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktValLink Sq" data-pltdoc="x">string-prefix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"hello "</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"hi!"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktValLink Sq" data-pltdoc="x">string-prefix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"goodbye "</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"bye!"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528lib._racket%252Fstring..rkt%2529._string-suffix%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-suffix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"?"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVal">"I don't know"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">else</a></span><span class="hspace"> </span><span class="RktVal">"huh?"</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p> </p></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">reply-more</span><span class="hspace"> </span><span class="RktVal">"hello racket"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"hi!"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">reply-more</span><span class="hspace"> </span><span class="RktVal">"goodbye cruel world"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"bye!"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">reply-more</span><span class="hspace"> </span><span class="RktVal">"what is your favorite color?"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"I don't know"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">reply-more</span><span class="hspace"> </span><span class="RktVal">"mine is lime green"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"huh?"</span></p></td></tr></table></td></tr></table></blockquote><p>The use of square brackets for <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">cond</a></span> clauses is a
|
|
convention. In Racket, parentheses and square brackets are actually
|
|
interchangeable, as long as <span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span> is matched with <span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span> and
|
|
<span class="RktInBG"><span class="hspace"></span><span class="RktIn">[</span><span class="hspace"></span></span> is matched with <span class="RktInBG"><span class="hspace"></span><span class="RktIn">]</span><span class="hspace"></span></span>. Using square brackets in a
|
|
few key places makes Racket code even more readable.</p><h5 x-source-module="(lib "scribblings/guide/guide.scrbl")" x-source-pkg="racket-doc" x-part-tag=""Function_Calls__Again"">2.2.6<tt> </tt><a name="(part._.Function_.Calls__.Again)"></a>Function Calls, Again</h5><p>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 <span class="sroman">‹<span style="font-style: italic">id</span>›</span>:</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="finger.png" alt="+" width="24" height="24"/></span><a href="application.html" data-pltdoc="x">Function Calls</a> (later in this guide) explains more about function calls.</p></blockquote></blockquote></blockquote><blockquote class="SCodeFlow"><p><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="sroman">*</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span></p></blockquote><p>The first <span class="sroman">‹<span style="font-style: italic">expr</span>›</span> is often an <span class="sroman">‹<span style="font-style: italic">id</span>›</span>, such
|
|
as <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span> or <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">+</a></span>, but it can be anything that
|
|
evaluates to a function. For example, it can be a conditional
|
|
expression:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">double</span><span class="hspace"> </span><span class="RktSym">v</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=if.html%23%2528form._%2528%2528quote._%7E23%7E25kernel%2529._if%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">if</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string%7E3f%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string?</a></span><span class="hspace"> </span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym">v</span><span class="hspace"> </span><span class="RktSym">v</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p> </p></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">double</span><span class="hspace"> </span><span class="RktVal">"mnah"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"mnahmnah"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">double</span><span class="hspace"> </span><span class="RktVal">5</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">10</span></p></td></tr></table></td></tr></table></blockquote><p>Syntactically, the first expression in a function call could
|
|
even be a number—<wbr></wbr>but that leads to an error, since a number is not a
|
|
function.</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktVal">1</span><span class="hspace"> </span><span class="RktVal">2</span><span class="hspace"> </span><span class="RktVal">3</span><span class="hspace"> </span><span class="RktVal">4</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">application: not a procedure;</span></p></td></tr><tr><td><p><span class="RktErr"></span><span class="hspace"> </span><span class="RktErr">expected a procedure that can be applied to arguments</span></p></td></tr><tr><td><p><span class="RktErr"></span><span class="hspace"> </span><span class="RktErr">given: 1</span></p></td></tr></table></blockquote><p>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.</p><h5 x-source-module="(lib "scribblings/guide/guide.scrbl")" x-source-pkg="racket-doc" x-part-tag=""Anonymous_Functions_with_lambda"">2.2.7<tt> </tt><a name="(part._.Anonymous_.Functions_with_lambda)"></a>Anonymous Functions with <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span></h5><p>Programming in Racket would be tedious if you had to name all of your
|
|
numbers. Instead of writing <span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="stt"> </span><span class="RktVal">1</span><span class="stt"> </span><span class="RktVal">2</span><span class="RktPn">)</span>, you’d have to write</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="finger.png" alt="+" width="24" height="24"/></span><a href="lambda.html" data-pltdoc="x">Functions: <span class="RktSym"><span class="RktStxLink">lambda</span></span></a> (later in this guide) explains more about <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span>.</p></blockquote></blockquote></blockquote><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">a</span><span class="hspace"> </span><span class="RktVal">1</span><span class="RktPn">)</span></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">b</span><span class="hspace"> </span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%252B%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">+</a></span><span class="hspace"> </span><span class="RktSym">a</span><span class="hspace"> </span><span class="RktSym">b</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">3</span></p></td></tr></table></blockquote><p>It turns out that having to name all your functions can be tedious,
|
|
too. For example, you might have a function <span class="RktSym">twice</span> that takes
|
|
a function and an argument. Using <span class="RktSym">twice</span> is convenient if you
|
|
already have a name for the function, such as <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._sqrt%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">sqrt</a></span>:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">twice</span><span class="hspace"> </span><span class="RktSym">f</span><span class="hspace"> </span><span class="RktSym">v</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">f</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">f</span><span class="hspace"> </span><span class="RktSym">v</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p> </p></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">twice</span><span class="hspace"> </span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._sqrt%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">sqrt</a></span><span class="hspace"> </span><span class="RktVal">16</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">2</span></p></td></tr></table></td></tr></table></blockquote><p>If you want to call a function that is not yet defined, you could
|
|
define it, and then pass it to <span class="RktSym">twice</span>:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">louder</span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"!"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p> </p></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">twice</span><span class="hspace"> </span><span class="RktSym">louder</span><span class="hspace"> </span><span class="RktVal">"hello"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"hello!!"</span></p></td></tr></table></td></tr></table></blockquote><p>But if the call to <span class="RktSym">twice</span> is the only place where
|
|
<span class="RktSym">louder</span> is used, it’s a shame to have to write a whole
|
|
definition. In Racket, you can use a <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span> expression to
|
|
produce a function directly. The <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span> form is followed by
|
|
identifiers for the function’s arguments, and then the function’s
|
|
body expressions:</p><blockquote class="SCodeFlow"><p><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">lambda</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">id</span>›</span><span class="sroman">*</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span style="vertical-align: super; font-size: 80%">+</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span></p></blockquote><p>Evaluating a <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span> form by itself produces a function:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"!"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#<procedure></span></p></td></tr></table></blockquote><p>Using <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span>, the above call to <span class="RktSym">twice</span> can be
|
|
re-written as</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">twice</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"!"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktVal">"hello"</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p><span class="RktRes">"hello!!"</span></p></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">twice</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"?!"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktVal">"hello"</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p><span class="RktRes">"hello?!?!"</span></p></td></tr></table></blockquote><p>Another use of <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span> is as a result for a function that
|
|
generates functions:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">make-add-suffix</span><span class="hspace"> </span><span class="RktSym">s2</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktSym">s2</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p> </p></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">twice</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">make-add-suffix</span><span class="hspace"> </span><span class="RktVal">"!"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">"hello"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"hello!!"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">twice</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">make-add-suffix</span><span class="hspace"> </span><span class="RktVal">"?!"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">"hello"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"hello?!?!"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">twice</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">make-add-suffix</span><span class="hspace"> </span><span class="RktVal">"..."</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">"hello"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"hello......"</span></p></td></tr></table></td></tr></table></blockquote><p>Racket is a <span style="font-style: italic">lexically scoped</span> language, which means that
|
|
<span class="RktSym">s2</span> in the function returned by <span class="RktSym">make-add-suffix</span>
|
|
always refers to the argument for the call that created the
|
|
function. In other words, the <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span>-generated function
|
|
“remembers” the right <span class="RktSym">s2</span>:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">louder</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">make-add-suffix</span><span class="hspace"> </span><span class="RktVal">"!"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">less-sure</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">make-add-suffix</span><span class="hspace"> </span><span class="RktVal">"?"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">twice</span><span class="hspace"> </span><span class="RktSym">less-sure</span><span class="hspace"> </span><span class="RktVal">"really"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"really??"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">twice</span><span class="hspace"> </span><span class="RktSym">louder</span><span class="hspace"> </span><span class="RktVal">"really"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"really!!"</span></p></td></tr></table></blockquote><p>We have so far referred to definitions of the form <span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="stt"> </span><span class="sroman">‹<span style="font-style: italic">id</span>›</span><span class="stt"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="RktPn">)</span> as “non-function
|
|
definitions.” This characterization is misleading, because the
|
|
<span class="sroman">‹<span style="font-style: italic">expr</span>›</span> could be a <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span> form, in which case
|
|
the definition is equivalent to using the “function” definition
|
|
form. For example, the following two definitions of <span class="RktSym">louder</span>
|
|
are equivalent:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">louder</span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"!"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">louder</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktVal">"!"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p> </p></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktSym">louder</span></td></tr><tr><td><p><span class="RktRes">#<procedure:louder></span></p></td></tr></table></td></tr></table></blockquote><p>Note that the expression for <span class="RktSym">louder</span> in the second case is an
|
|
“anonymous” function written with <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=lambda.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._lambda%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">lambda</a></span>, but, if
|
|
possible, the compiler infers a name, anyway, to make printing and
|
|
error reporting as informative as possible.</p><h5 x-source-module="(lib "scribblings/guide/guide.scrbl")" x-source-pkg="racket-doc" x-part-tag=""local-binding-intro"">2.2.8<tt> </tt><a name="(part._local-binding-intro)"></a>Local Binding with
|
|
<span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span>, <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let</a></span>, and <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%252A%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let*</a></span></h5><p>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:</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="finger.png" alt="+" width="24" height="24"/></span><a href="define.html#%28part._intdefs%29" data-pltdoc="x">Internal Definitions</a> (later in this guide) explains more about local (internal) definitions.</p></blockquote></blockquote></blockquote><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">define</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">id</span>›</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">id</span>›</span><span class="sroman">*</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">definition</span>›</span><span class="sroman">*</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span style="vertical-align: super; font-size: 80%">+</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span></td></tr><tr><td><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">lambda</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">id</span>›</span><span class="sroman">*</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">definition</span>›</span><span class="sroman">*</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span style="vertical-align: super; font-size: 80%">+</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span></td></tr></table></blockquote><p>Definitions at the start of a function body are local to the
|
|
function body.</p><p><div class="SIntrapara">Examples:</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">converse</span><span class="hspace"> </span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">starts?</span><span class="hspace"> </span><span class="RktSym">s2</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">local to </span><span class="RktSym">converse</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span><span class="hspace"> </span><span class="RktSym">spaced-s2</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktSym">s2</span><span class="hspace"> </span><span class="RktVal">" "</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">local to </span><span class="RktSym">starts?</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktValLink Sq" data-pltdoc="x">string-prefix?</a></span><span class="hspace"> </span><span class="RktSym">s</span><span class="hspace"> </span><span class="RktSym">spaced-s2</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym">starts?</span><span class="hspace"> </span><span class="RktVal">"hello"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">"hi!"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym">starts?</span><span class="hspace"> </span><span class="RktVal">"goodbye"</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">"bye!"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">else</a></span><span class="hspace"> </span><span class="RktVal">"huh?"</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td> </td></tr></table></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">converse</span><span class="hspace"> </span><span class="RktVal">"hello world"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"hi!"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">converse</span><span class="hspace"> </span><span class="RktVal">"hellonearth"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"huh?"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">converse</span><span class="hspace"> </span><span class="RktVal">"goodbye friends"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"bye!"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">converse</span><span class="hspace"> </span><span class="RktVal">"urp"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"huh?"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktSym">starts?</span><span class="hspace"> </span><span class="RktCmt">;</span><span class="RktCmt"> </span><span class="RktCmt">outside of </span><span class="RktSym">converse</span><span class="RktCmt">, so...</span></td></tr><tr><td><p><span class="RktErr">starts?: undefined;</span></p></td></tr><tr><td><p><span class="RktErr"></span><span class="hspace"> </span><span class="RktErr">cannot reference an identifier before its definition</span></p></td></tr><tr><td><p><span class="RktErr"></span><span class="hspace"> </span><span class="RktErr">in module: top-level</span></p></td></tr></table></blockquote></div></p><p>Another way to create local bindings is the <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let</a></span> form. An
|
|
advantage of <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let</a></span> is that it can be used in any expression
|
|
position. Also, <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let</a></span> binds many identifiers at once, instead
|
|
of requiring a separate <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=define.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._define%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">define</a></span> for each identifier.</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="finger.png" alt="+" width="24" height="24"/></span><a href="define.html#%28part._intdefs%29" data-pltdoc="x">Internal Definitions</a> (later in this guide) explains more about <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let</a></span> and <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%252A%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let*</a></span>.</p></blockquote></blockquote></blockquote><blockquote class="SCodeFlow"><p><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">let</span><span class="hspace"></span></span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">{</span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">[</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">id</span>›</span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">]</span><span class="hspace"></span></span><span class="sroman">}</span><span class="sroman">*</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span><span class="hspace"> </span><span class="sroman">‹<span style="font-style: italic">expr</span>›</span><span style="vertical-align: super; font-size: 80%">+</span><span class="hspace"> </span><span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span></p></blockquote><p>Each binding clause is an <span class="sroman">‹<span style="font-style: italic">id</span>›</span> and an
|
|
<span class="sroman">‹<span style="font-style: italic">expr</span>›</span> surrounded by square brackets, and the
|
|
expressions after the clauses are the body of the <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let</a></span>. In
|
|
each clause, the <span class="sroman">‹<span style="font-style: italic">id</span>›</span> is bound to the result of the
|
|
<span class="sroman">‹<span style="font-style: italic">expr</span>›</span> for use in the body.</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">[</span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._random%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">random</a></span><span class="hspace"> </span><span class="RktVal">4</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym">o</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._random%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">random</a></span><span class="hspace"> </span><span class="RktVal">4</span><span class="RktPn">)</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%7E3e%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">></a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktSym">o</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">"X wins"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%7E3e%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">></a></span><span class="hspace"> </span><span class="RktSym">o</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktVal">"O wins"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">else</a></span><span class="hspace"> </span><span class="RktVal">"cat's game"</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p><span class="RktRes">"O wins"</span></p></td></tr></table></blockquote><p>The bindings of a <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let</a></span> form are available only in the body of
|
|
the <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let</a></span>, so the binding clauses cannot refer to each
|
|
other. The <span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%252A%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let*</a></span> form, in contrast, allows later clauses to
|
|
use earlier bindings:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=let.html%23%2528form._%2528%2528lib._racket%252Fprivate%252Fletstx-scheme..rkt%2529._let%252A%2529%2529&version=8.6" class="RktStxLink Sq" data-pltdoc="x">let*</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktPn">[</span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._random%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">random</a></span><span class="hspace"> </span><span class="RktVal">4</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym">o</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528lib._racket%252Fprivate%252Fbase..rkt%2529._random%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">random</a></span><span class="hspace"> </span><span class="RktVal">4</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym">diff</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._number-%7E3estring%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">number->string</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._abs%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">abs</a></span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._-%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktSym">o</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%7E3e%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">></a></span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktSym">o</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktVal">"X wins by "</span><span class="hspace"> </span><span class="RktSym">diff</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=generic-numbers.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._%7E3e%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">></a></span><span class="hspace"> </span><span class="RktSym">o</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/doc/local-redirect/index.html?doc=reference&rel=strings.html%23%2528def._%2528%2528quote._%7E23%7E25kernel%2529._string-append%2529%2529&version=8.6" class="RktValLink Sq" data-pltdoc="x">string-append</a></span><span class="hspace"> </span><span class="RktVal">"O wins by "</span><span class="hspace"> </span><span class="RktSym">diff</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym"><a href="https://download.racket-lang.org/releases/8.6/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.6" class="RktStxLink Sq" data-pltdoc="x">else</a></span><span class="hspace"> </span><span class="RktVal">"cat's game"</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p><span class="RktRes">"cat's game"</span></p></td></tr></table></blockquote><div class="navsetbottom"><span class="navleft"><form class="searchform"><input class="searchbox" id="searchbox" type="text" tabindex="1" placeholder="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, "8.6", "../");"/></form> <a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("8.6");">top</a><span class="tocsettoggle"> <a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright"> <a href="Simple_Values.html" title="backward to "2.1 Simple Values"" data-pltdoc="x">← prev</a> <a href="to-scheme.html" title="up to "2 Racket Essentials"" data-pltdoc="x">up</a> <a href="Lists__Iteration__and_Recursion.html" title="forward to "2.3 Lists, Iteration, and Recursion"" data-pltdoc="x">next →</a></span> </div></div></div><div id="contextindicator"> </div></body></html> |