emacs.d/clones/lisp/htdp.org/2022-8-7/Book/part_prologue.html

608 lines
200 KiB
HTML
Raw Normal View History

2022-08-15 11:06:56 +02:00
<!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>Prologue: How to Program</title><link rel="stylesheet" type="text/css" href="scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="shared.css" title="default"/><link rel="stylesheet" type="text/css" href="figure.css" title="default"/><link rel="stylesheet" type="text/css" href="racket.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"/><script type="text/javascript" src="scribble-common.js"></script><script type="text/javascript" src="figure.js"></script><script type="text/javascript" src="manual-racket.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="scribble-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,&quot;tocview_0&quot;);">&#9660;</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">How to Design Programs, Second Edition</a></td></tr></table></div><div class="tocviewsublisttop" style="display: block;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right"></td><td><a href="part_preface.html" class="tocviewlink" data-pltdoc="x">Preface</a></td></tr><tr><td align="right"></td><td><a href="part_prologue.html" class="tocviewselflink" data-pltdoc="x">Prologue:<span class="mywbr"> &nbsp;</span> How to Program</a></td></tr><tr><td align="right">I&nbsp;</td><td><a href="part_one.html" class="tocviewlink" data-pltdoc="x">Fixed-<wbr></wbr>Size Data</a></td></tr><tr><td align="right"></td><td><a href="i1-2.html" class="tocviewlink" data-pltdoc="x">Intermezzo 1: Beginning Student Language</a></td></tr><tr><td align="right">II&nbsp;</td><td><a href="part_two.html" class="tocviewlink" data-pltdoc="x">Arbitrarily Large Data</a></td></tr><tr><td align="right"></td><td><a href="i2-3.html" class="tocviewlink" data-pltdoc="x">Intermezzo 2: Quote, Unquote</a></td></tr><tr><td align="right">III&nbsp;</td><td><a href="part_three.html" class="tocviewlink" data-pltdoc="x">Abstraction</a></td></tr><tr><td align="right"></td><td><a href="i3-4.html" class="tocviewlink" data-pltdoc="x">Intermezzo 3: Scope and Abstraction</a></td></tr><tr><td align="right">IV&nbsp;</td><td><a href="part_four.html" class="tocviewlink" data-pltdoc="x">Intertwined Data</a></td></tr><tr><td align="right"></td><td><a href="i4-5.html" class="tocviewlink" data-pltdoc="x">Intermezzo 4: The Nature of Numbers</a></td></tr><tr><td align="right">V&nbsp;</td><td><a href="part_five.html" class="tocviewlink" data-pltdoc="x">Generative Recursion</a></td></tr><tr><td align="right"></td><td><a href="i5-6.html" class="tocviewlink" data-pltdoc="x">Intermezzo 5: The Cost of Computation</a></td></tr><tr><td align="right">VI&nbsp;</td><td><a href="part_six.html" class="tocviewlink" data-pltdoc="x">Accumulators</a></td></tr><tr><td align="right"></td><td><a href="part_epilogue.html" class="tocviewlink" data-pltdoc="x">Epilogue:<span class="mywbr"> &nbsp;</span> Moving On</a></td></tr></table></div></div><div class="tocviewlist"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;">&bull;</td><td></td><td><a href="part_prologue.html" class="tocviewselflink" data-pltdoc="x">Prologue:<span class="mywbr"> &nbsp;</span> How to Program</a></td></tr></table></div></div></div><div class="maincolumn"><div class="main"><div class="versionbox"><span class="version">8.6.0.2</span></div><div class="navsettop"><span class="navleft"><div class="nosearchform"></div>&nbsp;&nbsp;<span class="tocsettoggle">&nbsp;&nbsp;<a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a
and<span class="refelem"><span class="refcolumn"><span class="refcontent">Consider a quick look at
<a href="https://htdp.org/2022-8-7/notes/note_teaching.html" data-pltdoc="x">On Teaching Part I</a>.</span></span></span> perform simple calculations with your fingers:
&ldquo;1 + 1 is 2&rdquo;; &ldquo;1 + 2 is 3&rdquo;; and so on. Then they would ask &ldquo;what&rsquo;s 3
+ 2?&rdquo; and you would count off the fingers of one hand. They programmed,
and you computed. And in some way, that&rsquo;s really all there is to
programming and computing.</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p>Download DrRacket from <a href="https://download.racket-lang.org/">its web site</a>.</p></blockquote></blockquote></blockquote><p><div class="SIntrapara">Now it is time to switch roles. Start DrRacket. Doing so brings up the window
of <a href="part_prologue.html#%28counter._%28figure._fig~3adrracket%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">3</span></a>. Select &ldquo;Choose language&rdquo;
from the &ldquo;Language&rdquo; menu, which opens a dialog listing &ldquo;Teaching
Languages&rdquo; for &ldquo;How to Design Programs.&rdquo; Choose &ldquo;Beginning Student&rdquo;
(the Beginning Student Language, or BSL)
and click <span class="emph">OK</span> to set up DrRacket. With this task
completed, <span style="font-weight: bold">you</span> can program, and the DrRacket software becomes the child. Start
with the simplest of all calculations. You type
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara"> into the top part of DrRacket, click <span class="emph">RUN</span>, and
a <span class="RktVal">2</span> shows up in the bottom.</div></p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><p><img src="drracket-plain.png" alt="" width="722" height="754"/></p></blockquote></blockquote><p class="Centertext"><span class="Legend"><span class="FigureTarget"><a name="(counter._(figure._fig~3adrracket))" x-target-lift="Figure"></a>Figure&nbsp;3: </span>Meet DrRacket</span></p></blockquote><p><div class="SIntrapara">That&rsquo;s how simple programming is. You ask questions as if DrRacket were
a child, and DrRacket computes for you. You can also ask DrRacket to process several
requests at once:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="hspace">&nbsp;</span><span class="RktVal">6</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">After you click <span class="emph">RUN</span>, you see
<span class="RktRes">4<span class="stt"> </span>9<span class="stt"> </span>2<span class="stt"> </span>3</span>
in the bottom half of DrRacket, which are the expected results.</div></p><p><div class="SIntrapara">Let&rsquo;s slow down for a moment and introduce some words:
</div><div class="SIntrapara"><ul><li><p>The top half of DrRacket is called the <span style="font-style: italic">definitions area</span>. In
this area, you create the programs, which is called <span style="font-style: italic">editing</span>. As
soon as you add a word or change something in the definitions area, the
<span class="emph">SAVE</span> button shows up in the top-left corner. When you click
<span class="emph">SAVE</span> for the first time, DrRacket asks you for the name of a file so
that it can store your program for good. Once your definitions area is
associated with a file, clicking <span class="emph">SAVE</span> ensures that the
content of the definitions area is stored safely in the file.</p></li><li><p><span style="font-style: italic">Programs</span> consist of <span style="font-style: italic">expressions</span>. You have seen
expressions in mathematics. For now, an expression is either a plain
number or something that starts with a left parenthesis &ldquo;(&rdquo; and ends in
a matching right parenthesis &ldquo;)&rdquo;&#8212;<wbr></wbr>which DrRacket rewards by shading the
area between the pair of parentheses.</p></li><li><p><div class="SIntrapara">When you click <span class="emph">RUN</span>, DrRacket evaluates the expressions in the
definitions area and shows their result in the <span style="font-style: italic">interactions
area</span>. Then, DrRacket, your faithful servant, awaits your commands at the
<span style="font-style: italic">prompt</span> (<span class="stt">&gt;</span>). The appearance of the prompt signals that DrRacket is
waiting for you to enter additional expressions, which it then evaluates
like those in the definitions area:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">2</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Enter an expression at the prompt, hit the &ldquo;return&rdquo; or &ldquo;enter&rdquo; key on
your keyboard, and watch how DrRacket responds with the result. You can do
so as often as you wish:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">4</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">9</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">2</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="hspace">&nbsp;</span><span class="RktVal">6</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">3</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._sqr%29%29" class="RktValLink" data-pltdoc="x">sqr</a></span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">9</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._expt%29%29" class="RktValLink" data-pltdoc="x">expt</a></span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">8</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._sin%29%29" class="RktValLink" data-pltdoc="x">sin</a></span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">0</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._cos%29%29" class="RktValLink" data-pltdoc="x">cos</a></span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._pi%29%29" class="RktValLink" data-pltdoc="x">pi</a></span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#i-1.0</span></p></td></tr></table></blockquote></div></p>
don&rsquo;t really know the precise number so take that for now&rdquo; or an
<span style="font-style: italic">inexact number</span>. Unlike your
calculator or other programming systems, DrRacket is
honest. When it doesn&rsquo;t know the exact number, it warns you with this
special prefix. Later, we will show you really strange facts about
&ldquo;computer numbers,&rdquo; and you will then truly appreciate that DrRacket
issues such warnings.</div></p><p><div class="SIntrapara">By now you might be wondering whether DrRacket can add more than two
numbers at once, and yes, it can! As a matter of fact, it can do it in two
different ways:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">9</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">9</span></p></td></tr></table></blockquote></div><div class="SIntrapara">The first one is <span style="font-style: italic">nested arithmetic</span>, as you know it from school.
The second one is <span style="font-style: italic">BSL arithmetic</span>; and the latter is natural,
because in this notation you always use parentheses to group operations
and numbers together.</div></p><p><div class="SIntrapara">In BSL,<span class="refelem"><span class="refcolumn"><span class="refcontent">This book does not teach you Racket, even if the
editor is called DrRacket. See the Preface, especially the section on
<span style="font-style: italic">DrRacket and the Teaching Languages</span> for details on the choice to
develop our own language.</span></span></span> every time you want to use a &ldquo;calculator
operation,&rdquo; you write down an opening parenthesis, the operation you wish
to perform, say <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span>, the numbers on which the operation should work
(separated by spaces or even line breaks), and, finally, a closing
parenthesis. The items following the operation are called the
<span style="font-style: italic">operands</span>. Nested arithmetic means that you can use an
expression for an operand, which is why
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">9</span></p></td></tr></table></blockquote></div><div class="SIntrapara">is a fine program. You can do this as often as you wish:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">15</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="hspace">&nbsp;</span><span class="RktVal">12</span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">15</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" d
and love from math. Like you, it can determine the result of an addition
only when all the operands are plain numbers. If an operand is a
parenthesized operator expression&#8212;<wbr></wbr>something that starts with a &ldquo;(&rdquo; and
an operation&#8212;<wbr></wbr>it determines the result of that nested expression
first. Unlike you, it never needs to ponder which expression to
calculate first&#8212;<wbr></wbr>because this first rule is the only rule there is.</p><p><div class="SIntrapara">The price for DrRacket&rsquo;s convenience is that parentheses have meaning. You
must enter all these parentheses, and you may not enter too
many. For example, while extra parentheses are acceptable to your math
teacher, this is not the case for BSL. The expression <span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="stt"> </span><span class="RktPn">(</span><span class="RktVal">1</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktPn">(</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span>
contains way too many parentheses, and DrRacket lets you know in no uncertain terms:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktVal">1</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">function call:expected a function after the open parenthesis, found a number</span></p></td></tr></table></blockquote></div></p><p><div class="SIntrapara">Once you get used to BSL programming, though, you will see that it
isn&rsquo;t a price at all. First, you get to use operations on several
operands at once, if it is natural to do so:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="hspace">&nbsp;</span><span class="RktVal">6</span><span class="hspace">&nbsp;</span><span class="RktVal">7</span><span class="hspace">&nbsp;</span><span class="RktVal">8</span><span class="hspace">&nbsp;</span><span class="RktVal">9</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">45</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="hspace">&nbsp;</span><span class="RktVal">6</span><span class="hspace">&nbsp;</span><span class="RktVal">7</span><span class="hspace">&nbsp;</span><span class="RktVal">8</span><span class="hspace">&nbsp;</span><span class="RktVal">9</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">0</span></p></td></tr></table></blockquote></div><div class="SIntrapara">If you don&rsquo;t know what an operation does for several operands, enter an example
into the interactions area and hit &ldquo;return&rdquo;; DrRacket lets you know whether
and how it works. Or use HelpDesk to read the documentation.<span class="refelem"><span class="refcolumn"><span class="refcontent">If you
are using the standard BSL option, click &ldquo;Check Syntax&rdquo; and notice that the
names are now hyper-linked to the documentation in HelpDesk. If you are using
<span class="stt">#lang htdp/bsl</span>, the names of operations are always
linked. Right-click on any name, say <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span>, and choose &ldquo;View
Documentation ...&rdquo; from the pop-up menu. This action will open a browser
window to the appropriate documentation entry.</span></span></span>
Second, when you read programs that others write, you will never have
to wonder which expressions are evaluated first. The parentheses and the
nesting will immediately tell you.</div></p><p>In this context, to program is to write down comprehensible arithmetic
expressions, and to compute is to determine their value. With DrRacket, it is
easy to explore this kind of programming and computing.</p><h4><a name="(part._arithmetic-pro)"></a>Arithmetic and Arithmetic</h4><p>If programming were just about numbers and arithmetic, it would be as
boring as mathematics. Fortunately, there is much more to programming than
numbers: text, truths, images, and a great deal more.<span class="refelem"><span class="refcolumn"><span class="refcontent">Just
kidding: mathematics is a fascinating subject, but you won&rsquo;t need much of
it for now.</span></span></span></p><p><div class="SIntrapara">The first thing you need to know is that in BSL, text is any sequence of
keyboard characters enclosed in double quotes (<span class="RktInBG"><span class="hspace"></span><span class="RktIn">"</span><span class="hspace"></span></span>). We call it a
string. Thus, <span class="RktVal">"hello world"</span> is a perfectly fine string; and when
DrRacket evaluates this string, it just echoes it back in the interactions
area, like a number:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktVal">"hello world"</span></td></tr><tr><td><p><span class="RktRes">"hello world"</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Indeed, many people&rsquo;s first program is one that displays exactly this string.</div></p><p><div class="SIntrapara">Otherwise, you need to know that in addition to an arithmetic of numbers,
DrRacket also knows about an arithmetic of strings. So here are two
interactions that illustrate this form of arithmetic:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"hello"</span><span class="hspace">&nbsp;</span><span class="RktVal">"world"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"helloworld"</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"hello "</span><span class="hspace">&nbsp;</span><span class="RktVal">"world"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"hello world"</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Just like <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span>, <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span> is an operation; it makes a
string by adding the second to the end of the first. As the first interaction
shows, it does this literally, without adding anything between the two
strings: no blank space, no comma, nothing. Thus, if you want to see the
phrase <span class="RktVal">"hello world"</span>, you really need to add a space to one of
these words somewhere; that&rsquo;s what the second interaction shows. Of
course, the most natural way to create this phrase from the two words is
to enter
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"hello"</span><span class="hspace">&nbsp;</span><span class="RktVal">" "</span><span class="hspace">&nbsp;</span><span class="RktVal">"world"</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">because <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span>, like <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span>, can handle as many
operands as desired.</div></p><p>You can do more with strings than append them. You can extract pieces from
a string, reverse them, render all letters uppercase (or lowercase), strip
blank spaces from the left and right, and so on. And best of all, you
don&rsquo;t have to memorize any of that. If you need to know what you can do
with strings, look up the term in HelpDesk.<span class="refelem"><span class="refcolumn"><span class="refcontent">Use F1 or the drop-down
menu on the right to open HelpDesk. Look at the manuals for BSL and its
section on pre-defined operations, especially those for strings.</span></span></span></p><p><div class="SIntrapara">If you looked up the primitive operations of BSL, you saw that
<span style="font-style: italic">primitive</span> (sometimes called <span style="font-style: italic">pre-defined</span> or
<span style="font-style: italic">built-in</span>) operations can consume strings and produce
numbers:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-length%29%29" class="RktValLink" data-pltdoc="x">string-length</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"hello world"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">31</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._number-~3estring%29%29" class="RktValLink" data-pltdoc="x">number-&gt;string</a></span><span class="hspace">&nbsp;</span><span class="RktVal">42</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"42"</span></p></td></tr></table></blockquote></div><div class="SIntrapara">There is also an operation that converts strings into numbers:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-~3enumber%29%29" class="RktValLink" data-pltdoc="x">string-&gt;number</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"42"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">42</span></p></td></tr></table></blockquote></div><div class="SIntrapara">If you expected &ldquo;forty-two&rdquo; or something clever along those lines,
sorry, that&rsquo;s really not what you want from a string calculator.</div></p><p><div class="SIntrapara">The last expression raises a question, though. What if someone uses <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-~3enumber%29%29" class="RktValLink" data-pltdoc="x">string-&gt;number</a></span>
with a string that is not a number wrapped within string quotes? In that
case, the operation produces a different kind of result:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-~3enumber%29%29" class="RktValLink" data-pltdoc="x">string-&gt;number</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"hello world"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#false</span></p></td></tr></table></blockquote></div><div class="SIntrapara">This is neither a number nor a string; it is a Boolean. Unlike numbers
and strings, Boolean values come in only two varieties: <span class="RktVal">#true</span> and
<span class="RktVal">#false</span>. The first is truth, the second falsehood. Even so, DrRacket
has several operations for combining Boolean values:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._and%29%29" class="RktStxLink" data-pltdoc="x">and</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#true</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#true</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._and%29%29" class="RktStxLink" data-pltdoc="x">and</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#true</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#false</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._or%29%29" class="RktStxLink" data-pltdoc="x">or</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#true</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#true</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._or%29%29" class="RktStxLink" data-pltdoc="x">or</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#false</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._not%29%29" class="RktValLink" data-pltdoc="x">not</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#true</span></p></td></tr></table></blockquote></div><div class="SIntrapara">and you get the results that the name of the operation suggests. (Don&rsquo;t
know what <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._and%29%29" class="RktStxLink" data-pltdoc="x">and</a></span>, <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._or%29%29" class="RktStxLink" data-pltdoc="x">or</a></span>, and <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._not%29%29" class="RktValLink" data-pltdoc="x">not</a></span> compute? Easy:
<span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._and%29%29" class="RktStxLink" data-pltdoc="x">and</a></span><span class="stt"> </span><span class="RktSym">x</span><span class="stt"> </span><span class="RktSym">y</span><span class="RktPn">)</span> is true if <span class="RktSym">x</span> and <span class="RktSym">y</span> are true;
<span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._or%29%29" class="RktStxLink" data-pltdoc="x">or</a></span><span class="stt"> </span><span class="RktSym">x</span><span class="stt"> </span><span class="RktSym">y</span><span class="RktPn">)</span> is true if either <span class="RktSym">x</span> or <span class="RktSym">y</span> or both are
true; and <span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._not%29%29" class="RktValLink" data-pltdoc="x">not</a></span><span class="stt"> </span><span class="RktSym">x</span><span class="RktPn">)</span> results in <span class="RktVal">#true</span> precisely when
<span class="RktSym">x</span> is <span class="RktVal">#false</span>.)</div></p><p><div class="SIntrapara">It is also useful to &ldquo;convert&rdquo; two numbers into a Boolean:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3e%29%29" class="RktValLink" data-pltdoc="x">&gt;</a></span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">9</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#true</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3c%29%29" class="RktValLink" data-pltdoc="x">&lt;</a></span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span></span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#true</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3d%29%29" class="RktValLink" data-pltdoc="x">=</a></span><span class="hspace">&nbsp;</span><span class="RktVal">42</span><span class="hspace">&nbsp;</span><span class="RktVal">9</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#false</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Stop! Try the following three expressions: <span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3e~3d%29%29" class="RktValLink" data-pltdoc="x">&gt;=</a></span><span class="stt"> </span><span class="RktVal">10</span><span class="stt"> </span><span class="RktVal">10</span><span class="RktPn">)</span>, <span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="stt"> </span><span class="RktVal"><span class="nobreak">-1</span></span><span class="stt"> </span><span class="RktVal">0</span><span class="RktPn">)</span>, and
<span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="stt"> </span><span class="RktVal">"design"</span><span class="stt"> </span><span class="RktVal">"tinker"</span><span class="RktPn">)</span>. This last one is different
again; but don&rsquo;t worry, you can do it.</div></p><p><div class="SIntrapara">With all these new kinds of data&#8212;<wbr></wbr>yes, numbers, strings, and
Boolean values are data&#8212;<wbr></wbr>and operations floating around, it is easy to
forget some basics, like nested arithmetic:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._and%29%29" class="RktStxLink" data-pltdoc="x">and</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._or%29%29" class="RktStxLink" data-pltdoc="x">or</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3d%29%29" class="RktValLink" data-pltdoc="x">=</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-length%29%29" class="RktValLink" data-pltdoc="x">string-length</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"hello world"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-~3enumber%29%29" class="RktValLink" data-pltdoc="x">string-&gt;number</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"11"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"hello world"</span><span class="hspace">&nbsp;</span><span class="RktVal">"good morning"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3e~3d%29%29" class="RktValLink" data-pltdoc="x">&gt;=</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-length%29%29" class="RktValLink" data-pltdoc="x">string-length</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"hello world"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">80</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">What is the result of this expression? How did you figure it out? All by
yourself? Or did you just type it into DrRacket&rsquo;s interactions area
and hit the &ldquo;return&rdquo; key? If you did the latter, do you think you would
know how to do this on your own? After all, if you can&rsquo;t predict what
DrRacket does for small expressions, you may not want to trust it when
you submit larger tasks than that for evaluation.</div></p><p><div class="SIntrapara">Before we show you how to do some &ldquo;real&rdquo; programming, let&rsquo;s discuss one
more kind of data to spice things up: images.<span class="refelem"><span class="refcolumn"><span class="refcontent">To insert
images such as this rocket into DrRacket, use the <span class="stt">Insert</span> menu. Or,
copy and paste the image from your browser into DrRacket.</span></span></span> When you
insert an image into the interactions area and hit &ldquo;return&rdquo; like this
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="stt">&gt; </span><img src="rocket.png" alt="" width="28" height="42"/></p></blockquote></div><div class="SIntrapara">DrRacket replies with the image. In contrast to many other programming
languages, BSL understands images, and it supports an arithmetic of
images just as it supports an arithmetic of numbers or strings. In short,
your programs can calculate with images, and you can do so in the
interactions area. Furthermore, BSL programmers&#8212;<wbr></wbr>like the programmers for
other programming languages&#8212;<wbr></wbr>create <span style="font-style: italic">libraries</span> that others may
find helpful. Using such libraries is just like expanding your
vocabularies with new words or your programming vocabulary with new
primitives. We dub such libraries <span style="font-style: italic">teachpacks</span> because they are
helpful with teaching.</div></p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p>Add <span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._require%29%29" class="RktStxLink" data-pltdoc="x">require</a></span><span class="stt"> </span><span class="RktSym">2htdp/image</span><span class="RktPn">)</span> to the <span style="font-weight: bold">definitions
area</span>, or select <span class="stt">Add Teachpack</span> from the <span class="stt">Language</span> menu and
choose <span class="stt">image</span> from the <span class="stt">Preinstalled HtDP/2e Teachpack</span> menu.</p></blockquote></blockquote></blockquote><p><div class="SIntrapara">One important teachpack&#8212;<wbr></wbr><span class="stt">2htdp/image</span>&#8212;<wbr></wbr>supports operations for computing
the width and height of an image:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-width%29%29" class="RktValLink" data-pltdoc="x">image-width</a></span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</a></span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Once your program includes the teachpack, clicking <span class="emph">RUN</span> gives
you
<span class="RktRes">1176</span>
because that&rsquo;s the area of a 28 by 42 image.</div></p><p><div class="SIntrapara">You don&rsquo;t have to use Google to find images and insert them in your DrRacket
programs with the &ldquo;Insert&rdquo; menu. You can also instruct DrRacket to
create simple images from scratch:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._circle%29%29" class="RktValLink" data-pltdoc="x">circle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_2.png" alt="image" width="27" height="27"/></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._rectangle%29%29" class="RktValLink" data-pltdoc="x">rectangle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">30</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">"outline"</span><span class="hspace">&nbsp;</span><span class="RktVal">"blue"</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_3.png" alt="image" width="37" height="27"/></p></td></tr></table></blockquote></div><div class="SIntrapara">When the result of an expression is an image, DrRacket draws it into the
interactions area. But otherwise, a BSL program deals with
images as data that is just like numbers. In particular, BSL has
operations for combining images in the same way that it has operations for adding
numbers or appending strings:
</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">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._overlay%29%29" class="RktValLink" data-pltdoc="x">overlay</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._circle%29%29" class="RktValLink" data-pltdoc="x">circle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._rectangle%29%29" class="RktValLink" data-pltdoc="x">rectangle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"blue"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_4.png" alt="image" width="27" height="27"/></p></td></tr></table></blockquote></div><div class="SIntrapara">Overlaying these images in the opposite order produces a solid blue
square:
</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">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._overlay%29%29" class="RktValLink" data-pltdoc="x">overlay</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._rectangle%29%29" class="RktValLink" data-pltdoc="x">rectangle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"blue"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._circle%29%29" class="RktValLink" data-pltdoc="x">circle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_5.png" alt="image" width="27" height="27"/></p></td></tr></table></blockquote></div><div class="SIntrapara">Stop and reflect on this last result for a moment.</div></p><p><div class="SIntrapara">As you can see, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._overlay%29%29" class="RktValLink" data-pltdoc="x">overlay</a></span> is more like <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span> than
<span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span>, but it does &ldquo;add&rdquo; images just like <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span>
&ldquo;adds&rdquo; strings and <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span> adds numbers. Here is another illustration
of the idea:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-width%29%29" class="RktValLink" data-pltdoc="x">image-width</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._square%29%29" class="RktValLink" data-pltdoc="x">square</a></span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">10</span></p></td></tr><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-width%29%29" class="RktValLink" data-pltdoc="x">image-width</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._overlay%29%29" class="RktValLink" data-pltdoc="x">overlay</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._rectangle%29%29" class="RktValLink" data-pltdoc="x">rectangle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"blue"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._circle%29%29" class="RktValLink" data-pltdoc="x">circle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p><span class="RktRes">20</span></p></td></tr></table></blockquote></div><div class="SIntrapara">These interactions with DrRacket don&rsquo;t draw anything at all; they really just
measure their width.</div></p><p><div class="SIntrapara">Two more operations matter: <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span> and
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span>. The first creates a scene, a special kind of
rectangle. The second places an image into such a scene:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._circle%29%29" class="RktValLink" data-pltdoc="x">circle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktVal">80</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">and you get this:<span class="refelem"><span class="refcolumn"><span class="refcontent">Not quite. The image comes without a
grid. We superimpose the grid on the empty scene so that you can see where
exactly the green dot is placed.</span></span></span></div></p><p><div class="SIntrapara"><blockquote class="SCodeFlow"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_6.png" alt="image" width="72.0" height="72.0"/></p></blockquote></div><div class="SIntrapara">As you can see from this image, the origin (or <span style="font-style: italic"></span>(<span style="font-style: italic"></span>0<span style="font-style: italic">,</span>0<span style="font-style: italic"></span>)<span style="font-style: italic"></span>) is in the upper-left
corner. Unlike in mathematics, the y-coordinate is
measured <span style="font-weight: bold">downward</span>, not upward. Otherwise, the image shows what
you should have expected: a solid green disk at the coordinates (50,80) in
a 100 by 100 empty rectangle.</div></p><p>Let&rsquo;s summarize again. To program is to write down an arithmetic
expression, but you&rsquo;re no longer restricted to boring numbers. In BSL,
arithmetic is the arithmetic of numbers, strings, Booleans, and
even images. To compute, though, still means to determine the value of an
expression&#8212;<wbr></wbr>except that this value can be a string, a number, a
Boolean, or an image.</p><p>And now you&rsquo;re ready to write programs that make rockets fly.</p><h4><a name="(part._some-i/o)"></a>Inputs and Output</h4><p>The programs you have written so far are pretty boring. You write down an
expression or several expressions; you click <span class="emph">RUN</span>; you see some
results. If you click <span class="emph">RUN</span> again, you see the exact same results. As
a matter of fact, you can click <span class="emph">RUN</span> as often as you want, and the
same results show up. In short, your programs really are like calculations
on a pocket calculator, except that DrRacket calculates with all kinds
of data, not just numbers.</p><p>That&rsquo;s good news and bad news. It is good because programming and computing
ought to be a natural generalization of using a calculator. It is bad
because the purpose of programming is to deal with lots of data and to get
lots of different results, with more or less the same calculations. (It
should also compute these results quickly, at least faster than we can.)
That is, you need to learn more still before you know how to program. No
need to worry though: with all your knowledge about arithmetic of numbers,
strings, Boolean values, and images, you&rsquo;re almost ready to write a program
that creates movies, not just some silly program for displaying &ldquo;hello
world&rdquo; somewhere. And that&rsquo;s what we&rsquo;re going to do next.</p><p>Just in case you didn&rsquo;t know, a movie is a sequence of images that are
rapidly displayed in order. If your algebra teachers had known about the
&ldquo;arithmetic of images&rdquo; that you saw in the preceding section, you could
have produced movies in algebra instead of boring number
sequences. Well, here is one more such table:</p><p><div class="SIntrapara"><blockquote><table cellspacing="0" cellpadding="0" style="border-collapse: collapse;"><tr><td align="left" style="border-bottom: 1px solid black;"><p>x = </p></td><td align="left" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" style="border-bottom: 1px solid black;"><p>1</p></td><td align="right" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" style="border-bottom: 1px solid black;"><p>2</p></td><td align="right" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" style="border-bottom: 1px solid black;"><p>3</p></td><td align="right" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" style="border-bottom: 1px solid black;"><p>4</p></td><td align="right" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" style="border-bottom: 1px solid black;"><p>5</p></td><td align="right" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" style="border-bottom: 1px solid black;"><p>6</p></td><td align="right" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" style="border-bottom: 1px solid black;"><p>7</p></td><td align="right" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" style="border-bottom: 1px solid black;"><p>8</p></td><td align="right" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" style="border-bottom: 1px solid black;"><p>9</p></td><td align="right" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" style="border-bottom: 1px solid black;"><p>10</p></td></tr><tr><td align="left" valign="top" colspan="21"><p></p></td></tr><tr><td align="left" valign="top"><p>y =</p></td><td align="left" valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" valign="top"><p>1</p></td><td align="right" valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" valign="top"><p>4</p></td><td align="right" valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" valign="top"><p>9</p></td><td align="right" valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" valign="top"><p>16</p></td><td align="right" valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" valign="top"><p>25</p></td><td align="right" valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" valign="top"><p>36</p></td><td align="right" valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" valign="top"><p>49</p></td><td align="right" valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" valign="top"><p>64</p></td><td align="right" valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" valign="top"><p>81</p></td><td align="right" valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="right" valign="top"><p>?</p></td></tr></table></blockquote></div><div class="SIntrapara">Your teachers would now ask you to fill in the blank, that is, replace the &ldquo;?&rdquo;
mark with a number.</div></p><p>It turns out that making a movie is no more complicated than completing a
table of numbers like that. Indeed, it is all about such tables:</p><p><div class="SIntrapara"><blockquote><table cellspacing="0" cellpadding="0" style="border-collapse: collapse;"><tr><td align="left" style="border-bottom: 1px solid black;"><p>x = </p></td><td align="left" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="center" style="border-bottom: 1px solid black;"><p>1</p></td><td align="center" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="center" style="border-bottom: 1px solid black;"><p>2</p></td><td align="center" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="center" style="border-bottom: 1px solid black;"><p>3</p></td><td align="center" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="center" style="border-bottom: 1px solid black;"><p>4</p></td></tr><tr><td align="left" valign="bottom" colspan="9"><p></p></td></tr><tr><td align="left" valign="bottom"><p>y =</p></td><td align="left" valign="bottom"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="center" valign="bottom"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_7.png" alt="image" width="56.0" height="56.0"/></p></td><td align="center" valign="bottom"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="center" valign="bottom"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_8.png" alt="image" width="56.0" height="56.0"/></p></td><td align="center" valign="bottom"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="center" valign="bottom"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_9.png" alt="image" width="56.0" height="56.0"/></p></td><td align="center" valign="bottom"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td align="center" valign="bottom"><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_10.png" alt="image" width="56.0" height="56.0"/></p></td></tr></table></blockquote></div><div class="SIntrapara">To be concrete, your teacher should ask you here to draw the fourth image,
the fifth, and the 1273rd one because a movie is just a lot of
images, some 20 or 30 of them per second. So you need some 1200 to 1800
of them to make one minute&rsquo;s worth of it.</div></p><p><div class="SIntrapara">You may also recall that your teacher not only asked for the fourth or
fifth number in some sequence but also for an expression that
determines any element of the sequence from a given <span style="font-style: italic">x</span>. In the
numeric example, the teacher wants to see something like
this:
</div><div class="SIntrapara"><blockquote class="SCentered"><p><img src="pict_11.png" alt="image" width="46" height="8"/></p></blockquote></div><div class="SIntrapara">If you plug in 1, 2, 3, and so on for <span style="font-style: italic">x</span>, you get 1, 4, 9, and so on for
<span style="font-style: italic">y</span>&#8212;<wbr></wbr>just as the table says. For the sequence of images, you could say
something like
</div><div class="SIntrapara"><blockquote class="SCentered"><p><span style="font-style: italic">y = </span> the image that contains a dot <span style="font-style: italic">x</span><span style="vertical-align: super; font-size: 80%"><span style="font-style: italic"></span>2<span style="font-style: italic"></span></span><span style="font-style: italic"></span> pixels below the top.</p></blockquote></div><div class="SIntrapara">The key is that these one-liners are not just expressions but functions.</div></p><p><div class="SIntrapara">At first glance, functions are like expressions, always with a <span style="font-style: italic">y</span> on the
left, followed by an <span style="font-style: italic">=</span> sign, and an expression. They aren&rsquo;t expressions,
however. And the notation you often see in school for functions is
utterly misleading. In DrRacket, you therefore write functions a bit
differently: <a name="(idx._(gentag._0))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">The <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span> says &ldquo;consider <span class="RktSym">y</span> a function,&rdquo; which, like
an expression, computes a value. A function&rsquo;s value, though, depends on
the value of something called the <span style="font-style: italic">input</span>, which we express with
<span class="RktPn">(</span><span class="RktSym">y</span><span class="stt"> </span><span class="RktSym">x</span><span class="RktPn">)</span>. Since we
don&rsquo;t know what this input is, we use a name to represent the
input. Following the mathematical tradition, we use <span class="RktSym">x</span> here
to stand in for the unknown input; but pretty soon, we will use all kinds
of names.</div></p><p>This second part means you must supply one number&#8212;<wbr></wbr>for <span class="RktSym">x</span>&#8212;<wbr></wbr>to
determine a specific value for <span class="RktSym">y</span>. When you do, DrRacket plugs the
value for <span class="RktSym">x</span> into the expression associated with the
function. Here the expression is <span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="stt"> </span><span class="RktSym">x</span><span class="stt"> </span><span class="RktSym">x</span><span class="RktPn">)</span>. Once <span class="RktSym">x</span> is
replaced with a value, say <span class="RktVal">1</span>, DrRacket can compute the result of the
expressions, which is also called the <span style="font-style: italic">output</span> of the function.</p><p><div class="SIntrapara">Click <span class="emph">RUN</span> and watch nothing happen. Nothing shows up in the
interactions area. Nothing seems to change anywhere else in
DrRacket. It is as if you hadn&rsquo;t accomplished anything. But you
did. You actually defined a function and informed DrRacket about its
existence. As a matter of fact, the latter is now ready for you to use the
function. Enter
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">at the prompt in the interactions area and watch a <span class="RktVal">1</span> appear in
response. The <span class="RktPn">(</span><span class="RktSym">y</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span> is called a <span style="font-style: italic">function application</span> in
DrRacket.<span class="refelem"><span class="refcolumn"><span class="refcontent">Mathematics also calls <span style="font-style: italic">y</span>(<span style="font-style: italic"></span>1<span style="font-style: italic"></span>)<span style="font-style: italic"></span> a function
application, but your teachers forgot to tell you.</span></span></span> Try
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">and see a <span class="RktVal">4</span> pop out. Of course, you can also enter all these
expressions in the definitions area and click <span class="emph">RUN</span>:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">In response, DrRacket displays:
<span class="RktRes">1<span class="stt"> </span>4<span class="stt"> </span>9<span class="stt"> </span>16<span class="stt"> </span>25</span>,
which are the numbers from the table. Now determine the missing entry.</div></p><p><div class="SIntrapara">What all this means for you is that functions provide a rather economic way
of computing lots of interesting values with a single expression. Indeed,
programs are functions; and once you understand functions well, you know
almost everything there is to know about programming. Given their importance,
let&rsquo;s recap what we know about functions so far:
</div><div class="SIntrapara"><ul><li><p><div class="SIntrapara">First,
</div><div class="SIntrapara"><blockquote class="SCentered"><p><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="stt"> </span><span class="RktPn">(</span><span class="RktVar">FunctionName</span><span class="stt"> </span><span class="RktVar">InputName</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktVar">BodyExpression</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">is a <a name="(idx._(gentag._1))"></a><span style="font-style: italic">function definition</span>. You recognize it as such because it
starts with the &ldquo;<span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span>&rdquo; keyword. It essentially
consists of three pieces: two names and an expression. The first name is
the name of the function; you need it to apply the function as often as
you wish. The second name&#8212;<wbr></wbr>called a
<span style="font-style: italic">parameter</span>&#8212;<wbr></wbr>represents the input of the function, which is
unknown until you apply the function. The expression, dubbed
<span style="font-style: italic">body</span>, computes the output of the function for a specific
input.</div></p></li><li><p><div class="SIntrapara">Second,
</div><div class="SIntrapara"><blockquote class="SCentered"><p><span class="RktPn">(</span><span class="RktVar">FunctionName</span><span class="stt"> </span><span class="RktVar">ArgumentExpression</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">is a <span style="font-style: italic">function application</span>. The first part tells DrRacket which
function you wish to use. The second part is the input to which you want
to apply the function. If you were reading a Windows or a Mac manual, it
might tell you that this expression &ldquo;launches&rdquo; the
&ldquo;application&rdquo; called <span class="RktVar">FunctionName</span> and that it is going to
process <span class="RktVar">ArgumentExpression</span> as the input. Like all expressions,
the latter is possibly a plain piece of data or a deeply nested expression. </div></p></li></ul></div></p><p><div class="SIntrapara">Functions can input more than numbers, and they can output all kinds of
data, too. Our next task is to create a function that simulates the
second table&#8212;<wbr></wbr>the one with images of a colored dot&#8212;<wbr></wbr>just like the first
function simulated the numeric table. Since the creation of images from
expressions isn&rsquo;t something you know from high school, let&rsquo;s start
simply. Do you remember <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span>? We quickly mentioned it at
the end of the previous section. When you type it into the interactions
area, like that:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_12.png" alt="image" width="107" height="67"/></p></td></tr></table></blockquote></div><div class="SIntrapara">DrRacket produces an empty rectangle, also called a scene.
You can add images to a scene with <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span>:</div></p><p><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_13.png" alt="image" width="34" height="48"/><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktVal">23</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_14.png" alt="image" width="107" height="67"/></p></td></tr></table></blockquote></div><div class="SIntrapara">Think of the rocket as an object that is like the dot in the above table
from your mathematics class. The difference is that a rocket is interesting.</div></p><p><div class="SIntrapara">Next, you should make the rocket descend, just like the dot in the above
table. From the preceding section, you know how to achieve this effect by
increasing the y-coordinate that is supplied to <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span>:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_15.png" alt="image" width="34" height="48"/><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_16.png" alt="image" width="107" height="67"/></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_17.png" alt="image" width="34" height="48"/><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktVal">30</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_18.png" alt="image" width="107" height="67"/></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_19.png" alt="image" width="34" height="48"/><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktVal">40</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_20.png" alt="image" width="107" height="67"/></p></td></tr></table></blockquote></div><div class="SIntrapara">All that&rsquo;s needed now is to produce lots of these scenes easily and to
display all of them in rapid order. <a name="(idx._(gentag._2))"></a></div></p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">picture-of-rocket</span><span class="hspace">&nbsp;</span><span class="RktSym">height</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktSym">height</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></blockquote><p class="Centertext"><span class="Legend"><span class="FigureTarget"><a name="(counter._(figure._fig~3apicture-of-rocket..v1))" x-target-lift="Figure"></a>Figure&nbsp;4: </span>Landing a rocket (version 1)</span></p></blockquote><p><div class="SIntrapara">The first goal can be achieved with a function, of course; see
<a href="part_prologue.html#%28counter._%28figure._fig~3apicture-of-rocket..v1%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">4</span></a>.<span class="refelem"><span class="refcolumn"><span class="refcontent">In BSL, you can use all kinds of characters in
names, including &ldquo;-&rdquo; and &ldquo;.&rdquo;.</span></span></span> Yes, this is a function
definition. Instead of <span class="RktSym">y</span>, it uses the name
<span class="RktSym">picture-of-rocket</span>, a name that immediately tells you what the
function outputs: a scene with a rocket. Instead of <span class="RktSym">x</span>, the
function definition uses <span class="RktSym">height</span> for the name of its parameter, a
name that suggests that it is a number and that it tells the function
where to place the rocket. The body expression of the function is exactly
like the series of expressions with which we just experimented, except
that it uses <span class="RktSym">height</span> in place of a number. And we can easily
create all of those images with this one function:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">picture-of-rocket</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">picture-of-rocket</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">picture-of-rocket</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">picture-of-rocket</span><span class="hspace">&nbsp;</span><span class="RktVal">30</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Try this out in the definitions area or the interactions
area; both create the expected scenes.</div></p><p><div class="SIntrapara">The second goal requires knowledge about a primitive
operation from another teachpack: <span class="stt">2htdp/universe</span>: <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span>.</div><div class="SIntrapara"><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p>Add <span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._require%29%29" class="RktStxLink" data-pltdoc="x">require</a></span><span class="stt"> </span><span class="RktSym">2htdp/universe</span><span class="RktPn">)</span> to the <span style="font-weight: bold">definitions
area</span>, or select <span class="stt">Add Teachpack</span> from the <span class="stt">Language</span> menu and
choose <span class="stt">universe</span> from the <span class="stt">Preinstalled HtDP/2e Teachpack</span> menu.</p></blockquote></blockquote></blockquote></div><div class="SIntrapara"> So, click <span class="emph">RUN</span> and
enter the following expression:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span><span class="hspace">&nbsp;</span><span class="RktSym">picture-of-rocket</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Stop and note that the argument expression is a function. Don&rsquo;t
worry for now about using functions as arguments; it works well with
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span>, but don&rsquo;t try to define functions like <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span>
at home just yet.</div></p><p>As soon as you hit the &ldquo;return&rdquo; key, DrRacket evaluates the expression; but
it does not display a result, not even a prompt. It opens
another window&#8212;<wbr></wbr>a <span style="font-style: italic">canvas</span>&#8212;<wbr></wbr>and starts a clock that ticks 28
times per second. Every time the clock ticks, DrRacket applies
<span class="RktSym">picture-of-rocket</span> to the number of ticks passed since this
function call. The results of these function calls are displayed in the
canvas, and it produces the effect of an animated movie. The simulation
runs until you close the window. At that point,
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span> returns the number of ticks that have passed.</p><p><div class="SIntrapara">The question is where the images on the window come from. The short
explanation is that <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span> runs its operand on the numbers
<span class="RktVal">0</span>, <span class="RktVal">1</span>, <span class="RktVal">2</span>, and so on,<span class="refelem"><span class="refcolumn"><span class="refcontent"><a href="part_three.html#%28counter._%28exercise._ex~3aanimate%29%29" data-pltdoc="x">Exercise&nbsp;298</a>
explains how to design <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span>.</span></span></span> and displays the resulting
images. The long explanation is this:
</div><div class="SIntrapara"><ul><li><p><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span> starts a clock and counts the number of ticks;</p></li><li><p>the clock ticks 28 times per second;</p></li><li><p>every time the clock ticks, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span> applies the function
<span class="RktSym">picture-of-rocket</span> to the current clock tick; and</p></li><li><p>the scene that this application creates is displayed on the canvas.</p></li></ul></div><div class="SIntrapara">This means that the rocket first appears at height <span class="RktVal">0</span>, then
<span class="RktVal">1</span>, then <span class="RktVal">2</span>, and so on, which explains why the rocket descends
from the top of the canvas to the bottom. That is, our three-line program
creates some 100 pictures in about 3.5 seconds, and displaying these
pictures rapidly creates the effect of a rocket descending to the ground.</div></p><p>So here is what you learned in this section. Functions are useful because they
can process lots of data in a short time. You can launch a function by
hand on a few select inputs to ensure that it produces the proper outputs. This
is called testing a function. Or, DrRacket can launch a function on lots of
inputs with the help of some teachpacks; when you do that, you are running
the function. Naturally, DrRacket can launch functions when you press a key
on your keyboard or when you manipulate the mouse of your computer. To
find out how, keep reading. Whatever triggers a function application isn&rsquo;t
important, but do keep in mind that (simple) programs are functions.</p><h4><a name="(part._pro-cond)"></a>Many Ways to Compute</h4><p>When you evaluate <span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span><span class="stt"> </span><span class="RktSym">picture-of-rocket</span><span class="RktPn">)</span>, the rocket
eventually disappears into the ground. <span class="refelem"><span class="refcolumn"><span class="refcontent">As Venkat
Pamulapati notes, SpaceX has landed many rockets vertically since
these words were written.</span></span></span> That&rsquo;s plain silly. Rockets in old
science fiction movies don&rsquo;t sink into the ground; they gracefully land on
their bottoms, and the movie should end right there.</p><p>This idea suggests that computations should proceed differently, depending
on the situation. In our example, the <span class="RktSym">picture-of-rocket</span> program
should work &ldquo;as is&rdquo; while the rocket is in flight. When the rocket&rsquo;s
bottom touches the bottom of the canvas, however, it should stop the
rocket from descending any farther.</p><p>In a sense, the idea shouldn&rsquo;t be new to you. Even your mathematics
teachers define functions that distinguish various situations:</p><p><div class="SIntrapara"><blockquote class="SCentered"><p><img src="pict_21.png" alt="image" width="132" height="45"/></p></blockquote></div><div class="SIntrapara">This <span style="font-style: italic">sign</span> function distinguishes three kinds of inputs: those
numbers that are larger than <span style="font-style: italic"></span>0<span style="font-style: italic"></span>, those equal to <span style="font-style: italic"></span>0<span style="font-style: italic"></span>, and those
smaller than <span style="font-style: italic"></span>0<span style="font-style: italic"></span>. Depending on the input, the result of the
function is +1, 0, or -1.</div></p><p><div class="SIntrapara">You can define this function in DrRacket without much ado using a
<span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._cond%29%29" class="RktStxLink" data-pltdoc="x">cond</a></span>itional expression: <a name="(idx._(gentag._3))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">sign</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._cond%29%29" class="RktStxLink" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3e%29%29" class="RktValLink" data-pltdoc="x">&gt;</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3d%29%29" class="RktValLink" data-pltdoc="x">=</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3c%29%29" class="RktValLink" data-pltdoc="x">&lt;</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span></span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">After you click <span class="emph">RUN</span>, you can interact with <span class="RktSym">sign</span> like any
other function:<span class="refelem"><span class="refcolumn"><span class="refcontent">Open a new tab in DrRacket and start with a clean slate.</span></span></span>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">sign</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">1</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">sign</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-5</span></span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">-1</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">sign</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">0</span></p></td></tr></table></blockquote></div></p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p>This is a good time to explore what the <span class="emph">STEP</span> button
does. Add <span class="RktPn">(</span><span class="RktSym">sign</span><span class="stt"> </span><span class="RktVal"><span class="nobreak">-5</span></span><span class="RktPn">)</span> to the definitions area and click
<span class="emph">STEP</span> for the above <span class="RktSym">sign</span> program. When the new window
comes up, click the right and left arrows there.</p></blockquote></blockquote></blockquote><p><div class="SIntrapara">In general, a <span style="font-style: italic">conditional expression</span> has the shape
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._cond%29%29" class="RktStxLink" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktVar">ConditionExpression1</span><span class="hspace">&nbsp;</span><span class="RktVar">ResultExpression1</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktVar">ConditionExpression2</span><span class="hspace">&nbsp;</span><span class="RktVar">ResultExpression2</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span>...</td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktVar">ConditionExpressionN</span><span class="hspace">&nbsp;</span><span class="RktVar">ResultExpressionN</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">That is, a <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._cond%29%29" class="RktStxLink" data-pltdoc="x">cond</a></span>itional expression consists of as many
<span style="font-style: italic">conditional lines</span> as needed. Each line contains two expressions:
the left one is often called <span style="font-style: italic">condition</span>, and the right one is
called <span style="font-style: italic">result</span>; occasionally we also use <span style="font-style: italic">question</span> and
<span style="font-style: italic">answer</span>. To evaluate a <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._cond%29%29" class="RktStxLink" data-pltdoc="x">cond</a></span> expression, DrRacket evaluates
the first condition expression, <span class="RktVar">ConditionExpression1</span>. If this
yields <span class="RktVal">#true</span>, DrRacket replaces the <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._cond%29%29" class="RktStxLink" data-pltdoc="x">cond</a></span> expression with
<span class="RktVar">ResultExpression1</span>, evaluates it, and uses the value as the
result of the entire <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._cond%29%29" class="RktStxLink" data-pltdoc="x">cond</a></span> expression. If the evaluation of
<span class="RktVar">ConditionExpression1</span> yields <span class="RktVal">#false</span>, DrRacket drops the
first line and starts over. In case all condition expressions evaluate to
<span class="RktVal">#false</span>, DrRacket signals an error. <a name="(idx._(gentag._4))"></a></div></p><blockquote class="Herefigure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">picture-of-rocket.v2</span><span class="hspace">&nbsp;</span><span class="RktSym">height</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._cond%29%29" class="RktStxLink" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktSym">height</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktSym">height</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3e%29%29" class="RktValLink" data-pltdoc="x">&gt;</a></span><span class="hspace">&nbsp;</span><span class="RktSym">height</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&
goal is to not let the rocket descend below the ground level of a
<span style="font-style: italic"></span>1<span style="font-style: italic"></span>0<span style="font-style: italic"></span>0<span style="font-style: italic"></span>-by-<span style="font-style: italic"></span>6<span style="font-style: italic"></span>0<span style="font-style: italic"></span> scene. Since the <span class="RktSym">picture-of-rocket</span> function consumes
the height where it should place the rocket in the scene, a simple test
comparing the given height to the maximum height appears to suffice.</p><p>See <a href="part_prologue.html#%28counter._%28figure._fig~3apicture-of-rocket..v2%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">5</span></a> for the revised function definition. The
definition uses the name <span class="RktSym">picture-of-rocket.v2</span> to distinguish
the two versions. Using distinct names also allows us to use both
functions in the interactions area and to compare the results. Here is how
the original version works:</p><p><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">picture-of-rocket</span><span class="hspace">&nbsp;</span><span class="RktVal">5555</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_22.png" alt="image" width="107" height="67"/></p></td></tr></table></blockquote></div><div class="SIntrapara">And here is the second one:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">picture-of-rocket.v2</span><span class="hspace">&nbsp;</span><span class="RktVal">5555</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_23.png" alt="image" width="107" height="67"/></p></td></tr></table></blockquote></div><div class="SIntrapara">No matter what number you give to <span class="RktSym">picture-of-rocket.v2</span>, if it
is over <span class="RktVal">60</span>, you get the same scene. In particular, when you run
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span><span class="hspace">&nbsp;</span><span class="RktSym">picture-of-rocket.v2</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara"> the rocket descends and sinks halfway into the ground before it stops.</div></p><p>Stop! What do you think we want to see?</p><p>Landing the rocket this far down is ugly. Then again, you know how to fix
this aspect of the program. As you have seen, BSL knows an arithmetic
of images. When <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span> adds an image to a scene, it uses its
center point as if it were the whole image, even though the image has a
real height and a real width. As you may recall, you can measure the
height of an image with the operation <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</a></span>. This function
comes in handy here because you really want to fly the rocket only until
its bottom touches the ground.</p><p><div class="SIntrapara">Putting one and one together you can now figure out that
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</a></span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">is the point at which you want the rocket to stop its descent. You
could figure this out by playing with the program directly, or you can
experiment in the interactions area with your image arithmetic.</div></p><p><div class="SIntrapara">Here is a first attempt:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</a></span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Now replace the third argument in the above application with
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</a></span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Stop! Conduct the experiments. Which result do you like better?
<a name="(idx._(gentag._5))"></a></div></p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">picture-of-rocket.v3</span><span class="hspace">&nbsp;</span><span class="RktSym">height</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._cond%29%29" class="RktStxLink" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktSym">height</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</a></span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktSym">height</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3e%29%29" class="RktValLink" data-pltdoc="x">&gt;</a></span><span class="hspace">&nbsp;</span><span class="RktSym">height</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href=
program in <a href="part_prologue.html#%28counter._%28figure._fig~3apicture-of-rocket..v3%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">6</span></a>.
Given some number, which represents the <span class="RktSym">height</span> of the rocket, it
first tests whether the rocket&rsquo;s bottom is above the ground. If it is, it
places the rocket into the scene as before. If it isn&rsquo;t, it places the
rocket&rsquo;s image so that its bottom touches the ground.</p><h4><a name="(part._pro-many-def)"></a>One Program, Many Definitions</h4><p>Now suppose your friends watch the animation but don&rsquo;t like the size of
your canvas. They might request a version that uses
<span class="RktVal">200</span>-by-<span class="RktVal">400</span> scenes. This simple request forces you to
replace <span class="RktVal">60</span> with <span class="RktVal">400</span> in five places in the program and
<span class="RktVal">100</span> with <span class="RktVal">200</span> in two other places&#8212;<wbr></wbr>not to speak
of the occurrences of <span class="RktVal">50</span>, which really means &ldquo;middle of the
canvas.&rdquo;</p><p>Stop! Before you read on, try to do just that so that you get an idea of
how difficult it is to execute this request for a five-line program. As
you read on, keep in mind that programs in the world consist of 50,000 or 500,000
or even 5,000,000 or more lines of program code.</p><p><div class="SIntrapara">In the ideal program, a small request, such as changing the sizes of the
canvas, should require an equally small change. The tool to achieve this
simplicity with BSL is <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span>. In addition to defining functions,
you can also introduce <span style="font-style: italic">constant definitions</span>, which assign some
name to a constant. The general shape of a constant definition is
straightforward:
</div><div class="SIntrapara"><blockquote class="SCentered"><p><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="stt"> </span><span class="RktVar">Name</span><span class="stt"> </span><span class="RktVar">Expression</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Thus, for example, if you write down
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">in your program, you are saying that <span class="RktSym">HEIGHT</span> always
represents the number <span class="RktVal">60</span>. The meaning of such a definition is
what you expect. Whenever DrRacket encounters <span class="RktSym">HEIGHT</span> during its
calculations, it uses <span class="RktVal">60</span> instead. <a name="(idx._(gentag._6))"></a></div></p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">picture-of-rocket.v4</span><span class="hspace">&nbsp;</span><span class="RktSym">h</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._cond%29%29" class="RktStxLink" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktSym">h</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET</span><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktSym">h</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktSym">WIDTH</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3e%29%29" class="RktValLink" data-pltdoc="x">&gt;</a></span><span class="hspace">&nbsp;</span><span class="RktSym">h</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beg
simple change and also names the image of the rocket. Copy the program
into DrRacket; and after clicking <span class="emph">RUN</span>, evaluate the following
interaction:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span><span class="hspace">&nbsp;</span><span class="RktSym">picture-of-rocket.v4</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Confirm that the program still functions as before.</div></p><p>The program in <a href="part_prologue.html#%28counter._%28figure._fig~3apicture-of-rocket..v4%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">7</span></a> consists of four definitions: one
function definition and three constant definitions. The numbers
<span class="RktVal">100</span> and <span class="RktVal">60</span> occur only twice&#8212;<wbr></wbr>once as the value of
<span class="RktSym">WIDTH</span> and once as the value of <span class="RktSym">HEIGHT</span>. You may also
have noticed that it uses <span class="RktSym">h</span> instead of <span class="RktSym">height</span> for the
function parameter of <span class="RktSym">picture-of-rocket.v4</span>. Strictly speaking,
this change isn&rsquo;t necessary because DrRacket doesn&rsquo;t confuse <span class="RktSym">height</span>
with <span class="RktSym">HEIGHT</span>, but we did it to avoid confusing <span style="font-weight: bold">you</span>.</p><p>When DrRacket evaluates <span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span><span class="stt"> </span><span class="RktSym">picture-of-rocket.v4</span><span class="RktPn">)</span>, it replaces
<span class="RktSym">HEIGHT</span> with <span class="RktVal">60</span>, <span class="RktSym">WIDTH</span> with <span class="RktVal">100</span>, and
<span class="RktSym">ROCKET</span> with the image every time it encounters these names. To
experience the joys of real programmers, change the <span class="RktVal">60</span> next to
<span class="RktSym">HEIGHT</span> into a <span class="RktVal">400</span> and click <span class="emph">RUN</span>. You see a
rocket descending and landing in a 100 by 400 scene. One small change did
it all.</p><p><div class="SIntrapara">In modern parlance, you have just experienced your first <span style="font-style: italic">program
refactoring</span>. Every time you reorganize your program to prepare yourself
for likely future change requests, you refactor your program. Put it on
your resume. It sounds good, and your future employer probably enjoys
reading such buzzwords, even if it doesn&rsquo;t make you a good programmer.
What a good programmer would never live with, however, is having a program
contain the same expression three times:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Every time your friends and colleagues read this program, they need to
understand what this expression computes, namely, the distance between the
top of the canvas and the center point of a rocket resting on the
ground. Every time DrRacket computes the value of the expressions, it
has to perform three steps: (1) determine the height of the
image; (2) divide it by <span class="RktVal">2</span>; and (3) subtract the result from
<span class="RktSym">HEIGHT</span>. And, every time, it comes up with the same number.</div></p><p><div class="SIntrapara">This observation calls for the introduction of one more definition:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET-CENTER-TO-TOP</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Now substitute <span class="RktSym">ROCKET-CENTER-TO-TOP</span> for the expression
<span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="stt"> </span><span class="RktSym">HEIGHT</span><span class="stt"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="stt"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</a></span><span class="stt"> </span><span class="RktSym">ROCKET</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span> in the rest of
the program. You may be wondering whether
this definition should be placed above or below the definition for
<span class="RktSym">HEIGHT</span>. More generally, you should be wondering whether the
ordering of definitions matters. The answer is that for constant
definitions, the order matters; and for function definitions, it
doesn&rsquo;t. As soon as DrRacket encounters a constant definition, it determines
the value of the expression and then associates the name with this
value. For example,
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="hspace">&nbsp;</span><span class="RktSym">CENTER</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">CENTER</span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">causes DrRacket to complain that &ldquo;<span class="RktSym">CENTER</span> is used before
its definition,&rdquo; when it encounters the definition for
<span class="RktSym">HEIGHT</span>. In contrast,
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">CENTER</span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="hspace">&nbsp;</span><span class="RktSym">CENTER</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">works as expected. First, DrRacket associates <span class="RktSym">CENTER</span> with
<span class="RktVal">100</span>. Second, it evaluates <span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="stt"> </span><span class="RktVal">2</span><span class="stt"> </span><span class="RktSym">CENTER</span><span class="RktPn">)</span>, which yields
<span class="RktVal">200</span>. Finally, DrRacket associates <span class="RktVal">200</span> with
<span class="RktSym">HEIGHT</span>.</div></p><p>While the order of constant definitions matters, it does not matter where
you place constant definitions relative to function definitions. Indeed,
if your program consists of many function definitions, their order doesn&rsquo;t
matter either, though it is good to introduce all constant definitions
first, followed by the definitions of functions in decreasing order of
importance. When you start writing your own multi-definition programs, you
will see why this ordering matters.<a name="(idx._(gentag._7))"></a></p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">constants </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">WIDTH</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktVal">100</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">MTSCN</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktSym">WIDTH</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET</span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET-CENTER-TO-TOP</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">functions</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href=
introduced with semicolons (&ldquo;;&rdquo;). While DrRacket ignores such comments,
people who read programs should not because comments are intended for
human readers. It is a &ldquo;back channel&rdquo; of communication between the
author of the program and all of its future readers to convey information
about the program.</span></span></span></p><p>Once you eliminate all repeated expressions, you get the program in
<a href="part_prologue.html#%28counter._%28figure._fig~3apicture-of-rocket..v5%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">8</span></a>. It consists of one function definition and five
constant definitions. Beyond the placement of the rocket&rsquo;s center, these
constant definitions also factor out the image itself as well as the
creation of the empty scene.</p><p><div class="SIntrapara">Before you read on, ponder the following changes to your program:
</div><div class="SIntrapara"><ul><li><p>How would you change the program to create a
<span class="RktVal">200</span>-by-<span class="RktVal">400</span> scene?</p></li><li><p>How would you change the program so that it depicts the landing of a
green UFO (unidentified flying object)? Drawing the UFO is easy:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._overlay%29%29" class="RktValLink" data-pltdoc="x">overlay</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._circle%29%29" class="RktValLink" data-pltdoc="x">circle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._rectangle%29%29" class="RktValLink" data-pltdoc="x">rectangle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">40</span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></li><li><p>How would you change the program so that the background is always
blue?</p></li><li><p>How would you change the program so that the rocket lands on a flat rock
bed that is 10 pixels higher than the bottom of the scene? Don&rsquo;t forget to
change the scenery, too.</p></li></ul></div><div class="SIntrapara">Better than pondering is doing. It&rsquo;s the only way to learn. So don&rsquo;t let
us stop you. Just do it.</div></p><p><span style="font-weight: bold">Magic Numbers</span> Take another look at
<span class="RktSym">picture-of-rocket.v5</span>. Because we eliminated all repeated
expressions, all but one number disappeared from this function
definition. In the world of programming, these numbers are called
<span style="font-style: italic">magic numbers</span>, and nobody likes them. Before you know it, you
forget what role the number plays and what changes are legitimate. It is
best to name such numbers in a definition.</p><p>Here we actually know that <span class="RktVal">50</span> is our choice for an
x-coordinate for the rocket. Even though <span class="RktVal">50</span> doesn&rsquo;t look
like much of an expression, it really is a repeated expression, too.
Thus, we have two reasons to eliminate <span class="RktVal">50</span> from the
function definition, and we leave it to you to do so.</p><h4><a name="(part._more-def)"></a>One More Definition</h4><p><div class="SIntrapara">Recall that <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span> actually applies its functions to the number
of clock ticks that have passed since it was first called. That is, the
argument to <span class="RktSym">picture-of-rocket</span> isn&rsquo;t a height but a time. Our
previous definitions of <span class="RktSym">picture-of-rocket</span> use the wrong name
for<span class="refelem"><span class="refcolumn"><span class="refcontent">Danger ahead! This section introduces one piece of
knowledge from physics. If physics scares you, skip it on a first reading;
programming doesn&rsquo;t require physics knowledge.</span></span></span> the argument of the
function; instead of <span class="RktSym">h</span>&#8212;<wbr></wbr>short for height&#8212;<wbr></wbr>it ought to use
<span class="RktSym">t</span> for time: <a name="(idx._(gentag._8))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">picture-of-rocket</span><span class="hspace">&nbsp;</span><span class="RktSym">t</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._cond%29%29" class="RktStxLink" data-pltdoc="x">cond</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktSym">t</span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET-CENTER-TO-TOP</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET</span><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktSym">t</span><span class="hspace">&nbsp;</span><span class="RktSym">MTSCN</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._~3e%29%29" class="RktValLink" data-pltdoc="x">&gt;</a></span><span class="hspace">&nbsp;</span><span class="RktSym">t</span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET-CENTER-TO-TOP</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._place-image%29%29" class="RktValLink" data-pltdoc="x">place-image</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET-CENTER-TO-TOP</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktSym">MTSCN</span><span class="RktPn">)</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara"> And this small change to the definition immediately clarifies that
this program uses time as if it were a distance. What a bad idea.</div></p><p>Even if you have never taken a physics course, you know that a time is not
a distance. So somehow our program worked by accident. Don&rsquo;t worry,
though; it is all easy to fix. All you need to know is a bit of rocket
science, which people like us call physics.</p><p><div class="SIntrapara">Physics?!? Well, perhaps you have already forgotten what you learned in
that course. Or perhaps you have never taken a course on physics because
you are way too young or gentle. No worries. This happens to the best
programmers all the time because they need to help people with problems in
music, economics, photography, nursing, and all kinds of other
disciplines. Obviously, not even programmers know everything. So they
look up what they need to know. Or they talk to the right kind of
people. And if you talk to a physicist, you will find out that the
distance traveled is proportional to the time:
</div><div class="SIntrapara"><blockquote class="SCentered"><p><img src="pict_24.png" alt="image" width="43" height="9"/></p></blockquote></div><div class="SIntrapara">That is, if the velocity of an object is <span style="font-style: italic">v</span>, then the object travels
<span style="font-style: italic">d</span> miles (or meters or pixels or whatever) in <span style="font-style: italic">t</span> seconds.</div></p><p><div class="SIntrapara">Of course, a teacher ought to show you a proper function definition:
</div><div class="SIntrapara"><blockquote class="SCentered"><p><img src="pict_25.png" alt="image" width="57" height="12"/></p></blockquote></div><div class="SIntrapara">because this tells everyone immediately that the computation of <span style="font-style: italic">d</span>
depends on <span style="font-style: italic">t</span> and that <span style="font-style: italic">v</span> is a constant. A programmer goes
even further and uses meaningful names for these one-letter abbreviations:
<a name="(idx._(gentag._9))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">V</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">distance</span><span class="hspace">&nbsp;</span><span class="RktSym">t</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktSym">V</span><span class="hspace">&nbsp;</span><span class="RktSym">t</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">This program fragment consists of two definitions: a function
<span class="RktSym">distance</span> that computes the distance traveled by an object
traveling at a constant velocity, and a constant <span class="RktSym">V</span> that describes the
velocity.</div></p><p>You might wonder why <span class="RktSym">V</span> is <span class="RktVal">3</span> here. There is no special
reason. We consider <span class="RktVal">3</span> pixels per clock tick a good velocity. You
may not. Play with this number and see what happens with the animation.
<a name="(idx._(gentag._10))"></a></p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">properties of the "world" and the descending rocket</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">WIDTH</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktVal">100</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktVal">60</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">V</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">X</span><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">graphical constants </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">MTSCN</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktSym">WIDTH</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET</span><span class="hspace">&nbsp;</span><img src="rocket.png" alt="" width="28" height="42"/><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ROCKET-CENTER-TO-TOP</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><
<span class="RktSym">t</span> with a height, the function can use <span class="RktPn">(</span><span class="RktSym">distance</span><span class="stt"> </span><span class="RktSym">t</span><span class="RktPn">)</span> to
calculate how far down the rocket is. The final program is displayed in
<a href="part_prologue.html#%28counter._%28figure._fig~3apicture-of-rocket..v6%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">9</span></a>. It consists of two function definitions:
<span class="RktSym">picture-of-rocket.v6</span> and <span class="RktSym">distance</span>. The remaining
constant definitions make the function definitions readable and
modifiable. As always, you can run this program with <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span>:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span><span class="hspace">&nbsp;</span><span class="RktSym">picture-of-rocket.v6</span><span class="RktPn">)</span></p></blockquote></div></p><p>In comparison to the previous versions of <span class="RktSym">picture-of-rocket</span>,
this one shows that a program may consist of several function definitions
that refer to each other. Then again, even the first version used
<span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span> and <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span>&#8212;<wbr></wbr>it&rsquo;s just that you think of those as built
into BSL.</p><p>As you become a true-blue programmer, you will find out that programs
consist of many function definitions and many constant definitions. You
will also see that functions refer to each other all the time. What you
really need to practice is to organize them so that you can read them
easily, even months after completion. After all, an older version of
you&#8212;<wbr></wbr>or someone else&#8212;<wbr></wbr>will want to make changes to these programs; and
if you cannot understand the program&rsquo;s organization,
you will have a difficult time with even the smallest task. Otherwise, you
mostly know what there is to know.</p><h4><a name="(part._program-now)"></a>You Are a Programmer Now</h4><p>The claim that you are a programmer may have come as a surprise to you at
the end of the preceding section, but it is true. You know all the
mechanics that there are to know about BSL. You know that programming uses
the arithmetic of numbers, strings, images, and whatever other data
your chosen programming languages support. You know that programs consist
of function and constant definitions. You know, because we have told you,
that in the end, it&rsquo;s all about organizing these definitions
properly. Last but not least, you know that DrRacket and the teachpacks
support lots of other functions and that DrRacket&rsquo;s HelpDesk explains what these
functions do.</p><p>You might think that you still don&rsquo;t know enough to write programs that
react to keystrokes, mouse clicks, and so on. As it turns out, you do. In
addition to the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._%28%28lib._2htdp%2Funiverse..rkt%29._animate%29%29" class="RktValLink" data-pltdoc="x">animate</a></span> function, <span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/universe</span></span> teachpack</span>
provides other functions that hook up your programs to the keyboard, the
mouse, the clock, and other moving parts in your computer. Indeed, it even
supports writing programs that connect your computer with anybody else&rsquo;s
computer around the world. So this isn&rsquo;t really a problem.</p><p>In short, you have seen almost all the mechanics of putting together
programs. If you read up on all the functions that are available, you can
write programs that play interesting computer games, run simulations, or
keep track of business accounts. The question is whether this really means
you are a programmer. Are you?</p><p><div class="SIntrapara"><table cellspacing="0" cellpadding="0"><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p></p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr></table></div><div class="SIntrapara">Stop! Don&rsquo;t turn the page yet. Think!
</div><div class="SIntrapara"><table cellspacing="0" cellpadding="0"><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p></p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr><tr><td><p>&#160;</p></td></tr></table></div></p><h4><a name="(part._sec~3anot)"></a>Not!</h4><p>When you look at the &ldquo;programming&rdquo; bookshelves in a random bookstore,
you will see loads of books that promise to turn you into a programmer on
the spot. Now that you have worked your way through some first examples,
however, you probably realize that this cannot possibly happen.</p><p>Acquiring the mechanical skills of programming&#8212;<wbr></wbr>learning to write
expressions that the computer understands, getting to know which functions
and teachpacks are available, and similar activities&#8212;<wbr></wbr>isn&rsquo;t helping you
all that much with <span style="font-weight: bold">real</span> programming. If it were, you could equally
well learn a foreign language by memorizing a thousand words from the
dictionary and a few rules from a grammar book.</p><p>Good programming is far more than the mechanics of acquiring a
language. Most importantly, it is about keeping in mind that programmers
create programs for other people to read them in the future. A good
program reflects the problem statements and its important concepts. It
comes with a concise self-description. Examples illustrate this
description and relate it back to the problem. The examples make sure that
the future reader knows why and how your code works. In short, good
programming is about solving problems systematically and conveying the
system within the code. Best of all, this approach to programming actually
makes programming accessible to everyone&#8212;<wbr></wbr>so it serves two masters at
once.</p><p>The rest of this book is all about these things; very little of the book&rsquo;s
content is actually about the mechanics of DrRacket, BSL, or
teachpacks. The book shows you how good programmers think about problems.
And, you will even learn that this way of solving problems applies to
other situations in life, such as the work of doctors, journalists, lawyers,
and engineers.</p><p>Oh, and by the way, the rest of the book uses a tone that is more
appropriate for a serious text than this Prologue. Enjoy!</p><p><span style="font-weight: bold">Note on What This Book Is <span style="font-style: italic">Not</span> About</span> Introductory books on
programming tend to contain lots of material about the authors&rsquo; favorite application
discipline: puzzles, mathematics, physics, music, and so on. Such material
is natural because programming is obviously useful in all these areas, but
it also distracts from the essential elements of programming. Hence, we
have made every attempt to minimize the use of knowledge from other areas
so that we can focus on what computer science can teach you about
computational problem solving.</p><div class="navsetbottom"><span class="navleft"><div class="nosearchform"></div>&nbsp;&nbsp;<span class="tocsettoggle">&nbsp;&nbsp;<a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright">&nbsp;&nbsp;<a href="part_preface.html" title="backward to &quot;Preface&quot;" data-pltdoc="x">&larr; prev</a>&nbsp;&nbsp;<a href="index.html" title="up to &quot;How to Design Programs, Second Edition&quot;" data-pltdoc="x">up</a>&nbsp;&nbsp;<a href="part_one.html" title="forward to &quot;I Fixed-Size Data&quot;" data-pltdoc="x">next &rarr;</a></span>&nbsp;</div></div></div><div id="contextindicator">&nbsp;</div></body></html>