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

3415 lines
1.3 MiB
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>I&nbsp;Fixed-Size Data</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="racket.css" title="default"/><link rel="stylesheet" type="text/css" href="figure.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="tocviewlink" 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="tocviewselflink" 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;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,&quot;tocview_1&quot;);">&#9658;</a></td><td>I&nbsp;</td><td><a href="part_one.html" class="tocviewselflink" data-pltdoc="x">Fixed-<wbr></wbr>Size Data</a></td></tr></table><div class="tocviewsublistbottom" style="display: none;" id="tocview_1"><table cellspacing="0" cellpadding="0"><tr><td align="right">1&nbsp;</td><td><a href="part_one.html#%28part._ch~3abasic-arithmetic%29" class="tocviewlink" data-pltdoc="x">Arithmetic</
operations on data. The first language always provides some forms of
atomic data; to represent the variety of information in the real world as
data, a programmer must learn to compose basic data and to describe such
compositions. Similarly, the second language provides some basic
operations on atomic data; it is the programmer&rsquo;s task to compose these
operations into programs that perform the desired computations. We use
<span style="font-style: italic">arithmetic</span> for the combination of these two parts of a
programming language because it generalizes what you know from grade
school.</p><p>This first part of the book (I) introduces the arithmetic of BSL, the
programming language used in the Prologue. From arithmetic, it is a short
step to your first simple programs, which you may know as
<span style="font-style: italic">functions</span> from mathematics. Before you know it, though, the
process of writing programs looks confusing, and you will long for a way
to organize your thoughts. We equate &ldquo;organizing thoughts&rdquo; with
<span class="emph">design</span>, and this first part of the book introduces you to a
systematic way of designing programs.</p><h3>1<tt>&nbsp;</tt><a name="(part._ch~3abasic-arithmetic)"></a>Arithmetic</h3><p><div class="SIntrapara">From <a href="part_prologue.html" data-pltdoc="x">Prologue: How to Program</a>, you know how to write down the kind of
<span style="font-style: italic">expression</span> you know from first grade in BSL notation:
</div><div class="SIntrapara"><ul><li><p>write &ldquo;<span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span>&rdquo;,<span class="refelem"><span class="refcolumn"><span class="refcontent">Scan this first chapter quickly, skip ahead
to the second one, and return here, when you encounter &ldquo;arithmetic&rdquo;
that you don&rsquo;t recognize.</span></span></span></p></li><li><p>write down the name of a primitive operation <span class="RktSym">op</span>,</p></li><li><p>write down the arguments, separated by some space, and</p></li><li><p>write down &ldquo;<span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span>&rdquo;.</p></li></ul></div><div class="SIntrapara">Just as a reminder, here is a primitive expression:
</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">2</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">It 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._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span>, the operation for adding two numbers, followed by two
arguments, which are plain numbers. But here is another example:
</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="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="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><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</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="RktPn">)</span></p></blockquote></div><div class="SIntrapara">This second example exploits two points in the above description that are open
to interpretation. First, primitive operations may consume more than two
arguments. Second, the arguments don&rsquo;t have to be numbers per se; they can
be expressions, too.</div></p><p><div class="SIntrapara">Evaluating expressions is also straightforward. First, BSL evaluates all
the arguments of a primitive operation. Second, it &ldquo;feeds&rdquo; the resulting
pieces of data to the operation, which produces a result. Thus,
</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">1</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span></td></tr><tr><td><span class="RktVal">3</span></td></tr></table></blockquote></div><div class="SIntrapara"><span class="refelem"><span class="refcolumn"><span class="refcontent">We use <span class="RktSym">==</span> to mean &ldquo;is equal to according to the
laws of computation.&rdquo;</span></span></span>
and
</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">1</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">1</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">1</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</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._%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><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</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._%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="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">2</span><span class="RktPn">)</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="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</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._%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">5</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="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span></td></tr><tr><td><span class="RktVal">18</span></td></tr></table></blockquote></div><div class="SIntrapara">These calculations should look familiar because they are the same kind of
calculations that you performed in mathematics classes. You may have
written down the steps in a different way; you may have never been taught
how to write down a sequence of calculation steps. Yet, BSL performs
calculations just like you do, and this should be a relief. It guarantees
that you understand what it does with primitive operations and primitive
data, so there is some hope that you can predict what your programs will
compute. Generally speaking, it is critical for a programmer to know how
the chosen language calculates because otherwise a program&rsquo;s computation
may harm the people who use them or on whose behalf the programs
calculate.</div></p><p>The rest of this chapter introduces four forms of <span style="font-style: italic">atomic data</span> of
BSL: numbers, strings, images, and Boolean values.<span class="refelem"><span class="refcolumn"><span class="refcontent">The
next volume, <span style="font-style: italic">How to Design Components</span>, will explain how to design atomic data.</span></span></span> We use the
word &ldquo;atomic&rdquo; here in analogy to physics. You cannot peek inside atomic
pieces of data, but you do have functions that combine several pieces of
atomic data into another one, retrieve &ldquo;properties&rdquo; of them,
also in terms of atomic data, and so on. The sections of this chapter
introduce some of these functions, also called <span style="font-style: italic">primitive
operations</span> or <span style="font-style: italic">pre-defined operations</span>. You can find others in
the documentation of BSL that comes with DrRacket.</p><h4>1.1<tt>&nbsp;</tt><a name="(part._sec~3aarith-num)"></a>The Arithmetic of Numbers</h4><p>Most people think &ldquo;numbers&rdquo; and &ldquo;operations on numbers&rdquo; when they hear
&ldquo;arithmetic.&rdquo; &ldquo;Operations on numbers&rdquo; means adding two numbers to
yield a third, subtracting one number from another, determining the
greatest common divisor of two numbers, and many more such things. If we
don&rsquo;t take arithmetic too literally, we may even include the sine of an
angle, rounding a real number to the closest integer, and so on.</p><p><div class="SIntrapara">The BSL language supports <a name="(tech._number)"></a><span style="font-style: italic">Number</span>s and arithmetic on
them. As discussed in the Prologue, an arithmetic operation such as
<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> is used like this:
</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">3</span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">that is, in <span style="font-style: italic">prefix notation</span> form. Here are some of the operations on
numbers that our language provides:
<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._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></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._%2A%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._%2F%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._abs%29%29" class="RktValLink" data-pltdoc="x">abs</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._add1%29%29" class="RktValLink" data-pltdoc="x">add1</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._ceiling%29%29" class="RktValLink" data-pltdoc="x">ceiling</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._denominator%29%29" class="RktValLink" data-pltdoc="x">denominator</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._exact-~3einexact%29%29" class="RktValLink" data-pltdoc="x">exact-&gt;inexact</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._expt%29%29" class="RktValLink" data-pltdoc="x">expt</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._floor%29%29" class="RktValLink" data-pltdoc="x">floor</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._gcd%29%29" class="RktValLink" data-pltdoc="x">gcd</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._log%29%29" class="RktValLink" data-pltdoc="x">log</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._max%29%29" class="RktValLink" data-pltdoc="x">max</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._numerator%29%29" class="RktValLink" data-pltdoc="x">numerator</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._quotient%29%29" class="RktValLink" data-pltdoc="x">quotient</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._random%29%29" class="RktValLink" data-pltdoc="x">random</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._remainder%29%29" class="RktValLink" data-pltdoc="x">remainder</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._sqr%29%29" class="RktValLink" data-pltdoc="x">sqr</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._tan%29%29" class="RktValLink" data-pltdoc="x">tan</a></span>.
We picked our way through the alphabet just to show the variety of
operations. Explore what they compute, and then find out how many more
there are.</div></p><p><div class="SIntrapara">If you need an operation on numbers that you know from your mathematics
courses, chances are that BSL knows about it, too. Guess its name and
experiment in the interactions area. Say you need to compute the <span style="font-style: italic">sin</span>
of some angle; try
</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._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></table></blockquote></div><div class="SIntrapara">and use it happily ever after. Or look in the HelpDesk.<span class="refelem"><span class="refcolumn"><span class="refcontent">You
might know <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._e%29%29" class="RktValLink" data-pltdoc="x">e</a></span> from calculus. It&rsquo;s a real number, close to 2.718,
called &ldquo;Euler&rsquo;s constant.&rdquo;</span></span></span> You will find there that in addition to
operations BSL also recognizes the names of some widely used numbers,
for example, <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> 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._e%29%29" class="RktValLink" data-pltdoc="x">e</a></span>.</div></p><p>When it comes to numbers, BSL programs may use
natural numbers,
integers,
rational numbers,
real numbers,
and
complex numbers.
We assume that you have heard of all but the last one. The last one may
have been mentioned in your high school class. If not, don&rsquo;t worry; while
complex numbers are useful for all kinds of calculations, a novice doesn&rsquo;t
have to know about them.</p><p>A truly important distinction concerns the precision of numbers. For now,
it is important to understand that BSL distinguishes <span style="font-style: italic">exact
numbers</span> and <span style="font-style: italic">inexact numbers</span>. When it calculates with exact
numbers, BSL preserves this precision whenever possible. For example,
<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="RktVal">4</span><span class="stt"> </span><span class="RktVal">6</span><span class="RktPn">)</span> produces the precise fraction <span class="RktVal">2/3</span>, which
DrRacket can render as a proper fraction, an improper fraction, or a
mixed decimal. Play with your computer&rsquo;s mouse to find the menu that
changes the fraction into decimal expansion.</p><p>Some of BSL&rsquo;s numeric operations cannot produce an exact result. For
example, using the <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._sqrt%29%29" class="RktValLink" data-pltdoc="x">sqrt</a></span> operation on <span class="RktVal">2</span> produces an
irrational number that cannot be described with a finite number of
digits. Because computers are of finite size and BSL must somehow fit such
numbers into the computer, it chooses an approximation:
<span class="RktRes">1.4142135623730951</span>. As mentioned in the Prologue, the
<span class="stt">#i</span> prefix warns novice programmers of this lack of
precision. While most programming languages choose to reduce precision in
this manner, few advertise it and even fewer warn programmers.</p><p><span style="font-weight: bold">Note on Numbers</span> The word &ldquo;<a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a>&rdquo; refers to a wide variety of
numbers, including counting numbers, integers, rational numbers, real
numbers, and even complex numbers. For most uses, you can safely equate
<a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a> with the number line from elementary school, though on
occasion this translation is too imprecise. If we wish to be precise, we
use appropriate words: <a name="(tech._integer)"></a><span style="font-style: italic">Integer</span>, <a name="(tech._rational)"></a><span style="font-style: italic">Rational</span>, and so
on. We may even refine these notions using such standard terms as
<a name="(tech._positiveinteger)"></a><span style="font-style: italic">PositiveInteger</span>, <a name="(tech._nonnegativenumber)"></a><span style="font-style: italic">NonnegativeNumber</span>,
<a name="(tech._negativenumber)"></a><span style="font-style: italic">NegativeNumber</span>, and so on. <span style="font-weight: bold">End</span></p><p><div class="SIntrapara"><a name="(counter._(exercise._arith-n))"></a><span style="font-weight: bold">Exercise</span>&nbsp;1. Add the following definitions for <span class="RktSym">x</span> and <span class="RktSym">y</span> to
DrRacket&rsquo;s definitions area:
</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">x</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">y</span><span class="hspace">&nbsp;</span><span class="RktVal">4</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Now imagine that <span class="RktSym">x</span> and <span class="RktSym">y</span> are the coordinates of a
Cartesian point. Write down an expression that computes the distance of
this point to the origin, that is, a point with the coordinates
(<span class="RktVal">0</span>,<span class="RktVal">0</span>).</div></p><p>The expected result for these values is <span class="RktVal">5</span>, but your expression
should produce the correct result even after you change these definitions.</p><p><div class="SIntrapara">Just in case you have not taken geometry courses or in case you forgot the
formula that you encountered there, the point <span style="font-style: italic"></span>(<span style="font-style: italic">x,y</span>)<span style="font-style: italic"></span> has the
distance
</div><div class="SIntrapara"><blockquote class="SCentered"><p><img src="pict_26.png" alt="image" width="52" height="15"/></p></blockquote></div><div class="SIntrapara">from the origin. After all, we are teaching you how to design programs, not
how to be a geometer.</div></p><p><div class="SIntrapara">To develop the desired expression, it is best to click <span class="emph">RUN</span> and to
experiment in the interactions area. The <span class="emph">RUN</span> action tells DrRacket
what the current values of <span class="RktSym">x</span> and <span class="RktSym">y</span> are so that you can
experiment with expressions that involve <span class="RktSym">x</span> and <span class="RktSym">y</span>:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">&gt; </span><span class="RktSym">x</span></td></tr><tr><td><p><span class="RktRes">3</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktSym">y</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._%2B%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">10</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">13</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="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">12</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Once you have the expression that produces the correct result, copy it from
the interactions area to the definitions area.</div></p><p>To confirm that the expression works properly, change <span class="RktSym">x</span> to
<span class="RktVal">12</span> and <span class="RktSym">y</span> to <span class="RktVal">5</span>, then click <span class="emph">RUN</span>.
The result should be <span class="RktVal">13</span>.</p><p>Your mathematics teacher would say that you computed the <span style="font-weight: bold">distance
formula</span>. To use the formula on alternative inputs, you need to open
DrRacket, edit the definitions of <span class="RktSym">x</span> and <span class="RktSym">y</span> so they represent
the desired coordinates, and click <span class="emph">RUN</span>. But this way of reusing
the distance formula is cumbersome and naive. We will soon show
you a way to define functions, which makes reusing formulas
straightforward. For now, we use this kind of exercise to call attention
to the idea of functions and to prepare you for programming with them. <a href="part_one.html#%28counter._%28exercise._arith-n%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>1.2<tt>&nbsp;</tt><a name="(part._sec~3aarith-str)"></a>The Arithmetic of Strings</h4><p>A widespread prejudice about computers concerns their innards. Many believe
that it is all about bits and bytes&#8212;<wbr></wbr>whatever those are&#8212;<wbr></wbr>and possibly
numbers because everyone knows that computers can calculate. While it is
true that electrical engineers must understand and study the computer as
just such an object, beginning programmers and everyone else need never
(ever) succumb to this thinking.</p><p>Programming languages are about computing with information, and
information comes in all shapes and forms. For example, a program may deal
with colors, names, business letters, or conversations between
people. Even though we could encode this kind of information as numbers,
it would be a horrible idea. Just imagine remembering large tables of
codes, such as <span class="RktVal">0</span> means &ldquo;red&rdquo; and <span class="RktVal">1</span> means &ldquo;hello,&rdquo;
and the like.</p><p>Instead, most programming languages provide at least one kind of data that
deals with such symbolic information. For now, we use BSL&rsquo;s
strings. Generally speaking, a <a name="(tech._string)"></a><span style="font-style: italic">String</span> is a sequence of the
characters that you can enter on the keyboard, plus a few others, about
which we aren&rsquo;t concerned just yet, enclosed in double quotes.
In
<a href="part_prologue.html" data-pltdoc="x">Prologue: How to Program</a>, we have seen a number of BSL strings: <span class="RktVal">"hello"</span>,
<span class="RktVal">"world"</span>, <span class="RktVal">"blue"</span>, <span class="RktVal">"red"</span>, and others. The first two are
words that may show up in a conversation or in a letter; the others are
names of colors that we may wish to use.</p><p><span style="font-weight: bold">Note</span> We use <span style="font-style: italic">1String</span> to refer to the keyboard characters
that make up a <a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a>. For example, <span class="RktVal">"red"</span> consists of
three such <a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a>s: <span class="RktVal">"r"</span>, <span class="RktVal">"e"</span>, <span class="RktVal">"d"</span>. As
it turns out, there is a bit more to the definition of <a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a>, but
for now thinking of them as <a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a>s of length <span style="font-style: italic"></span>1<span style="font-style: italic"></span> is
fine. <span style="font-weight: bold">End</span></p><p><div class="SIntrapara">BSL includes only one operation that exclusively consumes and produces
strings: <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>, which, as we have seen in
<a href="part_prologue.html" data-pltdoc="x">Prologue: How to Program</a>, concatenates two given strings into one. Think of
<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> as an operation that is 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>. While
the latter consumes two (or more) numbers and produces a new number, the
former consumes two or more strings and produces a new string:
</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">"what a "</span><span class="hspace">&nbsp;</span><span class="RktVal">"lovely "</span><span class="hspace">&nbsp;</span><span class="RktVal">"day"</span><span class="hspace">&nbsp;</span><span class="RktVal">" 4 BSL"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"what a lovely day 4 BSL"</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Nothing about the given numbers changes when <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 them up, and
nothing about the given strings changes when <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>
concatenates them into one big string. If you wish to evaluate such
expressions, you just need to think that the obvious laws hold for
<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>, similar to those for <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>:
</div><div class="SIntrapara"><blockquote><table cellspacing="0" cellpadding="0"><tr><td><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="stt"> </span><span class="RktVal">1</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">==</span><span class="stt"> </span><span class="RktVal">2</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td><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="stt"> </span><span class="RktVal">"a"</span><span class="stt"> </span><span class="RktVal">"b"</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">==</span><span class="stt"> </span><span class="RktVal">"ab"</span></p></td></tr><tr><td><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="stt"> </span><span class="RktVal">1</span><span class="stt"> </span><span class="RktVal">2</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">==</span><span class="stt"> </span><span class="RktVal">3</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td><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="stt"> </span><span class="RktVal">"ab"</span><span class="stt"> </span><span class="RktVal">"c"</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">==</span><span class="stt"> </span><span class="RktVal">"abc"</span></p></td></tr><tr><td><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="stt"> </span><span class="RktVal">2</span><span class="stt"> </span><span class="RktVal">2</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">==</span><span class="stt"> </span><span class="RktVal">4</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td><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="stt"> </span><span class="RktVal">"a"</span><span class="stt"> </span><span class="RktVal">" "</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">==</span><span class="stt"> </span><span class="RktVal">"a "</span></p></td></tr><tr><td><p>...</p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td><p>...</p></td></tr></table></blockquote></div></p><p><div class="SIntrapara"><a name="(counter._(exercise._arith-s0))"></a><span style="font-weight: bold">Exercise</span>&nbsp;2. Add the following two lines to the definitions area:
</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">prefix</span><span class="hspace">&nbsp;</span><span class="RktVal">"hello"</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">suffix</span><span class="hspace">&nbsp;</span><span class="RktVal">"world"</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Then use string primitives to create an expression that concatenates
<span class="RktSym">prefix</span> and <span class="RktSym">suffix</span> and adds <span class="RktVal">"_"</span> between them.
When you run this program, you will see <span class="RktVal">"hello_world"</span> in the
interactions area.</div></p><p>See <a href="part_one.html#%28counter._%28exercise._arith-n%29%29" data-pltdoc="x">exercise&nbsp;1</a> for how to create expressions using DrRacket. <a href="part_one.html#%28counter._%28exercise._arith-s0%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>1.3<tt>&nbsp;</tt><a name="(part._sec~3aarith-mix)"></a>Mixing It Up</h4><p><div class="SIntrapara">All other operations (in BSL) concerning strings consume or produce data
other than strings. Here are some examples:
</div><div class="SIntrapara"><ul><li><p><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> consumes a string and produces a number;</p></li><li><p><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-ith%29%29" class="RktValLink" data-pltdoc="x">string-ith</a></span> consumes a string <span class="RktSym">s</span> together with a
number <span class="RktSym">i</span> and extracts the <a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a>
located at the <span class="RktSym">i</span>th position (counting from 0); and</p></li><li><p><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> consumes a number and produces a string.</p></li></ul></div><div class="SIntrapara">Also look up <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._substring%29%29" class="RktValLink" data-pltdoc="x">substring</a></span> and find out what it does.</div></p><p><div class="SIntrapara">If the documentation in HelpDesk appears confusing, experiment with the
functions in the interactions area. Give them appropriate arguments, and find
out what they compute. Also use <span style="font-weight: bold">inappropriate</span> arguments for some
operations just to find out how BSL reacts:
</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-length%29%29" class="RktValLink" data-pltdoc="x">string-length</a></span><span class="hspace">&nbsp;</span><span class="RktVal">42</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">string-length:expects a string, given 42</span></p></td></tr></table></blockquote></div><div class="SIntrapara">As you can see, BSL reports an error. The first part &ldquo;string-length&rdquo;
informs you about the operation that is misapplied; the second half states
what is wrong with the arguments. In this specific example,
<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> is supposed to be applied to a string but is given
a number, specifically <span class="RktVal">42</span>.</div></p><p><div class="SIntrapara">Naturally, it is possible to nest operations that consume and produce
different kinds of data <span style="font-weight: bold">as long as you keep track of what is proper
and what is not</span>. Consider this expression from the <a href="part_prologue.html" data-pltdoc="x">Prologue: How to Program</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#%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></p></blockquote></div><div class="SIntrapara">The inner expression applies <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> to
<span class="RktVal">"hello world"</span>,
our favorite string. The outer expression has <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>
consume the result of the inner expression and <span class="RktVal">20</span>.</div></p><p><div class="SIntrapara">Let&rsquo;s determine the result of this expression in a step-by-step fashion:
</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="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><span class="RktSym">==</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._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">11</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span></td></tr><tr><td><span class="RktVal">31</span></td></tr></table></blockquote></div><div class="SIntrapara">Not surprisingly, computing with such nested expressions that deal with
a mix of data is no different from computing with numeric
expressions. Here is another 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#%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="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><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</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._%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">"42"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</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._%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="RktSym">==</span></td></tr><tr><td><span class="RktVal">4</span></td></tr></table></blockquote></div><div class="SIntrapara">Before you go on, construct some nested expressions that mix data in the
<span style="font-weight: bold">wrong</span> way, say,
</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="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">42</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Run them in DrRacket. Study the red error message but
also watch what DrRacket highlights in the definitions area.</div></p><p><div class="SIntrapara"><a name="(counter._(exercise._arith-s1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;3. Add the following two lines to the definitions area:
</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">str</span><span class="hspace">&nbsp;</span><span class="RktVal">"helloworld"</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">i</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Then create an expression using string primitives that adds <span class="RktVal">"_"</span> at
position <span class="RktSym">i</span>. In general this means the resulting string is longer
than the original one; here the expected result is <span class="RktVal">"hello_world"</span>.</div></p><p><div class="SIntrapara"><span style="font-style: italic">Position</span> means <span style="font-style: italic">i</span> characters from the left of the
string, but programmers start counting at <span class="RktVal">0</span>. Thus, the
5<span style="font-style: italic">th</span> letter in this example is <span class="RktVal">"w"</span>, because the
<span class="RktVal">0</span>th letter is <span class="RktVal">"h"</span>. <span style="font-weight: bold">Hint</span> When you encounter such
&ldquo;counting problems&rdquo; you may wish to add a string of digits below
<span class="RktSym">str</span> to help with counting:
</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">str</span><span class="hspace">&nbsp;</span><span class="RktVal">"helloworld"</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">ind</span><span class="hspace">&nbsp;</span><span class="RktVal">"0123456789"</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">i</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p>See <a href="part_one.html#%28counter._%28exercise._arith-n%29%29" data-pltdoc="x">exercise&nbsp;1</a> for how to create expressions in DrRacket. <a href="part_one.html#%28counter._%28exercise._arith-s1%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._arith-s2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;4. Use the same setup as in <a href="part_one.html#%28counter._%28exercise._arith-s1%29%29" data-pltdoc="x">exercise&nbsp;3</a> to
create an expression that deletes the <span style="font-style: italic">i</span>th position from
<span class="RktSym">str</span>. Clearly this expression creates a shorter string than the
given one. Which values for <span class="RktSym">i</span> are legitimate? <a href="part_one.html#%28counter._%28exercise._arith-s2%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>1.4<tt>&nbsp;</tt><a name="(part._sec~3aarith-images)"></a>The Arithmetic of Images</h4><p>An <a name="(tech._image)"></a><span style="font-style: italic">Image</span> is a visual, rectangular piece of data, for
example, a photo or a geometric figure and its frame.<span class="refelem"><span class="refcolumn"><span class="refcontent">Remember to require
<span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/image</span></span> teachpack</span> in a new tab.</span></span></span> You can insert images in DrRacket
wherever you can write down an expression because images are values, just
like numbers and strings.</p><p><div class="SIntrapara">Your programs can also manipulate images with primitive operations. These
primitive operations come in three flavors. The first kind concerns
the creation of basic images:
</div><div class="SIntrapara"><ul><li><p><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> produces a circle image from a radius, a mode string,
and a color string; </p></li><li><p><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._ellipse%29%29" class="RktValLink" data-pltdoc="x">ellipse</a></span> produces an ellipse from two diameters, a mode string,
and a color string; </p></li><li><p><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._line%29%29" class="RktValLink" data-pltdoc="x">line</a></span> produces a line from two points and a color string; </p></li><li><p><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> produces a rectangle from a width, a height, a
mode string, and a color string; </p></li><li><p><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span> produces a text image from a string, a font size, and a
color string; and </p></li><li><p><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._triangle%29%29" class="RktValLink" data-pltdoc="x">triangle</a></span> produces an upward-pointing equilateral triangle
from a size, a mode string, and a color string.</p></li></ul></div><div class="SIntrapara">The names of these operations mostly explain what kind of image they create. All you
must know is that <span style="font-style: italic">mode strings</span> means <span class="RktVal">"solid"</span> or
<span class="RktVal">"outline"</span>, and <span style="font-style: italic">color strings</span> are strings such as
<span class="RktVal">"orange"</span>, <span class="RktVal">"black"</span>, and so on.</div></p><p><div class="SIntrapara">Play with these operations in the interactions window:
</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">"green"</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_27.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">10</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><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_28.png" alt="image" width="17" 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._star%29%29" class="RktValLink" data-pltdoc="x">star</a></span><span class="hspace">&nbsp;</span><span class="RktVal">12</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"gray"</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_29.png" alt="image" width="26" height="25"/></p></td></tr></table></blockquote></div><div class="SIntrapara">Stop! The above uses a previously unmentioned operation. Look up its
documentation and find out how many more such operations <span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/image</span></span> teachpack</span>
comes with. Experiment with the operations you find.</div></p><p><div class="SIntrapara">The second kind of functions on images concern image properties:
</div><div class="SIntrapara"><ul><li><p><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> determines the width of an image in terms
of pixels;</p></li><li><p><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> determines the height of an image;</p></li></ul></div><div class="SIntrapara">They extract the kind of values from images that you expect:
</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._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><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">20</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/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="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">10</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><tr><td><p><span class="RktRes">20</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Stop! Explain how DrRacket determines the value of this expression:
</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="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._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><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&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._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</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">10</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><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p>A proper understanding of the third kind of image-composing primitives
requires the introduction of one new idea: the <span style="font-style: italic">anchor point</span>. An
image isn&rsquo;t just a single pixel, it consists of many pixels. Specifically,
each image is like a photograph, that is, a rectangle of pixels. One of
these pixels is an implicit anchor point. When you use an image primitive
to compose two images, the composition happens with respect to the anchor
points, unless you specify some other point explicitly:</p><p><div class="SIntrapara"><ul><li><p><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> places all the images to which it is applied on top
of each other, using the center as anchor point;</p></li><li><p><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._overlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">overlay/xy</a></span> is like <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> but accepts two
numbers&#8212;<wbr></wbr><span class="RktSym">x</span> and <span class="RktSym">y</span>&#8212;<wbr></wbr>between two image arguments. It
shifts the second image by <span class="RktSym">x</span> pixels to the right and <span class="RktSym">y</span>
pixels down&#8212;<wbr></wbr>all with respect to the first image&rsquo;s top-left corner; unsurprisingly, a negative
<span class="RktSym">x</span> shifts the image to the left and a negative <span class="RktSym">y</span> up; and</p></li><li><p><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._overlay%2Falign%29%29" class="RktValLink" data-pltdoc="x">overlay/align</a></span> is like <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> but accepts two
strings that shift the anchor point(s) to other parts of the
rectangles. There are nine different positions overall; experiment with
all possibilities!</p></li></ul></div><div class="SIntrapara"><span class="sroman">The <span class="Smaller"><span style="font-style: italic">2htdp/image</span></span> teachpack</span> comes with many other primitive functions
for combining images. As you get familiar with image processing, you will
want to read up on those. For now, we introduce three more because they
are important for creating animated scenes and images for games:
</div><div class="SIntrapara"><ul><li><p><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> creates a rectangle of some given width
and height;</p></li><li><p><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> places an image into a scene at a specified
position. If the image doesn&rsquo;t fit into the given scene, it is
appropriately cropped;</p></li><li><p><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._scene%2Bline%29%29" class="RktValLink" data-pltdoc="x">scene+line</a></span> consumes a scene, four numbers, and a color to
draw a line into the given image. Experiment with it to see how it works.</p></li></ul></div></p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><table cellspacing="0" cellpadding="0" style="border-collapse: collapse;"><tr><td style="border-bottom: 1px solid black;"><p>arithmetic of numbers</p></td><td style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td style="border-bottom: 1px solid black;"><p>arithmetic of images</p></td></tr><tr><td valign="top"><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="stt"> </span><span class="RktVal">1</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">==</span><span class="stt"> </span><span class="RktVal">2</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td valign="top"><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._square%29%29" class="RktValLink" data-pltdoc="x">square</a></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">"orange"</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._circle%29%29" class="RktValLink" data-pltdoc="x">circle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">6</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span></td></tr><tr><td><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_30.png" alt="image" width="18" height="18"/></td></tr></table></td></tr><tr><td valign="top"><p></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td valign="top"><p></p></td></tr><tr><td valign="top"><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="stt"> </span><span class="RktVal">1</span><span class="stt"> </span><span class="RktVal">2</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">==</span><span class="stt"> </span><span class="RktVal">3</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td valign="top"><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._underlay%29%29" class="RktValLink" data-pltdoc="x">underlay</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">6</span><span class="hspace"
<a href="part_one.html#%28counter._%28figure._fig~3alaws-of-images%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">10</span></a> for some examples and a comparison with
numeric arithmetic. Again, no image gets destroyed or changed. 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>, these primitives just make up new images that combine
the given ones in some manner.</p><p><a name="(counter._(exercise._arith-i2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;5. Use <span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/image</span></span> teachpack</span> to create the image of a simple
boat or tree. Make sure you can easily change the scale of the
entire image. <a href="part_one.html#%28counter._%28exercise._arith-i2%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara"><a name="(counter._(exercise._arith-i1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;6. Add the following line to the definitions
area:<span class="refelem"><span class="refcolumn"><span class="refcontent">Copy and paste the image into your DrRacket.</span></span></span>
</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">cat</span><span class="hspace">&nbsp;</span><img src="cat1.png" alt="" width="75" height="117"/><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Create an expression that counts the number of pixels in the image. <a href="part_one.html#%28counter._%28exercise._arith-i1%29%29" class="ex-end" data-pltdoc="x"></a></div></p><h4>1.5<tt>&nbsp;</tt><a name="(part._sec~3aarith-bools)"></a>The Arithmetic of Booleans</h4><p>We need one last kind of primitive data before we can design programs:
Boolean values. There are only two kinds of <a name="(tech._boolean)"></a><span style="font-style: italic">Boolean</span> values:
<span class="RktVal">#true</span> and <span class="RktVal">#false</span>. Programs use Boolean values for
representing decisions or the status of switches.</p><p><div class="SIntrapara">Computing with Boolean values is simple, too. In particular, BSL programs get
away with three operations: <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="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>, 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>. These operations are kind of like addition, multiplication,
and negation for numbers. Of course, because there are only two Boolean values,
it is actually possible to demonstrate how these functions work in
<span style="font-weight: bold">all</span> possible situations:
</div><div class="SIntrapara"><ul><li><p><div class="SIntrapara"><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> checks whether <span style="font-weight: bold">any</span> of the given Boolean values is <span class="RktVal">#true</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/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">#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._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">#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._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></table></blockquote></div></p></li><li><p><div class="SIntrapara"><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> checks whether <span style="font-weight: bold">all</span> of the given Boolean values are <span class="RktVal">#true</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/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._and%29%29" class="RktStxLink" data-pltdoc="x">and</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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._and%29%29" class="RktStxLink" data-pltdoc="x">and</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></table></blockquote></div></p></li><li><p><div class="SIntrapara">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> always picks the Boolean that isn&rsquo;t given:
</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._not%29%29" class="RktValLink" data-pltdoc="x">not</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#true</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#false</span></p></td></tr></table></blockquote></div></p></li></ul></div><div class="SIntrapara">Unsurprisingly, <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#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._and%29%29" class="RktStxLink" data-pltdoc="x">and</a></span> may be used with more than
two expressions. Finally, there is more to <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#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._and%29%29" class="RktStxLink" data-pltdoc="x">and</a></span>
than these explanations suggest, but to explain the extra bit requires a
second look at nested expressions.</div></p><p><a name="(counter._(exercise._arith-b1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;7. Boolean expressions can express some everyday problems.
Suppose you want to decide whether today is an appropriate
day to go to the mall. You go to the mall either if it is not sunny or
if<span class="refelem"><span class="refcolumn"><span class="refcontent">Nadeem Hamid suggested this formulation of the exercise.</span></span></span>
today is Friday (because that is when stores post new sales items).</p><p><div class="SIntrapara">Here is how you could go about it using your new knowledge about
Booleans. First add these two lines to the definitions area of
DrRacket:
</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">sunny</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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">friday</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Now create an expression that computes whether <span class="RktSym">sunny</span> is false or
<span class="RktSym">friday</span> is true. So in this particular case, the answer is
<span class="RktVal">#false</span>. (Why?)</div></p><p>See <a href="part_one.html#%28counter._%28exercise._arith-n%29%29" data-pltdoc="x">exercise&nbsp;1</a> for how to create expressions in DrRacket. How many
combinations of Booleans can you associate with <span class="RktSym">sunny</span> and
<span class="RktSym">friday</span>? <a href="part_one.html#%28counter._%28exercise._arith-b1%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>1.6<tt>&nbsp;</tt><a name="(part._sec~3aboolean-if)"></a>Mixing It Up with Booleans</h4><p><div class="SIntrapara">One important use of Boolean values concerns calculations with
different kinds of data. We know from the Prologue that BSL programs
may name values via definitions. For example, we could start a program 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#%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">2</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">and then compute its inverse:
</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">inverse-of-x</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">1</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">This works fine, as long as we don&rsquo;t edit the program and change <span class="RktSym">x</span> to
<span class="RktVal">0</span>.</div></p><p><div class="SIntrapara">This is where Boolean values come in, in particular conditional
calculations. First, the primitive function <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> determines whether
two (or more) numbers are equal. If so, it produces <span class="RktVal">#true</span>,
otherwise <span class="RktVal">#false</span>. Second, there is a kind of BSL expression that we
haven&rsquo;t mentioned so far: the <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._if%29%29" class="RktStxLink" data-pltdoc="x">if</a></span> expression. It uses the word
&ldquo;if&rdquo; as if it were a primitive function; it isn&rsquo;t. The word &ldquo;if&rdquo; is
followed by three expressions, separated by blank spaces (that includes
tabs, line breaks, etc.). Naturally the entire expression is enclosed in
parentheses. Here is an example:
</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._if%29%29" class="RktStxLink" data-pltdoc="x">if</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="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="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">1</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">This <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._if%29%29" class="RktStxLink" data-pltdoc="x">if</a></span> expression contains <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="stt"> </span><span class="RktSym">x</span><span class="stt"> </span><span class="RktVal">0</span><span class="RktPn">)</span>, <span class="RktVal">0</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._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="stt"> </span><span class="RktVal">1</span><span class="stt"> </span><span class="RktSym">x</span><span class="RktPn">)</span>, three <span style="font-style: italic">sub-expressions</span>. The
evaluation of this expression proceeds in two steps:
</div><div class="SIntrapara"><ol><li><p>The first expression is always evaluated. Its result must be a
Boolean.</p></li><li><p>If the result of the first expression is <span class="RktVal">#true</span>, then the
second expression is evaluated; otherwise the third one is. Whatever their
results are, they are also 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._if%29%29" class="RktStxLink" data-pltdoc="x">if</a></span>
expression.</p></li></ol></div></p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p>Right-click on the result and choose a different representation.</p></blockquote></blockquote></blockquote><p><div class="SIntrapara">Given the definition of <span class="RktSym">x</span> above, you can experiment with
<span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._if%29%29" class="RktStxLink" data-pltdoc="x">if</a></span> expressions in the interactions 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#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._if%29%29" class="RktStxLink" data-pltdoc="x">if</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="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="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">1</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">0.5</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Using the laws of arithmetic, you can figure out the result yourself:
</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._if%29%29" class="RktStxLink" data-pltdoc="x">if</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="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="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">1</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="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">because </span><span class="RktSym">x</span><span class="RktCmt"> stands for </span><span class="RktVal">2</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._if%29%29" class="RktStxLink" data-pltdoc="x">if</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="RktVal">2</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="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">1</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktVal">2</span><span class="RktCmt"> is not equal to </span><span class="RktVal">0</span><span class="RktCmt">, </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="stt"> </span><span class="RktVal">2</span><span class="stt"> </span><span class="RktVal">0</span><span class="RktPn">)</span><span class="RktCmt"> is </span><span class="RktVal">#false</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._if%29%29" class="RktStxLink" data-pltdoc="x">if</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="hspace">&nbsp;</span><span class="RktVal">0</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="
the latter is not equal to <span class="RktVal">0</span>. Hence, <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="stt"> </span><span class="RktSym">x</span><span class="stt"> </span><span class="RktVal">0</span><span class="RktPn">)</span> produces
the result <span class="RktVal">#false</span>, meaning <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._if%29%29" class="RktStxLink" data-pltdoc="x">if</a></span> picks its third
sub-expression to be evaluated.</div></p><p><div class="SIntrapara">Stop! Imagine you edit the definition so that it looks like this:
</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">x</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">What do you think
</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._if%29%29" class="RktStxLink" data-pltdoc="x">if</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="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="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">1</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">evaluates to in this context? Why? Show your calculation.</div></p><p>In addition to <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>, BSL provides a host of other comparison
primitives. Explain what the following four comparison primitives
determine about numbers: <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="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="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="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>.</p><p>Strings aren&rsquo;t compared with <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> and its relatives. Instead, you
must use <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> or <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~3c~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string&lt;=?</a></span> or <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~3e~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string&gt;=?</a></span> if
you ever need to compare strings. While it is obvious that
<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> checks whether the two given strings are equal, the
other two primitives are open to interpretation. Look up their
documentation. Or, experiment, guess a general law, and then check in the
documentation whether you guessed right.</p><p><div class="SIntrapara">You may wonder why it is ever necessary to compare strings with each
other. So imagine a program that deals with traffic lights. It may use
the strings <span class="RktVal">"green"</span>, <span class="RktVal">"yellow"</span>, and
<span class="RktVal">"red"</span>. This kind of program may contain a fragment such as
this:
</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">current-color</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara"><span class="refelem"><span class="refcolumn"><span class="refcontent">The dots in the definition of <span class="RktSym">current-color</span>
aren&rsquo;t a part of the program, of course. Replace them with a string that
refers to a color.</span></span></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="RktSym">next-color</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._if%29%29" class="RktStxLink" data-pltdoc="x">if</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~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="hspace">&nbsp;</span><span class="RktSym">current-color</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">It should be easy to imagine that this fragment deals with the computation
that determines which light bulb is to be turned on next and which one
should be turned off.</div></p><p>The next few chapters introduce better expressions than <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._if%29%29" class="RktStxLink" data-pltdoc="x">if</a></span> to
express conditional computations and, most importantly, systematic ways
for designing them.</p><p><div class="SIntrapara"><a name="(counter._(exercise._arith-b2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;8. Add the following line to the definitions area:
</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">cat</span><span class="hspace">&nbsp;</span><img src="cat1.png" alt="" width="75" height="117"/><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Create a conditional expression that
computes whether the image is tall or wide. An image should be labeled <span class="RktVal">"tall"</span> if its height
is larger than or equal to its width; otherwise it is <span class="RktVal">"wide"</span>. See
<a href="part_one.html#%28counter._%28exercise._arith-n%29%29" data-pltdoc="x">exercise&nbsp;1</a> for how to create such expressions in DrRacket; as you experiment,
replace the cat with a rectangle of your choice to ensure that you
know the expected answer.</div></p><p>Now try the following modification. Create an expression that computes
whether a picture is <span class="RktVal">"tall"</span>, <span class="RktVal">"wide"</span>, or
<span class="RktVal">"square"</span>. <a href="part_one.html#%28counter._%28exercise._arith-b2%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>1.7<tt>&nbsp;</tt><a name="(part._sec~3apredicates)"></a>Predicates: Know Thy Data</h4><p><div class="SIntrapara">Remember 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._string-length%29%29" class="RktValLink" data-pltdoc="x">string-length</a></span><span class="stt"> </span><span class="RktVal">42</span><span class="RktPn">)</span> and its
result. Actually, the expression doesn&rsquo;t have a result, it signals an
error. DrRacket lets you know about errors via red text in the
interactions area and highlighting of the faulty expression (in the
definitions area). This way of marking
errors is particularly helpful when you use this expression (or its relatives)
deeply nested within some other expression:
</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/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">42</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</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></p></blockquote></div><div class="SIntrapara">Experiment with this expression by entering it both into DrRacket&rsquo;s
interactions area and in the definitions area (and then click on
<span class="emph">RUN</span>).</div></p><p><div class="SIntrapara">Of course, you really don&rsquo;t want such error-signaling expressions in your
program. And usually, you don&rsquo;t make such obvious mistakes as using
<span class="RktVal">42</span> as a string. It is quite common, however, that
programs deal with variables that may stand for either a number or a
string:
</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">in</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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#%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="RktSym">in</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">A variable such as <span class="RktSym">in</span> can be a placeholder for any value,
including a number, and this value then shows up in the
<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> expression.</div></p><p><div class="SIntrapara">One way to prevent such accidents is to use a <span style="font-style: italic">predicate</span>, which is
a function that consumes a value and determines whether or not it belongs
to some class of data. For example, the predicate <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~3f%29%29" class="RktValLink" data-pltdoc="x">number?</a></span>
determines whether the given value is a number or not:
</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._number~3f%29%29" class="RktValLink" data-pltdoc="x">number?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">4</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._number~3f%29%29" class="RktValLink" data-pltdoc="x">number?</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">#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._number~3f%29%29" class="RktValLink" data-pltdoc="x">number?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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._number~3f%29%29" class="RktValLink" data-pltdoc="x">number?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"fortytwo"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#false</span></p></td></tr></table></blockquote></div><div class="SIntrapara">As you see, the predicates produce Boolean values. Hence, when predicates are
combined with conditional expressions, programs can protect expressions
from misuse:
</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">in</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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._if%29%29" class="RktStxLink" data-pltdoc="x">if</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~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">in</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._string-length%29%29" class="RktValLink" data-pltdoc="x">string-length</a></span><span class="hspace">&nbsp;</span><span class="RktSym">in</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p>Every class of data that we introduced in this chapter comes with a
predicate. Experiment with <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~3f%29%29" class="RktValLink" data-pltdoc="x">number?</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~3f%29%29" class="RktValLink" data-pltdoc="x">string?</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._image~3f%29%29" class="RktValLink" data-pltdoc="x">image?</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._boolean~3f%29%29" class="RktValLink" data-pltdoc="x">boolean?</a></span> to ensure that you understand how
they work.</p><p><div class="SIntrapara">In addition to predicates that distinguish different forms of data,
programming languages also come with predicates that distinguish different
kinds of numbers. In BSL, numbers are classified in two ways: by
construction and by their exactness. Construction refers to the familiar
sets of numbers: <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._integer~3f%29%29" class="RktValLink" data-pltdoc="x">integer?</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._rational~3f%29%29" class="RktValLink" data-pltdoc="x">rational?</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._real~3f%29%29" class="RktValLink" data-pltdoc="x">real?</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._complex~3f%29%29" class="RktValLink" data-pltdoc="x">complex?</a></span>,<span class="refelem"><span class="refcolumn"><span class="refcontent">Put <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._sqrt%29%29" class="RktValLink" data-pltdoc="x">sqrt</a></span><span class="stt"> </span><span class="RktVal"><span class="nobreak">-1</span></span><span class="RktPn">)</span> at the prompt in the
interactions area and hit the &ldquo;enter&rdquo; key. Take a close look at the result. The result you see
is the first so-called complex number anyone encounters. While your
teacher may have told you that one doesn&rsquo;t compute the square
root of negative numbers, the truth is that mathematicians and some programmers
find it acceptable and useful to do so anyway. But don&rsquo;t worry:
understanding complex numbers is not essential to being a program designer.</span></span></span>
but many programming languages, including BSL, also choose to use finite
approximations to well-known constants, which leads to somewhat surprising
results with the <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._rational~3f%29%29" class="RktValLink" data-pltdoc="x">rational?</a></span> predicate:
</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._rational~3f%29%29" class="RktValLink" data-pltdoc="x">rational?</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">#true</span></p></td></tr></table></blockquote></div><div class="SIntrapara">As for exactness, we have mentioned the idea before. For now, experiment with
<span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._exact~3f%29%29" class="RktValLink" data-pltdoc="x">exact?</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._inexact~3f%29%29" class="RktValLink" data-pltdoc="x">inexact?</a></span> to make sure they perform the checks
that their names suggest. Later we are going to discuss the nature of numbers
in some detail.</div></p><p><div class="SIntrapara"><a name="(counter._(exercise._arith-p1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;9. Add the following line to the definitions area of DrRacket:
</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">in</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Then create an expression that converts the value of <span class="RktSym">in</span> to a
non-negative number. For a <a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a>, it determines how long the
<a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a> is; for an <a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a>, it uses the area; for a
<a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a>, it uses the absolute value; for <span class="RktVal">#true</span> it uses <span class="RktVal">10</span> and
for <span class="RktVal">#false</span> <span class="RktVal">20</span>. <span style="font-weight: bold">Hint</span> Check out <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>
from the <a href="part_prologue.html" data-pltdoc="x">Prologue: How to Program</a> (again).</div></p><p>See <a href="part_one.html#%28counter._%28exercise._arith-n%29%29" data-pltdoc="x">exercise&nbsp;1</a> for how to create expressions in DrRacket. <a href="part_one.html#%28counter._%28exercise._arith-p1%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._arith-p2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;10. Now relax, eat, sleep, and then tackle the next
chapter. <a href="part_one.html#%28counter._%28exercise._arith-p2%29%29" class="ex-end" data-pltdoc="x"></a></p><h3>2<tt>&nbsp;</tt><a name="(part._ch~3afuncs-progs)"></a>Functions and Programs</h3><p>As far as programming is concerned, &ldquo;arithmetic&rdquo; is half the game; the
other half is &ldquo;algebra.&rdquo; Of course, &ldquo;algebra&rdquo; relates to the school
notion of algebra as little/much as the notion of &ldquo;arithmetic&rdquo; from the
preceding chapter relates to arithmetic taught in grade-school
arithmetic. Specifically, the algebra notions needed are variable,
function definition, function application, and function composition. This
chapter reacquaints you with these notions in a fun and accessible
manner.</p><h4>2.1<tt>&nbsp;</tt><a name="(part._sec~3afuncs)"></a>Functions</h4><p>Programs are functions. Like functions, programs consume inputs and produce
outputs. Unlike the functions you may know, programs work with a variety
of data: numbers, strings, images, mixtures of all these, and so on.
Furthermore, programs are triggered by events in the real world, and the
outputs of programs affect the real world. For example, a spreadsheet
program may react to an accountant&rsquo;s key presses by filling some cells
with numbers, or the calendar program on a computer may launch a monthly
payroll program on the last day of every month. Lastly, a program may not
consume all of its input data at once, instead it may decide to
process data in an incremental manner.</p><p><span style="font-weight: bold">Definitions</span> While many programming languages obscure the
relationship between programs and functions, BSL brings it to the
fore. Every BSL program consists of several definitions, usually
followed by an expression that involves those definitions. There are two
kinds of definitions:</p><ul><li><p><span style="font-style: italic">constant definitions</span>, of the shape <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">Variable</span><span class="stt"> </span><span class="RktVar">Expression</span><span class="RktPn">)</span>, which we encountered in the preceding chapter; and</p></li><li><p><a name="(idx._(gentag._11))"></a><span style="font-style: italic">function definitions</span>, which come in many flavors, one of
which we used in the Prologue.</p></li></ul><p><div class="SIntrapara">Like expressions, <a name="(idx._(gentag._12))"></a>function definitions in BSL come in a uniform 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._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktVar">FunctionName</span><span class="hspace">&nbsp;</span><span class="RktVar">Variable</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktVar">Variable</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktVar">Expression</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">That is, to define a function, we write down
</div><div class="SIntrapara"><ul><li><p>&ldquo;<span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></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="RktMeta"></span> <span class="RktInBG"><span class="hspace"></span><span class="RktIn">(</span><span class="hspace"></span></span>&rdquo;,</p></li><li><p>the name of the function,</p></li><li><p>followed by several variables, separated by space and ending in &ldquo;<span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span>&rdquo;,</p></li><li><p>and an expression followed by &ldquo;<span class="RktInBG"><span class="hspace"></span><span class="RktIn">)</span><span class="hspace"></span></span>&rdquo;.</p></li></ul></div><div class="SIntrapara">And that is all there is to it. Here are some small examples:
</div><div class="SIntrapara"><ul><li><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="RktSym">f</span><span class="stt"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span></p></li><li><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="RktSym">g</span><span class="stt"> </span><span class="RktSym">x</span><span class="stt"> </span><span class="RktSym">y</span><span class="RktPn">)</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._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="stt"> </span><span class="RktVal">1</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="RktPn">)</span></p></li><li><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="RktSym">h</span><span class="stt"> </span><span class="RktSym">x</span><span class="stt"> </span><span class="RktSym">y</span><span class="stt"> </span><span class="RktSym">z</span><span class="RktPn">)</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._%2B%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/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="RktVal">2</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktVal">3</span><span class="RktPn">)</span><span class="RktPn">)</span></p></li></ul></div></p><p>Before we explain why these examples are silly, we need to explain what
function definitions mean. Roughly speaking, a function definition
introduces a new operation on data; put differently, it adds an operation
to our vocabulary if we think of the primitive operations as the ones that
are always available. Like a primitive function, a defined function
consumes inputs. The number of variables determines how many inputs&#8212;<wbr></wbr>also
called <span style="font-style: italic">arguments</span> or <span style="font-style: italic">parameters</span>&#8212;<wbr></wbr>a function
consumes. Thus, <span class="RktSym">f</span> is a one-argument function, sometimes called a
<span style="font-style: italic">unary</span> function. In contrast, <span class="RktSym">g</span> is a two-argument
function, also dubbed <span style="font-style: italic">binary</span>, and <span class="RktSym">h</span> is a
<span style="font-style: italic">ternary</span> or three-argument function. The expression&#8212;<wbr></wbr>often
referred to as the <span style="font-style: italic">function body</span>&#8212;<wbr></wbr>determines the output.</p><p>The examples are silly because the expressions inside the functions do not
involve the variables. Since variables are about inputs, not mentioning
them in the expressions means that the function&rsquo;s output is independent of
its input and therefore always the same. We don&rsquo;t need to write functions
or programs if the output is always the same.</p><p><div class="SIntrapara">Variables aren&rsquo;t data; they represent data. For example, a constant
definition such as
</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">x</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">says that <span class="RktSym">x</span> always stands for <span class="RktVal">3</span>. The variables in a
<span style="font-style: italic">function header</span>, that is, the variables that follow the function
name, are placeholders for <span style="font-weight: bold">unknown</span> pieces of data, the inputs of the
function. Mentioning a variable in the function body is the way to use these
pieces of data when the function is applied and the values of the variables
become known.</div></p><p><div class="SIntrapara">Consider the following fragment, a <span class="emph">stub</span> function definition:
</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">ff</span><span class="hspace">&nbsp;</span><span class="RktSym">a</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara"><span class="RktPn">(</span><span class="RktSym">ff</span><span class="stt"> </span><span class="RktSym">a</span><span class="RktPn">)</span> is the function header. It means <span class="RktSym">ff</span> consumes one
piece of input, and the variable <span class="RktSym">a</span> is a placeholder for
this input. Of course, at the time we define a function, we don&rsquo;t
know what its input(s) will be. Indeed, the whole point of defining a
function is that we can use the function many times on many different
inputs.</div></p><p><div class="SIntrapara">Useful function bodies refer to the function parameters. A reference to a
function parameter is really a reference to the piece of data that is the
input to the function. If we complete the definition of <span class="RktSym">ff</span> like
this
</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">ff</span><span class="hspace">&nbsp;</span><span class="RktSym">a</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="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktSym">a</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">we are saying that the output of a function is ten times its
input. Presumably this function is going to be supplied with numbers as
inputs because it makes no sense to multiply images or Boolean values or
strings by <span class="RktVal">10</span>.</div></p><p>For now, the only remaining question is how a function obtains its
inputs. And to this end, we turn to the notion of applying a
function.</p><p><div class="SIntrapara"><span style="font-weight: bold">Applications</span> A <span style="font-style: italic">function application</span> puts <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>d
functions to work, and it looks just like the applications of a pre-defined operation:
</div><div class="SIntrapara"><ul><li><p>write &ldquo;(&rdquo;,</p></li><li><p>write down the name of a defined function <span class="RktSym">f</span>,</p></li><li><p>write down as many arguments as <span class="RktSym">f</span> consumes, separated by space,</p></li><li><p>and add &ldquo;)&rdquo; at the end.</p></li></ul></div></p><p><div class="SIntrapara">With this bit of explanation, you can now experiment with functions in the
interactions area just as we suggested you experiment with primitives to
find out what they compute. The following three experiments, for example,
confirm that <span class="RktSym">f</span> from above produces the same value no matter what
input it is applied to:
</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">f</span><span class="hspace">&nbsp;</span><span class="RktVal">1</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">f</span><span class="hspace">&nbsp;</span><span class="RktVal">"hello world"</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">f</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">1</span></p></td></tr></table></blockquote></div><div class="SIntrapara">What does <span class="RktPn">(</span><span class="RktSym">f</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._circle%29%29" class="RktValLink" data-pltdoc="x">circle</a></span><span class="stt"> </span><span class="RktVal">3</span><span class="stt"> </span><span class="RktVal">"solid"</span><span class="stt"> </span><span class="RktVal">"red"</span><span class="RktPn">)</span><span class="RktPn">)</span>
yield?<span class="refelem"><span class="refcolumn"><span class="refcontent">Remember to 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
definitions area.</span></span></span></div></p><p><div class="SIntrapara">See, even images as inputs don&rsquo;t change <span class="RktSym">f</span>&rsquo;s behavior. But here is
what happens when the function is applied to too few or too many arguments:
</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">f</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">f:expects 1 argument, found none</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">f</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="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">f:expects only 1 argument, found 5</span></p></td></tr></table></blockquote></div><div class="SIntrapara">DrRacket signals an error that is just like those you see when you apply a
primitive to the wrong number of arguments:
</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="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">+:expects at least 2 arguments, found none</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Functions don&rsquo;t have to be applied at the prompt in the interactions
area. It is perfectly acceptable to use function applications nested
within other function applications:
</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">ff</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">32</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="RktPn">(</span><span class="RktSym">ff</span><span class="hspace">&nbsp;</span><span class="RktVal">4</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">ff</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">1280</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">ff</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ff</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">100</span></p></td></tr></table></blockquote></div></p><p><a name="(counter._(exercise._fun0))"></a><span style="font-weight: bold">Exercise</span>&nbsp;11. Define a function that consumes two numbers, <span style="font-style: italic">x</span> and
<span style="font-style: italic">y</span>, and that computes the distance of point <span style="font-style: italic"></span>(<span style="font-style: italic">x,y</span>)<span style="font-style: italic"></span> to the
origin.</p><p>In <a href="part_one.html#%28counter._%28exercise._arith-n%29%29" data-pltdoc="x">exercise&nbsp;1</a> you developed the right-hand side of this function for
concrete values of <span style="font-style: italic">x</span> and <span style="font-style: italic">y</span>. Now add a header. <a href="part_one.html#%28counter._%28exercise._fun0%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._fun1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;12. Define the function <span class="RktSym">cvolume</span>, which accepts
the length of a side of an equilateral cube and computes its volume. If you
have time, consider defining <span class="RktSym">csurface</span>, too.</p><p><span style="font-weight: bold">Hint</span> An equilateral cube is a three-dimensional container bounded by
six squares. You can determine the surface of a cube if you know that the
square&rsquo;s area is its length multiplied by itself. Its volume is the length
multiplied with the area of one of its squares. (Why?) <a href="part_one.html#%28counter._%28exercise._fun1%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._fun2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;13. Define the function <span class="RktSym">string-first</span>, which extracts
the first <a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a> from a <span style="font-weight: bold">non-empty</span> string. <a href="part_one.html#%28counter._%28exercise._fun2%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._fun3))"></a><span style="font-weight: bold">Exercise</span>&nbsp;14. Define the function <span class="RktSym">string-last</span>, which extracts
the last <a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a> from a non-empty string. <a href="part_one.html#%28counter._%28exercise._fun3%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._fun4))"></a><span style="font-weight: bold">Exercise</span>&nbsp;15. Define <span class="RktSym">==&gt;</span>. The function consumes two Boolean
values, call them <span class="RktSym">sunny</span> and <span class="RktSym">friday</span>. Its answer is
<span class="RktVal">#true</span> if <span class="RktSym">sunny</span> is false or <span class="RktSym">friday</span> is
true. <span style="font-weight: bold">Note</span> Logicians call this Boolean operation
<span style="font-style: italic">implication</span>, and they use the notation <span style="font-style: italic">sunny =&gt; friday</span> for
this purpose. <a href="part_one.html#%28counter._%28exercise._fun4%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._fun5))"></a><span style="font-weight: bold">Exercise</span>&nbsp;16. Define the function <span class="RktSym">image-area</span>, which counts the
number of pixels in a given image. See <a href="part_one.html#%28counter._%28exercise._arith-i1%29%29" data-pltdoc="x">exercise&nbsp;6</a> for ideas. <a href="part_one.html#%28counter._%28exercise._fun5%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._fun10))"></a><span style="font-weight: bold">Exercise</span>&nbsp;17. Define the function <span class="RktSym">image-classify</span>, which consumes an
image and conditionally produces <span class="RktVal">"tall"</span> if the image is taller than wide,
<span class="RktVal">"wide"</span> if it is wider than tall, or <span class="RktVal">"square"</span> if
its width and height are the same. See <a href="part_one.html#%28counter._%28exercise._arith-b2%29%29" data-pltdoc="x">exercise&nbsp;8</a> for ideas. <a href="part_one.html#%28counter._%28exercise._fun10%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._fun10a))"></a><span style="font-weight: bold">Exercise</span>&nbsp;18. Define the function <span class="RktSym">string-join</span>, which consumes two
strings and appends them with <span class="RktVal">"_"</span> in between. See <a href="part_one.html#%28counter._%28exercise._arith-s0%29%29" data-pltdoc="x">exercise&nbsp;2</a>
for ideas. <a href="part_one.html#%28counter._%28exercise._fun10a%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._fun10b))"></a><span style="font-weight: bold">Exercise</span>&nbsp;19. Define the function <span class="RktSym">string-insert</span>, which
consumes a string <span class="RktSym">str</span> plus a number <span class="RktSym">i</span> and inserts
<span class="RktVal">"_"</span> at the <span style="font-style: italic">i</span>th position of <span class="RktSym">str</span>. Assume <span class="RktSym">i</span>
is a number between <span class="RktVal">0</span> and the length of the given string
(inclusive). See <a href="part_one.html#%28counter._%28exercise._arith-s1%29%29" data-pltdoc="x">exercise&nbsp;3</a> for ideas. Ponder how
<span class="RktSym">string-insert</span> copes with <span class="RktVal">""</span>. <a href="part_one.html#%28counter._%28exercise._fun10b%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._fun10c))"></a><span style="font-weight: bold">Exercise</span>&nbsp;20. Define the function <span class="RktSym">string-delete</span>, which
consumes a string plus a number <span class="RktSym">i</span> and deletes the <span style="font-style: italic">i</span>th
position from <span class="RktSym">str</span>. Assume <span class="RktSym">i</span> is a number between
<span class="RktVal">0</span> (inclusive) and the length of the given string (exclusive).
See <a href="part_one.html#%28counter._%28exercise._arith-s2%29%29" data-pltdoc="x">exercise&nbsp;4</a> for ideas. Can <span class="RktSym">string-delete</span> deal with empty
strings? <a href="part_one.html#%28counter._%28exercise._fun10c%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>2.2<tt>&nbsp;</tt><a name="(part._sec~3acomputing)"></a>Computing</h4><p>Function definitions and applications work in tandem. If you want to design
programs, you must understand this collaboration because you need to
imagine how DrRacket runs your programs and because you need to figure out
<span style="font-weight: bold">what</span> goes wrong <span style="font-weight: bold">when</span> things go wrong&#8212;<wbr></wbr>and they <span style="font-weight: bold">will</span> go
wrong.</p><p>While you may have seen this idea in an algebra course, we prefer to
explain it our way. So here we go. Evaluating a function application
proceeds in three steps: DrRacket determines the values of the argument
expressions; it checks that the number of arguments and the number of
function parameters are the same; if so, DrRacket computes the value of the
body of the function, with all parameters replaced by the corresponding
argument values. This last value is the value of the function application.
This is a mouthful, so we need examples.</p><p><div class="SIntrapara">Here is a sample calculation for <span class="RktSym">f</span> from the previous section:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">f</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">1</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> knows that </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="stt"> </span><span class="RktVal">1</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">==</span><span class="stt"> </span><span class="RktVal">2</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">f</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> replaced all occurrences of </span><span class="RktSym">x</span><span class="RktCmt"> with </span><span class="RktVal">2</span></td></tr><tr><td><span class="RktVal">1</span></td></tr></table></blockquote></div><div class="SIntrapara">That last equation is weird because <span class="RktSym">x</span> does not occur in the
body of <span class="RktSym">f</span>. Therefore, replacing the occurrences of <span class="RktSym">x</span>
with <span class="RktVal">2</span> in the function body produces <span class="RktVal">1</span>, which is the
function body itself.</div></p><p><div class="SIntrapara">For <span class="RktSym">ff</span>, DrRacket performs a different kind of computation:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">ff</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">1</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> again knows that </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="stt"> </span><span class="RktVal">1</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">==</span><span class="stt"> </span><span class="RktVal">2</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">ff</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> replaces </span><span class="RktSym">a</span><span class="RktCmt"> with </span><span class="RktVal">2</span><span class="RktCmt"> in </span><span class="RktSym">ff</span><span class="RktCmt">'</span><span class="RktCmt">s body</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">10</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">and from here, </span><span class="RktCmt">DrRacket</span><span class="RktCmt"> uses plain arithmetic </span></td></tr><tr><td><span class="RktVal">20</span></td></tr></table></blockquote></div></p><p><div class="SIntrapara">The best point is that when you combine these laws of computation with
those of arithmetic, you can pretty much predict the outcome of any
program in BSL:
</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="RktPn">(</span><span class="RktSym">ff</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">1</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> knows that </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="stt"> </span><span class="RktVal">1</span><span class="stt"> </span><span class="RktVal">2</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">==</span><span class="stt"> </span><span class="RktVal">3</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._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ff</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> replaces </span><span class="RktSym">a</span><span class="RktCmt"> with </span><span class="RktVal">3</span><span class="RktCmt"> in </span><span class="RktSym">ff</span><span class="RktCmt">'</span><span class="RktCmt">s body</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._%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">10</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">now </span><span class="RktCmt">DrRacket</span><span class="RktCmt"> uses the laws of arithmetic </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._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">30</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</sp
</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._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ff</span><span class="hspace">&nbsp;</span><span class="RktVal">4</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">ff</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> substitutes </span><span class="RktVal">4</span><span class="RktCmt"> for </span><span class="RktSym">a</span><span class="RktCmt"> in </span><span class="RktSym">ff</span><span class="RktCmt">'</span><span class="RktCmt">s body</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="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">10</span><span class="hspace">&nbsp;</span><span class="RktVal">4</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">ff</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> knows that </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="stt"> </span><span class="RktVal">10</span><span class="stt"> </span><span class="RktVal">4</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">==</span><span class="stt"> </span><span class="RktVal">40</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">40</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="
knows all the laws of arithmetic and it is great at substitution. Even
better, DrRacket cannot only determine the value of an expression; it can
also show you <span style="font-weight: bold">how</span> it does it. That is, it can show you step-by-step
how to solve these algebra problems that ask you to determine the value of
an expression.</p><p>Take a second look at the buttons that come with DrRacket. One of them looks
like an &ldquo;advance to next track&rdquo; button on an audio player. If you click this
button, the <span style="font-weight: bold">stepper</span> window pops up and you can step through the
evaluation of the program in the definitions area.</p><p>Enter the definition of <span class="RktSym">ff</span> into the definitions area. Add
<span class="RktPn">(</span><span class="RktSym">ff</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._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="stt"> </span><span class="RktVal">1</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="RktPn">)</span> at the bottom. Now click the <span class="emph">STEP</span>. The
stepper window will show up; <a href="part_one.html#%28counter._%28figure._fig~3astepper%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">11</span></a> shows what it looks
like in version 6.2 of the software. At this point, you can use the
forward and backward arrows to see all the computation steps that DrRacket
uses to determine the value of an expression. Watch how the stepper
performs the same calculations as we do.</p><p>Stop! Yes, you could have used DrRacket to solve some of your algebra
homework. Experiment with the various options that the stepper offers.</p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><p><img src="dd-stepper.png" alt="" width="363" height="125"/></p></blockquote></blockquote><p class="Centertext"><span class="Legend"><span class="FigureTarget"><a name="(counter._(figure._fig~3astepper))" x-target-lift="Figure"></a>Figure&nbsp;11: </span>The DrRacket stepper</span></p></blockquote><p><a name="(counter._(exercise._ex~3astepper1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;21. Use DrRacket&rsquo;s stepper to evaluate <span class="RktPn">(</span><span class="RktSym">ff</span><span class="stt"> </span><span class="RktPn">(</span><span class="RktSym">ff</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="RktPn">)</span> step-by-step. Also try <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="RktSym">ff</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktPn">(</span><span class="RktSym">ff</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="RktPn">)</span>. Does DrRacket&rsquo;s
stepper reuse the results of computations? <a href="part_one.html#%28counter._%28exercise._ex~3astepper1%29%29" class="ex-end" data-pltdoc="x"></a></p><p>At this point, you might think that you are back in an algebra course with
all these computations involving uninteresting functions and
numbers. Fortunately, this approach generalizes to <span style="font-weight: bold">all</span> programs,
including the interesting ones, in this book.</p><p><div class="SIntrapara">Let&rsquo;s start by looking at functions that process strings. Recall some of
the laws of string 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#%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><span class="hspace">&nbsp;</span><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktVal">"hello world"</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._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"bye"</span><span class="hspace">&nbsp;</span><span class="RktVal">", "</span><span class="hspace">&nbsp;</span><span class="RktVal">"world"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktVal">"bye, world"</span></td></tr><tr><td><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span></td></tr></table></blockquote></div></p><p><div class="SIntrapara">Now suppose we define a function that creates the opening of a letter: <a name="(idx._(gentag._13))"></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">opening</span><span class="hspace">&nbsp;</span><span class="RktSym">first-name</span><span class="hspace">&nbsp;</span><span class="RktSym">last-name</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._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"Dear "</span><span class="hspace">&nbsp;</span><span class="RktSym">first-name</span><span class="hspace">&nbsp;</span><span class="RktVal">","</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">When you apply this function to two strings, you get a letter opening:
</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">opening</span><span class="hspace">&nbsp;</span><span class="RktVal">"Matthew"</span><span class="hspace">&nbsp;</span><span class="RktVal">"Fisler"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"Dear Matthew,"</span></p></td></tr></table></blockquote></div><div class="SIntrapara">More importantly, though, the laws of computing explain how DrRacket
determines this result and how you can anticipate what DrRacket does:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">opening</span><span class="hspace">&nbsp;</span><span class="RktVal">"Matthew"</span><span class="hspace">&nbsp;</span><span class="RktVal">"Fisler"</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> substitutes </span><span class="RktVal">"Matthew"</span><span class="RktCmt"> for </span><span class="RktSym">first-name</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._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"Dear "</span><span class="hspace">&nbsp;</span><span class="RktVal">"Matthew"</span><span class="hspace">&nbsp;</span><span class="RktVal">","</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span></td></tr><tr><td><span class="RktVal">"Dear Matthew,"</span></td></tr></table></blockquote></div><div class="SIntrapara">Since <span class="RktSym">last-name</span> does not occur in the definition of
<span class="RktSym">opening</span>, replacing it with <span class="RktVal">"Fisler"</span> has no effect.</div></p><p>The rest of the book introduces more forms of data.<span class="refelem"><span class="refcolumn"><span class="refcontent">Eventually you will encounter imperative
operations, which do not combine or extract values but <span style="font-weight: bold">modify</span>
them. To calculate with such operations, you will need to add some laws to
those of arithmetic and substitution.</span></span></span> To explain operations on data,
we always use laws like those of arithmetic in this book.</p><p><div class="SIntrapara"><a name="(counter._(exercise._dr-step0))"></a><span style="font-weight: bold">Exercise</span>&nbsp;22. Use DrRacket&rsquo;s stepper on this program fragment: <a name="(idx._(gentag._14))"></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">distance-to-origin</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</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._sqrt%29%29" class="RktValLink" data-pltdoc="x">sqrt</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._sqr%29%29" class="RktValLink" data-pltdoc="x">sqr</a></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._sqr%29%29" class="RktValLink" data-pltdoc="x">sqr</a></span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">distance-to-origin</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></table></blockquote></div><div class="SIntrapara">Does the explanation match your intuition? <a href="part_one.html#%28counter._%28exercise._dr-step0%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><div class="SIntrapara"><a name="(counter._(exercise._dr-step2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;23. The first <a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a> in <span class="RktVal">"hello world"</span> is
<span class="RktVal">"h"</span>. How does the following function compute this result? <a name="(idx._(gentag._15))"></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">string-first</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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._substring%29%29" class="RktValLink" data-pltdoc="x">substring</a></span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Use the stepper to confirm your ideas. <a href="part_one.html#%28counter._%28exercise._dr-step2%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><div class="SIntrapara"><a name="(counter._(exercise._dr-step4))"></a><span style="font-weight: bold">Exercise</span>&nbsp;24. Here is the definition of <span class="RktSym">==&gt;</span>: <a name="(idx._(gentag._16))"></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">==&gt;</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</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._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._not%29%29" class="RktValLink" data-pltdoc="x">not</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Use the stepper to determine the value of <span class="RktPn">(</span><span class="RktSym">==&gt;</span><span class="stt"> </span><span class="RktVal">#true</span><span class="stt"> </span><span class="RktVal">#false</span><span class="RktPn">)</span>. <a href="part_one.html#%28counter._%28exercise._dr-step4%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><div class="SIntrapara"><a name="(counter._(exercise._dr-step10))"></a><span style="font-weight: bold">Exercise</span>&nbsp;25. Take a look at this attempt to solve <a href="part_one.html#%28counter._%28exercise._fun10%29%29" data-pltdoc="x">exercise&nbsp;17</a>: <a name="(idx._(gentag._17))"></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">image-classify</span><span class="hspace">&nbsp;</span><span class="RktSym">img</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~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/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">img</span><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-width%29%29" class="RktValLink" data-pltdoc="x">image-width</a></span><span class="hspace">&nbsp;</span><span class="RktSym">img</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"tall"</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="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">img</span><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-width%29%29" class="RktValLink" data-pltdoc="x">image-width</a></span><span class="hspace">&nbsp;</span><span class="RktSym">img</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"square"</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~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</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">img</span><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-width%29%29" cl
</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">string-insert</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktSym">i</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._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</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._substring%29%29" class="RktValLink" data-pltdoc="x">substring</a></span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktSym">i</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;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktVal">"_"</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;</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._substring%29%29" class="RktValLink" data-pltdoc="x">substring</a></span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktSym">i</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="RktPn">(</span><span class="RktSym">string-insert</span><span class="hspace">&nbsp;</span><span class="RktVal">"helloworld"</span><span class="hspace">&nbsp;</span><span class="RktVal">6</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Confirm your expectation with DrRacket and its stepper. <a href="part_one.html#%28counter._%28exercise._dr-step10b%29%29" class="ex-end" data-pltdoc="x"></a></div></p><h4>2.3<tt>&nbsp;</tt><a name="(part._sec~3acomposing)"></a>Composing Functions</h4><p>A program rarely consists of a single function definition. Typically,
programs consist of a <span style="font-style: italic">main</span> definition and several other
functions and turn the result of one function application
into the input for another. In analogy to algebra, we call this way of
defining functions <span style="font-style: italic">composition</span>, and we call these additional
functions <span style="font-style: italic">auxiliary functions</span> or <span style="font-style: italic">helper functions</span>.
<a name="(idx._(gentag._19))"></a>
<a name="(idx._(gentag._20))"></a>
<a name="(idx._(gentag._21))"></a>
<a name="(idx._(gentag._22))"></a></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">letter</span><span class="hspace">&nbsp;</span><span class="RktSym">fst</span><span class="hspace">&nbsp;</span><span class="RktSym">lst</span><span class="hspace">&nbsp;</span><span class="RktSym">signature-name</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._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">opening</span><span class="hspace">&nbsp;</span><span class="RktSym">fst</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktVal">"\n\n"</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">body</span><span class="hspace">&nbsp;</span><span class="RktSym">fst</span><span class="hspace">&nbsp;</span><span class="RktSym">lst</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktVal">"\n\n"</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">closing</span><span class="hspace">&nbsp;</span><span class="RktSym">signature-name</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="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">opening</span><span class="hspace">&nbsp;</span><span class="RktSym">fst</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._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"Dear "</span><span class="hspace">&nbsp;</span><span class="RktSym">fst</span><span class="hspace">&nbsp;</span><span class="RktVal">","</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"><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">body</span><span class="hspace">&nbsp;</span><span class="RktSym">fst</span><span class="hspace">&nbsp;</span><span class="RktSym">lst</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._string-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;</span><span class="RktVal">"We have
letter templates. It consists of four functions. The first one is the main
function, which produces a complete letter from the first and last name of
the addressee plus a signature. The main function refers to three
auxiliary functions to produce the three pieces of the letter&#8212;<wbr></wbr>the
opening, body, and signature&#8212;<wbr></wbr>and composes the results in the correct
order with <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>.</p><p><div class="SIntrapara">Stop! Enter these definitions into DrRacket&rsquo;s definitions area, click
<span class="emph">RUN</span>, and evaluate these expressions in the interactions 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">letter</span><span class="hspace">&nbsp;</span><span class="RktVal">"Matthew"</span><span class="hspace">&nbsp;</span><span class="RktVal">"Fisler"</span><span class="hspace">&nbsp;</span><span class="RktVal">"Felleisen"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"Dear Matthew,\n\nWe have discovered that ...\n"</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">letter</span><span class="hspace">&nbsp;</span><span class="RktVal">"Kathi"</span><span class="hspace">&nbsp;</span><span class="RktVal">"Felleisen"</span><span class="hspace">&nbsp;</span><span class="RktVal">"Findler"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"Dear Kathi,\n\nWe have discovered that ...\n"</span></p></td></tr></table></blockquote></div></p><p><div class="SIntrapara"><span style="font-weight: bold">Aside</span> The result is a long string that contains <span class="RktVal">"\n"</span>, which
represents a new line when the string is <span style="font-weight: bold">printed</span>. Once your program
has access to functions that write to files or the console, this part of
the string will make sense. Read on.<span class="refelem"><span class="refcolumn"><span class="refcontent">Think of <span class="RktVal">'</span><span class="RktVal">stdout</span> as a <a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a> for now.</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"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span><span class="hspace">&nbsp;</span><span class="RktVal">'</span><span class="RktVal">stdout</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">letter</span><span class="hspace">&nbsp;</span><span class="RktVal">"Matt"</span><span class="hspace">&nbsp;</span><span class="RktVal">"Fiss"</span><span class="hspace">&nbsp;</span><span class="RktVal">"Fell"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><table cellspacing="0" cellpadding="0"><tr><td><p><span class="RktOut">Dear Matt,</span></p></td></tr><tr><td><p><span class="RktOut"></span><span class="hspace">&nbsp;</span><span class="RktOut"></span></p></td></tr><tr><td><p><span class="RktOut">We have discovered that all people with the</span></p></td></tr><tr><td><p><span class="RktOut">last name Fiss have won our lottery. So, </span></p></td></tr><tr><td><p><span class="RktOut">Matt, hurry and pick up your prize.</span></p></td></tr><tr><td><p><span class="RktOut"></span><span class="hspace">&nbsp;</span><span class="RktOut"></span></p></td></tr><tr><td><p><span class="RktOut">Sincerely, </span></p></td></tr><tr><td><p><span class="RktOut"></span></p></td></tr><tr><td><p><span class="RktOut">Fell</span></p></td></tr></table></td></tr><tr><td><p><span class="RktRes">'stdout</span></p></td></tr></table></blockquote></div><div class="SIntrapara"><a href="part_one.html#%28part._sec~3aprogs%29" data-pltdoc="x">Programs</a> explains such batch programs in some depth. <span style="font-weight: bold">End</span></div></p><p>In general, when a problem refers to distinct tasks of computation, a
program should consist of one function per task and a main function that
puts it all together. We formulate this idea as a simple slogan:</p><blockquote><p><span style="font-style: italic">Define one function per task.</span></p></blockquote><p>The advantage of following this slogan is that you get reasonably small
functions, each of which is easy to comprehend and whose composition is
easy to understand. Once you learn to design functions, you will recognize
that getting small functions to work correctly is much easier than doing so
with large ones. Better yet, if you ever need to change a part of the
program due to some change to the problem statement, it tends to be much
easier to find the relevant parts when it is organized as a collection of
small functions as opposed to a large, monolithic block.</p><p>Here is a small illustration of this point with a sample problem:</p><p><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> The owner of a monopolistic movie theater in a small town has
complete freedom in setting ticket prices. The more he charges, the fewer
people can afford tickets. The less he charges, the more it costs
to run a show because attendance goes up. In a recent experiment the owner
determined a relationship between the price of a ticket and average
attendance.</p><p><div class="SIntrapara">At a price of $5.00 per ticket, 120 people attend a performance.
For each 10-cent change in the ticket price, the average attendance
changes by 15 people. That is, if the owner charges $5.10, some 105 people
attend on the average; if the price goes down to $4.90, average
attendance increases to 135. Let&rsquo;s translate this idea into a
mathematical formula:
</div><div class="SIntrapara"><blockquote class="SCentered"><p><img src="pict_33.png" alt="image" width="312" height="21"/></p></blockquote></div><div class="SIntrapara">Stop! Explain the minus sign before you proceed.</div></p><p>Unfortunately, the increased attendance also comes at an increased
cost. Every performance comes at a fixed cost of $180 to the owner plus a
variable cost of $0.04 per attendee.</p><p>The owner would like to know the exact relationship between profit and
ticket price in order to maximize the profit.</p></blockquote></div><div class="SIntrapara">While the task is clear, how to go about it is not. All we can say at this
point is that several quantities depend on each other.</div></p><p><div class="SIntrapara">When we are confronted with such a situation, it is best to tease out the
various dependencies, one by one:
</div><div class="SIntrapara"><ol><li><p>The problem statement specifies how the number of attendees
depends on the ticket price. Computing this number is clearly a separate
task and thus deserves its own function definition: <a name="(idx._(gentag._23))"></a></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/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">attendees</span><span class="hspace">&nbsp;</span><span class="RktSym">ticket-price</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._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktVal">120</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="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">ticket-price</span><span class="hspace">&nbsp;</span><span class="RktVal">5.0</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._%2F%29%29" class="RktValLink" data-pltdoc="x">/</a></span><span class="hspace">&nbsp;</span><span class="RktVal">15</span><span class="hspace">&nbsp;</span><span class="RktVal">0.1</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></li><li><p>The <span style="font-style: italic">revenue</span> is exclusively generated by the sale of
tickets, meaning it is exactly the product of ticket price and number of
attendees: <a name="(idx._(gentag._24))"></a></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/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">revenue</span><span class="hspace">&nbsp;</span><span class="RktSym">ticket-price</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">ticket-price</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">attendees</span><span class="hspace">&nbsp;</span><span class="RktSym">ticket-price</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></li><li><p>The <span style="font-style: italic">cost</span> consists of two parts: a fixed part ($180) and a
variable part that depends on the number of attendees. Given that the
number of attendees is a function of the ticket price, a function for
computing the cost of a show must also consume the ticket price so that it
can reuse the <span class="RktSym">attendees</span> function: <a name="(idx._(gentag._25))"></a></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/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">cost</span><span class="hspace">&nbsp;</span><span class="RktSym">ticket-price</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._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">180</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">0.04</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">attendees</span><span class="hspace">&nbsp;</span><span class="RktSym">ticket-price</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></li><li><p>Finally, <span style="font-style: italic">profit</span> is the difference between revenue and
costs for some given ticket price: <a name="(idx._(gentag._26))"></a></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/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">profit</span><span class="hspace">&nbsp;</span><span class="RktSym">ticket-price</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._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">revenue</span><span class="hspace">&nbsp;</span><span class="RktSym">ticket-price</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">cost</span><span class="hspace">&nbsp;</span><span class="RktSym">ticket-price</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote><p>The BSL definition of <span class="RktSym">profit</span> directly follows the suggestion of
the informal problem description.</p></li></ol></div><div class="SIntrapara">These four functions are all there is to the computation of the
profit, and we can now use the profit function to determine a good ticket
price.</div></p><p><a name="(counter._(exercise._fun6-constant))"></a><span style="font-weight: bold">Exercise</span>&nbsp;27. Our solution to the sample problem contains
several constants in the middle of functions. As <a href="part_prologue.html#%28part._pro-many-def%29" data-pltdoc="x">One Program, Many Definitions</a>
already points out, it is best to give names to such constants so that
future readers understand where these numbers come from. Collect all
definitions in DrRacket&rsquo;s definitions area and change them so that all magic
numbers are refactored into constant definitions. <a href="part_one.html#%28counter._%28exercise._fun6-constant%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._fun6))"></a><span style="font-weight: bold">Exercise</span>&nbsp;28. Determine the potential profit for these ticket prices:
$1, $2, $3, $4, and $5. Which price maximizes the profit of the movie
theater? Determine the best ticket price to a dime. <a href="part_one.html#%28counter._%28exercise._fun6%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara">Here is an alternative version of the same program, given as a single
function definition: <a name="(idx._(gentag._27))"></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">profit</span><span class="hspace">&nbsp;</span><span class="RktSym">price</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._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></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="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">120</span></td></tr><tr><td><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/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/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">15</span><span class="hspace">&nbsp;</span><span class="RktVal">0.1</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;&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">5.0</span><span class="hspace">&nbsp;</span><span class="RktSym">price</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;&nbsp;&nbsp;&nbsp;</span><span class="RktSym">price</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._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktVal">180</span></td></tr><tr><td><span class="hspace">&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._%2A%29%29" class="RktValLink" data-pltdoc="x">*</a></span><span class="hspace">&nbsp;</span><span class="RktVal">0.04</span></td></tr><tr><td><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/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._%2B%29%29" class="RktValLink" data-pltdoc="x
results as the original version for $1, $2, $3, $4, and $5. A single look
should suffice to show how much more difficult it is to comprehend this
one function compared to the above four.</div></p><p><a name="(counter._(exercise._fun7))"></a><span style="font-weight: bold">Exercise</span>&nbsp;29. After studying the costs of a show, the owner
discovered several ways of lowering the cost. As a result of these improvements,
there is no longer a fixed cost; a variable cost of $1.50 per attendee
remains.</p><p>Modify both programs to reflect this change. When the programs are
modified, test them again with ticket prices of $3, $4, and
$5 and compare the results. <a href="part_one.html#%28counter._%28exercise._fun7%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>2.4<tt>&nbsp;</tt><a name="(part._sec~3aglobal)"></a>Global Constants</h4><p><div class="SIntrapara">As the Prologue already says, functions such as <span class="RktSym">profit</span> benefit
from the use of global constants. Every programming language allows
programmers to define constants. In BSL, such a definition has the following shape:
</div><div class="SIntrapara"><ul><li><p>write &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;,</p></li><li><p>write down the name,</p></li><li><p>followed by a space and an expression, and</p></li><li><p>write down &ldquo;)&rdquo;.</p></li></ul></div><div class="SIntrapara">The name of a constant is a <span style="font-style: italic">global variable</span> while the definition
is called a <span style="font-style: italic">constant definition</span>. We tend to call the expression
in a constant definition the <span style="font-style: italic">right-hand side</span> of the definition.</div></p><p><div class="SIntrapara">Constant definitions introduce names for all forms of data:
numbers, images, strings, and so on. Here are some simple examples:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">the current price of a movie ticket:</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">CURRENT-PRICE</span><span class="hspace">&nbsp;</span><span class="RktVal">5</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">useful to compute the area of a disk:</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">ALMOST-PI</span><span class="hspace">&nbsp;</span><span class="RktVal">3.14</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">a blank line:</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">NL</span><span class="hspace">&nbsp;</span><span class="RktVal">"\n"</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">an empty scene:</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">MT</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">100</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">The first two are numeric constants, the last two are a string and
an image. By convention, we use uppercase letters for global constants
because it ensures that no matter how large the program is, the readers of
our programs can easily distinguish such variables from others.</div></p><p><div class="SIntrapara">All functions in a program may refer to these global variables. A reference
to a variable is just like using the corresponding constants. The advantage
of using variable names instead of constants is that a single edit of a
constant definition affects all uses. For example, we may wish to add
digits to <span class="RktSym">ALMOST-PI</span> or enlarge an empty 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/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">ALMOST-PI</span><span class="hspace">&nbsp;</span><span class="RktVal">3.14159</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">an empty scene:</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">MT</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">200</span><span class="hspace">&nbsp;</span><span class="RktVal">800</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p><div class="SIntrapara">Most of our sample definitions employ <span style="font-style: italic">literal constants</span> on the
right-hand side, but the last one uses an expression. And indeed,
a programmer can use arbitrary expressions to compute constants. Suppose a
program needs to deal with an image of some size and its center:
</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">WIDTH</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="RktVal">200</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="RktSym">MID-WIDTH</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="RktSym">WIDTH</span><span class="hspace">&nbsp;</span><span class="RktVal">2</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">MID-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="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">It can use two definitions with literal constants on the right-hand side
and two <span style="font-style: italic">computed constants</span>, that is, variables whose
values are not just literal constants but the results of computing the
value of an expression.</div></p><p>Again, we state an imperative slogan:</p><blockquote><p><span style="font-style: italic">For every constant mentioned in a problem statement, introduce
one constant definition.</span></p></blockquote><p><a name="(counter._(exercise._constant1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;30. Define constants for the price optimization program
at the movie theater so that the price sensitivity of attendance (15 people
for every 10 cents) becomes a computed constant. <a href="part_one.html#%28counter._%28exercise._constant1%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>2.5<tt>&nbsp;</tt><a name="(part._sec~3aprogs)"></a>Programs</h4><p><div class="SIntrapara">You are ready to create simple programs. From a coding perspective, a
program is just a bunch of function and constant definitions. Usually one
function is singled out as the &ldquo;main&rdquo; function, and this main function
tends to compose others. From the perspective of launching a
program, however, there are two distinct kinds:
</div><div class="SIntrapara"><ul><li><p>a <a name="(idx._(gentag._28))"></a><span style="font-style: italic">batch program</span> consumes all of its inputs at once
and computes its result. Its main function is the composition of auxiliary functions,
which may refer to additional auxiliary functions, and so on. When we
launch a batch program, the operating system calls the main function on its
inputs and waits for the program&rsquo;s output.</p></li><li><p>an <a name="(idx._(gentag._29))"></a><span style="font-style: italic">interactive program</span> consumes some of its inputs,
computes, produces some output, consumes more input, and so on. When an
input shows up, we speak of an <span style="font-style: italic">event</span>, and we create interactive
programs as <span style="font-style: italic">event-driven</span> programs. The main function of such an
event-driven program uses an expression to describe which functions to call
for which kinds of events. These functions are called <span style="font-style: italic">event
handlers</span>.</p><p>When we launch an interactive program, the main function informs the
operating system of this description. As soon as input events happen, the
operating system calls the matching event handler. Similarly, the operating
system knows from the description when and how to present the results of
these function calls as output.</p></li></ul></div><div class="SIntrapara">This book focuses mostly on programs that interact via <span style="font-style: italic">graphical
user interfaces (GUI)</span>; there are other kinds of interactive programs, and
you will get to know those as you continue to study computer science.</div></p><p><span style="font-weight: bold">Batch Programs</span> As mentioned, a batch program consumes all of its
inputs at once and computes the result from these inputs. Its main function
expects some arguments, hands them to auxiliary functions, receives results
from those, and composes these results into its own final answer.</p><p>Once programs are created, we want to use them. In DrRacket, we launch batch
programs in the interactions area so that we can watch the program at
work.</p><p>Programs are even more useful if they can retrieve the input from some file
and deliver the output to some other file. Indeed, the name &ldquo;batch program&rdquo; dates
to the early days of computing when a program read a file (or several
files) from a batch of punch cards and placed the result in some other
file(s), also a batch of cards. Conceptually, a batch program reads the
input file(s) at once and also produces the result file(s) all at once.</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/batch-io</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">batch-io</span> from the <span class="stt">Preinstalled HtDP/2e Teachpack</span> menu.</p></blockquote></blockquote></blockquote><p><div class="SIntrapara">We create such file-based batch programs with <span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/batch-io</span></span> teachpack</span>, which adds two
functions to our vocabulary (among others):
</div><div class="SIntrapara"><ul><li><p><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._read-file%29%29" class="RktValLink" data-pltdoc="x">read-file</a></span>, which reads the content of an entire file as a
string, and</p></li><li><p><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span>, which creates a file from a given string.</p></li></ul></div><div class="SIntrapara">These functions<span class="refelem"><span class="refcolumn"><span class="refcontent">Before you evaluate these expressions, save
the definitions area in a file.</span></span></span> write strings to files and
read strings from them:
</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/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"sample.dat"</span><span class="hspace">&nbsp;</span><span class="RktVal">"212"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"sample.dat"</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/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._read-file%29%29" class="RktValLink" data-pltdoc="x">read-file</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"sample.dat"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"212"</span></p></td></tr></table></blockquote></div></p><p><div class="SIntrapara">After the first interaction the file named <span class="RktVal">"sample.dat"</span> contains
</div><div class="SIntrapara"><table cellspacing="0" cellpadding="0" class="SVerbatim"><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="stt">212</span></p></td></tr></table></div><div class="SIntrapara">The result of <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span> is an acknowledgment
that it has placed the string in the file. If the file already exists, it
replaces its content with the given string; otherwise, it creates a file
and makes the given string its content. The second interaction,
<span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._read-file%29%29" class="RktValLink" data-pltdoc="x">read-file</a></span><span class="stt"> </span><span class="RktVal">"sample.dat"</span><span class="RktPn">)</span>, produces <span class="RktVal">"212"</span> because it turns
the content of <span class="RktVal">"sample.dat"</span> into a <a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a>.</div></p><p><div class="SIntrapara">For pragmatic reasons, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span> also accepts
<span class="RktVal">'</span><span class="RktVal">stdout</span>, a special kind of token, as the first argument. It then
displays the resulting file content in the current interactions area, for
example:<span class="refelem"><span class="refcolumn"><span class="refcontent">The names <span class="RktVal">'</span><span class="RktVal">stdout</span> and <span class="RktVal">'</span><span class="RktVal">stdin</span> are
short for standard output device and standard input device, respectively.</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"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span><span class="hspace">&nbsp;</span><span class="RktVal">'</span><span class="RktVal">stdout</span><span class="hspace">&nbsp;</span><span class="RktVal">"212\n"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktOut">212</span></p></td></tr><tr><td><p><span class="RktRes">'stdout</span></p></td></tr></table></blockquote></div><div class="SIntrapara">By analogy, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._read-file%29%29" class="RktValLink" data-pltdoc="x">read-file</a></span> accepts <span class="RktVal">'</span><span class="RktVal">stdin</span> in lieu of a
file name and then reads input from the current interactions area.</div></p><p>Let&rsquo;s illustrate the creation of a batch program with a simple
example. Suppose we wish to create a program that
converts a temperature measured on a Fahrenheit
thermometer into a Celsius temperature. Don&rsquo;t worry, this question isn&rsquo;t a
test about your physics knowledge; here is the conversion
formula:<span class="refelem"><span class="refcolumn"><span class="refcontent">This book is not about memorizing facts, but we do
expect you to know where to find them. Do you know where to find out how
temperatures are converted?</span></span></span></p><blockquote class="SCentered"><p><img src="pict_34.png" alt="image" width="87" height="15"/></p></blockquote><p>Naturally, in this formula <span style="font-style: italic">f</span> is the Fahrenheit temperature and
<span style="font-style: italic">C</span> is the Celsius temperature. While this formula might be good
enough for a pre-algebra textbook, a mathematician or a programmer would
write <span style="font-style: italic">C</span>(<span style="font-style: italic">f</span>)<span style="font-style: italic"></span> on the left side of the equation to remind readers that
<span style="font-style: italic">f</span> is a given value and <span style="font-style: italic">C</span> is computed from <span style="font-style: italic">f</span>.</p><p><div class="SIntrapara">Translating this formula into BSL is straightforward: <a name="(idx._(gentag._30))"></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">C</span><span class="hspace">&nbsp;</span><span class="RktSym">f</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="RktVal">5/9</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">f</span><span class="hspace">&nbsp;</span><span class="RktVal">32</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Recall that <span class="RktVal">5/9</span> is a number, a rational fraction to be precise,
and that <span style="font-style: italic">C</span> depends on the given <span style="font-style: italic">f</span>, which is what the function
notation expresses.</div></p><p><div class="SIntrapara">Launching this batch program in the interactions area works as usual:
</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">C</span><span class="hspace">&nbsp;</span><span class="RktVal">32</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">C</span><span class="hspace">&nbsp;</span><span class="RktVal">212</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">100</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">C</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-4</span>0</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">-40</span></p></td></tr></table></blockquote></div><div class="SIntrapara">But suppose we wish to use this function as part of a program that reads
the Fahrenheit temperature from a file, converts this number into a Celsius
temperature, and then creates another file that contains the result.</div></p><p><div class="SIntrapara">Once we have the conversion formula in BSL, creating the main function
means composing <span class="RktSym">C</span> with existing primitive functions: <a name="(idx._(gentag._31))"></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">convert</span><span class="hspace">&nbsp;</span><span class="RktSym">in</span><span class="hspace">&nbsp;</span><span class="RktSym">out</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/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span><span class="hspace">&nbsp;</span><span class="RktSym">out</span></td></tr><tr><td><span class="hspace">&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-append%29%29" class="RktValLink" data-pltdoc="x">string-append</a></span></td></tr><tr><td><span class="hspace">&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._number-~3estring%29%29" class="RktValLink" data-pltdoc="x">number-&gt;string</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">C</span></td></tr><tr><td><span class="hspace">&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></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/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._read-file%29%29" class="RktValLink" data-pltdoc="x">read-file</a></span><span class="hspace">&nbsp;</span><span class="RktSym">in</span><span class="RktPn">)</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;&nbsp;</span><span class="RktVal">"\n"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">We call the main function <span class="RktSym">convert</span>.
It consumes two file names: <span class="RktSym">in</span> for the file where the Fahrenheit
temperature is found and <span class="RktSym">out</span> for where we want the Celsius
result. A composition of five functions computes <span class="RktSym">convert</span>&rsquo;s result.
Let&rsquo;s step through <span class="RktSym">convert</span>&rsquo;s body carefully:
</div><div class="SIntrapara"><ol><li><p><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._read-file%29%29" class="RktValLink" data-pltdoc="x">read-file</a></span><span class="stt"> </span><span class="RktSym">in</span><span class="RktPn">)</span> retrieves the content of the named file as a string;</p></li><li><p><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> turns this string into a number;</p></li><li><p><span class="RktSym">C</span> interprets the number as a Fahrenheit
temperature and converts it into a Celsius temperature;</p></li><li><p><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> consumes this Celsius temperature and turns
it into a string; and</p></li><li><p><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span><span class="stt"> </span><span class="RktSym">out</span><span class="stt"> </span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span> places this string into the file named <span class="RktSym">out</span>.</p></li></ol></div><div class="SIntrapara">This long list of steps might look overwhelming, and it doesn&rsquo;t even
include the <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> part. Stop! Explain
</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="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"\n"</span><span class="RktPn">)</span></p></blockquote></div></p><p>In contrast, the average function composition in a pre-algebra course
involves two functions, possibly three. Keep in mind, though, that
programs accomplish a real-world purpose while exercises in algebra merely
illustrate the idea of function composition.</p><p><div class="SIntrapara">At this point, we can experiment with <span class="RktSym">convert</span>. To start with, we
use <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span> to create an input file for
<span class="RktSym">convert</span>:<span class="refelem"><span class="refcolumn"><span class="refcontent">You can also create <span class="stt">"sample.dat"</span>
with a file editor.</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"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"sample.dat"</span><span class="hspace">&nbsp;</span><span class="RktVal">"212"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"sample.dat"</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">convert</span><span class="hspace">&nbsp;</span><span class="RktVal">"sample.dat"</span><span class="hspace">&nbsp;</span><span class="RktVal">'</span><span class="RktVal">stdout</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktOut">100</span></p></td></tr><tr><td><p><span class="RktRes">'stdout</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">convert</span><span class="hspace">&nbsp;</span><span class="RktVal">"sample.dat"</span><span class="hspace">&nbsp;</span><span class="RktVal">"out.dat"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"out.dat"</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/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._read-file%29%29" class="RktValLink" data-pltdoc="x">read-file</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"out.dat"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"100"</span></p></td></tr></table></blockquote></div><div class="SIntrapara">For the first interaction, we use <span class="RktVal">'</span><span class="RktVal">stdout</span> so that we can view
what <span class="RktSym">convert</span> outputs in DrRacket&rsquo;s interactions area. For the second
one, <span class="RktSym">convert</span> is given the name <span class="RktVal">"out.dat"</span>. As expected,
the call to <span class="RktSym">convert</span> returns this string; from the description of
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span> we also know that it deposited a Fahrenheit
temperature in the file. Here we read the content of this file with
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._read-file%29%29" class="RktValLink" data-pltdoc="x">read-file</a></span>, but you could also view it with a text editor.</div></p><p><a name="(idx._(gentag._32))"></a></p><p>In addition to running the batch program, it is also instructive to step
through the computation. Make sure that the file <span class="stt">"sample.dat"</span>
exists and contains just a number, then click the <span class="emph">STEP</span> button in
DrRacket. Doing so opens another window in which you can peruse the
computational process that the call to the main function of a batch
program triggers. You will see that the process follows the above outline.</p><p><div class="SIntrapara"><a name="(counter._(exercise._letter-writing))"></a><span style="font-weight: bold">Exercise</span>&nbsp;31. Recall the <span class="RktSym">letter</span> program from
<a href="part_one.html#%28part._sec~3acomposing%29" data-pltdoc="x">Composing Functions</a>. Here is how to launch the program and have it write
its output to the interactions area:
</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/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="hspace">&nbsp;</span><span class="RktVal">'</span><span class="RktVal">stdout</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">letter</span><span class="hspace">&nbsp;</span><span class="RktVal">"Matthew"</span><span class="hspace">&nbsp;</span><span class="RktVal">"Fisler"</span><span class="hspace">&nbsp;</span><span class="RktVal">"Felleisen"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><table cellspacing="0" cellpadding="0"><tr><td><p><span class="RktOut">Dear Matthew,</span></p></td></tr><tr><td><p><span class="RktOut"></span><span class="hspace">&nbsp;</span><span class="RktOut"></span></p></td></tr><tr><td><p><span class="RktOut">We have discovered that all people with the</span></p></td></tr><tr><td><p><span class="RktOut">last name Fisler have won our lottery. So, </span></p></td></tr><tr><td><p><span class="RktOut">Matthew, hurry and pick up your prize.</span></p></td></tr><tr><td><p><span class="RktOut"></span><span class="hspace">&nbsp;</span><span class="RktOut"></span></p></td></tr><tr><td><p><span class="RktOut">Sincerely, </span></p></td></tr><tr><td><p><span class="RktOut"></span></p></td></tr><tr><td><p><span class="RktOut">Felleisen</span></p></td></tr></table></td></tr><tr><td><p><span class="RktRes">'stdout</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Of course, programs are useful because you can launch them for many
different inputs. Run <span class="RktSym">letter</span> on three inputs of your choice.</div></p><p><div class="SIntrapara">Here is a letter-writing batch program that reads names from three files
and writes a letter to one: <a name="(idx._(gentag._33))"></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">main</span><span class="hspace">&nbsp;</span><span class="RktSym">in-fst</span><span class="hspace">&nbsp;</span><span class="RktSym">in-lst</span><span class="hspace">&nbsp;</span><span class="RktSym">in-signature</span><span class="hspace">&nbsp;</span><span class="RktSym">out</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/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._write-file%29%29" class="RktValLink" data-pltdoc="x">write-file</a></span><span class="hspace">&nbsp;</span><span class="RktSym">out</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">letter</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._read-file%29%29" class="RktValLink" data-pltdoc="x">read-file</a></span><span class="hspace">&nbsp;</span><span class="RktSym">in-fst</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;&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/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._read-file%29%29" class="RktValLink" data-pltdoc="x">read-file</a></span><span class="hspace">&nbsp;</span><span class="RktSym">in-lst</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;&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/2htdpbatch-io.html#%28def._%28%28lib._2htdp%2Fbatch-io..rkt%29._read-file%29%29" class="RktValLink" data-pltdoc="x">read-file</a></span><span class="hspace">&nbsp;</span><span class="RktSym">in-signature</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">The function consumes four strings: the first three are the names of input
files and the last one serves as an output file. It uses the first three to
read one string each from the three named files, hands these strings to
<span class="RktSym">letter</span>, and eventually writes the result of this function call
into the file named by <span class="RktSym">out</span>, the fourth argument to <span class="RktSym">main</span>.</div></p><p>Create appropriate files, launch <span class="RktSym">main</span>, and check whether it
delivers the expected letter in a given file. <a href="part_one.html#%28counter._%28exercise._letter-writing%29%29" class="ex-end" data-pltdoc="x"></a></p><p><span style="font-weight: bold">Interactive Programs</span> Batch programs are a staple of business uses of
computers, but the programs people encounter now are interactive. In this
day and age, people mostly interact with desktop applications via a
keyboard and a mouse. Furthermore, interactive programs can also react to
computer-generated events, for example, clock ticks or the arrival of a
message from some other computer.</p><p><a name="(counter._(exercise._cell))"></a><span style="font-weight: bold">Exercise</span>&nbsp;32. Most people no longer use desktop computers just to run
applications but also employ cell phones, tablets, and their cars&rsquo; information control
screen. Soon people will use wearable computers in the form of intelligent
glasses, clothes, and sports gear. In the somewhat more distant future,
people may come with built-in bio computers that directly interact with
body functions. Think of ten different forms of events that software
applications on such computers will have to deal with. <a href="part_one.html#%28counter._%28exercise._cell%29%29" class="ex-end" data-pltdoc="x"></a></p><p>The purpose of this section is to introduce the mechanics of writing
<span style="font-weight: bold">interactive</span> BSL programs. Because many of the project-style examples in
this book are interactive programs, we introduce the ideas slowly and
carefully. You may wish to return to this section when you tackle some of
the interactive programming projects; a second or third reading may
clarify some of the advanced aspects of the mechanics.</p><p>By itself, a raw computer is a useless piece of physical equipment. It is
called <span style="font-style: italic">hardware</span> because you can touch it. This equipment becomes
useful once you install <span style="font-style: italic">software</span>, that is, a suite of programs.
Usually the first piece of software to be installed on a computer is an
<span style="font-style: italic">operating system</span>. It has the task of managing the computer for
you, including connected devices such as the monitor, the keyboard, the
mouse, the speakers, and so on. The way it works is that when a user
presses a key on the keyboard, the operating system runs a function that
processes keystrokes. We say that the keystroke is a <span style="font-style: italic">key
event</span>, and the function is an <span style="font-style: italic">event handler</span>. In the same vein,
the operating system runs an event handler for clock
ticks, for mouse actions, and so on. Conversely, after an event handler is
done with its work, the operating system may have to change the image on
the screen, ring a bell, print a document, or perform a similar action. To accomplish these tasks,
it also runs functions that translate the operating system&rsquo;s data into
sounds, images, actions on the printer, and so on.</p><p>Naturally, different programs have different needs. One program may
interpret keystrokes as signals to control a nuclear reactor; another
passes them to a word processor. To make a general-purpose computer work
on these radically different tasks, different programs install different
event handlers. That is, a rocket-launching program uses one kind of
function to deal with clock ticks while an oven&rsquo;s software uses a
different kind.</p><p>Designing an interactive program requires a way to designate some function
as the one that takes care of keyboard events, another function for
dealing with clock ticks, a third one for presenting some data as an image,
and so forth. It is the task of an interactive program&rsquo;s main function to
communicate these designations to the operating system, that is, the
software platform on which the program is launched.</p><p>DrRacket is a small operating system, and BSL is one of its programming
languages. The latter comes with <span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/universe</span></span> teachpack</span>, which provides
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>, a mechanism for telling the operating system which
function deals with which event. In addition, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> keeps
track of the <span style="font-style: italic">state of the program</span>. To this end, it comes with
one required sub-expression, whose value becomes the <span style="font-style: italic">initial
state</span> of the program. Otherwise <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> consists of one
required clause and many optional clauses. The required <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span>
clause tells DrRacket how to render the state of the program, including the
initial one. Each of the other, optional clauses tells the
operating system that a certain function takes care of a certain
event. Taking care of an event in BSL means that the function consumes
the state of the program and a description of the event, and that it
produces the next state of the program. We therefore speak of the
<span style="font-style: italic">current state</span> of the program.</p><p><span style="font-weight: bold">Terminology</span> In a sense, a <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression describes how
a program connects with a small segment of the world. This world might be
a game that the program&rsquo;s users play, an animation that the user watches,
or a text editor that the user employs to manipulate some notes.
Programming language researchers therefore often say that
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> is a description of a small world: its initial state,
how states are transformed, how states are rendered, and how
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> may determine other attributes of the current state. In
this spirit, we also speak of the <span style="font-style: italic">state of the world</span> and even
call <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> programs <span style="font-style: italic">world programs</span>. <span style="font-weight: bold">End</span></p><p><div class="SIntrapara">Let&rsquo;s study this idea step-by-step, starting with this definition:<a name="(idx._(gentag._34))"></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">number-&gt;square</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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._square%29%29" class="RktValLink" data-pltdoc="x">square</a></span><span class="hspace">&nbsp;</span><span class="RktSym">s</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></blockquote></div><div class="SIntrapara">The function consumes a positive number and produces a solid red square of
that size. After clicking <span class="emph">RUN</span>, experiment with the function,
like this:</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">number-&gt;square</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_35.png" alt="image" width="12" height="12"/></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">number-&gt;square</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_36.png" alt="image" width="17" height="17"/></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">number-&gt;square</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_37.png" alt="image" width="27" height="27"/></p></td></tr></table></blockquote></div><div class="SIntrapara">It behaves like a batch program, consuming a number and producing an
image, which DrRacket renders for you.</div></p><p><div class="SIntrapara">Now try the following <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression in the interactions
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/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace">&nbsp;</span><span class="RktSym">number-&gt;square</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">A separate window appears, and it displays a <img src="pict_38.png" alt="image" width="51" height="9"/> red
square. In addition, the DrRacket interactions area does not display another
prompt; it is as if the program keeps running, and this is indeed the
case. To stop the program, click on DrRacket&rsquo;s <span class="emph">STOP</span> button or the
window&rsquo;s <span class="emph">CLOSE</span> button:
</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace">&nbsp;</span><span class="RktSym">number-&gt;square</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">100</span></p></td></tr></table></blockquote></div><div class="SIntrapara">When DrRacket stops the evaluation of a <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression, it
returns the current state, which in this case is just the initial state:
<span class="RktVal">100</span>.</div></p><p><div class="SIntrapara">Here is a more interesting <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression:
</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace">&nbsp;</span><span class="RktSym">number-&gt;square</span><span class="RktPn">]</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</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._sub1%29%29" class="RktValLink" data-pltdoc="x">sub1</a></span><span class="RktPn">]</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</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._zero~3f%29%29" class="RktValLink" data-pltdoc="x">zero?</a></span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">This <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression adds two optional clauses to the
previous one: the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span> clause tells DrRacket how to deal with
clock ticks and the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</a></span> clause says when to stop the
program. We read it as follows, starting with <span class="RktVal">100</span> as the initial
state:
</div><div class="SIntrapara"><ol><li><p>every time the clock ticks, subtract <span class="RktVal">1</span> from the current state;</p></li><li><p>then check whether <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._zero~3f%29%29" class="RktValLink" data-pltdoc="x">zero?</a></span> is true of the new state and if so,
stop; and</p></li><li><p>every time an event handler returns a value, use
<span class="RktSym">number-&gt;square</span> to render it as an image.</p></li></ol></div><div class="SIntrapara">Now hit the &ldquo;return&rdquo; key and observe what happens. Eventually the evaluation
of the expressions terminates and DrRacket displays <span class="RktVal">0</span>.</div></p><p><div class="SIntrapara">The <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression keeps track of the current
state. Initially this state is <span class="RktVal">100</span>. Every time the clock ticks, it
calls the clock-tick handler and gets a new state. Hence, the state of
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> changes as follows:
</div><div class="SIntrapara"><blockquote class="SCentered"><p><span class="RktVal">100</span>, <span class="RktVal">99</span>, <span class="RktVal">98</span>, ..., <span class="RktVal">2</span>, <span class="RktVal">1</span>, <span class="RktVal">0</span></p></blockquote></div><div class="SIntrapara">When the state&rsquo;s value becomes <span class="RktVal">0</span>, the evaluation is done. For
every other state&#8212;<wbr></wbr>from <span class="RktVal">100</span> to <span class="RktVal">1</span>&#8212;<wbr></wbr><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>
translates the state into an image, using <span class="RktSym">number-&gt;square</span> as the
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span> clause tells it to. Hence, the window displays a red
square that shrinks from <img src="pict_39.png" alt="image" width="51" height="9"/> pixels to <img src="pict_40.png" alt="image" width="26" height="8"/> pixel over 100 clock
ticks.</div></p><p><div class="SIntrapara">Let&rsquo;s add a clause for dealing with key events. First, we need a function
that consumes the current state and a string that describes
the key event and then returns a new state: <a name="(idx._(gentag._35))"></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">reset</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktSym">ke</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktVal">100</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">This function throws away its arguments and returns <span class="RktVal">100</span>, which is
the initial state of the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression we wish to
modify. Second, we add an <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-key%29%29" class="RktStxLink" data-pltdoc="x">on-key</a></span> clause to the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>
expression:
</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktVal">100</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace">&nbsp;</span><span class="RktSym">number-&gt;square</span><span class="RktPn">]</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</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._sub1%29%29" class="RktValLink" data-pltdoc="x">sub1</a></span><span class="RktPn">]</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</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._zero~3f%29%29" class="RktValLink" data-pltdoc="x">zero?</a></span><span class="RktPn">]</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-key%29%29" class="RktStxLink" data-pltdoc="x">on-key</a></span><span class="hspace">&nbsp;</span><span class="RktSym">reset</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">Stop! Explain what happens when you hit &ldquo;return&rdquo;, count to 10,
and finally press <span class="RktVal">"a"</span>.</div></p><p>What you will see is that the red square shrinks at the rate of one pixel
per clock tick. As soon as you press the <span class="RktVal">"a"</span> key, though, the red
square reinflates to full size because <span class="RktSym">reset</span> is called on the
current length of the square and <span class="RktVal">"a"</span> and returns
<span class="RktVal">100</span>. This number becomes <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>&rsquo;s new state and
<span class="RktSym">number-&gt;square</span> renders it as a full-sized red square.</p><p>In order to understand the evaluation of <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expressions in
general, let&rsquo;s look at a schematic version:</p><p><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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktSym">cw0</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace">&nbsp;</span><span class="RktSym">tock</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-key%29%29" class="RktStxLink" data-pltdoc="x">on-key</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ke-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/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-mouse%29%29" class="RktStxLink" data-pltdoc="x">on-mouse</a></span><span class="hspace">&nbsp;</span><span class="RktSym">me-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/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace">&nbsp;</span><span class="RktSym">render</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</a></span><span class="hspace">&nbsp;</span><span class="RktSym">end?</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">This <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression specifies three event
handlers&#8212;<wbr></wbr><span class="RktSym">tock</span>, <span class="RktSym">ke-h</span>, and <span class="RktSym">me-h</span>&#8212;<wbr></wbr>and a
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</a></span> clause.</div></p><p>The evaluation of this <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression starts with
<span class="RktSym">cw0</span>, which is usually an expression. DrRacket, our operating system,
installs the value of <span class="RktSym">cw0</span> as the current state. It uses
<span class="RktSym">render</span> to translate the current state into an image, which is
then displayed in a separate window. Indeed, <span class="RktSym">render</span> is the
<span style="font-weight: bold">only</span> means for a <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression to present data
to the world.</p><p><div class="SIntrapara">Here is how events are processed:
</div><div class="SIntrapara"><ul><li><p>Every time the clock ticks, DrRacket applies <span class="RktSym">tock</span> to
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>&rsquo;s current state and receives a value in response;
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> treats this return value as the next current state.</p></li><li><p>Every time a key is pressed, DrRacket applies <span class="RktSym">ke-h</span> to
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>&rsquo;s current state and a string that represents the key;
for example, pressing the &ldquo;a&rdquo; key is represented with <span class="RktVal">"a"</span> and
the left arrow key with <span class="RktVal">"left"</span>. When <span class="RktSym">ke-h</span> returns a
value, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> treats it as the next current state.</p></li><li><p>Every time a mouse enters the window, leaves it, moves, or is
clicked, DrRacket applies <span class="RktSym">me-h</span> to <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>&rsquo;s current state,
the event&rsquo;s x- and y-coordinates, and a string that
represents the kind of mouse event that happened; for example, clicking a
mouse&rsquo;s button is represented with <span class="RktVal">"button-down"</span>. When
<span class="RktSym">me-h</span> returns a value, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> treats it as the next
current state.</p></li></ul></div><div class="SIntrapara">All events are processed in order; if two events seem to happen at the
same time, DrRacket acts as a tie-breaker and arranges them in some order.</div></p><p><div class="SIntrapara">After an event is processed, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> uses both <span class="RktSym">end?</span>
and <span class="RktSym">render</span> to check the current state:
</div><div class="SIntrapara"><ul><li><p><span class="RktPn">(</span><span class="RktSym">end?</span><span class="stt"> </span><span class="RktSym">cw</span><span class="RktPn">)</span> produces a Boolean value. If it is #<span class="RktVal">#true</span>,
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> stops the computation immediately. Otherwise it
proceeds.</p></li><li><p><span class="RktPn">(</span><span class="RktSym">render</span><span class="stt"> </span><span class="RktSym">cw</span><span class="RktPn">)</span> is expected to produce an image and
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> displays this image in a separate window.</p></li></ul></div></p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><blockquote class="SCentered"><table cellspacing="0" cellpadding="0" style="border-collapse: collapse;"><tr><td align="left"><p>current state</p></td><td align="left"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="left"><p><span style="font-style: italic">cw</span><span style="font-style: italic"></span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span></p></td><td align="left"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="left"><p><span style="font-style: italic">cw</span><span style="font-style: italic"></span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>1<span style="font-style: italic"></span></span><span style="font-style: italic"></span></p></td><td align="left"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="left"><p>...</p></td></tr><tr><td align="left" style="border-bottom: 1px solid black;"><p>event</p></td><td align="left" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="left" style="border-bottom: 1px solid black;"><p><span style="font-style: italic">e</span><span style="font-style: italic"></span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span></p></td><td align="left" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="left" style="border-bottom: 1px solid black;"><p><span style="font-style: italic">e</span><span style="font-style: italic"></span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>1<span style="font-style: italic"></span></span><span style="font-style: italic"></span></p></td><td align="left" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="left" style="border-bottom: 1px solid black;"><p>...</p></td></tr><tr><td align="left"><p>on clock tick</p></td><td align="left"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="left"><p><span class="RktPn">(</span><span class="RktSym">tock</span><span class="stt"> </span><span style="font-style: italic">cw</span><span style="font-style: italic"></span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span><span class="RktPn">)</span></p></td><td align="left"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="left"><p><span class="RktPn">(</span><span class="RktSym">tock</span><span class="stt"> </span><span style="font-style: italic">cw</span><span style="font-style: italic"></span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>1<span style="font-style: italic"></span></span><span style="font-style: italic"></span><span class="RktPn">)</span></p></td><td align="left"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="left"><p>...</p></td></tr><tr><td align="left"><p>on keystroke</p></td><td align="left"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="left"><p><span class="RktPn">(</span><span class="RktSym">ke-h</span><span class="stt"> </span><span style="font-style: italic">cw</span><span style="font-style: italic"></span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span><span class="stt"> </span><span style="
In the first row, it lists names for the current states. The second row
enumerates names for the events that DrRacket encounters: <span style="font-style: italic">e</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span>,
<span style="font-style: italic">e</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>1<span style="font-style: italic"></span></span><span style="font-style: italic"></span>, and so on. Each <span style="font-style: italic">e</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic">i</span></span><span style="font-style: italic"></span> might be a clock tick, a key press,
or a mouse event. The next three rows specify the result of dealing with
the event:
</div><div class="SIntrapara"><ul><li><p>If <span style="font-style: italic">e</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span> is a clock tick, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> evaluates
<span class="RktPn">(</span><span class="RktSym">tock</span><span class="stt"> </span><span style="font-style: italic">cw</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span><span class="RktPn">)</span> to produce <span style="font-style: italic">cw</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>1<span style="font-style: italic"></span></span><span style="font-style: italic"></span>.</p></li><li><p>If <span style="font-style: italic">e</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span> is a key event, <span class="RktPn">(</span><span class="RktSym">ke-h</span><span class="stt"> </span><span style="font-style: italic">cw</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span><span class="stt"> </span><span style="font-style: italic">e</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span><span class="RktPn">)</span>
is evaluated and yields <span style="font-style: italic">cw</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>1<span style="font-style: italic"></span></span><span style="font-style: italic"></span>. The handler must be applied to the
event itself because, in general, programs are going to react to each key
differently.</p></li><li><p>If <span style="font-style: italic">e</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span> is a mouse event, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> runs
<span class="RktPn">(</span><span class="RktSym">me-h</span><span class="stt"> </span><span style="font-style: italic">cw</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span><span class="stt"> </span><span style="font-style: italic">e</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span><span class="stt"> </span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span> to get
<span style="font-style: italic">cw</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>1<span style="font-style: italic"></span></span><span style="font-style: italic"></span>. The call is a sketch because a mouse event <span style="font-style: italic">e</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>0<span style="font-style: italic"></span></span><span style="font-style: italic"></span> is really associated with
several pieces of data&#8212;<wbr></wbr>its nature and its coordinates&#8212;<wbr></wbr>and we just wish
to indicate that much.</p></li><li><p>Finally, <span class="RktSym">render</span> turns the current state into an image, which
is indicated by the last row. DrRacket displays these images in the separate
window.</p></li></ul></div><div class="SIntrapara">The column below <span style="font-style: italic">cw</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>1<span style="font-style: italic"></span></span><span style="font-style: italic"></span> shows how <span style="font-style: italic">cw</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>2<span style="font-style: italic"></span></span><span style="font-style: italic"></span> is
generated, depending on what kind of event <span style="font-style: italic">e</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic"></span>1<span style="font-style: italic"></span></span><span style="font-style: italic"></span> takes place.</div></p><p><div class="SIntrapara">Let&rsquo;s interpret this table with the specific sequence of events:
the user presses the &ldquo;a&rdquo; key, then the clock ticks, and finally the user
clicks the mouse to trigger a &ldquo;button down&rdquo; event at position
<span style="font-style: italic"></span>(<span style="font-style: italic"></span>9<span style="font-style: italic"></span>0<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>)<span style="font-style: italic"></span>. Then, in Racket notation,
</div><div class="SIntrapara"><ol><li><p><span class="RktSym">cw1</span> is the result of <span class="RktPn">(</span><span class="RktSym">ke-h</span><span class="stt"> </span><span class="RktSym">cw0</span><span class="stt"> </span><span class="RktVal">"a"</span><span class="RktPn">)</span>;</p></li><li><p><span class="RktSym">cw2</span> is the result of <span class="RktPn">(</span><span class="RktSym">tock</span><span class="stt"> </span><span class="RktSym">cw1</span><span class="RktPn">)</span>; and</p></li><li><p><span class="RktSym">cw3</span> is the result of <span class="RktPn">(</span><span class="RktSym">me-h</span><span class="stt"> </span><span class="RktSym">cw2</span><span class="stt"> </span><span class="RktVal">90</span><span class="stt"> </span><span class="RktVal">100</span><span class="stt"> </span><span class="RktVal">"button-down"</span><span class="RktPn">)</span>.</p></li></ol></div><div class="SIntrapara">We can actually express these three steps as a sequence of three definitions:
</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">cw1</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ke-h</span><span class="hspace">&nbsp;</span><span class="RktSym">cw0</span><span class="hspace">&nbsp;</span><span class="RktVal">"a"</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">cw2</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tock</span><span class="hspace">&nbsp;</span><span class="RktSym">cw1</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">cw3</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">me-h</span><span class="hspace">&nbsp;</span><span class="RktSym">cw2</span><span class="hspace">&nbsp;</span><span class="RktVal">90</span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">"button-down"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Stop! How does <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> display each of these three states?</div></p><p><div class="SIntrapara">Now let&rsquo;s consider a sequence of three clock ticks. In that case,
</div><div class="SIntrapara"><ol><li><p><span class="RktSym">cw1</span> is the result of <span class="RktPn">(</span><span class="RktSym">tock</span><span class="stt"> </span><span class="RktSym">cw0</span><span class="RktPn">)</span>;</p></li><li><p><span class="RktSym">cw2</span> is the result of <span class="RktPn">(</span><span class="RktSym">tock</span><span class="stt"> </span><span class="RktSym">cw1</span><span class="RktPn">)</span>; and</p></li><li><p><span class="RktSym">cw3</span> is the result of <span class="RktPn">(</span><span class="RktSym">tock</span><span class="stt"> </span><span class="RktSym">cw2</span><span class="RktPn">)</span>.</p></li></ol></div><div class="SIntrapara">Or, reformulated in BSL:
</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">cw1</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tock</span><span class="hspace">&nbsp;</span><span class="RktSym">cw0</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">cw2</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tock</span><span class="hspace">&nbsp;</span><span class="RktSym">cw1</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">cw3</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tock</span><span class="hspace">&nbsp;</span><span class="RktSym">cw2</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Indeed, we can also determine <span class="RktSym">cw3</span> via a single expression:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym">tock</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tock</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tock</span><span class="hspace">&nbsp;</span><span class="RktSym">cw0</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">This determines the state that <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> computes after
three clock ticks. Stop! Reformulate the first sequence of events as
an expression. <a name="(idx._(gentag._36))"></a> <a name="(idx._(gentag._37))"></a> <a name="(idx._(gentag._38))"></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="RktSym">BACKGROUND</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">100</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">DOT</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">3</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><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">main</span><span class="hspace">&nbsp;</span><span class="RktSym">y</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktSym">y</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</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._sub1%29%29" class="RktValLink" data-pltdoc="x">sub1</a></span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</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._zero~3f%29%29" class="RktValLink" data-pltdoc="x">zero?</a></span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> conceptually traverses the above table of possible
states to arrive at the current state for each time slot. Of course,
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> does not touch the current state; it merely safeguards
it and passes it to event handlers and other functions when needed.</p><p>From here, it is straightforward to define a first interactive program. See
<a href="part_one.html#%28counter._%28figure._fig~3ainteractive%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">14</span></a>. The program consists of two constant
definitions followed by three function definitions: <span class="RktSym">main</span>, which
launches a <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> interactive program; <span class="RktSym">place-dot-at</span>,
which translates the current state into an image; and <span class="RktSym">stop</span>, which
throws away its inputs and produces <span class="RktVal">0</span>.</p><p><div class="SIntrapara">After clicking <span class="emph">RUN</span>, we can ask DrRacket to evaluate applications of
these handler functions. This is one way to confirm their workings:
</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">place-dot-at</span><span class="hspace">&nbsp;</span><span class="RktVal">89</span><span class="RktPn">)</span></td></tr><tr><td><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_41.png" alt="image" width="107" height="107"/></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">stop</span><span class="hspace">&nbsp;</span><span class="RktVal">89</span><span class="hspace">&nbsp;</span><span class="RktVal">"q"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">0</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Stop! Try now to understand how <span class="RktSym">main</span> reacts when you press a
key.</div></p><p><div class="SIntrapara">One way to find out whether your conjecture is correct is to launch
the <span class="RktSym">main</span> function on some reasonable number:
</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">main</span><span class="hspace">&nbsp;</span><span class="RktVal">90</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p><table cellspacing="0" cellpadding="0"><tr><td><p>&#160;</p></td></tr><tr><td><p>Relax.</p></td></tr><tr><td><p>&#160;</p></td></tr></table></p><p>By now, you may feel that these first two chapters are overwhelming. They
introduce many new concepts, including a new language, its vocabulary, its
meaning, its idioms, a tool for writing down texts in this vocabulary, and
a way of running these programs. Confronted with this plethora of ideas,
you may wonder how one creates a program when presented with a problem
statement. To answer this central question, the next chapter takes a step
back and explicitly addresses the systematic design of programs. So take a
breather and continue when ready.</p><h3>3<tt>&nbsp;</tt><a name="(part._ch~3ahtdp)"></a>How to Design Programs</h3><p>The first few chapters of this book show that learning to program requires
some mastery of many concepts. On the one hand, programming needs a
language, a notation for communicating what we wish to compute. The
languages for formulating programs are artificial constructions, though
acquiring a programming language shares some elements with acquiring a
natural language. Both come with vocabulary, grammar, and an understanding
of what &ldquo;phrases&rdquo; mean.</p><p>On the other hand, it is critical to learn how to get from a problem
statement to a program. We need to determine what is relevant in the
problem statement and what can be ignored. We need to tease out what the
program consumes, what it produces, and how it relates inputs to
outputs. We have to know, or find out, whether the chosen language and its
teachpacks provide certain basic operations for the data that our program is
to process. If not, we might have to develop auxiliary functions that
implement these operations. Finally, once we have a program, we must check
whether it actually performs the intended computation. And this might
reveal all kinds of errors, which we need to be able to understand and fix.</p><p>All this sounds rather complex, and you might wonder why we don&rsquo;t just
muddle our way through, experimenting here and there, leaving well enough
alone when the results look decent. This approach to programming, often
dubbed &ldquo;garage programming,&rdquo; is common and succeeds on many occasions;
sometimes it is the launching pad for a start-up company. Nevertheless, the
start-up cannot sell the results of the &ldquo;garage effort&rdquo; because only the
original programmers and their friends can use them.</p><p>A good program comes with a short write-up that explains what it does, what
inputs it expects, and what it produces. Ideally, it also comes with some
assurance that it actually works. In the best circumstances, the program&rsquo;s
connection to the problem statement is evident so that a small change to
the problem statement is easy to translate into a small change to the
program. Software engineers call this a &ldquo;programming product.&rdquo;</p><p>All this extra work is necessary because programmers don&rsquo;t create programs
for themselves. Programmers write programs for other programmers to read,
and on occasion, people run these programs to get work
done.<span class="refelem"><span class="refcolumn"><span class="refcontent">The word &ldquo;other&rdquo; also includes older versions of the
programmer who usually cannot recall all the thinking that the younger
version put into the production of the program.</span></span></span> Most programs are large,
complex collections of collaborating functions, and nobody can write all
these functions in a day. Programmers join projects, write code, leave
projects; others take over their programs and work on them. Another
difficulty is that the programmer&rsquo;s clients tend to change their mind
about what problem they really want solved. They usually have it almost
right, but more often than not, they get some details wrong. Worse,
complex logical constructions such as programs almost always suffer from
human errors; in short, programmers make mistakes. Eventually someone
discovers these errors and programmers must fix them. They need to reread
the programs from a month ago, a year ago, or twenty years ago and change
them.</p><p><a name="(counter._(exercise._y2k))"></a><span style="font-weight: bold">Exercise</span>&nbsp;33. Research the &ldquo;year 2000&rdquo; problem. <a href="part_one.html#%28counter._%28exercise._y2k%29%29" class="ex-end" data-pltdoc="x"></a></p><p>Here we present a <a name="(idx._(gentag._39))"></a>design recipe that integrates a step-by-step
<a name="(idx._(gentag._40))"></a>process with a way of organizing programs around problem data. For the
readers who don&rsquo;t like to stare at blank screens for a long time, this
design recipe offers a way to make progress in a systematic manner. For
those of you who teach others to design programs, the recipe is a device
for diagnosing a novice&rsquo;s difficulties. For others, our recipe might be
something that they can apply to other areas&#8212;<wbr></wbr>say, medicine, journalism, or
engineering. For those who wish to become real programmers, the design
recipe also offers a way to understand and work on existing
programs&#8212;<wbr></wbr>though not all programmers use a method like this design recipe
to come up with programs. The rest of this chapter is dedicated to the
first baby steps into the world of the design recipe; the following
chapters and parts refine and expand the recipe in one way or another.</p><h4>3.1<tt>&nbsp;</tt><a name="(part._sec~3adesign-func)"></a>Designing Functions</h4><p><span style="font-weight: bold">Information and Data</span> The <a name="(idx._(gentag._41))"></a>purpose of a program is to describe a
<a name="(idx._(gentag._42))"></a>computational process that consumes some information and produces new
information. In this sense, a program is like the instructions a
mathematics teacher gives to grade school students. Unlike a student,
however, a program works with more than numbers: it calculates with
navigation information, looks up a person&rsquo;s address, turns on switches, or
inspects the state of a video game. All this information comes from a part
of the real world&#8212;<wbr></wbr>often called the program&rsquo;s <span style="font-style: italic">domain</span>&#8212;<wbr></wbr>and the
results of a program&rsquo;s computation represent more information in this
domain.</p><p><span style="font-style: italic">Information</span> plays a central role in our description. Think of
information as facts about the program&rsquo;s domain. For a program that deals
with a furniture catalog, a &ldquo;table with five legs&rdquo; or a &ldquo;square table
of two by two meters&rdquo; are pieces of information. A game program deals
with a different kind of domain, where &ldquo;five&rdquo; might refer to the number
of pixels per clock tick that some object travels on its way from one
part of the canvas to another. Or, a payroll program is likely to deal
with &ldquo;five deductions.&rdquo;</p><p>For a program to process information, it must turn it into some form of
<span style="font-style: italic">data</span> in the programming language; then it processes the data;
and once it is finished, it turns the resulting data into information
again. An interactive program may even intermingle these steps, acquiring
more information from the world as needed and delivering information in
between.</p><p>We use BSL and DrRacket so that you do <span style="font-weight: bold">not</span> have to worry about the
translation of information into data. In DrRacket&rsquo;s BSL you can apply a
function directly to data and observe what it produces. As a result, we
avoid the serious chicken-and-egg problem of writing functions that
convert information into data and vice versa. For simple kinds of
information, designing such program pieces is trivial; for anything other
than simple information, you need to know about parsing, for example, and
<span style="font-weight: bold">that</span> immediately requires a lot of expertise in program design.</p><p>Software engineers use the slogan <span style="font-style: italic">model-view-controller</span> (MVC) for
the way BSL and DrRacket separate data processing from parsing information
into data and turning data into information. Indeed, it is now
accepted wisdom that well-engineered software systems enforce this
separation, even though most introductory books still commingle
them. Thus, working with BSL and DrRacket allows you to focus on the design of
the core of programs, and, when you have enough experience with that, you
can learn to design the information-data translation parts.</p><p>Here we use two preinstalled teachpacks to demonstrate the
separation of data and information: <span class="stt">2htdp/batch-io</span> and
<span class="stt">2htdp/universe</span>. Starting with this chapter, we develop design recipes
for <a name="(idx._(gentag._43))"></a><span style="font-weight: bold">batch</span> and
<a name="(idx._(gentag._44))"></a><span style="font-weight: bold">interactive</span> programs
to give you an idea of how complete programs are designed. Do keep in mind
that the teachpacks of full-fledged programming languages offer many more
contexts for complete programs, and that you will need to adapt the design
recipes appropriately.</p><p><a name="(idx._(gentag._45))"></a>
<a name="(idx._(gentag._46))"></a>
<a name="(idx._(gentag._47))"></a></p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><blockquote class="SCentered"><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_42.png" alt="image" width="269.4677734375" height="172.078125"/></p></blockquote></blockquote></blockquote><p class="Centertext"><span class="Legend"><span class="FigureTarget"><a name="(counter._(figure._fig~3adata-info))" x-target-lift="Figure"></a>Figure&nbsp;15: </span>From information to data, and back</span></p></blockquote><p>Given the central role of information and data, program design must start
with the connection between them. Specifically, we, the programmers, must
decide how to use our chosen programming language to <span style="font-style: italic">represent</span>
the relevant pieces of information as data and how we should
<span style="font-style: italic">interpret</span> data as information. <a href="part_one.html#%28counter._%28figure._fig~3adata-info%29%29" data-pltdoc="x">Figure&nbsp;<span class="FigureRef">15</span></a>
explains this idea with an abstract diagram.</p><p><div class="SIntrapara">To make this idea concrete, let&rsquo;s work through some examples. Suppose you
are designing a program that consumes and produces information in the form
of numbers. While choosing a representation is easy, an interpretation
requires explaining what a number such as <span class="RktVal">42</span> denotes in the
domain:
</div><div class="SIntrapara"><ul><li><p><span class="RktVal">42</span> may refer to the number of pixels from the top margin in
the domain of images;</p></li><li><p><span class="RktVal">42</span> may denote the number of pixels per clock tick that a
simulation or game object moves;</p></li><li><p><span class="RktVal">42</span> may mean a temperature, on the Fahrenheit, Celsius,
or Kelvin scale for the domain of physics;</p></li><li><p><span class="RktVal">42</span> may specify the size of some table if the domain of the
program is a furniture catalog; or</p></li><li><p><span class="RktVal">42</span> could just count the number of characters in a string.</p></li></ul></div><div class="SIntrapara">The key is to know how to go from numbers as information to numbers as data
and vice versa.</div></p><p>Since this knowledge is so important for everyone who reads the program, we
often write it down in the form of comments, which we call <span style="font-style: italic">data
definitions</span>. A data definition serves two purposes. First, it names a
collection of data&#8212;<wbr></wbr>a <span style="font-style: italic">class</span>&#8212;<wbr></wbr>using a meaningful
word. Second,<span class="refelem"><span class="refcolumn"><span class="refcontent">Computing scientists use &ldquo;class&rdquo; to mean
something like a
&ldquo;mathematical set.&rdquo;</span></span></span> it informs readers how to create elements of this
class and how to decide whether some arbitrary piece of data belongs to
the collection.</p><p><div class="SIntrapara">Here is a data definition for one of the above examples:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._temperature)"></a><span style="font-style: italic">Temperature</span><span class="RktCmt"> is a </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt">. </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> represents Celsius degrees</span></td></tr></table></blockquote></div><div class="SIntrapara">The first line introduces the name of the data collection,
<a href="part_one.html#%28tech._temperature%29" class="techoutside" data-pltdoc="x"><span class="techinside">Temperature</span></a>, and tells us that the class consists of all
<a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a>s. So, for example, if we ask whether
<span class="RktVal">102</span> is a temperature, you can respond with &ldquo;yes&rdquo; because
<span class="RktVal">102</span> is a number and all numbers are temperatures. Similarly,
if we ask whether <span class="RktVal">"cold"</span> is a <a href="part_one.html#%28tech._temperature%29" class="techoutside" data-pltdoc="x"><span class="techinside">Temperature</span></a>, you will
say &ldquo;no&rdquo; because no string belongs to <a href="part_one.html#%28tech._temperature%29" class="techoutside" data-pltdoc="x"><span class="techinside">Temperature</span></a>. And, if we
asked you to make up a sample <a href="part_one.html#%28tech._temperature%29" class="techoutside" data-pltdoc="x"><span class="techinside">Temperature</span></a>, you might come up
with something like <span class="RktVal"><span class="nobreak">-4</span>00</span>.</div></p><p>If you happen to know that the lowest possible temperature is approximately
<img src="pict_43.png" alt="image" width="32" height="10"/> C, you may wonder whether it is possible to express this knowledge in
a data definition. Since our data definitions are really just English
descriptions of classes, you may indeed define the class of temperatures
in a much more accurate manner than shown here. In this book, we use a
stylized form of English for such data definitions, and the next chapter
introduces the style for imposing constraints such as &ldquo;larger than
<span class="RktVal"><span class="nobreak">-2</span>74</span>.&rdquo;</p><p>So far, you have encountered the names of four classes of data:
<a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a>, <a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a>, <a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a>, and <a href="part_one.html#%28tech._boolean%29" class="techoutside" data-pltdoc="x"><span class="techinside">Boolean</span></a>. With that,
formulating a new data definition means nothing more than introducing a
new name for an existing form of data, say, &ldquo;temperature&rdquo; for
numbers. Even this limited knowledge, though, suffices to explain the
outline of our <a name="(idx._(gentag._48))"></a>design process.</p><p><div class="SIntrapara"><span style="font-weight: bold">The Design Process</span> Once you understand how to represent input
information<span class="refelem"><span class="refcolumn"><span class="refcontent">At this point, you may wish to reread the section
on <a href="part_preface.html#%28part._sec~3asystematic-design%29" data-pltdoc="x">Systematic Program Design</a> in the
Preface, especially <a href="part_preface.html#%28counter._%28figure._fig~3athe-design-recipe%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">1</span></a>.</span></span></span>
as data and to interpret output data as information, the design of an
individual <a name="(idx._(gentag._49))"></a>function proceeds according to a straightforward process:
</div><div class="SIntrapara"><ol><li><p><div class="SIntrapara">Express how you wish to represent information as data. A
one-line comment suffices:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">We use numbers to represent centimeters. </span></p></blockquote></div><div class="SIntrapara">Formulate data definitions, like the one for <a href="part_one.html#%28tech._temperature%29" class="techoutside" data-pltdoc="x"><span class="techinside">Temperature</span></a>, for the
classes of data you consider critical for the success of your program.</div></p></li><li><p>Write down a signature, a <a name="(idx._(gentag._50))"></a>statement of purpose, and a function header.</p><p><div class="SIntrapara">A <a name="(idx._(gentag._51))"></a><span style="font-style: italic">function signature</span> is a comment that tells the readers of your
design how many inputs your function consumes, from which classes they are
drawn, and what kind of data it produces. Here are three examples for
functions that respectively
</div><div class="SIntrapara"><ul><li><p><div class="SIntrapara">consume one <a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a> and produce a <a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a>:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a></p></blockquote></div></p></li><li><p><div class="SIntrapara">consume a <a href="part_one.html#%28tech._temperature%29" class="techoutside" data-pltdoc="x"><span class="techinside">Temperature</span></a> and produce a <a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a>:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._temperature%29" class="techoutside" data-pltdoc="x"><span class="techinside">Temperature</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a></p></blockquote></div><div class="SIntrapara">As this <a name="(idx._(gentag._52))"></a>signature points out, introducing a <a name="(idx._(gentag._53))"></a>data definition as an
alias for an existing form of data makes it easy to read the intention
behind signatures.</div></p><p>Nevertheless, we recommend that you stay away from aliasing data
definitions for now. A proliferation of such names can cause quite
a lot of confusion. It takes practice to balance the need for new names
and the readability of programs, and there are more important ideas to
understand right now.</p></li><li><p><div class="SIntrapara">consume a <a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a>, a <a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a>, and an <a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a>:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></p></blockquote></div><div class="SIntrapara">Stop! What does this function produce?</div></p></li></ul></div></p><p><div class="SIntrapara">A <a name="(idx._(gentag._54))"></a><span style="font-style: italic">purpose statement</span> is a BSL comment that summarizes the purpose
of the function in a single line. If you are ever in doubt about a purpose
statement, write down the shortest possible answer to the question
</div><div class="SIntrapara"><blockquote><p><span style="font-style: italic">what does the function compute?</span></p></blockquote></div><div class="SIntrapara">Every reader of your program should understand what your functions compute
<span style="font-weight: bold">without</span> having to read the function itself.</div></p><p>A multi-function program should also come with a <a name="(idx._(gentag._55))"></a>purpose statement. Indeed,
good programmers write two purpose statements: one for the reader who may
have to modify the code and another one for the person who wishes to use
the program but not read it.</p><p><div class="SIntrapara">Finally, a <a name="(idx._(gentag._56))"></a><span style="font-style: italic">header</span> is a simplistic function definition, also
called a <span style="font-style: italic">stub</span>. Pick one variable name for each class of input in the
signature; the body of the function can be any piece of data from the output
class. These three function headers match the above three
signatures:
</div><div class="SIntrapara"><ul><li><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="RktSym">f</span><span class="stt"> </span><span class="RktSym">a-string</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktVal">0</span><span class="RktPn">)</span></p></li><li><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="RktSym">g</span><span class="stt"> </span><span class="RktSym">n</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktVal">"a"</span><span class="RktPn">)</span></p></li><li><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="RktSym">h</span><span class="stt"> </span><span class="RktSym">num</span><span class="stt"> </span><span class="RktSym">str</span><span class="stt"> </span><span class="RktSym">img</span><span class="RktPn">)</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._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="stt"> </span><span class="RktVal">100</span><span class="stt"> </span><span class="RktVal">100</span><span class="RktPn">)</span><span class="RktPn">)</span></p></li></ul></div><div class="SIntrapara">Our parameter names reflect what kind of data the parameter
represents. Sometimes, you may wish to use names that suggest the
purpose of the parameter.</div></p><p><div class="SIntrapara">When you formulate a <a name="(idx._(gentag._57))"></a>purpose statement, it is often useful to employ the
parameter names to clarify what is computed. For example,
<a name="(idx._(gentag._58))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds </span><span class="RktSym">s</span><span class="RktCmt"> to </span><span class="RktSym">img</span><span class="RktCmt">,</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktSym">y</span><span class="RktCmt"> pixels from the top and </span><span class="RktVal">10</span><span class="RktCmt"> from the left </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">add-image</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktSym">img</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._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></p><p>At this point, you can click the <span class="emph">RUN</span> button and experiment with
the function. Of course, the result is always the same value, which makes
these experiments quite boring.</p></li><li><p>Illustrate the <a name="(idx._(gentag._59))"></a>signature and the <a name="(idx._(gentag._60))"></a>purpose statement with some functional
examples. To construct a <a name="(idx._(gentag._61))"></a><span style="font-style: italic">functional example</span>, pick one piece of
data from each input class from the signature and determine what you expect
back.</p><p><div class="SIntrapara">Suppose you are designing a function that computes the area of a
square. Clearly this function consumes the length of the square&rsquo;s side, and
that is best represented with a (positive) number. Assuming you have done
the first process step according to the recipe, you add the examples
between the purpose statement and the header and get this: <a name="(idx._(gentag._62))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">computes the area of a square with side </span><span class="RktSym">len</span><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">given: 2, expect: 4</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">given: 7, expect: 49</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">area-of-square</span><span class="hspace">&nbsp;</span><span class="RktSym">len</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p></li><li><p>The next step is to take <span style="font-style: italic">inventory</span>, to understand what are
the givens and what we need to compute.<span class="refelem"><span class="refcolumn"><span class="refcontent">We owe the term
&ldquo;inventory&rdquo; to Stephen Bloch.</span></span></span> For the simple functions we are
considering right now, we know that they are given data via
parameters. While parameters are placeholders for values that we don&rsquo;t
know yet, we do know that it is from this unknown data that the function
must compute its result. To remind ourselves of this fact, we replace the
function&rsquo;s body with a <a name="(idx._(gentag._63))"></a><span style="font-style: italic">template</span>.</p><p><div class="SIntrapara">For now, the template contains just the parameters, so that the preceding
example looks like this: <a name="(idx._(gentag._64))"></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">area-of-square</span><span class="hspace">&nbsp;</span><span class="RktSym">len</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&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._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktSym">len</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">The dots remind you that this isn&rsquo;t a complete function, but a template, a
suggestion for an organization.</div></p><p>The templates of this section look boring. As soon as we introduce new
forms of data, templates become interesting.</p></li><li><p>It is now time to <span style="font-style: italic">code</span>. In general, to code means to
program, though often in the narrowest possible way, namely, to write
executable expressions and function definitions.</p><p><div class="SIntrapara">To us, coding means to replace the body of the function with an expression
that attempts to compute from the pieces in the template what the <a name="(idx._(gentag._65))"></a>purpose
statement promises. Here is the complete definition for
<a name="(idx._(gentag._66))"></a><span class="RktSym">area-of-square</span>:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">computes the area of a square with side </span><span class="RktSym">len</span><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">given: 2, expect: 4</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">given: 7, expect: 49</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">area-of-square</span><span class="hspace">&nbsp;</span><span class="RktSym">len</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._sqr%29%29" class="RktValLink" data-pltdoc="x">sqr</a></span><span class="hspace">&nbsp;</span><span class="RktSym">len</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div></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><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds </span><span class="RktSym">s</span><span class="RktCmt"> to </span><span class="RktSym">img</span><span class="RktCmt">, </span><span class="RktSym">y</span><span class="RktCmt"> pixels from top, </span><span class="RktVal">10</span><span class="RktCmt"> pixels to the left</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">given: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;&nbsp;</span><span class="RktVal">5</span><span class="RktCmt"> for </span><span class="RktSym">y</span><span class="RktCmt">, </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;&nbsp;</span><span class="RktVal">"hello"</span><span class="RktCmt"> for </span><span class="RktSym">s</span><span class="RktCmt">, and</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&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="stt"> </span><span class="RktVal">100</span><span class="stt"> </span><span class="RktVal">100</s
that: see <a href="part_one.html#%28counter._%28figure._fig~3adesign1%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">16</span></a>. In particular, the function needs to
turn the given string <span class="RktSym">s</span> into an image, which is then placed into
the given scene.</p></li><li><p><div class="SIntrapara">The last step of a proper design is to test the function on the
examples that you worked out before. For now, testing works like
this. Click the <span class="emph">RUN</span> button and enter function applications that
match the examples in the interactions area: <a name="(idx._(gentag._67))"></a>
</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">area-of-square</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">area-of-square</span><span class="hspace">&nbsp;</span><span class="RktVal">7</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">49</span></p></td></tr></table></blockquote></div><div class="SIntrapara">The results must match the output that you expect; you must
inspect each result and make sure it is equal to what is written down in
the example portion of the design. If the result doesn&rsquo;t match the expected
output, consider the following three possibilities:
</div><div class="SIntrapara"><ol><li><p>You miscalculated and determined the wrong expected output for some
of the examples.</p></li><li><p>Alternatively, the function definition computes the wrong
result. When this is the case, you have a <span style="font-style: italic">logical error</span> in your
program, also known as a <span style="font-style: italic">bug</span>.</p></li><li><p>Both the <a name="(idx._(gentag._68))"></a>examples and the function definition are wrong.</p></li></ol></div><div class="SIntrapara">When you do encounter a mismatch between expected results and actual
values, we recommend that you first reassure yourself that the expected
results are correct. If so, assume that the mistake is in the function
definition. Otherwise, fix the example and then run the tests again. If you
are still encountering problems, you may have encountered the third,
somewhat rare, situation.</div></p></li></ol></div></p><h4>3.2<tt>&nbsp;</tt><a name="(part._sec~3afinger-design)"></a>Finger Exercises: Functions</h4><p>The first few of the following exercises are almost copies of those in
<a href="part_one.html#%28part._sec~3afuncs%29" data-pltdoc="x">Functions</a>, though where the latter use the word &ldquo;define&rdquo; the
exercises below use the word &ldquo;design.&rdquo; What this difference means is that
you should work through the design recipe to create these functions and
your solutions should include all relevant pieces.</p><p>As the title of the section suggests, these exercises are practice
exercises to help you internalize the process. Until the steps become
second nature, never skip one because doing so leads to easily avoidable
errors. There is plenty of room left in programming for complicated
errors; we have no need to waste our time on silly ones.</p><p><a name="(counter._(exercise._design1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;34. Design the function <span class="RktSym">string-first</span>, which
extracts the first character from a non-empty string. Don&rsquo;t worry about
empty strings. <a href="part_one.html#%28counter._%28exercise._design1%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._design2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;35. Design the function <span class="RktSym">string-last</span>, which
extracts the last character from a non-empty string. <a href="part_one.html#%28counter._%28exercise._design2%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._design3))"></a><span style="font-weight: bold">Exercise</span>&nbsp;36. Design the function <span class="RktSym">image-area</span>, which counts
the number of pixels in a given image. <a href="part_one.html#%28counter._%28exercise._design3%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._design4))"></a><span style="font-weight: bold">Exercise</span>&nbsp;37. Design the function <span class="RktSym">string-rest</span>, which
produces a string like the given one with the first character removed. <a href="part_one.html#%28counter._%28exercise._design4%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._design5))"></a><span style="font-weight: bold">Exercise</span>&nbsp;38. Design the function <span class="RktSym">string-remove-last</span>, which
produces a string like the given one with the <span style="font-weight: bold">last</span> character
removed. <a href="part_one.html#%28counter._%28exercise._design5%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>3.3<tt>&nbsp;</tt><a name="(part._sec~3adomain)"></a>Domain Knowledge</h4><p><div class="SIntrapara">It is natural to wonder what knowledge it takes to code up the body of a
function. A little bit of reflection tells you that this step
demands an appropriate grasp of the domain of the program. Indeed, there
are two forms of such <span style="font-style: italic">domain knowledge</span>:
</div><div class="SIntrapara"><ol><li><p>Knowledge from external domains, such as mathematics, music, biology,
civil engineering, art, and so on. Because programmers cannot know all of the
application domains of computing, they must be prepared to understand the
language of a variety of application areas so that they can discuss
problems with domain experts. Mathematics is at the intersection of many,
but not all, domains. Hence, programmers must often pick up new languages
as they work through problems with domain experts.</p></li><li><p>Knowledge about the teachpack functions in the chosen programming language.
When your task is to translate a mathematical formula involving the
tangent function, you need to know or guess that your chosen language
comes with a function such as BSL&rsquo;s <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._tan%29%29" class="RktValLink" data-pltdoc="x">tan</a></span>. When your task involves
graphics, you will benefit from understanding the possibilities of
<span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/image</span></span> teachpack</span>.</p></li></ol></div><div class="SIntrapara">Since you can never predict the area you will be working in, or which
programming language you will have to use, it is imperative that you have
a solid understanding of the full possibilities of whatever computer
languages are around and suitable. Otherwise some domain expert with
half-baked programming knowledge will take over your job.</div></p><p>You can recognize problems that demand domain knowledge from the data
definitions that you work out. As long as the data definitions use classes
that exist in the chosen programming language, the definition of the
function body (and program) mostly relies on expertise in the
domain. Later, when we introduce complex forms of data, the design of
functions demands computer science knowledge.</p><h4>3.4<tt>&nbsp;</tt><a name="(part._sec~3adesign)"></a>From Functions to Programs</h4><p>Not all programs consist of a single function definition. Some require
several functions; many also use constant definitions. No matter what, it
is always important to design every function systematically, though
global constants as well as auxiliary functions change the design
process a bit.</p><p>When you have defined global constants, your functions may use them to
compute results. To remind yourself of their existence, you may wish to add
these constants to your templates; after all, they belong to the inventory
of things that may contribute to the function definition.</p><p>Multi-function programs come about because interactive programs
automatically need functions that handle key and mouse events, functions
that render the state as music, and possibly more. Even batch programs may
require several different functions because they perform several separate
tasks. Sometimes the problem statement itself suggests these tasks; other
times you will discover the need for auxiliary functions as you are in the
middle of designing some function.</p><p>For these reasons, we recommend keeping around a list of needed functions
or a <span style="font-style: italic">wish list</span>.<span class="refelem"><span class="refcolumn"><span class="refcontent">We owe the term &ldquo;wish list&rdquo; to
John Stone.</span></span></span> Each entry on a wish list should consist of three things:
a meaningful name for the function, a signature, and a purpose
statement. For the design of a batch program, put the main function on the
wish list and start designing it. For the design of an interactive program,
you can put the event handlers, the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</a></span> function, and the
scene-rendering function on the list. As long as the list isn&rsquo;t empty, pick
a wish and design the function. If you discover during the design that you
need another function, put it on the list. When the list is empty, you are
done.</p><h4>3.5<tt>&nbsp;</tt><a name="(part._sec~3atesting)"></a>On Testing</h4><p>Testing quickly becomes a labor-intensive chore. While it is easy to run
small programs in the interactions area, doing so requires a lot of
mechanical labor and intricate inspections. As programmers grow their
systems, they wish to conduct many tests. Soon this labor becomes
overwhelming, and programmers start to neglect it. At the same
time, testing is the first tool for discovering and preventing basic
flaws. Sloppy testing quickly leads to buggy functions&#8212;<wbr></wbr>that is,
functions with hidden problems&#8212;<wbr></wbr>and buggy functions retard projects,
often in multiple ways.</p><p><div class="SIntrapara">Hence, it is critical to mechanize tests instead of performing them
manually. Like many programming languages, BSL includes a testing
facility, and DrRacket is aware of this facility. To introduce this testing
facility, we take a second look at the function that converts temperatures
in Fahrenheit to Celsius temperatures from <a href="part_one.html#%28part._sec~3aprogs%29" data-pltdoc="x">Programs</a>. Here is the
definition:<a name="(idx._(gentag._69))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">converts Fahrenheit temperatures to Celsius</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">given 32, expect 0</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">given 212, expect 100</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">given -40, expect -40</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">f2c</span><span class="hspace">&nbsp;</span><span class="RktSym">f</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="RktVal">5/9</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">f</span><span class="hspace">&nbsp;</span><span class="RktVal">32</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Testing the function&rsquo;s examples calls for three computations and three
comparisons between two numbers each. You can formulate these tests
and add them to the definitions area in DrRacket:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">f2c</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-4</span>0</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-4</span>0</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">f2c</span><span class="hspace">&nbsp;</span><span class="RktVal">32</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="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">f2c</span><span class="hspace">&nbsp;</span><span class="RktVal">212</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">When you now click the <span class="emph">RUN</span> button, you see a report from BSL that
the program passed all three tests&#8212;<wbr></wbr>and you have nothing else to do.</div></p><p><div class="SIntrapara">In addition to getting tests to run automatically, the <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span>
forms show another advantage when tests fail. To see how this works,
change one of the above tests so that the result is wrong, for example
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">f2c</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-4</span>0</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">40</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">When you now click the <span class="emph">RUN</span> button, an additional window pops
up. The window&rsquo;s text explains that one of three tests failed. For the
failed test, the window displays three pieces: the computed value, the
result of the function call (<span class="RktVal"><span class="nobreak">-4</span>0</span>); the expected value
(<span class="RktVal">40</span>); and a hyperlink to the text of the failed test case.</div></p><blockquote class="Herefigure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">converts Fahrenheit temperatures to Celsius temperatures </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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">f2c</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-4</span>0</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-4</span>0</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">f2c</span><span class="hspace">&nbsp;</span><span class="RktVal">32</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="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">f2c</span><span class="hspace">&nbsp;</span><span class="RktVal">212</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">100</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">f2c</span><span class="hspace">&nbsp;</span><span class="RktSym">f</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="RktVal">5/9</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">f</span><span class="hspace">&nbsp;</span><span class="RktVal">32</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~3atesting))" x-target-lift="Figure"></a>Figure&nbsp;17: </span>Testing in BSL</span></p></blockquote><p>You can place <span
function definitions that they test. When you click <span class="emph">RUN</span>, DrRacket
collects all <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span> specifications and evaluates them
<span style="font-weight: bold">after</span> all function definitions have been added to the
&ldquo;vocabulary&rdquo; of operations. <a href="part_one.html#%28counter._%28figure._fig~3atesting%29%29" data-pltdoc="x">Figure&nbsp;<span class="FigureRef">17</span></a> shows how to exploit this
freedom to combine the example and test step. Instead of writing down the
examples as comments, you can translate them directly into tests. When
you&rsquo;re all done with the design of the function, clicking <span class="emph">RUN</span>
performs the test. And if you ever change the function for some reason,
the next click retests the function.</p><p><div class="SIntrapara">Last but not least, <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span> also works for images. That is,
you can test image-producing functions. Say you wish to design
the function <span class="RktSym">render</span>, which places the image of a car, dubbed
<span class="RktSym">CAR</span>, into a background scene, named <span class="RktSym">BACKGROUND</span>. For the
design of this function, you may formulate the tests such as the following:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">render</span><span class="hspace">&nbsp;</span><span class="RktVal">50</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;&nbsp;</span><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_44.png" alt="image" width="306.0" height="35.0"/><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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">render</span><span class="hspace">&nbsp;</span><span class="RktVal">200</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;&nbsp;</span><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_45.png" alt="image" width="306.0" height="35.0"/><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Alternatively, you could write them like this:<span class="refelem"><span class="refcolumn"><span class="refcontent">For additional
ways of formulating tests, see intermezzo 1.</span></span></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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">render</span><span class="hspace">&nbsp;</span><span class="RktVal">50</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;&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">CAR</span><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktSym">Y-CAR</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKGROUND</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">render</span><span class="hspace">&nbsp;</span><span class="RktVal">200</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;&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">CAR</span><span class="hspace">&nbsp;</span><span class="RktVal">200</span><span class="hspace">&nbsp;</span><span class="RktSym">Y-CAR</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKGROUND</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">This alternative approach helps you figure out how to express the function
body and is therefore preferable. One way to develop such expressions is
to experiment in the interactions area.</div></p><p>Because it is so useful to have DrRacket conduct the tests and not to check
everything yourself manually, we immediately switch to this style of
testing for the rest of the book. This form of testing is dubbed
<a name="(idx._(gentag._70))"></a><span style="font-style: italic">unit testing</span>, and BSL&rsquo;s unit-testing framework is especially
tuned for novice programmers. One day you will switch to some other
programming language; one of your first tasks will be to figure out
its unit-testing framework.</p><h4>3.6<tt>&nbsp;</tt><a name="(part._.D.K._sec~3adesign-world)"></a>Designing World Programs</h4><p>While the previous chapter introduces <span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/universe</span></span> teachpack</span> in an ad
hoc way, this section demonstrates how the <a name="(idx._.D.K._(gentag._71._.D.K))"></a>design recipe also helps you create
world programs systematically. It starts with a brief summary of
<span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/universe</span></span> teachpack</span> based on data definitions and function signatures. Then
it spells out the design recipe for world programs.</p><p>The teachpack expects that a programmer develops a data definition that
represents the state of the world and a function <span class="RktSym">render</span> that
knows how to create an image for every possible state of the
world. Depending on the needs of the program, the programmer must then
design functions that respond to clock ticks, keystrokes, and mouse
events. Finally, an interactive program may need to stop when its current
world belongs to a sub-class of states; <span class="RktSym">end?</span> recognizes these
<span style="font-style: italic">final states</span>. <a href="part_one.html#%28counter._.D.K._%28figure._fig~3aworld-signatures%29%29" data-pltdoc="x">Figure&nbsp;<span class="FigureRef">18</span></a> spells out this
idea in a schematic and simplified way.</p><blockquote class="Herefigure"><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 style="font-style: italic">WorldState</span><span class="RktCmt">: data representing the current world (</span><span class="RktSym">cw</span><span class="RktCmt">)</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="stt">WorldState</span><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">when needed, </span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="RktCmt"> obtains the image of the current </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">state of the world by evaluating </span><span class="RktPn">(</span><span class="RktSym">render</span><span class="stt"> </span><span class="RktSym">cw</span><span class="RktPn">)</span><span class="RktCmt"> </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">render</span><span class="hspace">&nbsp;</span><span class="RktSym">cw</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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="stt">WorldState</span><span class="RktCmt"> -&gt; </span><span class="stt">WorldState</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">for each tick of the clock, </span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="RktCmt"> obtains the next </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">state of the world from </span><span class="RktPn">(</span><span class="RktSym">clock-tick-handler</span><span class="stt"> </span><span class="RktSym">cw</span><span class="RktPn">)</span><span class="RktCmt"> </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">clock-tick-handler</span><span class="hspace">&nbsp;</span><span class="RktSym">cw</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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="stt">WorldState</span><span class="RktCmt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltd
you can focus on the truly important problem of designing world programs.
Let&rsquo;s construct a concrete example for the following design recipe:
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> Design a program that moves a car from left to right on the
world canvas, three pixels per clock tick.</p></blockquote></div><div class="SIntrapara">For this problem statement, it is easy to imagine scenes for the domain:
</div><div class="SIntrapara"><blockquote class="SCentered"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_46.png" alt="image" width="306.0" height="35.0"/></p></blockquote></div><div class="SIntrapara"><blockquote class="SCentered"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_47.png" alt="image" width="306.0" height="35.0"/></p></blockquote></div><div class="SIntrapara"><blockquote class="SCentered"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_48.png" alt="image" width="306.0" height="35.0"/></p></blockquote></div><div class="SIntrapara">In this book, we often refer to the domain of an interactive
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> program as a &ldquo;world,&rdquo; and we speak of designing
&ldquo;world programs.&rdquo;</div></p><p><div class="SIntrapara">The design recipe for world programs, like the one for functions, is a tool
for systematically moving from a problem statement to a working
program. It consists of three big steps and one small one:
</div><div class="SIntrapara"><ol><li><p><div class="SIntrapara">For all those properties of the world that remain the same over time
and are needed to render it as an <a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a>, introduce constants. In BSL,
we specify such constants via definitions. For the purpose of world
programs, we distinguish between two kinds of constants:
</div><div class="SIntrapara"><ol><li><p>&ldquo;Physical&rdquo; constants describe general attributes of objects in the
world, such as the speed or velocity of an object, its color, its height,
its width, its radius, and so forth. Of course these constants don&rsquo;t really
refer to physical facts, but many are analogous to physical aspects of the
real world.</p><p><div class="SIntrapara">In the context of our sample problem, the radius of the car&rsquo;s wheels and
the distance between the wheels are such &ldquo;physical&rdquo; constants:
</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">WIDTH-OF-WORLD</span><span class="hspace">&nbsp;</span><span class="RktVal">200</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="RktSym">WHEEL-RADIUS</span><span class="hspace">&nbsp;</span><span class="RktVal">5</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">WHEEL-DISTANCE</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">WHEEL-RADIUS</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Note how the second constant is computed from the first.</div></p></li><li><p>Graphical constants are images of objects in the world. The program
composes them into images that represent the complete state of the world.</p><p><div class="SIntrapara">Here are graphical constants for wheel images of our sample
car:
</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">WHEEL</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._circle%29%29" class="RktValLink" data-pltdoc="x">circle</a></span><span class="hspace">&nbsp;</span><span class="RktSym">WHEEL-RADIUS</span><span class="hspace">&nbsp;</span><span class="RktVal">"solid"</span><span class="hspace">&nbsp;</span><span class="RktVal">"black"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="refelem"><span class="refcolumn"><span class="refcontent">We suggest you experiment in DrRacket&rsquo;s interactions area to develop such graphical constants.</span></span></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">SPACE</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._rectangle%29%29" class="RktValLink" data-pltdoc="x">rectangle</a></span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktSym">WHEEL-RADIUS</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"white"</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">BOTH-WHEELS</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._beside%29%29" class="RktValLink" data-pltdoc="x">beside</a></span><span class="hspace">&nbsp;</span><span class="RktSym">WHEEL</span><span class="hspace">&nbsp;</span><span class="RktSym">SPACE</span><span class="hspace">&nbsp;</span><span class="RktSym">WHEEL</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Graphical constants are usually computed, and the computations tend to
involve physical constants and other images.</div></p></li></ol></div><div class="SIntrapara">It is good practice to annotate constant definitions with a comment that
explains what they mean.</div></p></li><li><p>Those properties that change over time&#8212;<wbr></wbr>in reaction to clock ticks,
keystrokes, or mouse actions&#8212;<wbr></wbr>give rise to the current state of the
world. Your task is to develop a <a name="(idx._.D.K._(gentag._72._.D.K))"></a>data representation for all possible
states of the world. The development results in a <a name="(idx._.D.K._(gentag._73._.D.K))"></a>data definition, which
comes with a comment that tells readers how to represent world information
as data and how to interpret data as information about the world.</p><p>Choose simple forms of data to represent the state of the world.</p><p>For the running example, it is the car&rsquo;s distance from the left margin that
changes over time. While the distance to the right margin changes, too, it
is obvious that we need only one or the other to create an image. A
distance is measured in numbers, so the following is an adequate data
definition:</p><p><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._.D.K._worldstate)"></a><span style="font-style: italic">WorldState</span><span class="RktCmt"> is a </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt">.</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> the number of pixels between</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">the left border of the scene and the car</span></td></tr></table></blockquote></div><div class="SIntrapara">An alternative is to count the number of clock ticks that have
passed and to use this number as the state of the world. We leave this
design variant as an exercise.</div></p></li><li><p>Once you have a data representation for the state of the world, you
need to design a number of functions so that you can form a valid
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression.</p><p><div class="SIntrapara">To start with, you need a function that maps any given state into an image
so that <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> can render the sequence of states as images:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktSym">render</span></p></blockquote></div><div class="SIntrapara">Next you need to decide which kind of events should change which aspects of
the world state. Depending on your decisions, you need to design some or
all of the following three functions:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktSym">clock-tick-handler</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktSym">keystroke-handler</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktSym">mouse-event-handler</span></td></tr></table></blockquote></div><div class="SIntrapara">Finally, if the problem statement suggests that the program should stop if
the world has certain properties, you must design
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktSym">end?</span></p></blockquote></div><div class="SIntrapara">For the generic signatures and purpose statements of these functions, see
<a href="part_one.html#%28counter._.D.K._%28figure._fig~3aworld-signatures%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">18</span></a>. Adapt these generic purpose statements to
the particular problems you solve so that readers know what they compute.</div></p><p>In short, the desire to design an interactive program automatically creates
several initial entries for your wish list. Work them off one by
one and you get a complete world program.</p><p><div class="SIntrapara">Let&rsquo;s work through this step for the sample program. While
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> dictates that we must design a rendering function, we
still need to figure out whether we want any event-handling
functions. Since the car is supposed to move from left to right, we
definitely need a function that deals with clock ticks. Thus, we get this
wish list: <a name="(idx._.D.K._(gentag._74._.D.K))"></a> <a name="(idx._.D.K._(gentag._75._.D.K))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">places the image of the car </span><span class="RktSym">x</span><span class="RktCmt"> pixels from </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">the left margin of the </span><span class="RktSym">BACKGROUND</span><span class="RktCmt"> image </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">render</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="RktSym">BACKGROUND</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><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds </span><span class="RktVal">3</span><span class="RktCmt"> to </span><span class="RktSym">x</span><span class="RktCmt"> to move the car right </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">tock</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="RktSym">x</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Note how we tailored the purpose statements to the problem at hand, with
an understanding of how <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> will use these functions.</div></p></li><li><p>Finally, you need a <span class="RktSym">main</span> function. Unlike all other
functions, a <span class="RktSym">main</span> function for world programs doesn&rsquo;t demand
design or testing. Its sole reason for existing is that you can
<span style="font-weight: bold">launch</span> your world program conveniently from DrRacket&rsquo;s interactions
area.</p><p><div class="SIntrapara">The one decision you must make concerns <span class="RktSym">main</span>&rsquo;s arguments. For our
sample problem, we opt for one argument: the initial state of the
world. Here we go: <a name="(idx._.D.K._(gentag._76._.D.K))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">launches the program from some initial state </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">main</span><span class="hspace">&nbsp;</span><span class="RktSym">ws</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ws</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace">&nbsp;</span><span class="RktSym">tock</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace">&nbsp;</span><span class="RktSym">render</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Hence, you can launch this interactive program with
</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">main</span><span class="hspace">&nbsp;</span><span class="RktVal">13</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">to watch the car start at 13 pixels from the left margin. It will stop
when you close <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>&rsquo;s window. Remember that <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>
returns the current state of the world when the evaluation stops.</div></p></li></ol></div></p><p>Naturally, you don&rsquo;t have to use the name &ldquo;WorldState&rdquo; for the class of
data that represents the states of the world. Any name will do as long as
you use it consistently for the signatures of the event-handling
functions. Also, you don&rsquo;t have to use the names <span class="RktSym">tock</span>,
<span class="RktSym">end?</span>, or <span class="RktSym">render</span>. You may name these functions whatever
you like, as long as you use the same names when you write down the clauses
of the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression. Lastly, you may have noticed that you
may list the clauses of a <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression in any order as long
as you list the initial state first.</p><p>Let&rsquo;s now work through the rest of the program design process, using the
<a name="(idx._.D.K._(gentag._77._.D.K))"></a>design recipe for functions and other design concepts spelled out so far.</p><p><div class="SIntrapara"><a name="(counter._.D.K._(exercise._design6))"></a><span style="font-weight: bold">Exercise</span>&nbsp;39. Good programmers ensure that an image such as
<span class="RktSym">CAR</span> can be enlarged or reduced via a single change to a constant
definition.<span class="refelem"><span class="refcolumn"><span class="refcontent">Good programmers establish a single point of
control for all aspects of their programs, not just the graphical
constants. Several chapters deal with this issue.</span></span></span> We started the
development of our car image with a single plain definition:
</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">WHEEL-RADIUS</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">The definition of <span class="RktSym">WHEEL-DISTANCE</span> is based on the wheel&rsquo;s
radius. Hence, changing <span class="RktSym">WHEEL-RADIUS</span> from <span class="RktVal">5</span> to
<span class="RktVal">10</span> doubles the size of the car image. This kind of program organization is
dubbed <span style="font-style: italic">single point of control</span>, and good design employs this
idea as much as possible.</div></p><p>Develop your favorite image of an automobile so that <span class="RktSym">WHEEL-RADIUS</span>
remains the single point of control. <a href="part_one.html#%28counter._.D.K._%28exercise._design6%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara">The next entry on the wish list is the clock tick handling function: <a name="(idx._.D.K._(gentag._78._.D.K))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">moves the car by </span><span class="RktVal">3</span><span class="RktCmt"> pixels for every clock tick</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">tock</span><span class="hspace">&nbsp;</span><span class="RktSym">cw</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">cw</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Since the state of the world represents the distance between the left
margin of the canvas and the car, and since the car moves at three pixels
per clock tick, a concise purpose statement combines these two facts into
one. This also makes it easy to create examples and to define the function:
<a name="(idx._.D.K._(gentag._79._.D.K))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">moves the car by </span><span class="RktVal">3</span><span class="RktCmt"> pixels for every clock tick</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">examples: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktCmt">given: 20, expect 23</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktCmt">given: 78, expect 81</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">tock</span><span class="hspace">&nbsp;</span><span class="RktSym">cw</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._%2B%29%29" class="RktValLink" data-pltdoc="x">+</a></span><span class="hspace">&nbsp;</span><span class="RktSym">cw</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p><div class="SIntrapara">The last design step calls for confirmation that the examples work as
expected. So we click the <span class="emph">RUN</span> button and evaluate these expressions: <a name="(idx._.D.K._(gentag._80._.D.K))"></a>
</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">tock</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">23</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">tock</span><span class="hspace">&nbsp;</span><span class="RktVal">78</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">81</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Since the results are as expected, the design of <span class="RktSym">tock</span> is
finished.</div></p><p><a name="(counter._.D.K._(exercise._car-test))"></a><span style="font-weight: bold">Exercise</span>&nbsp;40. Formulate the examples as BSL tests, that is,
using the <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span> form. Introduce a mistake. Re-run the tests. <a href="part_one.html#%28counter._.D.K._%28exercise._car-test%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara">Our second entry on the wish list specifies a function that translates the
state of the world into an image: <a name="(idx._.D.K._(gentag._81._.D.K))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">places the car into the </span><span class="RktSym">BACKGROUND</span><span class="RktCmt"> scene,</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">according to the given world state </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">render</span><span class="hspace">&nbsp;</span><span class="RktSym">cw</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktSym">BACKGROUND</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p>To make <a name="(idx._.D.K._(gentag._82._.D.K))"></a>examples for a rendering function, we suggest arranging a table
like the upper half of <a href="part_one.html#%28counter._.D.K._%28figure._fig~3amoving-car%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">19</span></a>. It lists the given
world states and the desired scenes. For your first few rendering
functions, you may wish to draw these images by hand.</p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><blockquote><p><div class="SIntrapara"><table cellspacing="0" cellpadding="0" style="border-collapse: collapse;"><tr><td align="right" style="border-bottom: 1px solid black;"><p>cw</p></td><td align="right" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td align="left" style="border-bottom: 1px solid black;"><p>its image</p></td></tr><tr><td align="right"><p>50</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td align="left"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_49.png" alt="image" width="306.0" height="35.0"/></p></td></tr><tr><td align="right"><p>100</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td align="left"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_50.png" alt="image" width="306.0" height="35.0"/></p></td></tr><tr><td align="right"><p>150</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td align="left"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_51.png" alt="image" width="306.0" height="35.0"/></p></td></tr><tr><td align="right"><p>200</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td align="left"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_52.png" alt="image" width="306.0" height="35.0"/></p></td></tr><tr><td align="right"><p></p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td align="left"><p></p></td></tr></table></div><div class="SIntrapara"><table cellspacing="0" cellpadding="0" style="border-collapse: collapse;"><tr><td align="right" style="border-bottom: 1px solid black;"><p>cw</p></td><td align="right" style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td align="left" style="border-bottom: 1px solid black;"><p>an expression</p></td></tr><tr><td align="right"><p>50</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td align="left"><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">CAR</span><span class="hspace">&nbsp;</span><span class="RktVal">50</span><span class="hspace">&nbsp;</span><span class="RktSym">Y-CAR</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKGROUND</span><span class="RktPn">)</span></td></tr><tr><td align="right"><p>100</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td align="left"><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">CAR</span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktSym">Y-CAR</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKGROUND</span><span class="RktPn">)</span></td></tr><tr><td align="right"><p>150</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td align="left"><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">CAR</span><span class="hspace">&nbsp;</span><span class="RktVal">150</span><
running function is going to display&#8212;<wbr></wbr>a moving car&#8212;<wbr></wbr>it does not explain
<span style="font-weight: bold">how</span> the function creates this result. To get from here to there, we
recommend writing down expressions like those in the lower half of
<a href="part_one.html#%28counter._.D.K._%28figure._fig~3amoving-car%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">19</span></a> that create the images in the table. The capitalized names
refer to the obvious constants: the image of a car, its fixed
y-coordinate, and the background scene, which is currently empty.</p><p><div class="SIntrapara">This extended table suggests a pattern for the formula that goes into the
body of the <span class="RktSym">render</span> function: <a name="(idx._.D.K._(gentag._83._.D.K))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">places the car into the </span><span class="RktSym">BACKGROUND</span><span class="RktCmt"> scene,</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">according to the given world state </span></td></tr><tr><td><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._define%29%29" class="RktStxLink" data-pltdoc="x">define</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">render</span><span class="hspace">&nbsp;</span><span class="RktSym">cw</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&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">CAR</span><span class="hspace">&nbsp;</span><span class="RktSym">cw</span><span class="hspace">&nbsp;</span><span class="RktSym">Y-CAR</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKGROUND</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">And that is mostly all there is to designing a simple world program.</div></p><p><div class="SIntrapara"><a name="(counter._.D.K._(exercise._design8))"></a><span style="font-weight: bold">Exercise</span>&nbsp;41. Finish the sample problem and get the program to
run. That is, assuming that you have solved <a href="part_one.html#%28counter._.D.K._%28exercise._design6%29%29" data-pltdoc="x">exercise&nbsp;39</a>, define the
constants <span class="RktSym">BACKGROUND</span> and <span class="RktSym">Y-CAR</span>. Then assemble all the
function definitions, including their tests. When your program runs to your
satisfaction, add a tree to the scenery. We used
</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">tree</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._underlay%2Fxy%29%29" class="RktValLink" data-pltdoc="x">underlay/xy</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktVal">9</span><span class="hspace">&nbsp;</span><span class="RktVal">15</span></td></tr><tr><td><span class="hspace">&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._rectangle%29%29" class="RktValLink" data-pltdoc="x">rectangle</a></span><span class="hspace">&nbsp;</span><span class="RktVal">2</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">"brown"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">to create a tree-like shape. Also add a clause to the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>
expression that stops the animation when the car has disappeared on the
right side. <a href="part_one.html#%28counter._.D.K._%28exercise._design8%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p>After settling on an initial data representation for world states, a careful
programmer may have to revisit this fundamental design decision during the
rest of the design process. For example, the data definition for the sample
problem represents the car as a point. But (the image of) the car isn&rsquo;t
just a mathematical point without width and height. Hence, the
interpretation statement&#8212;<wbr></wbr>the number of pixels from the left margin&#8212;<wbr></wbr>is
an ambiguous statement. Does this statement measure the distance between
the left margin and the left end of the car? Its center point? Or even its
right end? We ignored this issue here and leave it to BSL&rsquo;s image
primitives to make the decision for us. If you don&rsquo;t like the result,
revisit the data definition above and modify it or its interpretation
statement to suit your taste.</p><p><a name="(counter._.D.K._(exercise._design9a))"></a><span style="font-weight: bold">Exercise</span>&nbsp;42. Modify the interpretation of the sample data
definition so that a state denotes the x-coordinate of the
right-most edge of the car. <a href="part_one.html#%28counter._.D.K._%28exercise._design9a%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara"><a name="(counter._.D.K._(exercise._design9))"></a><span style="font-weight: bold">Exercise</span>&nbsp;43. Let&rsquo;s work through the same problem statement with a
time-based data definition:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">An </span><a name="(tech._.D.K._animationstate)"></a><span style="font-style: italic">AnimationState</span><span class="RktCmt"> is a </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt">.</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> the number of clock ticks </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">since the animation started</span></td></tr></table></blockquote></div><div class="SIntrapara">Like the original data definition, this one also equates the states of the
world with the class of numbers. Its interpretation, however, explains that
the number means something entirely different.</div></p><p>Design the functions <span class="RktSym">tock</span> and <span class="RktSym">render</span>. Then develop a
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression so that once again you get an animation of a
car traveling from left to right across the world&rsquo;s canvas.</p><p>How do you think this program relates to <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>
from <a href="part_prologue.html" data-pltdoc="x">Prologue: How to Program</a>?</p><p>Use the data definition to design a program that moves the car according to
a sine wave. (Don&rsquo;t try to drive like that.) <a href="part_one.html#%28counter._.D.K._%28exercise._design9%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara">We end the section with an illustration of mouse event handling, which also
illustrates the advantages that a separation of view and model
provide.</div><div class="SIntrapara"><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p>Dealing with mouse movements is occasionally tricky
because they aren&rsquo;t exactly what they seem to be. For a first idea of why that
is, read <a href="https://htdp.org/2022-8-7/notes/note_mice-and-chars.html" data-pltdoc="x">On Mice and Keys</a>.</p></blockquote></blockquote></blockquote></div><div class="SIntrapara"> Suppose we wish to
allow people to move the car through &ldquo;hyperspace&rdquo;:
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> Design a program that moves a car across the world canvas,
from left to right, at the rate of three pixels per clock tick. <span style="font-weight: bold">If
the mouse is clicked anywhere on the canvas, the car is placed at the
x-coordinate of that click.</span></p></blockquote></div><div class="SIntrapara">The bold part is the expansion of the sample problem from above.</div></p><p><div class="SIntrapara">When we are confronted with a modified problem, we use the design process
to guide us to the necessary changes. If used properly, this process
naturally determines what we need to add to our existing program to cope
with the expansion of the problem statement. So here we go:
</div><div class="SIntrapara"><ol><li><p>There are no new properties, meaning we do not need new constants.</p></li><li><p>The program is still concerned with just one property that
changes over time, the x-coordinate of the car. Hence, the data
representation suffices.</p></li><li><p><div class="SIntrapara">The revised problem statement calls for a mouse-event handler,
without giving up on the clock-based movement of the car. Hence,
we state an appropriate wish: <a name="(idx._.D.K._(gentag._84._.D.K))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">places the car at </span><span class="RktSym">x-mouse</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">if the given </span><span class="RktSym">me</span><span class="RktCmt"> is </span><span class="RktVal">"button-down"</span><span class="RktCmt"> </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">hyper</span><span class="hspace">&nbsp;</span><span class="RktSym">x-position-of-car</span><span class="hspace">&nbsp;</span><span class="RktSym">x-mouse</span><span class="hspace">&nbsp;</span><span class="RktSym">y-mouse</span><span class="hspace">&nbsp;</span><span class="RktSym">me</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktSym">x-position-of-car</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p></li><li><p><div class="SIntrapara">Lastly, we need to modify <span class="RktSym">main</span> to take care of mouse
events. All this requires is the addition of an <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-mouse%29%29" class="RktStxLink" data-pltdoc="x">on-mouse</a></span>
clause that defers to the new entry on our wish list: <a name="(idx._.D.K._(gentag._85._.D.K))"></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">main</span><span class="hspace">&nbsp;</span><span class="RktSym">ws</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ws</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace">&nbsp;</span><span class="RktSym">tock</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-mouse%29%29" class="RktStxLink" data-pltdoc="x">on-mouse</a></span><span class="hspace">&nbsp;</span><span class="RktSym">hyper</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace">&nbsp;</span><span class="RktSym">render</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">After all, the modified problem calls for dealing with mouse
clicks and everything else remains the same.</div></p></li></ol></div><div class="SIntrapara">The rest is a mere matter of designing one more function, and for that we
use the design recipe for functions.</div></p><p><div class="SIntrapara">An entry on the wish list covers the first two steps of the design recipe
for functions. Hence, our next step is to develop some functional
examples: <a name="(idx._.D.K._(gentag._86._.D.K))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">places the car at </span><span class="RktSym">x-mouse</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">if the given </span><span class="RktSym">me</span><span class="RktCmt"> is </span><span class="RktVal">"button-down"</span><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">given: </span><span class="RktVal">21</span><span class="RktCmt"> </span><span class="RktVal">10</span><span class="RktCmt"> </span><span class="RktVal">20</span><span class="RktCmt"> </span><span class="RktVal">"enter"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">wanted: </span><span class="RktVal">21</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">given: </span><span class="RktVal">42</span><span class="RktCmt"> </span><span class="RktVal">10</span><span class="RktCmt"> </span><span class="RktVal">20</span><span class="RktCmt"> </span><span class="RktVal">"button-down"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">wanted: </span><span class="RktVal">10</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">given: </span><span class="RktVal">42</span><span class="RktCmt"> </span><span class="RktVal">10</span><span class="RktCmt"> </span><span class="RktVal">20</span><span class="RktCmt"> </span><span class="RktVal">"move"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">wanted: </span><span class="RktVal">42</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">hyper</span><span class="hspace">&nbsp;</span><span class="RktSym">x-position-of-car</span><span class="hspace">&nbsp;</span><span class="RktSym">x-mouse</span><span class="hspace">&nbsp;</span><span class="RktSym">y-mouse</span><span class="hspace">&nbsp;</span><span class="RktSym">me</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktSym">x-position-of-car</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">The examples say that if the string argument is equal to
<span class="RktVal">"button-down"</span>, the function returns <span class="RktSym">x-mouse</span>; otherwise
it returns <span class="RktSym">x-position-of-car</span>.</div></p><p><a name="(counter._.D.K._(exercise._car-test-again))"></a><span style="font-weight: bold">Exercise</span>&nbsp;44. Formulate the examples as BSL tests. Click
<span class="emph">RUN</span> and watch them fail. <a href="part_one.html#%28counter._.D.K._%28exercise._car-test-again%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara">To complete the function definition, we must appeal to your fond memories
from <a href="part_prologue.html" data-pltdoc="x">Prologue: How to Program</a>,<span class="refelem"><span class="refcolumn"><span class="refcontent">In the next chapter, we explain
designing with <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> in detail. </span></span></span> specifically memories about 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>itional form. Using <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>, <span class="RktSym">hyper</span> is a
two-line definition:<a name="(idx._.D.K._(gentag._87._.D.K))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._.D.K._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">places the car at </span><span class="RktSym">x-mouse</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">if the given </span><span class="RktSym">me</span><span class="RktCmt"> is </span><span class="RktVal">"button-down"</span><span class="RktCmt"> </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">hyper</span><span class="hspace">&nbsp;</span><span class="RktSym">x-position-of-car</span><span class="hspace">&nbsp;</span><span class="RktSym">x-mouse</span><span class="hspace">&nbsp;</span><span class="RktSym">y-mouse</span><span class="hspace">&nbsp;</span><span class="RktSym">me</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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"button-down"</span><span class="hspace">&nbsp;</span><span class="RktSym">me</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">x-mouse</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&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._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x-position-of-car</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">If you solved <a href="part_one.html#%28counter._.D.K._%28exercise._car-test-again%29%29" data-pltdoc="x">exercise&nbsp;44</a>, rerun the program and watch all
tests succeed. Assuming the tests do succeed, evaluate
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym">main</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">in DrRacket&rsquo;s interactions area and transport your car through hyperspace.</div></p><p>You may wonder why this program modification is so straightforward. There
are really two reasons. First, this book and its software strictly separate
the data that a program tracks&#8212;<wbr></wbr>the <span style="font-style: italic">model</span>&#8212;<wbr></wbr>and the image that
it shows&#8212;<wbr></wbr>the <span style="font-style: italic">view</span>. In particular, functions that deal with events
have nothing to do with how the state is rendered. If we wish to modify how
a state is rendered, we can focus on the function specified in a
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span> clause. Second, the design recipes for programs and
functions organize programs in the right way. If anything changes in a
problem statement, following the design recipe a second time naturally
points out where the original problem solution has to change. While this
may look obvious for the simple kind of problems we are dealing with now,
it is critical for the kind of problems that programmers encounter in the
real world.</p><h4>3.7<tt>&nbsp;</tt><a name="(part._sec~3azoo1)"></a>Virtual Pet Worlds</h4><p>This exercise section introduces the first two elements of
a virtual pet game. It starts with just a display of a cat that
keeps walking across the canvas. Of course, all the walking makes the cat
unhappy and its unhappiness shows. As with all pets, you can try petting,
which helps some, or you can try feeding, which helps a lot more.</p><p><div class="SIntrapara">So let&rsquo;s start with an image of our favorite cat:
</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">cat1</span><span class="hspace">&nbsp;</span><img src="cat1.png" alt="" width="75" height="117"/><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Copy the cat image and paste it into DrRacket, then give the image a name
with <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>, just like above.</div></p><p><a name="(counter._(exercise._design12))"></a><span style="font-weight: bold">Exercise</span>&nbsp;45. Design a &ldquo;virtual cat&rdquo; world program that
continuously moves the cat from left to right. Let&rsquo;s call it
<span class="RktSym">cat-prog</span> and let&rsquo;s assume it consumes the starting position of
the cat. Furthermore, make the cat move three pixels per clock
tick. Whenever the cat disappears on the right, it reappears on the
left. You may wish to read up on the <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._modulo%29%29" class="RktValLink" data-pltdoc="x">modulo</a></span> function. <a href="part_one.html#%28counter._%28exercise._design12%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara"><a name="(counter._(exercise._design13))"></a><span style="font-weight: bold">Exercise</span>&nbsp;46. Improve the cat animation with a slightly
different 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#%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">cat2</span><span class="hspace">&nbsp;</span><img src="cat2.png" alt="" width="75" height="117"/><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Adjust the rendering function from <a href="part_one.html#%28counter._%28exercise._design12%29%29" data-pltdoc="x">exercise&nbsp;45</a> so that it uses one
cat image or the other based on whether the x-coordinate is odd. Read
up on <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._odd~3f%29%29" class="RktValLink" data-pltdoc="x">odd?</a></span> in the HelpDesk, and use 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
to select cat images. <a href="part_one.html#%28counter._%28exercise._design13%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><a name="(counter._(exercise._design14))"></a><span style="font-weight: bold">Exercise</span>&nbsp;47. Design a world program that maintains and displays a
&ldquo;happiness gauge.&rdquo; Let&rsquo;s call it <span class="RktSym">gauge-prog</span>, and let&rsquo;s agree
that the program consumes the maximum level <span class="RktSym">H</span> of happiness. The gauge
display starts with the maximum score, and with each clock tick, happiness
decreases by <span class="RktVal">0.1</span>; it never falls below <span class="RktVal">0</span>, the minimum
happiness score. Every time the down arrow key is pressed, happiness
decreases by <span class="RktVal">1/5</span>; every time the up arrow is pressed, happiness
jumps by <span class="RktVal">1/3</span>.</p><p>To show the level of happiness, we use a scene with a solid, red rectangle
with a black frame. For a happiness level of 0, the red bar should be
gone; for the maximum happiness level of <span class="RktSym">H</span>, the bar should go all the
way across the scene.</p><p><span style="font-weight: bold">Note</span> When you know enough, we will explain how to combine the gauge
program with the solution of <a href="part_one.html#%28counter._%28exercise._design12%29%29" data-pltdoc="x">exercise&nbsp;45</a>. Then we will be able to
help the cat because as long as you ignore it, it becomes less happy. If
you pet the cat, it becomes happier. If you feed the cat, it becomes much,
much happier. So you can see why you want to know a lot more about
designing world programs than these first three chapters can tell you. <a href="part_one.html#%28counter._%28exercise._design14%29%29" class="ex-end" data-pltdoc="x"></a></p><h3>4<tt>&nbsp;</tt><a name="(part._ch~3aintervals-enums)"></a>Intervals, Enumerations, and Itemizations</h3><p>At the moment, you have four choices to represent information as data:
numbers, strings, images, and Boolean values. For many problems this is
enough, but there are many more for which these four collections of data
in BSL (or other programming languages) don&rsquo;t suffice. Actual designers
need additional ways of representing information as data.</p><p>At a minimum, good programmers must learn to design programs with
restrictions on these built-in collections. One way to restrict is to
enumerate a bunch of elements from a collection and to say that these are
the only ones that are going to be used for some problem. Enumerating
elements works only when there is a finite number of them. To accommodate
collections with &ldquo;infinitely&rdquo; many elements, we introduce intervals,
which are collections of elements that satisfy a specific property.</p><p><span class="refelem"><span class="refcolumn"><span class="refcontent">Infinite may just mean &ldquo;so large that enumerating the
elements is entirely impractical.&rdquo;</span></span></span></p><p>Defining enumerations and intervals means distinguishing among different
kinds of elements. To distinguish in code requires conditional functions,
that is, functions that choose different ways of computing results depending
on the value of some argument. Both <a href="part_prologue.html#%28part._pro-cond%29" data-pltdoc="x">Many Ways to Compute</a> and
<a href="part_one.html#%28part._sec~3aboolean-if%29" data-pltdoc="x">Mixing It Up with Booleans</a> illustrate with examples of how to write such
functions. Neither section uses design, however. Both just present
some new construct in your favorite programming language (that&rsquo;s BSL),
and offer some examples on how to use it.</p><p>In this chapter, we discuss a general design for enumerations and
intervals, new forms of data descriptions. We start with a second look at
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. Then we go through three different kinds of
data descriptions: enumerations, intervals, and itemizations. An
enumeration lists every single piece of data that belongs to it, while an
interval specifies a range of data. The last one, itemizations, mixes the
first two, specifying ranges in one clause of its definition and specific
pieces of data in another. The chapter ends with the general
design strategy for such situations.</p><h4>4.1<tt>&nbsp;</tt><a name="(part._sec~3acond)"></a>Programming with Conditionals</h4><p><div class="SIntrapara">Recall the brief introduction to conditional expressions in
<a href="part_prologue.html" data-pltdoc="x">Prologue: How to Program</a>. Since <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> is the most complicated expression
form in this book, let&rsquo;s take a close look at its general 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><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="refelem"><span class="refcolumn"><span class="refcontent">Brackets make <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> lines stand out. It is fine to use <span class="stt">( ... )</span> in place of <span class="stt">[ ... ]</span>.</span></span></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">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 starts with <span class="stt">(cond</span>, its
<span style="font-style: italic">keyword</span>, and ends in <span class="stt">)</span>. Following the keyword, a
programmer writes as many <span style="font-style: italic"><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> lines</span> as needed; each
<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> line consists of <span style="font-weight: bold">two</span> expressions, enclosed in opening
and closing brackets: <span class="stt">[</span> and <span class="stt">]</span>.</div></p><p>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> line is also known as a <span style="font-style: italic">cond clause</span>.</p><p><div class="SIntrapara">Here is a function definition that uses a conditional expression: <a name="(idx._(gentag._88))"></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">next</span><span class="hspace">&nbsp;</span><span class="RktSym">traffic-light-state</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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="hspace">&nbsp;</span><span class="RktSym">traffic-light-state</span><span class="RktPn">)</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;</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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="hspace">&nbsp;</span><span class="RktSym">traffic-light-state</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</span><span class="hspace">&nbsp;</span><span class="RktSym">traffic-light-state</span><span class="RktPn">)</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></blockquote></div><div class="SIntrapara">Like the mathematical example in <a href="part_prologue.html" data-pltdoc="x">Prologue: How to Program</a>, this example
illustrates the convenience of using <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> expressions. In many
problem contexts, a function must distinguish several different
situations. With 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, you can use one line per
possibility and thus remind the reader of the code for the different
situations from the problem statement.</div></p><p>A note on pragmatics: Contrast <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> expressions with
<span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._if%29%29" class="RktStxLink" data-pltdoc="x">if</a></span> expressions from <a href="part_one.html#%28part._sec~3aboolean-if%29" data-pltdoc="x">Mixing It Up with Booleans</a>. The latter distinguish
one situation from all others. As such, <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._if%29%29" class="RktStxLink" data-pltdoc="x">if</a></span> expressions are
much less suited for multi-situation contexts; they are best used when all we
wish to say is &ldquo;one or the other.&rdquo; We therefore <span style="font-weight: bold">always</span> use
<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> for situations when we wish to remind the reader of our code
that some distinct situations come directly from data definitions. For
other pieces of code, we use whatever construct is most convenient.</p><p>When the conditions get too complex in 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, you
occasionally wish to say something like "in all other cases." For these
kinds of problems, <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> expressions permit the use of the
<span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span> keyword for the very last <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> line:</p><p><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><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktVar">DefaultResultExpression</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">If you make the mistake of using <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span> in some other <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>
line, BSL in DrRacket signals an error:
</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/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="hspace">&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">10</span><span class="RktPn">]</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/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="hspace">&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">10</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">30</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p><span class="RktErr">cond:found an else clause that isn't the last clause in its cond expression</span></p></td></tr></table></blockquote></div><div class="SIntrapara">That is, BSL rejects grammatically incorrect phrases because it makes no
sense to figure out what such a phrase might mean.</div></p><p><div class="SIntrapara">Imagine designing a function that, as part of a game-playing program,
computes some award at the end of the game. Here is its header:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._positivenumber)"></a><span style="font-style: italic">PositiveNumber</span><span class="RktCmt"> is a </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> greater than/equal to </span><span class="RktVal">0</span><span class="RktCmt">. </span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._positivenumber%29" class="techoutside" data-pltdoc="x"><span class="techinside">PositiveNumber</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">computes the reward level from the given score </span><span class="RktSym">s</span></td></tr></table></blockquote></div><div class="SIntrapara">And here are two variants for a side-by-side comparison: <a name="(idx._(gentag._89))"></a>
</div><div class="SIntrapara"><blockquote><table cellspacing="0" cellpadding="0"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://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">reward</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktVal">"bronze"</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#%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#%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">10</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&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._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktVal">"silver"</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="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktVal">"gold"</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><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">(</sp
three full-fledged conditions; on the right, the function comes with an
<span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span> clause. To formulate the last condition for the function
on the left, you must calculate that <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="stt"> </span><span class="RktVal">20</span><span class="stt"> </span><span class="RktSym">s</span><span class="RktPn">)</span> holds because
</div><div class="SIntrapara"><ul><li><p><span class="RktSym">s</span> is in <a href="part_one.html#%28tech._positivenumber%29" class="techoutside" data-pltdoc="x"><span class="techinside">PositiveNumber</span></a></p></li><li><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._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="stt"> </span><span class="RktVal">0</span><span class="stt"> </span><span class="RktSym">s</span><span class="stt"> </span><span class="RktVal">10</span><span class="RktPn">)</span> is <span class="RktVal">#false</span></p></li><li><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._and%29%29" class="RktStxLink" data-pltdoc="x">and</a></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._~3c%29%29" class="RktValLink" data-pltdoc="x">&lt;</a></span><span class="stt"> </span><span class="RktVal">10</span><span class="stt"> </span><span class="RktSym">s</span><span class="RktPn">)</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._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="stt"> </span><span class="RktSym">s</span><span class="stt"> </span><span class="RktVal">20</span><span class="RktPn">)</span><span class="RktPn">)</span> evaluates to <span class="RktVal">#false</span> as well.</p></li></ul></div><div class="SIntrapara">While the calculation looks simple in this case, it is easy to make small
mistakes and to introduce bugs into your program. It is therefore better
to formulate the function definition as shown on the right, <span style="font-weight: bold">if</span> you
know that you want the exact opposite&#8212;<wbr></wbr>called the <span style="font-style: italic">complement</span>&#8212;<wbr></wbr>of
all previous conditions in 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>.</div></p><h4>4.2<tt>&nbsp;</tt><a name="(part._sec~3aworks)"></a>Computing Conditionally</h4><p><div class="SIntrapara">From reading the <a href="part_prologue.html#%28part._pro-cond%29" data-pltdoc="x">Many Ways to Compute</a> and <a href="part_one.html#%28part._sec~3aboolean-if%29" data-pltdoc="x">Mixing It Up with Booleans</a>, you roughly
know how DrRacket evaluates conditional expressions. Let&rsquo;s go over the
idea a bit more precisely for <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> expressions. Take another look
at this definition:<a name="(idx._(gentag._90))"></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">reward</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"bronze"</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#%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#%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">10</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"silver"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&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._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"gold"</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">This function consumes a numeric score&#8212;<wbr></wbr>a positive number&#8212;<wbr></wbr>and produces
a color.</div></p><p>Just looking at 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, you cannot predict
which of the three <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> clauses is going to be used. And that is
the point of a function. The function deals with many different
inputs, for example, <span class="RktVal">2</span>, <span class="RktVal">3</span>, <span class="RktVal">7</span>, <span class="RktVal">18</span>,
<span class="RktVal">29</span>. For each of these inputs, it may have to proceed in a
different manner. Differentiating among the varying classes of inputs is the
purpose of 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.</p><p><div class="SIntrapara">Take, for example
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym">reward</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">You know that DrRacket replaces function applications with the function&rsquo;s
body after substituting the argument for the parameter. Hence,
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">reward</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">say </span><span class="RktCmt">&ldquo;</span><span class="RktCmt">equals</span><span class="RktCmt">&rdquo;</span></td></tr><tr><td><span class="RktSym">==</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._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="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="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"bronze"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&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#%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#%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">10</span><span class="hspace">&nbsp;</span><span class="RktVal">3</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._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"silver"</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._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"gold"</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">At this point, DrRacket evaluates one condition at a time. For the
<span style="font-weight: bold">first</span> one to evaluate to <span class="RktVal">#true</span>, it continues with the
result expression:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">reward</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</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._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="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="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"bronze"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&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#%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#%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">10</span><span class="hspace">&nbsp;</span><span class="RktVal">3</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._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"silver"</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._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"gold"</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</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._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="RktVal">#true</span><span class="hspace">&nbsp;</span><span class="RktVal">"bronze"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&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#%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#%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">10</span><span class="hspace">&nbsp;</span><span c
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">reward</span><span class="hspace">&nbsp;</span><span class="RktVal">21</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</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._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="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="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktVal">21</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"bronze"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&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#%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#%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">10</span><span class="hspace">&nbsp;</span><span class="RktVal">21</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._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktVal">21</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"silver"</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._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"gold"</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</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._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="RktVal">#false</span><span class="hspace">&nbsp;</span><span class="RktVal">"bronze"</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&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#%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#%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">10</span><span class="hspace">&nbsp;</span><s
mentioned in <a href="part_prologue.html#%28part._pro-cond%29" data-pltdoc="x">Many Ways to Compute</a> 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> clause is
dropped. The rest of the calculation proceeds as expected:
</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="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#%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">10</span><span class="hspace">&nbsp;</span><span class="RktVal">21</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._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktVal">21</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"silver"</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._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"gold"</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</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._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="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="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="RktVal">21</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"silver"</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._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"gold"</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</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._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="RktPn">(</span><span class="RktSym
and thus the calculation proceeds to the third <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> line. The
<span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span> tells DrRacket to replace 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
with the answer from this clause.</div></p><p><a name="(counter._(exercise._cond1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;48. Enter the definition of <span class="RktSym">reward</span> followed by
<span class="RktPn">(</span><span class="RktSym">reward</span><span class="stt"> </span><span class="RktVal">18</span><span class="RktPn">)</span> into the definitions area of DrRacket and use the stepper
to find out <span style="font-weight: bold">how</span> DrRacket evaluates applications of the function. <a href="part_one.html#%28counter._%28exercise._cond1%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara"><a name="(counter._(exercise._cond1-nested))"></a><span style="font-weight: bold">Exercise</span>&nbsp;49. 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 is really just an expression
and may therefore show up in the middle of another expression:
</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">200</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._cond%29%29" class="RktStxLink" data-pltdoc="x">cond</a></span><span class="hspace">&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">y</span><span class="hspace">&nbsp;</span><span class="RktVal">200</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">0</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#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Use the stepper to evaluate the expression for <span class="RktSym">y</span> as <span class="RktVal">100</span> and <span class="RktVal">210</span>.
<a name="(idx._(gentag._91))"></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="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="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">create-rocke
Consider the function for launching a rocket, repeated in
<a href="part_one.html#%28counter._%28figure._fig~3av5-from-prologue%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">20</span></a>. Both branches of 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 have the same shape except as indicated
with <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span>:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><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="RktSym">X</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktSym">MTSCN</span><span class="RktPn">)</span></p></blockquote></div></p><p>Reformulate <span class="RktSym">create-rocket-scene.v5</span> to use a <span class="RktSym">nested</span>
expression; the resulting function mentions <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> only
once. <a href="part_one.html#%28counter._%28exercise._cond1-nested%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>4.3<tt>&nbsp;</tt><a name="(part._sec~3aenums)"></a>Enumerations</h4><p><div class="SIntrapara">Not all strings represent mouse events. If you looked in HelpDesk when the
last section introduced the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-mouse%29%29" class="RktStxLink" data-pltdoc="x">on-mouse</a></span> clause for <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>,
you found out that only six strings are used to notify programs of mouse events:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._mouseevt)"></a><span style="font-style: italic">MouseEvt</span><span class="RktCmt"> is one of these </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktCmt">s:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"button-down"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"button-up"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"drag"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"move"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"enter"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"leave"</span></td></tr></table></blockquote></div><div class="SIntrapara">The interpretation of these strings is quite obvious. One of the first two
strings shows up when the computer user clicks the mouse button or
releases it. In contrast, the third and fourth are about moving the mouse
and possibly holding down the mouse button at the same time. Finally, the
last two strings represent the events of a mouse moving over the edge of
the canvas: either going into the canvas from the outside or exiting the
canvas.</div></p><p><div class="SIntrapara">More importantly, the data definition for representing mouse events as
strings looks quite different from the data definitions we have seen so
far. It is called an <span style="font-style: italic">enumeration</span>, and it is a data
representation in which every possibility is listed. It should not come as
a surprise that enumerations are common. Here is a simple one:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._trafficlight)"></a><span style="font-style: italic">TrafficLight</span><span class="RktCmt"> is one of the following </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktCmt">s:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"red"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"green"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"yellow"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> the three strings represent the three </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">possible states that a traffic light may assume </span></td></tr></table></blockquote></div><div class="SIntrapara">It is a simplistic<span class="refelem"><span class="refcolumn"><span class="refcontent">We call it &ldquo;simplistic&rdquo; because it does
not include the &ldquo;off&rdquo; state, the &ldquo;blinking red&rdquo; state, or the &ldquo;blinking
yellow&rdquo; state.</span></span></span> representation of the states that a traffic
light can take on. Unlike others, this data definition also uses a
slightly different phrase to explain what the term <a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a>
means, but this is an inessential difference.</div></p><p><div class="SIntrapara">Programming with enumerations is mostly straightforward. When a function&rsquo;s
input is a class of data whose description spells out its elements on a
case-by-case basis, the function should distinguish just those cases and
compute the result on a per-case basis. For example, if you wanted to
define a function that computes the next state of a traffic light, given
the current state as an element of <a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a>, you would come up
with a definition like this one: <a name="(idx._(gentag._92))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">yields the next state given current state </span><span class="RktSym">s</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">traffic-light-next</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</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="RktPn">(</span><span class="RktSym">traffic-light-next</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</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;</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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</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></blockquote></div><div class="SIntrapara">Because the data definition for <a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a> consists of three
distinct elements, the <span class="RktSym">traffic-light-next</span> function naturally
distinguishes between three different cases. For each case, the result
expression is just another string, the one that corresponds to the next
case.</div></p><p><a name="(counter._(exercise._cond2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;50. If you copy and paste the above function definition into the
definitions area of DrRacket and click <span class="emph">RUN</span>, DrRacket highlights two of
the three <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> lines. This coloring tells you that your test cases
do not cover the full <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. Add enough tests to make DrRacket happy. <a href="part_one.html#%28counter._%28exercise._cond2%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._cond3))"></a><span style="font-weight: bold">Exercise</span>&nbsp;51. Design a <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> program that simulates a
traffic light for a given duration. The program renders the state of
a traffic light as a solid circle of the appropriate color, and it
changes state on every clock tick. <span style="font-weight: bold">Hint</span> Read the documentation
for <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>; there is a reason all these &ldquo;words&rdquo; are
linked to their documentation. What is the most appropriate initial
state? Ask your engineering friends. <a href="part_one.html#%28counter._%28exercise._cond3%29%29" class="ex-end" data-pltdoc="x"></a></p><p>The main idea of an enumeration is that it defines a collection of data
as a <span style="font-weight: bold">finite</span> number of pieces of data. Each item explicitly
spells out which piece of data belongs to the class of data that we are
defining. Usually, the piece of data is just shown as is; on some
occasions, the
item of an enumeration is an English sentence that describes a finite
number of elements of pieces of data with a single phrase.</p><p><div class="SIntrapara">Here is an important example:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._1string)"></a><span style="font-style: italic">1String</span><span class="RktCmt"> is a </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktCmt"> of length </span><span class="RktVal">1</span><span class="RktCmt">, </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">including</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"\\"</span><span class="RktCmt"> (the backslash),</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">" "</span><span class="RktCmt"> (the space bar), </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"\t"</span><span class="RktCmt"> (tab),</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"\r"</span><span class="RktCmt"> (return), and </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"\b"</span><span class="RktCmt"> (backspace).</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> represents keys on the keyboard</span></td></tr></table></blockquote></div><div class="SIntrapara">You know that such a data definition is proper if you can describe all of
its elements with a BSL test. In the case of <a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a>, you can find
out whether some string <span class="RktSym">s</span> belongs to the collection 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._~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="RktSym">s</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">An alternative way to check that you have succeeded is to enumerate all
the members of the collection of data that you wish to describe:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><span style="font-style: italic">1String</span><span class="RktCmt"> is one of: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"q"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"w"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"e"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"r"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span>...</td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"\t"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"\r"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"\b"</span></td></tr></table></blockquote></div></p><p><div class="SIntrapara">If you look at your keyboard, you find <span class="RktInBG"><span class="hspace"></span><span class="RktIn">&#8592;</span><span class="hspace"></span></span>, <span class="RktInBG"><span class="hspace"></span><span class="RktIn">&#8593;</span><span class="hspace"></span></span>, and
similar labels. Our chosen programming language, BSL, uses its own data
definition to represent this information. Here is an
excerpt:<span class="refelem"><span class="refcolumn"><span class="refcontent">You know where to find the full definition.</span></span></span>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._keyevent)"></a><span style="font-style: italic">KeyEvent</span><span class="RktCmt"> is one of: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"left"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"right"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"up"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span>...</td></tr></table></blockquote></div><div class="SIntrapara">The first item in this enumeration describes the same bunch of strings
that <a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a> describes. The clauses that follow enumerate strings
for special key events, such as pressing one of the four arrow keys or
releasing a key.</div></p><p><div class="SIntrapara">At this point, we can actually design a key-event handler systematically.
Here is a sketch: <a name="(idx._(gentag._93))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a><span class="RktCmt"> -&gt; </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">handle-key-events</span><span class="hspace">&nbsp;</span><span class="RktSym">w</span><span class="hspace">&nbsp;</span><span class="RktSym">ke</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._~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="RktSym">ke</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"left"</span><span class="hspace">&nbsp;</span><span class="RktSym">ke</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"right"</span><span class="hspace">&nbsp;</span><span class="RktSym">ke</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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/begi
each line in the enumeration of the data definition, there is one
<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> line. The condition in the first <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> line
identifies the <a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a>s identified in the first line of the
enumeration, the second <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> clause corresponds to the second
data enumeration line, and so on.<a name="(idx._(gentag._94))"></a></div></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">A </span><a name="(tech._position)"></a><span style="font-style: italic">Position</span><span class="RktCmt"> is a </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt">.</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> distance between the left margin and the ball </span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._position%29" class="techoutside" data-pltdoc="x"><span class="techinside">Position</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._position%29" class="techoutside" data-pltdoc="x"><span class="techinside">Position</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">computes the next location of the ball </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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">keh</span><span class="hspace">&nbsp;</span><span class="RktVal">13</span><span class="hspace">&nbsp;</span><span class="RktVal">"left"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">8</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">keh</span><span class="hspace">&nbsp;</span><span class="RktVal">13</span><span class="hspace">&nbsp;</span><span class="RktVal">"right"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">18</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">keh</span><span class="hspace">&nbsp;</span><span class="RktVal">13</span><span class="hspace">&nbsp;</span><span class="RktVal">"a"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">13</span><span class="RktPn">)</span></td></tr></table><table cellspacing="0" cellpadding="0"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="https://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">keh</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktSym">k</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym"><a href="h
language (such as BSL) or its teachpacks (such as <span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/universe</span></span> teachpack</span>),
it is common that they use only a part of the enumeration. To illustrate
this point, let us look at a representative problem.
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> Design a key-event handler that moves a red dot left or
right on a horizontal line in response to pressing the left and right
arrow keys.</p></blockquote></div><div class="SIntrapara"><a href="part_one.html#%28counter._%28figure._fig~3acond-enum%29%29" data-pltdoc="x">Figure&nbsp;<span class="FigureRef">21</span></a> presents <span style="font-weight: bold">two</span> solutions to this problem.
The function on the left is organized according to the basic idea of using
one <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> clause per line in the data definition of the input,
<a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a>. In contrast, the right-hand side displays a version that
uses the three essential lines: two for the keys that matter and one for
everything else. The reordering is appropriate because only two of 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>-lines are relevant, and they can be cleanly separated from
other lines. Naturally, this kind of rearrangement is done <span style="font-weight: bold">after</span>
the function is designed properly.</div></p><h4>4.4<tt>&nbsp;</tt><a name="(part._sec~3aintervals)"></a>Intervals</h4><p><div class="SIntrapara">Imagine yourself responding to the following sample design task:
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> Design a program that simulates the descent of a UFO.</p></blockquote></div><div class="SIntrapara">After a bit of thinking, you could come up with something like
<a href="part_one.html#%28counter._%28figure._fig~3aufo%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">22</span></a>. Stop! Study the definitions and replace the dots
before you read on. <a name="(idx._(gentag._95))"></a> <a name="(idx._(gentag._96))"></a> <a name="(idx._(gentag._97))"></a></div></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">A </span><a name="(tech._worldstate)"></a><span style="font-style: italic">WorldState</span><span class="RktCmt"> is a </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt">.</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> number of pixels between the top and the UFO</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="RktSym">WIDTH</span><span class="hspace">&nbsp;</span><span class="RktVal">300</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">distances in terms of pixels </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="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">CLOSE</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="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktVal">3</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">MTSCN</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></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">UFO</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._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-la
display of the status line to the canvas:
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> Add a status line. It says <span class="RktVal">"descending"</span> when
the UFO&rsquo;s height is above one third of the height of the canvas. It
switches to <span class="RktVal">"closing in"</span> below that. And finally, when the UFO has reached
the bottom of the canvas, the status notifies the player that the UFO has
<span class="RktVal">"landed"</span>. You are free to use appropriate colors for the status line.</p></blockquote></div></p><p>In this case, we don&rsquo;t have a finite enumeration of distinct elements or
distinct sub-classes of data. After all, conceptually, the interval between
<span class="RktVal">0</span> and <span class="RktSym">HEIGHT</span> (for some number greater than <span class="RktVal">0</span>)
contains an infinite number of numbers and a large number of
integers. Therefore we use intervals to superimpose some organization on
the generic data definition, which just uses &ldquo;numbers&rdquo; to describe the
class of coordinates.</p><p>An <span style="font-style: italic">interval</span> is a description of a class of numbers via
boundaries. The simplest interval has two boundaries: left and right. If
the left boundary is to be included in the interval, we say it is
<span style="font-style: italic">closed</span> on the left. Similarly, a right-closed interval includes
its right boundary. Finally, if an interval does not include a boundary,
it is said to be <span style="font-style: italic">open</span> at that boundary.</p><p><div class="SIntrapara">Pictures of, and notations for, intervals use brackets for closed
boundaries and parentheses for open boundaries. Here are four such intervals:
</div><div class="SIntrapara"><ul><li><p><span style="font-style: italic"></span>[<span style="font-style: italic"></span>3<span style="font-style: italic">,</span>5<span style="font-style: italic"></span>]<span style="font-style: italic"></span> is a closed interval:</p><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_53.png" alt="image" width="258.0" height="51.2390625"/></p></li><li><p><span style="font-style: italic"></span>(<span style="font-style: italic"></span>3<span style="font-style: italic">,</span>5<span style="font-style: italic"></span>]<span style="font-style: italic"></span> is a left-open interval:</p><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_54.png" alt="image" width="258.0" height="51.2390625"/></p></li><li><p><span style="font-style: italic"></span>[<span style="font-style: italic"></span>3<span style="font-style: italic">,</span>5<span style="font-style: italic"></span>)<span style="font-style: italic"></span> is a right-open interval:</p><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_55.png" alt="image" width="258.0" height="51.2390625"/></p></li><li><p>and <span style="font-style: italic"></span>(<span style="font-style: italic"></span>3<span style="font-style: italic">,</span>5<span style="font-style: italic"></span>)<span style="font-style: italic"></span> is an open interval:</p><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_56.png" alt="image" width="258.0" height="51.2390625"/></p></li></ul></div></p><p><a name="(counter._(exercise._cond4))"></a><span style="font-weight: bold">Exercise</span>&nbsp;52. Which integers are contained in the four intervals above? <a href="part_one.html#%28counter._%28exercise._cond4%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara">The interval concept helps us formulate a data definition that captures the
revised problem statement better than the &ldquo;numbers&rdquo;-based definition:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A WorldState falls into one of three intervals: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> between </span><span class="RktVal">0</span><span class="RktCmt"> and </span><span class="RktSym">CLOSE</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> between </span><span class="RktSym">CLOSE</span><span class="RktCmt"> and </span><span class="RktSym">HEIGHT</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> below </span><span class="RktSym">HEIGHT</span></td></tr></table></blockquote></div></p><p>Specifically, there are three intervals, which we may picture as follows:</p><p><div class="SIntrapara"><blockquote class="SCentered"><p><img style="vertical-align: -1.5px; margin: -3px -3px -3px -3px;" src="pict_57.png" alt="image" width="91.891845703125" height="123.36328125"/></p></blockquote></div><div class="SIntrapara">What you see is the standard number line, turned vertical and broken into
intervals. Each interval starts with an angular downward-pointing bracket
(<img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_58.png" alt="image" width="28" height="11"/>) and ends with an upward-pointing bracket
(<img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_59.png" alt="image" width="28.0" height="11.0"/>). The picture identifies three intervals in this manner:
</div><div class="SIntrapara"><ul><li><p>the upper interval goes from <span class="RktVal">0</span> to <span class="RktSym">CLOSE</span>;</p></li><li><p>the middle one starts at <span class="RktSym">CLOSE</span> and reaches
<span class="RktSym">HEIGHT</span>;<span class="refelem"><span class="refcolumn"><span class="refcontent">On a plain number line, the last interval
starts at <span class="RktSym">HEIGHT</span> and goes on forever.</span></span></span> and</p></li><li><p>the lower, invisible interval is just a single line at
<span class="RktSym">HEIGHT</span>.</p></li></ul></div></p><p>Visualizing the data definition in this manner helps with the design of
functions in two ways. First, it immediately suggests how to pick
examples. Clearly we want the function to work inside of all the intervals,
and we want the function to work properly at the ends of each
interval. Second, the image tells us that we need to formulate a condition
that determines whether or not some &ldquo;point&rdquo; is within one of the intervals.</p><p>Putting the two together also raises a question, namely, how exactly the
function deals with the end points. In the context of our example,
two points on the number line belong to two intervals: <span class="RktSym">CLOSE</span>
belongs to both the upper interval and the middle one, while <span class="RktSym">HEIGHT</span>
seems to fall into both the middle one and the lowest one. Such overlaps usually
cause problems for programs, and they ought to be avoided.</p><p><div class="SIntrapara">BSL functions avoid them naturally due to the way <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>
expressions are evaluated. Consider this natural organization of a function
that consumes elements of <span class="RktSym">WorldState</span>:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a></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">f</span><span class="hspace">&nbsp;</span><span class="RktSym">y</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="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSE</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSE</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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~3d%29%29" class="RktValLink" data-pltdoc="x">&gt;=</a></span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">The three <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> lines correspond to the three intervals. Each
condition identifies those values of <span class="RktSym">y</span> that are in between the
limits of the intervals. Due to the way <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> lines are checked one
by one, however, a <span class="RktSym">y</span> value of <span class="RktSym">CLOSE</span> makes BSL pick the
first <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> line, and a <span class="RktSym">y</span> value of <span class="RktSym">HEIGHT</span>
triggers the evaluation of the second <span class="RktVar">ResultExpression</span>.</div></p><p><div class="SIntrapara">If we wanted to make this choice obvious and immediate for every reader of
our code, we would use different conditions:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a></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">g</span><span class="hspace">&nbsp;</span><span class="RktSym">y</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="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSE</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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#%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#%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">CLOSE</span><span class="hspace">&nbsp;</span><span class="RktSym">y</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._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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">y</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28for
to combine a strictly-less check with a less-than-or-equal
check instead of <span class="RktSym">f</span>&rsquo;s <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> with three arguments.
<a name="(idx._(gentag._98))"></a></div></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><a href="part_one.html#%28tech._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds a status line to the scene created by </span><span class="RktSym">render</span><span class="hspace">&nbsp;&nbsp;</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">render/status</span><span class="hspace">&nbsp;</span><span class="RktVal">10</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;&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="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"descending"</span><span class="hspace">&nbsp;</span><span class="RktVal">11</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">10</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">render</span><span class="hspace">&nbsp;</span><span class="RktVal">10</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="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">render/status</span><span class="hspace">&nbsp;</span><span class="RktSym">y</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="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="
the requested status line to our UFO animation; see
<a href="part_one.html#%28counter._%28figure._fig~3arender-status%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">23</span></a> for the complete definition. The function
uses 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 to distinguish the three intervals. In each
<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> clause, the <span class="RktVar">ResultExpression</span> uses <span class="RktSym">render</span>
(from <a href="part_one.html#%28counter._%28figure._fig~3aufo%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">22</span></a>) to create the image with the descending UFO and
then places an appropriate text at position <span style="font-style: italic"></span>(<span style="font-style: italic"></span>1<span style="font-style: italic"></span>0<span style="font-style: italic">,</span>1<span style="font-style: italic"></span>0<span style="font-style: italic"></span>)<span style="font-style: italic"></span> 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>.</p><p><div class="SIntrapara">To run this version, you need to change <span class="RktSym">main</span> from
<a href="part_one.html#%28counter._%28figure._fig~3aufo%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">22</span></a> a bit: <a name="(idx._(gentag._99))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a></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">main</span><span class="hspace">&nbsp;</span><span class="RktSym">y0</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktSym">y0</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/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace">&nbsp;</span><span class="RktSym">nxt</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/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace">&nbsp;</span><span class="RktSym">render/status</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p><div class="SIntrapara">One aspect of this function definition might disturb you, and to clarify
why, let&rsquo;s refine the sample problem from above just a tiny bit:
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> Add a status line, <span style="font-weight: bold">positioned at <span style="font-style: italic"></span>(<span style="font-style: italic"></span>2<span style="font-style: italic"></span>0<span style="font-style: italic">,</span>2<span style="font-style: italic"></span>0<span style="font-style: italic"></span>)<span style="font-style: italic"></span></span>,
that says &ldquo;descending&rdquo; when the UFO&rsquo;s height is above one third of the
height of the canvas. ...</p></blockquote></div><div class="SIntrapara">This could be the response of a client who has watched your animation for a
first time. <a name="(idx._(gentag._100))"></a></div></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><a href="part_one.html#%28tech._worldstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">WorldState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds a status line to the scene created by </span><span class="RktSym">render</span><span class="hspace">&nbsp;&nbsp;</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">render/status</span><span class="hspace">&nbsp;</span><span class="RktVal">42</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;&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="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"closing in"</span><span class="hspace">&nbsp;</span><span class="RktVal">11</span><span class="hspace">&nbsp;</span><span class="RktVal">"orange"</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">20</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">render</span><span class="hspace">&nbsp;</span><span class="RktVal">42</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="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">render/status</span><span class="hspace">&nbsp;</span><span class="RktSym">y</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></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&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;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><a h
<span class="RktSym">render/status</span> at <span style="font-weight: bold">six</span> distinct places because you have three
copies of one external piece of information: the location of the status
line. To avoid multiple changes for a single element, programmers try to
avoid copies. You have two choices to fix this problem. The first one
is to use constant definitions, which you might recall from early
chapters. The second one is to think of 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 as an
expression that may appear anywhere in a function, including in the middle
of some other expression; see <a href="part_one.html#%28counter._%28figure._fig~3arender-status2%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">24</span></a> and compare
with <a href="part_one.html#%28counter._%28figure._fig~3arender-status%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">23</span></a>. In this revised definition of
<span class="RktSym">render/status</span>, 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 is the first argument
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>. As you can see, its result is always a
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span> image that is placed at position <span style="font-style: italic"></span>(<span style="font-style: italic"></span>2<span style="font-style: italic"></span>0<span style="font-style: italic">,</span>2<span style="font-style: italic"></span>0<span style="font-style: italic"></span>)<span style="font-style: italic"></span> into the
image created by <span class="RktPn">(</span><span class="RktSym">render</span><span class="stt"> </span><span class="RktSym">y</span><span class="RktPn">)</span>.</p><h4>4.5<tt>&nbsp;</tt><a name="(part._itemization._sec~3aitemization)"></a>Itemizations</h4><p>An interval distinguishes different sub-classes of numbers, which, in
principle, is an infinitely large class. An enumeration spells out item
for item the useful elements of an existing class of data. Some data
definitions need to include elements from both. They use
<span style="font-style: italic">itemizations</span>, which generalize intervals and enumerations. They
allow the combination of any already-defined data classes with each other
and with individual pieces of data.</p><p>Consider the following example, a rewrite of an important data definition
from <a href="part_one.html#%28part._sec~3aenums%29" data-pltdoc="x">Enumerations</a>:</p><p><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._itemization._keyevent)"></a><span style="font-style: italic">KeyEvent</span><span class="RktCmt"> is one of: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"left"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"right"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"up"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span>...</td></tr></table></blockquote></div><div class="SIntrapara">In this case, the <span class="RktSym">KeyEvent</span> data definition refers to the
<a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a> data definition. Since functions that deal with
<a href="part_one.html#%28tech._itemization._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a>s often deal with <a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a>s separately from the rest
and do so with auxiliary functions, we now have a convenient way to
express signatures for these functions, too.</div></p><p><div class="SIntrapara">The description of the <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> primitive employs the idea
of an itemization in a sophisticated way. Its signature is <a name="(idx._itemization._(gentag._101._itemization))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._itemization._norf%29" class="techoutside" data-pltdoc="x"><span class="techinside">NorF</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">converts the given string into a number;</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">produces </span><span class="RktVal">#false</span><span class="RktCmt"> if impossible </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"><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="RktSym">s</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#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">meaning that the result signature names a simple class of data:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">An </span><a name="(tech._itemization._norf)"></a><span style="font-style: italic">NorF</span><span class="RktCmt"> is one of: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">#false</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> a </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a></td></tr></table></blockquote></div><div class="SIntrapara">This itemization combines one piece of data (<span class="RktVal">#false</span>)
with a large, and distinct, class of data (Number).</div></p><p><div class="SIntrapara">Now imagine a function that consumes the result of <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>
and adds <span class="RktVal">3</span>, dealing with <span class="RktVal">#false</span> as if it were <span class="RktVal">0</span>:
<a name="(idx._itemization._(gentag._102._itemization))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._itemization._norf%29" class="techoutside" data-pltdoc="x"><span class="techinside">NorF</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds </span><span class="RktVal">3</span><span class="RktCmt"> to the given number; </span><span class="RktVal">3</span><span class="RktCmt"> otherwise</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">add3</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">)</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">add3</span><span class="hspace">&nbsp;</span><span class="RktVal">0.12</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">3.12</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="RktPn">(</span><span class="RktSym">add3</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._false~3f%29%29" class="RktValLink" data-pltdoc="x">false?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&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._else%29%29" class="RktStxLink" data-pltdoc="x">else</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="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktVal">3</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">As above, the function&rsquo;s body consists of a <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._con
as many clauses as there are items in the enumeration of the data
definition. The first <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> clause recognizes when the function is
applied to <span class="RktVal">#false</span>; the corresponding result is <span class="RktVal">3</span> as
requested. The second clause is about numbers and adds <span class="RktVal">3</span> as
required.</div></p><p>Let&rsquo;s study a somewhat more purposeful design task:</p><blockquote><p><span style="font-weight: bold">Sample Problem</span> Design a program that launches a rocket when the user of
your program presses the space bar. The program first displays the rocket
sitting at the bottom of the canvas. Once launched, it moves upward at
three pixels per clock tick.</p></blockquote><p><div class="SIntrapara">This revised version suggests a representation with two classes of states:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">An </span><a name="(tech._itemization._lr)"></a><span style="font-style: italic">LR</span><span class="RktCmt"> (short for </span><span style="font-style: italic">launching rocket</span><span class="RktCmt">) is one of:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"resting"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><a href="part_one.html#%28tech._nonnegativenumber%29" class="techoutside" data-pltdoc="x"><span class="techinside">NonnegativeNumber</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> </span><span class="RktVal">"resting"</span><span class="RktCmt"> represents a grounded rocket</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">a number denotes the height of a rocket in flight</span></td></tr></table></blockquote></div><div class="SIntrapara">While the interpretation of <span class="RktVal">"resting"</span> is obvious, the
interpretation of numbers is ambiguous in its notion of height:
</div><div class="SIntrapara"><ol><li><p>the word &ldquo;height&rdquo; could refer to the distance between the ground
and the rocket&rsquo;s point of reference, say, its center; or</p></li><li><p>it could mean the distance between the top of the canvas and the
reference point.</p></li></ol></div><div class="SIntrapara">Either one works fine. The second one uses the conventional computer
meaning of the word &ldquo;height.&rdquo; It is thus slightly more convenient for
functions that translate the state of the world into an image, and we
therefore choose to interpret the number in that spirit.</div></p><p>To drive home this choice, <a href="part_one.html#%28counter._itemization._%28exercise._interpret-height%29%29" data-pltdoc="x">exercise&nbsp;57</a> below asks you to solve
the exercises of this section using the first interpretation of height.</p><p><a name="(counter._itemization._(exercise._cond5))"></a><span style="font-weight: bold">Exercise</span>&nbsp;53. The design recipe for world programs demands that you
translate information into data and vice versa to ensure a complete
understanding of the data definition. It&rsquo;s best to draw some
world scenarios and to represent them with data and, conversely, to pick
some data examples and to draw pictures that match them. Do so for the
<a href="part_one.html#%28tech._itemization._lr%29" class="techoutside" data-pltdoc="x"><span class="techinside">LR</span></a> definition, including at least <span class="RktSym">HEIGHT</span> and <span class="RktVal">0</span> as
examples. <a href="part_one.html#%28counter._itemization._%28exercise._cond5%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara">In reality, rocket launches come with countdowns:
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> Design a program that launches a rocket when the user
presses the space bar. At that point, the simulation starts a countdown
for three ticks, before it displays the scenery of a rising rocket. The
rocket should move upward at a rate of three pixels per clock tick.</p></blockquote></div></p><p><div class="SIntrapara">Following the program design recipe, we first collect constants:
</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="RktVal">300</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">distances in pixels </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">YDELTA</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="RktSym">BACKG</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><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">5</span><span class="hspace">&nbsp;</span><span class="RktVal">30</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><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="RktSym">CENTER</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>
canvas and the background scene, <span class="RktSym">YDELTA</span> describes how fast
the rocket moves along the y-axis, as specified in the problem statement.
The <span class="RktSym">CENTER</span> constant is the <span style="font-weight: bold">computed</span> center of the rocket.</div></p><p><div class="SIntrapara">Next we turn to the development of a data definition. This revision of the
problem clearly calls for three distinct sub-classes of states:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">An </span><a name="(tech._itemization._lrcd)"></a><span style="font-style: italic">LRCD</span><span class="RktCmt"> (for </span><span style="font-style: italic">launching rocket countdown</span><span class="RktCmt">) is one of:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"resting"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> a </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> between </span><span class="RktVal"><span class="nobreak">-3</span></span><span class="RktCmt"> and </span><span class="RktVal"><span class="nobreak">-1</span></span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> a </span><a href="part_one.html#%28tech._nonnegativenumber%29" class="techoutside" data-pltdoc="x"><span class="techinside">NonnegativeNumber</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> a grounded rocket, in countdown mode,</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">a number denotes the number of pixels between the</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">top of the canvas and the rocket (its height)</span></td></tr></table></blockquote></div><div class="SIntrapara">The second, new sub-class of data&#8212;<wbr></wbr>three negative numbers&#8212;<wbr></wbr>represents the
world after the user pressed the space bar and before the rocket lifts off.</div></p><p><div class="SIntrapara">At this point, we write down our wish list for a function that renders
states as images and for any event-handling functions that we may need:
<a name="(idx._itemization._(gentag._103._itemization))"></a> <a name="(idx._itemization._(gentag._104._itemization))"></a> <a name="(idx._itemization._(gentag._105._itemization))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._itemization._lrcd%29" class="techoutside" data-pltdoc="x"><span class="techinside">LRCD</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">renders the state as a resting or flying 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="RktPn">(</span><span class="RktSym">show</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="RktSym">BACKG</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><a href="part_one.html#%28tech._itemization._lrcd%29" class="techoutside" data-pltdoc="x"><span class="techinside">LRCD</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._itemization._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._itemization._lrcd%29" class="techoutside" data-pltdoc="x"><span class="techinside">LRCD</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">starts the countdown when space bar is pressed, </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">if the rocket is still resting </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">launch</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">ke</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktSym">x</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><a href="part_one.html#%28tech._itemization._lrcd%29" class="techoutside" data-pltdoc="x"><span class="techinside">LRCD</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._itemization._lrcd%29" class="techoutside" data-pltdoc="x"><span class="techinside">LRCD</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">raises the rocket by </span><span class="RktSym">YDELTA</span><span class="RktCmt">,</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt"> if it is moving already </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">fly</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="RktSym">x</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Remember that the <a name="(idx._itemization._(gentag.
though the choice of names for the data collection and the event handlers
are ours. Also, we have specialized the purpose statements to fit our
problem statement.</div></p><p><div class="SIntrapara">From here, we use the design recipe for functions to create complete
definitions for all three of them, starting with examples for the first
one:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span></td></tr><tr><td><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">show</span><span class="hspace">&nbsp;</span><span class="RktVal">"resting"</span><span class="RktPn">)</span></td></tr><tr><td><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._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">10</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKG</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"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span></td></tr><tr><td><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">show</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-2</span></span><span class="RktPn">)</span></td></tr><tr><td><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._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._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"-2"</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</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;&nbsp;</span><span class="RktVal">10</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/4</span><span class="hspace">&nbsp;</span><span class="RktSym">WIDTH</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;&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">10</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKG</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="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span></td
definition. The first example shows the resting state, the second the
middle of a countdown, and the last one the rocket in flight.
Furthermore, we express the expected values as expressions
that draw appropriate images. We used DrRacket&rsquo;s interactions area to create
these images; what would <span style="font-weight: bold">you</span> do?</div></p><p>A close look at the examples reveals that making examples also means making
choices. Nothing in the problem statement actually demands how exactly the
rocket is displayed before it is launched, but doing so is
natural. Similarly, nothing says to display a number during the countdown,
yet it adds a nice touch. Lastly, if you solved <a href="part_one.html#%28counter._itemization._%28exercise._cond5%29%29" data-pltdoc="x">exercise&nbsp;53</a> you
also know that <span class="RktVal">0</span> and <span class="RktSym">HEIGHT</span> are special points for the
third clause of the data definition.</p><p><div class="SIntrapara">In general, intervals deserve special attention when you make up examples,
that is, they deserve at least three kinds of examples: one from each end and
another one from inside. Since the second sub-class of <a href="part_one.html#%28tech._itemization._lrcd%29" class="techoutside" data-pltdoc="x"><span class="techinside">LRCD</span></a> is a
(finite) interval and the third one is a half-open interval, let&rsquo;s
take a look at their end points:
</div><div class="SIntrapara"><ul><li><p>Clearly, <span class="RktPn">(</span><span class="RktSym">show</span><span class="stt"> </span><span class="RktVal"><span class="nobreak">-3</span></span><span class="RktPn">)</span> and <span class="RktPn">(</span><span class="RktSym">show</span><span class="stt"> </span><span class="RktVal"><span class="nobreak">-1</span></span><span class="RktPn">)</span> must produce
images like the one for <span class="RktPn">(</span><span class="RktSym">show</span><span class="stt"> </span><span class="RktVal"><span class="nobreak">-2</span></span><span class="RktPn">)</span>. After all, the rocket still
rests on the ground, even if the countdown numbers differ.</p></li><li><p><div class="SIntrapara">The case for <span class="RktPn">(</span><span class="RktSym">show</span><span class="stt"> </span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span> is different. According to our
agreement, the value <span class="RktSym">HEIGHT</span> represents the state when the rocket has just
been launched. Pictorially this means the rocket is still resting on the
ground. Based on the last test case above, here is the test case that
expresses this insight:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span></td></tr><tr><td><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">show</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span></td></tr><tr><td><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._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">10</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKG</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Except that if you evaluate the &ldquo;expected value&rdquo; expression by itself in
DrRacket&rsquo;s interactions area, you see that the rocket is halfway underground.
This shouldn&rsquo;t be the case, of course, meaning that we need to adjust this test
case and the above:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span></td></tr><tr><td><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">show</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="RktPn">)</span></td></tr><tr><td><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._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">10</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="RktSym">CENTER</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKG</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"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span></td></tr><tr><td><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">show</span><span class="hspace">&nbsp;</span><span class="RktVal">53</span><span class="RktPn">)</span></td></tr><tr><td><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._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">10</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">53</span><span class="hspace">&nbsp;</span><span class="RktSym">CENTER</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKG</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p></li><li><p>Finally, determine the result you now expect from <span class="RktPn">(</span><span class="RktSym">show</span><span class="stt"> </span><span class="RktVal">0</span><span class="RktPn">)</span>. It is a simple but revealing exercise.</p></li></ul></div></p><p><div class="SIntrapara">Following the precedents in this chapter, <span class="RktSym">show</span> uses 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 to deal with the three clauses of the data
definition: <a name="(idx._itemization._(gentag._107._itemization))"></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">show</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._string~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span></span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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~3d%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="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Each clause identifies the corresponding sub-class with a precise
condition: <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~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></span><span class="stt"> </span><span class="RktSym">x</span><span class="RktPn">)</span> picks the first sub-class, which consists
of just one element, the string <span class="RktVal">"resting"</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">-3</span></span><span class="stt"> </span><span class="RktSym">x</span><span class="stt"> </span><span class="RktVal"><span class="nobreak">-1</span></span><span class="RktPn">)</span>
completely describes the second sub-class of data; 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._~3e~3d%29%29" class="RktValLink" data-pltdoc="x">&gt;=</a></span><span class="stt"> </span><span class="RktSym">x</span><span class="stt"> </span><span class="RktVal">0</span><span class="RktPn">)</span> is
a test for all non-negative numbers.</div></p><p><a name="(counter._itemization._(exercise._cond6))"></a><span style="font-weight: bold">Exercise</span>&nbsp;54. Why would it be <span style="font-weight: bold">incorrect</span> to use <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">"resting"</span><span class="stt"> </span><span class="RktSym">x</span><span class="RktPn">)</span>
as the first condition in <span class="RktSym">show</span>? Conversely, formulate a completely accurate
condition, that is, a <a href="part_one.html#%28tech._boolean%29" class="techoutside" data-pltdoc="x"><span class="techinside">Boolean</span></a> expression that evaluates to
<span class="RktVal">#true</span> precisely when <span class="RktSym">x</span> belongs to the first sub-class of
<a href="part_one.html#%28tech._itemization._lrcd%29" class="techoutside" data-pltdoc="x"><span class="techinside">LRCD</span></a>. <a href="part_one.html#%28counter._itemization._%28exercise._cond6%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara">Combining the examples and the above skeleton of the <span class="RktSym">show</span> function
yields a complete definition in a reasonably straightforward manner:
<a name="(idx._itemization._(gentag._108._itemization))"></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">show</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._string~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</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">10</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="RktSym">CENTER</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKG</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._~3c~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span></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="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._text%29%29" class="RktValLink" data-pltdoc="x">text</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._number-~3estring%29%29" class="RktValLink" data-pltdoc="x">number-&gt;string</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n
essential element of the full-fledged design approach in this book.</div></p><p><div class="SIntrapara"><a name="(counter._itemization._(exercise._cond-draw-rocket))"></a><span style="font-weight: bold">Exercise</span>&nbsp;55. Take another look at <span class="RktSym">show</span>. It
contains three instances of an expression with the approximate shape:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><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">10</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"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktSym">CENTER</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKG</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">This expression appears three times in the function: twice to draw a resting
rocket and once to draw a flying rocket. Define an auxiliary function that
performs this work and thus shorten <span class="RktSym">show</span>. Why is this a
good idea? You may wish to reread <a href="part_prologue.html" data-pltdoc="x">Prologue: How to Program</a>. <a href="part_one.html#%28counter._itemization._%28exercise._cond-draw-rocket%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><div class="SIntrapara">Let&rsquo;s move on to the second function, which deals with the key event to
launch the rocket. We have its header material, so we formulate examples
as tests:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">launch</span><span class="hspace">&nbsp;</span><span class="RktVal">"resting"</span><span class="hspace">&nbsp;</span><span class="RktVal">" "</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">launch</span><span class="hspace">&nbsp;</span><span class="RktVal">"resting"</span><span class="hspace">&nbsp;</span><span class="RktVal">"a"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"resting"</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">launch</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></span><span class="hspace">&nbsp;</span><span class="RktVal">" "</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">launch</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span></span><span class="hspace">&nbsp;</span><span class="RktVal">" "</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span></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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">launch</span><span class="hspace">&nbsp;</span><span class="RktVal">33</span><span class="hspace">&nbsp;</span><span class="RktVal">" "</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">33</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">launch</span><span class="hspace">&nbsp;</span><span class="RktVal">33</span><span class="hspace">&nbsp;</span><span class="RktVal">"a"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">33</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">An inspection of these six examples shows that the first two are about the
first sub-class of <a href="part_one.html#%28tech._itemization._lrcd%29" class="techoutside" data-pltdoc="x"><span class="techinside">LRCD</span></a>, the third and fourth concern the
countdown, and the last two are about key events when the rocket is
already in the air.</div></p><p><div class="SIntrapara">Since writing down the sketch of 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 worked well for
the design of the <span class="RktSym">show</span> function, we do it again: <a name="(idx._itemization._(gentag._109._itemization))"></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">launch</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">ke</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._string~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span></span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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~3d%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="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Looking back at the examples suggests that nothing changes when the world
is in a state that is represented by the second or third sub-class of
data. Meaning, <span class="RktSym">launch</span> should produce <span class="RktSym">x</span> when this happens:
</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">launch</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">ke</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._string~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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~3d%29%29" class="RktValLink" data-pltdoc="x">&lt;=</a></span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span></span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">x</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~3d%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="RktSym">x</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Finally, the first example identifies the exact case when the
<span class="RktSym">launch</span> function produces a new world state:
</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">launch</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">ke</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._string~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></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#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._if%29%29" class="RktStxLink" data-pltdoc="x">if</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~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">" "</span><span class="hspace">&nbsp;</span><span class="RktSym">ke</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></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;&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="RktVal"><span class="nobreak">-3</span></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span></span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">x</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~3d%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="RktSym">x</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Specifically, when the state of the world is <span class="RktVal">"resting"</span> and the
user presses the space bar, the function starts the countdown with <span class="RktVal"><span class="nobreak">-3</span></span>.</div></p><p><div class="SIntrapara">Copy the code into the definitions area of DrRacket and ensure that the above
definitions work. At that point, you may wish to add a function for
running the program:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._itemization._lrcd%29" class="techoutside" data-pltdoc="x"><span class="techinside">LRCD</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._itemization._lrcd%29" class="techoutside" data-pltdoc="x"><span class="techinside">LRCD</span></a></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">main1</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktSym">s</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace">&nbsp;</span><span class="RktSym">show</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-key%29%29" class="RktStxLink" data-pltdoc="x">on-key</a></span><span class="hspace">&nbsp;</span><span class="RktSym">launch</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">This function does <span style="font-weight: bold">not</span> specify what to do when the clock ticks; after
all, we haven&rsquo;t designed <span class="RktSym">fly</span> yet. Still, with <span class="RktSym">main1</span> it is
possible to run this incomplete version of the program and to check that
you can start the countdown. What would you provide as the argument in a
call to <span class="RktSym">main1</span>?</div></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><a href="part_one.html#%28tech._itemization._lrcd%29" class="techoutside" data-pltdoc="x"><span class="techinside">LRCD</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._itemization._lrcd%29" class="techoutside" data-pltdoc="x"><span class="techinside">LRCD</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">raises the rocket by </span><span class="RktSym">YDELTA</span><span class="RktCmt"> if it is moving already </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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fly</span><span class="hspace">&nbsp;</span><span class="RktVal">"resting"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">"resting"</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fly</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-2</span></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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fly</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-2</span></span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span></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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fly</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span></span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fly</span><span class="hspace">&nbsp;</span><span class="RktVal">10</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._-%29%29" class="RktValLink" data-pltdoc="x"><span class="nobreak">-</span></a></span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktSym">YDELTA</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="
The design of <span class="RktSym">fly</span>&#8212;<wbr></wbr>the clock-tick handler&#8212;<wbr></wbr>proceeds just like
the design of the preceding two functions, and <a href="part_one.html#%28counter._itemization._%28figure._fig~3acountdown%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">25</span></a> displays
the result of the <a name="(idx._itemization._(gentag._111._itemization))"></a>design process. Once again the key is to cover the space
of possible input data with a goodly bunch of examples, especially for
the two intervals. These examples ensure that the countdown and the
transition from the countdown to the liftoff work properly.</p><p><a name="(counter._itemization._(exercise._cond7))"></a><span style="font-weight: bold">Exercise</span>&nbsp;56. Define <span class="RktSym">main2</span> so that you can launch the rocket
and watch it lift off. Read up on the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span> clause to determine
the length of one tick and how to change it.</p><p>If you watch the entire launch, you will notice that once the rocket
reaches the top something curious happens. Explain. Add a
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</a></span> clause to <span class="RktSym">main2</span> so that the simulation of the
liftoff stops gracefully when the rocket is out of sight. <a href="part_one.html#%28counter._itemization._%28exercise._cond7%29%29" class="ex-end" data-pltdoc="x"></a></p><p>The solution of <a href="part_one.html#%28counter._itemization._%28exercise._cond7%29%29" data-pltdoc="x">exercise&nbsp;56</a> yields a complete, working
program, but one that behaves a bit strangely. Experienced
programmers tell you that using negative numbers to represent the
countdown phase is too &ldquo;brittle.&rdquo; The next chapter introduces the means
to provide a good data definition for this problem. Before we go there,
however, the next section spells out in detail how to design programs
that consume data described by itemizations.</p><p><a name="(counter._itemization._(exercise._interpret-height))"></a><span style="font-weight: bold">Exercise</span>&nbsp;57. Recall that the word &ldquo;height&rdquo; forced us to
choose one of two possible interpretations. Now that you have solved the
exercises in this section, solve them again using the first interpretation
of the word. Compare and contrast the solutions. <a href="part_one.html#%28counter._itemization._%28exercise._interpret-height%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>4.6<tt>&nbsp;</tt><a name="(part._sec~3adesign-itemization)"></a>Designing with Itemizations</h4><p>What the preceding three sections have clarified is that the design of functions
can&#8212;<wbr></wbr>and must&#8212;<wbr></wbr>exploit the organization of the data definition. Specifically, if a
data definition singles out certain pieces of data or specifies ranges of data,
then the creation of examples and the organization of the function reflect
these cases and ranges.</p><p><div class="SIntrapara">In this section, we refine the <a name="(idx._(gentag._112))"></a>design recipe of <a href="part_one.html#%28part._sec~3adesign%29" data-pltdoc="x">From Functions to Programs</a> so that you
can proceed in a systematic manner when you encounter problems
concerning functions that consume itemizations, including enumerations and
intervals. To keep the explanation grounded, we illustrate the six design
steps with the following, somewhat simplistic, example:
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> The state of Tax Land has created a three-stage sales tax
to cope with its budget deficit. Inexpensive items, those costing less
than $1,000, are not taxed. Luxury items, with a price of more than $10,000, are
taxed at the rate of eight percent (8.00%). Everything in between comes with a
five percent (5.00%) markup.</p><p>Design a function for a cash register that, given the price of an item,
computes the sales tax.</p></blockquote></div></p><p><div class="SIntrapara">Keep this problem in mind as we revise the steps of the design recipe:
</div><div class="SIntrapara"><ol><li><p>When the problem statement distinguishes different classes of input
information, you need carefully formulated data definitions.</p><p>A <a name="(idx._(gentag._113))"></a>data definition must use distinct <span style="font-style: italic">clauses</span> for each
sub-class of data or in some cases just individual pieces of data. Each
clause specifies a <a name="(idx._(gentag._114))"></a>data representation for a particular sub-class of
information. The key is that each sub-class of data is distinct from every
other class, so that our function can proceed by analyzing disjoint cases.</p><p>Our sample problem deals with prices and taxes, which are usually
positive numbers. It also clearly distinguishes three ranges:</p><p><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._price)"></a><span style="font-style: italic">Price</span><span class="RktCmt"> falls into one of three intervals: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </span><span class="RktVal">0</span><span class="RktCmt"> through </span><span class="RktVal">1000</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </span><span class="RktVal">1000</span><span class="RktCmt"> through </span><span class="RktVal">10000</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </span><span class="RktVal">10000</span><span class="RktCmt"> and above.</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> the price of an item</span><span class="hspace">&nbsp;&nbsp;</span></td></tr></table></blockquote></div><div class="SIntrapara">Do you understand how these ranges relate to the original problem?</div></p></li><li><p>As far as the signature, purpose statement, and function
header are concerned, you proceed as before.</p><p><div class="SIntrapara">Here is the material for our running example:<span class="refelem"><span class="refcolumn"><span class="refcontent">Developers in
the real world do not use plain numbers in the chosen programming language
for representing amounts of money. See intermezzo 4 for some problems with
numbers.</span></span></span>
<a name="(idx._(gentag._115))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._price%29" class="techoutside" data-pltdoc="x"><span class="techinside">Price</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">computes the amount of tax charged for </span><span class="RktSym">p</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">sales-tax</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p></li><li><p>For functional examples, however, it is imperative that you pick at least
one example from each sub-class in the data definition. Also, if a sub-class is a
finite range, be sure to pick examples from the boundaries of the range and
from its interior.</p><p>Since our sample data definition involves three distinct intervals, let&rsquo;s
pick all boundary examples and one price from inside each interval and
determine the amount of tax for each: <span class="RktVal">0</span>, <span class="RktVal">537</span>, <span class="RktVal">1000</span>,
<span class="RktVal">1282</span>, <span class="RktVal">10000</span>, and <span class="RktVal">12017</span>.</p><p>Stop! Try to calculate the tax for each of these prices.</p><p><div class="SIntrapara">Here is our first attempt, with rounded tax amounts:
</div><div class="SIntrapara"><table cellspacing="0" cellpadding="0"><tr><td align="right"><p>0</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="right"><p>537</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="right"><p>1000</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="right"><p>1282</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="right"><p>10000</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="right"><p>12017</p></td></tr><tr><td align="right"><p>0</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="right"><p>0</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="right"><p>????</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="right"><p>64</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="right"><p>?????</p></td><td align="right"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td align="right"><p>961</p></td></tr></table></div><div class="SIntrapara">The question marks point out that the problem statement uses the
vague phrase &ldquo;those costing less than $1,000&rdquo; and &ldquo;more than $10,000&rdquo;
to specify the tax table. While a programmer may jump to the
conclusion that these words mean &ldquo;strictly less&rdquo; or &ldquo;strictly more,&rdquo;
the lawmakers may have meant to say &ldquo;less than or equal to&rdquo; or &ldquo;more than or
equal to,&rdquo; respectively. Being skeptical, we decide here that Tax Land
legislators always want more money to spend, so the tax rate for $1,000 is
5% and the rate for $10,000 is 8%. A programmer at a tax company would
have to ask a tax-law specialist.</div></p><p>Now that we have figured out how the boundaries are to be
interpreted in the domain, we could refine the data definition. We trust
you can do this on your own.</p><p><div class="SIntrapara">Before we go, let&rsquo;s turn some of the examples into test cases:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">sales-tax</span><span class="hspace">&nbsp;</span><span class="RktVal">537</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="RktPn">(</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">sales-tax</span><span class="hspace">&nbsp;</span><span class="RktVal">1000</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="RktVal">0.05</span><span class="hspace">&nbsp;</span><span class="RktVal">1000</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">sales-tax</span><span class="hspace">&nbsp;</span><span class="RktVal">12017</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="RktVal">0.08</span><span class="hspace">&nbsp;</span><span class="RktVal">12017</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Take a close look. Instead of just writing down the expected result, we
write down how to compute the expected result. This makes it easier later
to formulate the function definition.</div></p><p>Stop! Write down the remaining test cases. Think about why you may need
more test cases than sub-classes in the data definition.</p></li><li><p><div class="SIntrapara">The biggest novelty is the <a name="(idx._(gentag._116))"></a>conditional template. In general,
</div><div class="SIntrapara"><blockquote><p><span style="font-style: italic">the template mirrors the organization of sub-classes with 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>.</span></p></blockquote></div><div class="SIntrapara">This slogan means two concrete things. First, the function&rsquo;s body must be a
conditional expression with as many clauses as there are distinct sub-classes in
the data definition. If the data definition mentions three distinct sub-classes of
input data, you need three <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> clauses; if it has seventeen sub-classes,
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 contains seventeen clauses. Second, you must formulate one
condition expression per <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> clause. Each expression involves the
function parameter and identifies one of the sub-classes of data in the data
definition:<a name="(idx._(gentag._117))"></a></div></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/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">sales-tax</span><span class="hspace">&nbsp;</span><span class="RktSym">p</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#%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#%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="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktSym">p</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._~3c%29%29" class="RktValLink" data-pltdoc="x">&lt;</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktVal">1000</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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#%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#%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="RktVal">1000</span><span class="hspace">&nbsp;</span><span class="RktSym">p</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._~3c%29%29" class="RktValLink" data-pltdoc="x">&lt;</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktVal">10000</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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~3d%29%29" class="RktValLink" data-plt
function. Given that the function body already contains a schematic
<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, it is natural to start from the various
<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> lines. For each <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> line, you may assume that the
input parameter meets the condition and so you exploit the corresponding test
cases. To formulate the corresponding result expression, you write down the
computation for this example as an expression that involves the function
parameter. Ignore all other possible kinds of input data when you work on
one line; the other <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> clauses take care of those.<a name="(idx._(gentag._118))"></a></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/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">sales-tax</span><span class="hspace">&nbsp;</span><span class="RktSym">p</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#%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#%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="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktSym">p</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._~3c%29%29" class="RktValLink" data-pltdoc="x">&lt;</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktVal">1000</span><span class="RktPn">)</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#%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#%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="RktVal">1000</span><span class="hspace">&nbsp;</span><span class="RktSym">p</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._~3c%29%29" class="RktValLink" data-pltdoc="x">&lt;</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktVal">10000</span><span class="RktPn">)</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="RktVal">0.05</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp
<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> clauses.</p><p>What do you do when one of your test cases fails? Review the end of
<a href="part_one.html#%28part._sec~3adesign-func%29" data-pltdoc="x">Designing Functions</a> concerning test failures.</p></li></ol></div></p><p><a name="(counter._(exercise._cond9))"></a><span style="font-weight: bold">Exercise</span>&nbsp;58. Introduce constant definitions that separate the
intervals for low prices and luxury prices from the others so that the
legislators in Tax Land can easily raise the taxes even more. <a href="part_one.html#%28counter._%28exercise._cond9%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>4.7<tt>&nbsp;</tt><a name="(part._sec~3aworlds-more)"></a>Finite State Worlds</h4><p>With the design knowledge in this chapter, you can develop a complete
simulation of American traffic lights. When such a light is green and it is
time to stop the traffic, the light turns yellow, and, after that, it turns
red. When the light is red and it is time to get the traffic going, the
light simply switches to green.</p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><blockquote class="SCentered"><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_60.png" alt="image" width="331.08281250000005" height="222.0"/></p></blockquote></blockquote></blockquote><p class="Centertext"><span class="Legend"><span class="FigureTarget"><a name="(counter._(figure._fig~3atraffic-light))" x-target-lift="Figure"></a>Figure&nbsp;26: </span>How a traffic light functions</span></p></blockquote><p>The left-hand side of <a href="part_one.html#%28counter._%28figure._fig~3atraffic-light%29%29" data-pltdoc="x">Figure&nbsp;<span class="FigureRef">26</span></a> summarizes this description as a
<span style="font-style: italic">state transition diagram</span>. Such a diagram consists of
<span style="font-style: italic">states</span> and arrows that connect these states. Each state depicts
a traffic light in one particular configuration: red, yellow, or
green. Each arrow shows how the world can change, from which state it can
<span style="font-style: italic">transition</span> to another state. Our sample diagram contains
three arrows, because there are three possible ways in which the traffic
light can change. Labels on the arrows indicate the reason for changes; a
traffic light transitions from one state to another as time passes.</p><p>In many situations, state transition diagrams have only a finite number of
states and arrows. Computer scientists call such diagrams <span style="font-style: italic">finite
state machines</span> (FSM), also known as <span style="font-style: italic">finite state automata</span>
(FSA). Despite their simplicity, FSMs/FSAs play an important role
in computer science.</p><p>To create a world program for an FSA, we must first pick a data
representation for the possible &ldquo;states of the world,&rdquo; which, according
to <a href="part_one.html#%28part._.D.K._sec~3adesign-world%29" data-pltdoc="x">Designing World Programs</a>, represents those
aspects of the world that may change in some ways as opposed to those that
remain the same. In the case of our traffic light, what changes is the
color of the light, that is, which bulb is turned on. The size of the
bulbs, their arrangement (horizontal or vertical), and other aspects don&rsquo;t
change. Since there are only three states, we reuse the string-based data
definition of <a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a> from above.</p><p>The right-hand side of <a href="part_one.html#%28counter._%28figure._fig~3atraffic-light%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">26</span></a> is a diagrammatic interpretation of the
<a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a> data definition. Like the diagram in
<a href="part_one.html#%28counter._%28figure._fig~3atraffic-light%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">26</span></a>, it consists of three states, arranged in
such a way that it is easy to view each data element as a representation
of a concrete configuration. Also, the arrows are now labeled with
<span style="font-weight: bold">tick</span> to suggest that our world program uses the passing of time as
the trigger that changes the state of the traffic light. If we wanted to
simulate a manually operated light, we might choose transitions
based on keystrokes.</p><p><div class="SIntrapara">Now that we know how to represent the states of our world, how to go from
one to the next, and that the state changes at every tick of the clock, we
can write down the signature, a purpose statement, and a stub
for the two functions we must design: <a name="(idx._(gentag._119))"></a> <a name="(idx._(gentag._120))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">yields the next state, given current state </span><span class="RktSym">cs</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">tl-next</span><span class="hspace">&nbsp;</span><span class="RktSym">cs</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">cs</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><a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">renders the current state </span><span class="RktSym">cs</span><span class="RktCmt"> as an image</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">tl-render</span><span class="hspace">&nbsp;</span><span class="RktSym">current-state</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._empty-scene%29%29" class="RktValLink" data-pltdoc="x">empty-scene</a></span><span class="hspace">&nbsp;</span><span class="RktVal">90</span><span class="hspace">&nbsp;</span><span class="RktVal">30</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Preceding sections use the names <span class="RktSym">render</span> and <span class="RktSym">next</span>
to name the functions that translate a state of the world into an
image and that deal with clock ticks. Here we prefix these names
with some syllable that suggests to which world the functions belong.
Because the specific functions have appeared before, we leave them as
exercises.</div></p><p><div class="SIntrapara"><a name="(counter._(exercise._cond11))"></a><span style="font-weight: bold">Exercise</span>&nbsp;59. Finish the design of a world program that simulates the
traffic light FSA. Here is the main function: <a name="(idx._(gentag._121))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">simulates a clock-based American traffic light</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">traffic-light-simulation</span><span class="hspace">&nbsp;</span><span class="RktSym">initial-state</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktSym">initial-state</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace">&nbsp;</span><span class="RktSym">tl-render</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace">&nbsp;</span><span class="RktSym">tl-next</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">The function&rsquo;s argument is the initial state for the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>
expression, which tells DrRacket to redraw the state of the world with
<span class="RktSym">tl-render</span> and to handle clock ticks with <span class="RktSym">tl-next</span>. Also
note that it informs the computer that the clock should tick once per second.</div></p><p>Complete the design of <span class="RktSym">tl-render</span> and <span class="RktSym">tl-next</span>. Start with
copying <a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a>, <span class="RktSym">tl-next</span>, and <span class="RktSym">tl-render</span> into
DrRacket&rsquo;s definitions area.</p><p><div class="SIntrapara">Here are some test cases for the design of the latter:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tl-render</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_61.png" alt="image" width="56" height="26"/><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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tl-render</span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_62.png" alt="image" width="56" height="26"/><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Your function may use these images directly. If you decide to create images
with the functions from <span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/image</span></span> teachpack</span>, design an auxiliary function for
creating the image of a one-color bulb. Then read up on the
<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> function, which can place bulbs into a background
scene. <a href="part_one.html#%28counter._%28exercise._cond11%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><div class="SIntrapara"><a name="(counter._(exercise._ex~3aalternative-data-design))"></a><span style="font-weight: bold">Exercise</span>&nbsp;60. An alternative data representation
for a traffic light program may use numbers instead of strings:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">An </span><a name="(tech._n._trafficlight)"></a><span style="font-style: italic">N-TrafficLight</span><span class="RktCmt"> is one of:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">0</span><span class="RktCmt"> </span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> the traffic light shows red</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">1</span><span class="RktCmt"> </span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> the traffic light shows green</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">2</span><span class="RktCmt"> </span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> the traffic light shows yellow</span></td></tr></table></blockquote></div><div class="SIntrapara">It greatly simplifies the definition of <span class="RktSym">tl-next</span>: <a name="(idx._(gentag._122))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._n._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">N-TrafficLight</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._n._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">N-TrafficLight</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">yields the next state, given current state </span><span class="RktSym">cs</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">tl-next-numeric</span><span class="hspace">&nbsp;</span><span class="RktSym">cs</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._modulo%29%29" class="RktValLink" data-pltdoc="x">modulo</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="RktSym">cs</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Reformulate <span class="RktSym">tl-next</span>&rsquo;s tests for <span class="RktSym">tl-next-numeric</span>.</div></p><p>Does the <span class="RktSym">tl-next</span> function convey its intention more clearly than
the <span class="RktSym">tl-next-numeric</span> function? If so, why? If not, why not? <a href="part_one.html#%28counter._%28exercise._ex~3aalternative-data-design%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara"><a name="(counter._(exercise._ex~3aalternative-data-design2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;61. As <a href="part_one.html#%28part._sec~3adesign%29" data-pltdoc="x">From Functions to Programs</a> says,
programs must define constants and use names instead of actual
constants. In this spirit, a data definition for traffic lights
must use constants, too:<span class="refelem"><span class="refcolumn"><span class="refcontent">This form of data
definition is what a seasoned designer would use.</span></span></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="RktSym">RED</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"><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">GREEN</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"><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">YELLOW</span><span class="hspace">&nbsp;</span><span class="RktVal">2</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">An </span><a name="(tech._s._trafficlight)"></a><span style="font-style: italic">S-TrafficLight</span><span class="RktCmt"> is one of:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktSym">RED</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktSym">GREEN</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktSym">YELLOW</span></td></tr></table></blockquote></div><div class="SIntrapara">If the names are chosen properly, the data definition does not need an
interpretation statement.
<a name="(idx._(gentag._123))"></a></div></p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><table cellspacing="0" cellpadding="0"><tr><td valign="top" colspan="3"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._s._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">S-TrafficLight</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._s._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">S-TrafficLight</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">yields the next state, given current state </span><span class="RktSym">cs</span></td></tr></table></td></tr><tr><td valign="top"><p></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td valign="top"><p></p></td></tr><tr><td valign="top" colspan="3"><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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tl-next-</span><span class="hspace">&nbsp;</span>...<span class="hspace">&nbsp;</span><span class="RktSym">RED</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">YELLOW</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tl-next-</span><span class="hspace">&nbsp;</span>...<span class="hspace">&nbsp;</span><span class="RktSym">YELLOW</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">GREEN</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td valign="top"><p></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td valign="top"><p></p></td></tr><tr><td valign="top"><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">tl-next-numeric</span><span class="hspace">&nbsp;</span><span class="RktSym">cs</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._modulo%29%29" class="RktValLink" data-pltdoc="x">modulo</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="RktSym">cs</span><span class="hspace">&nbsp;</span><span class="RktVal">1</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td valign="top"><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._
switch the state of a traffic light in a simulation program.
Which of the two is properly designed using the recipe for itemization?
Which of the two continues to work if you change the constants to the following
</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">RED</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</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">GREEN</span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</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">YELLOW</span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Does this help you answer the questions?</div></p><p><span style="font-weight: bold">Aside</span> The <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._equal~3f%29%29" class="RktValLink" data-pltdoc="x">equal?</a></span> function in
<a href="part_one.html#%28counter._%28figure._fig~3aex~3aalternative%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">27</span></a> compares two arbitrary values, regardless
of what these values are. Equality is a complicated topic in the world of
programming. <span style="font-weight: bold">End</span> <a href="part_one.html#%28counter._%28exercise._ex~3aalternative-data-design2%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara">Here is another finite state problem that introduces a few additional
complications:
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> Design a world program that simulates the working of a door
with an automatic door closer. If this kind of door is locked, you can
unlock it with a key. An unlocked door is closed, but someone pushing at the
door opens it. Once the person has passed through the door and lets go, the
automatic door takes over and closes the door again. When a door is
closed, it can be locked again.</p></blockquote></div></p><p>To tease out the essential elements, we again draw a transition diagram;
see the left-hand side of <a href="part_one.html#%28counter._%28figure._fig~3adoor-closer%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">28</span></a>. Like the traffic light, the door has
three distinct states: locked, closed, and open. Locking and unlocking are
the activities that cause the door to transition from the locked to the
closed state and vice versa. As for opening an unlocked door, we say that
one needs to <span style="font-weight: bold">push</span> the door open. The remaining transition is unlike
the others because it doesn&rsquo;t require any activities by anyone or
anything else. Instead, the door closes automatically over time. The
corresponding transition arrow is labeled with <span style="font-weight: bold">*time*</span> to emphasize this.</p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><blockquote class="SCentered"><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_63.png" alt="image" width="133.0576171875" height="204.234375"/> <span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_64.png" alt="image" width="142.9697265625" height="204.234375"/></p></blockquote></blockquote></blockquote><p class="Centertext"><span class="Legend"><span class="FigureTarget"><a name="(counter._(figure._fig~3adoor-closer))" x-target-lift="Figure"></a>Figure&nbsp;28: </span>A transition diagram for a door with an automatic closer</span></p></blockquote><p><div class="SIntrapara">Following our recipe, we start with a translation of the three real-world
states into BSL data:
</div><div class="SIntrapara"><blockquote><table cellspacing="0" cellpadding="0"><tr><td valign="top"><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">LOCKED</span><span class="hspace">&nbsp;</span><span class="RktVal">"locked"</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">CLOSED</span><span class="hspace">&nbsp;</span><span class="RktVal">"closed"</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">OPEN</span><span class="hspace">&nbsp;</span><span class="RktVal">"open"</span><span class="RktPn">)</span></td></tr></table></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;&nbsp;</span></p></td><td valign="top"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._doorstate)"></a><span style="font-style: italic">DoorState</span><span class="RktCmt"> is one of:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktSym">LOCKED</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktSym">CLOSED</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktSym">OPEN</span></td></tr></table></td></tr></table></blockquote></div><div class="SIntrapara">We also keep in mind the lesson of <a href="part_one.html#%28counter._%28exercise._ex~3aalternative-data-design2%29%29" data-pltdoc="x">exercise&nbsp;61</a>,
namely, that it is best to define symbolic constants and formulate data
definitions in terms of such constants.</div></p><p>The next step of a world design demands that we translate the chosen
actions in our domain&#8212;<wbr></wbr>the arrows in the left-hand diagram&#8212;<wbr></wbr>into
interactions with the computer that <span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/universe</span></span> teachpack</span> can deal with. Our
pictorial representation of the door&rsquo;s states and transitions,
specifically the arrow from <span class="stt">open</span> to <span class="stt">closed</span>, suggests the use of
clock ticks. For the other arrows, we could use either key presses or
mouse clicks. Let&rsquo;s use three keystrokes: <span class="RktVal">"u"</span> for unlocking the
door, <span class="RktVal">"l"</span> for locking it, and the space bar <span class="RktVal">" "</span> for
pushing it open. The right-hand-side diagram of
<a href="part_one.html#%28counter._%28figure._fig~3adoor-closer%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">28</span></a> expresses these choices graphically; it
translates the state-machine diagram from the world of information into
the world of data in BSL.</p><p><div class="SIntrapara">Once we have decided to use the passing of time for one action and key
presses for the others, we must design functions that render the current
state of the world&#8212;<wbr></wbr>represented as <a href="part_one.html#%28tech._doorstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">DoorState</span></a>&#8212;<wbr></wbr>and that transform
it into the next state of the world. And that, of course, amounts to a
wish list of <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> functions:
</div><div class="SIntrapara"><ul><li><p><span class="RktSym">door-closer</span>, which closes the door during one tick;</p></li><li><p><span class="RktSym">door-action</span>, which acts on it in response to pressing a key; and</p></li><li><p><span class="RktSym">door-render</span>, which translates the current state into an image.</p></li></ul></div><div class="SIntrapara">Stop! Formulate appropriate signatures.</div></p><p><div class="SIntrapara">We start with <span class="RktSym">door-closer</span>. Since <span class="RktSym">door-closer</span>
acts as the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span> handler, we get its signature from our choice
of <a href="part_one.html#%28tech._doorstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">DoorState</span></a> as the collection of world states:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._doorstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">DoorState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._doorstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">DoorState</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">closes an open door over the period of one tick </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">door-closer</span><span class="hspace">&nbsp;</span><span class="RktSym">state-of-door</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">state-of-door</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Making up examples is trivial when the world can only be in one of three
states. Here we use a table to express the basic idea, just like in some
of the mathematical examples given above:
</div><div class="SIntrapara"><blockquote><table cellspacing="0" cellpadding="0"><tr><td><p>given state</p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p>desired state</p></td></tr><tr><td><p><span class="RktSym">LOCKED</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p><span class="RktSym">LOCKED</span></p></td></tr><tr><td><p><span class="RktSym">CLOSED</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p><span class="RktSym">CLOSED</span></p></td></tr><tr><td><p><span class="RktSym">OPEN</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p><span class="RktSym">CLOSED</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Stop! Express these examples as BSL tests.</div></p><p><div class="SIntrapara">The template step demands a conditional with three clauses:
</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">door-closer</span><span class="hspace">&nbsp;</span><span class="RktSym">state-of-door</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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">LOCKED</span><span class="hspace">&nbsp;</span><span class="RktSym">state-of-door</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSED</span><span class="hspace">&nbsp;</span><span class="RktSym">state-of-door</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">OPEN</span><span class="hspace">&nbsp;</span><span class="RktSym">state-of-door</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">and the process of turning this template into a function definition is
dictated by the examples:
</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">door-closer</span><span class="hspace">&nbsp;</span><span class="RktSym">state-of-door</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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">LOCKED</span><span class="hspace">&nbsp;</span><span class="RktSym">state-of-door</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">LOCKED</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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSED</span><span class="hspace">&nbsp;</span><span class="RktSym">state-of-door</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSED</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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">OPEN</span><span class="hspace">&nbsp;</span><span class="RktSym">state-of-door</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSED</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Don&rsquo;t forget to run your tests.</div></p><p><div class="SIntrapara">The second function, <span class="RktSym">door-action</span>, takes care of the remaining
three arrows of the diagram. Functions that deal with keyboard events
consume both a world and a key event, meaning the signature is as follows:
<a name="(idx._(gentag._124))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._doorstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">DoorState</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._doorstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">DoorState</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">turns key event </span><span class="RktSym">k</span><span class="RktCmt"> into an action on state </span><span class="RktSym">s</span><span class="RktCmt"> </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">door-action</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktSym">k</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">We once again present the examples in tabular form:
</div><div class="SIntrapara"><blockquote><table cellspacing="0" cellpadding="0"><tr><td><p>given state</p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p>given key event</p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p>desired state</p></td></tr><tr><td><p><span class="RktSym">LOCKED</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p><span class="RktVal">"u"</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p><span class="RktSym">CLOSED</span></p></td></tr><tr><td><p><span class="RktSym">CLOSED</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p><span class="RktVal">"l"</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p><span class="RktSym">LOCKED</span></p></td></tr><tr><td><p><span class="RktSym">CLOSED</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p><span class="RktVal">" "</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p><span class="RktSym">OPEN</span></p></td></tr><tr><td><p><span class="RktSym">OPEN</span></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p>&#8212;<wbr></wbr></p></td><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td><p><span class="RktSym">OPEN</span></p></td></tr></table></blockquote></div><div class="SIntrapara">The examples combine information from our drawing with the choices we made
about mapping actions to keyboard events. Unlike the table of
examples for traffic light, this table is incomplete. Think of some
other examples; then consider why our table suffices.</div></p><p><div class="SIntrapara">From here, it is straightforward to create a complete design: <a name="(idx._(gentag._125))"></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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">door-action</span><span class="hspace">&nbsp;</span><span class="RktSym">LOCKED</span><span class="hspace">&nbsp;</span><span class="RktVal">"u"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSED</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">door-action</span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSED</span><span class="hspace">&nbsp;</span><span class="RktVal">"l"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">LOCKED</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">door-action</span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSED</span><span class="hspace">&nbsp;</span><span class="RktVal">" "</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">OPEN</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">door-action</span><span class="hspace">&nbsp;</span><span class="RktSym">OPEN</span><span class="hspace">&nbsp;</span><span class="RktVal">"a"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">OPEN</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">door-action</span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSED</span><span class="hspace">&nbsp;</span><span class="RktVal">"a"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSED</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">door-action</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktSym">k</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=
current state of the door and the other concerning the given key event.</div></p><p><div class="SIntrapara">Lastly, we need to render the state of the world as a scene:<a name="(idx._(gentag._126))"></a> <a name="(idx._(gentag._127))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._doorstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">DoorState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">translates the state </span><span class="RktSym">s</span><span class="RktCmt"> into a large text image</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">door-render</span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSED</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;&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._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span><span class="hspace">&nbsp;</span><span class="RktSym">CLOSED</span><span class="hspace">&nbsp;</span><span class="RktVal">40</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</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="RktPn">(</span><span class="RktSym">door-render</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="hspace">&nbsp;</span><span class="RktVal">40</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">This simplistic function uses large text. Here is how we run it all:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._doorstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">DoorState</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._doorstate%29" class="techoutside" data-pltdoc="x"><span class="techinside">DoorState</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">simulates a door with an automatic door closer</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">door-simulation</span><span class="hspace">&nbsp;</span><span class="RktSym">initial-state</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktSym">initial-state</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace">&nbsp;</span><span class="RktSym">door-closer</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-key%29%29" class="RktStxLink" data-pltdoc="x">on-key</a></span><span class="hspace">&nbsp;</span><span class="RktSym">door-action</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span><span class="hspace">&nbsp;</span><span class="RktSym">door-render</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Now it is time for you to collect the pieces and run them in DrRacket to see
whether it all works.</div></p><p><a name="(counter._(exercise._cond12))"></a><span style="font-weight: bold">Exercise</span>&nbsp;62. During a door simulation the &ldquo;open&rdquo; state is barely visible.
Modify <span class="RktSym">door-simulation</span> so that the clock ticks once every three
seconds. Rerun the simulation. <a href="part_one.html#%28counter._%28exercise._cond12%29%29" class="ex-end" data-pltdoc="x"></a></p><h3>5<tt>&nbsp;</tt><a name="(part._ch~3astructure)"></a>Adding Structure</h3><p>Suppose you want to design a world program that simulates a ball bouncing
back and forth on a straight vertical line between the floor and ceiling
of some imaginary, perfect room. Assume that it always moves two pixels
per clock tick. If you follow the design recipe, your first goal is to
develop a <a name="(idx._(gentag._128))"></a>data representation for what changes over time. Here, the ball&rsquo;s
position and its direction change over time, but that&rsquo;s <span style="font-weight: bold">two</span> values
while <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> keeps track of just one. Thus the question arises
how one piece of data can represent two changing quantities of
information.</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p>Mathematicians know tricks that &ldquo;merge&rdquo; two numbers into a
single number such that it is possible to retrieve the original
ones. Programmers consider these kinds of tricks evil because they obscure a
program&rsquo;s true intentions.</p></blockquote></blockquote></blockquote><p>Here is another scenario that raises the same question. Your cell phone is
mostly a few million lines of code wrapped in plastic. Among other things,
it administrates your contacts. Each contact comes with a name, a phone
number, an email address, and perhaps some other information. When you
have lots of contacts, each single contact is best represented as a single
piece of data; otherwise the various pieces could get mixed up by
accident.</p><p>Because of such programming problems, every programming language provides
some mechanism to combine several pieces of data into a single piece of
<span style="font-style: italic">compound data</span> and ways to retrieve the constituent values when
needed. This chapter introduces BSL&rsquo;s mechanics, so-called structure type
definitions, and how to design programs that work on compound data.</p><h4>5.1<tt>&nbsp;</tt><a name="(part._sec~3aposn-structures)"></a>From Positions to <span class="RktSym">posn</span> Structures</h4><p>A position on a world canvas is uniquely identified by two pieces of data:
the distance from the left margin and the distance from the top margin.
The first is called an <span style="font-style: italic">x-coordinate</span> and the second one is the
<span style="font-style: italic">y-coordinate</span>.</p><p><div class="SIntrapara">DrRacket, which is basically a BSL program, represents such positions with
<span class="RktSym">posn</span> structures. A <span class="RktSym">posn</span> structure combines two numbers
into a single value. We can create a <span class="RktSym">posn</span> structure with the
operation <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span>, which consumes two numbers and makes a
<span class="RktSym">posn</span>. For example,
</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</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></p></blockquote></div><div class="SIntrapara">is an expression that creates a <span class="RktSym">posn</span> structure whose x-coordinate
is <span class="RktVal">3</span> and whose y-coordinate is <span class="RktVal">4</span>.</div></p><p><div class="SIntrapara">A <span class="RktSym">posn</span> structure has the same status as a number or a Boolean or a string. In
particular, both primitive operations and functions may consume and
produce structures. Also, a program can name a <span class="RktSym">posn</span> structure:
</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">one-posn</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">8</span><span class="hspace">&nbsp;</span><span class="RktVal">6</span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Stop! Describe <span class="RktSym">one-posn</span> in terms of coordinates.</div></p><p>Before doing anything else, let&rsquo;s take a look at the laws of computation
for <span class="RktSym">posn</span> structures. That way, we can both create functions that
process <span class="RktSym">posn</span> structures and predict what they compute.</p><h4>5.2<tt>&nbsp;</tt><a name="(part._sec~3aeval-posns)"></a>Computing with <span class="RktSym">posn</span>s</h4><p>While functions and the laws of functions are completely familiar from
pre-algebra, <span class="RktSym">posn</span> structures appear to be a new idea. Then again,
the concept of a <span class="RktSym">posn</span> ought to look like the Cartesian points or
positions in the plane you may have encountered before.</p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><blockquote class="SCentered"><p><img src="pict_65.png" alt="image" width="250" height="250"/></p></blockquote></blockquote></blockquote><p class="Centertext"><span class="Legend"><span class="FigureTarget"><a name="(counter._(figure._fig~3acartesian-point))" x-target-lift="Figure"></a>Figure&nbsp;29: </span>A Cartesian point</span></p></blockquote><p>Selecting a Cartesian point&rsquo;s pieces is also a familiar process. For
example,<span class="refelem"><span class="refcolumn"><span class="refcontent">We thank Neil Toronto for the
<span class="RktSym">plot</span> library.</span></span></span> when a teacher says, &ldquo;take a look at the graph of
<a href="part_one.html#%28counter._%28figure._fig~3acartesian-point%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">29</span></a> and tell me what <span style="font-style: italic">p</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic">x</span></span><span style="font-style: italic"></span> and <span style="font-style: italic">p</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic">y</span></span><span style="font-style: italic"></span>
are,&rdquo; you are likely to answer <span style="font-style: italic"></span>3<span style="font-style: italic"></span>1<span style="font-style: italic"></span> and <span style="font-style: italic"></span>2<span style="font-style: italic"></span>6<span style="font-style: italic"></span>, respectively,
because you know that you need to read off the values where the vertical
and horizontal lines that radiate out from <span style="font-style: italic">p</span> hit the axes.</p><p><div class="SIntrapara">We can express this idea in BSL. Assume you add
</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">p</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">31</span><span class="hspace">&nbsp;</span><span class="RktVal">26</span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">to the definitions area, click <span class="emph">RUN</span>, and perform these interactions:
</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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">26</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Defining <span class="RktSym">p</span> is like marking the point in
a Cartesian plane; using <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span> is like
subscripting <span class="RktSym">p</span> with indexes: <span style="font-style: italic">p</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic">x</span></span><span style="font-style: italic"></span> and <span style="font-style: italic">p</span><span style="vertical-align: sub; font-size: 80%"><span style="font-style: italic">y</span></span><span style="font-style: italic"></span>.</div></p><p><div class="SIntrapara">Computationally speaking, <span class="RktSym">posn</span> structures come with two equations:
</div><div class="SIntrapara"><blockquote><table cellspacing="0" cellpadding="0"><tr><td valign="top"><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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><span class="RktSym">x0</span><span class="stt"> </span><span class="RktSym">y0</span><span class="RktPn">)</span><span class="RktPn">)</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><p><span class="RktSym">==</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><p><span class="RktSym">x0</span></p></td></tr><tr><td valign="top"><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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><span class="RktSym">x0</span><span class="stt"> </span><span class="RktSym">y0</span><span class="RktPn">)</span><span class="RktPn">)</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><p><span class="RktSym">==</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><p><span class="RktSym">y0</span></p></td></tr></table></blockquote></div><div class="SIntrapara">DrRacket uses these equations during computations. Here is an example of a
computation involving <span class="RktSym">posn</span> structures:
</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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> replaces </span><span class="RktSym">p</span><span class="RktCmt"> with </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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><span class="RktVal">31</span><span class="stt"> </span><span class="RktVal">26</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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">31</span><span class="hspace">&nbsp;</span><span class="RktVal">26</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> uses the law for </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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="RktCmt"> </span></td></tr><tr><td><span class="RktVal">31</span></td></tr></table></blockquote></div><div class="SIntrapara">Stop! Confirm the second interaction above with your own computation. Also
use DrRacket&rsquo;s stepper to double-check.</div></p><h4>5.3<tt>&nbsp;</tt><a name="(part._sec~3aprogramming-posn)"></a>Programming with <span class="RktSym">posn</span></h4><p><div class="SIntrapara">Now consider designing a function that computes the distance of some
location to the origin of the canvas:
</div><div class="SIntrapara"><blockquote class="SCentered"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_66.png" alt="image" width="160.0" height="129.2"/></p></blockquote></div><div class="SIntrapara">The picture clarifies that &ldquo;distance&rdquo; means the length of the most
direct path&#8212;<wbr></wbr>&ldquo;as the crow flies&rdquo;&#8212;<wbr></wbr>from the designated point to the
top-left corner of the canvas.</div></p><p><div class="SIntrapara">Here are the purpose statement and the header:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">computes the distance of </span><span class="RktSym">ap</span><span class="RktCmt"> to the origin </span><a name="(idx._(gentag._129))"></a></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-to-0</span><span class="hspace">&nbsp;</span><span class="RktSym">ap</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">The key is that <span class="RktSym">distance-to-0</span> consumes a single value, some
<span class="RktSym">posn</span>. It produces a single value, the distance of the location to
the origin.</div></p><p><div class="SIntrapara">In order to make up examples, we need to know how to compute this
distance. For points with <span class="RktVal">0</span> as one of the coordinates, the result
is the other coordinate:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">distance-to-0</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">5</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">distance-to-0</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">7</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">7</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">For the general case, we could try to figure out the formula on our own,
or we may recall the formula from our geometry courses. As you know, this
is domain knowledge that you might have, but in case you don&rsquo;t we supply
it; after all, this domain knowledge isn&rsquo;t computer science. So, here is
the distance formula for <span style="font-style: italic"></span>(<span style="font-style: italic">x,y</span>)<span style="font-style: italic"></span> again:
</div><div class="SIntrapara"><blockquote class="SCentered"><p><img src="pict_67.png" alt="image" width="52" height="15"/></p></blockquote></div><div class="SIntrapara">Given this formula, we can easily make up some more functional examples:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">distance-to-0</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</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><span class="hspace">&nbsp;</span><span class="RktVal">5</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">distance-to-0</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">8</span><span class="hspace">&nbsp;</span><span class="RktVal">6</span><span class="RktPn">)</span><span class="RktPn">)</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"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">distance-to-0</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="hspace">&nbsp;</span><span class="RktVal">12</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">13</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Just in case you&rsquo;re wondering, we rigged the examples so that the results
would be easy to figure out. This isn&rsquo;t the case for all <span class="RktSym">posn</span>
structures.</div></p><p>Stop! Plug the x- and y-coordinates from the examples into the
formula. Confirm the expected results for all five examples.</p><p><div class="SIntrapara">Next we can turn our attention to the definition of the function. The
examples imply that the design of <span class="RktSym">distance-to-0</span> does not need to
distinguish between different situations; it always just computes the
distance from the x- and y-coordinates inside the given <span class="RktSym">posn</span>
structure. But the function must select these coordinates from the given
<span class="RktSym">posn</span> structure. And for that, it uses the <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span> primitives. Specifically, the function needs to compute
<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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="stt"> </span><span class="RktSym">ap</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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="stt"> </span><span class="RktSym">ap</span><span class="RktPn">)</span> because
<span class="RktSym">ap</span> is the name of the given, unknown <span class="RktSym">posn</span> structure:
</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">distance-to-0</span><span class="hspace">&nbsp;</span><span class="RktSym">ap</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._......%29%29" class="RktStxLink" 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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ap</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" 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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ap</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Using this template and the examples, the rest is straightforward:<a name="(idx._(gentag._130))"></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">distance-to-0</span><span class="hspace">&nbsp;</span><span class="RktSym">ap</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._sqrt%29%29" class="RktValLink" data-pltdoc="x">sqrt</a></span></td></tr><tr><td><span class="hspace">&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._%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._sqr%29%29" class="RktValLink" data-pltdoc="x">sqr</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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ap</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&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._sqr%29%29" class="RktValLink" data-pltdoc="x">sqr</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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ap</span><span class="RktPn">)</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">The function squares <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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="stt"> </span><span class="RktSym">ap</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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="stt"> </span><span class="RktSym">ap</span><span class="RktPn">)</span>, which represent the x- and y-coordinates, sums up the results,
and takes the square root. With DrRacket, we can also quickly check that our
new function produces the proper results for our examples.</div></p><p><div class="SIntrapara"><a name="(counter._(exercise._struct1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;63. Evaluate the following expressions:
</div><div class="SIntrapara"><ul><li><p><span class="RktPn">(</span><span class="RktSym">distance-to-0</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><span class="RktVal">3</span><span class="stt"> </span><span class="RktVal">4</span><span class="RktPn">)</span><span class="RktPn">)</span></p></li><li><p><span class="RktPn">(</span><span class="RktSym">distance-to-0</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><span class="RktVal">6</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._%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="RktVal">4</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></p></li><li><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="stt"> </span><span class="RktPn">(</span><span class="RktSym">distance-to-0</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><span class="RktVal">12</span><span class="stt"> </span><span class="RktVal">5</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktVal">10</span><span class="RktPn">)</span></p></li></ul></div><div class="SIntrapara">by hand. Show all steps. Assume that <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> performs its
computation in a single step. Check the results with DrRacket&rsquo;s
stepper. <a href="part_one.html#%28counter._%28exercise._struct1%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><div class="SIntrapara"><a name="(counter._(exercise._struct2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;64. The Manhattan distance of a point to the origin
considers a path that follows the rectangular grid of streets found in
Manhattan. Here are two examples:
</div><div class="SIntrapara"><blockquote class="SCentered"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_68.png" alt="image" width="167.70000000000002" height="136.9"/><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_69.png" alt="image" width="167.70000000000002" height="136.9"/></p></blockquote></div><div class="SIntrapara">The left one shows a &ldquo;direct&rdquo; strategy, going as far left as needed,
followed by as many upward steps as needed. In comparison, the
right one shows a &ldquo;random walk&rdquo; strategy, going some blocks leftward,
some upward, and so on until the destination&#8212;<wbr></wbr>here, the origin&#8212;<wbr></wbr>is reached.</div></p><p>Stop! Does it matter which strategy you follow?</p><p>Design the function <span class="RktSym">manhattan-distance</span>, which measures the
Manhattan distance of the given <span class="RktSym">posn</span> to the origin. <a href="part_one.html#%28counter._%28exercise._struct2%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>5.4<tt>&nbsp;</tt><a name="(part._sec~3astructures)"></a>Defining Structure Types</h4><p>Unlike numbers or Boolean values, structures such as
<span class="RktSym">posn</span> usually don&rsquo;t come with a programming language. Only the
mechanism to define structure types is provided; the rest is left up to the
programmer. This is also true for BSL.</p><p><div class="SIntrapara">A <span style="font-style: italic">structure type definition</span> is another form of definition,
distinct from constant and function definitions. Here is how
the creator of DrRacket defined the <span class="RktSym">posn</span> structure type in BSL:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">posn</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="RktPn">]</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">In general, a structure type definition has this shape:<span class="refelem"><span class="refcolumn"><span class="refcontent">The
use of brackets in a structure type definition is a
convention, not a necessity. It makes the field names stand out. Replacing
brackets with parentheses is perfectly acceptable.</span></span></span>
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktVar">StructureName</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktVar">FieldName</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">]</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">The keyword <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span> signals the introduction of a new
structure type. It is followed by the name of the structure. The third
part of a structure type definition is a sequence of names enclosed in
brackets; these names are the <span style="font-style: italic">fields</span>.</div></p><p><div class="SIntrapara">A structure type definition actually defines functions. But, unlike an
ordinary function definition, <span style="font-weight: bold">a structure type definition defines
many functions</span> simultaneously. Specifically, it defines three
kinds of functions:
</div><div class="SIntrapara"><ul><li><p>one <span style="font-style: italic">constructor</span>, a function that creates <span style="font-style: italic">structure
instances</span>. It takes as many values as there are fields; as mentioned,
<span style="font-style: italic">structure</span> is short for structure instance. The phrase
<span style="font-style: italic">structure type</span> is a generic name for the collection of all
possible instances;</p></li><li><p>one <span style="font-style: italic">selector</span> per field, which extracts the value of the
field from a structure instance; and</p></li><li><p>one <span style="font-style: italic">structure predicate</span>, which, like ordinary predicates,
distinguishes instances from all other kinds of values.</p></li></ul></div><div class="SIntrapara">A program can use these as if they were functions or built-in primitives.</div></p><p>Curiously, a structure type definition makes up names for the various new
operations it creates. For the name of the constructor, it prefixes the
structure name with &ldquo;make-&rdquo; and for the names of the selectors it
postfixes the structure name with the field names. Finally, the predicate
is just the structure name with &ldquo;?&rdquo; added, pronounced &ldquo;huh&rdquo; when read
aloud.</p><p>This naming convention looks complicated and perhaps even confusing. But,
with a little bit of practice, you&rsquo;ll get the hang of it. It also explains
the functions that come with <span class="RktSym">posn</span> structures: <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span>
is the constructor, <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span> are
selectors. While we haven&rsquo;t encountered <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span> yet, we now know
that it exists; the next chapter explains the role of these predicates in
detail.</p><p><div class="SIntrapara"><a name="(counter._(exercise._struct3))"></a><span style="font-weight: bold">Exercise</span>&nbsp;65. Take a look at the following structure type definitions:
</div><div class="SIntrapara"><ul><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">movie</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">title</span><span class="stt"> </span><span class="RktSym">producer</span><span class="stt"> </span><span class="RktSym">year</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">person</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">name</span><span class="stt"> </span><span class="RktSym">hair</span><span class="stt"> </span><span class="RktSym">eyes</span><span class="stt"> </span><span class="RktSym">phone</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">pet</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">name</span><span class="stt"> </span><span class="RktSym">number</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">CD</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">artist</span><span class="stt"> </span><span class="RktSym">title</span><span class="stt"> </span><span class="RktSym">price</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">sweater</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">material</span><span class="stt"> </span><span class="RktSym">size</span><span class="stt"> </span><span class="RktSym">producer</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li></ul></div><div class="SIntrapara">Write down the names of the functions (constructors, selectors, and
predicates) that each introduces. <a href="part_one.html#%28counter._%28exercise._struct3%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><div class="SIntrapara">Enough with <span class="RktSym">posn</span> structures for a while. Let&rsquo;s look at a structure
type definition that we might use to keep track of contacts such as those
in your cell phone:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">entry</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">name</span><span class="hspace">&nbsp;</span><span class="RktSym">phone</span><span class="hspace">&nbsp;</span><span class="RktSym">email</span><span class="RktPn">]</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Here are the names of the functions that this definition introduces:
</div><div class="SIntrapara"><ul><li><p><span class="RktSym">make-entry</span>, which consumes three values and
constructs an instance of <span class="RktSym">entry</span>;</p></li><li><p><span class="RktSym">entry-name</span>, <span class="RktSym">entry-phone</span>, and <span class="RktSym">entry-email</span>,
which consume one instance of <span class="RktSym">entry</span> and select one of the
three field values; and</p></li><li><p><span class="RktSym">entry?</span>, the predicate.</p></li></ul></div><div class="SIntrapara">Since each <span class="RktSym">entry</span> combines three values, the expression
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym">make-entry</span><span class="hspace">&nbsp;</span><span class="RktVal">"Al Abe"</span><span class="hspace">&nbsp;</span><span class="RktVal">"666-7771"</span><span class="hspace">&nbsp;</span><span class="RktVal">"<a href="https://htdp.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="65090000251d4b0800">[email&#160;protected]</a>"</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">creates an <span class="RktSym">entry</span> structure with <span class="RktVal">"Al Abe"</span> in the
<span class="RktSym">name</span> field, <span class="RktVal">"666-7771"</span> in the <span class="RktSym">phone</span> field,
and <span class="RktVal">"<a href="https://htdp.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aec2cbcbeed680c3cb">[email&#160;protected]</a>"</span> in the <span class="RktSym">email</span> field.</div></p><p><a name="(counter._(exercise._struct3b))"></a><span style="font-weight: bold">Exercise</span>&nbsp;66. Revisit the structure type definitions of
<a href="part_one.html#%28counter._%28exercise._struct3%29%29" data-pltdoc="x">exercise&nbsp;65</a>. Make sensible guesses as to what kind of values go with
which fields. Then create at least one instance per structure type
definition. <a href="part_one.html#%28counter._%28exercise._struct3b%29%29" class="ex-end" data-pltdoc="x"></a></p><p>Every structure type definition introduces a new kind of structure, distinct
from all others. Programmers want this kind of expressive power because
they wish to convey an <span style="font-weight: bold">intention</span> with the structure name. Wherever
a structure is created, selected, or tested, the text of the program
explicitly reminds the reader of this intention. If it weren&rsquo;t for these
future readers of code, programmers could use one structure definition for
structures with one field, another for structures with two fields, a third for
structures with three, and so on.</p><p><div class="SIntrapara">In this context, let&rsquo;s study another programming problem:
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> Develop a structure type definition for a program that
deals with &ldquo;bouncing balls,&rdquo; briefly mentioned at the very beginning of this
chapter. The ball&rsquo;s location is a single number, namely the distance of
pixels from the top. Its constant <span style="font-style: italic">speed</span> is the number of pixels
it moves per clock tick. Its <span style="font-style: italic">velocity</span> is the speed <span style="font-weight: bold">plus</span>
the direction in which it moves.</p></blockquote></div><div class="SIntrapara">Since the ball moves along a straight, vertical line, a number is a
perfectly adequate data representation for its velocity:
</div><div class="SIntrapara"><ul><li><p>A positive number means the ball moves down.</p></li><li><p>A negative number means it moves up.</p></li></ul></div><div class="SIntrapara">We can use this domain knowledge to formulate a structure type definition:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ball</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">location</span><span class="hspace">&nbsp;</span><span class="RktSym">velocity</span><span class="RktPn">]</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Both fields are going to contain numbers, so <span class="RktPn">(</span><span class="RktSym">make-ball</span><span class="stt"> </span><span class="RktVal">10</span><span class="stt"> </span><span class="RktVal"><span class="nobreak">-3</span></span><span class="RktPn">)</span> is
a good data example. It represents a ball that is <span class="RktVal">10</span> pixels from
the top and moves up at <span class="RktVal">3</span> pixels per clock tick.</div></p><p>Notice how, in principle, a <span class="RktSym">ball</span> structure merely combines two
numbers, just like a <span class="RktSym">posn</span> structure. When a program contains the
expression <span class="RktPn">(</span><span class="RktSym">ball-velocity</span><span class="stt"> </span><span class="RktSym">a-ball</span><span class="RktPn">)</span>, it immediately conveys that
this program deals with the representation of a ball and its velocity. In
contrast, if the program used <span class="RktSym">posn</span> structures instead,
<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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="stt"> </span><span class="RktSym">a-ball</span><span class="RktPn">)</span> might mislead a reader of the code into thinking
that the expression is about a y-coordinate.</p><p><div class="SIntrapara"><a name="(counter._(exercise._struct4))"></a><span style="font-weight: bold">Exercise</span>&nbsp;67. Here is another way to represent bouncing balls:
</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">SPEED</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">balld</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">location</span><span class="hspace">&nbsp;</span><span class="RktSym">direction</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">make-balld</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">"up"</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Interpret this code fragment and create other instances of <span class="RktSym">balld</span>. <a href="part_one.html#%28counter._%28exercise._struct4%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p>Since structures are values, just like numbers or Booleans or strings, it
makes sense that one instance of a structure occurs inside another
instance. Consider game objects. Unlike bouncing balls, such objects don&rsquo;t
always move along vertical lines. Instead, they move in some &ldquo;oblique&rdquo;
manner across the canvas. Describing both the location and the velocity
of a ball moving across a 2-dimensional world canvas demands two numbers:
one per direction.<span class="refelem"><span class="refcolumn"><span class="refcontent">It is physics that tells you to add an
object&rsquo;s velocity to its location to obtain its next location. Developers
need to learn whom to ask about which domain.</span></span></span> For the location part, the
two numbers represent the x- and y-coordinates. Velocity describes the
<span style="font-style: italic">changes</span> in the horizontal and vertical direction; in other words,
these &ldquo;change numbers&rdquo; must be added to the respective coordinates to
find out where the object will be next.</p><p><div class="SIntrapara">Clearly, <span class="RktSym">posn</span> structures can represent locations. For the
velocities, we define the <span class="RktSym">vel</span> structure 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#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">vel</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">deltax</span><span class="hspace">&nbsp;</span><span class="RktSym">deltay</span><span class="RktPn">]</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">It comes with two fields: <span class="RktSym">deltax</span> and <span class="RktSym">deltay</span>. The word
&ldquo;delta&rdquo; is commonly used to speak of change when it comes to simulations
of physical activities, and the x and y parts indicate which axis is
concerned.</div></p><p><div class="SIntrapara">Now we can use instances of <span class="RktSym">ball</span> to combine a <span class="RktSym">posn</span>
structure with a <span class="RktSym">vel</span> structure to represent balls that move in
straight lines but not necessarily along only vertical (or horizontal) lines:
</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">ball1</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-ball</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">30</span><span class="hspace">&nbsp;</span><span class="RktVal">40</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-vel</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span>0</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">One way to interpret this instance is to think of a ball that is
<span class="RktVal">30</span> pixels from the left and <span class="RktVal">40</span> pixels from the
top. It moves <span class="RktVal">10</span> pixels toward the left per clock tick, because
subtracting 10 pixels from the x-coordinate brings it closer to the
left. As for the vertical direction, the ball drops at <span class="RktVal">5</span> pixels
per clock tick, because adding positive numbers to a y-coordinate
increases the distance from the top.</div></p><p><div class="SIntrapara"><a name="(counter._(exercise._struct5))"></a><span style="font-weight: bold">Exercise</span>&nbsp;68. An alternative to the nested data representation of
balls uses four fields to keep track of the four
properties:<span class="refelem"><span class="refcolumn"><span class="refcontent">Yet another alternative is to use
<span style="font-style: italic">complex numbers</span>. If you know about them, contemplate a data
representation that uses them for both location and velocity. For example,
in BSL, <span class="RktVal">4-3i</span> is a complex number and could be used to
represent the location or velocity <span style="font-style: italic"></span>(<span style="font-style: italic"></span>4<span style="font-style: italic">,-</span>3<span style="font-style: italic"></span>)<span style="font-style: italic"></span>.</span></span></span>
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ballf</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">deltax</span><span class="hspace">&nbsp;</span><span class="RktSym">deltay</span><span class="RktPn">]</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Programmers call this a <span style="font-style: italic">flat representation</span>. Create an
instance of <span class="RktSym">ballf</span> that has the same interpretation as <span class="RktSym">ball1</span>. <a href="part_one.html#%28counter._%28exercise._struct5%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><div class="SIntrapara">For a second example of nested structures, let&rsquo;s briefly look at the
example of contact lists. Many cell phones support contact lists that
allow several phone numbers per name: one for a home line, one for the
office, and one for a cell phone number. For phone numbers, we wish to
include both the area code and the local number. Since this nests the
information, it&rsquo;s best to create a nested data representation, too:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">centry</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">name</span><span class="hspace">&nbsp;</span><span class="RktSym">home</span><span class="hspace">&nbsp;</span><span class="RktSym">office</span><span class="hspace">&nbsp;</span><span class="RktSym">cell</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"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">phone</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">area</span><span class="hspace">&nbsp;</span><span class="RktSym">number</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">make-centry</span><span class="hspace">&nbsp;</span><span class="RktVal">"Shriram Fisler"</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">make-phone</span><span class="hspace">&nbsp;</span><span class="RktVal">207</span><span class="hspace">&nbsp;</span><span class="RktVal">"363-2421"</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">make-phone</span><span class="hspace">&nbsp;</span><span class="RktVal">101</span><span class="hspace">&nbsp;</span><span class="RktVal">"776-1099"</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">make-phone</span><span class="hspace">&nbsp;</span><span class="RktVal">208</span><span class="hspace">&nbsp;</span><span class="RktVal">"112-9981"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">The intention here is that an entry on a contact list has four fields: a
name and three phone records. The latter are represented with instance of
<span class="RktSym">phone</span>, which separates the area code from the local phone number.</div></p><p>In sum, nesting information is natural. The best way to represent such
information with data is to mirror the nesting with nested structure
instances. Doing so makes it easy to interpret the data in the application
domain of the program, and it is also straightforward to go from examples
of information to data. Of course, it is really the task of data
definitions to specify how to go back and forth between information and
data. Before we study data definitions for structure type definitions,
however, we first take a systematic look at computing with, and thinking
about, structures.</p><h4>5.5<tt>&nbsp;</tt><a name="(part._sec~3aeval-structs)"></a>Computing with Structures</h4><p>Structure types generalize Cartesian points in two ways. First, a
structure type may specify an arbitrary number of fields: zero, one, two,
three, and so forth. Second, structure types name fields, they don&rsquo;t
number them.<span class="refelem"><span class="refcolumn"><span class="refcontent">Most programming languages also support
structure-like data that use numeric field names.</span></span></span> This helps programmers
read code because it is much easier to
remember that a family name is available in a field called
<span class="RktSym">last-name</span> than in the <span style="font-style: italic"></span>7<span style="font-style: italic"></span>th field.</p><p><div class="SIntrapara">In the same spirit, computing with structure instances generalizes the
manipulation of Cartesian points. To appreciate this idea, let us first
look at a diagrammatic way to think about structure instances as lockboxes
with as many compartments as there are fields. Here is a representation of
</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">pl</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-entry</span><span class="hspace">&nbsp;</span><span class="RktVal">"Al Abe"</span><span class="hspace">&nbsp;</span><span class="RktVal">"666-7771"</span><span class="hspace">&nbsp;</span><span class="RktVal">"<a href="https://htdp.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a666f6f4a7224676f">[email&#160;protected]</a>"</span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">as such a diagram:
</div><div class="SIntrapara"><blockquote class="SCentered"><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_70.png" alt="image" width="206.388671875" height="53.861328125"/></p></blockquote></div><div class="SIntrapara">The box&rsquo;s italicized label identifies it as an instance of a
specific structure type; each compartment is labeled, too. Here is another
instance:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym">make-entry</span><span class="hspace">&nbsp;</span><span class="RktVal">"Tara Harp"</span><span class="hspace">&nbsp;</span><span class="RktVal">"666-7770"</span><span class="hspace">&nbsp;</span><span class="RktVal">"<a href="https://htdp.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="daaeb29aa9b7b6aff4bfbeaf">[email&#160;protected]</a>"</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">corresponds to a similar box diagram, though the content differs:
</div><div class="SIntrapara"><blockquote class="SCentered"><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_71.png" alt="image" width="243.0361328125" height="53.861328125"/></p></blockquote></div></p><p>Not surprisingly, nested structure instances have a diagram of boxes
nested in boxes. Thus, <span class="RktSym">ball1</span> from above is equivalent to this
diagram:</p><p><div class="SIntrapara"><blockquote class="SCentered"><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_72.png" alt="image" width="123.921875" height="85.64453125"/></p></blockquote></div><div class="SIntrapara">In this case, the outer box contains two boxes, one per field.</div></p><p><a name="(counter._(exercise._struct3a))"></a><span style="font-weight: bold">Exercise</span>&nbsp;69. Draw box representations for the solution of <a href="part_one.html#%28counter._%28exercise._struct3%29%29" data-pltdoc="x">exercise&nbsp;65</a>. <a href="part_one.html#%28counter._%28exercise._struct3a%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara">In the context of this imagery, a selector is like a key. It opens a
specific compartment for a certain kind of box and thus enables the holder
to extract its content. Hence, applying <span class="RktSym">entry-name</span> to <span class="RktSym">pl</span>
from above yields a string:
</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">entry-name</span><span class="hspace">&nbsp;</span><span class="RktSym">pl</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"Al Abe"</span></p></td></tr></table></blockquote></div><div class="SIntrapara">But <span class="RktSym">entry-name</span> applied to a <span class="RktSym">posn</span> structure signals an
error:
</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">entry-name</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">42</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">entry-name:expects an entry, given (make-posn 42 5)</span></p></td></tr></table></blockquote></div><div class="SIntrapara">If a compartment contains a box, it might be necessary to use two
selectors in a row to get to the desired number:
</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">ball-velocity</span><span class="hspace">&nbsp;</span><span class="RktSym">ball1</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">(make-vel -10 5)</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Applying <span class="RktSym">ball-velocity</span> to <span class="RktSym">ball1</span> extracts the value of
the velocity field, which is an instance of <span class="RktSym">vel</span>. To get to the
velocity along the x axis, we apply a selector to the result of the first
selection:
</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">vel-deltax</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ball-velocity</span><span class="hspace">&nbsp;</span><span class="RktSym">ball1</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">-10</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Since the inner expression extracts the velocity from <span class="RktSym">ball1</span>, the
outer expression extracts the value of the <span class="RktSym">deltax</span> field, which in
this case is <span class="RktVal"><span class="nobreak">-1</span>0</span>.</div></p><p><div class="SIntrapara">The interactions also show that structure instances are values. DrRacket
prints them exactly as entered, just like for plain values such as 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">make-vel</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span>0</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">(make-vel -10 5)</span></p></td></tr><tr><td><span class="stt">&gt; </span><span class="RktPn">(</span><span class="RktSym">make-entry</span><span class="hspace">&nbsp;</span><span class="RktVal">"Tara Harp"</span><span class="hspace">&nbsp;</span><span class="RktVal">"666-7770"</span><span class="hspace">&nbsp;</span><span class="RktVal">"<a href="https://htdp.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="582c30182b35342d763d3c2d">[email&#160;protected]</a>"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">(make-entry "Tara Harp" "666-7770" "<a href="https://htdp.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec9884ac9f818099c2898899">[email&#160;protected]</a>")</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">make-centry</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktVal">"Shriram Fisler"</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-phone</span><span class="hspace">&nbsp;</span><span class="RktVal">207</span><span class="hspace">&nbsp;</span><span class="RktVal">"363-2421"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-phone</span><span class="hspace">&nbsp;</span><span class="RktVal">101</span><span class="hspace">&nbsp;</span><span class="RktVal">"776-1099"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-phone</span><span class="hspace">&nbsp;</span><span class="RktVal">208</span><span class="hspace">&nbsp;</span><span class="RktVal">"112-9981"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><p><span class="RktRes">(make-centry ...)</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Stop! Try this last interaction at home, so you see the proper result.</div></p><p>Generally speaking, a structure type definition not only creates new
functions and new ways to create values, but it also adds new laws of
computation to DrRacket&rsquo;s knowledge. These laws generalize those for
<span class="RktSym">posn</span> structures in <a href="part_one.html#%28part._sec~3aeval-posns%29" data-pltdoc="x">Computing with <span class="RktSym">posn</span>s</a>, and they
are best understood by example.</p><p><div class="SIntrapara">When DrRacket encounters a structure type definition with two fields,
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ball</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">location</span><span class="hspace">&nbsp;</span><span class="RktSym">velocity</span><span class="RktPn">]</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">it introduces two laws, one per selector:
</div><div class="SIntrapara"><blockquote><table cellspacing="0" cellpadding="0"><tr><td valign="top"><p><span class="RktPn">(</span><span class="RktSym">ball-location</span><span class="stt"> </span><span class="RktPn">(</span><span class="RktSym">make-ball</span><span class="stt"> </span><span class="RktSym">l0</span><span class="stt"> </span><span class="RktSym">v0</span><span class="RktPn">)</span><span class="RktPn">)</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><p><span class="RktSym">==</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><p><span class="RktSym">l0</span></p></td></tr><tr><td valign="top"><p><span class="RktPn">(</span><span class="RktSym">ball-velocity</span><span class="stt"> </span><span class="RktPn">(</span><span class="RktSym">make-ball</span><span class="stt"> </span><span class="RktSym">l0</span><span class="stt"> </span><span class="RktSym">v0</span><span class="RktPn">)</span><span class="RktPn">)</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><p><span class="RktSym">==</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><p><span class="RktSym">v0</span></p></td></tr></table></blockquote></div><div class="SIntrapara">For different structure type definitions, it introduces analogous laws. Thus,
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">vel</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">deltax</span><span class="hspace">&nbsp;</span><span class="RktSym">deltay</span><span class="RktPn">]</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">DrRacket adds these two laws to its knowledge:
</div><div class="SIntrapara"><blockquote><table cellspacing="0" cellpadding="0"><tr><td valign="top"><p><span class="RktPn">(</span><span class="RktSym">vel-deltax</span><span class="stt"> </span><span class="RktPn">(</span><span class="RktSym">make-vel</span><span class="stt"> </span><span class="RktSym">dx0</span><span class="stt"> </span><span class="RktSym">dy0</span><span class="RktPn">)</span><span class="RktPn">)</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><p><span class="RktSym">==</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><p><span class="RktSym">dx0</span></p></td></tr><tr><td valign="top"><p><span class="RktPn">(</span><span class="RktSym">vel-deltay</span><span class="stt"> </span><span class="RktPn">(</span><span class="RktSym">make-vel</span><span class="stt"> </span><span class="RktSym">dx0</span><span class="stt"> </span><span class="RktSym">dy0</span><span class="RktPn">)</span><span class="RktPn">)</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><p><span class="RktSym">==</span></p></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><p><span class="RktSym">dy0</span></p></td></tr></table></blockquote></div></p><p><div class="SIntrapara">Using these laws, we can now explain the interaction from above:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">vel-deltax</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ball-velocity</span><span class="hspace">&nbsp;</span><span class="RktSym">ball1</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> replaces </span><span class="RktSym">ball1</span><span class="RktCmt"> with its value </span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">vel-deltax</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ball-velocity</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-ball</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">30</span><span class="hspace">&nbsp;</span><span class="RktVal">40</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-vel</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span>0</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> uses the law for </span><span class="RktSym">ball-velocity</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">vel-deltax</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-vel</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-1</span>0</span><span class="hspace">&nbsp;</span><span class="RktVal">5</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktSym">==</span><span class="hspace">&nbsp;</span><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">DrRacket</span><span class="RktCmt"> uses the law for </span><span class="RktSym">vel-deltax</span></td></tr><tr><td><span class="RktVal"><span class="nobreak">-1</span>0</span></td></tr></table></blockquote></div></p><p><div class="SIntrapara"><a name="(counter._(exercise._ex~3acompute-struct1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;70. Spell out the laws for these
structure type definitions:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">centry</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">name</span><span class="hspace">&nbsp;</span><span class="RktSym">home</span><span class="hspace">&nbsp;</span><span class="RktSym">office</span><span class="hspace">&nbsp;</span><span class="RktSym">cell</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">phone</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">area</span><span class="hspace">&nbsp;</span><span class="RktSym">number</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Use DrRacket&rsquo;s stepper to confirm <span class="RktVal">101</span> as the value of this expression:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">phone-area</span></td></tr><tr><td><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">centry-office</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-centry</span><span class="hspace">&nbsp;</span><span class="RktVal">"Shriram Fisler"</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-phone</span><span class="hspace">&nbsp;</span><span class="RktVal">207</span><span class="hspace">&nbsp;</span><span class="RktVal">"363-2421"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-phone</span><span class="hspace">&nbsp;</span><span class="RktVal">101</span><span class="hspace">&nbsp;</span><span class="RktVal">"776-1099"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-phone</span><span class="hspace">&nbsp;</span><span class="RktVal">208</span><span class="hspace">&nbsp;</span><span class="RktVal">"112-9981"</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><a href="part_one.html#%28counter._%28exercise._ex~3acompute-struct1%29%29" class="ex-end" data-pltdoc="x"></a></td></tr></table></blockquote></div></p><p><div class="SIntrapara">Predicates are the last idea that we must discuss to understand
structure type definitions. As mentioned, every structure type definition
introduces one new predicate. DrRacket uses these predicates to discover
whether a selector is applied to the proper kind of value; the next
chapter explains this idea in detail. Here we just want to convey
that these predicates are just like the predicates from &ldquo;arithmetic.&rdquo;
While <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~3f%29%29" class="RktValLink" data-pltdoc="x">number?</a></span> recognizes numbers 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._string~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></span> recognizes
strings, predicates such as <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span> and <span class="RktSym">entry?</span> recognize
<span class="RktSym">posn</span> structures and <span class="RktSym">entry</span> structures. We can confirm our
ideas of how they work with experiments in the interactions area. Assume
that the definitions area contains these definitions:
</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">ap</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">7</span><span class="hspace">&nbsp;</span><span class="RktVal">0</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"><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">pl</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-entry</span><span class="hspace">&nbsp;</span><span class="RktVal">"Al Abe"</span><span class="hspace">&nbsp;</span><span class="RktVal">"666-7771"</span><span class="hspace">&nbsp;</span><span class="RktVal">"<a href="https://htdp.org/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dbb7bebe9ba3f5b6be">[email&#160;protected]</a>"</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">If <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span> is a predicate that distinguishes <span class="RktSym">posn</span>s
from all other values, we should expect that it yields
<span class="RktVal">#false</span> for numbers and <span class="RktVal">#true</span> for <span class="RktSym">ap</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/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ap</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._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">42</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._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</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">#true</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Similarly, <span class="RktSym">entry?</span> distinguishes <span class="RktSym">entry</span> structures from
all other 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">entry?</span><span class="hspace">&nbsp;</span><span class="RktSym">pl</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">entry?</span><span class="hspace">&nbsp;</span><span class="RktVal">42</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">entry?</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">#false</span></p></td></tr></table></blockquote></div><div class="SIntrapara">In general, a predicate recognizes exactly those values constructed with a
constructor of the same name. <a href="i1-2.html" data-pltdoc="x">Intermezzo 1: Beginning Student Language</a> explains this law in detail,
and it also collects the laws of computing for BSL in one place.</div></p><p><div class="SIntrapara"><a name="(counter._(exercise._ex~3acompute-struct2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;71. Place the following into DrRacket&rsquo;s
definitions area:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">distances in terms of pixels:</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="RktVal">200</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">MIDDLE</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._quotient%29%29" class="RktValLink" data-pltdoc="x">quotient</a></span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktVal">2</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">WIDTH</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktVal">400</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="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._quotient%29%29" class="RktValLink" data-pltdoc="x">quotient</a></span><span class="hspace">&nbsp;</span><span class="RktSym">WIDTH</span><span class="hspace">&nbsp;</span><span class="RktVal">2</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"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._define-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">game</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">left-player</span><span class="hspace">&nbsp;</span><span class="RktSym">right-player</span><span class="hspace">&nbsp;</span><span class="RktSym">ball</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"><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">game0</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-game</span><span class="hspace">&nbsp;</span><span class="RktSym">MIDDLE</span><span class="hspace">&nbsp;</span><span class="RktSym">MIDDLE</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%2
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">game-ball</span><span class="hspace">&nbsp;</span><span class="RktSym">game0</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._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">game-ball</span><span class="hspace">&nbsp;</span><span class="RktSym">game0</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym">game-left-player</span><span class="hspace">&nbsp;</span><span class="RktSym">game0</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Explain the results with step-by-step computations. Double-check your computations with
DrRacket&rsquo;s stepper. <a href="part_one.html#%28counter._%28exercise._ex~3acompute-struct2%29%29" class="ex-end" data-pltdoc="x"></a></div></p><h4>5.6<tt>&nbsp;</tt><a name="(part._sec~3aprogstructs)"></a>Programming with Structures</h4><p><div class="SIntrapara">Proper programming calls for data definitions. With the introduction of
structure type definitions, data definitions become interesting. Remember
that a data definition provides a way of representing information into
data and interpreting that data as information. For structure types, this calls
for a description of what kind of data goes into which field. For some
structure type definitions, formulating such descriptions is easy and
obvious:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">posn</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._posn)"></a><span style="font-style: italic">Posn</span><span class="RktCmt"> is a structure: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> a point </span><span class="RktSym">x</span><span class="RktCmt"> pixels from left, </span><span class="RktSym">y</span><span class="RktCmt"> from top</span></td></tr></table></blockquote></div><div class="SIntrapara">It doesn&rsquo;t make any sense to use other kinds of data to create a
<span class="RktSym">posn</span>. Similarly, all fields of <span class="RktSym">entry</span>&#8212;<wbr></wbr>our structure type
definition for entries on a contact list&#8212;<wbr></wbr>are clearly supposed to be
strings, according to our usage in the preceding section:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">entry</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">name</span><span class="hspace">&nbsp;</span><span class="RktSym">phone</span><span class="hspace">&nbsp;</span><span class="RktSym">email</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">An </span><a name="(tech._entry)"></a><span style="font-style: italic">Entry</span><span class="RktCmt"> is a structure: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-entry</span><span class="stt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="stt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="stt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> a contact</span><span class="RktCmt">'</span><span class="RktCmt">s name, phone#, and email </span></td></tr></table></blockquote></div><div class="SIntrapara">For both <span class="RktSym">posn</span> and <span class="RktSym">entry</span>, a reader can easily interpret
instances of these structures in the application domain.</div></p><p><div class="SIntrapara">Contrast this simplicity with the structure type definition for
<span class="RktSym">ball</span>, which obviously allows at least two distinct
interpretations:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ball</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">location</span><span class="hspace">&nbsp;</span><span class="RktSym">velocity</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._ball._1d)"></a><span style="font-style: italic">Ball-1d</span><span class="RktCmt"> is a structure:</span><span class="hspace">&nbsp;&nbsp;</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-ball</span><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation 1</span><span class="RktCmt"> distance to top and velocity </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation 2</span><span class="RktCmt"> distance to left and velocity </span></td></tr></table></blockquote></div><div class="SIntrapara">Whichever one we use in a program, we must stick to it consistently. As
<a href="part_one.html#%28part._sec~3astructures%29" data-pltdoc="x">Defining Structure Types</a> shows, however, it is also possible to use
<span class="RktSym">ball</span> structures in an entirely different manner:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._ball._2d)"></a><span style="font-style: italic">Ball-2d</span><span class="RktCmt"> is a structure: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-ball</span><span class="stt"> </span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="stt"> </span><a href="part_one.html#%28tech._vel%29" class="techoutside" data-pltdoc="x"><span class="techinside">Vel</span></a><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> a 2-dimensional position and velocity</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">vel</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">deltax</span><span class="hspace">&nbsp;</span><span class="RktSym">deltay</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._vel)"></a><span style="font-style: italic">Vel</span><span class="RktCmt"> is a structure: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-vel</span><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> </span><span class="RktPn">(</span><span class="RktSym">make-vel</span><span class="stt"> </span><span class="RktSym">dx</span><span class="stt"> </span><span class="RktSym">dy</span><span class="RktPn">)</span><span class="RktCmt"> means a velocity of </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktSym">dx</span><span class="RktCmt"> pixels [per tick] along the horizontal and</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktSym">dy</span><span class="RktCmt"> pixels [per tick] along the vertical direction</span></td></tr></table></blockquote></div></p><p>Here we name a second collection of data, <a href="part_one.html#%28tech._ball._2d%29" class="techoutside" data-pltdoc="x"><span class="techinside">Ball-2d</span></a>, distinct
from <a href="part_one.html#%28tech._ball._1d%29" class="techoutside" data-pltdoc="x"><span class="techinside">Ball-1d</span></a>, to describe data representations for
balls that move in straight lines across a world canvas. In short, it is
possible to use <span style="font-weight: bold">one and the same</span> structure type in <span style="font-weight: bold">two
different ways</span>. Of course, within one program, it is best to stick to one
and only one use; otherwise you are setting yourself up for problems.</p><p>Also, <a href="part_one.html#%28tech._ball._2d%29" class="techoutside" data-pltdoc="x"><span class="techinside">Ball-2d</span></a> refers to another one of our data
definitions, namely, the one for <a href="part_one.html#%28tech._vel%29" class="techoutside" data-pltdoc="x"><span class="techinside">Vel</span></a>. While all other data
definitions have thus far referred to built-in data collections
(<a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a>, <a href="part_one.html#%28tech._boolean%29" class="techoutside" data-pltdoc="x"><span class="techinside">Boolean</span></a>, <a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a>), it is perfectly
acceptable, and indeed common, that one of your data definitions refers to
another.</p><p><a name="(counter._(exercise._struct6))"></a><span style="font-weight: bold">Exercise</span>&nbsp;72. Formulate a data definition for the above <span class="RktSym">phone</span>
structure type definition that accommodates the given examples.</p><p><div class="SIntrapara">Next formulate a data definition for phone numbers using this structure
type definition:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">phone#</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">area</span><span class="hspace">&nbsp;</span><span class="RktSym">switch</span><span class="hspace">&nbsp;</span><span class="RktSym">num</span><span class="RktPn">]</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Historically, the first three digits make up the area code, the next three the
code for the phone switch (exchange) of your neighborhood, and the last
four the phone with respect to the neighborhood. Describe the
content of the three fields as precisely as possible with intervals. <a href="part_one.html#%28counter._%28exercise._struct6%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p>At this point, you might be wondering what data definitions really
mean. This question, and its answer, is the topic of the next section.
For now, we indicate how to use data definitions for program design.</p><p><div class="SIntrapara">Here is a problem statement to set up some context:
</div><div class="SIntrapara"><blockquote><p><div class="SIntrapara"><span style="font-weight: bold">Sample Problem</span> Your team is designing an interactive game program that
moves a red dot across a <img src="pict_73.png" alt="image" width="51" height="9"/> canvas and allows players to use the
mouse to reset the dot. Here is how far you got together:<a name="(idx._(gentag._131))"></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">MTS</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">100</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">DOT</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">3</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><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="RktCmt"> represents the state of the world.</span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="RktCmt"> </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">main</span><span class="hspace">&nbsp;</span><span class="RktSym">p0</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p0</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-tick%29%29" class="RktStxLink" data-pltdoc="x">on-tick</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x+</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">[</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-mouse%29%29" class="RktStxLink" data-pltdoc="x">on-mouse</a></span><span class="hspace">&nbsp;</span><span class="RktSym">reset-dot</span><span class="RktPn">]</span></td></tr><tr><td><span class="hsp
dot to the empty canvas at the specified position.</div></p></blockquote></div><div class="SIntrapara">The problem context dictates the signature of your function:<a name="(idx._(gentag._132))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds a red spot to </span><span class="RktSym">MTS</span><span class="RktCmt"> at </span><span class="RktSym">p</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">scene+dot</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">MTS</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Adding a purpose statement is straightforward. As
<a href="part_one.html#%28part._sec~3adesign-func%29" data-pltdoc="x">Designing Functions</a> mentions, it uses the function&rsquo;s parameter to
express what the function computes.</div></p><p><div class="SIntrapara">Now we work out a couple of examples and formulate them as tests:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">scene+dot</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">20</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;&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">DOT</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktSym">MTS</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">scene+dot</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">88</span><span class="hspace">&nbsp;</span><span class="RktVal">73</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;&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">DOT</span><span class="hspace">&nbsp;</span><span class="RktVal">88</span><span class="hspace">&nbsp;</span><span class="RktVal">73</span><span class="hspace">&nbsp;</span><span class="RktSym">MTS</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p><div class="SIntrapara">Given that the function consumes a <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>, we know that the function can
extract the values of the <span class="RktSym">x</span> and <span class="RktSym">y</span> fields:
</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">scene+dot</span><span class="hspace">&nbsp;</span><span class="RktSym">p</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._......%29%29" class="RktStxLink" 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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" 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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Once we see these additional pieces in the body of the function, the rest
of the definition is straightforward. Using <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 function
puts <span class="RktSym">DOT</span> into <span class="RktSym">MTS</span> at the coordinates contained in <span class="RktSym">p</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">scene+dot</span><span class="hspace">&nbsp;</span><span class="RktSym">p</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><span class="RktSym">DOT</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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">MTS</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p><div class="SIntrapara">A function may produce structures. Let&rsquo;s resume our
sample problem from above because it includes just such a task:
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> A colleague is asked to define <span class="RktSym">x+</span>, a
function that consumes a <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a> and increases the x-coordinate by
<span class="RktVal">3</span>.</p></blockquote></div><div class="SIntrapara">Recall that the <span class="RktSym">x+</span> function handles clock ticks.</div></p><p><div class="SIntrapara">We can adapt the first few steps of the design of <span class="RktSym">scene+dot</span>: <a name="(idx._(gentag._133))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">increases the x-coordinate of </span><span class="RktSym">p</span><span class="RktCmt"> by </span><span class="RktVal">3</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">x+</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">0</span><span class="RktPn">)</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">13</span><span class="hspace">&nbsp;</span><span class="RktVal">0</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="RktPn">(</span><span class="RktSym">x+</span><span class="hspace">&nbsp;</span><span class="RktSym">p</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._......%29%29" class="RktStxLink" 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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" 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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">The signature, the purpose, and the example all come out of the
problem statement. Instead of a header&#8212;<wbr></wbr>a function with a default
result&#8212;<wbr></wbr>our sketch contains the two selector expressions for
<a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>s. After all, the information for the result must come from the
inputs, and the input is a structure that contains two values.</div></p><p><div class="SIntrapara">Finishing the definition is easy now. Since the desired result is a
<a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>, the function 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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span> to combine the
pieces:
</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">x+</span><span class="hspace">&nbsp;</span><span class="RktSym">p</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">3</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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p><a name="(counter._(exercise._ex~3aupdater1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;73. Design the function <span class="RktSym">posn-up-x</span>, which
consumes a <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a> <span class="RktSym">p</span> and a Number <span class="RktSym">n</span>. It produces a
<a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a> like <span class="RktSym">p</span> with <span class="RktSym">n</span> in the <span class="RktSym">x</span> field.</p><p><div class="SIntrapara">A neat observation is that we can define <span class="RktSym">x+</span> using <span class="RktSym">posn-up-x</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">x+</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">posn-up-x</span><span class="hspace">&nbsp;</span><span class="RktSym">p</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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara"><span style="font-weight: bold">Note</span> Functions such as <span class="RktSym">posn-up-x</span> are often called
<span style="font-style: italic">updaters</span> or <span style="font-style: italic">functional setters</span>. They are extremely
useful when you write large programs. <a href="part_one.html#%28counter._%28exercise._ex~3aupdater1%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><div class="SIntrapara">A function may also produce instances from atomic data. While
<span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span> is a built-in primitive that does so, our
running problem provides another fitting illustration:
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> Another colleague is tasked to design
<span class="RktSym">reset-dot</span>, a function that resets the dot when
the mouse is clicked.</p></blockquote></div><div class="SIntrapara">To tackle this problem, you need to recall from <a href="part_one.html#%28part._.D.K._sec~3adesign-world%29" data-pltdoc="x">Designing World Programs</a>
that mouse-event handlers consume four values: the current state of the
world, the x- and y-coordinates of the mouse click, and a <a href="part_one.html#%28tech._mouseevt%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvt</span></a>.</div></p><p><div class="SIntrapara">By adding the knowledge from the sample problem to the program
design recipe, we get a signature, a purpose statement, and a header:
<a name="(idx._(gentag._134))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._mouseevt%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvt</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">for mouse clicks, </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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><span class="RktSym">x</span><span class="stt"> </span><span class="RktSym">y</span><span class="RktPn">)</span><span class="RktCmt">; otherwise </span><span class="RktSym">p</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">reset-dot</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">me</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p><div class="SIntrapara">Examples for mouse-event handlers need a <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>, two numbers, and a <a href="part_one.html#%28tech._mouseevt%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvt</span></a>,
which is just a special kind of <a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a>. A mouse click, for example, is
represented with one of two strings: <span class="RktVal">"button-down"</span> and
<span class="RktVal">"button-up"</span>. The first one signals that a user clicked the mouse
button, the latter signals its release. With this in mind,
here are two examples, which you may wish to study and interpret:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">reset-dot</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">29</span><span class="hspace">&nbsp;</span><span class="RktVal">31</span><span class="hspace">&nbsp;</span><span class="RktVal">"button-down"</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">29</span><span class="hspace">&nbsp;</span><span class="RktVal">31</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">reset-dot</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">29</span><span class="hspace">&nbsp;</span><span class="RktVal">31</span><span class="hspace">&nbsp;</span><span class="RktVal">"button-up"</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p><div class="SIntrapara">Although the function consumes only atomic forms of data, its purpose
statement and the examples suggest that it differentiates
between two kinds of <a href="part_one.html#%28tech._mouseevt%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvt</span></a>s: <span class="RktVal">"button-down"</span> and all
others. Such a case split suggests 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:
</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">reset-dot</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">me</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/teachpack/2htdpuniverse.html#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._mouse~3d~3f%29%29" class="RktValLink" data-pltdoc="x">mouse=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"button-down"</span><span class="hspace">&nbsp;</span><span class="RktSym">me</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#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._else%29%29" class="RktStxLink" data-pltdoc="x">else</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._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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">Following the design recipe, this skeleton mentions the parameters to
remind you of what data is available.</div></p><p><div class="SIntrapara">The rest is straightforward again because the purpose statement itself
dictates what the function computes in each of the two cases:
</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">reset-dot</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">me</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/teachpack/2htdpuniverse.html#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._mouse~3d~3f%29%29" class="RktValLink" data-pltdoc="x">mouse=?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">me</span><span class="hspace">&nbsp;</span><span class="RktVal">"button-down"</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</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="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">As above, we could have mentioned that <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span> creates
instances of <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>, but you know this and we don&rsquo;t need to remind
you constantly.</div></p><p><a name="(counter._(exercise._ex~3arun-mouse-program))"></a><span style="font-weight: bold">Exercise</span>&nbsp;74. Copy all relevant constant and function
definitions to DrRacket&rsquo;s definitions area. Add the tests and make sure they
pass. Then run the program and use the mouse to place the red dot. <a href="part_one.html#%28counter._%28exercise._ex~3arun-mouse-program%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara">Many programs deal with nested structures. We illustrate this point with
another small excerpt from a world program:
</div><div class="SIntrapara"><blockquote><p><div class="SIntrapara"><span style="font-weight: bold">Sample Problem</span> Your team is designing a game program that keeps track of
an object that moves across the canvas at changing speed. The chosen data
representation requires two data definitions:<span class="refelem"><span class="refcolumn"><span class="refcontent">Remember, it&rsquo;s about physics.</span></span></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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ufo</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">loc</span><span class="hspace">&nbsp;</span><span class="RktSym">vel</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._ufo)"></a><span style="font-style: italic">UFO</span><span class="RktCmt"> is a structure: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-ufo</span><span class="stt"> </span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="stt"> </span><a href="part_one.html#%28tech._vel%29" class="techoutside" data-pltdoc="x"><span class="techinside">Vel</span></a><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> </span><span class="RktPn">(</span><span class="RktSym">make-ufo</span><span class="stt"> </span><span class="RktSym">p</span><span class="stt"> </span><span class="RktSym">v</span><span class="RktPn">)</span><span class="RktCmt"> is at location</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktSym">p</span><span class="RktCmt"> moving at velocity </span><span class="RktSym">v</span></td></tr></table></blockquote></div><div class="SIntrapara">It is your task to develop <span class="RktSym">ufo-move-1</span>. The function computes the
location of a given <a href="part_one.html#%28tech._ufo%29" class="techoutside" data-pltdoc="x"><span class="techinside">UFO</span></a> after one clock tick passes.</div></p></blockquote></div></p><p><div class="SIntrapara">Let us start with some examples that explore the data definitions a bit:
</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">v1</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-vel</span><span class="hspace">&nbsp;</span><span class="RktVal">8</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></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">v2</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-vel</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-5</span></span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;</span></td></tr><tr><td><span class="refelem"><span class="refcolumn"><span class="refcontent">The order of these definitions matters. See <a href="i1-2.html" data-pltdoc="x">Intermezzo 1: Beginning Student Language</a>.</span></span></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">p1</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">22</span><span class="hspace">&nbsp;</span><span class="RktVal">80</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">p2</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">30</span><span class="hspace">&nbsp;</span><span class="RktVal">77</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"><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">u1</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-ufo</span><span class="hspace">&nbsp;</span><span class="RktSym">p1</span><span class="hspace">&nbsp;</span><span class="RktSym">v1</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
combine the first four in all possible combinations.</div></p><p><div class="SIntrapara">Next we write down a signature, a purpose, some examples, and a function
header:<a name="(idx._(gentag._135))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._ufo%29" class="techoutside" data-pltdoc="x"><span class="techinside">UFO</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._ufo%29" class="techoutside" data-pltdoc="x"><span class="techinside">UFO</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">determines where </span><span class="RktSym">u</span><span class="RktCmt"> moves in one clock tick; </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">leaves the velocity as is</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-move-1</span><span class="hspace">&nbsp;</span><span class="RktSym">u1</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">u3</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-move-1</span><span class="hspace">&nbsp;</span><span class="RktSym">u2</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;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-ufo</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">17</span><span class="hspace">&nbsp;</span><span class="RktVal">77</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">v2</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"><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">ufo-move-1</span><span class="hspace">&nbsp;</span><span class="RktSym">u</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">u</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">For the <a name="(idx._(gentag._136))"></a>function examples, we use the <a name="(idx._(gentag._137))"></a>data examples <span style="font-weight: bold">and</span> our
domain knowledge of positions and velocities. Specifically, we know that
a vehicle that is moving north at 60 miles per hour and west at 10 miles
per hour is going to end up 60 miles north from its starting point and 10 miles west
after one hour of driving. After two hours, it will be 120 miles north
from the starting point and 20 miles to its west.</div></p><p><div class="SIntrapara">As always, a function that consumes a structure instance can (and probably
must) extract information from the structure to compute its result. So
once again we add selector expressions to the function 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="RktPn">(</span><span class="RktSym">ufo-move-1</span><span class="hspace">&nbsp;</span><span class="RktSym">u</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._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-loc</span><span class="hspace">&nbsp;</span><span class="RktSym">u</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-vel</span><span class="hspace">&nbsp;</span><span class="RktSym">u</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p><div class="SIntrapara"><span style="font-weight: bold">Note</span> The selector expressions raise the question
whether we need to refine this sketch even more. After all, the two
expressions extract instances of <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a> and <a href="part_one.html#%28tech._vel%29" class="techoutside" data-pltdoc="x"><span class="techinside">Vel</span></a>,
respectively. These two are also structure instances, and we could extract
values from them in turn. Here is what the resulting skeleton would look
like:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._ufo%29" class="techoutside" data-pltdoc="x"><span class="techinside">UFO</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._ufo%29" class="techoutside" data-pltdoc="x"><span class="techinside">UFO</span></a><span class="RktCmt"> </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">ufo-move-1</span><span class="hspace">&nbsp;</span><span class="RktSym">u</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._......%29%29" class="RktStxLink" 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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-loc</span><span class="hspace">&nbsp;</span><span class="RktSym">u</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" 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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-loc</span><span class="hspace">&nbsp;</span><span class="RktSym">u</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">vel-deltax</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-vel</span><span class="hspace">&nbsp;</span><span class="RktSym">u</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">vel-deltay</span><span class="hspace"
realistic programs, following this idea to its logical end would create
incredibly complex program outlines. More generally,
</div><div class="SIntrapara"><blockquote><p><span style="font-style: italic">If a function deals with nested structures, develop one function per
level of nesting.</span></p></blockquote></div><div class="SIntrapara">In the second part of the book, this <a name="(idx._(gentag._138))"></a>guideline becomes even more important
and we refine it a bit. <span style="font-weight: bold">End</span></div></p><p><div class="SIntrapara">Here we focus on how to combine the given <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a> and the given
<a href="part_one.html#%28tech._vel%29" class="techoutside" data-pltdoc="x"><span class="techinside">Vel</span></a> in order to obtain the next location of the <a href="part_one.html#%28tech._ufo%29" class="techoutside" data-pltdoc="x"><span class="techinside">UFO</span></a>&#8212;<wbr></wbr>because
that&rsquo;s what our physics knowledge tells us. Specifically, it
says to &ldquo;add&rdquo; the two together, where &ldquo;adding&rdquo; can&rsquo;t mean the
operation we usually apply to numbers. So let us imagine that we have a
function for adding a <a href="part_one.html#%28tech._vel%29" class="techoutside" data-pltdoc="x"><span class="techinside">Vel</span></a> to a <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>: <a name="(idx._(gentag._139))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._vel%29" class="techoutside" data-pltdoc="x"><span class="techinside">Vel</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds </span><span class="RktSym">v</span><span class="RktCmt"> to </span><span class="RktSym">p</span><span class="RktCmt"> </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">posn+</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Writing down the signature, purpose, and header like this is a legitimate
way of programming. It is called &ldquo;making a wish&rdquo; and is a part of
&ldquo;making a wish list&rdquo; as described in <a href="part_one.html#%28part._sec~3adesign%29" data-pltdoc="x">From Functions to Programs</a>.</div></p><p><div class="SIntrapara">The key is to make wishes in such a way that we can complete the function
that we are working on. In this manner, we can split difficult
programming tasks into different tasks, a technique that helps us solve
problems in reasonably small steps. For the sample problem, we get a
complete definition for <span class="RktSym">ufo-move-1</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">ufo-move-1</span><span class="hspace">&nbsp;</span><span class="RktSym">u</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-ufo</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">posn+</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-loc</span><span class="hspace">&nbsp;</span><span class="RktSym">u</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-vel</span><span class="hspace">&nbsp;</span><span class="RktSym">u</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;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-vel</span><span class="hspace">&nbsp;</span><span class="RktSym">u</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Because <span class="RktSym">ufo-move-1</span> and <span class="RktSym">posn+</span> are complete definitions,
we can even click <span class="emph">RUN</span>, which checks that DrRacket doesn&rsquo;t complain
about grammatical problems with our work so far. Naturally, the tests fail
because <span class="RktSym">posn+</span> is just a wish, not the function we need.</div></p><p><div class="SIntrapara">Now it is time to focus on <span class="RktSym">posn+</span>. We have completed the first two
steps of the design (data definitions,
signature/purpose/header),<span class="refelem"><span class="refcolumn"><span class="refcontent">In geometry, the operation
corresponding to <span class="RktSym">posn+</span> is called a <span style="font-style: italic">translation</span>.</span></span></span> so we
must create examples. One easy way to create functional examples for a
&ldquo;wish&rdquo; is to use the examples for the original function and to turn them
into examples for the new function:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">posn+</span><span class="hspace">&nbsp;</span><span class="RktSym">p1</span><span class="hspace">&nbsp;</span><span class="RktSym">v1</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">p2</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">posn+</span><span class="hspace">&nbsp;</span><span class="RktSym">p1</span><span class="hspace">&nbsp;</span><span class="RktSym">v2</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">17</span><span class="hspace">&nbsp;</span><span class="RktVal">77</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">For this problem, we know that <span class="RktPn">(</span><span class="RktSym">ufo-move-1</span><span class="stt"> </span><span class="RktPn">(</span><span class="RktSym">make-ufo</span><span class="stt"> </span><span class="RktSym">p1</span><span class="stt"> </span><span class="RktSym">v1</span><span class="RktPn">)</span><span class="RktPn">)</span> is to
produce <span class="RktSym">p2</span>. At the same time, we know that <span class="RktSym">ufo-move-1</span>
applies <span class="RktSym">posn+</span> to <span class="RktSym">p1</span> and <span class="RktSym">v1</span>, implying that
<span class="RktSym">posn+</span> must produce <span class="RktSym">p2</span> for these inputs. Stop! Check our
manual calculations to ensure that you are following what we are doing.</div></p><p><div class="SIntrapara">We are now able to add selector expressions to our design sketch:
</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">posn+</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._......%29%29" class="RktStxLink" 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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" 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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">vel-deltax</span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">vel-deltay</span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Because <span class="RktSym">posn+</span> consumes instances of <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a> and
<a href="part_one.html#%28tech._vel%29" class="techoutside" data-pltdoc="x"><span class="techinside">Vel</span></a> and because each piece of data is an instance of a
two-field structure, we get four expressions. In contrast to the nested
selector expressions from above, these are simple applications of a
selector to a parameter.</div></p><p><div class="SIntrapara">If we remind ourselves what these four expressions represent, or if we
recall how we computed the desired results from the two structures,
our completion of the definition of <span class="RktSym">posn+</span> is straightforward: <a name="(idx._(gentag._140))"></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">posn+</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">vel-deltax</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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;&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._%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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">vel-deltay</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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">The first step is to add the velocity in the horizontal direction to the
x-coordinate and the velocity in the vertical direction to the
y-coordinate. This yields two expressions, one per
new coordinate. With <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span> we can combine them
into a single <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a> again.</div></p><p><a name="(counter._(exercise._ex~3atry-it-out))"></a><span style="font-weight: bold">Exercise</span>&nbsp;75. Enter these definitions and their test cases
into the definitions area of DrRacket and make sure they work. This is the
first time that you have dealt with a &ldquo;wish,&rdquo; and you need to make sure you
understand how the two functions work together. <a href="part_one.html#%28counter._%28exercise._ex~3atry-it-out%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>5.7<tt>&nbsp;</tt><a name="(part._data-uni._sec~3adata-uni)"></a>The Universe of Data</h4><p>Every language comes with a universe of data. This data represents
information from and about the external world; it is what programs
manipulate. This universe of data is a collection that not only
contains<span class="refelem"><span class="refcolumn"><span class="refcontent">Remember that mathematicians call data collections
or data classes <span style="font-style: italic">sets</span>.</span></span></span> all built-in data but also any piece of data
that any program may ever create.</p><p>The left side of <a href="part_one.html#%28counter._data-uni._%28figure._fig~3auniverse%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">30</span></a> shows one way to imagine the
universe of BSL. Since there are infinitely many numbers and
strings, the collection of all data is infinite. We indicate &ldquo;infinity&rdquo;
in the figure with &ldquo;...&rdquo;, but a real definition would have to avoid
this imprecision.</p><p>Neither programs nor individual functions in programs deal with the entire
universe of data. It is the purpose of a data definition to describe parts
of this universe and to name these parts so that we can refer to them
concisely. Put differently, a named data definition is a description of a
collection of data, and that name is usable in other data definitions and
in function signatures. In a function signature, the name specifies what
data a function will deal with and, implicitly, which part of the universe
of data it won&rsquo;t deal with.</p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><blockquote class="SCentered"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_74.png" alt="image" width="206.0" height="296.0"/><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_75.png" alt="image" width="206.0" height="296.0"/></p></blockquote></blockquote></blockquote><p class="Centertext"><span class="Legend"><span class="FigureTarget"><a name="(counter._data-uni._(figure._fig~3auniverse))" x-target-lift="Figure"></a>Figure&nbsp;30: </span>The universe of data</span></p></blockquote><p><div class="SIntrapara">Practically, the data definitions of the first four chapters restrict built-in
collections of data. They do so via an explicit or implicit itemization of
all included values. For example, the region shaded with gray on
the right side in <a href="part_one.html#%28counter._data-uni._%28figure._fig~3auniverse%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">30</span></a> depicts the following data
definition:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._data-uni._b)"></a><span style="font-style: italic">BS</span><span class="RktCmt"> is one of: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </span><span class="RktVal">"hello"</span><span class="RktCmt">,</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </span><span class="RktVal">"world"</span><span class="RktCmt">, or</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </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="RktCmt">.</span></td></tr></table></blockquote></div><div class="SIntrapara">While this particular data definition looks silly,
note the stylized mix of English and BSL that is used. Its meaning is
precise and unambiguous, clarifying exactly which elements belong to
<a href="part_one.html#%28tech._data-uni._b%29" class="techoutside" data-pltdoc="x"><span class="techinside">BS</span></a> and which don&rsquo;t.</div></p><p>The definition of structure types completely revised the picture. When a
programmer defines a structure type, the universe expands with all
possible structure instances. For example, the addition of <span class="RktSym">posn</span>
means that instances of <span class="RktSym">posn</span> with all possible values in the two
fields appear. The middle bubble in <a href="part_one.html#%28counter._data-uni._%28figure._fig~3auni-struct%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">31</span></a> depicts
the addition of these values, including such seeming nonsense as
<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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><span class="RktVal">"hello"</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><span class="RktVal">0</span><span class="stt"> </span><span class="RktVal">1</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktVal">2</span><span class="RktPn">)</span>.
And yes, some of these instances of <span class="RktSym">posn</span> make no
sense to us. But, a BSL program may construct any of them.</p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><blockquote class="SCentered"><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_76.png" alt="image" width="726.0" height="316.0"/></p></blockquote></blockquote></blockquote><p class="Centertext"><span class="Legend"><span class="FigureTarget"><a name="(counter._data-uni._(figure._fig~3auni-struct))" x-target-lift="Figure"></a>Figure&nbsp;31: </span>Adding structure to a universe</span></p></blockquote><p><div class="SIntrapara">Adding yet another structure type definition mixes and matches
everything again. Say we add the definition for <span class="RktSym">ball</span>,
also with two fields. As the third bubble in <a href="part_one.html#%28counter._data-uni._%28figure._fig~3auni-struct%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">31</span></a>
shows, this addition creates instances of <span class="RktSym">ball</span> that contain
numbers, <span class="RktSym">posn</span> structures, and so on, as well as instances of <span class="RktSym">posn</span>
that contain instances of <span class="RktSym">ball</span>. Try it out in DrRacket! Add
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">ball</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">location</span><span class="hspace">&nbsp;</span><span class="RktSym">velocity</span><span class="RktPn">]</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">to the definitions area, hit <span class="emph">RUN</span>, and create some structure
instances.</div></p><p><div class="SIntrapara">As far as the pragmatics of data definitions is concerned, a data
definition for structure types describes large collections of data via
combinations of existing data definitions with instances. When we write
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-style: italic">Posn</span><span class="RktCmt"> is </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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">we are describing an infinite number of possible instances of
<span class="RktSym">posn</span>. Like above, the data definitions use combinations of
natural language, data collections defined elsewhere, and data
constructors. Nothing else should show up in a data definition at the
moment.</div></p><p><div class="SIntrapara">A data definition for structures specifies a new collection of data made up
of those instances to be used by our functions. For example, the data
definition for <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>s identifies the region shaded in gray
in the center bubble of the universe in <a href="part_one.html#%28counter._data-uni._%28figure._fig~3auni-struct%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">31</span></a>,
which includes all those <span class="RktSym">posn</span> structures whose two fields contain
numbers. At the same time, it is perfectly possible to construct an
instance of <span class="RktSym">posn</span> that doesn&rsquo;t satisfy the requirement that both
fields contain numbers:
</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</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><span class="hspace">&nbsp;</span><span class="RktVal">"hello"</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">This structure contains a <span class="RktSym">posn</span> in the <span class="RktSym">x</span> field and a string
in the <span class="RktSym">y</span> field.</div></p><p><div class="SIntrapara"><a name="(counter._data-uni._(exercise._struct7))"></a><span style="font-weight: bold">Exercise</span>&nbsp;76. Formulate data definitions for the following structure type definitions:
</div><div class="SIntrapara"><ul><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">movie</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">title</span><span class="stt"> </span><span class="RktSym">producer</span><span class="stt"> </span><span class="RktSym">year</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">person</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">name</span><span class="stt"> </span><span class="RktSym">hair</span><span class="stt"> </span><span class="RktSym">eyes</span><span class="stt"> </span><span class="RktSym">phone</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">pet</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">name</span><span class="stt"> </span><span class="RktSym">number</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">CD</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">artist</span><span class="stt"> </span><span class="RktSym">title</span><span class="stt"> </span><span class="RktSym">price</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">sweater</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">material</span><span class="stt"> </span><span class="RktSym">size</span><span class="stt"> </span><span class="RktSym">producer</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li></ul></div><div class="SIntrapara">Make sensible assumptions as to what kind of values go into each field. <a href="part_one.html#%28counter._data-uni._%28exercise._struct7%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><a name="(counter._data-uni._(exercise._ex~3atime-structure))"></a><span style="font-weight: bold">Exercise</span>&nbsp;77. Provide a structure type definition and a data
definition for representing points in time since midnight. A point in time
consists of three numbers: hours, minutes, and seconds. <a href="part_one.html#%28counter._data-uni._%28exercise._ex~3atime-structure%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._data-uni._(exercise._struct9))"></a><span style="font-weight: bold">Exercise</span>&nbsp;78. Provide a structure type and a data
definition for representing three-letter words. A word consists of
lowercase letters, represented with the <a href="part_one.html#%28tech._1string%29" class="techoutside" data-pltdoc="x"><span class="techinside">1String</span></a>s <span class="RktVal">"a"</span>
through <span class="RktVal">"z"</span> plus <span class="RktVal">#false</span>. <span style="font-weight: bold">Note</span> This exercise is a
part of the design of a hangman game; see <a href="part_four.html#%28counter._%28exercise._ex~3ahangman-list%29%29" data-pltdoc="x">exercise&nbsp;396</a>. <a href="part_one.html#%28counter._data-uni._%28exercise._struct9%29%29" class="ex-end" data-pltdoc="x"></a></p><p>Programmers not only write data definitions, they also read them in order
to understand programs, to expand the kind of data they can deal with, to
eliminate errors, and so on. We read a data definition to understand how
to create data that belongs to the designated collection and to determine
whether some piece of data belongs to some specified class.</p><p><div class="SIntrapara">Since <a name="(idx._data-uni._(gentag._141._data-uni))"></a>data definitions play such a central and important role in the design
process, it is often best to illustrate data definitions with <a name="(idx._data-uni._(gentag._142._data-uni))"></a>examples just
like we illustrate the behavior of functions with <a name="(idx._data-uni._(gentag._143._data-uni))"></a>examples. And indeed,
creating data examples from a data definition is straightforward:
</div><div class="SIntrapara"><ul><li><p>for a built-in collection of data (number, string, Boolean, images),
choose your favorite examples;</p><p><span style="font-weight: bold">Note</span> On occasion, people use descriptive names to qualify built-in data
collections, such as <span class="stt">NegativeNumber</span> or <span class="stt">OneLetterString</span>. They are
no replacement for a well-written data definition. <span style="font-weight: bold">End</span></p></li><li><p>for an enumeration, use several of the items of the enumeration;</p></li><li><p>for intervals, use the end points (if they are included) and at least
one interior point;</p></li><li><p>for itemizations, deal with each part separately; and</p></li><li><p>for data definitions for structures, follow the natural language
description; that is, use the constructor and pick an example from the data
collection named for each field.</p></li></ul></div><div class="SIntrapara">That&rsquo;s all there is to constructing examples from data definitions for
most of this book, though data definitions are going to become much more
complex than what you have seen so far.</div></p><p><div class="SIntrapara"><a name="(counter._data-uni._(exercise._struct10))"></a><span style="font-weight: bold">Exercise</span>&nbsp;79. Create examples for the following data definitions:
</div><div class="SIntrapara"><ul><li><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk" style="display: inline-table; vertical-align: text-top; margin-top: 0;"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._data-uni._color)"></a><span style="font-style: italic">Color</span><span class="RktCmt"> is one of: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </span><span class="RktVal">"white"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </span><span class="RktVal">"yellow"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </span><span class="RktVal">"orange"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </span><span class="RktVal">"green"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </span><span class="RktVal">"red"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </span><span class="RktVal">"blue"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&#8212;<wbr></wbr></span><span class="RktCmt"> </span><span class="RktVal">"black"</span></td></tr></table></blockquote><p><span style="font-weight: bold">Note</span> DrRacket recognizes many more strings as colors. <span style="font-weight: bold">End</span></p></li><li><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk" style="display: inline-table; vertical-align: text-top; margin-top: 0;"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a name="(tech._data-uni._h)"></a><span style="font-style: italic">H</span><span class="RktCmt"> is a </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> between </span><span class="RktVal">0</span><span class="RktCmt"> and </span><span class="RktVal">100</span><span class="RktCmt">.</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> represents a happiness value</span></td></tr></table></blockquote></li><li><p><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk" style="display: inline-table; vertical-align: text-top; margin-top: 0;"><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">person</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">fstname</span><span class="hspace">&nbsp;</span><span class="RktSym">lstname</span><span class="hspace">&nbsp;</span><span class="RktSym">male?</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._data-uni._person)"></a><span style="font-style: italic">Person</span><span class="RktCmt"> is a structure:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-person</span><span class="stt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-
structure type. The next chapter deals with such definitions in depth. <a href="part_one.html#%28counter._data-uni._%28exercise._struct10%29%29" class="ex-end" data-pltdoc="x"></a></div></p><h4>5.8<tt>&nbsp;</tt><a name="(part._sec~3adesignstructs)"></a>Designing with Structures</h4><p>The introduction of structure types reinforces the need for all six steps
in the <a name="(idx._(gentag._144))"></a>design recipe. It no longer suffices to rely on built-in data
collections to represent information; it is now clear that programmers
must create data definitions for all but the simplest problems.</p><p><div class="SIntrapara">This section adds a design recipe, illustrating it with the following:
</div><div class="SIntrapara"><blockquote><p><span style="font-weight: bold">Sample Problem</span> Design a function that computes the distance of objects in
a 3-dimensional space to the origin.</p></blockquote></div><div class="SIntrapara">Here we go:</div></p><p><div class="SIntrapara"><ol><li><p>When a problem calls for the representation of pieces of information
that belong together or describe a natural whole, you need a structure
type definition. It requires as many fields as there are relevant
properties. An instance of this structure type corresponds to the whole,
and the values in the fields correspond to its attributes.</p><p>A data definition for a structure type introduces a name for the collection
of instances that are legitimate. Furthermore, it must describe which kind
of data goes with which field. <span style="font-weight: bold">Use only names of built-in data
collections or previously defined data definitions.</span></p><p>In the end, we (and others) must be able to use the data definition to
create sample structure instances. Otherwise, something is wrong with our
data definition. To ensure that we can create instances, our data
definitions should come with <a name="(idx._(gentag._145))"></a><span style="font-weight: bold">data examples</span>.</p><p><div class="SIntrapara">Here is how we apply this idea to the sample problem:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">r3</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="hspace">&nbsp;</span><span class="RktSym">z</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">An </span><a name="(tech._r3)"></a><span style="font-style: italic">R3</span><span class="RktCmt"> is a structure:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-r3</span><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><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="RktSym">ex1</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-r3</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">13</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">ex2</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-r3</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="hspace">&nbsp;</span><span class="RktVal">3</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">The structure type definition introduces a new kind of structure,
<span class="RktSym">r3</span>, and the data definition introduces <a href="part_one.html#%28tech._r3%29" class="techoutside" data-pltdoc="x"><span class="techinside">R3</span></a> as the name for
all instances of <span class="RktSym">r3</span> that contain only numbers.</div></p></li><li><p>You still need a signature, a purpose statement, and a function
header but they remain the same. Stop! Do it for the sample problem.</p></li><li><p>Use the examples from the first step to create functional
examples. For each field associated with intervals or enumerations, make
sure to pick end points and intermediate points to create functional
examples. We expect you to continue working on the sample problem.</p></li><li><p>A function that consumes structures usually&#8212;<wbr></wbr>though not
always&#8212;<wbr></wbr>extracts the values from the various fields in the structure. To
remind yourself of this possibility, add a selector for each field to the
<a name="(idx._(gentag._146))"></a>templates for such functions.</p><p><div class="SIntrapara">Here is what we have for the sample problem:<a name="(idx._(gentag._147))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._r3%29" class="techoutside" data-pltdoc="x"><span class="techinside">R3</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">determines the distance of </span><span class="RktSym">p</span><span class="RktCmt"> to the origin </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">r3-distance-to-0</span><span class="hspace">&nbsp;</span><span class="RktSym">p</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._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">r3-x</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">r3-y</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">r3-z</span><span class="hspace">&nbsp;</span><span class="RktSym">p</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">You may want to write down next to each selector expression what kind of data it
extracts from the given structure; you can find this information in the data
definition. Stop! Just do it!</div></p></li><li><p>Use the selector expressions from the template when you define the
function. Keep in mind that you may not need some of them.</p></li><li><p>Test. Test as soon as the function header is written. Test until all
expressions have been covered. Test again when you make changes.</p></li></ol></div><div class="SIntrapara">Finish the sample problem. If you cannot remember the distance of a
3-dimensional point to the origin, look it up in a geometry
book.<span class="refelem"><span class="refcolumn"><span class="refcontent">There you will find a formula such as
<img src="pict_77.png" alt="image" width="79" height="15"/>.</span></span></span></div></p><p><div class="SIntrapara"><a name="(counter._(exercise._struct11))"></a><span style="font-weight: bold">Exercise</span>&nbsp;80. Create templates for functions that consume instances of the
following structure types:
</div><div class="SIntrapara"><ul><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">movie</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">title</span><span class="stt"> </span><span class="RktSym">director</span><span class="stt"> </span><span class="RktSym">year</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">pet</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">name</span><span class="stt"> </span><span class="RktSym">number</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">CD</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">artist</span><span class="stt"> </span><span class="RktSym">title</span><span class="stt"> </span><span class="RktSym">price</span><span class="RktPn">]</span><span class="RktPn">)</span></p></li><li><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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="stt"> </span><span class="RktSym">sweater</span><span class="stt"> </span><span class="RktPn">[</span><span class="RktSym">material</span><span class="stt"> </span><span class="RktSym">size</span><span class="stt"> </span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._color%29%29" class="RktValLink" data-pltdoc="x">color</a></span><span class="RktPn">]</span><span class="RktPn">)</span></p></li></ul></div><div class="SIntrapara">No, you do not need data definitions for this task. <a href="part_one.html#%28counter._%28exercise._struct11%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><a name="(counter._(exercise._struct11a))"></a><span style="font-weight: bold">Exercise</span>&nbsp;81. Design the function <span class="RktSym">time-&gt;seconds</span>, which
consumes instances of time structures (see <a href="part_one.html#%28counter._data-uni._%28exercise._ex~3atime-structure%29%29" data-pltdoc="x">exercise&nbsp;77</a>) and
produces the number of seconds that have passed since midnight. For
example, if you are representing 12 hours, 30 minutes, and 2 seconds with
one of these structures and if you then apply <span class="RktSym">time-&gt;seconds</span> to
this instance, the correct result is <span class="RktVal">45002</span>. <a href="part_one.html#%28counter._%28exercise._struct11a%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._struct11b))"></a><span style="font-weight: bold">Exercise</span>&nbsp;82. Design the function <span class="RktSym">compare-word</span>. The
function consumes two three-letter words (see <a href="part_one.html#%28counter._data-uni._%28exercise._struct9%29%29" data-pltdoc="x">exercise&nbsp;78</a>). It
produces a word that indicates where the given ones agree and
disagree. The function retains the content of the structure fields if the
two agree; otherwise it places <span class="RktVal">#false</span> in the field of the
resulting word. <span style="font-weight: bold">Hint</span> The exercises mentions two tasks: the
comparison of words and the comparison of &ldquo;letters.&rdquo; <a href="part_one.html#%28counter._%28exercise._struct11b%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>5.9<tt>&nbsp;</tt><a name="(part._sec~3aworld-structs)"></a>Structure in the World</h4><p>When a world program must track two independent pieces of information, we
must use a collection of structures to represent the world state data. One
field keeps track of one piece of information and the other field the
second piece of information. Naturally, if the domain world contains more
than two independent pieces of information, the structure type definition
must specify as many fields as there are distinct pieces of information.</p><p><div class="SIntrapara">Consider a space invader game that consists of a UFO and a tank. The UFO
descends along a straight vertical line and a tank moves horizontally
at the bottom of a scene. If both objects move at known constant speeds,
all that&rsquo;s needed to describe these two objects is one piece of
information per object: the y-coordinate for the UFO and the x-coordinate
for the tank. Putting those together requires a structure with two fields:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">space-game</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">ufo</span><span class="hspace">&nbsp;</span><span class="RktSym">tank</span><span class="RktPn">]</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">We leave it to you to formulate an adequate data definition for this
structure type definition, including an interpretation. Ponder the hyphen
in the name of the structure. BSL really allows the use of all kinds of
characters in the names of variables, functions, structures, and field
names. What are the selector names for this structure? The name of the
predicate?</div></p><p>Every time we say &ldquo;piece of information,&rdquo; we don&rsquo;t necessarily mean a
single number or a single word. A piece of information may itself combine
several pieces of information. Creating a <a name="(idx._(gentag._148))"></a>data representation for that
kind of information naturally leads to nested structures.</p><p><div class="SIntrapara">Let&rsquo;s add a modicum of spice to our imaginary space invader game. A
UFO that descends only along a vertical line is boring. To turn this idea
into an interesting game where the tank attacks the UFO with some weapon,
the UFO must be able to descend in nontrivial lines, perhaps jumping
randomly. An implementation of this idea means that we need two
coordinates to describe the location of the UFO, so that our revised
data definition for the space game becomes:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._spacegame)"></a><span style="font-style: italic">SpaceGame</span><span class="RktCmt"> is a structure:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-space-game</span><span class="stt"> </span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktPn">)</span><span class="RktCmt">. </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> </span><span class="RktPn">(</span><span class="RktSym">make-space-game</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><span class="RktSym">ux</span><span class="stt"> </span><span class="RktSym">uy</span><span class="RktPn">)</span><span class="stt"> </span><span class="RktSym">tx</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">describes a configuration where the UFO is </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">at (</span><span class="RktSym">ux</span><span class="RktCmt">,</span><span class="RktSym">uy</span><span class="RktCmt">) and the tank</span><span class="RktCmt">'</span><span class="RktCmt">s x-coordinate is </span><span class="RktSym">tx</span><span class="RktMeta"></span></td></tr></table></blockquote></div></p><p>Understanding what kind of <a name="(idx._(gentag._149))"></a>data representations are needed for world
programs takes practice. The following two sections introduce several
reasonably complex problem statements. Solve them before
moving on to the kind of games that you might like to design on your own.</p><h4>5.10<tt>&nbsp;</tt><a name="(part._sec~3aedit1)"></a>A Graphical Editor</h4><p>To program in BSL, you open DrRacket, type on the keyboard, and watch text
appear. Pressing the left arrow on the keyboard moves the cursor to the
left; pressing the backspace (or delete) key erases a single letter to the
left of the cursor&#8212;<wbr></wbr>if there is a letter to start with.</p><p>This process is called &ldquo;editing,&rdquo; though its precise name should be &ldquo;text
editing of programs&rdquo; because we will use &ldquo;editing&rdquo; for a more demanding
task than typing on a keyboard. When you write and revise other kinds of
documents, say, an English assignment, you are likely to use other
software applications, called word processors, though computer scientists
dub all of them editors or even graphical editors.</p><p>You are now in a position to design a world program that acts as a one-line
editor for plain text. Editing here includes entering letters and somehow
changing the already existing text, including the deletion and the
insertion of letters. This implies some notion of position within the
text. People call this position a <span style="font-style: italic">cursor</span>; most graphical
editors display it in such a way that it can easily be spotted.</p><p>Take a look at the following editor configuration:</p><blockquote class="SCentered"><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_78.png" alt="image" width="206.0" height="26.0"/></p></blockquote><p>Someone might have entered the text &ldquo;helloworld&rdquo; and hit the left
arrow key five times, causing the cursor to move from the end of the text
to the position between &ldquo;o&rdquo; and &ldquo;w.&rdquo; Pressing the space bar would now
cause the editor to change its display as follows:</p><p><div class="SIntrapara"><blockquote class="SCentered"><p><img style="vertical-align: -0.0px; margin: -3px -3px -3px -3px;" src="pict_79.png" alt="image" width="206.0" height="26.0"/></p></blockquote></div><div class="SIntrapara">In short, the action inserts &ldquo; &rdquo; and places the cursor between
it and &ldquo;w.&rdquo;</div></p><p><div class="SIntrapara">Given this much, an editor must track two pieces of information:
</div><div class="SIntrapara"><ol><li><p>the text entered so far, and</p></li><li><p>the current location of the cursor.</p></li></ol></div><div class="SIntrapara">And this suggests a structure type with two fields.</div></p><p><div class="SIntrapara">We can imagine several different ways of going from the information to data
and back. For example, one field in the structure may contain the entire
text entered, and the other the number of characters between the first
character (counting from the left) and the cursor. Another data
representation is to use two strings in the two fields: the part of the
text to the left of the cursor and the part of the text to its right. Here
is our preferred approach to representing the state of an editor:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">editor</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">pre</span><span class="hspace">&nbsp;</span><span class="RktSym">post</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">An </span><a name="(tech._editor)"></a><span style="font-style: italic">Editor</span><span class="RktCmt"> is a structure:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-editor</span><span class="stt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="stt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> </span><span class="RktPn">(</span><span class="RktSym">make-editor</span><span class="stt"> </span><span class="RktSym">s</span><span class="stt"> </span><span class="RktSym">t</span><span class="RktPn">)</span><span class="RktCmt"> describes an editor</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">whose visible text is </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="stt"> </span><span class="RktSym">s</span><span class="stt"> </span><span class="RktSym">t</span><span class="RktPn">)</span><span class="RktCmt"> with </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">the cursor displayed between </span><span class="RktSym">s</span><span class="RktCmt"> and </span><span class="RktSym">t</span></td></tr></table></blockquote></div><div class="SIntrapara">Solve the next few exercises based on this data representation.</div></p><p><a name="(counter._(exercise._struct-edit0))"></a><span style="font-weight: bold">Exercise</span>&nbsp;83. Design the function <span class="RktSym">render</span>, which
consumes an <a href="part_one.html#%28tech._editor%29" class="techoutside" data-pltdoc="x"><span class="techinside">Editor</span></a> and produces an image.</p><p>The purpose of the function is to render the text within an empty scene of
<img src="pict_80.png" alt="image" width="46" height="9"/> pixels. For the cursor, use a <img src="pict_81.png" alt="image" width="33" height="9"/> red rectangle and for the
strings, black text of size 16.</p><p><div class="SIntrapara">Develop the image for a sample string in DrRacket&rsquo;s interactions area. We
started with this expression:
</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._overlay%2Falign%29%29" class="RktValLink" data-pltdoc="x">overlay/align</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"left"</span><span class="hspace">&nbsp;</span><span class="RktVal">"center"</span></td></tr><tr><td><span class="hspace">&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._text%29%29" class="RktValLink" data-pltdoc="x">text</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"hello world"</span><span class="hspace">&nbsp;</span><span class="RktVal">11</span><span class="hspace">&nbsp;</span><span class="RktVal">"black"</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;&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">200</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">You may wish to read up on <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._beside%29%29" class="RktValLink" data-pltdoc="x">beside</a></span>, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._above%29%29" class="RktValLink" data-pltdoc="x">above</a></span>, and such
functions. When you are happy with the looks of the image, use the
expression as a test and as a guide to the design of <span class="RktSym">render</span>. <a href="part_one.html#%28counter._%28exercise._struct-edit0%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><a name="(counter._(exercise._struct-edit1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;84. Design <span class="RktSym">edit</span>. The function consumes two
inputs, an editor <span class="RktSym">ed</span> and a <a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a> <span class="RktSym">ke</span>, and it
produces another editor. Its task is to add a single-character
<a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a> <span class="RktSym">ke</span> to the end of the <span style="font-style: italic">pre</span> field of
<span class="RktSym">ed</span>, unless <span class="RktSym">ke</span> denotes the backspace (<span class="RktVal">"\b"</span>) key. In that
case, it deletes the character immediately to the left of the cursor (if
there are any). The function ignores the tab key (<span class="RktVal">"\t"</span>) and the
return key (<span class="RktVal">"\r"</span>).</p><p>The function pays attention to only two <a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a>s longer than one
letter: <span class="RktVal">"left"</span> and <span class="RktVal">"right"</span>. The left arrow moves the
cursor one character to the left (if any), and the right arrow moves it
one character to the right (if any). All other such <a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a>s are
ignored.</p><p>Develop a goodly number of examples for <span class="RktSym">edit</span>, paying attention to special
cases. When we solved this exercise, we created 20 examples and turned all
of them into tests.</p><p><span style="font-weight: bold">Hint</span> Think of this function as consuming <a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a>s, a collection
that is specified as an enumeration. It uses auxiliary functions to deal
with the <a href="part_one.html#%28tech._editor%29" class="techoutside" data-pltdoc="x"><span class="techinside">Editor</span></a> structure. Keep a wish list handy; you will need to
design additional functions for most of these auxiliary functions, such as
<span class="RktSym">string-first</span>, <span class="RktSym">string-rest</span>, <span class="RktSym">string-last</span>, and
<span class="RktSym">string-remove-last</span>. If you haven&rsquo;t done so, solve the exercises
in <a href="part_one.html#%28part._sec~3afuncs%29" data-pltdoc="x">Functions</a>. <a href="part_one.html#%28counter._%28exercise._struct-edit1%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._struct-edit2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;85. Define the function <span class="RktSym">run</span>. Given the
<span class="RktSym">pre</span> field of an editor, it launches an interactive editor, using
<span class="RktSym">render</span> and <span class="RktSym">edit</span> from the preceding two exercises for the
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._to-draw%29%29" class="RktStxLink" data-pltdoc="x">to-draw</a></span> and <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._on-key%29%29" class="RktStxLink" data-pltdoc="x">on-key</a></span> clauses, respectively. <a href="part_one.html#%28counter._%28exercise._struct-edit2%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._struct-edit3))"></a><span style="font-weight: bold">Exercise</span>&nbsp;86. Notice that if you type a lot, your editor
program does not display all of the text. Instead the text is cut off at
the right margin. Modify your function <span class="RktSym">edit</span> from
<a href="part_one.html#%28counter._%28exercise._struct-edit1%29%29" data-pltdoc="x">exercise&nbsp;84</a> so that it ignores a keystroke if adding it to the
end of the <span class="RktSym">pre</span> field would mean the rendered text is too wide for
your canvas. <a href="part_one.html#%28counter._%28exercise._struct-edit3%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._struct-edit4))"></a><span style="font-weight: bold">Exercise</span>&nbsp;87. Develop a data representation for an editor based
on our first idea, using a string and an index. Then solve the preceding
exercises again. <span style="font-weight: bold">Retrace the design recipe.</span> <span style="font-weight: bold">Hint</span> if you
haven&rsquo;t done so, solve the exercises in <a href="part_one.html#%28part._sec~3afuncs%29" data-pltdoc="x">Functions</a>.</p><p><span style="font-weight: bold">Note on Design Choices</span> The exercise is a first study of making
design choices. It shows that the very first design choice concerns the
data representation. Making the right choice requires planning ahead and
weighing the complexity of each. Of course, getting good at this is a
question of gaining experience. <a href="part_one.html#%28counter._%28exercise._struct-edit4%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>5.11<tt>&nbsp;</tt><a name="(part._sec~3azoo2)"></a>More Virtual Pets</h4><p>In this section we continue our virtual zoo project from
<a href="part_one.html#%28part._sec~3azoo1%29" data-pltdoc="x">Virtual Pet Worlds</a>. Specifically, the goal of the exercise is to combine
the cat world program with the program for managing its happiness
gauge. When the combined program runs, you see the cat walking across the
canvas, and, with each step, its happiness goes down. The only way to make
the cat happy is to feed it or pet it (up arrow). Finally,
the goal of the last exercise in this section is to create another virtual,
happy pet.</p><p><a name="(counter._(exercise._struct12))"></a><span style="font-weight: bold">Exercise</span>&nbsp;88. Define a structure type that keeps track of the cat&rsquo;s
x-coordinate and its happiness. Then formulate a data definition
for cats, dubbed <a name="(tech._vcat)"></a><span style="font-style: italic">VCat</span>, including an interpretation. <a href="part_one.html#%28counter._%28exercise._struct12%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._struct13))"></a><span style="font-weight: bold">Exercise</span>&nbsp;89. Design the <span class="RktSym">happy-cat</span> world program, which
manages a walking cat and its happiness level. Let&rsquo;s assume that the cat
starts out with perfect happiness.</p><p><span style="font-weight: bold">Hints</span> (1) Reuse the functions from the world programs in
<a href="part_one.html#%28part._sec~3azoo1%29" data-pltdoc="x">Virtual Pet Worlds</a>. (2) Use structure type from the preceding exercise to
represent the state of the world. <a href="part_one.html#%28counter._%28exercise._struct13%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._struct14))"></a><span style="font-weight: bold">Exercise</span>&nbsp;90. Modify the <span class="RktSym">happy-cat</span> program from the
preceding exercises so that it stops whenever the cat&rsquo;s happiness falls
to <span class="RktVal">0</span>. <a href="part_one.html#%28counter._%28exercise._struct14%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._struct15))"></a><span style="font-weight: bold">Exercise</span>&nbsp;91. Extend your structure type definition and data
definition from <a href="part_one.html#%28counter._%28exercise._struct12%29%29" data-pltdoc="x">exercise&nbsp;88</a> to include a direction field. Adjust your
<span class="RktSym">happy-cat</span> program so that the cat moves in the specified
direction. The program should move the cat in the current direction, and
it should turn the cat around when it reaches either end of the scene. <a href="part_one.html#%28counter._%28exercise._struct15%29%29" class="ex-end" data-pltdoc="x"></a></p><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">cham</span><span class="hspace">&nbsp;</span><img src="chameleon.png" alt="" width="63" height="150"/><span class="RktPn">)</span></p></blockquote><p>The above drawing of a chameleon is a <span style="font-weight: bold">transparent</span> image. To insert it
into DrRacket, insert it with the &ldquo;Insert Image&rdquo; menu item. Using this
instruction preserves the transparency of the drawing&rsquo;s pixels.</p><p>When a partly transparent image is combined with a colored shape, say a
rectangle, the image takes on the underlying color. In the chameleon
drawing, it is actually the inside of the animal that is transparent; the
area outside is solid white. Try out this expression in your DrRacket:</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/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">background</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._rectangle%29%29" class="RktValLink" data-pltdoc="x">rectangle</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><span class="RktSym">cham</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._image-height%29%29" class="RktValLink" data-pltdoc="x">image-height</a></span><span class="hspace">&nbsp;</span><span class="RktSym">cham</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">"solid"</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">"red"</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"><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="RktSym">cham</span><span class="hspace">&nbsp;</span><span class="RktSym">background</span><span class="RktPn">)</span></td></tr></table></blockquote><p><a name="(counter._(exercise._struct16))"></a><span style="font-weight: bold">Exercise</span>&nbsp;92. Design the <span class="RktSym">cham</span> program, which has the
chameleon continuously walking across the canvas from left to right. When
it reaches the right end of the canvas, it disappears and immediately
reappears on the left. Like the cat, the chameleon gets hungry from all
the walking, and, as time passes by, this hunger expresses itself as
unhappiness.</p><p>For managing the chameleon&rsquo;s happiness gauge, you may reuse the happiness
gauge from the virtual cat. To make the chameleon happy, you feed it (down
arrow, two points only); petting isn&rsquo;t allowed. Of course, like all
chameleons, ours can change color, too: <span class="RktVal">"r"</span> turns it red,
<span class="RktVal">"b"</span> blue, and <span class="RktVal">"g"</span> green. Add the chameleon world program
to the virtual cat game and reuse functions from the latter when
possible.</p><p>Start with a data definition, <a name="(tech._vcham)"></a><span style="font-style: italic">VCham</span>, for representing
chameleons. <a href="part_one.html#%28counter._%28exercise._struct16%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara"><a name="(counter._(exercise._struct16-copy))"></a><span style="font-weight: bold">Exercise</span>&nbsp;93. Copy your solution to <a href="part_one.html#%28counter._%28exercise._struct16%29%29" data-pltdoc="x">exercise&nbsp;92</a> and
modify the copy so that the chameleon walks across a tricolor
background. Our solution uses these colors:
</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">BACKGROUND</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._beside%29%29" class="RktValLink" data-pltdoc="x">beside</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._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="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="refelem"><span class="refcolumn"><span class="refcontent">Have some Italian pizza when you&rsquo;re done.</span></span></span></td></tr><tr><td><span class="hspace">&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="RktSym">WIDTH</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</span><span class="hspace">&nbsp;</span><span class="RktVal">"white"</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&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="RktSym">WIDTH</span><span class="hspace">&nbsp;</span><span class="RktSym">HEIGHT</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></blockquote></div><div class="SIntrapara">but you may use any colors. Observe how the chameleon changes
colors to blend in as it crosses the border between two colors.</div></p><p><span style="font-weight: bold">Note</span> When you watch the animation carefully, you see the
chameleon riding on a white rectangle. If you know how to use image
editing software, modify the picture so that the white rectangle is
invisible. Then the chameleon will really blend in. <a href="part_one.html#%28counter._%28exercise._struct16-copy%29%29" class="ex-end" data-pltdoc="x"></a></p><h3>6<tt>&nbsp;</tt><a name="(part._ch~3amix)"></a>Itemizations and Structures</h3><p>The preceding two chapters introduce two ways of formulating data
definitions. Those that employ itemization (enumeration and intervals) are
used to create small collections from large ones. Those that use
structures combine multiple collections. Since the development of data
representations is the starting point for proper program design, it cannot
surprise you that programmers frequently want to itemize data definitions
that involve structures or to use structures to combine itemized data.</p><p><div class="SIntrapara">Recall the imaginary space invader game from <a href="part_one.html#%28part._sec~3aworld-structs%29" data-pltdoc="x">Structure in the World</a> in
the preceding chapter. Thus far, it involves one UFO, descending from
space, and one tank on the ground, moving horizontally. Our
<a name="(idx._(gentag._150))"></a>data representation uses a structure with two fields: one
for the data representation of the UFO and another one for the data
representation of the tank. Naturally, players will want a tank that can
fire off a missile. All of a sudden, we can think of a second kind of
state that contains three independently moving objects: the UFO, the tank,
and the missile. Thus we have two distinct structures: one for
representing two independently moving objects and another one for the
third. Since a world state may now be one of these two structures, it is
natural to use an itemization to describe all possible states:
</div><div class="SIntrapara"><ol><li><p>the state of the world is a structure with <span style="font-weight: bold">two</span> fields, or</p></li><li><p>the state of the world is a structure with <span style="font-weight: bold">three</span> fields.</p></li></ol></div><div class="SIntrapara">As far as our domain is<span class="refelem"><span class="refcolumn"><span class="refcontent">No worries, the next part of the book
is about firing as many missiles as you want, without reloading.</span></span></span> concerned&#8212;<wbr></wbr>the
actual game&#8212;<wbr></wbr>the first kind of state represents the time
before the tank has launched its sole missile and the second one the time after the
missile has been fired.</div></p><p>This chapter introduces the basic idea of itemizing data definitions that
involve structures. Because we have all the other ingredients we need, we
start straight with itemizing structures. After that, we discuss some
examples, including world programs that benefit from our new power. The
last section is about errors in programming.</p><h4>6.1<tt>&nbsp;</tt><a name="(part._mix._sec~3aitemization-design2)"></a>Designing with Itemizations, Again</h4><p>Let&rsquo;s start with a refined problem statement for our space invader game
from <a href="part_one.html#%28part._sec~3aprogstructs%29" data-pltdoc="x">Programming with Structures</a>.</p><blockquote><p><span style="font-weight: bold">Sample Problem</span> Design a game program using <span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/universe</span></span> teachpack</span> for
playing a simple space invader game. The player is in control of a tank (a
small rectangle) that must defend our planet (the bottom of the canvas)
from a UFO (see <a href="part_one.html#%28part._sec~3aintervals%29" data-pltdoc="x">Intervals</a> for one possibility) that descends
from the top of the canvas to the bottom. In order to stop the UFO from
landing, the player may fire a single missile (a triangle smaller than the
tank) by hitting the space bar. In response, the missile emerges from the
tank. If the UFO collides with the missile, the player wins; otherwise the
UFO lands and the player loses.</p><p>Here are some details concerning the three game objects and their
movements. First, the tank moves a constant speed along the bottom of the
canvas, though the player may use the left arrow key and the right arrow
key to change directions. Second, the UFO descends at a constant velocity
but makes small random jumps to the left or right. Third, once fired, the
missile ascends along a straight vertical line at a constant speed at
least twice as fast as the UFO descends. Finally, the UFO and the missile
collide if their reference points are close enough, for whatever you think
&ldquo;close enough&rdquo; means.</p></blockquote><p>The following two subsections use this sample problem as a running example,
so study it well and solve the following exercise before you
continue. Doing so will help you understand the problem in enough
depth.</p><p><a name="(counter._mix._(exercise._mix1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;94. Draw some sketches of what the game scenery looks like at
various stages. Use the sketches to determine the constant and the
variable pieces of the game. For the former, develop physical and
graphical constants that describe the dimensions of the world (canvas) and
its objects. Also develop some background scenery. Finally, create your
initial scene from the constants for the tank, the UFO, and the
background. <a href="part_one.html#%28counter._mix._%28exercise._mix1%29%29" class="ex-end" data-pltdoc="x"></a></p><p><span style="font-weight: bold">Defining Itemizations</span> The first step in our design recipe calls for
the development of data definitions. One purpose of a data definition is to
describe the construction of data that represents the state of the world;
another is to describe all possible pieces of data that the event-handling
functions of the world program may consume. Since we haven&rsquo;t seen
itemizations that include structures, this first subsection introduces this
idea. While this probably won&rsquo;t surprise you, pay close attention.</p><p><span class="refelem"><span class="refcolumn"><span class="refcontent">For this space invader game, we could get away with one
structure type definition of three fields where the third field contains
<span class="RktVal">#false</span> until the missile is fired, and a <span class="RktSym">Posn</span> for the missile&rsquo;s
coordinates thereafter. See below.</span></span></span></p><p><div class="SIntrapara">As argued in the introduction to this chapter, the space invader game with
a missile-firing tank requires a <a name="(idx._mix._(gentag._151._mix))"></a>data representation for two different
kinds of game states. We choose two structure type definitions:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">aim</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">ufo</span><span class="hspace">&nbsp;</span><span class="RktSym">tank</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">fired</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">ufo</span><span class="hspace">&nbsp;</span><span class="RktSym">tank</span><span class="hspace">&nbsp;</span><span class="RktSym">missile</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">The first one is for the time period when the player is trying to get the
tank in position for a shot, and the second one is for representing states
after the missile is fired. Before we can formulate a data definition for
the complete game state, however, we need data representations for the
tank, the UFO, and the missile.</div></p><p><div class="SIntrapara">Assuming constant definitions for such physical constants as <span class="RktSym">WIDTH</span>
and <span class="RktSym">HEIGHT</span>, which are the subject of <a href="part_one.html#%28counter._mix._%28exercise._mix1%29%29" data-pltdoc="x">exercise&nbsp;94</a>, we formulate
the data definitions like this:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._mix._ufo)"></a><span style="font-style: italic">UFO</span><span class="RktCmt"> is a </span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="RktCmt">. </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> </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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><span class="RktSym">x</span><span class="stt"> </span><span class="RktSym">y</span><span class="RktPn">)</span><span class="RktCmt"> is the UFO</span><span class="RktCmt">'</span><span class="RktCmt">s location </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">(using the top-down, left-to-right convention)</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">tank</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">loc</span><span class="hspace">&nbsp;</span><span class="RktSym">vel</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._mix._tank)"></a><span style="font-style: italic">Tank</span><span class="RktCmt"> is a structure:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-tank</span><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="stt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktPn">)</span><span class="RktCmt">. </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> </span><span class="RktPn">(</span><span class="RktSym">make-tank</span><span class="stt"> </span><span class="RktSym">x</span><span class="stt"> </span><span class="RktSym">dx</span><span class="RktPn">)</span><span class="RktCmt"> specifies the position:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">(</span><span class="RktSym">x</span><span class="RktCmt">, </span><span class="RktSym">HEIGHT</span><span class="RktCmt">) and the tank</span><span class="RktCmt">'</span><span class="RktCmt">s speed: </span><span class="RktSym">dx</span><span class="RktCmt"> pixels/tick </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">A </span><a name="(tech._mix._missile)"></a><span style="font-style: italic">Missile</span><span class="RktCmt"> is a </span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="RktCmt">. </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> </span><span class="RktPn">(</span><span class="RktSym"><a href="https
newly defined one, <span class="RktSym">tank</span>, or a built-in data collection, <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>.
Concerning the latter, it may surprise you a little bit that <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>s
are used to represent two distinct aspects of the world. Then again, we
have used numbers (and strings and Boolean values) to represent many different
kinds of information in the real world, so reusing a collection of
structures such as <span class="RktSym">Posn</span> isn&rsquo;t a big deal.</div></p><p><div class="SIntrapara">Now we are in a position to formulate the data definitions for the state of the
space invader game:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._mix._sig)"></a><span style="font-style: italic">SIGS</span><span class="RktCmt"> is one of: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktPn">(</span><span class="RktSym">make-aim</span><span class="stt"> </span><a href="part_one.html#%28tech._mix._ufo%29" class="techoutside" data-pltdoc="x"><span class="techinside">UFO</span></a><span class="stt"> </span><a href="part_one.html#%28tech._mix._tank%29" class="techoutside" data-pltdoc="x"><span class="techinside">Tank</span></a><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktPn">(</span><span class="RktSym">make-fired</span><span class="stt"> </span><a href="part_one.html#%28tech._mix._ufo%29" class="techoutside" data-pltdoc="x"><span class="techinside">UFO</span></a><span class="stt"> </span><a href="part_one.html#%28tech._mix._tank%29" class="techoutside" data-pltdoc="x"><span class="techinside">Tank</span></a><span class="stt"> </span><a href="part_one.html#%28tech._mix._missile%29" class="techoutside" data-pltdoc="x"><span class="techinside">Missile</span></a><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> represents the complete state of a </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">space invader game</span></td></tr></table></blockquote></div><div class="SIntrapara">The shape of the data definition is that of an itemization. Each clause,
however, describes the content of a structure type, just like the data
definition for structure types we have seen so far. Still, this data
definition shows that not every data definition comes with exactly one
structure type definition; here one data definition involves two distinct structure
type definitions.</div></p><p><div class="SIntrapara">The meaning of such a data definition is also straightforward. It introduces the
name <a href="part_one.html#%28tech._mix._sig%29" class="techoutside" data-pltdoc="x"><span class="techinside">SIGS</span></a> for the collection of all those structure instances
that you can create according to the definition. So let us create some:
</div><div class="SIntrapara"><ul><li><p><div class="SIntrapara">Here is an instance that describes the tank maneuvering into position to
fire the missile:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><p><span class="RktPn">(</span><span class="RktSym">make-aim</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-tank</span><span class="hspace">&nbsp;</span><span class="RktVal">28</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></span><span class="RktPn">)</span><span class="RktPn">)</span></p></blockquote></div></p></li><li><p><div class="SIntrapara">This one is just like the previous one but the missile has been fired:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">make-fired</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">10</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">make-tank</span><span class="hspace">&nbsp;</span><span class="RktVal">28</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">28</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="RktSym">TANK-HEIGHT</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Of course, the capitalized names refer to the physical constants that you
defined.</div></p></li><li><p><div class="SIntrapara">Finally, here is one where the missile is about to collide with the UFO:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">make-fired</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">100</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">make-tank</span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">3</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">22</span><span class="hspace">&nbsp;</span><span class="RktVal">103</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">This example assumes that the canvas is more than 100 pixels tall.</div></p></li></ul></div><div class="SIntrapara">Notice that the first instance of <a href="part_one.html#%28tech._mix._sig%29" class="techoutside" data-pltdoc="x"><span class="techinside">SIGS</span></a> is generated
according to the first clause of the data definition, and the second and
third follow the second clause. Naturally the numbers in each field
depend on your choices for global game constants.</div></p><p><a name="(counter._mix._(exercise._mix2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;95. Explain why the three instances are generated according
to the first or second clause of the data definition. <a href="part_one.html#%28counter._mix._%28exercise._mix2%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._mix._(exercise._mix3))"></a><span style="font-weight: bold">Exercise</span>&nbsp;96. Sketch how each of the three game states could be
rendered assuming a <img src="pict_82.png" alt="image" width="52" height="9"/> canvas. <a href="part_one.html#%28counter._mix._%28exercise._mix3%29%29" class="ex-end" data-pltdoc="x"></a></p><p><span style="font-weight: bold">The Design Recipe</span> With a new way of formulating data definitions
comes an inspection of the design recipe. This chapter introduces a way to
combine two or more means of describing data, and the revised <a name="(idx._mix._(gentag._152._mix))"></a>design recipe
reflects this, especially the first step:</p><p><div class="SIntrapara"><ol><li><p>When do you need this new way of defining data? You already know that
the need for itemizations is due to distinctions among different classes
of information in the problem statement. Similarly, the need for
structure-based data definitions is due to the demand to group several
different pieces of information.</p><p>An itemization of different forms of data&#8212;<wbr></wbr>including collections of
structures&#8212;<wbr></wbr>is required when your problem statement distinguishes
different kinds of information and when at least some of these pieces of
information consist of several different pieces.</p><p>One thing to keep in mind is that data definitions may refer to other data
definitions. Hence, if a particular clause in a data definition looks
overly complex, it is acceptable to write down a separate data definition
for this clause and refer to this auxiliary definition.</p><p>And, as always, formulate <a name="(idx._mix._(gentag._153._mix))"></a>data examples using the <a name="(idx._mix._(gentag._154._mix))"></a>data definitions.</p></li><li><p>The second step remains the same. Formulate a function signature that
mentions only the names of defined or built-in data collections, add a
purpose statement, and create a function header.</p></li><li><p>Nothing changes for the third step. You still need to formulate
functional examples that illustrate the purpose statement from the second
step, and you still need one example per item in the itemization.</p></li><li><p>The development of the <a name="(idx._mix._(gentag._155._mix))"></a>template now exploits two different
dimensions: the itemization itself and the use of structures in its clauses.</p><p>By the first, the body of the template consists of 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 that has as many <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> clauses as the itemizations has
items. Furthermore, you must add a condition to each <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> clause
that identifies the sub-class of data in the corresponding item.</p><p>By the second, if an item deals with a structure, the template contains the
selector expressions&#8212;<wbr></wbr>in 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> clause that deals with the
sub-class of data described in the item.</p><p>When you choose to describe the data with a separate data definition,
however, you do <span style="font-weight: bold">not</span> add selector expressions. Instead, you create a
template for the separate data definition to the task at hand and refer to
that template with a function call. The latter indicates that this
sub-class of data is being processed separately.</p><p><span style="font-weight: bold">Before going through the work of developing a template</span>, briefly
reflect on the nature of the function. If the problem statement suggests
that there are several tasks to be performed, it is likely that a
composition of several, separately designed functions is needed instead of
a template. In that case, skip the template step.</p></li><li><p>Fill the gaps in the template. The more complex you make your data
definitions, the more complex this step becomes. The good news is that
this design recipe can help in many situations.</p><p>If you are stuck, fill the easy cases first and use default values for the
others. While this makes some of the test cases fail, you are making
progress and you can visualize this progress.</p><p>If you are stuck on some cases of the itemization, analyze the examples
that correspond to those cases. Determine what the pieces of the template
compute from the given inputs. Then consider how to combine these pieces
(plus some constants) to compute the desired output. Keep in mind
that you might need an auxiliary function.</p><p>Also, if your template &ldquo;calls&rdquo; another template because the data
definitions refer to each other, assume that the other function delivers
what its purpose statement and its examples promise&#8212;<wbr></wbr>even if this other
function&rsquo;s definition isn&rsquo;t finished yet.</p></li><li><p>Test. If tests fail, determine what&rsquo;s wrong: the function, the tests,
or both. Go back to the appropriate step.</p></li></ol></div><div class="SIntrapara">Go back to <a href="part_one.html#%28part._sec~3adesign-func%29" data-pltdoc="x">Designing Functions</a>, reread the description of the simple design
recipe, and compare it to this revision.</div></p><p>Let&rsquo;s illustrate the design recipe with the design of a rendering function
for the sample problem at the beginning of this section. Recall that a
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> expression needs such a rendering function to turn the
state of the world into an image after every clock tick, mouse click, or
keystroke.</p><p><div class="SIntrapara">The signature of this rendering function says that it maps an element of
the state-of-the-world class to the class of <a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a>s:<a name="(idx._mix._(gentag._156._mix))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._mix._sig%29" class="techoutside" data-pltdoc="x"><span class="techinside">SIGS</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds </span><span class="RktSym">TANK</span><span class="RktCmt">, </span><span class="RktSym">UFO</span><span class="RktCmt">, and possibly </span><span class="RktSym">MISSILE</span><span class="RktCmt"> to </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">the </span><span class="RktSym">BACKGROUND</span><span class="RktCmt"> scene</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">si-render</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKGROUND</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Here <span class="RktSym">TANK</span>, <span class="RktSym">UFO</span>, <span class="RktSym">MISSILE</span>, and <span class="RktSym">BACKGROUND</span>
are the requested image constants from <a href="part_one.html#%28counter._mix._%28exercise._mix1%29%29" data-pltdoc="x">exercise&nbsp;94</a>. Recall that this
signature is just an instance of the general signature for rendering
functions, which always consume the collections of world states and
produce some image.</div></p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><blockquote class="SCentered"><table cellspacing="0" cellpadding="0" style="border-collapse: collapse;"><tr><td style="border-bottom: 1px solid black;"><p>s</p></td><td style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td style="border-bottom: 1px solid black;"><p><span class="RktPn">(</span><span class="RktSym">si-render</span><span class="stt"> </span><span class="RktSym">s</span><span class="RktPn">)</span></p></td></tr><tr><td valign="bottom"><p></p></td><td valign="bottom"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="bottom"><p></p></td></tr><tr><td valign="bottom"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">make-aim</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-tank</span><span class="hspace">&nbsp;</span><span class="RktVal">28</span><span class="hspace">&nbsp;</span><span class="RktVal"><span class="nobreak">-3</span></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td><td valign="bottom"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="bottom"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_83.png" alt="image" width="86" height="76"/></p></td></tr><tr><td valign="bottom"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">make-fired</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">20</span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-tank</span><span class="hspace">&nbsp;</span><span class="RktVal">100</span><span class="hspace">&nbsp;</span><span class="RktVal">3</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">22</span><span class="hspace">&nbsp;</span><span class="RktVal">103</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td><td valign="bottom"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="bottom"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_84.png" alt="image" width="86" height="76"/></p></td></tr><tr><td valign="bottom"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">make-fired</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><spa
make three examples, using the data examples from above. See
<a href="part_one.html#%28counter._mix._%28figure._fig~3asigs-examples%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">32</span></a>. Unlike the function tables found in
mathematics books, this table is rendered vertically. The left column
contains sample inputs for our desired function; the right column lists
the corresponding desired results. As you can see, we used the data
examples from the first step of the design recipe, and they cover both
items of the itemization.</p><p><div class="SIntrapara">Next we turn to the development of the template, the most important step of
the <a name="(idx._mix._(gentag._157._mix))"></a>design process. First, we know that the body of <span class="RktSym">si-render</span>
must be 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 with two <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>
clauses. Following the design recipe, the two conditions are <span class="RktPn">(</span><span class="RktSym">aim?</span><span class="stt"> </span><span class="RktSym">s</span><span class="RktPn">)</span> and <span class="RktPn">(</span><span class="RktSym">fired?</span><span class="stt"> </span><span class="RktSym">s</span><span class="RktPn">)</span>, and they distinguish the two possible kinds
of data that <span class="RktSym">si-render</span> may consume: <a name="(idx._mix._(gentag._158._mix))"></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">si-render</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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">aim?</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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">fired?</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Second, we add selector expressions to every <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>
clause that deals with structures. In this case, both clauses
concern the processing of structures: <span class="RktSym">aim</span> and <span class="RktSym">fired</span>. The
former comes with two fields and thus requires two selector expressions
for the first <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> clause, and the latter kind of structures
consists of three values and thus demands three selector expressions:
</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">si-render</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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">aim?</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">aim-tank</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">aim-ufo</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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">fired?</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fired-tank</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fired-ufo</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fired-missile</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-plt
complete the definition, we figure out for each <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> line how to
combine the values we have in order to compute the expected result. Beyond the
pieces of the input, we may also use globally defined constants, for
example, <span class="RktSym">BACKGROUND</span>, which is obviously of help here; primitive
or built-in operations; and, if all else fails, wish-list functions, that
is, we describe functions we wish we had.</p><p><div class="SIntrapara">Consider the first <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> clause, where we have a <a name="(idx._mix._(gentag._159._mix))"></a>data
representation of a tank, that is, <span class="RktPn">(</span><span class="RktSym">aim-tank</span><span class="stt"> </span><span class="RktSym">s</span><span class="RktPn">)</span>, and the data
representation of a UFO, that is, <span class="RktPn">(</span><span class="RktSym">aim-ufo</span><span class="stt"> </span><span class="RktSym">s</span><span class="RktPn">)</span>. From the first
example in <a href="part_one.html#%28counter._mix._%28figure._fig~3asigs-examples%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">32</span></a>, we know that we need to add the two objects to the
background scenery. In addition, the design recipe suggests that if these
pieces of data come with their own data definition, we are to consider
defining helper (auxiliary) functions and to use those to compute the
result: <a name="(idx._mix._(gentag._160._mix))"></a> <a name="(idx._mix._(gentag._161._mix))"></a><a name="(idx._mix._(gentag._162._mix))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tank-render</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">aim-tank</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-render</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">aim-ufo</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">BACKGROUND</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Here <span class="RktSym">tank-render</span> and <span class="RktSym">ufo-render</span> are wish-list functions:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._mix._tank%29" class="techoutside" data-pltdoc="x"><span class="techinside">Tank</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds </span><span class="RktSym">t</span><span class="RktCmt"> to the given image </span><span class="RktSym">im</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">tank-render</span><span class="hspace">&nbsp;</span><span class="RktSym">t</span><span class="hspace">&nbsp;</span><span class="RktSym">im</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">im</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><a href="part_one.html#%28tech._mix._ufo%29" class="techoutside" data-pltdoc="x"><span class="techinside">UFO</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds </span><span class="RktSym">u</span><span class="RktCmt"> to the given image </span><span class="RktSym">im</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">ufo-render</span><span class="hspace">&nbsp;</span><span class="RktSym">u</span><span class="hspace">&nbsp;</span><span class="RktSym">im</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">im</span><span class="RktPn">)</span></td></tr></table></blockquote></div></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><a href="part_one.html#%28tech._mix._sig%29" class="techoutside" data-pltdoc="x"><span class="techinside">SIGS</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">renders the given game state on top of </span><span class="RktSym">BACKGROUND</span><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">for examples see </span><a href="part_one.html#%28counter._mix._%28figure._fig~3asigs-examples%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">32</span></a></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="
the same way. <a href="part_one.html#%28counter._mix._%28figure._fig~3asi-render%29%29" data-pltdoc="x">Figure&nbsp;<span class="FigureRef">33</span></a> shows the complete definition.
Best of all, we can immediately reuse our wish-list functions,
<span class="RktSym">tank-render</span> and <span class="RktSym">ufo-render</span>, and all we need to add is a
function for including a missile in the scene. The appropriate wish-list
entry is: <a name="(idx._mix._(gentag._163._mix))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._mix._missile%29" class="techoutside" data-pltdoc="x"><span class="techinside">Missile</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds </span><span class="RktSym">m</span><span class="RktCmt"> to the given image </span><span class="RktSym">im</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">missile-render</span><span class="hspace">&nbsp;</span><span class="RktSym">m</span><span class="hspace">&nbsp;</span><span class="RktSym">im</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">im</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">As above, the comment describes in sufficient detail what we want.</div></p><p><div class="SIntrapara"><a name="(counter._mix._(exercise._mix-ufo-render))"></a><span style="font-weight: bold">Exercise</span>&nbsp;97. Design the functions <span class="RktSym">tank-render</span>,
<span class="RktSym">ufo-render</span>, and <span class="RktSym">missile-render</span>. Compare this
expression:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">tank-render</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fired-tank</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-render</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fired-ufo</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">missile-render</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fired-missile</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktSym">BACKGROUND</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">with this one:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktPn">(</span><span class="RktSym">ufo-render</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fired-ufo</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tank-render</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fired-tank</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">missile-render</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">fired-missile</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktSym">BACKGROUND</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">When do the two expressions produce the same result? <a href="part_one.html#%28counter._mix._%28exercise._mix-ufo-render%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><a name="(counter._mix._(exercise._mix-ufo-stop))"></a><span style="font-weight: bold">Exercise</span>&nbsp;98. Design the function <span class="RktSym">si-game-over?</span> for use as the
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</a></span> handler. The game stops if the UFO lands or
if the missile hits the UFO. For both conditions, we recommend that you
check for proximity of one object to another.</p><p>The <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</a></span> clause allows for an optional second sub-expression,
namely a function that renders the final state of the game. Design
<span class="RktSym">si-render-final</span> and use it as the second part for your
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._stop-when%29%29" class="RktStxLink" data-pltdoc="x">stop-when</a></span> clause in the <span class="RktSym">main</span> function of
<a href="part_one.html#%28counter._mix._%28exercise._mix-ufo-fire%29%29" data-pltdoc="x">exercise&nbsp;100</a>. <a href="part_one.html#%28counter._mix._%28exercise._mix-ufo-stop%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._mix._(exercise._mix-ufo-move))"></a><span style="font-weight: bold">Exercise</span>&nbsp;99. Design <span class="RktSym">si-move</span>. This function
is called for every clock tick to determine to which position the objects
move now. Accordingly, it consumes an element of <a href="part_one.html#%28tech._mix._sig%29" class="techoutside" data-pltdoc="x"><span class="techinside">SIGS</span></a> and produces another
one.</p><p>Moving the tank and the missile (if any) is relatively
straightforward. They move in straight lines at a constant speed. Moving
the UFO calls for small random jumps to the left or the right. Since you
have never dealt with functions that create random numbers, the rest of this
exercise is a longish hint on how to deal with this issue.</p><p><div class="SIntrapara">BSL comes with a function that creates random numbers. Introducing this
function illustrates why the signatures and purpose statements play such an
important role during the design. Here is the relevant material for the
function you need: <a name="(idx._mix._(gentag._164._mix))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">produces a number in the interval [</span><span class="RktVal">0</span><span class="RktCmt">,</span><span class="RktSym">n</span><span class="RktCmt">),</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">possibly a different one each time it is called </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"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._random%29%29" class="RktValLink" data-pltdoc="x">random</a></span><span class="hspace">&nbsp;</span><span class="RktSym">n</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Since the signature and purpose statement precisely describe what a
function computes, you can now experiment with <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._random%29%29" class="RktValLink" data-pltdoc="x">random</a></span> in DrRacket&rsquo;s
interactions area. Stop! Do so!</div></p><p><div class="SIntrapara">If <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._random%29%29" class="RktValLink" data-pltdoc="x">random</a></span> produces different numbers (almost) every time it is
called, testing functions that use <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._random%29%29" class="RktValLink" data-pltdoc="x">random</a></span> is difficult. To start with,
separate <span class="RktSym">si-move</span> and its proper functionality into two
parts:<span class="refelem"><span class="refcolumn"><span class="refcontent">The idea that you must use <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._random%29%29" class="RktValLink" data-pltdoc="x">random</a></span> is BSL
knowledge, not a part of the design skills you must acquire, which is why
we provide this hint. Also, <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._random%29%29" class="RktValLink" data-pltdoc="x">random</a></span> is the first and only BSL
primitive that is not a mathematical function. Functions in programming are
inspired by mathematical functions, but they are not identical concepts.</span></span></span>
<a name="(idx._mix._(gentag._165._mix))"></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">si-move</span><span class="hspace">&nbsp;</span><span class="RktSym">w</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">si-move-proper</span><span class="hspace">&nbsp;</span><span class="RktSym">w</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._random%29%29" class="RktValLink" data-pltdoc="x">random</a></span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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><a href="part_one.html#%28tech._mix._sig%29" class="techoutside" data-pltdoc="x"><span class="techinside">SIGS</span></a><span class="RktCmt"> Number -&gt; </span><a href="part_one.html#%28tech._mix._sig%29" class="techoutside" data-pltdoc="x"><span class="techinside">SIGS</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">moves the space-invader objects predictably by </span><span class="RktSym">delta</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">si-move-proper</span><span class="hspace">&nbsp;</span><span class="RktSym">w</span><span class="hspace">&nbsp;</span><span class="RktSym">delta</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktSym">w</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">With this definition you separate the creation of a random number from the
act of moving the game objects. While <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._random%29%29" class="RktValLink" data-pltdoc="x">random</a></span> may produce
different results every time it is called, <span class="RktSym">si-move-proper</span> can be
tested on specific numeric inputs and is thus
guaranteed to return the same result when given the same inputs. In
short, most of the code remains testable.</div></p><p>Instead of calling <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._random%29%29" class="RktValLink" data-pltdoc="x">random</a></span> directly, you may wish to design a
function that creates a random x-coordinate for the UFO. Consider using
<span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._check-random%29%29" class="RktStxLink" data-pltdoc="x">check-random</a></span> from BSL&rsquo;s testing framework to test such a
function. <a href="part_one.html#%28counter._mix._%28exercise._mix-ufo-move%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara"><a name="(counter._mix._(exercise._mix-ufo-fire))"></a><span style="font-weight: bold">Exercise</span>&nbsp;100. Design the function <span class="RktSym">si-control</span>, which plays
the role of the key-event handler. As such, it consumes a game state and a
<a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a> and produces a new game state. It reacts to three different keys:
</div><div class="SIntrapara"><ul><li><p>pressing the left arrow ensures that the tank moves left;</p></li><li><p>pressing the right arrow ensures that the tank moves right; and</p></li><li><p>pressing the space bar fires the missile if it hasn&rsquo;t been launched yet.</p></li></ul></div></p><p>Once you have this function, you can define the <span class="RktSym">si-main</span> function,
which uses <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> to spawn the game-playing window. Enjoy! <a href="part_one.html#%28counter._mix._%28exercise._mix-ufo-fire%29%29" class="ex-end" data-pltdoc="x"></a>
<a name="(idx._mix._(gentag._166._mix))"></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><a href="part_one.html#%28tech._mix._sigs..v2%29" class="techoutside" data-pltdoc="x"><span class="techinside">SIGS.v2</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">renders the given game state on top of </span><span class="RktSym">BACKGROUND</span><span class="RktCmt"> </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">si-render.v2</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">tank-render</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">sigs-tank</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">ufo-render</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">sigs-ufo</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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;&nbsp;&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">missile-render.v2</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">sigs-missile</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktSym">BACKGROUND</span><span class="RktPn">)</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._mix._(figure._fig~3amix-ufo2))" x-target-lift="Figure"></a>Figure&nbsp;34: </span>Rendering game states again</span></p></blockquote><p><div class="SIntrapara">Data representations are rarely unique. For example, we could use a single
structure type to represent the states of a space invader game:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">sigs</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">ufo</span><span class="hspace">&nbsp;</span><span class="RktSym">tank</span><span class="hspace">&nbsp;</span><span class="RktSym">missile</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._mix._sigs..v2)"></a><span style="font-style: italic">SIGS.v2</span><span class="RktCmt"> (short for </span><span style="font-style: italic">SIGS version 2</span><span class="RktCmt">) is a structure:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-sigs</span><span class="stt"> </span><a href="part_one.html#%28tech._mix._ufo%29" class="techoutside" data-pltdoc="x"><span class="techinside">UFO</span></a><span class="stt"> </span><a href="part_one.html#%28tech._mix._tank%29" class="techoutside" data-pltdoc="x"><span class="techinside">Tank</span></a><span class="stt"> </span><a href="part_one.html#%28tech._mix._missileornot%29" class="techoutside" data-pltdoc="x"><span class="techinside">MissileOrNot</span></a><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> represents the complete state of a</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">space invader game</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">A </span><a name="(tech._mix._missileornot)"></a><span style="font-style: italic">MissileOrNot</span><span class="RktCmt"> is one of: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">#false</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktVal">#false</span><span class="RktCmt"> means the missile is in the tank;</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a><span class="RktCmt"> says the missile is at that location</span></td></tr></table></blockquote></div><div class="SIntrapara">Unlike the first <a name="(idx._mix._(gentag._167._mix))"></a>data representation for game states, this second version
does not distinguish between before and after the missile launch. Instead,
each state contains some data about the missile though this piece
of data may just be <span class="RktVal">#false</span>, indicating that the missile hasn&rsquo;t
been fired yet.</div></p><p>As a result, the functions for this second data representation of states
differ from the functions for the first one. In particular, functions that
consume an element of <a href="part_one.html#%28tech._mix._sigs..v2%29" class="techoutside" data-pltdoc="x"><span class="techinside">SIGS.v2</span></a> do not use 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
because there is only one kind of element in the collection. In terms of
design approach, the <a name="(idx._mix._(gentag._168._mix))"></a>design recipe for structures from
<a href="part_one.html#%28part._sec~3adesignstructs%29" data-pltdoc="x">Designing with Structures</a> suffices. <a href="part_one.html#%28counter._mix._%28figure._fig~3amix-ufo2%29%29" data-pltdoc="x">Figure&nbsp;<span class="FigureRef">34</span></a> shows the
result of designing the rendering function for this data representation.</p><p><div class="SIntrapara">In contrast, the design of functions using <a href="part_one.html#%28tech._mix._missileornot%29" class="techoutside" data-pltdoc="x"><span class="techinside">MissileOrNot</span></a> requires the recipe from this section. Let&rsquo;s look
at the design of <span class="RktSym">missile-render.v2</span>, whose job it is to add a
missile to an image. Here is the header material: <a name="(idx._mix._(gentag._169._mix))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._mix._missileornot%29" class="techoutside" data-pltdoc="x"><span class="techinside">MissileOrNot</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">adds an image of missile m to scene </span><span class="RktSym">s</span><span class="RktCmt"> </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">missile-render.v2</span><span class="hspace">&nbsp;</span><span class="RktSym">m</span><span class="hspace">&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktSym">s</span><span class="RktPn">)</span></td></tr></table></blockquote></div></p><p>As for examples, we must consider at least two cases: one when <span class="RktSym">m</span>
is <span class="RktVal">#false</span> and another one when <span class="RktSym">m</span> is a <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>. In the
first case, the missile hasn&rsquo;t been fired, which means that no
image of a missile is to be added to the given scene. In the second case,
the missile&rsquo;s position is specified and that is where the image of the
missile must show up. <a href="part_one.html#%28counter._mix._%28figure._fig~3amissile-render%29%29" data-pltdoc="x">Figure&nbsp;<span class="FigureRef">35</span></a> demonstrates the
workings of the function with two distinct scenarios.</p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><blockquote><table cellspacing="0" cellpadding="0" style="border-collapse: collapse;"><tr><td style="border-bottom: 1px solid black;"><p><span class="RktSym">m</span></p></td><td style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td style="border-bottom: 1px solid black;"><p><span class="RktPn">(</span><span class="RktSym">missile-render.v2</span><span class="stt"> </span><span class="RktSym">m</span><span class="stt"> </span><span class="RktSym">s</span><span class="RktPn">)</span></p></td></tr><tr><td valign="bottom"><p></p></td><td valign="bottom"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td valign="bottom"><p></p></td></tr><tr><td valign="bottom"><span class="RktVal">#false</span></td><td valign="bottom"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td valign="bottom"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_86.png" alt="image" width="86" height="76"/></p></td></tr><tr><td valign="bottom"><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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktVal">32</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></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktSym">TANK-HEIGHT</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td><td valign="bottom"><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p></td><td valign="bottom"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_87.png" alt="image" width="86" height="76"/></p></td></tr></table></blockquote></blockquote></blockquote><p class="Centertext"><span class="Legend"><span class="FigureTarget"><a name="(counter._mix._(figure._fig~3amissile-render))" x-target-lift="Figure"></a>Figure&nbsp;35: </span>Rendering the space invader games, with tanks</span></p></blockquote><p><a name="(counter._mix._(exercise._mix-ufo2-test))"></a><span style="font-weight: bold">Exercise</span>&nbsp;101. Turn the examples in <a href="part_one.html#%28counter._mix._%28figure._fig~3amissile-render%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">35</span></a> into test cases. <a href="part_one.html#%28counter._mix._%28exercise._mix-ufo2-test%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara">Now we are ready to develop the template. Because the data definition
for the major argument (<span class="RktSym">m</span>) is an itemization with two items, the
function body is likely to consist of 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 with two
clauses: <a name="(idx._mix._(gentag._170._mix))"></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">missile-render.v2</span><span class="hspace">&nbsp;</span><span class="RktSym">m</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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._boolean~3f%29%29" class="RktValLink" data-pltdoc="x">boolean?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">m</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">m</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Following the data definition again, the first <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> clause checks
whether <span class="RktSym">m</span> is a <a href="part_one.html#%28tech._boolean%29" class="techoutside" data-pltdoc="x"><span class="techinside">Boolean</span></a> value and the second one checks whether it
is an element of <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>. And, if someone were to accidentally
apply <span class="RktSym">missile-render.v2</span> to <span class="RktVal">#true</span> and to some image, the
function would use the first <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> clause to compute the
result. We have more to say on such errors below.</div></p><p><div class="SIntrapara">The second template step requests selector expressions in all those <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>
clauses that deal with structures. In our example, this is true for the
second clause, and the selector expressions extract the x- and
y-coordinates from the given <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>: <a name="(idx._mix._(gentag._171._mix))"></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">missile-render.v2</span><span class="hspace">&nbsp;</span><span class="RktSym">m</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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._boolean~3f%29%29" class="RktValLink" data-pltdoc="x">boolean?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">m</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">m</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#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" 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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">m</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" 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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktSym">m</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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">Compare this template with the one for <span class="RktSym">si-render</span> above. The data
definition for the latter deals with two distinct structure types, and
therefore the function template for <span class="RktSym">si-render</span> contains
selector expressions in both <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> clauses. The data definition
for <a href="part_one.html#%28tech._mix._missileornot%29" class="techoutside" data-pltdoc="x"><span class="techinside">MissileOrNot</span></a>, however, mixes items that are plain values with
items that describe structures. Both kinds of definitions are perfectly
fine; the key for you is to follow the recipe and to find a code
organization that matches the data definition.</div></p><p><div class="SIntrapara">Here is the complete function 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="RktPn">(</span><span class="RktSym">missile-render.v2</span><span class="hspace">&nbsp;</span><span class="RktSym">m</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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._boolean~3f%29%29" class="RktValLink" data-pltdoc="x">boolean?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">m</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">m</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">MISSILE</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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">m</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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktSym">m</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym">s</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">Doing this step-by-step, you first work on the easy clauses; in this
function that&rsquo;s the first one. Since it says the missile hasn&rsquo;t been
fired, the function returns the given <span class="RktSym">s</span>. For the second
clause, you need to remember that <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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="stt"> </span><span class="RktSym">m</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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="stt"> </span><span class="RktSym">m</span><span class="RktPn">)</span>
select the coordinates for the image of the missile. This function must
add <span class="RktSym">MISSILE</span> to <span class="RktSym">s</span>, so you have to figure out the best
combination of primitive operations and your own functions to combine the
four values. The choice of this combining operation is precisely where
your creative insight as a programmer comes into play.</div></p><p><a name="(counter._mix._(exercise._mix-ufo2-finish))"></a><span style="font-weight: bold">Exercise</span>&nbsp;102. Design all other functions that are needed to
complete the game for this second data definition. <a href="part_one.html#%28counter._mix._%28exercise._mix-ufo2-finish%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara"><a name="(counter._mix._(exercise._mix-zoo))"></a><span style="font-weight: bold">Exercise</span>&nbsp;103. Develop a data representation for the following four
kinds of zoo animals:
</div><div class="SIntrapara"><ul><li><p><span style="font-weight: bold">spiders</span>, whose relevant attributes are the number of remaining
legs (we assume that spiders can lose legs in accidents) and the space they
need in case of transport;</p></li><li><p><span style="font-weight: bold">elephants</span>, whose only attributes are the space they need in
case of transport;</p></li><li><p><span style="font-weight: bold">boa constrictors</span>, whose attributes include length and girth; and</p></li><li><p><span style="font-weight: bold">armadillos</span>, for which you must determine appropriate attributes,
including one that determines the space needed for transport.</p></li></ul></div><div class="SIntrapara">Develop a template for functions that consume zoo animals.</div></p><p>Design the <span class="RktSym">fits?</span> function, which consumes a zoo animal and a
description of a cage. It determines whether the cage&rsquo;s volume is large
enough for the animal. <a href="part_one.html#%28counter._mix._%28exercise._mix-zoo%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._mix._(exercise._mix-vehicle))"></a><span style="font-weight: bold">Exercise</span>&nbsp;104. Your home town manages a fleet of vehicles:
automobiles, vans, buses, and SUVs. Develop a data representation for
vehicles. The representation of each vehicle must describe the number of
passengers that it can carry, its license plate number, and its fuel
consumption (miles per gallon). Develop a template for functions that
consume vehicles. <a href="part_one.html#%28counter._mix._%28exercise._mix-vehicle%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara"><a name="(counter._mix._(exercise._mix-coordinates))"></a><span style="font-weight: bold">Exercise</span>&nbsp;105. Some program contains the following data definition:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._mix._coordinate)"></a><span style="font-style: italic">Coordinate</span><span class="RktCmt"> is one of: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> a </span><a href="part_one.html#%28tech._negativenumber%29" class="techoutside" data-pltdoc="x"><span class="techinside">NegativeNumber</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> on the y axis, distance from top</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> a </span><a href="part_one.html#%28tech._positivenumber%29" class="techoutside" data-pltdoc="x"><span class="techinside">PositiveNumber</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> on the x axis, distance from left</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> a </span><a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> an ordinary Cartesian point</span></td></tr></table></blockquote></div><div class="SIntrapara">Make up at least two data examples per clause in the data definition.
For each of the examples, explain its meaning with a sketch of a canvas. <a href="part_one.html#%28counter._mix._%28exercise._mix-coordinates%29%29" class="ex-end" data-pltdoc="x"></a></div></p><h4>6.2<tt>&nbsp;</tt><a name="(part._sec~3aworlds-mix)"></a>Mixing Up Worlds</h4><p>This section suggests several design problems for world program, starting
with simple extension exercises concerning our virtual pets.</p><p><a name="(counter._(exercise._zoo3))"></a><span style="font-weight: bold">Exercise</span>&nbsp;106. In <a href="part_one.html#%28part._sec~3azoo2%29" data-pltdoc="x">More Virtual Pets</a> we discussed the creation of virtual pets
that come with happiness gauges. One of the virtual pets is a cat; the
other one, a chameleon. Each program is dedicated to a single pet,
however.</p><p><div class="SIntrapara">Design the <span class="RktSym">cat-cham</span> world program. Given both a
location and an animal, it walks the latter across the canvas, starting
from the given location. Here is the chosen data representation for
animals:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._vanimal)"></a><span style="font-style: italic">VAnimal</span><span class="RktCmt"> is either</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> a </span><span style="font-style: italic">VCat</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> a </span><span style="font-style: italic">VCham</span></td></tr></table></blockquote></div><div class="SIntrapara">where <span style="font-style: italic">VCat</span> and <span style="font-style: italic">VCham</span> are your data definitions from
<a href="part_one.html#%28counter._%28exercise._struct12%29%29" data-pltdoc="x">exercises&nbsp;88</a>
<a href="part_one.html#%28counter._%28exercise._struct16%29%29" data-pltdoc="x">and&nbsp;92</a>.</div></p><p><div class="SIntrapara">Given that <a href="part_one.html#%28tech._vanimal%29" class="techoutside" data-pltdoc="x"><span class="techinside">VAnimal</span></a> is the collection of world states, you need to
design
</div><div class="SIntrapara"><ul><li><p>a rendering function from <a href="part_one.html#%28tech._vanimal%29" class="techoutside" data-pltdoc="x"><span class="techinside">VAnimal</span></a> to <a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a>;</p></li><li><p>a function for handling clock ticks, from <a href="part_one.html#%28tech._vanimal%29" class="techoutside" data-pltdoc="x"><span class="techinside">VAnimal</span></a> to <a href="part_one.html#%28tech._vanimal%29" class="techoutside" data-pltdoc="x"><span class="techinside">VAnimal</span></a>; and</p></li><li><p>a function for dealing with key events so that you can feed and
pet and colorize your animal&#8212;<wbr></wbr>as applicable.</p></li></ul></div><div class="SIntrapara">It remains impossible to change the color of a cat or to pet a chameleon. <a href="part_one.html#%28counter._%28exercise._zoo3%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><a name="(counter._(exercise._zoo4))"></a><span style="font-weight: bold">Exercise</span>&nbsp;107. Design the <span class="RktSym">cham-and-cat</span> program, which deals
with both a virtual cat <span style="font-weight: bold">and</span> a virtual chameleon. You need a data
definition for a &ldquo;zoo&rdquo; containing both animals and functions for dealing
with it.</p><p><div class="SIntrapara">The problem statement leaves open how keys manipulate the two animals. Here
are two possible interpretations:
</div><div class="SIntrapara"><ol><li><p>Each key event goes to both animals.</p></li><li><p>Each key event applies to only one of the two animals.</p><p>For this alternative, you need a <a name="(idx._(gentag._172))"></a>data representation that specifies a
<span style="font-style: italic">focus</span> animal, that is, the animal that can currently be
manipulated. To switch focus, have the key-handling function interpret
<span class="RktVal">"k"</span> for &ldquo;kitty&rdquo; and <span class="RktVal">"l"</span> for lizard. Once a player
hits <span class="RktVal">"k"</span>, the following keystrokes apply to the cat only&#8212;<wbr></wbr>until
the player hits <span class="RktVal">"l"</span>.</p></li></ol></div><div class="SIntrapara">Choose one of the alternatives and design the appropriate program. <a href="part_one.html#%28counter._%28exercise._zoo4%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><a name="(counter._(exercise._pedestrian))"></a><span style="font-weight: bold">Exercise</span>&nbsp;108. In its default state, a pedestrian crossing light
shows an orange person standing, on a black background. When it is time to
allow the pedestrian to cross the street, the light receives a signal and
switches to a green, walking person. This phase lasts for 10 seconds.
After that the light displays the digits <span class="RktVal">9</span>, <span class="RktVal">8</span>, ...,
<span class="RktVal">0</span> with odd numbers colored orange and even numbers colored
green. When the countdown reaches 0, the light switches back to its
default state.</p><p><div class="SIntrapara">Design a world program that implements such a pedestrian traffic
light. The light switches from its default state when you press the
space bar on your keyboard. All other transitions must be reactions to
clock ticks. You may wish to use the following images
</div><div class="SIntrapara"><blockquote class="SCentered"><p><img src="pedestrian_traffic_light_red.png" alt="" width="57" height="56"/> <img src="pedestrian_traffic_light_green.png" alt="" width="68" height="56"/></p></blockquote></div><div class="SIntrapara">or you can make up your own stick figures with the <span class="sroman">the <span class="Smaller"><span style="font-style: italic">2htdp/image</span></span> teachpack</span> teachpack. <a href="part_one.html#%28counter._%28exercise._pedestrian%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><a name="(counter._(exercise._ex~3afsm))"></a><span style="font-weight: bold">Exercise</span>&nbsp;109. Design a world program that recognizes a pattern in a
sequence of <a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a>s. Initially the program shows a 100 by 100
white rectangle. Once your program has encountered the first desired
letter, it displays a yellow rectangle of the same size. After
encountering the final letter, the color of the rectangle turns green. If
any &ldquo;bad&rdquo; key event occurs, the program displays a red rectangle.</p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><table cellspacing="0" cellpadding="0" style="border-collapse: collapse;"><tr><td style="border-bottom: 1px solid black;"><p>conventional</p></td><td style="border-bottom: 1px solid black;"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td style="border-bottom: 1px solid black;"><p>defined abbreviations</p></td></tr><tr><td valign="top"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a name="(tech._expectstosee..v1)"></a><span style="font-style: italic">ExpectsToSee.v1</span><span class="RktCmt"> is one of: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"start, expect an 'a'"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"expect 'b', 'c', or 'd'"</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"finished"</span><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktVal">"error, illegal key"</span><span class="RktCmt"> </span></td></tr></table></td><td valign="top"><p><span class="hspace">&nbsp;&nbsp;</span></p></td><td valign="top"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a name="(tech._expectstosee..v2)"></a><span style="font-style: italic">ExpectsToSee.v2</span><span class="RktCmt"> is one of:</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktSym">AA</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktSym">BB</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktSym">DD</span><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktSym">ER</span><span class="RktCmt"> </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="RktSym">AA</span><span class="hspace">&nbsp;</span><span class="RktVal">"start, ..."</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">BB</span><span class="hspace">&nbsp;</span><span class="RktVal">"expect ..."</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">DD</span><span class="hspace">&nbsp;</span><span class="RktVal">"finished"</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktPn">(</span><span class="RktSym"><a href="ht
followed by an arbitrarily long mix of <span class="RktVal">"b"</span> and <span class="RktVal">"c"</span>, and
ended by a <span class="RktVal">"d"</span>. Clearly, <span class="RktVal">"acbd"</span> is one example of an
acceptable string; two others are <span class="RktVal">"ad"</span> and
<span class="RktVal">"abcbbbcd"</span>. Of course, <span class="RktVal">"da"</span>, <span class="RktVal">"aa"</span>, or
<span class="RktVal">"d"</span> do not match.</p><blockquote class="Figure"><blockquote class="Centerfigure"><blockquote class="FigureInside"><blockquote class="SCentered"><p><img style="vertical-align: 0px; margin: -3px -3px -3px -3px;" src="pict_88.png" alt="image" width="606.0" height="256.0"/></p></blockquote></blockquote></blockquote><p class="Centertext"><span class="Legend"><span class="FigureTarget"><a name="(counter._(figure._fig~3afsm-a-b-or-c*-d))" x-target-lift="Figure"></a>Figure&nbsp;37: </span>A finite state machine as a diagram</span></p></blockquote><p><span style="font-weight: bold">Hint</span> Your solution implements a finite state machine (FSM), an idea
introduced in <a href="part_one.html#%28part._sec~3aworlds-more%29" data-pltdoc="x">Finite State Worlds</a> as one design principle behind
world programs. As the name says, an FSM program may be in one of a finite
number of states. The first state is called an <span style="font-style: italic">initial
state</span>. Each key event causes the machine to reconsider its current
state; it may transition to the same state or to another one. When your
program recognizes a proper sequence of key events, it transitions to a
<span style="font-style: italic">final state</span>.</p><p>For a sequence-recognition problem, states typically represent the letters
that the machine expects to see next; see <a href="part_one.html#%28counter._%28figure._fig~3afsm-dd%29%29" data-pltdoc="x">figure&nbsp;<span class="FigureRef">36</span></a> for a
data definition.<span class="refelem"><span class="refcolumn"><span class="refcontent">The data definition on the right uses the
naming technique introduced in <a href="part_one.html#%28counter._%28exercise._ex~3aalternative-data-design2%29%29" data-pltdoc="x">exercise&nbsp;61</a>.</span></span></span> Take
a look at the last state, which says an illegal input has been
encountered. <a href="part_one.html#%28counter._%28figure._fig~3afsm-a-b-or-c%2A-d%29%29" data-pltdoc="x">Figure&nbsp;<span class="FigureRef">37</span></a> shows how to think of these
states and their relationships in a diagrammatic manner. Each node
corresponds to one of the four finite states; each arrow specifies which
<a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a> causes the program to transition from one state to
another.</p><p><div class="SIntrapara"><span style="font-weight: bold">History</span> In the 1950s, Stephen C. Kleene, whom we would call a
computer scientist, invented <span style="font-style: italic">regular expressions</span> as a notation
for the problem of recognizing text patterns. For the above problem,
Kleene would write
</div><div class="SIntrapara"><blockquote class="SCentered"><p><span class="stt">a (b|c)* d</span></p></blockquote></div><div class="SIntrapara">which means <span class="stt">a</span> followed by <span class="stt">b</span> or <span class="stt">c</span> arbitrarily often until
<span class="stt">d</span> is encountered. <a href="part_one.html#%28counter._%28exercise._ex~3afsm%29%29" class="ex-end" data-pltdoc="x"></a></div></p><h4>6.3<tt>&nbsp;</tt><a name="(part._sec~3ainput-errors)"></a>Input Errors</h4><p>One central point of this chapter concerns the role of predicates. They are
critical when you must design functions that process mixes of data. Such
mixes come up naturally when your problem statement mentions many
different kinds of information, but they also come up when you hand your
functions and programs to others. After all, you know and respect your
data definitions and function signatures. You never know, however, what
your friends and colleagues do, and you<span class="refelem"><span class="refcolumn"><span class="refcontent">It is a form of
self-delusion to expect that we always respect our own function
signatures. Calling a function on the wrong kind of data happens to the
best of us. While many languages are like BSL and expect programmers to
check signatures on their own, others do so automatically at the cost of
some additional complexity.</span></span></span> especially don&rsquo;t know how someone without
knowledge of BSL and programming uses your programs. This section
therefore presents one way of protecting programs from inappropriate
inputs.</p><p><div class="SIntrapara">Let&rsquo;s demonstrate this point with a simple program, a function for
computing the area of a disk: <a name="(idx._(gentag._173))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">computes the area of a disk with radius </span><span class="RktSym">r</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">area-of-disk</span><span class="hspace">&nbsp;</span><span class="RktSym">r</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="RktVal">3.14</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">r</span><span class="hspace">&nbsp;</span><span class="RktSym">r</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Our friends may wish to use this function for their geometry
homework. Unfortunately, when our friends use this function, they may
accidentally apply it to a string rather than a number. When that happens,
the function stops the program execution with a mysterious error message:<a name="(idx._(gentag._174))"></a>
</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">area-of-disk</span><span class="hspace">&nbsp;</span><span class="RktVal">"my-disk"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">*:expects a number, given "my-disk"</span></p></td></tr></table></blockquote></div><div class="SIntrapara">With predicates, you can prevent this kind of cryptic error message and
signal an informative error of your own choice.</div></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><a name="(tech._any)"></a><span style="font-style: italic">Any</span><span class="RktCmt"> </span><span class="RktCmt">BSL</span><span class="RktCmt"> value is one of: </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><a href="part_one.html#%28tech._boolean%29" class="techoutside" data-pltdoc="x"><span class="techinside">Boolean</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><a href="part_one.html#%28tech._string%29" class="techoutside" data-pltdoc="x"><span class="techinside">String</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><a href="part_one.html#%28tech._image%29" class="techoutside" data-pltdoc="x"><span class="techinside">Image</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="stt"> </span><a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a><span class="stt"> </span><a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span>...</td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">&ndash;</span><span class="RktCmt"> </span><span class="RktPn">(</span><span class="RktSym">make-tank</span><span class="stt"> </span><a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a><span class="stt"> </span><a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span>...</td></tr></table></blockquote></blockquote><p class="Centertext"><span class="Legend"><span class="FigureTarget"><a name="(counter._(figure._fig~3absl-universe))" x-target-lift="Figure"></a>Figure&nbsp;38: </span>The universe of BSL data</span></p></blockquote><p>Specifically, we can define checked versions of our functions, when we wish
to hand them to our friends. Because our friends may not know much BSL,
we must expect that they
apply this <span style="font-style: italic">checked function</span> to arbitrary BSL values: numbers,
strings, images, <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>s, and so on. Although we
cannot anticipate which structure types will be defined in BSL, we know
the rough shape of the data definition for the collection of all BSL
values. <a href="part_one.html#%28counter._%28figure._fig~3absl-universe%29%29" data-pltdoc="x">Figure&nbsp;<span class="FigureRef">38</span></a> displays this shape of this data
definition. As discussed in <a href="part_one.html#%28part._data-uni._sec~3adata-uni%29" data-pltdoc="x">The Universe of Data</a>, the data definition for <a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a> is
open-ended because every structure type definition adds new
instances. These instances may contain <a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a> values again, which
implies that the data definition of <a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a> must
refer to itself&#8212;<wbr></wbr>a scary thought at first.</p><p><div class="SIntrapara">Based on this itemization, the template for a checked function has the
following rough shape: <a name="(idx._(gentag._175))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a><span class="RktCmt"> -&gt; ???</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">checked-f</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._number~3f%29%29" class="RktValLink" data-pltdoc="x">number?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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._boolean~3f%29%29" class="RktValLink" data-pltdoc="x">boolean?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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._string~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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._image~3f%29%29" class="RktValLink" data-pltdoc="x">image?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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-beginne
that&rsquo;s not necessary. What we do know is that for all those values in
the class of values for which the original function is defined, the
checked version must produce the same results; for all others, it must
signal an error.</div></p><p><div class="SIntrapara">Concretely, our sample function <span class="RktSym">checked-area-of-disk</span> consumes an
arbitrary BSL value and uses <span class="RktSym">area-of-disk</span> to compute the area
of a disk if the input is a number. It must stop with an error
message otherwise; in BSL we use the function <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._error%29%29" class="RktValLink" data-pltdoc="x">error</a></span> to accomplish
this. The error function consumes a string and stops the program:<a name="(idx._(gentag._176))"></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#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._error%29%29" class="RktValLink" data-pltdoc="x">error</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"area-of-disk: number expected"</span><span class="RktPn">)</span></p></blockquote></div><div class="SIntrapara">Hence the rough definition of <span class="RktSym">checked-area-of-disk</span> looks like
this:
</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">MESSAGE</span><span class="hspace">&nbsp;</span><span class="RktVal">"area-of-disk: number expected"</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">checked-area-of-disk</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._number~3f%29%29" class="RktValLink" data-pltdoc="x">number?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">area-of-disk</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._boolean~3f%29%29" class="RktValLink" data-pltdoc="x">boolean?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._error%29%29" class="RktValLink" data-pltdoc="x">error</a></span><span class="hspace">&nbsp;</span><span class="RktSym">MESSAGE</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._string~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._error%29%29" class="RktValLink" data-pltdoc="x">error</a></span><span class="hspace">&nbsp;</span><span class="RktSym">MESSAGE</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._image~3f%29%29" class="RktValLink" data-pltdoc="x">image?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hs
the natural way:<a name="(idx._(gentag._177))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">computes the area of a disk with radius </span><span class="RktSym">v</span><span class="RktCmt">, </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">if </span><span class="RktSym">v</span><span class="RktCmt"> is a number</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">checked-area-of-disk</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._number~3f%29%29" class="RktValLink" data-pltdoc="x">number?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">area-of-disk</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._else%29%29" class="RktStxLink" data-pltdoc="x">else</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._error%29%29" class="RktValLink" data-pltdoc="x">error</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"area-of-disk: number expected"</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 just to make sure we get what we want, let&rsquo;s experiment:
</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">checked-area-of-disk</span><span class="hspace">&nbsp;</span><span class="RktVal">"my-disk"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">area-of-disk:number expected</span></p></td></tr></table></blockquote></div></p><p>Writing checked functions is important if we distribute our programs for
others to use. Designing programs that work properly, however, is far more
important. This book focuses on the design process for proper program
design, and, to do this without distraction, we agree that we always adhere
to data definitions and signatures. At least, we almost always do so, and
on rare occasions we may ask you to design checked versions of a function
or a program.</p><p><a name="(counter._(exercise._ex~3achecked2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;110. A checked version of <span class="RktSym">area-of-disk</span> can
also enforce that the arguments to the function are positive numbers, not
just arbitrary numbers. Modify <span class="RktSym">checked-area-of-disk</span> in this way. <a href="part_one.html#%28counter._%28exercise._ex~3achecked2%29%29" class="ex-end" data-pltdoc="x"></a></p><p><div class="SIntrapara"><a name="(counter._(exercise._ex~3achecked3))"></a><span style="font-weight: bold">Exercise</span>&nbsp;111. Take a look at these definitions:
</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-struct%29%29" class="RktStxLink" data-pltdoc="x">define-struct</a></span><span class="hspace">&nbsp;</span><span class="RktSym">vec</span><span class="hspace">&nbsp;</span><span class="RktPn">[</span><span class="RktSym">x</span><span class="hspace">&nbsp;</span><span class="RktSym">y</span><span class="RktPn">]</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._vec)"></a><span style="font-style: italic">vec</span><span class="RktCmt"> is</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktPn">(</span><span class="RktSym">make-vec</span><span class="stt"> </span><span class="RktSym">PositiveNumber</span><span class="stt"> </span><span class="RktSym">PositiveNumber</span><span class="RktPn">)</span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span style="font-weight: bold">interpretation</span><span class="RktCmt"> represents a velocity vector</span></td></tr></table></blockquote></div><div class="SIntrapara">Develop the function <span class="RktSym">checked-make-vec</span>, which is to be understood
as a checked version of the primitive operation <span class="RktSym">make-vec</span>. It
ensures that the arguments to <span class="RktSym">make-vec</span> are positive numbers. In
other words, <span class="RktSym">checked-make-vec</span> enforces our informal data definition. <a href="part_one.html#%28counter._%28exercise._ex~3achecked3%29%29" class="ex-end" data-pltdoc="x"></a></div></p><p><div class="SIntrapara"><span style="font-weight: bold">Predicates</span>
You might wonder how you can design your own predicates. After all,
checked functions really seem to have this general shape:<a name="(idx._(gentag._178))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a><span class="RktCmt"> -&gt; </span>...</td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">checks that </span><span class="RktSym">a</span><span class="RktCmt"> is a proper input for function </span><span class="RktSym">g</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">checked-g</span><span class="hspace">&nbsp;</span><span class="RktSym">a</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">XYZ?</span><span class="hspace">&nbsp;</span><span class="RktSym">a</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">g</span><span class="hspace">&nbsp;</span><span class="RktSym">a</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="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._else%29%29" class="RktStxLink" data-pltdoc="x">else</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._error%29%29" class="RktValLink" data-pltdoc="x">error</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"g: bad input"</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">where <span class="RktSym">g</span> itself is defined like this:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._xyz%29" class="techoutside" data-pltdoc="x"><span class="techinside">XYZ</span></a><span class="RktCmt"> -&gt; </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">g</span><span class="hspace">&nbsp;</span><span class="RktSym">some-x</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">We assume that there is a data definition labeled <a name="(tech._xyz)"></a><span style="font-style: italic">XYZ</span>, and that
<span class="RktPn">(</span><span class="RktSym">XYZ?</span><span class="stt"> </span><span class="RktSym">a</span><span class="RktPn">)</span> produces <span class="RktVal">#true</span> when <span class="RktSym">a</span> is an
element of <a href="part_one.html#%28tech._xyz%29" class="techoutside" data-pltdoc="x"><span class="techinside">XYZ</span></a> and <span class="RktVal">#false</span> otherwise.</div></p><p>For <span class="RktSym">area-of-disk</span>, which consumes <a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a>s, <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~3f%29%29" class="RktValLink" data-pltdoc="x">number?</a></span>
is clearly the appropriate predicate. In contrast, for some functions
like <span class="RktSym">missile-render</span> from above, we clearly need to define our own
predicate because <a href="part_one.html#%28tech._mix._missileornot%29" class="techoutside" data-pltdoc="x"><span class="techinside">MissileOrNot</span></a> is a made-up, not a built-in, data collection. So let us design a predicate for
<a href="part_one.html#%28tech._mix._missileornot%29" class="techoutside" data-pltdoc="x"><span class="techinside">MissileOrNot</span></a>.</p><p><div class="SIntrapara">We recall the signature for predicates: <a name="(idx._(gentag._179))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._boolean%29" class="techoutside" data-pltdoc="x"><span class="techinside">Boolean</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">is </span><span class="RktSym">a</span><span class="RktCmt"> an element of the </span><a href="part_one.html#%28tech._mix._missileornot%29" class="techoutside" data-pltdoc="x"><span class="techinside">MissileOrNot</span></a><span class="RktCmt"> collection</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">missile-or-not?</span><span class="hspace">&nbsp;</span><span class="RktSym">a</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">It is a good practice to use questions as purpose statements for
predicates, because applying a predicate is like asking a question about a
value. The question mark &ldquo;?&rdquo; at the end of the name also reinforces this
idea; some people may tack on &ldquo;huh&rdquo; to pronounce the name of such functions.</div></p><p><div class="SIntrapara">Making up examples is also straightforward:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">missile-or-not?</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">missile-or-not?</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._make-posn%29%29" class="RktValLink" data-pltdoc="x">make-posn</a></span><span class="hspace">&nbsp;</span><span class="RktVal">9</span><span class="hspace">&nbsp;</span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">missile-or-not?</span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">The first two examples recall that every element of
<a href="part_one.html#%28tech._mix._missileornot%29" class="techoutside" data-pltdoc="x"><span class="techinside">MissileOrNot</span></a> is either <span class="RktVal">#false</span> or some <a href="part_one.html#%28tech._posn%29" class="techoutside" data-pltdoc="x"><span class="techinside">Posn</span></a>. The
third test says that strings aren&rsquo;t elements of the collection. Here are
three more tests:
</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">missile-or-not?</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">missile-or-not?</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">missile-or-not?</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._empty-image%29%29" class="RktValLink" data-pltdoc="x">empty-image</a></span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Explain the expected answers!</div></p><p>Since predicates consume all possible BSL values, their templates are just
like the templates for <span class="RktSym">checked-f</span>. Stop! Find the template and
take a second look before you read on.</p><p><div class="SIntrapara">As with checked functions, a predicate doesn&rsquo;t need all possible
<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> lines. Only those that might produce <span class="RktVal">#true</span> are
required:<a name="(idx._(gentag._180))"></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">missile-or-not?</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._boolean~3f%29%29" class="RktValLink" data-pltdoc="x">boolean?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</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#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" 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._posn-x%29%29" class="RktValLink" data-pltdoc="x">posn-x</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" 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._posn-y%29%29" class="RktValLink" data-pltdoc="x">posn-y</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">All other cases are summarized via an <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#
<span class="RktVal">#false</span>.</div></p><p><div class="SIntrapara">Given the template, the definition of <span class="RktSym">missile-or-not?</span> is a simple
matter of thinking through each case:
</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">missile-or-not?</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._boolean~3f%29%29" class="RktValLink" data-pltdoc="x">boolean?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._boolean~3d~3f%29%29" class="RktValLink" data-pltdoc="x">boolean=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&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._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Only <span class="RktVal">#false</span> is a legitimate <a href="part_one.html#%28tech._mix._missileornot%29" class="techoutside" data-pltdoc="x"><span class="techinside">MissileOrNot</span></a>; <span class="RktVal">#true</span> isn&rsquo;t. We express this idea with
<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._boolean~3d~3f%29%29" class="RktValLink" data-pltdoc="x">boolean=?</a></span><span class="stt"> </span><span class="RktVal">#false</span><span class="stt"> </span><span class="RktSym">v</span><span class="RktPn">)</span>, but <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._false~3f%29%29" class="RktValLink" data-pltdoc="x">false?</a></span><span class="stt"> </span><span class="RktSym">v</span><span class="RktPn">)</span> would also do:
</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">missile-or-not?</span><span class="hspace">&nbsp;</span><span class="RktSym">v</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._false~3f%29%29" class="RktValLink" data-pltdoc="x">false?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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._posn~3f%29%29" class="RktValLink" data-pltdoc="x">posn?</a></span><span class="hspace">&nbsp;</span><span class="RktSym">v</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&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._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><span class="hspace">&nbsp;</span><span class="RktVal">#false</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Naturally all elements of <span class="RktSym">Posn</span> are also members of
<a href="part_one.html#%28tech._mix._missileornot%29" class="techoutside" data-pltdoc="x"><span class="techinside">MissileOrNot</span></a>, which explains the
<span class="RktVal">#true</span> in the second line.</div></p><p><a name="(counter._(exercise._ex~3amissile-or-not-or))"></a><span style="font-weight: bold">Exercise</span>&nbsp;112. Reformulate the predicate now using an
<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> expression. <a href="part_one.html#%28counter._%28exercise._ex~3amissile-or-not-or%29%29" class="ex-end" data-pltdoc="x"></a></p><p><a name="(counter._(exercise._predicate1))"></a><span style="font-weight: bold">Exercise</span>&nbsp;113. Design predicates for the following data
definitions from the preceding section: <a href="part_one.html#%28tech._mix._sig%29" class="techoutside" data-pltdoc="x"><span class="techinside">SIGS</span></a>, <a href="part_one.html#%28tech._mix._coordinate%29" class="techoutside" data-pltdoc="x"><span class="techinside">Coordinate</span></a> (<a href="part_one.html#%28counter._mix._%28exercise._mix-coordinates%29%29" data-pltdoc="x">exercise&nbsp;105</a>), and <a href="part_one.html#%28tech._vanimal%29" class="techoutside" data-pltdoc="x"><span class="techinside">VAnimal</span></a>. <a href="part_one.html#%28counter._%28exercise._predicate1%29%29" class="ex-end" data-pltdoc="x"></a></p><p>To wrap up, let us mention <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key-event~3f%29%29" class="RktValLink" data-pltdoc="x">key-event?</a></span> and
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._mouse-event~3f%29%29" class="RktValLink" data-pltdoc="x">mouse-event?</a></span> as two important predicates that you may wish to use
in your world programs. They check the expected property, but you should
check out their documentation to make sure you understand what they
compute.</p><h4>6.4<tt>&nbsp;</tt><a name="(part._sec~3aworld-checking)"></a>Checking the World</h4><p>In a world program, many things can go wrong. Although we just agreed to
trust that our functions are always applied to the proper kind of data, in
a world program we may juggle too many things at once to place that much
trust in ourselves. When we design a world program that takes care of
clock ticks, mouse clicks, keystrokes, and rendering, it is just too easy
to get one of those interplays wrong. Of course, going wrong doesn&rsquo;t mean
that BSL recognizes the mistake immediately. For example, one of our
functions may produce a result that isn&rsquo;t quite an element of your data
representation for world states. At the same time, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>
accepts this piece of data and holds on to it, until the next event takes
place. It is only when the following event handler receives this inappropriate
piece of data that the program may fail. But it may get worse because
even the second and third and fourth event-handling step may actually cope
with inappropriate state values, and it all blows up much later in the
process.</p><p>To help with this kind of problem, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> comes with an
optional <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._check-with%29%29" class="RktStxLink" data-pltdoc="x">check-with</a></span> clause that accepts
a predicate for world states. If, for example, we chose to represent all world states
with <a href="part_one.html#%28tech._number%29" class="techoutside" data-pltdoc="x"><span class="techinside">Number</span></a>, we could express this fact easily like this:
<a name="(idx._(gentag._181))"></a> <a name="(idx._(gentag._182))"></a></p><p><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">main</span><span class="hspace">&nbsp;</span><span class="RktSym">s0</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktSym">s0</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" 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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._check-with%29%29" class="RktStxLink" data-pltdoc="x">check-with</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._number~3f%29%29" class="RktValLink" data-pltdoc="x">number?</a></span><span class="RktPn">]</span><span class="hspace">&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">As soon as any event-handling function produces something other than a
number, the world stops with an appropriate error message.</div></p><p><div class="SIntrapara">A <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._check-with%29%29" class="RktStxLink" data-pltdoc="x">check-with</a></span> clause is even more useful when the data definition
is not just a class of data with a built-in predicate 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._number~3f%29%29" class="RktValLink" data-pltdoc="x">number?</a></span>
but something subtle such as this interval definition:
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">A </span><a name="(tech._unitworld)"></a><span style="font-style: italic">UnitWorld</span><span class="RktCmt"> is a number </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="hspace">&nbsp;&nbsp;</span><span class="RktCmt">between </span><span class="RktVal">0</span><span class="RktCmt"> (inclusive) and </span><span class="RktVal">1</span><span class="RktCmt"> (exclusive). </span></td></tr></table></blockquote></div><div class="SIntrapara">In that case you want to formulate a predicate for this interval: <a name="(idx._(gentag._183))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._boolean%29" class="techoutside" data-pltdoc="x"><span class="techinside">Boolean</span></a><span class="RktCmt"> </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">is </span><span class="RktSym">x</span><span class="RktCmt"> between </span><span class="RktVal">0</span><span class="RktCmt"> (inclusive) and </span><span class="RktVal">1</span><span class="RktCmt"> (exclusive)</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">between-0-and-1?</span><span class="hspace">&nbsp;</span><span class="RktVal">"a"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">between-0-and-1?</span><span class="hspace">&nbsp;</span><span class="RktVal">1.2</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">between-0-and-1?</span><span class="hspace">&nbsp;</span><span class="RktVal">0.2</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">between-0-and-1?</span><span class="hspace">&nbsp;</span><span class="RktVal">0.0</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">between-0-and-1?</span><span class="hspace">&nbsp;</span><span class="RktVal">1.0</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</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">between-0-and-1?</span><span class="hspace">&nbsp;</span><span class="RktSym">x</span><span
world program:
</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">main</span><span class="hspace">&nbsp;</span><span class="RktSym">s0</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/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span><span class="hspace">&nbsp;</span><span class="RktSym">s0</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></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/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._check-with%29%29" class="RktStxLink" data-pltdoc="x">check-with</a></span><span class="hspace">&nbsp;</span><span class="RktSym">between-0-and-1?</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="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._......%29%29" class="RktStxLink" data-pltdoc="x">...</a></span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">If any of the world-producing handlers creates a number outside of the
interval, or worse, a non-numeric-value, our program discovers this
mistake immediately and gives us a chance to fix the mistake.</div></p><p><a name="(counter._(exercise._predicate2))"></a><span style="font-weight: bold">Exercise</span>&nbsp;114. Use the predicates from <a href="part_one.html#%28counter._%28exercise._predicate1%29%29" data-pltdoc="x">exercise&nbsp;113</a> to check
the space invader world program, the virtual pet program (<a href="part_one.html#%28counter._%28exercise._zoo3%29%29" data-pltdoc="x">exercise&nbsp;106</a>),
and the editor program (<a href="part_one.html#%28part._sec~3aedit1%29" data-pltdoc="x">A Graphical Editor</a>). <a href="part_one.html#%28counter._%28exercise._predicate2%29%29" class="ex-end" data-pltdoc="x"></a></p><h4>6.5<tt>&nbsp;</tt><a name="(part._sec~3aequality1)"></a>Equality Predicates</h4><p><div class="SIntrapara">An <span style="font-style: italic">equality predicate</span> is a function that compares two elements of
the same collection of data. Recall the definition of
<a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a>, which is the collection of three strings:
<span class="RktVal">"red"</span>, <span class="RktVal">"green"</span>, and <span class="RktVal">"yellow"</span>. Here is one way to
define the <span class="RktSym">light=?</span> function: <a name="(idx._(gentag._184))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._boolean%29" class="techoutside" data-pltdoc="x"><span class="techinside">Boolean</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">are the two (states of) traffic lights equal</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">light=?</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">light=?</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">light=?</span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">light=?</span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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">light=?</span><span class="hspace">&nbsp;</span><span class="RktSym">a-value</span><span class="hspace">&nbsp;</span><span class="RktSym">another-value</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
interactions reveal conflicts with our intentions:
</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">light=?</span><span class="hspace">&nbsp;</span><span class="RktVal">"salad"</span><span class="hspace">&nbsp;</span><span class="RktVal">"greens"</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">light=?</span><span class="hspace">&nbsp;</span><span class="RktVal">"beans"</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">string=?:expects a string, given 10</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Compare these interactions with other, built-in equality predicates:
</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._boolean~3d~3f%29%29" class="RktValLink" data-pltdoc="x">boolean=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"#true"</span><span class="hspace">&nbsp;</span><span class="RktVal">10</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktErr">boolean=?:expects a boolean as 1st argument, given "#true"</span></p></td></tr></table></blockquote></div><div class="SIntrapara">Try <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">10</span><span class="stt"> </span><span class="RktVal">#true</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._~3d%29%29" class="RktValLink" data-pltdoc="x">=</a></span><span class="stt"> </span><span class="RktVal">20</span><span class="stt"> </span><span class="RktVal">"help"</span><span class="RktPn">)</span> on your own.
All of them signal an error about being applied to the wrong kind of
argument.</div></p><p><div class="SIntrapara">A checked version of <span class="RktSym">light=?</span> enforces that both arguments belong to
<a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a>;<span class="refelem"><span class="refcolumn"><span class="refcontent">The case
of characters matters; <span class="RktVal">"red"</span> is different from
<span class="RktVal">"Red"</span> or <span class="RktVal">"RED"</span>.</span></span></span> if not, it signals an error like those that built-in
equality predicates issue. We call the predicate for
<span class="RktSym">TrafficLight</span> <span class="RktSym">light?</span> for brevity:<a name="(idx._(gentag._185))"></a>
</div><div class="SIntrapara"><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._boolean%29" class="techoutside" data-pltdoc="x"><span class="techinside">Boolean</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">is the given value an element of </span><a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a></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">light?</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._string~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></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#%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._string~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string=?</a></span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="hspace">&nbsp;</span><span class="RktSym">x</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;&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">"green"</span><span class="hspace">&nbsp;</span><span class="RktSym">x</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;&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">"yellow"</span><span class="hspace">&nbsp;</span><span class="RktSym">x</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="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28form._%28%28lib._lang%2Fhtdp-beginner..rkt%29._else%29%29" class="RktStxLink" data-pltdoc="x">else</a></span><
original analysis. First, the function determines that the two
inputs are elements of <a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a>; if not it 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._error%29%29" class="RktValLink" data-pltdoc="x">error</a></span>
to signal the mistake:
</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">MESSAGE</span></td></tr><tr><td><span class="hspace">&nbsp;&nbsp;</span><span class="RktVal">"traffic light expected, given some other value"</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><a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a><span class="RktCmt"> </span><a href="part_one.html#%28tech._any%29" class="techoutside" data-pltdoc="x"><span class="techinside">Any</span></a><span class="RktCmt"> -&gt; </span><a href="part_one.html#%28tech._boolean%29" class="techoutside" data-pltdoc="x"><span class="techinside">Boolean</span></a></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">are the two values elements of </span><a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a><span class="RktCmt"> and, </span></td></tr><tr><td><span class="RktCmt">;</span><span class="RktCmt">&nbsp;</span><span class="RktCmt">if so, are they equal</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">light=?</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">light=?</span><span class="hspace">&nbsp;</span><span class="RktVal">"red"</span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#false</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">light=?</span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="hspace">&nbsp;</span><span class="RktVal">"green"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</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._check-expect%29%29" class="RktStxLink" data-pltdoc="x">check-expect</a></span><span class="hspace">&nbsp;</span><span class="RktPn">(</span><span class="RktSym">light=?</span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</span><span class="hspace">&nbsp;</span><span class="RktVal">"yellow"</span><span class="RktPn">)</span><span class="hspace">&nbsp;</span><span class="RktVal">#true</span><span class="RktPn">)</span></td></tr><tr>
specifies which of the two arguments isn&rsquo;t an element of
<a href="part_one.html#%28tech._trafficlight%29" class="techoutside" data-pltdoc="x"><span class="techinside">TrafficLight</span></a>. <a href="part_one.html#%28counter._%28exercise._cond10%29%29" class="ex-end" data-pltdoc="x"></a></p><p>While it is unlikely that your programs will use <span class="RktSym">light=?</span>, they
ought to use <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key~3d~3f%29%29" class="RktValLink" data-pltdoc="x">key=?</a></span> and <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._mouse~3d~3f%29%29" class="RktValLink" data-pltdoc="x">mouse=?</a></span>, two equality predicates
that we briefly mentioned at the end of the last subsection. Naturally,
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key~3d~3f%29%29" class="RktValLink" data-pltdoc="x">key=?</a></span> is an operation for comparing two <a href="part_one.html#%28tech._keyevent%29" class="techoutside" data-pltdoc="x"><span class="techinside">KeyEvent</span></a>s;
similarly, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._mouse~3d~3f%29%29" class="RktValLink" data-pltdoc="x">mouse=?</a></span> compares two <a href="part_one.html#%28tech._mouseevt%29" class="techoutside" data-pltdoc="x"><span class="techinside">MouseEvt</span></a>s. While both
kinds of events are represented as strings, it is important to realize
that not all strings represent key events or mouse events.</p><p>We recommend using <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key~3d~3f%29%29" class="RktValLink" data-pltdoc="x">key=?</a></span> in key-event handlers and
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._mouse~3d~3f%29%29" class="RktValLink" data-pltdoc="x">mouse=?</a></span> in mouse-event handlers from now on. The use of
<span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key~3d~3f%29%29" class="RktValLink" data-pltdoc="x">key=?</a></span> in a key-event handler ensures that the function really
compares strings that represent key events and not arbitrary strings. As
soon as, say, the function is accidentally applied to <span class="RktVal">"hello\n world"</span>, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28def._world._%28%28lib._2htdp%2Funiverse..rkt%29._key~3d~3f%29%29" class="RktValLink" data-pltdoc="x">key=?</a></span> signals an error and thus informs us that
something is wrong.</p><h3>7<tt>&nbsp;</tt><a name="(part._ch~3asummary1)"></a>Summary</h3><p>In this first part of the book, you learned a bunch of simple but important
lessons. Here is a summary:</p><ol><li><p>A <span style="font-weight: bold">good programmer</span> designs programs. A bad programmer tinkers
until the program seems to work.</p></li><li><p>The <a name="(idx._(gentag._186))"></a><span style="font-weight: bold">design recipe</span> has two dimensions. One
concerns the process of design, that is, the sequence of steps to be
taken. The other explains how the chosen <a name="(idx._(gentag._187))"></a>data
representation influences the <a name="(idx._(gentag._188))"></a>design process.</p></li><li><p>Every well-designed program consists of many constant definitions,
structure type definitions, data definitions, and function definitions.
For <span style="font-weight: bold">batch programs</span>, one function is the &ldquo;main&rdquo; function, and it
typically composes several other functions to perform its computation. For
<span style="font-weight: bold">interactive programs</span>, the <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span> function plays the role
of the main function; it specifies the initial state of the program, an
image-producing output function, and at most three event handlers: one for
clock ticks, one for mouse clicks, and one for key events. In both kinds of
programs, function definitions are presented &ldquo;top down,&rdquo; starting with
the main function, followed by those functions mentioned in the main
function, and so on.</p></li><li><p>Like all programming languages, <span style="font-style: italic">Beginning Student Language</span>
comes with <span style="font-weight: bold">a vocabulary and a grammar</span>. Programmers must be able to
determine the <span style="font-weight: bold">meaning</span> of each sentence in a language so that they
can anticipate how the program performs its computation when given an
input. The following intermezzo explains this idea in detail.</p></li><li><p>Programming languages, including BSL, come with a rich set of
teachpacks so that programmers don&rsquo;t have to reinvent the wheel all the
time. A programmer should become comfortable with the functions that a
teachpack provides, especially their signatures and purpose
statements. Doing so simplifies life.</p></li><li><p>A programmer must get to know the &ldquo;tools&rdquo; that a chosen programming
language offers. These tools are either part of the language&#8212;<wbr></wbr>such as
<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> or <span class="RktSym"><a href="https://docs.racket-lang.org/htdp-langs/beginner.html#%28def._htdp-beginner._%28%28lib._lang%2Fhtdp-beginner..rkt%29._max%29%29" class="RktValLink" data-pltdoc="x">max</a></span>&#8212;<wbr></wbr>or they are &ldquo;imported&rdquo; from a teachpack. In
this spirit, make sure you understand the following terms: <span style="font-weight: bold">structure
type</span> definition, <span style="font-weight: bold">function</span> definition, <span style="font-weight: bold">constant</span> definition,
<span style="font-weight: bold">structure instance</span>, <span style="font-weight: bold">data definition</span>, <span class="RktSym"><a href="https://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29" class="RktStxLink" data-pltdoc="x">big-bang</a></span>, and
<span style="font-weight: bold">event-handling function</span>.</p></li></ol><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_prologue.html" title="backward to &quot;Prologue: How to Program&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="i1-2.html" title="forward to &quot;Intermezzo 1: Beginning Student Language&quot;" data-pltdoc="x">next &rarr;</a></span>&nbsp;</div></div></div><div id="contextindicator">&nbsp;</div><script data-cfasync="false" src="../../cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script></body></html>