
\documentclass[11pt,twoside]{article}

\usepackage{html}
\usepackage{palatino}
\usepackage{a210}
\usepackage{slatex}
\usepackage{epsf}
\usepackage{mztp}

\overfullrule=3pt


\externallabels{../Companion}{/net/www1/htdocs/CS/PLT/Teaching/Staging/HtDP/Companion/labels.pl}





%% BEGIN INLINED FILE: preamble.dl.tex
\newcounter{foops}

\begin{htmlonly}
\end{htmlonly}





\renewcommand{\topfraction}{1}
\renewcommand{\dbltopfraction}{1}
\renewcommand{\bottomfraction}{1}
\renewcommand{\textfraction}{0}

\renewcommand{\floatpagefraction}{.90} \renewcommand{\dblfloatpagefraction}{.99} 


\title{ How to Design Programs\\
 ~\\
 \mbox{\Large An Introduction to Programming and Computing} ~\\
 ~\\
 ~\\
 ~\\
 ~\\
 ~\\
 }

\author{Matthias Felleisen\\
 Robert Bruce\ Findler\\
 Matthew Flatt\\
 Shriram Krishnamurthi\\
 \ \\
 Rice University\\
 Houston, Texas}

\date{\today\\
 \ \\
 \ \\
 \ \\
 \ \\
 \ \\
 \ \\
 \ \\
 \ \\
 {\Huge {\sc DRAFT}}}


\pagestyle{headings}


\begin{htmlonly}
\end{htmlonly}




\begin{htmlonly} 

\end{htmlonly}












\begin{htmlonly} 



\end{htmlonly}



 \newcommand{\jam}[8]{\hbox{$#1$ $#2$ $#3$ $#4$ $#5$ $#6$ $#7$ $#8$}}  \newcommand{\jamIM}[2]{\hbox{$\mbox{$\pm d_7 \ldots d_2$}$ $#1$ $#2$}} 
%% BEGIN INLINED FILE: rs-slatex.dl.tex

\leftcodeskip=\parindent  \rightcodeskip=1pt 		   








\begin{htmlonly}
\end{htmlonly}


\newexercise{exercise}{Exercise}[subsection]









\begin{htmlonly}
\end{htmlonly}



\begin{htmlonly}
\end{htmlonly}




\begin{htmlonly}
\end{htmlonly}



\newcount\invcnt \invcnt=20









\newcount\invcnt \invcnt=25



\newdimen\numwidth \numwidth=\textwidth \advance\numwidth by -40pt








\begin{htmlonly} 

\end{htmlonly}

\begin{document}

\maketitle\thispagestyle{empty}\newpage


~\vfill
 ~\hspace{.5cm}
   {Copyright \copyright 2000, 1999 Massachusetts Institute of Technology}
~\vfill
\thispagestyle{empty}
\newpage

\begin{htmlonly}
\begin{rawhtml}
<blockquote>
<blockquote>
<blockquote>
  <font size=+3 color=red>
  This is a preliminary draft of the book. The final book will appear
  with MIT Press in 2001 and will move to its permanent Web site 
  <!-- at <a href="http://www.htdp.org">www.htdp.org</a> -->
  sometime in the fall of 2000. 
  </font>
</blockquote>
</blockquote>
</blockquote>
\end{rawhtml}
\end{htmlonly}


\pagenumbering{roman}
\setcounter{page}{3}
\renewcommand{\thesection}{\Alph{section}}

\tableofcontents \newpage

\listoffigures
\newpage

~\newpage

%% BEGIN INLINED FILE: curriculum0.dl.tex

\renewcommand{\thesection}{A}

\begin{htmlonly}
\end{htmlonly}


\part{Preface} \begin{flushright} \begin{minipage}{3.5in} {\it It goes against the grain of modern education to teach children to program. What fun is there in making plans, acquiring discipline in organizing thoughts, devoting attention to detail and learning to be self-critical? \\
[.35cm] } \rule{0pt}{10pt}\hfill---Alan Perlis, {\em Epigrams on Programming\/} \end{minipage} \end{flushright}

\thispagestyle{empty}

Many professions require some form of computer programming. Accountants
program spreadsheets and word processors; photographers program photo
editors; musicians program synthesizers; and professional programmers
instruct plain computers. Programming has become a required skill.

Yet programming is more than just a vocational skill. Indeed, {\em good programming\/} is a fun activity; a creative outlet; a way to express ideas
in a concrete and tangible form. And designing programs teaches a variety
of skills that are important in all kinds of professions: critical reading,
analytical thinking, creative synthesis, and attention to detail.

We therefore believe that the study of program design deserves the same
central role in general education as mathematics and English. Or, put more
succinctly,
\begin{quote} 
  {\bf everyone should learn how to design programs.}
\end{quote}
On one hand, program design teaches the same analytical skills as
mathematics. But, unlike mathematics, working with programs is an active
approach to learning. Interacting with software provides immediate feedback
and thus leads to exploration, experimentation, and
self-evaluation. Furthermore, designing programs produces useful and fun
things, which vastly increases the sense of accomplishment when compared to
drill exercises in mathematics. On the other hand, program design teaches
the same analytical reading and writing skills as English.  Even the
smallest programming tasks are formulated as word problems. Without
critical reading skills, a student cannot design programs that match the
specification. Conversely, good program design methods force a student to
articulate thoughts about programs in proper English.

\hrule 




\begin{htmlonly}
\end{htmlonly}


 \begin{rawhtml} <table align=center bgcolor=lightgreen>  <tr><th align=center><font size=+1>  The Design Recipe for Functions </th></tr>  <tr><td bgcolor=beige><font size=+1 color=red>  Problem Analysis & Data Definition  </font></tr></td>  <tr><td bgcolor=beige><font size=+1 color=red>  Contract, Purpose & Effect Statements, Header  </font></tr></td>  <tr><td bgcolor=beige><font size=+1 color=red>  Examples </font></tr></td>  <tr><td bgcolor=beige><font size=+1 color=red>  Functiona Template </font></tr></td>  <tr><td bgcolor=beige><font size=+1 color=red>  Function Definition  </font></tr></td>  <tr><td bgcolor=beige><font size=+1 color=red>  Tests  </font></tr></td> </table> \end{rawhtml}


\center{Figure: The basic steps of a program design recipe}
\label{fig:the-design-recipe}
\hrule

This book is the first book on programming as the core subject of a liberal
arts education. Its main focus is {\em the design process\/} that leads
from problem statements to well-organized solutions; it deemphasizes the
study of programming language details, algorithmic minutiae, and specific
application domains.  Our desire to focus on the design process required
two radical innovations for introductory courses.  The first innovation is
a set of {\em explicit design guidelines\/}. Existing curricula tend to
provide vague and ill-defined suggestions, such as ``design from top to
bottom'' or ``make the program structural.'' We have instead developed
design guidelines that lead students from a problem statement to a
computational solution in step-by-step fashion with well-defined
intermediate products. In the process they learn to read, to analyze, to
organize, to experiment, to think in a systematic manner.  The second
innovation is a radically new programming environment.  In the past, texts
on programming ignored the role of the programming environment in the
learning process; they simply assumed that students had access to a
professional environment. This book provides a {\em programming environment for beginners\/}. It also grows with the students as they master more and
more of the material until it supports a full-fledged language for the
whole spectrum of programming tasks: large-scale programming as well as
scripting.

Our guidelines are formulated as a number of {\em program design recipes\/}.\footnote{Readers whose experience is exclusively based on programming languages such as C/C++, Basic, and Pascal should read ``procedure'' or ``method'' where the preface mentions ``program.''} A
design recipe guides a beginning programmer through the entire
problem-solving process. With design recipes, a beginner almost never again
stares at a blank piece of paper or a blank computer screen. Instead, the
student will check the design recipe and use the question-and-answer
guidelines to make some progress.

\htmladdimg{../icons/teacher.gif}~{\em Students should have a copy of figure~\ref{fig:the-design-recipe} handy at all times. Have them cut it out or make a copy. They should carry the copy with them at all times. When they program, they should pull out their copy and place it near the monitor. By the time midterms come around, students should know the recipe steps by heart. -- The complete set of steps will only make sense after section~\ref{sec:design3}  has been covered, but that doesn't make any difference. Train them as early as possible.}

We created the design recipes by identifying categories of problems. The
identification of a problem category is based on the classes of data that
are used to represent the relevant information. Starting from the structure
of this class description students derive the programs with a checklist.
Figure~\ref{fig:the-design-recipe} shows the basic six steps of a design
recipe checklist. Each step produces a well-defined intermediate product:
\begin{enumerate}
\item the description of the class of problem data; 
\item the informal specification of a program's behavior; 
\item the illustration of the behavior with examples; 
\item the development of a program template or lay-out; 
\item the transformation of the template into a complete definition; and 
\item the discovery of errors through testing. 
\end{enumerate}
The major difference concerns the relationship of steps~1 and~4.

Design recipes help beginners and teachers alike. Teachers can use the
recipes to inspect a beginner's problem-solving skills, to diagnose
weaknesses, and to suggest specific remedial steps. After all, each stage
of the design recipe yields a well-defined, checkable product.  If a
beginner is stuck, a teacher can inspect the intermediate products and
determine what the problem is. Based on this analysis, the teacher can then
provide guidance for a specific step in the recipe, raise appropriate
questions, and recommend additional practice exercises.




\subsection{Why Everyone Should Learn to Program} \begin{flushright} \begin{minipage}{3.5in} {\it  And as imagination bodies forth\\
 The forms of things to unknown, and the poet's pen\\
 Turns them to shapes, and gives to airy nothing\\
 A local habitation and a name.\\
[.35cm] } \rule{0pt}{10pt}\hfill---Shakespeare, {\em A Midsummer Night's Dream\rm (v(i))} \end{minipage} \end{flushright}

Our claim that everyone programs or should learn to program might appear
strange considering that, at first glance, fewer and fewer people seem to
program these days. Instead, the majority of people use application
packages, which are typically tailored to a particular task. Even
programmers in certain sub-disciplines are nowadays using ``program
generators,'' packages that create programs from, say, business rules or
law cases. So why should anyone learn to program?

The answer consists of two parts. First, it is indeed true that {\em traditional forms of programming\/} are useful for just a few people. But,
programming {\em as we the authors understand it\/} is useful for everyone:
the administrative secretary who uses spreadsheets as well as the high-tech
programmer. In other words, we have a broader notion of programming in mind
than the traditional one. We explain our notion in a moment. Second, we
teach our idea of programming with a technology that is based on the
principle of minimal intrusion. Hence, our notion of programming teaches
problem-analysis and problem-solving skills {\em without\/} imposing the
overhead of traditional programming notations and tools.

To get a better understanding of modern programming, take a closer look at
spreadsheets, one of today's popular application packages. A user enters
formulas into a spreadsheet. The formulas describe how a cell $A$ depends
on another cell $B$. Then, as the user enters a number into $B$, the
spreadsheet automatically calculates the contents of cell $A$.  For
complicated spreadsheets, a cell may depend on many other cells, not just
one.

Other application packages require similar activities. Consider word
processors and style sheets. A style sheet specifies how to create a (part
of a) document from yet-to-be-determined words or sentences. When someone
provides specific words and a style sheet, the word processor creates the
document by replacing names in the style sheet with specific
words. Similarly, someone who conducts a Web search may wish to specify
what words to look for, what words should be next to each other, and what
words should not occur in the page. In this case, the output depends on the
search engine's cache of Web pages and the user's search expression.

Finally, using a program generator in many ways relies on the same skills
as those necessary for application packages. A program generator creates a
program in a traditional programming language, such as C++ or Java, from
high-level descriptions, such as business rules or scientific laws. Such
rules typically relate quantities, sales, and inventory records and thus
specify computations. The other parts of the program, especially how it
interacts with a user and how it stores data in the computer's disk,
are generated with little or no human intervention. 

All of these activities instruct some computer software to do something for
us. Some use scientific notation, some may use stylized English, some use a
concrete programming notation. All of them are some form of programming. 
The essence of these activities boils down to two concepts: 
\begin{enumerate}
\item relating one quantity to another quantity, and 

\item evaluating a relationship by substituting values for names. 
\end{enumerate}
Indeed, the two concepts characterize programming at the lowest level, the
computer's native language, and in a modern fashionable language such as
Java. A program relates its inputs to outputs; and, when a program is used
for specific inputs, the evaluation substitutes concrete values for names.

No one can predict what kind of application packages will exist in five or
ten years from now. But application packages will continue to require some
form of programming. To prepare students for these kinds of programming
activities, schools can either force them to study algebra, which is the
mathematical foundation of programming or expose them to some form of
programming. Using modern programming languages and environments, schools
can do the latter, they can do it effectively, and they can make algebra
fun.



\subsection{Design Recipes} \relax

Learning to design programs is like learning to play soccer.  A player must
 learn to trap a ball, to dribble with a ball, to pass, and to shoot a
 ball. Once the player knows those basic skills, the next goals are to
 learn to play a position, to play certain strategies, to choose among
 feasible strategies, and, on occasion, to create variations of a strategy
 because none fits.

A programmer is also very much like an architect, a composers, or a writer.
 They are creative people who start with ideas in their heads and blank
 pieces of paper. They conceive of an idea, form a mental outline, and
 refine it on paper until their writings reflect their mental image as much
 as possible. As they bring their ideas to paper, they employ basic
 drawing, writing, and playing music to express certain style elements of a
 building, to describe a person's character, or to formulate portions of a
 melody. They can practice their trade because they have honed their basic
 skills for a long time and can use them on an instinctive level.

Programmers also form outlines, translate them into first designs, and
 iteratively refine them until they truly match the initial idea. Indeed,
 the best programmers edit and rewrite their programs many times until they
 meet certain aesthetic standards. And just like soccer players,
 architects, composers, or writers, programmers must practice the basic
 skills of their trade for a long time before they can be truly creative.

Design recipes are the equivalent of soccer ball handling techniques,
 writing techniques, arrangements, and drawing skills.  A single design
 recipe represents a point of the program design space. We have studied
 this space and have identified many important categories.  This book
 selects the most fundamental and the most practical recipes and presents
 them in increasing order of difficulty.

About half the design recipes focus on the connection between input data
 and programs. More specifically, they show how the template of a program
 is derived from the description of the input data. We call this {\em  data-driven\/} program design, and it is the most frequently used form of
 design. Data-driven designs are easy to create, easy to understand, and
 easy to extend and modify. Other design recipes introduce the notion of
 {\em generative recursion\/}, {\em accumulation\/}, and {\em history  sensitivity\/}. The first one produces recursive programs that generate
 new instances of problems as they recur; accumulator-style programs
 collect data as they process inputs; and history-sensitive programs
 remember information between successive applications. Last, but not least,
 we also introduce a design recipe for {\em abstracting\/} over
 programs. Abstracting is the act of generalizing two (or more) similar
 designs into a one and of deriving the original instances from it.
 
On many occasions, a problem naturally suggests one design recipe.  On
 others, a programmer must choose from among several possibilities; each
 choice may produce programs with vastly different organizations.  Making
 choices is natural for a creative programmer. But, unless a programmer is
 thoroughly familiar with the bag of design recipes to choose from and
 completely understands the consequences of choosing one over the other,
 the process is necessarily {\em ad hoc\/} and leads to whimsical, bad
 designs. We hope that by mapping out a collection of design recipes, we
 can help programmers understand what to choose from and how to choose.

Now that we have explained what we mean by ``programming'' and ``program
design,'' the reader can see why and how teaching program design instills 
thinking skills that are important in a variety of professions. To design a
program properly, a student must
\begin{enumerate}
\item analyze a problem statement, typically stated as a word problem; 
\item express its essence, abstractly and with examples; 
\item formulate statements and comments in a precise language;
\item evaluate and revise these activities in light of checks and tests;  and 
\item pay attention to details. 
\end{enumerate}
All of these are activities that are useful for a businessman, a lawyer, a
journalist, a scientist, an engineer, and many others.

While traditional programming requires these skills, too, beginners often
don't understand this connection. The problem is that traditional
programming languages and traditional forms of programming force students
to perform a large amount of book-keeping work and to memorize a large
number of language-specific facts.  In short, {\em menial work drowns the teaching of essential skills\/}.  To avoid this problem, teachers must use
a programming environment that imposes as little overhead as possible and
that accommodates beginners. Because such tools didn't exist when we
started, we developed them. 



\subsection{The Choice of Scheme and DrScheme} \begin{flushright} \begin{minipage}{3.10in} {\it We ascribe beauty to that which is simple,\\
 which has no superfluous parts;\\
 which exactly answers its end,\\
 which stands related to all things,\\
 which is the mean of many extremes.\\
[.35cm] } \hfill---Ralph Waldo Emerson, {\em The Conduct of Life} \end{minipage} \end{flushright}

We have chosen Scheme as the programming language for this book, and we
have designed and implemented DrScheme, a programming environment for the
language with special assistance for beginning students. The programming
environment is freely available at the book's official Web site (see
below).

Still, the book it is not about programming in Scheme. We only use a small
 number of Scheme constructs in this book. Specifically, we use six
 constructs (function definition and application, conditional expressions,
 structure defininition, local definitions, and assignments) plus a dozen
 or so basic operations. This tiny subset of the language is all that is
 needed to teach the principles of computing and programming. Someone who
 wishes to use Scheme as a tool will need to read additional material.

The choice of Scheme for beginners is natural. First, the core of Scheme
 permits programmers to focus on just those two elements of programming
 that we pointed out at the beginning of the preface: programs as relations
 between quantities and evaluating programs for specific inputs.  Using
 just this core language, students can develop complete programs {\em  during the first session\/} with a teacher.

Second, Scheme can easily be arranged as a tower of languages. This
 property is crucial for beginners who make simple notational mistakes that
 generate obscure error messages about advanced features of a language. The
 result is often a wasteful search and a feeling of frustration on the
 student's side. To avoid this problem, our programming environment,
 DrScheme, implements several carefully chosen sublanguages of
 Scheme. Based on this arrangement, the environment can signal error
 messages that are appropriate to a student's level of knowledge. Better
 still, the layering of languages prevents many basic mistakes. We
 developed the layers and the protection modes by observing beginners for
 weeks in our laboratory. As students learn more about programming and the
 language, a teacher can expose students to richer layers of the language,
 which allows students to write more interesting and more concise programs.

Third, the DrScheme programming environment offers a truly interactive
 evaluator. It consists of two windows: a {\tt Definitions} window,
 where students define programs, and an {\tt Interactions} window, which
 acts like a pocket calculator.  Students can enter expressions into the
 latter, and DrScheme determines their values. In other words, computation
 starts with pocket-calculator arithmetic, which they know quite well, and
 quickly proceeds from there to calculations with structures, lists, and
 trees---the kinds of data that computer programs really manipulate. 
 Furthermore, an interactive mode of evaluation encourages students to
 experiment in all kinds of manners and thus stimulates their curiosity.

Finally, the use of an interactive evaluator with a rich data language
 permits students to focus on problem solving and program design
 activities. The key improvement is that interactive evaluation renders a
 discussion of input and output operations (almost) superfluous. This has
 several consequences.  First, input and output operations require
 memorization. Learning these things is tedious and boring. Conversely,
 students are better off learning problem-solving skills and using canned
 input and output support. Second, {\em good\/} text-oriented input
 requires deep programming skills, which are best acquired in a course on
 computational problem-solving.  Teaching bad text-oriented input is a
 waste of the teachers' and the students' time.  Third, modern software
 employs graphical user interfaces (GUI), which programmers design with
 editors and ``wizards'' but not by hand. Again, students are best off
 learning to design the functions that are connected to rulers, buttons,
 text fields and so on, rather than memoizing the specific protocols that
 currently fashionable GUI libraries impose. In short, discussing input and
 output is a waste of valuable learning time during a first introduction to
 programming.  If students decide to pursue programming in more depth,
 acquiring the necessary (Scheme) knowledge about input and output
 procedures is straightforward.

In summary, students can learn the core of Scheme in a couple of hours, yet
 the language is as powerful as a conventional programming language. As a
 result, students can immediately focus on the essence of programming,
 which greatly enhances their general problem solving skills.


\subsection{The Parts of the Book} \relax

The book consists of eight parts and seven intermezzos. The parts focus on
 program design; the intermezzos introduce other topics concerning
 programming and computing. Figure~\ref{fig:dependence} shows the
 dependence graph for the pieces of the book. The graph demonstrates that
 there are several paths through the book and that a partial coverage of
 the material is feasible. 

Parts~\ref{part:basic} through~\ref{part:advanced} cover the foudations of
 data-driven program design. Part~\ref{part:abstract} introduces
 abstraction in designs.  Parts~\ref{part:recursion} and~\ref{part:loops}
 are about generative recursion and accumulation.  For these first six
 parts, the book uses a completely functional---or algebraic---form of
 programming. One and the same expression always evaluates to the same
 result, no matter how often we evaluate it. This property makes it easy to
 design, and to reason about, programs. To cope with interfaces between
 programs and the rest of the world, however, we enrich the language with
 assignment statements and abandon some of our algebraic reasoning. The
 last two parts show what this means for the design of programs. More
 precisely, they show how the design recipes of the first six parts apply
 and why we must be much more careful once assignments are added.

Intermezzos introduce topics that are important for computing and
 programming in general but not for program design {\it per se\/}. Some
 introduce the syntax and semantics of our chosen subsets of Scheme on a
 rigorous basis, a few introduce additional programming constructs.
 Intermezzo~5 is a discussion of the abstract cost of computing (time,
 space, energy) and introduces vectors. Intermezzo~6 contrasts two ways of
 representing numbers and processing them. 

The coverage of some intermezzos can be delayed until a specific need
 arises. Especially the intermezzos on Scheme's syntax and semantics fall
 into this category. But, considering the central role of intermezzo~3 in
 figure~\ref{fig:dependence}, it should be covered in a timely fashion. 

%% BEGIN INLINED FILE: dependence.dl.tex


\def\apart(#1,#2)#3#4{  
\put(#1,#2){    \begin{picture}(50,\partheight)           
\put(0,0){\line(1,0){50}}      
\put(50,0){\line(0,1){\partheight}}      
\put(50,\partheight){\line(-1,0){50}}      
\put(0,\partheight){\line(0,-1){\partheight}}           
\put(0,7){\mbox{\parbox{\partwd}{\centerline{Part #3}}}}    \end{picture}}}

\def\intmz(#1,#2)#3#4{   
\put(#1,#2){     \begin{picture}(50,\partheight)        
\put(25}   \def\partheight{20,10}  \def\deltaln{-60){\oval(50,\partheight)}        
\put(0,7){\mbox{\parbox{\intwd}{\centerline{Intm.\ #3}}}}     \end{picture}}}

\def\stdep(#1,#2){   
\put(#1,#2){     \begin{picture}(50,40}  \def\twoheight{80)        \multiput(25}   \def\partheight{20,0)(+.1,0){5}{\vector(0,-1){40}  \def\twoheight{80}}     \end{picture}}}

\def\dgdep(#1,#2)#3#4{   
\put(#1,#2){     \begin{picture}(50,40}  \def\twoheight{80)        \multiput(25}   \def\partheight{20,0)(+.1,0){10}{\vector(#3,-1){#4}}     \end{picture}}}

\newdimen\partwd\setbox4=\hbox{~Part VIII~}\partwd=\wd4
\newdimen\intwd\setbox4=\hbox{~Interme 9~}\intwd=\wd4


\setcounter{foops}{380} 


\hrule


\begin{htmlonly}
\end{htmlonly}

 \begin{rawhtml} <center> <font color=red>  The dependence graph has to be translated into an HTML-suitable format. </font> </center> \end{rawhtml}

\center{Figure: The dependencies among parts and intermezzos} \label{fig:dependence}
\hrule

\bigskip

{\sc Iterative Refinement and Iteration of Topics}: Systematic program
 design is particularly interesting and important for large projects. The
 step from small single-function problems to small multi-function projects
 requires an additional design idea: iterative refinement. The goal is to
 design the core of a program and to add functionality to this core until
 the entire set of requirements is met.

Students in a first course can, and must, get their first taste of
 iterative refinement. Hence, in order to acquaint students with the
 technique, we have included extended exercises. Typically, a brief
 overview sets the stage for a collection of exercises. The exercises
 gently guide students through some design iterations. In
 section~\ref{sec:files}, the idea is spelled out explicitly.
 
Furthermore, the book revisits certain exercise and example topics time
 and again.  For example, sections~\ref{sec:move-circle},
 \ref{sec:move-shape}, \ref{sec:move-fig}, \ref{sec:abs-moving},
 \ref{sec:move-final}, and a few exercises in between the last two sections
 cover the idea of moving pictures across a canvas.  The students thus see
 the same problem several times, each time with more and more knowledge
 about how to organize programs.  

Adding pieces of functionality to a program demonstrates why programmers
 must follow a design discipline.  Solving the problem again shows students
 how to choose from alternative design recipes. Finally, on occasion, new
 knowledge just helps students improve the program organziation; in other
 words, students learn that programs aren't finished after they work for the
 first time but that, like papers and books, they need editing.


\bigskip

{\sc Teachpacks}: A second aspect of working on projects is that
 programmers have to work in teams. In an instructional context, it means
 that one student's program has to fit precisely to someone else's.  To
 simulate what fitting one's function to someone else's means, we provide
 DrScheme teachpacks. Roughly speaking, a teachpack simulates a team
 partner yet avoids the frustration of working with mistakes in a partner's
 program component.  More technically, the projects almost always consist
 of a {\sl view} and a {\sl model} program component (in the sense of the
 model-view software architecture). In a typical setting, students design
 the model component.  The teachpacks provide the view components, often in
 the form of (grapical) user interfaces. Thus they eliminate the tedious,
 mindless portions of coding. Furthermore, this particular separation of
 concerns mimics that of real-world projects. 

 Fitting model components to view components requires students to pay
 attention to precise specifications of functions. It demonstrates the
 paramount importance of following a design discipline. It is also a
 critical skill for programmers and is often underemphasized in beginning
 courses. In part~\ref{part:abstract} we show how to construct some simple
 GUIs and how GUI events trigger the application of model functions.  The
 goal is to explain that constructing GUIs is no mystery, but not to spend
 a lot of time on a topic that requires mostly rote learning and little
 computational thinking.

\bigskip

{\sc Schedule}: Each university, college, and school has its own needs and
 must find an appropriate schedule. At Rice University, we cover the entire
 book plus some additional material. An instructor at a research university
 should probably keep up a similar pace. At the high school end of the
 book's test users, the pace is slower.  Many of the high schools that
 tested the book covered the first two parts and selected sections from the
 next two or three in a semester; some used just the first part to teach
 algebraic problem solving from a computational perspective; and yet others
 worked through all of the first six parts in a year.



\bigskip

{\sc The Book on the Web}: The book comes in two versions: a paper
copy and a freely accessible on-line version at \\

\verb|         http://www.htdp.org/|

\medskip

\noindent The Web site also provides additional material, especially
extended exercises of the style mentioned above.  At this time, the Web
page offers exercises on visual simulation of ball games and the management
of Web site. More exercises will be added.

The on-line version of the book contains three kinds of additional
hints. Each is marked with one of the following three icons, the first two
also occur in the paper copy: 

\smallskip


\begin{htmlonly}
\end{htmlonly}

\begin{rawhtml} <table bgcolor=beige width=600 align=center> <tr><td><img src=../icons/teacher1.gif></td> <td>This marker refers to <em>teacher hints</em>, which suggest strategies on how to present a section, on how to approach an exercise, or on how to supplement some material.</td> </tr>  <tr><td><img src=../icons/plt.gif></td> <td> This marker refers to <em>DrScheme hints</em>. The programming environment has been designed with students in mind. The hints suggest how to use it at various stages of the learning process.</td> </tr>  <tr><td><img src=../icons/hand.right.gif></td> <td>This marker, placed at the end of exercises, links to <em>on-line solutions.</em> Solutions are available to registered teachers. To find out more about registration,  see the book's registration site (not available yet). </tr> </table> \end{rawhtml}

\bigskip

{\sc Typography and Definitions}: For readability, Scheme programs are
type-set using a small number of fonts. {\it Italic words\/} refers to
program names and variables.  {\sf Sans Serif} items are constants and
built-in operations. {\bf Boldface} words are Scheme keywords.

Definitions come in three varieties. There are those terms that concern the
principles of programming and computing. The book lists the first
occurrence of such terms with {\sc {small capital letters}}. Other
definitions are of a more fleeting nature; they introduce terms that are
important for a section, an example, an exercise or some other small part
of the book. The book uses {\sl slanted letters} to emphasize such
definitions. Finally, the book also defines classes of data. Most data
definitions are boxed, and the first occurrence of the defined name is also
type-set using {\sl {slanted words.}}


\subsection{Acknowledgements} \relax

Four people deserve special thanks: Robert ``Corky'' Cartwright, who
co-developed a predecessor of Rice's introductory course with the first
author; Daniel P.\ Friedman, for asking the first author to rewrite The
Little LISPer (also MIT Press) in 1984, because it started this project;
John Clements, who designed, implemented, and maintains DrScheme's stepper;
and Paul Steckler, who faithfully supported the team with contributions to
our Scheme programming tool suite.

The development of the book benefited from many other friends and
colleagues who used it in their courses and/or gave detailed comments on
early drafts. We are grateful to them for their help and their patience:
Ian Barland,
Corky Cartwright,
John Clements, 
Bruce Duba,
Mike Ernst,
Kathi Fisler,
Daniel P.\ Friedman,
John Greiner,
and
John Stone.
Peter Doyle, 
Christopher Felleisen, 
Geraldine Morin, 
and
Valdemar Tamez
also suggested
improvements to early drafts. 

In addition, numerous attendees of our TeachScheme!\ workshops used early
drafts in their classroom. Many sent in comments and
suggestions. As representative of these we mention the following: 
Ms.\ Barbara Adler,
Dr.\ Stephen Bloch,
Mr.\ Jack Clay,
Dr.\ Richard Clemens,
Mr.\ Kyle Gillette,
Ms.\ Karen Buras,
Mr.\ Marvin Hernandez,
Mr.\ Michael Hunt,
Ms.\ Karen North,
Mr.\ Jamie Raymond,
and
Mr.\ Robert Reid.
Thank you. 



\pagenumbering{arabic}
\setcounter{page}{3}
\renewcommand{\thesection}{\arabic{section}}
\setcounter{section}{0}

%% BEGIN INLINED FILE: curriculum1a.dl.tex


\newpage\part{Processing Simple Forms of Data}\thispagestyle{empty}\newpage\label{part:basic}

\section{Students, Teachers, and Computers} \label{sec:intro} 
\htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:intro}

\htmladdimg{../icons/teacher.gif}~{\em   Have students read the preface and conduct a pop quizz on the reading.   Make sure they carry with them the program design recipe at all times   and have it in front of them when they program. They will understand it  more and more as they work through the book.    We recommend that you cover numbers (section~\ref{sec:basics}),  booleans (\ref{sec:cond:booleans}), the shape of conditional expressions  (beginning of \ref{sec:cond:prog2}), and symbols (\ref{sec:sym}) as  quickly as possible in a first pass. A student in a standard high school  course should be able to understand this material in a week or less, that  is, 5 sessions with one small homework per day.   The goal is to get to the exercises~\ref{ex:f2c}, \ref{ex:convert3},  \ref{ex:guess1}, \ref{ex:check-guess}, and \ref{ex:guess2} [in that  order]. These exercises show how modern programming works and why we {\em  don't\/} wish to think about input and output concepts---yet.   Programmers work out the essence, and then put together GUI interfaces  with ``wizards''. The latter will be totally automated in the near  future. It makes no sense to teach your students these skills; they may  never need them, or if they do, the wizards will work in a different  manner.   The game exercise in section~\ref{sec:sym} are generalized one more time  in exercise~\ref{ex:guess3}, where students learn to cope with lists.   Then when the kids are bit more hooked, return to conditionals  (\ref{sec:cond:prog2}), cover the two sections on design recipes again,  and reinforce the section on composing programs.   }

We learn to compute at a young age. At first we just add and subtract
numbers. 
\begin{quote}
One plus one equals two. Five minus two is three.
\end{quote}
As we grow older we learn about additional mathematical operations, like
exponentiation and sine, but we also learn to describe rules of
computation.
\begin{quote}
Given a circle of radius {\it r\/}, its circumference is {\it r\/} times
two times {\it pi\/}. A minimum-wage laborer who works for {\it N\/} hours
earns {\it N\/} times 5.35 dollars. 
\end{quote}
The truth is, our teachers turn us into computers and program us to execute
simple computer programs.

So, the secret is out. Computer programs are just very fast students. They
can perform millions of additions while we might still be struggling with
the first one. But computer programs can do more than just manipulate
numbers.  They can guide an airplane.  They can play games. They can look
up a person's phone number. They can print the payroll checks for huge
corporations. In short, computers process all kinds of information.

People state information and instructions in English. 
\begin{quote}
The temperature is $35^o C$; convert this temperature into Fahrenheit.  It
takes this car 35 seconds to accelerate from zero to 100 miles per hour;
determine how far the car gets in 20 seconds. 
\end{quote}
Computers, however, barely understand basic English and certainly can't
understand complex instructions expressed in English. Instead we must learn
to speak a computer language so that we can communicate information and
instructions.

A computer's language of instruction and information is a
{\sc {programming language}}.  Information expressed in a programming
language is called {\sc {data}}. There are many flavors of data.  {\sl Numbers\/} are one class of data.  {\sl Number series\/} belong to
the class of {\sc {compound data}}, because each series is made up of
other pieces of smaller pieces of data, namely, numbers. To contrast the
two kinds of data, we also call numbers {\sc {atomic data}}. Letters are
other examples of atomic data; family trees are compound data.  

Data represents information, but the concrete interpretation is up to
us. For example, a number like 37.51 may represent a temperature, a time,
or a distance.  A letter like ``A'' may denote a school grade, a quality
symbol for eggs, or a part of an address.

Like data, instructions, also called {\sc {operations}}, come in several
flavors. Each class of data comes with a set of {\sc {primitive operations}}. For numbers, we naturally get {{\tt +}\/}, {{\tt -}\/},
{{\tt *}\/} and so on.  Programmers compose primitive operations into
{\sc {programs}}. Thus, we may think of primitive operations as the
words of a foreign language and of programming as forming sentences in this
language.

Some programs are as small as essays. Others are like sets of
encyclopedias.  Writing good essays and books requires careful planning,
and writing good programs does, too. Small or large, a good program cannot
be created by tinkering around. It must be carefully {\em designed\/}. Each
piece needs a lot of attention; composing programs into larger units must
follow a well-planned strategy. Designing programs properly must be
practiced from our very first day of programming.


In this book, we will learn to design computer programs, and we will learn
to understand how they function.  Becoming and being a programmer is fun,
but it is not easy.  The best part of being a programmer is watching our
``products'' grow and become successful. It is fun to observe a computer
program play a game. It is exciting to see a computer program help
someone. To get to this point, however, we must practice many skills. As we
will find out, programming languages are primitive; especially, their
grammar is restrictive. And unfortunately, computers are stupid. The
smallest grammatical mistake in a program is a fatal stumbling block for a
computer.  Worse, once our program is in proper grammatical shape, it might
not perform the computations as intended.

Programming a computer requires patience and concentration. Only attention
to minute details will avoid frustrating grammatical mistakes. Only
rigorous planning and adherence to the plan will prevent serious logical
mistakes in our designs. But when we finally succeed with our designs, we
will have learned skills that are useful far beyond the realm of
programming.

Let's get started! 



\section{Numbers, Expressions, Simple Programs} \label{sec:basics} 
In the beginning, people thought of computers as number crunchers. And
indeed, computers are very good at working with numbers. Since teachers
start their first-graders on computing with numbers, we start with numbers,
too. Once we know how computers deal with numbers, we can develop simple
programs in no time; we just translate common-sense into our programming
notation. Still, even developing such simple programs requires discipline,
and so we introduce the outline of the most fundamental design recipe and
the basic programming guideline at the end of this section.

\subsection{Numbers and Arithmetic} 
\label{sec:basics:num} 
\htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:basics:num}

Numbers come in many different flavors: positive and negative integers,
fractions (also known as rationals) and reals are the most well-known
classes of numbers:
\begin{center}
{{\tt 5}\/}   \hfill
{{\tt -5}\/}  \hfill
{{\tt 2/3}\/} \hfill
{{\tt 17/3}\/}\hfill
{{\tt \char'043}{\tt i1.4142135623731}\/} \hfill
\end{center}
The first is an integer, the second one a negative integer, the next two
are fractions, and the last one is an inexact representation of a real
number.

Like a pocket calculator, the simplest of computers, Scheme permits
programmers to add, subtract, multiply, and divide numbers:
\begin{center}
{{\tt (+}\ {\tt 5}\ {\tt 5)}\/} \hfill
{{\tt (+}\ {\tt -5}\ {\tt 5)}\/}\hfill
{{\tt (+}\ {\tt 5}\ {\tt -5)}\/}\hfill
{{\tt (-}\ {\tt 5}\ {\tt 5)}\/}\hfill
{{\tt (*}\ {\tt 3}\ {\tt 4)}\/}  \hfill
{{\tt (/}\ {\tt 8}\ {\tt 12)}\/} \hfill
\end{center}
The first three ask Scheme to perform additions; the last three demand a
subtraction, a multiplication, and a division. All arithmetic expressions
are parenthesized and mention the operation first; the numbers follow the
operation and are separated by spaces.

As in arithmetic or algebra, we can nest expressions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (*}\  {\tt (+}\  {\tt 2}\  {\tt 2)}\  {\tt (/}\  {\tt (*}\  {\tt (+}\  {\tt 3}\  {\tt 5)}\  {\tt (/}\  {\tt 30}\  {\tt 10))}\  {\tt 2))}\end{tabbing}\end{minipage}\end{center}
Scheme evaluates these expressions exactly like we do. It first reduces the
innermost parenthesized expressions to numbers, then the next layer and so
on: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (*}\  {\tt (+}\  {\tt 2}\  {\tt 2)}\  {\tt (/}\  {\tt (*}\  {\tt (+}\  {\tt 3}\  {\tt 5)}\  {\tt (/}\  {\tt 30}\  {\tt 10))}\  {\tt 2))}\\
{\tt =}\  {\tt (*}\  {\tt 4}\  {\tt (/}\  {\tt (*}\  {\tt 8}\  {\tt 3)}\  {\tt 2))}
\\
{\tt =}\  {\tt (*}\  {\tt 4}\  {\tt (/}\  {\tt 24}\  {\tt 2))}
\\
{\tt =}\  {\tt (*}\  {\tt 4}\  {\tt 12)}
\\
{\tt =}\  {\tt 48}
\end{tabbing}\end{minipage}\end{center}
Because every Scheme expression has the shape\\
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (operation}\  {\tt A}\  {\tt ...}\  {\tt B)}\end{tabbing}\end{minipage}\end{center}
there is never any question about which part has to be evaluated first.
Whenever {{\tt A}\ {\tt ...}\ {\tt B}\/} are numbers, the expression can be evaluated;
otherwise, {{\tt A}\ {\tt ...}\ {\tt B}\/} are evaluated first.  Contrast this with
$$ 3 + 4 \cdot 5 \ , $$
which is an expression that we encounter in grade school.  Only a
substantial amount of practice guarantees that we remember to evaluate the
multiplication first and the addition afterwards.\footnote{Another advantage of Scheme's notation is that we always know where to place an operator or where to find it: to the immediate right of the opening parenthesis. This is important in computing because we need many more operators than just the few numerical operators that we use in arithmetic and algebra.}

Finally, Scheme not only provides simple arithmetical operations but a whole
range of advanced mathematical operations on numbers. Here are five
examples: 
\begin{enumerate}
\item {{\tt (sqrt}\ {\tt A)}\/} computes $\sqrt{A}$;
\item {{\tt (expt}\ {\tt A}\ {\tt B)}\/} computes $A^B$;
\item {{\tt (remainder}\ {\tt A}\ {\tt B)}\/} computes the remainder of the integer division $A/B$; 
\item {{\tt (log}\ {\tt A)}\/} computes the natural logarithm of {{\tt A}\/};
and
\item {{\tt (sin}\ {\tt A)}\/} computes the sine of {\it A\/} radians.
\end{enumerate}
When in doubt whether a primitive operation exists or how it works, use
DrScheme to test whether an operation is available with a simple example.

\medskip\noindent{\bf A Note on Numbers}:\  \label{anote:numbers}
Scheme computes with {\sc {exact}} integers and rationals as long as we
use primitive operations that produce exact results. Thus, it displays the
result of {{\tt (/}\ {\tt 44}\ {\tt 14)}\/} as {{\tt 22/7}\/}. Unfortunately, Scheme and
other programming languages compromise as far as real numbers are
concerned. For example, since the square root of {{\tt 2}\/} is not a
rational but a real number, Scheme uses an {\sc {inexact number}}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (sqrt}\  {\tt 2)}\\
{\tt =}\  {\tt \char'043}{\tt i1.4142135623731}
\end{tabbing}\end{minipage}\end{center}
The {{\tt \char'043}{\tt i}\/} notation warns the programmer that the result is an
approximation of the true number. Once an inexact number has become a part
of a calculation, the process continues in an approximate manner the true result of
operations. To wit:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (-}\  {\tt \char'043}{\tt i1.0}\  {\tt \char'043}{\tt i0.9)}\\
{\tt =}\  {\tt \char'043}{\tt i0.09999999999999998}
\end{tabbing}\end{minipage}\end{center}
but 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (-}\  {\tt \char'043}{\tt i1000.0}\  {\tt \char'043}{\tt i999.9)}\\
{\tt =}\  {\tt \char'043}{\tt i0.10000000000002274}
\end{tabbing}\end{minipage}\end{center}
even though we know from mathematics that both differences should be
{{\tt 0.1}\/} and equal. Once numbers are inexact, caution is necessary. 

This imprecision is due to the common simplification of writing down
numbers like the square root of 2 or $\pi$ as rational numbers. Recall that
the decimal representation of these numbers are infinitely long (without
repetition).  A computer, however, has a finite size, and can therefore
only represent a portion of such a number. If we choose to represent these
numbers as rationals of with a fixed number of digits, the representation
is necessarily inexact. Intermezzo~6 will explain how inexact numbers work.

To focus our studies on the important concepts of computing and away from
these details, the teaching lanuages of DrScheme deal with numbers as
precise numbers as much as possible. When we write {{\tt 1.25}\/}, DrScheme
interprets this number as a precise fraction, not as an inexact number.
When DrScheme's {\tt Interaction} window displays a number such as
{{\tt 1.25}\/} or {{\tt 22/7}\/}, it is the result of a computation with
precise rationals and fractions. Only numbers prefixed by {{\tt \char'043}{\tt i}\/} are
inexact representations.~{\rule{3pt}{5pt}}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 

{\bf Exercise 2.1.1}

 \label{ex:discover1}
Evaluate {{\tt (sqrt}\ {\tt 4)}\/}, {{\tt (sqrt}\ {\tt 2)}\/}, and {{\tt (sqrt}\ {\tt -1)}\/} in
DrScheme. Then, find out whether DrScheme knows an operation for
determining the tangent of an angle. {\rule{3pt}{5pt}}



\subsection{Variables and Programs} \label{sec:basics:var-prog} \htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:basics:var-prog}

In algebra we learn to formulate dependencies between quantities using {\em variable expressions\/}. A variable is a placeholder that stands for an
unknown quantity. For example, a disk of radius $r$ has the approximate
area\footnote{It is common to speak of the area of a circle, but mathematically speaking, the circle is only the disk's outer edge.}
$$ 3.14 \cdot r^2 \ .$$
In this expression, {\it r\/} stands for any positive number.  If we now
come across a disk with radius $5$, we can determine its area by
substituting $5$ for $r$ in the above formula and by reducing the resulting
expression to a number:
$$ 3.14 \cdot 5^2  = 3.14 \cdot 25 = 78.5 \ .$$
More generally, expressions that contain variables are rules that describe
how to compute a number {\em when\/} we are given values for the variables.

\htmladdimg{../icons/teacher.gif}~{\em Blanks, line breaks, and tabs have no meaning in Scheme. Still,  Schemers have adopted rather rigorous rules on program layout, because  particular layouts help writers check their parentheses quickly and help  readers grasp code quickly. We therefore recommend that you force students  to follow the book's conventions of using blank space and indentations.  Insist on the format: header, return/enter, expression; a proper amount of  white space is automatically inserted by DrScheme after a user hits  return/enter. Students should not modify this white space. If it is an  unexpected amount of white space, they should check their parentheses.   Finally, if all else fails, use {\tt Scheme|Indent} or {\tt Scheme|Indent All}  to adjust white space. The former indents a selected region, the latter  the entire {\tt Definitions} window.}

A {\sc {program}} is such a rule. It is a rule that tells us and the
computer how to produce data from some other data. Large programs consist
of many small programs and combine them in some manner. It is therefore
important that programmers name each rule as they write it down. A good
name for our sample expression is {{\tt area-of-disk}\/}. Using this name,
we would express the rule for computing the area of a disk as
follows:\label{prg:area-of-disk}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (area-of-disk}\  {\tt r)}\  \\
\>{\tt (*}\  {\tt 3.14}\  {\tt (*}\  {\tt r}\  {\tt r)))}
\end{tabbing}\end{minipage}\end{center}
The two lines say that {{\tt area-of-disk}\/} is a rule, that it consumes
one {\sl input}, called {{\tt r}\/}, and that the result, or {\sl output}, is
going to be {{\tt (*}\ {\tt 3.14}\ {\tt (*}\ {\tt r}\ {\tt r))}\/} once we know what number {{\tt r}\/}
stands for.

Programs combine basic operations. In our example, {{\tt area-of-disk}\/}
uses only one basic operation, multiplication, but {{\tt define}\/}d
programs may use as many operations as necessary. Once we have defined a
program, we may use it as if it were a primitive operation. For each
variable listed to the right of the program name, we must supply one
input. That is, we may write expressions whose operation is
{{\tt area-of-disk}\/} followed by a number:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (area-of-disk}\  {\tt 5)}\end{tabbing}\end{minipage}\end{center}
We also say that we {\sc {apply}} {{\tt area-of-disk}\/} to {{\tt 5}\/}. 

The application of a {{\tt define}\/}d operation is evaluated by copying the
expression named {{\tt area-of-disk}\/} and by replacing the variable
({{\tt r}\/}) with the number we supplied ({{\tt 5}\/}):
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (area-of-disk}\  {\tt 5)}\  \\
{\tt =}\  {\tt (*}\  {\tt 3.14}\  {\tt (*}\  {\tt 5}\  {\tt 5))}
\end{tabbing}\end{minipage}\end{center}
After that, we use plain arithmetic: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt ...}\\
{\tt =}\  {\tt (*}\  {\tt 3.14}\  {\tt 25)}
\\
{\tt =}\  {\tt 78.5}
\end{tabbing}\end{minipage}\end{center}

\htmladdimg{../icons/teacher.gif}~{\em Students may have doubts about this model of how the computer works.  These doubts are due to wide-spread misconception about computers. No  matter at what level we choose to explain the workings of a computer, we  always use an approximation. Initially, it is best to approximate at the  most intuitive level---the level of ordinary elementary school  arithmetic.}

Many programs consume more than one input. Say we wish to define a program
that computes the area of a ring, that is, a disk with a hole in the center:

\begin{htmlonly}
\begin{rawhtml}
<center><img src=../icons/disk.gif align=center></center>
\end{rawhtml}
\end{htmlonly}
\relax

\noindent The area of the ring is that of the outer disk minus the area of
the inner disk, which means that the program requires {\em two\/} unknown
quantities: the outer and the inner radii.  Let us call these unknown
numbers {{\tt outer}\/} and {{\tt inner}\/}. Then the
program that computes the area of a ring is defined as follows:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (area-of-ring}\  {\tt outer}\  {\tt inner)}\  \\
\>{\tt (-}\  \={\tt (area-of-disk}\  {\tt outer)}
\\
\>\>{\tt (area-of-disk}\  {\tt inner)))}
\end{tabbing}\end{minipage}\end{center}
The three lines express that {{\tt area-of-ring}\/} is a program, that the
program accepts two inputs, called {{\tt outer}\/} and \relax
{{\tt inner}\/}, and that the result is going to be the difference
between {{\tt (area-of-disk}\ {\tt outer)}\/} and {{\tt (area-of-disk}{ }{\tt inner)}\/}. In other words, we have used both basic Scheme operations
and {{\tt define}\/}d programs in the definition of {{\tt area-of-ring}\/}.

When we wish to use {{\tt area-of-ring}\/}, we must supply two inputs: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (area-of-ring}\  {\tt 5}\  {\tt 3)}\  \end{tabbing}\end{minipage}\end{center}
The expression is evaluated in the same manner as {{\tt (area-of-disk}{ }{\tt 5)}\/}.  We copy the expression from the definition of the program and replace
the variable with the numbers we supplied:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (area-of-ring}\  {\tt 5}\  {\tt 3)}\  \\
{\tt =}\  {\tt (-}\  \={\tt (area-of-disk}\  {\tt 5)}
\\
\>{\tt (area-of-disk}\  {\tt 3))}
\\
{\tt =}\  {\tt (-}\  \={\tt (*}\  {\tt 3.14}\  {\tt (*}\  {\tt 5}\  {\tt 5))}
\\
\>{\tt (*}\  {\tt 3.14}\  {\tt (*}\  {\tt 3}\  {\tt 3)))}
\\
{\tt =}\  {\tt ...}
\end{tabbing}\end{minipage}\end{center}
The rest is plain arithmetic.


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

\htmladdimg{../icons/teacher.gif}~{\em Explain that these exercises demonstrate that programs require a fair  amount of {\em domain knowledge\/}. Here the relevant domains are physics,  chemistry, business, geometry, and algebra. In general, it can be  anything: science, engineering, music, marketing, literature   The first exercise is critical. It illustrates that one function can be  used with three different user-interfaces.  If students don't believe  their functions play a role, have them add a mistake to the function and  watch how things change.   The only way to achieve this compatibility of functions with many  different interfaces is to develop the functions independently of {\em  all\/} input and output considerations. In software engineering  terminology, this separation of concerns is called the {\sl model-view\/}  pattern. We will study the model-view pattern and how to create our own  GUI views in more detail in part~\ref{part:abstract}. Without some  background in writing plain functions, beginners cannot appreciate the  idea.   Use the Help Desk to read the documentation for the teachpack and how to  install teachpacks. The documentation provides a bit more information.   For now, the quotes around the file names are just funny things. Later on  we will find out that this is just another kind of data.  Similarly,  handing a function to another function may appear strange. It is after all  an advanced mathematical topic. But in good programming, especially GUI  programming, this happens all the time. Again, we will study the idea in   part~\ref{part:abstract}. }

{\bf Exercise 2.2.1}

 \label{ex:f2c}
Define the program {{\mbox{\it Fahrenheit\/}{{}{\tt ->}{}}\mbox{\it Celsius\/}}\/},\footnote{An arrow is keyed  in as {\tt -} followed by {\tt >}.} which consumes a temperature measured
 in Fahrenheit and produces the Celsius equivalent. Use a chemistry or
 physics book to look up the conversion formula. 

When the function is fully developed, test it using the teachpack
 {\tt\bf convert.ss}. The teachpack provides three functions:
 {{\tt convert-gui}\/}, {{\tt convert-repl}\/}, and {{\tt convert-file}\/}.
 The first creates a graphical user interface. Use it with 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (convert-gui}\  {\mbox{\it Fahrenheit\/}{{}{\tt ->}{}}\mbox{\it Celsius\/}}{\tt )}\end{tabbing}\end{minipage}\end{center}
 The expression will spawn a new window in which users can manipulate
 sliders and buttons. 

The second emulates the {\tt Interactions} window. Users are asked to
 enter a Fahrenheit temperature, which the program reads, evaluates, and
 prints. Use it via 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (convert-repl}\  {\mbox{\it Fahrenheit\/}{{}{\tt ->}{}}\mbox{\it Celsius\/}}{\tt )}\end{tabbing}\end{minipage}\end{center}
 The last operation processes entire files. To use it, create a file with
 those numbers that are to be converted. Separate the numbers with blank
 spaces or newlines. The function reads the entire file, converts the
 numbers, and writes the results into a new file. Here is the expression: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (convert-file}\  {\tt ``in.dat''}\  {\mbox{\it Fahrenheit\/}{{}{\tt ->}{}}\mbox{\it Celsius\/}}\  {\tt ``out.dat'')}\end{tabbing}\end{minipage}\end{center}
 This assumes that the name of the newly created file is {\tt in.dat} and
 that we wish the results to be written to the file {\tt out.dat}. For more
 information, use DrScheme's Help Desk to look up the teachpack
 {\tt\bf convert.ss}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/f2c.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 2.2.2}

 \label{ex:us-dm}
Define the program {{\mbox{\it dollar\/}{{}{\tt ->}{}}\mbox{\it euro\/}}\/}, which consumes a number of
 dollars and produces the euro equivalent. Use the currency table in the
 newspaper to look up the current exchange rate. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/us-dm.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 2.2.3}

 \label{ex:triangle} 
Define the program {{\tt triangle}\/}. It consumes the length of a
 triangle's side and its height. The program produces the area of the
 triangle. Use a geometry book to look up the formula for computing the
 area of a triangle. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/triangle.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 2.2.4}

 \label{ex:convert3} 
Define the program {{\tt convert3}\/}. It consumes three digits, starting
 with the least significant digit, followed by the next most significant
 one and so on. The program produces the corresponding number. For
 example:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (convert3}\  {\tt 1}\  {\tt 2}\  {\tt 3)}\  {\tt =}\  {\tt 321}\end{tabbing}\end{minipage}\end{center}
 Use an algebra book to find out how such a conversion works.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/convert3.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 2.2.5}

 \label{ex:algebra1}  \label{ex:algebra1-r} 
A typical exercise in an algebra book asks the reader to evaluate an expression
like 
$$ \frac{n}{3} + 2 $$
for $n = 2$, $n = 5$, and $n = 9$. Using Scheme, we can formulate such an
expression as a program and use the program as many times as
necessary. Here is the program that corresponds to the above
expression: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt n)}\\
\>{\tt (+}\  {\tt (/}\  {\tt n}\  {\tt 3)}\  {\tt 2))}
\end{tabbing}\end{minipage}\end{center}
First determine the result of the expression at $n = 2$, $n = 5$, and $n =
9$ by hand, then with DrScheme's stepper.

Also formulate the following three expressions as programs: 
\begin{enumerate}
\item $n^2 + 10$
\item $\frac12 \cdot n^2 + 20$
\item $2 - \frac{1}{n}$
\end{enumerate}
Determine their results for $n = 2$ and $n = 9$ by hand and with DrScheme.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/algebra1.html}{\rule{3pt}{5pt}}


\subsection{Word Problems} 
\label{sec:basics-word-probs} 

Programmers are rarely handed mathematical expressions to turn into
programs. Instead they typically receive informal problem descriptions that
often contain irrelevant and sometimes ambiguous information. The
programmers' first task is to extract the relevant information and then to
formulate appropriate expressions.

Here is a typical example:
\begin{quote}
  Company XYZ \& Co.\ pays all its employees \$12 per hour. A typical
  employee works between 20 and 65 hours per week. Develop a program
  that determines the wage of an employee from the number of hours of work.
\end{quote}
The last sentence is the first to mention the actual task: to write a
program that determines one quantity based on some other quantity. More
specifically, the program consumes one quantity, the number of hours of
work, and produces another one, the wage in dollars.  The first sentence
implies how to compute the result, but doesn't state it explicitly. In this
particular example, though, this poses no problem. If an employee works $h$
hours, the wage is
 $$ 12 \cdot h\ .$$
Now that we have a rule, we can formulate a Scheme program:\label{prg:wage}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (wage}\  {\tt h)}\\
\>{\tt (*}\  {\tt 12}\  {\tt h))}
\end{tabbing}\end{minipage}\end{center}
The program is called {{\tt wage}\/}; its parameter {{\tt h}\/} stands for
the hours an employee works; and its result is {{\tt (*}\ {\tt 12}\ {\tt h)}\/}, the
corresponding wage. 

\htmladdimg{../icons/teacher.gif}~{\em Discuss with your students how they teased out what they had to do  and what information was superfluous. It is impossible to give guidelines  on that, but practice and discussing the idea repeatedly helps a lot.  The following exercises prepare students thematically and practically for  the section on ``a program = many function definitions''. Use the solutions  as motivations for the section on comopsing functions.}


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 2.3.1}

 \label{ex:taxed-wage} \label{ex:taxed-wage-r}
Utopia's tax accountants always use programs that compute income taxes even
though the tax rate is a solid, never-changing 15\%.  Define the program
{{\tt tax}\/}, which determines the tax on the grosspay.

Also define {{\tt netpay}\/}.  The program determines the netpay of an
employee from the number of hours worked. Assume an hourly rate of
\$12.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/taxed-wage.html}{\rule{3pt}{5pt}}


{\bf Exercise 2.3.2}

 \label{ex:sum-coins}
The local supermarket needs a program that can compute the value of a bag
of coins. Define the program {{\tt sum-coins}\/}. It consumes four numbers:
the number of pennies, nickels, dimes, and quarters in the bag; it produces
the amount of money in the bag.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sum-coins.html}{\rule{3pt}{5pt}}


{\bf Exercise 2.3.3}

 \label{ex:movie-theater}
An old-style movie theater has a simple profit function. Each customer pays
\$5 per ticket. Every performance costs the theater \$20, plus \$.50 per
attendee. Develop the function {{\tt total-profit}\/}. It consumes the number
of attendees (of a show) and produces how much income the attendees
produce.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/movie-theater.html}{\rule{3pt}{5pt}}


\subsection{Errors} \label{sec:basics-eval} \htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:basics-eval}

When we write Scheme programs, we must follow a few carefully designed
rules, which are a compromise between a computer's capabilities and human
behavior.\footnote{This statement is true for any other programming language as well, for example, spreadsheet languages, C, word processor macro. Scheme is simpler than most of these and easy to understand for computers. Unfortunately, to human beings who grow up on infix expressions such as $5 + 4$, Scheme prefix expressions such as {{\tt (+}\ {\tt 5}\ {\tt 4)}\/} initially appear to be complicated. A bit of practice will quickly eliminate this misconception.} Fortunately, forming Scheme definitions and
expressions is intuitive. Expressions are either {\sl atomic}, that is,
numbers and variables; or they are {\sl compound} expressions, in which
case they start with ``('', followed by an operation, some more
expressions, and terminated by ``)''. Each expression in a compound
expression should be preceded by at least one space; newlines are
permissible, and sometimes increase readability.

Definitions have the following schematic shape:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt x}\  {\tt ...}\  {\tt y)}\\
\>{\tt an-expression)}
\end{tabbing}\end{minipage}\end{center}
That is, a definition is a sequence of several words and expressions:
``('', the word ``define'', ``('', a non-empty sequence of names separated
by spaces, ``)'', an expression, and a closing ``)''. The embedded sequence
of names, {{\tt f}\ {\tt x}\ {\tt ...}\ {\tt y}\/}, introduces the name of the program and the
names of its parameters.

\bigskip

\noindent{\bf Syntax\protect\footnote{We will find out in section~\ref{sec:synsem} why such errors are called {\em syntax\/} errors.} Errors:} Not all parenthesized expressions are Scheme expressions. For
example, {{\tt (10)}\/} is a parenthesized expression, but Scheme does not
accept it as a legal Scheme expressions because numbers are not supposed to
be included in parentheses. Similarly, a sentence like {{\tt (10}\ {\tt +}\ {\tt 20)}\/}
is also ill-formed; Scheme's rules demand that the operator is mentioned
first. Finally, the following two definitions are not well-formed:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (P}\  {\tt x)}\\
\>{\tt (+}\  {\tt (x)}\  {\tt 10))}
\\
{\tt (d}\={\tt efine}\  {\tt (P}\  {\tt x)}
\\
\>{\tt x}\  {\tt 10)}
\end{tabbing}\end{minipage}\end{center}
The first one contains an extra pair of parentheses around the variable
{{\tt x}\/}, which is not a compound expression; the second contains two
atomic expressions, {{\tt x}\/} and {{\tt 10}\/}, instead of one.

When we click DrScheme's {\tt Execute} button, the programming
environment first determines whether the definitions are formed according
to Scheme's rules. If some part of the program in the {\tt Definitions}
window is ill-formed, DrScheme signals a {\sc {syntax error}} with an
appropriate error message and highlights the offending part. Otherwise it
permits the user to evaluate expressions in the {\tt Interactions}
window.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 2.4.1}

 \label{ex:syn-error-exp} \htmlref{\htmladdimg{../icons/plt.gif}}{drn:ex:syn-error-exp}
Evaluate the following sentences in DrScheme, one at a time:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (+}\  {\tt (10)}\  {\tt 20)}\\
{\tt (10}\  {\tt +}\  {\tt 20)}
\\
{\tt (+}\  {\tt +)}
\end{tabbing}\end{minipage}\end{center}
Read and understand the error messages.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/syn-error-exp.html}{\rule{3pt}{5pt}}


{\bf Exercise 2.4.2}

 \label{ex:syn-error-def}
Enter the following sentences, one by one, into DrScheme's
{\tt Definitions} window and click {\tt Execute}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt 1)}\\
\>{\tt (+}\  {\tt x}\  {\tt 10))}
\\
{\tt (d}\={\tt efine}\  {\tt (g}\  {\tt x)}
\\
\>{\tt +}\  {\tt x}\  {\tt 10)}
\\
{\tt (d}\={\tt efine}\  {\tt h(x)}\  
\\
\>{\tt (+}\  {\tt x}\  {\tt 10))}
\end{tabbing}\end{minipage}\end{center}
Read the error messages, fix the offending definition in an appropriate
manner, and repeat until all definitions are legal.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/syn-error-def.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}



\noindent{\bf Run-time Errors:} The evaluation of Scheme expressions
proceeds according to the intuitive laws of algebra and arithmetic. When we
encounter new operations, we will extend these laws, first intuitively and
then in section~\ref{sec:synsem} rigorously. For now, it is more important
to understand that not all legal Scheme expressions have a result. One
obvious example is \relax\relax {{\tt (/}\ {\tt 1}\ {\tt 0)}\/}. Similarly, if we define
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt n)}\\
\>{\tt (+}\  {\tt (/}\  {\tt n}\  {\tt 3)}\  {\tt 2))}
\end{tabbing}\end{minipage}\end{center}
we cannot ask DrScheme to evaluate {{\tt (f}\ {\tt 5}\ {\tt 8)}\/}. 

When the evaluation of a legal Scheme expression demands a division by zero
or similarly nonsensical arithmetic operations, or when a program is
applied to the wrong number of inputs, DrScheme stops the evaluation and
signals a {\sc {run-time error}}. Typically it prints an explanation in
the {\tt Interactions} window and highlights the faulty expression. The
highlighted expression triggered the error signal.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 2.4.3}

 \label{ex:sem-error} \label{ex:sem-error-r}
Evaluate the following grammatically legal Scheme expressions in DrScheme's
{\tt Interactions} window:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (+}\  {\tt 5}\  {\tt (/}\  {\tt 1}\  {\tt 0))}\\
{\tt (sin}\  {\tt 10}\  {\tt 20)}
\\
{\tt (somef}\  {\tt 10)}
\end{tabbing}\end{minipage}\end{center}
Read the error messages.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sem-error.html}{\rule{3pt}{5pt}}


{\bf Exercise 2.4.4}

 \label{ex:sem-error2}
Enter the following grammatically legal Scheme program into the
{\tt Definitions} window and click the {\tt Execute} button: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (somef}\  {\tt x)}\\
\>{\tt (sin}\  {\tt x}\  {\tt x))}
\end{tabbing}\end{minipage}\end{center}
Then, in the {\tt Interactions} window, evaluate the expressions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (somef}\  {\tt 10}\  {\tt 20)}\\
{\tt (somef}\  {\tt 10)}
\end{tabbing}\end{minipage}\end{center}
and read the error messages. Also observe what DrScheme
highlights.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sem-error2.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}



\noindent{\bf Logical Errors:} A good programming environment assists the
programmer in finding syntax and runtime errors. The exercises in this
section illustrate how DrScheme's catches syntax and run-time errors. A
programmer, however, can also make {\sc {logical errors}}. A logical
mistakes does not trigger any error messages; instead, the program computes
incorrect results. Consider the {{\tt wage}\/} program from the preceding
section. If the programmer had accidentally defined it as 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (wage}\  {\tt h)}\\
\>{\tt (+}\  {\tt 12}\  {\tt h))}
\end{tabbing}\end{minipage}\end{center}
the program would still produce a number every time it is used. Indeed, if
we evaluate {{\tt (wage}\ {\tt 12/11)}\/}, we even get the correct result. A
programmer can only catch such mistakes by designing programs carefully and
systematically.

\subsection{Designing Programs} \label{sec:design1} \htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:design1}

The preceding sections show that the development of a program requires many
different steps. We need to determine what's relevant in the problem
statement and what we can ignore. We need to understand what the program
consumes, what it produces, and how it relates inputs to outputs. We must
know, or find out, whether Scheme provides certain basic operations for the
data that our program is to process. If not, we might have to develop
auxiliary programs that implement these operations. Finally, once we have a
program, we must check whether it actually performs the intended
computation. This might reveal syntax errors, run-time problems, or even
logical errors.

To bring some order to this apparent chaos, it is best to set up and to
follow a {\sc {design recipe}}, that is, a step-by-step prescription of
what to do next and in what order we should do things. Based on what we
have experienced thus far, the development of a program requires at least
the following four activities:
\begin{description}
\item[Understanding the Program's Purpose:] The goal of designing a
program is to create a mechanism that consumes and produces data. We
therefore start every program development by giving the program a
meaningful name and by stating what kind of information it consumes and
produces. We call this a {\sc {contract}}. 

Here is how we write down a contract for {{\tt area-of-ring}\/}, one of our
first programs:\footnote{An arrow is keyed in as {\tt -} followed by {\tt >}.}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt area-of-ring}\ {\tt :}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\end{tabbing}\end{minipage}\end{center}

The semicolons indicate that this line is a {\sc {comment}}. The
contract consists of two parts. The first, to the left of the colon, states
the program's name. The second, to the right of the colon, specifies what
kind of data the program consumes and what it produces; the inputs are
separated from the output by an arrow.

Once we have a contract, we can add the {\sc {header}}. The program
header re-states the program's name and gives each input a distinct
name. These names are (algebraic) variables and are referred to as the
program's {\sc {parameters}}.\footnote{Others also call them {\sl formal arguments} or {\sl input variables}.}

Let's take a look at the contract and header for {{\tt area-of-ring}\/}: 

\begin{minipage}[t]{8in}\begin{tabbing}\  \  \=\hbox{;; {{\tt area-of-ring}\ {\tt :}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\>{\tt (define}\  {\tt (area-of-ring}\  {\tt outer}\  {\tt inner)}\  {\tt ...)}
\end{tabbing}\end{minipage}

It says that we will refer to the first input as {{\tt outer}\/} and the
second one as {{\tt inner}\/}.

Finally, using the contract and the parameters, we should formulate a short
{\sc {purpose statement}} for the program, that is, a brief comment of
{\em what\/} the program is to compute. For most of our programs, one or
two lines will suffice; as we develop larger and larger programs, we may
need to add more information to explain a program's purpose.

Here is the complete starting-point for our running example: 

\begin{minipage}[t]{8in}\begin{tabbing}\  \  \=\hbox{;; {{\tt area-of-ring}\ {\tt :}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\>\hbox{;; to compute the area of a ring whose radius is}
\\
\>\hbox{;; {{\tt outer}\/} and whose hole has a radius of {{\tt inner}\/}}
\\
\>{\tt (define}\  {\tt (area-of-ring}\  {\tt outer}\  {\tt inner)}\  {\tt ...)}
\end{tabbing}\end{minipage}

  \noindent{\bf Hints:} \ If the problem statement provides a mathematical formula, the
  number of distinct variables in the formula suggests how many inputs
  the program consumes.

  For other word problems, we must inspect the problem to separate the
  given facts from what is to be computed.  If a given is a fixed number,
  it shows up in the program. If it is an unknown number, that is to be
  fixed by someone else later, it is an input.  The question (or the
  imperative) in the problem statement suggests a name for the program.

\htmladdimg{../icons/teacher.gif}~{\em Initially, your students should write down example as comments,  following the contract and purpose statement; the tests should {\em  always\/} be added to the bottom of the {\tt definitions} window and  should be derived from the examples.  Later, as students gain more  confidence, you may wish to allow them to formulate the examples as tests  and to add them to the bottom immediately.}

\item[Program Examples:] To gain a better understanding of what the program
should compute, we make up examples of inputs and determine what the output
should be. For example, {{\tt area-of-ring}\/} should produce {{\tt 50.24}\/}
for the inputs {{\tt 5}\/} and {{\tt 3}\/}, because it is the difference
between the area of the outer disk and the area of the inner disk. 

We add examples to the purpose statement:

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \=\hbox{;; {{\tt area-of-ring}\ {\tt :}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\>\hbox{;; to compute the area of a ring whose radius is}
\\
\>\hbox{;; {{\tt outer}\/} and whose hole has a radius of {{\tt inner}\/}}
\\
\>\hbox{;; example: {{\tt (area-of-ring}\ {\tt 5}\ {\tt 3)}\ {\tt =}\ {\tt 50.24}\/}}
\\
\>{\tt (define}\  {\tt (area-of-ring}\  {\tt outer}\  {\tt inner)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

Making up examples---{\bf before we write down the program's body}---helps
in many ways.  First, it is the only sure way to discover logical errors
with testing. If we use the finished program to make up examples, we are
tempted to trust the program because it so much easier to run the program
than to predict what it does. Second, examples force us to think through the
computational process, which, for the complicated cases we will encounter
later, is critical to the development of the function body. Finally,
examples illustrate the informal prose of a purpose statement. Future
readers of the program, such as teachers, colleagues, or buyers, greatly
appreciate illustrations of abstract concepts. 

\item[The Body:] Finally, we must formulate the program's body. That is,
{\bf we must replace the ``\ldots'' in our header with a expression}. The
expression computes the answer from the parameters, using Scheme's basic
operations and Scheme programs that we already {{\tt define}\/}d or intend
to {{\tt define}\/}.

We can only formulate the program's body if we understand how the program
computes the output from the given inputs. If the input-output relationship
is given as a mathematical formula, we just translate mathematics into
Scheme. If, instead, we are given a word problem, we must craft the
expression carefully. To this end, it is helpful to revisit the examples
from the second step and to understand {\em how\/} we computed the outputs
for specific inputs.

In our running example, the computational task was given via an informally
stated formula that re-used {{\tt area-of-disk}\/}, a previously
{{\tt define}\/}d program. Here is the translation into Scheme: 

\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (d}\={\tt efine}\  {\tt (area-of-ring}\  {\tt outer}\  {\tt inner)}\\
\>{\tt (-}\  \={\tt (area-of-disk}\  {\tt outer)}
\\
\>\>{\tt (area-of-disk}\  {\tt inner)))}
\end{tabbing}\end{minipage}

\item[Testing:] After we have completed the program definition, we must
still test the program. At a minimum, we should ensure that the program
computes the expected outputs for the program examples. To facilitate
testing, we may wish to add the examples to the bottom of the
{\tt Definitions} window as if they were equations. Then, when we click
the {\tt Execute} button, they are evaluated, and we see whether the
program works properly on them.

Testing cannot show that a program produces the correct outputs for all
possible inputs---because there are typically an infinite number of
possible inputs. But testing can reveal syntax errors, run-time problems,
and logical mistakes.

For faulty outputs, we must pay special attention to our program
examples. It is possible that the examples are wrong; that the program
contains a logical mistake; or that both the examples and the program are
wrong. In either case, we may have to step through the entire program
development again.
\end{description}
Figure~\ref{fig:recipe1-example} summarizes what we see after
we have developed the program according to our recipe;
figure~\ref{fig:design1} summarizes the recipe in tabular form.  It should
be consulted whenever we design a program.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline Contract}: {{\tt area-of-ring}\ {\tt :}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; {\underline Purpose}: to compute the area of a ring whose radius is}
\\
\hbox{;; {{\tt outer}\/} and whose hole has a radius of {{\tt inner}\/}}
\\
\hbox{;; {\underline Example}: {{\tt (area-of-ring}\ {\tt 5}\ {\tt 3)}\ {\tt =}\ {\tt 50.24}\/}}
\\
\hbox{;; {\underline Definition}: [refines the header]}
\\
{\tt (d}\={\tt efine}\  {\tt (area-of-ring}\  {\tt outer}\  {\tt inner)}
\\
\>{\tt (-}\  \={\tt (area-of-disk}\  {\tt outer)}
\\
\>\>{\tt (area-of-disk}\  {\tt inner)))}
\\
\>
\\
\hbox{;; {\underline Tests}:}
\\
{\tt (area-of-ring}\  {\tt 5}\  {\tt 3)}\  {\tt =}\  {\tt 50.24}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The design recipe: a complete example} \label{fig:recipe1-example}
\hrule

\htmladdimg{../icons/teacher.gif}~{\em None of these skills are computer-specific; all of them are needed to  solve all kinds of problems. Even lawyers and doctors, artisans and  secretaries can benefit from laying out their work along these lines.}

%% BEGIN INLINED FILE: idesign1.dl.tex
%% BEGIN INLINED FILE: design1.dl.tex






\hrule
\begin{center} \begin{tabular}{|l|l|l|}\hline Phase            &  Goal 		& Activity \\
 \hline\hline \begin{minipage}[t]{.60in} Contract\\
 Header~\&\\
 Purpose\\
 \end{minipage} 	&    \begin{minipage}[t]{1.6in}\raggedright to name the program,\\
  	 to specify its inputs \& \\
~~outputs,\\
 	 to describe its purpose,\\
 	 to formulate a program \\
~~header  \rule{0pt}{6pt}\end{minipage}  & \begin{minipage}[t]{2.2in}\raggedright { choose a {\it name\/} that fits the problem  $\bullet$\  study the problem for clues on how many numbers the function consumes\\
 \medskip $\bullet$\  pick one variable per input; if possible, use names that are mentioned for   unknown ``givens'' in the problem statement\\
 \medskip $\bullet$\  describe what the function should produce using the chosen variables names\\
 \medskip $\bullet$\  formulate the contract \& header: \\
    ~;; {\it name\/} : {\it number\/} \ldots $\rightarrow$ {\it number\/} \\
   ~;; {\bf Purpose}: to compute \ldots\ {\it x1\/} \ldots \\
   ~({\bf define} ({\it name x1 \ldots\/}) \ldots) }\rule{0pt}{6pt}\end{minipage} \\
 \hline Examples 	& \begin{minipage}[t]{1.6in}\raggedright to characterize the input-\\
 output relationship via examples \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {search the problem for examples $\bullet$\  add examples to function (input(s), output) $\bullet$\  validate the examples using the problem statement, if possible}\rule{0pt}{6pt}\end{minipage}   \\
 \hline Body	 	& \begin{minipage}[t]{1.6in}\raggedright to define the program \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {formulate how the function computes its results $\bullet$\  develop a Scheme expression that uses Scheme's primitive operations, other functions, and the variables $\bullet$\  translate the mathematical expressions in the problem statement, if possible}\rule{0pt}{6pt}\end{minipage}   \\
 \hline Test		& \begin{minipage}[t]{1.6in}\raggedright to expose mistakes\\
~~(``typos'' and logic) \rule{0pt}{6pt}\end{minipage} & \begin{minipage}[t]{2.2in}\raggedright {apply the function to the inputs of the examples  $\bullet$\  check that the outputs are as predicted above}\rule{0pt}{6pt}\end{minipage} \\
 \hline \end{tabular} \end{center}
\center{Figure: The design recipe at a glance} \label{fig:design1} 
\hrule

The design recipe is not a magic bullet for the problems we encounter
  during the design of a program. It provides some guidance for process
  that can often appear to be overwhelming. The most creative and most
  difficult step in our recipe concerns the design of the program's
  body. At this point, it heavily relies on our ability to read and
  understand written material, on our ability to extract mathematical
  relationships, and on our knowledge of basic facts. None of these skills
  are specific to the development of computer programs; the knowledge we
  exploit is specific to the domain of information with which we are
  working. The remainder of the book will show what and how much computing
  can contribute to this most complicated step. 

\medskip\noindent{\bf Domain Knowledge}:\  Formulating the body of a program often requires
 knowledge about the area, also known as domain, from which the problem is
 drawn. This form of knowledge is called {\sc {domain knowledge}}. It may
 have to be drawn from simple mathematics, such as arithmetic, from complex
 mathematics, such as differential equations, or from non-mathematical
 disciplines: 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. The language is often that of mathematics, but in some cases, the
 programmers must invent a language, especially a data language for the
 application area. For that reason, it is imperative that programmers have
 a solid understanding of the full possibilities of computer languages.~{\rule{3pt}{5pt}}

\section{Programs are Function and Variable Definitions} \label{sec:program+functions}

In general, a program consists not just of one, but of many definitions.
The {{\tt area-of-disk}\/} program, for example, consists of two
definitions: the one for {{\tt area-of-ring}\/} and another one for
{{\tt area-of-disk}\/}. We refer to both as {\sc {function definition}}s
and, using mathematical terminology in a loose way, say that the program
is {\sc {composed}} of several functions. Because first one,
{{\tt area-of-ring}\/}, is the function we really wish to use, we refer to
it as the {\sc {main function}}; the second one, {{\tt area-of-disk}\/},
is an {\sc {auxiliary function}}.

The use of auxiliary functions makes the design process manageable and
renders programs readable.  Compare the following two versions of
{{\tt area-of-ring}\/}:
\begin{center}
\begin{minipage}[b]{2.3in}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (area-of-ring}\  {\tt outer}\  {\tt inner)}\  \\
\>{\tt (-}\  \={\tt (area-of-disk}\  {\tt outer)}
\\
\>\>{\tt (area-of-disk}\  {\tt inner)))}
\end{tabbing}\end{minipage}
\end{minipage}\hfil\begin{minipage}[b]{2.3in}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (area-of-ring}\  {\tt outer}\  {\tt inner)}\  \\
\>{\tt (-}\  \={\tt (*}\  {\tt 3.14}\  {\tt (*}\  {\tt outer}\  {\tt outer))}
\\
\>\>{\tt (*}\  {\tt 3.14}\  {\tt (*}\  {\tt inner}\  {\tt inner))))}
\end{tabbing}\end{minipage}
\end{minipage}
\end{center}
The definition on the left composes auxiliary functions. Designing it
helped us break up the original problem into smaller, more easily
solvable problems. Reading it reminds us of our reasoning that the area
is the difference between the area of the full disk and the area of the
hole. In contrast, the definition on the right requires a reader to
reconstruct the idea that the two subexpressions compute the area of two
disks. Furthermore, we would have had to produce the right definition in
one monolithic block, without benefit of dividing the problem solving
process into smaller steps.

For a small program such as {{\tt area-of-ring}\/}, the differences between
the two styles are minor. For large programs, however, using auxiliary
functions is not an option but a necessity.  That is, even if we are asked
to write a single program, we should consider breaking it up into several
small programs and {\sc {composing}} them as needed. Although we are not
yet in a position to develop truly large programs, we can still get a
feeling for the idea by developing two versions in parallel. 

The first subsection contrasts the two development styles with an example
from the business domain. It demonstrates how breaking up a program into
several function definitions can greatly increase our confidence in the
correctness of the overall program. The second subsection introduces the
concept of a variable definition, which is an additional important
ingredient for the development of programs.  The last subsection proposes
some exercises.

\subsection{Composing Functions} \label{sec:basics-compose} 

Consider the following problem: 
\begin{quote}
 Imagine the owner of a movie theater who has complete freedom in setting
 ticket prices. The more he charges, the fewer the people who can afford
 tickets. In a recent experiment the owner determined a precise
 relationship between the price of a ticket and average attendance. At a
 price of \$5.00 per ticket, 120 people attend a performance. Decreasing
 the price by a dime (\$.10) increases attendance by 15. Unfortunately,
 the increased attendance also comes at an increased cost. Every
 performance costs the owner \$180. Each attendee costs another four
 cents (\$0.04). The owner would like to know the exact relationship
 between profit and ticket price so that he can determine the price at
 which he can make the highest profit.
\end{quote}
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. 

When we are confronted with such a situation, it is best to tease out the
 various dependencies one at a time:
\begin{enumerate}
\item {\sl Profit\/} is the difference between revenue and costs. 

\item The {\sl revenue\/} is exclusively generated by the sale of
  tickets. It is the product of ticket price and number of attendees.

\item The {\sl costs\/} consists of two parts: a fixed part (\$180) and a
 variable part that depends on the number of attendees. 

\item Finally, the problem statement also specifies how the number of
 attendees depends on the ticket price.
\end{enumerate}
Let's formulate a function for each of these dependencies; after all,
functions compute how quantities depend on each other. 

We start with contracts, headers, and purpose statements. Here is the one
for {{\tt profit}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt profit}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the profit as the difference between revenue and costs}
\\
\hbox{;; at some given {{\tt ticket-price}\/}}
\\
{\tt (define}\  {\tt (profit}\  {\tt ticket-price)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
 It depends on the ticket price because both revenue and cost depend on
 the ticket price. Here are the remaining three: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt revenue}\ {\tt :}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the revenue, given {{\tt ticket-price}\/} }
\\
{\tt (define}\  {\tt (revenue}\  {\tt ticket-price)}\  {\tt ...)}
\\
\hbox{;; {{\tt cost}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to compute the costs, given {{\tt ticket-price}\/}}
\\
{\tt (define}\  {\tt (cost}\  {\tt ticket-price)}\  {\tt ...)}
\\
\hbox{;; {{\tt attendees}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to compute the number of attendees, given {{\tt ticket-price}\/}}
\\
{\tt (define}\  {\tt (attendees}\  {\tt ticket-price)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
 Each purpose statement is a rough transliteration of some part of the
 problem statement.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 

{\bf Exercise 3.1.1}

 \label{ex:movie-examples}
The next step is to make up examples for each of the function. Determine
how many attendees can afford a show at a ticket price of \$3.00, \$4.00,
and \$5.00. Use the examples to formulate a general rule that shows how
to compute the number of attendees from the ticket price. Make up more
examples if needed.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/movie-examples.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 3.1.2}

 \label{ex:movie-examples2}
Use the results of exercise~\ref{ex:movie-examples} to determine how much
it costs to run a show at \$3, \$4, and \$5. Also determine how much
revenue each show produces at those prices. Finally, figure out how much
profit the monopolistic movie owner can make with each show. Which is the
best price (of these three) for maximizing the
profit?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/movie-examples2.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}
 

Once we have written down the basic material about our functions and
calculated out several examples, we can replace the ``\ldots'' with
Scheme expressions. The left column of figure~\ref{fig:profit} contains
complete definitions of all four functions. The {{\tt profit}\/} function
computes its result as the difference between the result of
{{\tt revenue}\/} and {{\tt cost}\/}, just as the problem analysis and
purpose statement suggest. The computation of both depends on
{{\tt ticket-price}\/}, which is what the applications say. To compute the
revenue, we first compute the number of attendees for the given
{{\tt ticket-price}\/} and multiply it with
{{\tt ticket-price}\/}. Similarly, to compute the cost we add the fixed
portion of the cost to the variable part, which is the product of the
number of attendees and {{\tt 0.04}\/} (four cents). Finally, the
computation of the number of attendees also follows the problem
statement. The base attendance at a price of five dollars is 120, and for
each 15 cents less than five dollars, 10 more attendees show up. 

\hrule
\begin{center}
\begin{minipage}[b]{2.5in}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (profit}\  {\tt ticket-price)}\\
\>{\tt (-}\  \={\tt (revenue}\  {\tt ticket-price)}
\\
\>\>{\tt (cost}\  {\tt ticket-price)))}
\\
\relax
\\
{\tt (d}\={\tt efine}\  {\tt (revenue}\  {\tt ticket-price)}
\\
\>{\tt (*}\  \  {\tt (attendance}\  {\tt ticket-price)}\  {\tt ticket-price))}
\\
\relax
\\
{\tt (d}\={\tt efine}\  {\tt (cost}\  {\tt ticket-price)}
\\
\>{\tt (+}\  \={\tt 180}\  
\\
\>\>{\tt (*}\  {\tt .04}\  {\tt (attendance}\  {\tt ticket-price))))}
\\
\relax
\\
{\tt (d}\={\tt efine}\  {\tt (attendance}\  {\tt ticket-price)}
\\
\>{\tt (+}\  \={\tt 120}
\\
\>\>{\tt (*}\  {\tt (/}\  {\tt 15}\  {\tt .10)}\  {\tt (-}\  {\tt 5.00}\  {\tt ticket-price))))}
\end{tabbing}\end{minipage}
\end{minipage}~~\rule{.5pt}{2.3in}~~\begin{minipage}[b]{2.3in}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (profit}\  {\tt price)}\\
\>{\tt (-}\  \={\tt (*}\  \={\tt (+}\  \={\tt 120}
\\
\>\>\>\>{\tt (*}\  \={\tt (/}\  {\tt 15}\  {\tt .10)}
\\
\>\>\>\>\>{\tt (-}\  {\tt 5.00}\  {\tt price)))}
\\
\>\>\>{\tt price)}
\\
\>\>{\tt (+}\  \={\tt 180}\  
\\
\>\>\>{\tt (*}\  \={\tt .04}
\\
\>\>\>\>{\tt (+}\  \={\tt 120}
\\
\>\>\>\>\>{\tt (*}\  \={\tt (/}\  {\tt 15}\  {\tt .10)}
\\
\>\>\>\>\>\>{\tt (-}\  {\tt 5.00}\  {\tt price)))))))}
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\end{tabbing}\end{minipage}
\end{minipage}
\end{center}

\center{Figure: Two ways to express the {\it profit\/} program} \label{fig:profit}
\hrule

Instead of developing a function per dependency in the problem statement,
we could have tried to express the relationship between the ticket price
and the owner's profit in a single function. The right column in
figure~\ref{fig:profit} shows the most straightforward way of doing so. And
indeed, it is easy to check that the two profit programs in
figure~\ref{fig:profit} produce the same profit when given the same ticket
price. Still, it is also obvious that while the arrangement on the left
conveys the intention behind the program directly, the program on the right
is nearly impossible to understand. Worse, if we are asked to modify some
aspect of the program, say, the relationship between the number of
attendees and the price of the ticket, we can do this for the left column
in a small amount of time, but we need to spend a much longer time for the
right one.

Based on our experience, we thus formulate the first and most important
guideline of programming:

   \begin{rawhtml}     <table bgcolor=red align=center>       <tr><td><font align=center color=white size=+3>   \end{rawhtml} Guideline on Auxiliary Functions   \begin{rawhtml}     <tr><td><font color=white><p>   \end{rawhtml} Formulate auxiliary function definitions for  every dependency between quantities mentioned in the problem statement or  discovered with example calculations.    \begin{rawhtml}     </p></table>   \end{rawhtml} 

\noindent Sometimes we will find that some of the required functions are
already available as programs for other problems. Indeed, we have already
encountered such an example: {{\tt area-of-disk}\/}.  At other times, we
will make a list of functions and develop each one separately. We may
then find that some of the functions, such as {{\tt attendees}\/}, are
useful in several other definitions, leading to a network-like
relationship among functions.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 3.1.3}

 \label{ex:profit-eval}
Determine the profit that the movie owner makes at \$3.00, \$4.00, and
\$5.00 using the program definitions in the both columns column. Make
sure that the results are the same as those predicted in
exercise~\ref{ex:movie-examples2}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/profit-eval.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 3.1.4}

 \label{ex:profit-comp}
After studying the cost structure of a show, the owner discovered several
ways of lowering the cost. As a result of his improvements, he no longer
has a fixed cost. He now simply pays \$1.50 per attendee.  

Modify both programs to reflect this change. When the programs are
modified, test them again with ticket prices of \$3.00, \$4.00, and
\$5.00 and compare the results.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/profit-comp.html}{\rule{3pt}{5pt}}
 

\subsection{Variable Definitions} \label{sec:use-var-defs}

When a number occurs many times in our program(s), we should give it a name
using a {\sc {variable definition}}, which associates a name with a
value. One example is {{\tt 3.14}\/}, which we have used in place of
$\pi$. Here is how we could give this number a name:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt PI}\  {\tt 3.14)}\end{tabbing}\end{minipage}\end{center}
Now, every time we refer to {{\tt PI}\/}, DrScheme replaces it with
{{\tt 3.14}\/}. 

Using a name for a constant makes it easier to replace it with a different
 value. Suppose our program contains the definition for {{\tt PI}\/}, and we
 decide that we need a better approximation of $\pi$ for the entire
 program. By changing the definition to
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt PI}\  {\tt 3.14159)}\end{tabbing}\end{minipage}\end{center}
 the improvement is used everywhere where we use {{\tt PI}\/}.  If we didn't
 have a name like {{\tt PI}\/} for $\pi$, we would have to find and all
 instances of {{\tt 3.14}\/} in the program and replace them with
 {{\tt 3.14159}\/}.

Let us formulate this observation as our second guideline:

   \begin{rawhtml}     <table bgcolor=red align=center>       <tr><td><font align=center color=white size=+3>   \end{rawhtml} Guideline on Variable Definitions   \begin{rawhtml}     <tr><td><font color=white><p>   \end{rawhtml} Give names to frequently used constants  and use the names instead of the constants in programs.    \begin{rawhtml}     </p></table>   \end{rawhtml} 

\noindent Initially, we won't use many variable definitions for
 constants, because our programs are small. But, as we learn to write
 larger programs, we will make more use of variable definitions. As we will
 see, the ability to have a single point of control for changes is
 important for variable and function definitions. 

\htmladdimg{../icons/teacher.gif}~{\em  Variable definitions are also useful to build spreadsheets in DrScheme.  Consider the following example:  \begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; A Lemonade Stand}\\
{\tt (define}\  {\tt quantity}\  {\tt 100)} \\
{\tt (define}\  {\tt price}\  {\tt .15)} \\
{\tt (define}\  {\tt var-cost}\  {\tt .07)} \\
{\tt (define}\  {\tt fixed-cost}\  {\tt 2.0)} \\
\hbox{;; ---------------------} \\
{\tt (define}\  {\tt gross}\  {\tt (*}\  {\tt quantity}\  {\tt price))} \\
{\tt (define}\  {\tt cost}\  {\tt (+}\  {\tt fixed-cost}\  {\tt (*}\  {\tt quantity}\  {\tt var-cost)))} \\
{\tt (define}\  {\tt net}\  {\tt (-}\  {\tt gross}\  {\tt cost))} \end{tabbing}\end{minipage}\end{center}  Each variable in the program is like a cell in a worksheet program, but our   ``cells'' have mnemonic variable names with a precise meaning.  In the   example, the variables above the line stand for the given numbers, and   the variables below the line define the dependencies. By editing a given,   we can examine various scenarios. For example, by changing   {{\tt (define}\ {\tt price}\ {\tt .15)}\/} to {{\tt (define}\ {\tt price}\ {\tt .20)}\/}, we can   examine the net profit for 20-cent glasses of lemonade instead of 15-cent   glasses.  Use a small number of such exercises if your students have difficulties understanding variables.   If there is demand, we will put up some exercises that Ms.\ Karen Buras used in her spread-sheet class.}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 3.2.1}

 \label{ex:profit-constants}
Provide variable definitions for all constants that appear in the profit
program of figure~\ref{fig:profit} and replace the constants with their
names.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/profit-constants.html}{\rule{3pt}{5pt}}


\subsection{Finger Exercises on Composing Functions}





{\bf Exercise 3.3.1}

 \label{ex:compose1}
The United States uses the {\em English\/} system of (length) measurements.
The rest of the world uses the {\em metric\/} system. So, people who travel
abroad and companies that trade with foreign partners often need to convert
English measurements to metric ones and {\it vice versa\/}. 

Here is a table that shows the six major units of length measurements of the
English system:\footnote{See {\em The World Book Encyclopedia \bf1993}, Weights and Measurements.}
\begin{center}
\begin{tabular}{lcrlcrl}
\multicolumn{4}{c}{English}		 & \multicolumn{2}{c}{metric} \\
 \hline
1 {\bf inch}    &   &            &       & = &  2.54 &{\rm cm} \\
1 {\bf foot}    & = & 12         & in.   \\
1 {\bf yard}    & = &  3         & ft.   \\
1 {\bf rod}     & = & 5$\frac12$ & yd.   \\
1 {\bf furlong} & = & 40         & rd.   \\
1 {\bf mile}    & = &  8         & fl.   \\
\end{tabular}
\end{center}
Develop the functions {\mbox{\it feet}{{}{\tt ->}{}}\mbox{\it inches}\/}, {\mbox{\it yards}{{}{\tt ->}{}}\mbox{\it inches}\/},
{\mbox{\it rods}{{}{\tt ->}{}}\mbox{\it cm}\/}, {\mbox{\it furlongs}{{}{\tt ->}{}}\mbox{\it cm}\/}, and {\mbox{\it miles}{{}{\tt ->}{}}\mbox{\it inches}\/}. 

Then develop the functions {\mbox{\it inches}{{}{\tt ->}{}}\mbox{\it cm}\/}, {\mbox{\it feet}{{}{\tt ->}{}}\mbox{\it cm}\/},
{\mbox{\it yards}{{}{\tt ->}{}}\mbox{\it cm}\/}, {\mbox{\it rods}{{}{\tt ->}{}}\mbox{\it cm}\/}, {\mbox{\it furlongs}{{}{\tt ->}{}}\mbox{\it cm}\/}, and
{\mbox{\it miles}{{}{\tt ->}{}}\mbox{\it cm}\/}. 

\noindent{\bf Hint:}  Reuse functions as much as possible. Use variable definitions to
specify constants.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/compose1.html}{\rule{3pt}{5pt}}


{\bf Exercise 3.3.2}

 \label{ex:compose-cylinder}
Develop the program {{\tt volume-cylinder}\/}. It consumes the radius of a
cylinder's base disk and its height; it computes the volume of the
cylinder.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/compose-cylinder.html}{\rule{3pt}{5pt}}


{\bf Exercise 3.3.3}

 \label{ex:area-cylinder}
Develop {{\tt area-cylinder}\/}. The program consumes the radius of the
cylinder's base disk and its height. Its result is the surface area of the
cylinder.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/area-cylinder.html}{\rule{3pt}{5pt}}


{\bf Exercise 3.3.4}

 \label{ex:area-pipe}
Develop {{\tt area-pipe}\/}, which computes the surface area of a pipe. A
pipe is an open cylinder. The program consumes three values: the pipe's
inner radius, its length, and the thickness of its wall.

Develop two versions: a program that consists of a single definition and a
program that consists of several function definitions.  Which one invokes
more confidence?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/area-pipe.html}{\rule{3pt}{5pt}}



{\bf Exercise 3.3.5}

 \label{ex:compose-rocket}
Develop the program {{\tt height}\/}, which computes the height that a
rocket reaches in a given amount of time. If the rocket accelerates at 
a constant rate $g$, it reaches a speed of $g \cdot t$ in {{\tt t}\/} time
units and a height of $1/2 * v * t$ where $v$ is the speed at $t$.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/compose-rocket.html}{\rule{3pt}{5pt}}


{\bf Exercise 3.3.6}

 \label{ex:compose-f2c}
Recall the program {{\mbox{\it Fahrenheit\/}{{}{\tt ->}{}}\mbox{\it Celsius\/}}\/} from
 exercise~\ref{ex:f2c}. The program consumes a temperature measured in
 Fahrenheit and produces the Celsius equivalent. 

Develop the program {{\mbox{\it Celsius\/}{{}{\tt ->}{}}\mbox{\it Fahrenheit\/}}\/}, which consumes a
 temperature measured in Celsius and produces the Fahrenheit equivalent.

Now consider the function 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt I}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to convert a Fahrenheit temperature to Celsius and back }
\\
{\tt (d}\={\tt efine}\  {\tt (I}\  {\tt f)}
\\
\>{\tt (}{\mbox{\it Celsius\/}{{}{\tt ->}{}}\mbox{\it Fahrenheit\/}}\  {\tt (}{\mbox{\it Fahrenheit\/}{{}{\tt ->}{}}\mbox{\it Celsius\/}}\  {\tt f)))}
\end{tabbing}\end{minipage}\end{center}
Evaluate {{\tt (I}\ {\tt 32)}\/} by hand and using DrScheme's stepper. What does
 this suggest about the composition of the two
 functions?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/compose-f2c.html}{\rule{3pt}{5pt}}
 

\section{Conditional Expressions and Functions} 
\label{sec:cond} 

For many problems, computer programs must deal with different situations in
different ways. A game program may have to determine whether an object's
speed is in some range or whether it is located in some specific area of
the screen. For an engine control program, a condition may describe whether
or when a valve is to be opened. To deal with conditions, we need to have a
way of saying a condition is true or false; we need a new class of values,
which, by convention, is called {\sc {Boolean}} (or truth) values. This
section introduces Booleans, expressions that evaluate to Booleans, and
expressions that compute values depending on the Boolean result of some
evaluation. 

\htmladdimg{../icons/teacher.gif}~{\em Use the teachpack {\tt\bf pingp-play.ss} and have your students play  the ping-pong game. To implement this kind of game, we need to conditional  computing (is the ball at the boundary? is the ball on the paddle?) and  compound data (the ball's position, the ball's velocity are data that have  two components). Let's study these things in Scheme.}

\subsection{Booleans and Relations} 
\label{sec:cond:booleans} 
\htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:cond:booleans}

Consider the following problem statement: 
\begin{quote}
  Company XYZ \& Co.\ pays all its employees \$12 per hour. A typical
  employee works between 20 and 65 hours per week. Develop a program that
  determines the wage of an employee from the number of hours of work, {\em   if the number is within the proper range\/}.
\end{quote}
The italic words highlight the new part (compared to
section~\ref{sec:basics-word-probs}). They imply that the program must deal
with its input in one way if it is in the legitimate range, and in a
different way if it is not. In short, just as people need to reason about
conditions, programs must compute in a conditional manner.

Conditions are nothing new. In mathematics we talk of true and false
claims, which are conditions.  For example, a number may be equal to, less
than, or greater than some other number. If $x$ and $y$ are numbers, we
state these three claims about $x$ and $y$ with
\begin{enumerate}
\item  $x = y$: \mbox{``$x$ is equal to $y$''};
\item  $x < y$: \mbox{``$x$ is strictly less than $y$''};
\item  $x > y$: \mbox{``$x$ is strictly greater than $y$''}.
\end{enumerate}
For any specific pair of (real) numbers, exactly one of these claims
holds. If $x=4$ and $y=5$, the second claim is a true statement, and the
others are false. If $x=5$ and $y=4$, however, the third claim is true, and
the others are false. In general, a claim is true for some values of the
variables and false for others.

In addition to determining whether an individual claim holds in a given
situation, it is sometimes important to determine whether combinations of
claims hold. Consider the three claims above, which we can combine in
several ways:
\begin{enumerate}
\item $x = y \quad \mbox{ and } \quad x < y \quad \mbox{ and } \quad x > y$
\item $x = y \quad \mbox{ or } \quad x < y \quad \mbox{ or } \quad x > y$
\item $x = y \quad \mbox{ or } \quad x < y \ . $
\end{enumerate}
The first compound claim is false because no matter what numbers we pick
for {\it x\/} and {\it y\/}, two of the three claims are false.  The second
compound claim, however, always holds no matter what numbers we pick for
$x$ and~$y$. Finally, the third kind of compound claim is the most
important of all, because it is true in some cases and false in others. For
example, it holds when $x = 4$, $y = 4$ and $x = 4$, $y = 5$, but is false
if $x = 5$ and $y = 3$.

Like mathematics, Scheme has ``words'' for expressing truth and falsity,
for stating individual claims, for combining claims into compound claims,
and for expressing that a claim is true or false. The ``word'' for true is
{{\sf true}\/} and the ``word'' for false is {{\sf false}\/}. If a claim concerns
the relationship between two numbers, it can typically be expressed with a
{\sc {relational operation}}, for example, {{\tt =}\/}, {{\tt <}\/}, and
{{\tt >}\/}. 

Translating the three mathematical claims from above follows our well-known
pattern of writing left parenthesis, followed by the operator, its
arguments, and a right parenthesis: 
conditions: 
\begin{enumerate}
\item {{\tt (=}\ {\tt x}\ {\tt y)}\/}: \mbox{``$x$ is equal to $y$''};
\item {{\tt (<}\ {\tt x}\ {\tt y)}\/}: \mbox{``$x$ is strictly less than $y$''}; and 
\item {{\tt (>}\ {\tt x}\ {\tt y)}\/}: \mbox{``$x$ is strictly greater than $y$''}. 
\end{enumerate}
We will also encounter {{\tt <=}\/} and {{\tt >=}\/} as relational operators.

A Scheme expression that compares numbers has a result just like any other
Scheme expression. The result, however, is {{\sf true}\/} or {{\sf false}\/}, not
a number.  That is, when an individual Scheme claim about two numbers is
true, it evaluates to {{\sf true}\/}. For example,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (<}\  {\tt 4}\  {\tt 5)}\  \\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
Similarly, a false claim evaluates to {{\sf false}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (=}\  {\tt 4}\  {\tt 5)}\  \\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/teacher.gif}~{\em While many algebra texts do not discuss the evaluation of such  comparison claims, a thorough understanding of a programming notation  requires an understanding of such evaluations.}

Expressing compound conditions in Scheme is equally natural. Suppose we want to
combine {{\tt (=}\ {\tt x}\ {\tt y)}\/} and {{\tt (<}\ {\tt y}\ {\tt z)}\/} so that the compound claim
holds if both conditions are true. In Scheme we would write
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  {\tt (=}\  {\tt x}\  {\tt y)}\  {\tt (<}\  {\tt y}\  {\tt z))}\end{tabbing}\end{minipage}\end{center}
to express this relationship. Similarly, if we wanted to formulate a
compound condition that is true if (at least) one of the two conditions
holds, we would use
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (or}\  {\tt (=}\  {\tt x}\  {\tt y)}\  {\tt (<}\  {\tt y}\  {\tt z))}\end{tabbing}\end{minipage}\end{center}
By using {{\tt not}\/} as follows, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (not}\  {\tt (=}\  {\tt x}\  {\tt y))}\end{tabbing}\end{minipage}\end{center}
we can also state the negation of a claim.\footnote{In truth, the operations {{\tt and}\/} and {{\tt or}\/} are different from {{\tt not}\/}, which is why they are type-set in different fonts. We ignore the minor difference for now.}

Compound conditions, like individual ones, evaluate to {{\sf true}\/} or
{{\sf false}\/}. Consider the following: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  {\tt (=}\  {\tt 5}\  {\tt 5)}\  {\tt (<}\  {\tt 5}\  {\tt 6))}\end{tabbing}\end{minipage}\end{center}
This compound condition is made up of two individual ones: {{\tt (=}\ {\tt 5}\ {\tt 5)}\/}
and {{\tt (<}\ {\tt 5}\ {\tt 6)}\/}.  Both evaluate to {{\sf true}\/}, and therefore the
evaluation of the {{\bf and}-exp\-res\-sion} continues as follows:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt ...}\  \\
{\tt =}\  {\tt (and}\  {\sf true}\  {\sf true}{\tt )}
\end{tabbing}\end{minipage}\end{center}
If both parts of an {{\bf and}-exp\-res\-sion} are {{\sf true}\/}, the entire
expression evaluates to {{\sf true}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \  {\tt ...}\  \\
{\tt =}\  {\tt (and}\  {\sf true}\  {\sf true}{\tt )}
\\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
In contrast, if one of them is {{\sf false}\/}, the {{\bf and}-exp\-res\-sion}
evaluates to {{\sf false}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (and}\  {\tt (=}\  {\tt 5}\  {\tt 5)}\  {\tt (<}\  {\tt 5}\  {\tt 5))}\\
{\tt =}\  {\tt (and}\  {\sf true}\  {\sf false}{\tt )}
\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
The evaluation rules for {{\tt or}\/} and {{\tt not}\/} are similarly
intuitive.

As we get to know Scheme better, we will find other operations for stating
conditions about the relationship among different pieces of data. 


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 4.1.1}

 \label{ex:boolean1}
What are the results of the following Scheme conditions?
\begin{enumerate}
\item {{\tt (and}\ {\tt (>}\ {\tt 4}\ {\tt 3)}\ {\tt (<=}\ {\tt 10}\ {\tt 100))}\/}
\item {{\tt (or}\ {\tt (>}\ {\tt 4}\ {\tt 3)}\ {\tt (=}\ {\tt 10}\ {\tt 100))}\/}
\item {{\tt (not}\ {\tt (=}\ {\tt 2}\ {\tt 3))}\/}~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/boolean1.html}{\rule{3pt}{5pt}}
\end{enumerate}


{\bf Exercise 4.1.2}

 \label{ex:boolean1a}
What are the results of 
\begin{enumerate}
\item {{\tt (>}\ {\tt x}\ {\tt 3)}\/}
\item {{\tt (and}\ {\tt (>}\ {\tt 4}\ {\tt x)}\ {\tt (>}\ {\tt x}\ {\tt 3))}\/} 
\item {{\tt (=}\ {\tt (*}\ {\tt x}\ {\tt x)}\ {\tt x)}\/}
\end{enumerate}
for (a) {{\tt x}\ {\tt =}\ {\tt 4}\/}, (b) {{\tt x}\ {\tt =}\ {\tt 2}\/}, and (c) {{\tt x}\ {\tt =}\ {\tt 7/2}\/} \ ? \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/boolean1a.html}{\rule{3pt}{5pt}}


\subsection{Functions that Test Conditions} 
\label{sec:cond:prog} 


Here is a simple function that tests some condition about a number:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt is-5\char'077}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt n}\/} is equal to {{\tt 5}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (is-5\char'077}\  {\tt n)}
\\
\>{\tt (=}\  {\tt n}\  {\tt 5))}
\end{tabbing}\end{minipage}\end{center}
The function produces {{\sf true}\/} if, and only if, its input is equal to
{{\tt 5}\/}.  Its contract contains one novel element: the word
{{\tt boolean}\/}. Just like {{\tt number}\/}, {{\tt boolean}\/} represents a
class of values that is built into Scheme. Unlike {{\tt number}\/},
{{\tt boolean}\/} consists of just two values: {{\sf true}\/} and {{\sf false}\/}.

Here is a slightly more interesting function with a Boolean output: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt is-between-5-6\char'077}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt n}\/} is between 5 and 6 (exclusive)}
\\
{\tt (d}\={\tt efine}\  {\tt (is-between-5-6\char'077}\  {\tt n)}
\\
\>{\tt (and}\  {\tt (<}\  {\tt 5}\  {\tt n)}\  {\tt (<}\  {\tt n}\  {\tt 6)))}
\end{tabbing}\end{minipage}\end{center}
It consumes a number and produces {{\sf true}\/} if the number is between, but
does not include, {{\tt 5}\/} and {{\tt 6}\/}. One good way to understand the
function is to say that it describes the following interval on the number
line:

 \par\smallskip \hspace{.2cm}{\framebox[\numwidth]{ \begin{picture}(300,30)(0,-20)  
\put(0,0){\line(1,0){300}} \multiput(0,-5)(25,0){12}{\line(0,1){10}} 
\put(-03,-18){\mbox{$0$}} 
\put(123,-18){\mbox{$5$}} 
\put(245,-18){\mbox{$10$}}   
\put(125,-4){{\mbox{\hspace{-1pt}\LARGE(}}}  \multiput(125,1)(0,-.1){25}{\line(1,0){25}}  \invcnt=125 \advance \invcnt by 25   
\put(\invcnt,-4){{\mbox{\hspace{-4.5pt}\LARGE)}}}  \end{picture}}}

\bigskip

\medskip\noindent{\bf Boundaries}:\  An interval boundary marked with ``('' or ``)''
is excluded from the interval; an interval boundary marked with ``['' or
``]'' is included.~{\rule{3pt}{5pt}}

\bigskip

The following third function from numbers to Boolean values represents the
most complicated form of interval: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt is-between-5-6-or-over-10\char'077}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt n}\/} is between 5 and 6 (exclusive) }
\\
\hbox{;; or larger than or equal to {{\tt 10}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (is-between-5-6-or-over-10\char'077}\  {\tt n)}
\\
\>{\tt (or}\  {\tt (is-between-5-6\char'077}\  {\tt n)}\  {\tt (>=}\  {\tt n}\  {\tt 10)))}
\end{tabbing}\end{minipage}\end{center}
The function returns {{\sf true}\/} for two portions of the number line: 
 \par\smallskip \hspace{.2cm}{\framebox[\numwidth]{ \begin{picture}(300,30)(0,-20)  
\put(0,0){\line(1,0){300}} \multiput(0,-5)(25,0){12}{\line(0,1){10}} 
\put(-03,-18){\mbox{$0$}} 
\put(123,-18){\mbox{$5$}} 
\put(245,-18){\mbox{$10$}}     
\put(125,-4){{\mbox{\hspace{-1pt}\LARGE(}}}  \multiput(125,1)(0,-.1){25}{\line(1,0){25}}  \invcnt=125 \advance \invcnt by 25   
\put(\invcnt,-4){{\mbox{\hspace{-4.5pt}\LARGE)}}}   
\put(250,-4){{\mbox{\hspace{-1.5pt}\LARGE[}}}\multiput(250,1)(0,-.1){25}{\vector(+1,0){52}} \end{picture}}}

\noindent The left part of the interval is the portion between, but not
including, {{\tt 5}\/} and {{\tt 6}\/}; the right one is the infinite line
starting at, and including, {{\tt 10}\/}. Any point on those two portions of
the line satisfies the condition expressed in the function
{{\tt is-between-5-6-or-over-10\char'077}\/}.

All three functions test numeric conditions. To design or to comprehend such
functions, we must understand intervals and combinations of intervals. The
following exercises practice this important skill. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 4.2.1}

 \label{ex:interval} \label{ex:interval-r}
Translate the following five intervals on the real line into Scheme
functions that accept a number and return {{\sf true}\/} if the number is in
the interval and {{\sf false}\/} if it is outside:
\begin{enumerate}
\item the interval $(3,7]$:
 \par\smallskip \hspace{.2cm}{\framebox[\numwidth]{ \begin{picture}(300,30)(0,-20)  
\put(0,0){\line(1,0){300}} \multiput(0,-5)(25,0){12}{\line(0,1){10}} 
\put(-03,-18){\mbox{$0$}} 
\put(123,-18){\mbox{$5$}} 
\put(245,-18){\mbox{$10$}}   
\put(75,-4){{\mbox{\hspace{-1pt}\LARGE(}}}  \multiput(75,1)(0,-.1){25}{\line(1,0){100}}  \invcnt=75 \advance \invcnt by 100   
\put(\invcnt,-4){{\mbox{\hspace{-4pt}\LARGE]}}}  \end{picture}}}

\item the interval $[3,7]$:
 \par\smallskip \hspace{.2cm}{\framebox[\numwidth]{ \begin{picture}(300,30)(0,-20)  
\put(0,0){\line(1,0){300}} \multiput(0,-5)(25,0){12}{\line(0,1){10}} 
\put(-03,-18){\mbox{$0$}} 
\put(123,-18){\mbox{$5$}} 
\put(245,-18){\mbox{$10$}}   
\put(75,-4){{\mbox{\hspace{-1.5pt}\LARGE[}}}  \multiput(75,1)(0,-.1){25}{\line(1,0){100}}  \invcnt=75 \advance \invcnt by 100   
\put(\invcnt,-4){{\mbox{\hspace{-4pt}\LARGE]}}}  \end{picture}}}

\item the interval $[3,9)$:
 \par\smallskip \hspace{.2cm}{\framebox[\numwidth]{ \begin{picture}(300,30)(0,-20)  
\put(0,0){\line(1,0){300}} \multiput(0,-5)(25,0){12}{\line(0,1){10}} 
\put(-03,-18){\mbox{$0$}} 
\put(123,-18){\mbox{$5$}} 
\put(245,-18){\mbox{$10$}}   
\put(75,-4){{\mbox{\hspace{-1.5pt}\LARGE[}}}  \multiput(75,1)(0,-.1){25}{\line(1,0){150}}  \invcnt=75 \advance \invcnt by 150   
\put(\invcnt,-4){{\mbox{\hspace{-4.5pt}\LARGE)}}}  \end{picture}}}

\item the combination of $(1,3)$ and $(9,11)$:
 \par\smallskip \hspace{.2cm}{\framebox[\numwidth]{ \begin{picture}(300,30)(0,-20)  
\put(0,0){\line(1,0){300}} \multiput(0,-5)(25,0){12}{\line(0,1){10}} 
\put(-03,-18){\mbox{$0$}} 
\put(123,-18){\mbox{$5$}} 
\put(245,-18){\mbox{$10$}}     
\put(25,-4){{\mbox{\hspace{-1pt}\LARGE(}}}  \multiput(25,1)(0,-.1){25}{\line(1,0){50}}  \invcnt=25 \advance \invcnt by 50   
\put(\invcnt,-4){{\mbox{\hspace{-4.5pt}\LARGE)}}}     
\put(225,-4){{\mbox{\hspace{-1pt}\LARGE(}}}  \multiput(225,1)(0,-.1){25}{\line(1,0){50}}  \invcnt=225 \advance \invcnt by 50   
\put(\invcnt,-4){{\mbox{\hspace{-4.5pt}\LARGE)}}}  \end{picture}}}

\item and the range of numbers {\em outside\/} of $[1,3]$. 
 \par\smallskip \hspace{.2cm}{\framebox[\numwidth]{ \begin{picture}(300,30)(0,-20)  
\put(0,0){\line(1,0){300}} \multiput(0,-5)(25,0){12}{\line(0,1){10}} 
\put(-03,-18){\mbox{$0$}} 
\put(123,-18){\mbox{$5$}} 
\put(245,-18){\mbox{$10$}}   
\put(75,-4){{\mbox{\hspace{-1pt}\LARGE(}}}\multiput(75,1)(0,-.1){25}{\vector(+1,0){227}}  
\put(25,-4){{\mbox{\hspace{-4.5pt}\LARGE)}}}\multiput(25,1)(0,-.1){25}{\vector(-1,0){27}} \end{picture}}}

\end{enumerate}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/interval.html}{\rule{3pt}{5pt}}


{\bf Exercise 4.2.2}

 \label{ex:cond-interval} \label{ex:cond-interval-r2}
Translate the following four Scheme functions into intervals on the line of
reals: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$1.$}\  {\tt (defin}\={\tt e}\  {\tt (in-interval-1\char'077}\  {\tt x)}\\
\>{\tt (and}\  {\tt (<}\  {\tt -3}\  {\tt x)}\  {\tt (<}\  {\tt x}\  {\tt 0)))}
\\
\hbox{$2.$}\  {\tt (defin}\={\tt e}\  {\tt (in-interval-2\char'077}\  {\tt x)}
\\
\>{\tt (or}\  {\tt (<}\  {\tt x}\  {\tt 1)}\  {\tt (>}\  {\tt x}\  {\tt 2)))}
\\
\hbox{$3.$}\  {\tt (defin}\={\tt e}\  {\tt (in-interval-3\char'077}\  {\tt x)}
\\
\>{\tt (not}\  {\tt (and}\  {\tt (<=}\  {\tt 1}\  {\tt x)}\  {\tt (<=}\  {\tt x}\  {\tt 5))))}
\end{tabbing}\end{minipage}\end{center}
Also formulate contracts and purpose statements for the four functions. 

Evaluate the following expressions by hand: 
\begin{enumerate}
\item {{\tt (in-interval-1\char'077}\ {\tt -2)}\/}

\item {{\tt (in-interval-2\char'077}\ {\tt -2)}\/}

\item {{\tt (in-interval-3\char'077}\ {\tt -2)}\/}
\end{enumerate}
Show the important steps. Use the pictures to check your
results.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/cond-interval.html}{\rule{3pt}{5pt}}


{\bf Exercise 4.2.3}

 \label{ex:eqs}
Mathematical equations in one variable are claims about an unknown
number. For example, the quadratic equation 
$$ x^2 + 2 \cdot x + 1 = 0 $$
is a claim concerning some unknown number $x$. For $x = -1$ the claim holds: 
$$ x^2 + 2 \cdot x + 1 = (-1)^2 + 2 \cdot (-1) + 1 = 1 - 2 + 1 = 0 \ . $$
For $x = 1$, it doesn't because 
$$ x^2 + 2 \cdot x + 1 = (1)^2 + 2 \cdot (1) + 1 = 1 + 2 + 1 = 4 \ , $$
and 4 is {\em not\/} equal to 0. A number for which the claim holds is
called a {\sl solution\/} to the equation.

We can use Scheme to formulate equational conditions as a function. If someone
then claims to have a solution, we can use the function to test whether the 
proposed solution is, in fact, a solution.  Our running example corresponds
to the function  
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt equation1}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt x}\/} is a solution for $x^2 + 2 \cdot x + 1 = 0$}
\\
{\tt (d}\={\tt efine}\  {\tt (equation1}\  {\tt x)}
\\
\>{\tt (=}\  {\tt (+}\  {\tt (*}\  {\tt x}\  {\tt x)}\  {\tt (+}\  {\tt (*}\  {\tt 2}\  {\tt x)}\  {\tt 1))}\  {\tt 0))}
\end{tabbing}\end{minipage}\end{center}
When we apply {{\tt equation1}\/} to some number, we get {{\sf true}\/} or
{{\sf false}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (equation1}\  {\tt -1)}\\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
and
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (equation1}\  {\tt +1)}\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}

\noindent Translate the following equations into Scheme functions: 
\begin{enumerate}
\item $4 \cdot n + 2 = 62$
\item $2 \cdot n^2   = 102$
\item $4 \cdot n^2 + 6 \cdot n + 2 = 462$
\end{enumerate}
Determine whether {{\tt 10}\/}, {{\tt 12}\/}, or {{\tt 14}\/} are solutions of
these equations. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/eqs.html}{\rule{3pt}{5pt}}


{\bf Exercise 4.2.4}

 \label{ex:test0}
Equations are not only ubiquitous in mathematics, they are also heavily
used in programming. We have used equations to state what a function should
do with examples, we have used them to evaluate expressions by hand, and we 
have added them as test cases to the {\tt Definitions} window. For example, 
if our goal is to define {{\mbox{\it Fahrenheit\/}{{}{\tt ->}{}}\mbox{\it Celsius\/}}\/}, we might add have
added our examples as test cases as follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (}{\mbox{\it Fahrenheit\/}{{}{\tt ->}{}}\mbox{\it Celsius\/}}\  {\tt 32)}\\
{\tt =}\  
\\
{\tt 0}
\end{tabbing}\end{minipage}\end{center}
and 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (}{\mbox{\it Fahrenheit\/}{{}{\tt ->}{}}\mbox{\it Celsius\/}}\  {\tt 212)}\\
{\tt =}\  
\\
{\tt 100}
\end{tabbing}\end{minipage}\end{center}
After clicking the {\tt Execute} button we can compare the numbers to
the left and right of {{\tt =}\/}. If they are equal, we know our function
works. 

As our results become more and more complex, comparing values becomes more
and more tedious. Using {{\tt =}\/}, we can instead translate these
equations into claims:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (=}\  \={\tt (}{\mbox{\it Fahrenheit\/}{{}{\tt ->}{}}\mbox{\it Celsius\/}}\  {\tt 32)}\\
\>{\tt 0)}
\end{tabbing}\end{minipage}\end{center}
and 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (=}\  \={\tt (}{\mbox{\it Fahrenheit\/}{{}{\tt ->}{}}\mbox{\it Celsius\/}}\  {\tt 212)}\\
\>{\tt 100)}
\end{tabbing}\end{minipage}\end{center}
Now, if all claims evaluate to {{\tt true}\/}, we know that our function
works for the specified examples. If we see a {{\tt false}\/} anywhere, 
something is still wrong. 

Reformulate the test cases for exercises~\ref{ex:f2c}, \ref{ex:us-dm},
\ref{ex:triangle}, and~\ref{ex:convert3} as claims. 

\medskip\noindent{\bf Testing}:\  Writing tests as claims is good practice, though we need
to know more about equality to develop good automatic tests. To do so, we
resume the discussion of equality and testing in
section~\ref{sec:equal-test}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/test0.html}{\rule{3pt}{5pt}}


\subsection{Conditionals and Conditional Functions} 
\label{sec:cond:prog2}
\htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:cond:prog2}

Some banks pay different levels of interest for saving accounts. The more a
customer deposits, the more the bank pays. In such arrangements, the
interest rate depends on the {\it interval\/} into which the savings amount
falls.  To assist their bank clerks, banks deploy interest-rate
functions. An interest function consumes the amount that a customer wishes to
deposit and responds with the interest that the customer receives for this
amount of money.

Our interest rate function must determine which of several conditions holds
for the input. We say that the function is a {\sc {conditional function}},
and we formulate the definition of such functions using {\sc {conditional expressions}}. The general shape of a conditional expression
is
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt question}\  {\tt answer\char'135}
\\
\>{\tt ...}
\\
\>{\tt \char'133}{\tt question}\  {\tt answer\char'135}{\tt )}
\end{tabbing}\end{minipage}\qquad\qquad {\rm or} \qquad\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt question}\  {\tt answer\char'135}
\\
\>{\tt ...}
\\
\>{\tt \char'133}{\tt else}\  {\tt answer\char'135}{\tt )}
\end{tabbing}\end{minipage}
\end{center}
The dots indicate that a {{\bf cond}-exp\-res\-sion} may contain an arbitrary
number of {{\tt cond}\/}-lines. Each {{\tt cond}\/}-line, also called a
{{\tt cond}\/}-clause, contains two expressions, called {\sc {condition}}
and {\sc {answer}}. A {condition} is a conditional expression that
involves the parameters; the answer is a Scheme expression that computes
the result from the parameters and other data if the conditional expression
holds.\footnote{The use of brackets, that is, {{\tt \char'133}\/} and {{\tt \char'135}\/}, in place of parentheses is optional, but it sets apart the conditional clauses from other expressions and helps people read functions.}

Conditional expressions are the most complicated form of expressions we
have encountered and will encounter. It is therefore easy to make mistakes
when we write them down. Compare the following two parenthesized expressions:
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (<}\  {\tt n}\  {\tt 10)}\  {\tt 5.0\char'135}
\\
\>{\tt \char'133}{\tt (<}\  {\tt n}\  {\tt 20)}\  {\tt 5\char'135}
\\
\>{\tt \char'133}{\tt (<}\  {\tt n}\  {\tt 30)}\  {\sf true}{\tt \char'135}{\tt )}
\end{tabbing}\end{minipage}\qquad\qquad\qquad\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (<}\  {\tt n}\  {\tt 10)}\  {\tt 30}\  {\tt 12\char'135}
\\
\>{\tt \char'133}{\tt (>}\  {\tt n}\  {\tt 25)}\  {\sf false}{\tt \char'135}
\\
\>{\tt \char'133}{\tt (>}\  {\tt n}\  {\tt 20)}\  {\tt 0\char'135}{\tt )}
\end{tabbing}\end{minipage}
\end{center}
The left one is a valid {{\bf cond}-exp\-res\-sion} because each
{{\tt cond}\/}-line contains two expressions. In contrast, the right one is
{\em not\/} a valid {{\bf cond}-exp\-res\-sion}. Its first line contains three
expressions instead of two.

When Scheme evaluates a {{\bf cond}-exp\-res\-sion}, it determines the value
of each condition, one by one. A condition must evaluate to {{\sf true}\/} or
{{\sf false}\/}.  For the first condition that evaluates to {{\sf true}\/}, Scheme
evaluates the corresponding answer, and the value of the answer is the
value of the entire {{\bf cond}-exp\-res\-sion}. If the last condition is
{{\tt else}\/} and all other conditions fail, the answer for the
{{\tt cond}\/} is the value of the last answer expression.\footnote{If the {{\bf cond}-exp\-res\-sion} has no {{\tt else}\/} clause and all conditions evaluate to {{\sf false}\/}, an error is signaled in {\tt Beginning Student} Scheme.}

Here are two simple examples:
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (<=}\  {\tt n}\  {\tt 1000)}\  {\tt .040\char'135}
\\
\>{\tt \char'133}{\tt (<=}\  {\tt n}\  {\tt 5000)}\  {\tt .045\char'135}
\\
\>{\tt \char'133}{\tt (<=}\  {\tt n}\  {\tt 10000)}\  {\tt .055\char'135}
\\
\>{\tt \char'133}{\tt (>}\  {\tt n}\  {\tt 10000)}\  {\tt .060\char'135}{\tt )}
\end{tabbing}\end{minipage}\qquad\qquad\qquad\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (<=}\  {\tt n}\  {\tt 1000)}\  {\tt .040\char'135}
\\
\>{\tt \char'133}{\tt (<=}\  {\tt n}\  {\tt 5000)}\  {\tt .045\char'135}
\\
\>{\tt \char'133}{\tt (<=}\  {\tt n}\  {\tt 10000)}\  {\tt .055\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt .060\char'135}{\tt )}
\end{tabbing}\end{minipage}
\end{center}
If we replace {{\tt n}\/} with {{\tt 20000}\/}, the first three conditions
evaluate to {{\sf false}\/} in both expressions. For the expression on the left
the fourth condition, {{\tt (>}\ {\tt 20000}\ {\tt 10000)}\/}, evaluates to {{\sf true}\/}
and therefore the answer is {{\tt 0.60}\/}. For the expression on the right,
the {{\tt else}\/} clause specifies what the result of the entire expression
is. In contrast, if {{\tt n}\/} is {{\tt 10000}\/}, the value of is
{{\tt .055}\/} because for both expressions, {{\tt (<=}\ {\tt 10000}\ {\tt 1000)}\/} and
{{\tt (<=}\ {\tt 10000}\ {\tt 5000)}\/} evaluate to {{\sf false}\/} and \relax\relax
{{\tt (<=}\ {\tt 10000}\ {\tt 10000)}\/} evaluates to {{\sf true}\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 4.3.1}

 \label{ex:cond-syntax}
Decide which of the following two {{\bf cond}-exp\-res\-sion}s is legal: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (<}\  {\tt n}\  {\tt 10)}\  {\tt 20\char'135}
\\
\>{\tt \char'133}{\tt (>}\  {\tt n}\  {\tt 20)}\  {\tt 0\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt 1\char'135}{\tt )}
\end{tabbing}\end{minipage}\qquad\qquad\qquad\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (<}\  {\tt n}\  {\tt 10)}\  {\tt 20\char'135}
\\
\>{\tt \char'133}{\tt (and}\  {\tt (>}\  {\tt n}\  {\tt 20)}\  {\tt (<=}\  {\tt n}\  {\tt 30))\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt 1\char'135}{\tt )}
\end{tabbing}\end{minipage}
\end{center}
Explain why the other one is not. Why is 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\  \\
\>{\tt \char'133}{\tt (<}\  {\tt n}\  {\tt 10)}\  {\tt 20\char'135}
\\
\>{\tt \char'133}{\tt *}\  {\tt 10}\  {\tt n\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt 555\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
not a valid {{\bf cond}-exp\-res\-sion}?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/cond-syntax.html}{\rule{3pt}{5pt}}


{\bf Exercise 4.3.2}

 \label{ex:boolean2}
What is the value of the above {{\bf cond}-exp\-res\-sion}s for \\
\qquad (a) {{\tt n}\ {\tt =}\ {\tt 500}\/}, (b) {{\tt n}\ {\tt =}\ {\tt 2800}\/}, and (c) {{\tt n}\ {\tt =}\ {\tt 15000}\/}? \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/boolean2.html}{\rule{3pt}{5pt}}


{\bf Exercise 4.3.3}

 \label{ex:cond1}
What is the value of
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (<=}\  {\tt n}\  {\tt 1000)}\  {\tt (*}\  {\tt .040}\  {\tt 1000)\char'135}
\\
\>{\tt \char'133}{\tt (<=}\  {\tt n}\  {\tt 5000)}\  {\tt (+}\  \={\tt (*}\  {\tt 1000}\  {\tt .040)}\  
\\
\>\>{\tt (*}\  {\tt (-}\  {\tt n}\  {\tt 1000)}\  {\tt .045))\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (+}\  \={\tt (*}\  {\tt 1000}\  {\tt .040)}\  
\\
\>\>{\tt (*}\  {\tt 4000}\  {\tt .045)}
\\
\>\>{\tt (*}\  {\tt (-}\  {\tt n}\  {\tt 10000)}\  {\tt .055))\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
for (a) {{\tt n}\ {\tt =}\ {\tt 500}\/}, (b) {{\tt n}\ {\tt =}\ {\tt 2800}\/}, and (c) {{\tt n}\ {\tt =}\ {\tt 15000}\/}?
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/cond1.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


With the help of {{\bf cond}-exp\-res\-sion}s, we can now define the interest
rate function that we mentioned at the beginning of this section. Suppose
the bank pays 4\% for deposits of up to \$1,000 (inclusive), 4.5\% for
deposits of up to \$5,000 (inclusive), and 5\% for deposits of more than
\$5,000. Clearly, the function consumes one number and produces one:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt interest-rate}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to determine the interest rate for the given {{\tt amount}\/}}
\\
{\tt (define}\  {\tt (interest-rate}\  {\tt amount)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Furthermore, the problem statement provides three examples: 
\begin{enumerate}
\item {{\tt (interest-rate}\ {\tt 1000)}\ {\tt =}\ {\tt .040}\/}
\item {{\tt (interest-rate}\ {\tt 5000)}\ {\tt =}\ {\tt .045}\/}
\item {{\tt (interest-rate}\ {\tt 8000)}\ {\tt =}\ {\tt .050}\/}
\end{enumerate}

The body of the function must be a {{\bf cond}-exp\-res\-sion} that
distinguishes the three cases mentioned in the problem statement. Here is a
sketch: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (<=}\  {\tt amount}\  {\tt 1000)}\  {\tt ...\char'135}
\\
\>{\tt \char'133}{\tt (<=}\  {\tt amount}\  {\tt 5000)}\  {\tt ...\char'135}
\\
\>{\tt \char'133}{\tt (>}\  {\tt amount}\  {\tt 5000)}\  {\tt ...\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
Using the examples and the outline of the {{\bf cond}-exp\-res\-sion}, the answers
are easy:\label{prg:interest-rate}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (interest-rate}\  {\tt amount)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (<=}\  {\tt amount}\  {\tt 1000)}\  {\tt 0.040\char'135}
\\
\>\>{\tt \char'133}{\tt (<=}\  {\tt amount}\  {\tt 5000)}\  {\tt 0.045\char'135}
\\
\>\>{\tt \char'133}{\tt (>}\  {\tt amount}\  {\tt 5000)}\  {\tt 0.050\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Since we know that the function only requires three cases, we can also replace
the last condition with {{\tt else}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (interest-rate}\  {\tt amount)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (<=}\  {\tt amount}\  {\tt 1000)}\  {\tt 0.040\char'135}
\\
\>\>{\tt \char'133}{\tt (<=}\  {\tt amount}\  {\tt 5000)}\  {\tt 0.045\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt 0.050\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/teacher.gif}~{\em Neither form is preferable. For beginners, it might be better to  spell all out all conditions. If they make a mistake, Scheme will signal a  run-time error. If they use {\bf else}, the function will produce the value  of the {\bf else}-clause, even though the conditions may be set up wrong.}

When we apply {{\tt interest-rate}\/} to an amount, say {{\tt 4000}\/}, the
calculation proceeds as usual. Scheme first copies the body of the
function and replaces {{\tt amount}\/} by {{\tt 4000}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (interest-rate}\  {\tt 4000)}\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (<=}\  {\tt 4000}\  {\tt 1000)}\  {\tt 0.040\char'135}
\\
\>{\tt \char'133}{\tt (<=}\  {\tt 4000}\  {\tt 5000)}\  {\tt 0.045\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt 0.050\char'135}{\tt )}
\\
{\tt =}\  {\tt 0.045}\  
\end{tabbing}\end{minipage}\end{center}
The first condition is {{\sf false}\/} but the second one is {{\sf true}\/}, so
the result is {{\tt 0.045}\/} or 4.5\%. The evaluation would proceed in the
same manner if we had used the variant of the function with {{\tt (>}\ {\tt amount}{ }{\tt 5000)}\/} instead of {{\tt else}\/}. 

\subsection{Designing Conditional Functions} \label{sec:design2} 

Developing conditional functions is more difficult than designing a plain function.
The key is to recognize that the problem statement lists cases and to identify the
different cases. To emphasize the importance of this idea, we introduce and
discuss a design recipe for designing conditional functions. The new recipe
introduces a new step, {\sc {data analysis}}, which requires a programmer to
understand the different situations that the problem statement discusses. It also
modifies the Examples and the Body steps of the design recipe in
section~\ref{sec:design1}:


\begin{description}
\item[Data Analysis:] After determining that a problem statement deals with
distinct situations, we must identify all of them.

For numeric functions, a good strategy is to draw a number line and to
identify the intervals that correspond to a specific situation. Consider
the contract for the {{\tt interest-rate}\/} function:

\begin{minipage}[t]{8in}\begin{tabbing}\  \  \=\hbox{;; {{\tt interest-rate}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\>\hbox{;; to determine the interest rate for the given {{\tt amount}\ {\tt >=}\ {\tt 0}\/}}
\\
\>{\tt (define}\  {\tt (interest-rate}\  {\tt amount)}\  {\tt ...)}
\end{tabbing}\end{minipage}

It inputs non-negative numbers and produces answers for three different
situations: 

 \par\smallskip \hspace{.2cm}{\framebox[\numwidth]{ \begin{picture}(300,30)(0,-20)  
\put(0,0){\line(1,0){300}} \multiput(0,-5)(25,0){12}{\line(0,1){10}} 
\put(-03,-18){\mbox{$0$}} 
\put(123,-18){\mbox{$5,000$}} 
\put(245,-18){\mbox{$10,000$}}      
\put(0,-4){{\mbox{\hspace{-1.5pt}\LARGE[}}}  \multiput(0,1)(0,-.1){25}{\line(1,0){25}}  \invcnt=0 \advance \invcnt by 25   
\put(\invcnt,-4){{\mbox{\hspace{-4pt}\LARGE]}}}      
\put(25,-4){{\mbox{\hspace{-1pt}\LARGE(}}}  \multiput(25,1)(0,-.1){25}{\line(1,0){100}}  \invcnt=25 \advance \invcnt by 100   
\put(\invcnt,-4){{\mbox{\hspace{-4pt}\LARGE]}}}    
\put(125,-4){{\mbox{\hspace{-1pt}\LARGE(}}}\multiput(125,1)(0,-.1){25}{\vector(+1,0){175}}  \end{picture}}}

For function that process booleans, the {{\bf cond}-exp\-res\-sion} must
distinguish between exactly two situations: {{\sf true}\/} and {{\sf false}\/}.
We will soon encounter other forms of data that require case-based
reasoning.

\item[Function Examples:] Our choice of examples account for the distinct
situations. At a minimum, we must develop one function example per
situation.  If we characterized the situations as numeric intervals, the
examples should also include all borderline cases.

For our {{\tt interest-rate}\/} function, we should use {{\tt 0}\/},
{{\tt 1000}\/}, and {{\tt 5000}\/} as border-line cases. In addition, we
should pick numbers like {{\tt 500}\/}, {{\tt 2000}\/}, and {{\tt 7000}\/} to
test the interiors of the three intervals.

\item[The Function Body---Conditions:] The function's body must consist of a
{{\bf cond}-exp\-res\-sion} that has as many clauses as there are distinct
situations. This requirement immediately suggests the following body of our
solution: 

\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (d}\={\tt efine}\  {\tt (interest-rate}\  {\tt amount)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt ...}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt ...}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt ...}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}

Next we must formulate the conditions that characterize each situation. The
conditions are claims about the function's parameters, expressed with Scheme's 
relational operators or with our own functions. 

The number line from our example translates into the following three
conditions:  
\begin{enumerate}
\item {{\tt (and}\ {\tt (<=}\ {\tt 0}\ {\tt amount)}\ {\tt (<=}\ {\tt amount}\ {\tt 1000))}\/}
\item {{\tt (and}\ {\tt (<}\ {\tt 1000}\ {\tt amount)}\ {\tt (<=}\ {\tt amount}\ {\tt 5000))}\/}
\item {{\tt (<}\ {\tt 5000}\ {\tt amount)}\/}
\end{enumerate}
Adding these conditions to the function produces a better approximation of
the final definition:

\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (interest-rate}\  {\tt amount)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (<=}\  {\tt 0}\  {\tt amount)}\  {\tt (<=}\  {\tt amount}\  {\tt 1000))}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (<}\  {\tt 1000}\  {\tt amount)}\  {\tt (<=}\  {\tt amount}\  {\tt 5000))}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (>}\  {\tt amount}\  {\tt 5000)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}

At this stage, a programmer should check that the chosen conditions
distinguish inputs in an appropriate manner. Specifically, if some input
belongs to a particular situation and {{\tt cond}\/}-line, the preceding
conditions should evaluate to {{\sf false}\/} and the condition of the line
should evaluate to {{\sf true}\/}. 

\htmladdimg{../icons/teacher.gif}~{\em If your students have difficulties with formulating conditions, have  them do the following. First, replace the \ldots\ in the  {{\tt cond}\/}-clauses with {{\tt 1}\/}, {{\tt 2}\/}, {{\tt 3}\/}, {\it  etc.}. Second, apply the function to the chosen examples in the  {\tt Interactions} window. An input for the first condition must  produce the result {{\tt 1}\/}, and so on. Otherwise, we have developed the  wrong conditions.}

\item[The Function Body---Answers:] Finally, it is time to determine what the
function should produce for each {{\tt cond}\/}-clause. More concretely, we
consider each line in the {{\bf cond}-exp\-res\-sion} separately, assuming
that the condition holds.

In our example, the results are directly specified by the problem
statement. They are {{\tt 4.0}\/}, {{\tt 4.5}\/}, and {{\tt 5.0}\/}.  In more
complicated examples, we may have to determine an expression for each
{{\tt cond}\/}-answer following the suggestion of our first design recipe.

\noindent{\bf Hint:} \ If the answers for each {{\tt cond}\/}-clause are complex, it is good
practice to develop one answer at a time. Assume that the condition
evaluates to {{\sf true}\/}, and develop an answer using the parameters,
primitives, and other functions. Then apply the function to inputs that force
the evaluation of this new answer. It is legitimate to leave ``\ldots'' in
place of the remaining answers.

\item[Simplification:]

When the definition is complete and tested, a programmer might wish to
check whether the conditions can be simplified. In our example, we know
that {{\tt amount}\/} is always greater than or equal to {{\tt 0}\/}, so the
first condition could be formulated as \\
 

\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (<=}\  {\tt amount}\  {\tt 1000)}\end{tabbing}\end{minipage}

Furthermore, we know that {{\bf cond}-exp\-res\-sion}s are evaluated
sequentially. That is, by the time the second condition is evaluated the
first one must have produced {{\sf false}\/}. Hence, we know that the amount is
{\em not\/} less than or equal to {{\tt 1000}\/}, which makes the left
component of the second condition superfluous. The appropriately simplified
sketch of {{\tt interest-rate}\/} is as follows:

\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (d}\={\tt efine}\  {\tt (interest-rate}\  {\tt amount)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (<=}\  {\tt amount}\  {\tt 1000)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (<=}\  {\tt amount}\  {\tt 5000)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (>}\  {\tt amount}\  {\tt 5000)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}

\end{description}
Figure~\ref{fig:design2} summarizes these suggestions on the design of conditional
functions. Read it in conjunction with figure~\ref{fig:design1} and compare the two
rows for ``Body.'' Re-read the table when designing a conditional function! 

%% BEGIN INLINED FILE: idesign2.dl.tex
%% BEGIN INLINED FILE: design2.dl.tex




\hrule
\begin{center} \begin{tabular}{|l|l|l|}\hline Phase            &  Goal 		& Activity \\
 \hline\hline \begin{minipage}[t]{.69in} Data \\
 Analysis  \end{minipage}  & \begin{minipage}[t]{1.6in}\raggedright to determine the distinct situations a function deals with \rule{0pt}{6pt}\end{minipage}		& \begin{minipage}[t]{2.2in}\raggedright {inspect the problem statement for distinct situations $\bullet$\  enumerate all possible situations}\rule{0pt}{6pt}\end{minipage} \\
 \hline Examples 	& \begin{minipage}[t]{1.6in}\raggedright to provide an example per situation \rule{0pt}{6pt}\end{minipage}   		& \begin{minipage}[t]{2.2in}\raggedright {choose at least one example per situation $\bullet$\  for intervals, the examples must include borderline cases}\rule{0pt}{6pt}\end{minipage} \\
 \hline \begin{minipage}[t]{.69in} Body (1) \\
 Conditions \end{minipage} & \begin{minipage}[t]{1.6in}\raggedright to formulate a conditional expression \rule{0pt}{6pt}\end{minipage}  		& \begin{minipage}[t]{2.2in}\raggedright {write down the skeleton of a {\bf cond} expression, with one clause per  situation  $\bullet$\  formulate one condition per situation, using the parameters $\bullet$\  show that the conditions distinguish the examples appropriately}\rule{0pt}{6pt}\end{minipage} \\
 \hline \begin{minipage}[t]{.69in} Body (2) \\
 Answers \end{minipage} & \begin{minipage}[t]{1.6in}\raggedright to formulate the body of each {\bf cond}-line \rule{0pt}{6pt}\end{minipage}   		& \begin{minipage}[t]{2.2in}\raggedright {deal with each {\bf cond}-line {\em separately\/} $\bullet$\  assume the condition holds and develop a Scheme expression that  computes the appropriate answer}\rule{0pt}{6pt}\end{minipage} \\
 \hline \end{tabular} \end{center} 
\center{Figure: Designing the body of a conditional program} \label{fig:design2}
\centerline{(Use with the recipe in figure~\ref{fig:design1} (pg.~\pageref{fig:design1}))}
\hrule

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 4.4.1}

 \label{ex:interest}
Develop the function {{\tt interest}\/}. Like {{\tt interest-rate}\/}, it
consumes a deposit amount. Instead of the rate, it produces the actual
amount of interest that the money earns in a year. The bank pays a flat 4\%
for deposits of up to \$1,000, a flat 4.5\% per year for deposits of up to
\$5,000, and a flat 5\% for deposits of more than
\$5,000.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/interest.html}{\rule{3pt}{5pt}}


{\bf Exercise 4.4.2}

 \label{ex:tax}
Develop the function {{\tt tax}\/}, which consumes the grosspay and produces
the amount of tax owed. For a grosspay of \$240 or less, the tax is 0\%; for
over \$240 and \$480 or less, the tax rate is 15\%; and for any pay over
\$480, the tax rate is 28\%.

Also develop {{\tt netpay}\/}. The function determines the netpay of an
employee from the number of hours worked. Assume the hourly pay rate is
\$12.

\noindent{\bf Hint:} \ Remember to develop auxiliary functions when a definition becomes too
large or too complex to manage.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tax.html}{\rule{3pt}{5pt}}


{\bf Exercise 4.4.3}

 \label{ex:credit}
Some credit card companies pay back a small portion of the charges a customer
makes during the year. One particular company follows the policy to return
\begin{enumerate}
\item .25\% for the first \$500 of charges, 
\item .50\% for the next \$1000 (that is, the portion between \$500 and \$1500),
\item .75\% for the next \$1000 (that is, the portion between \$1500 and \$2500), 
\item and 1.0\% for everything above \$2500.
\end{enumerate}
Thus, a customer who charges \$400 a year receives \$1.00, which is $0.25
\cdot 1/100 \cdot 400$, and one who charges \$1,400 a year receives \$5.75,
which Is $1.25 = 0.25 \cdot 1/100 \cdot 500$ for the first \$500 and $0.50
\cdot 1/100 \cdot 900 = 4.50$ for the next \$900.

Determine by hand the pay-backs for a customer who charged \$2000 and one
who charged \$2600.

Define the function {{\tt pay-back}\/}, which consumes a charge amount
and computes the corresponding pay-back amount.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/credit.html}{\rule{3pt}{5pt}}


{\bf Exercise 4.4.4}

 \label{ex:quad1}
An equation is a claim about numbers; a quadratic equation is a special
kind of equation. All quadratic equations (in one variable) have the
following general shape: 
$$ a \cdot x^2 + b \cdot x + c = 0\ . $$
In a specific equation, $a$, $b$ and $c$ are replaced by numbers, as in 
$$ 2 \cdot x^2 + 4 \cdot x + 2 = 0 $$
or 
$$ 1 \cdot x^2 + 0 \cdot x + (-1) = 0 \ .$$
The variable $x$ represents the unknown. 

Depending on the value of {{\tt x}\/}, the two sides of the equation
evaluate to the same value (see exercise~\ref{ex:eqs}).  If the two sides
are equal, the claim is true; otherwise it is false.  A number that makes
the claim true is a {\sl solution}.  The first equation has one solution,
$-1$, as we can easily check:
$$ 2 \cdot (-1)^2 + 4 \cdot (-1) + 2 = 2 - 4 + 2 = 0\ .$$
The second equation has two solutions: $+1$ and $-1$.

The number of solutions for a quadratic equation depends on the values of $a$,
$b$, and $c$. If the coefficient $a$ is 0, we say the equation is {\sl degenerate\/} and do not consider how many solutions it has. Assuming $a$ is not
$0$, the equation has 
\begin{enumerate}
\item two solutions if $b^2 > 4 \cdot a \cdot c$, 
\item one solution if $b^2 = 4 \cdot a \cdot c$, and
\item no solution if $b^2 < 4 \cdot a \cdot c$.
\end{enumerate}
To distinguish this case from the degenerate one, we sometimes use the phrase
``{\sl proper}'' quadratic equation.

Develop the function {{\tt how-many}\/}, which consumes the coefficients
{{\tt a}\/}, {{\tt b}\/}, and {{\tt c}\/} of a proper quadratic equation and
determines how many solutions the equation has:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (how-many}\  {\tt 1}\  {\tt 0}\  {\tt -1)}\  {\tt =}\  {\tt 2}\\
{\tt (how-many}\  {\tt 2}\  {\tt 4}\  {\tt 2)}\  {\tt =}\  {\tt 1}
\end{tabbing}\end{minipage}\end{center} 
Make up additional examples. First determine the number of solutions by
hand, then with DrScheme.

How would the function change if we didn't assume the equation was
proper?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/quad1.html}{\rule{3pt}{5pt}}


\section{Symbolic Information} \label{sec:sym} \htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:sym}

These days computers mostly process symbolic information such as names,
words, directions, or images. All modern programming languages support at
least one way of representing symbolic information. Scheme supports several
ways to express symbolic information: symbols, strings, (keyboard)
characters, and images. A {\sc {symbol}} is a sequence of keyboard
characters\footnote{Not all keyboard characters are legal in symbols.  For example, a blank space or a comma are illegal.} preceded by a single
forward quotation mark: 
\begin{center}
{{\tt \char'047}{\tt the}\/} \hfill
{{\tt \char'047}{\tt dog}\/} \hfill
{{\tt \char'047}{\tt ate}\/}\hfill
{{\tt \char'047}{\tt a}\/}\hfill
{{\tt \char'047}{\tt chocolate}\/}\hfill
{{\tt \char'047}{\tt cat!}\/}\hfill
{{\tt \char'047}{\tt two\char'136}{\tt 3}\/}\hfill
{{\tt \char'047}{\tt and\char'045}{\tt so\char'045}{\tt on\char'077}\/}\hfill
\end{center}
Like a number, a symbol has no inherent meaning. It is up to the function's
user to relate symbolic data and real-world information, though the
connection is typically obvious in a specific context. For example,
{{\tt \char'047}{\tt east}\/} will usually refer to the direction where the sun rises, 
{{\tt \char'047}{\tt professor}\/} will be the title of a person teaching and researching
at a university.

\hrule

~\bigskip\\
  \begin{rawhtml}   <center><img src=../icons/planets0.gif alt="[planets in DrScheme]"></center>  \end{rawhtml}\\
\bigskip

\center{Figure: The planets as images in DrScheme} \label{fig:planets-pics}
\hrule

Like numbers, symbols are atomic pieces of data. Their purpose is to
represent things such as family and first names, job titles, commands, and
announcements, and so on.  Scheme provides only one basic operation on
symbols: {{\tt symbol=?}\/}, a comparison operation. It consumes two symbols and
produces {{\sf true}\/} if and only if the two symbols are identical:
\begin{enumerate}
\item[] {{\tt (symbol=?}\ {\tt \char'047}{\tt Hello}\ {\tt \char'047}{\tt Hello)}\ {\tt =}\ {\sf true}\/} 
\item[] {{\tt (symbol=?}\ {\tt \char'047}{\tt Hello}\ {\tt \char'047}{\tt Howdy)}\ {\tt =}\ {\sf false}\/} 
\item[] {{\tt (symbol=?}\ {\tt \char'047}{\tt Hello}\ {\tt x)}\ {\tt =}\ {\sf true}\/}  if {{\tt x}\/} stands for {{\tt \char'047}{\tt Hello}\/}
\item[] {{\tt (symbol=?}\ {\tt \char'047}{\tt Hello}\ {\tt x)}\ {\tt =}\ {\sf false}\/}  if {{\tt x}\/} stands for {{\tt \char'047}{\tt Howdy}\/}
\end{enumerate}


Symbols were first introduced to computing by researchers in artificial
intelligence who wanted to design functions that could have conversations
with people. Consider the function {{\tt reply}\/}, which replies with some
remark to the following greetings: ``good morning,'' ``how are you,''
``good afternoon,'', and ``good evening.'' Each of those short sentences
can be represented as a symbol: {{\tt \char'047}{\tt GoodMorning}\/}, {{\tt \char'047}{\tt HowAreYou}\/},
{{\tt \char'047}{\tt GoodAfternoon}\/}, and {{\tt \char'047}{\tt GoodEvening}\/}.  Thus, {{\tt reply}\/}
consumes a symbol and replies with a symbol:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reply}\ {\tt :}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt symbol}\/}}\\
\hbox{;; to determine a reply for the greeting {{\tt s}\/}}
\\
{\tt (define}\  {\tt (reply}\  {\tt s)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Furthermore, the function must distinguish among four different situations.
This means our design recipe from section~\ref{sec:design2} applies.
According to this design recipe, we need a four-clause {{\bf cond}-exp\-res\-sion}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (reply}\  {\tt s)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt s}\  {\tt \char'047}{\tt GoodMorning)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt s}\  {\tt \char'047}{\tt HowAreYou\char'077}{\tt )}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt s}\  {\tt \char'047}{\tt GoodAfternoon)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt s}\  {\tt \char'047}{\tt GoodEvening)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The {{\tt cond}\/}-clauses match the four symbols, which is naturally much
easier than matching four intervals.

From this function {\sc {template}} it is a short step to the final
function. Here is one version of {{\tt reply}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (reply}\  {\tt s)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt s}\  {\tt \char'047}{\tt GoodMorning)}\  {\tt \char'047}{\tt Hi\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt s}\  {\tt \char'047}{\tt HowAreYou\char'077}{\tt )}\  {\tt \char'047}{\tt Fine\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt s}\  {\tt \char'047}{\tt GoodAfternoon)}\  {\tt \char'047}{\tt INeedANap\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt s}\  {\tt \char'047}{\tt GoodEvening)}\  {\tt \char'047}{\tt BoyAmITired\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
We can think of many different ways of how to replace the ``\ldots'' in the
template with replies. But no matter what we replace them with, the basic
template could be defined without concern for the output of the function. We
will see in subsequent sections that this focus on the input data is
actually the norm and that concern for the output data can be postponed.

\noindent {\bf A Note on Strings}: A {\sc {string}} is a second form of symbolic
data. Like a symbol, a string consists of a sequence of keyboard characters, but
they are enclosed in string quotes:
\begin{center}
{{\tt ``the}\ {\tt dog''}\/} \hfill {{\tt ``isn\char'047}{\tt t''}\/}\hfill {{\tt ``made}\ {\tt of''}\/}\hfill
{{\tt ``chocolate''}\/}\hfill {{\tt ``two\char'136}{\tt 3''}\/}\hfill {{\tt ``and}\ {\tt so}\ {\tt on\char'077}{\tt ''}\/}\hfill
\end{center}
In contrast to symbols, strings are not atomic. They are compound data,
which we discuss later in the book. For now, we use strings as if they were
fancy symbols; the only operation needed is {{\tt string=?}\/}, which
compares two strings the way {{\tt symbol=?}\/} compares two
symbols. Otherwise we ignore strings, and when we use them, we act as if
they were symbols.~{\rule{3pt}{5pt}}

\noindent {\bf A Note on Images}: An {\sc {image}} is a third form of symbolic
data, and it is fun to develop functions that process images.  Like symbols, images
don't have any {\it a priori\/} meaning, but we tend to connect them easily with
the intended information.

DrScheme supports images: see figure~\ref{fig:planets-pics}, which shows the
beginning of a function that manipulates planet pictures.  Images are values like
numbers and booleans. They can therefore be used inside of expressions. Most often
though, we give images names because they are typically used by several
functions. If we don't like the picture, it is then easily replaced with a
different one (see section~\ref{sec:use-var-defs}).~{\rule{3pt}{5pt}}

\subsection{Finger Exercises with Symbols}

{\bf Exercise 5.1.1}

 \label{ex:eval-sym} 
Evaluate {{\tt (reply}\ {\tt \char'047}{\tt HowAreYou\char'077}{\tt )}\/} and {{\tt (reply}{ }{\tt \char'047}{\tt GoodEvening)}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/eval-sym.html}{\rule{3pt}{5pt}}


\htmladdimg{../icons/teacher.gif}~{\em The next few exercises involve conditional reasoning in addition to  symbol-processing. Remind students to identify the possible situations and to  design examples/tests and {{\tt cond}\/}-expressions accordingly.  Exercise~\ref{ex:guess1} is the beginning of a series of exercises:  \ref{ex:guess2}, \ref{ex:guess3}, \ref{ex:guess-gui}. Each of the exercises gives  the students a bit more control over the game until they eventually write the  entire function. The next one, exercise~\ref{ex:guess2}, also reminds students  that we need to develop a program per concept if we don't want things to get out  of hand.}

{\bf Exercise 5.1.2}

 \label{ex:guess1} \label{ex:guess1-r} 
Develop the function {{\tt check-guess}\/}. It consumes two numbers, {{\tt guess}\/}
 and {{\tt target}\/}. Depending on how {{\tt guess}\/} relates to {{\tt target}\/},
 the function produces one of the following three answers: {{\tt \char'047}{\tt TooSmall}\/},
 {{\tt \char'047}{\tt Perfect}\/}, or {{\tt \char'047}{\tt TooLarge}\/}.

The function implements one part of a two-player number guessing game. One player
 picks a random number between 0 and 99999. The other player's goal is to
 determine this number, called {{\tt target}\/}, with the least number of
 guesses. To each guess, the first player responds with one of the three responses
 that {{\tt check-guess}\/} implements.

The function {{\tt check-guess}\/} and the teachpack {\tt\bf guess.ss} implement the
 first player. The teachpack picks the random number, pops up a window in which
 the second player can choose digits, and hands over the {{\tt guess}\/} and the
 {{\tt target}\/} to {{\tt check-guess}\/}. To play the game, set the
 teachpack to {\tt\bf guess.ss} using the \verb+Language|Set teachpack+ option. 
 Then evaluate the expression
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (repl}\  {\tt check-guess)}\end{tabbing}\end{minipage}\end{center}
 after {{\tt check-guess}\/} has been thoroughly tested.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/guess1.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 5.1.3}

 \label{ex:guess2} \label{ex:guess2-r} 
Develop the function {{\tt check-guess3}\/}. It implements a larger portion of
 the number guessing game of exercise~\ref{ex:guess1} than the function
 {{\tt check-guess}\/}. Now the teachpack hands over the {{\tt digits}\/} that
 the user guesses, not the number that they form. 

To simplify the problem a little bit, the game works with only three
 numbers. Thus, {{\tt check-guess3}\/} consumes three digits and a number. The
 first digit is the least significant, the third one is the most significant. The
 number is called {{\tt target}\/} and represents the randomly chosen number.
 Depending on how {{\tt guess}\/}, the number determined by the three digits,
 relates to {{\tt target}\/}, {{\tt check-guess3}\/} produces one of the following
 three answers: {{\tt \char'047}{\tt TooSmall}\/}, {{\tt \char'047}{\tt Perfect}\/}, or {{\tt \char'047}{\tt TooLarge}\/}.

The rest of the game is still implemented by {\tt\bf guess.ss}. To play
 the game with {{\tt check-guess3}\/}, evaluate
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (repl3}\  {\tt check-guess3)}\end{tabbing}\end{minipage}\end{center}
 after the function has been thoroughly tested.

\noindent{\bf Hint:}  Remember to develop an auxiliary function per concept.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/guess2.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 5.1.4}

 \label{ex:quad2}
Develop {{\tt what-kind}\/}.  The function consumes the coefficients {{\tt a}\/},
 {{\tt b}\/}, and {{\tt c}\/} of a quadratic equation. It then determines whether
 the equation is degenerate and, if not, how many solutions the equation has. The
 function produces one of four symbols: {{\tt \char'047}{\tt degenerate}\/}, {{\tt \char'047}{\tt two}\/},
 {{\tt \char'047}{\tt one}\/}, or {{\tt \char'047}{\tt none}\/}.

\noindent{\bf Hint:} \ Compare with exercise~\ref{ex:quad1}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/quad2.html}{\rule{3pt}{5pt}}


{\bf Exercise 5.1.5}

 \label{ex:check-guess} \htmlref{\htmladdimg{../icons/plt.gif}}{drn:ex:check-guess}
Develop the function {{\tt check-color}\/}. It implements a key portion of a color
 guessing game.  One player picks two colors for two squares; we call those
 ``targets''. The other one tries to guess which color is assigned to which
 square; they are guesses.  The first player's response to a guess is check the
 colors and to produce one of the following answers:
\begin{enumerate}
\item {{\tt \char'047}{\tt Perfect}\/}, if the first target is equal to the first
guess and the second target is equal to the second guess; 

\item {{\tt \char'047}{\tt OneColorAtCorrectPosition}\/}, if the first guess is equal to
the first target or the second guess is equal to the second target; 

\item {{\tt \char'047}{\tt OneColorOccurs}\/}, if either of the guesses is one of the two
targets; 

\item and {{\tt \char'047}{\tt NothingCorrect}\/}, otherwise. 
\end{enumerate}
These four answers are the only answers that the first player gives. The
second player is to guess the two chosen target colors with as few guesses
as possible. 

The function {{\tt check-color}\/} simulates the first player's checking action. It
consumes four colors; for simplicity, we assume that a color is a symbol, say,
{{\tt \char'047}{\tt red}\/}.  The first two arguments to {{\tt check-color}\/} are ``targets,''
the latter two are ``guesses.'' The function produces one of the four answers.

When the function is tested, use the teachpack to {\tt\bf master.ss} to play
the color-guessing game.\footnote{MasterMind, the commercial version of this game, is played in a slightly different manner.}  The teachpack
provides the function {{\tt master}\/}. Evaluate {{\tt (master}\ {\tt check-color)}\/} 
and choose colors with the mouse.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/check-guess.html}{\rule{3pt}{5pt}}




\section{Compound Data, Part 1: Structures} 
\label{sec:records} 

\htmladdimg{../icons/teacher.gif}~{\em Remind your students of ping-pong. To implement this kind of game, we  need to compound data: position, velocity, balls. Otherwise we quickly  lose track of what is what, especially if we were to use many balls. We  will study a game with a large number of balls in a short while.}

The input of a function is seldom a single measurement (number), a single
 switch position (boolean), or a single name (symbol). Instead, it is
 almost always a piece of data that represents an object with many
 properties. Each property is a piece of information. For example, a
 function may consume a record about a CD; the relevant information might
 include the artist's name, the CD title, and the price. Similarly, if we
 are to model the movement of an object across a plane with a function, we
 must represent the position of the object in the plane, its speed in each
 direction, and possibly its color. In both cases, we refer to several
 pieces of information as if they were one: {\em one\/} record and {\em  one\/} point.  In short, we {\sc {compound}} several pieces of data into
 a single piece of data.

Scheme provides many different methods for compounding data. In this
 section, we deal with {\sc {structures}}. A structure combines a fixed
 number of values into a single piece of data. In section~\ref{sec:lists},
 we will encounter a method for combining an arbitrarily large number of
 values into a single piece of data.

\subsection{Structures} 
\label{sec:structures} 

Suppose we wish to represent the {\sl pixels\/} (colored dots) on our
 computer monitors. A pixel is very much like a Cartesian point. It has a
 $x$-coordinate, which tells us where the pixel is in the horizontal
 direction, and it has a $y$-coordinate, which tells us where the pixel is
 located in the downwards vertical direction. Given the two numbers, we can
 locate a pixel on the monitor, and so can a computer program.

DrScheme's teachpacks represent pixels with {{\tt posn}\/} structures. A
 {{\tt posn}\/} structure combines two numbers. That is, a {{\tt posn}\/} is
 a single value that contains two values. We can create a {{\tt posn}\/}
 structure with the operation {{\tt make-posn}\/}, which consumes two
 numbers and makes a {{\tt posn}\/}. For example,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-posn}\  {\tt 3}\  {\tt 4)}\\
{\tt (make-posn}\  {\tt 8}\  {\tt 6)}
\\
{\tt (make-posn}\  {\tt 5}\  {\tt 12)}
\end{tabbing}\end{minipage}\end{center}
 are {{\tt posn}\/} structures. Each of these structures has the same status
 as a number as far as computations are concerned.  Both primitive
 operations and functions can consume and produce structures.

Now consider a function that computes how far some pixel is from the
 origin. The contract, header, and purpose statement are easy to formulate:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt distance-to-0}\ {\tt :}\ {\tt posn}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the distance of {{\tt a-posn}\/} to the origin }
\\
{\tt (define}\  {\tt (distance-to-0}\  {\tt a-posn)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
 In other words, {{\tt distance-to-0}\/} consumes a single value, a
 {{\tt posn}\/} structure, and produces a single value, a number.

We already have some input examples, namely the three {{\tt posn}\/}
 structures mentioned above. What we need next are examples that relate
 inputs and outputs. For points with {{\tt 0}\/} as one of the coordinates,
 the result is the other coordinate:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (distance-to-0}\  {\tt (make-posn}\  {\tt 0}\  {\tt 5))}\\
{\tt =}\  \={\tt 5}
\\
\hbox{;and}
\\
\>{\tt (distance-to-0}\  {\tt (make-posn}\  {\tt 7}\  {\tt 0))}
\\
{\tt =}\  {\tt 7}
\end{tabbing}\end{minipage}\end{center}
 In general, we know from geometry that the distance from the origin to a
 position with coordinates {{\tt x}\/} and {{\tt y}\/} is distance
$$ \sqrt{x^2 + y^2} \ .$$
 Thus, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (distance-to-0}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}\\
{\tt =}\  {\tt 5}
\\
\  \  {\tt (distance-to-0}\  {\tt (make-posn}\  {\tt 8}\  {\tt 6))}
\\
{\tt =}\  {\tt 10}
\\
\  \  {\tt (distance-to-0}\  {\tt (make-posn}\  {\tt 5}\  {\tt 12))}
\\
{\tt =}\  {\tt 13}\  \  
\end{tabbing}\end{minipage}\end{center}

Once we have examples, we can turn our attention to the definition of the
function. The examples imply that the design of {{\tt distance-to-0}\/}
doesn't need to distinguish between different situations. Still, we are
stuck now, because {{\tt distance-to-0}\/} has a single parameter that
represents the entire pixel but we need the two coordinates to compute the
distance.  Put differently, we know how to combine two numbers into a
{{\tt posn}\/} structure using {{\tt make-posn}\/} and we don't know how to
extract these numbers from a {{\tt posn}\/} structure.

Scheme provides operations for extracting values from
structures.\footnote{An alternative terminology is ``to access the fields of a record.'' We prefer to think of structure values as containers from which we can extract other values.} For {{\tt posn}\/} structures, Scheme
supports two such operations: {{\tt posn-x}\/} and {{\tt posn-y}\/}. The
former operation extracts the $x$-coordinate; the latter extracts the
$y$-coordinate.

To describe how {{\tt posn-x}\/}, {{\tt posn-y}\/}, and {{\tt make-posn}\/}
are related, we can use equations that are roughly analogous to the
equations that govern addition and subtraction:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (posn-x}\  {\tt (make-posn}\  {\tt 7}\  {\tt 0))}\  \\
{\tt =}\  {\tt 7}
\end{tabbing}\end{minipage}\end{center}
and  
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (posn-y}\  {\tt (make-posn}\  {\tt 7}\  {\tt 0))}\\
{\tt =}\  {\tt 0}
\end{tabbing}\end{minipage}\end{center}
The equations only confirm what we already know. But suppose we introduce
the following definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (define}\  {\tt a-posn}\  {\tt (make-posn}\  {\tt 7}\  {\tt 0))}\end{tabbing}\end{minipage}\end{center}
Then we can use the two operations as follows in the {\tt Interactions}
window: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (posn-x}\  {\tt a-posn)}\  \\
{\tt =}\  {\tt 7}
\\
\  \  {\tt (posn-y}\  {\tt a-posn)}
\\
{\tt =}\  {\tt 0}
\end{tabbing}\end{minipage}\end{center}
Naturally, we can nest such expressions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (*}\  {\tt (posn-x}\  {\tt a-posn)}\  {\tt 7)}\\
{\tt =}\  {\tt 49}
\\
\  \  {\tt (+}\  {\tt (posn-y}\  {\tt a-posn)}\  {\tt 13)}
\\
{\tt =}\  {\tt 13}
\end{tabbing}\end{minipage}\end{center}

Now we know enough to complete the definition of {{\tt distance-to-0}\/}. We
 know that the function's {{\tt a-posn}\/} parameter is a {{\tt posn}\/}
 structure and that the structure contains two numbers which we can extract
 with {{\tt (posn-x}\ {\tt a-posn)}\/} and {{\tt (posn-y}\ {\tt a-posn)}\/}. Let us add
 this knowledge to our function outline:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (distance-to-0}\  {\tt a-posn)}\\
\>{\tt ...}\  {\tt (posn-x}\  {\tt a-posn)}\  {\tt ...}
\\
\>{\tt ...}\  {\tt (posn-y}\  {\tt a-posn)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Using this outline and the examples, the rest is easy:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (distance-to-0}\  {\tt a-posn)}\\
\>{\tt (s}\={\tt qrt}
\\
\>\>{\tt (+}\  \={\tt (square}\  {\tt (posn-x}\  {\tt a-posn))}
\\
\>\>\>{\tt (square}\  {\tt (posn-y}\  {\tt a-posn)))))}
\end{tabbing}\end{minipage}\end{center}
The function squares {{\tt (posn-x}\ {\tt a-posn)}\/} and {{\tt (posn-y}\ {\tt a-posn)}\/},
which represent the {\it x\/} and {\it y\/} coordinates, sums up the
results, and takes the square root.  With DrScheme, we can also quickly
check that our new function produces the proper results for our examples.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 6.1.1}

 \label{ex:eval-distance-to-0}
Evaluate the following expressions:
\begin{enumerate}
\item {{\tt (distance-to-0}\ {\tt (make-posn}\ {\tt 3}\ {\tt 4))}\/} 
\item {{\tt (distance-to-0}\ {\tt (make-posn}\ {\tt (*}\ {\tt 2}\ {\tt 3)}\ {\tt (*}\ {\tt 2}\ {\tt 4)))}\/} 
\item {{\tt (distance-to-0}\ {\tt (make-posn}\ {\tt 12}\ {\tt (-}\ {\tt 6}\ {\tt 1)))}\/} 
\end{enumerate}
by hand. Show all steps. Assume that {{\tt square}\/} performs its
computation in a single step.  Check the results with DrScheme's
stepper.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/eval-distance-to-0.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


\subsection{Extended Exercise: Drawing Simple Pictures} \label{sec:traffic-draw}

\htmladdimg{../icons/teacher.gif}~{\em This set of exercises introduces a very small number of graphics  routines that are used over and over again throughout the book The goal is  to illustrate the idea of {{\tt posn}\/} structures graphically. The goal  is {\em not\/} to memorize the routines; instead have the students mark  the section and refer back to it for other exercises.  Have some fun, but  don't spend too much time in graphics.}

DrScheme provides the graphics teachpack {\tt\bf draw.ss}, which introduces 
simple graphics operations:
\begin{enumerate}
\item {{\tt draw-solid-line}\/}, which consumes two {{\tt posn}\/}
structures, the beginning and the end of the line on the canvas, and a
color. 

\item {{\tt draw-solid-rect}\/}, which consumes four arguments: 
a {{\tt posn}\/} structure for the upper-left corner of the rectangle, 
a number for the width of the rectangle, another number for its height, and
a color. 

\item {{\tt draw-solid-disk}\/}, which consumes three arguments: 
a {{\tt posn}\/} structure for the center of the disk, a number for the
radius of the disk, and a color. 

\item {{\tt draw-circle}\/}, which consumes three arguments: 
a {{\tt posn}\/} structure for the center of the circle, a number for the
radius, and a color. 
\end{enumerate}
Each of the operation produces {{\tt true}\/}, if it succeeds in changing
the canvas as specified. We refer to the action to the canvas as an
{\sc {effect}}, though we will ignore studying the precise nature of
effects until part~\ref{part:state}. Also, if anything goes wrong with the
operation, it stops the evaluation with an error.

Each drawing operation also comes with a matching {{\tt clear-}\/}
 operation: {{\tt clear-solid-line}\/}, {{\tt clear-solid-rect}\/},
 {{\tt clear-solid-disk}\/}, and {{\tt clear-circle}\/}. If these functions
 are applied to the same arguments as their matching {{\tt draw-}\/}
 function, they clear the corresponding shapes of the
 canvas.\footnote{For more documentation, see DrScheme's Help Desk.}


In contrast to school geometry, drawing operations on computers interpret
positions on the screen differently. First, the origin of the plane is in
the upper-left corner. Second, $y$ coordinates grow in
the downwards direction. Here is a picture: 
\begin{center}
\begin{picture}(150,110)
\put(000,000){\line(0,+1){100}}
\put(000,100){\line(1,+0){125}}
\put(125,100){\line(0,-1){100}}
\put(125,000){\line(-1,0){125}}
\put(-05,105){\circle*{5}}
\put(-05,100){\vector(0,-1){60}}
\put(-15,80){\mbox{\it y\/}}
\put(000,105){\vector(+1,0){60}}
\put(040,110){\mbox{\it x\/}}
\put(30,020){\circle*{4}}
\put(34,024){\mbox{a pixel}}
\put(25,020){\line(+1,0){10}}
\put(30,015){\line(0,+1){10}}
\end{picture}
\end{center}
Understanding this picture is critical for drawing pictures on computer
screens. Study it well! 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 6.2.1}

 \label{ex:draw-fig} \htmlref{\htmladdimg{../icons/plt.gif}}{drn:ex:draw-fig}
Evaluate the following expressions in order:
\begin{enumerate}
\item {{\tt (start}\ {\tt 300}\ {\tt 300)}\/}, which opens a canvas for future drawing
operations; 

\item {{\tt (draw-solid-line}\ {\tt (make-posn}\ {\tt 1}\ {\tt 1)}\ {\tt (make-posn}\ {\tt 5}\ {\tt 5)}\ {\tt RED)}\/}, which
draws a red line;  

\item {{\tt (draw-solid-rect}\ {\tt (make-posn}\ {\tt 20}\ {\tt 10)}\ {\tt 50}\ {\tt 200}\ {\tt BLUE)}\/}, which draws
a blue rectangle of width 50 parallel to the line; and 

\item {{\tt (draw-circle}\ {\tt (make-posn}\ {\tt 200}\ {\tt 10)}\ {\tt 50}\ {\tt RED)}\/}, which draws a red
circle of radius 50 and a center point at the upper line of the rectangle.

\item {{\tt (draw-solid-disk}\ {\tt (make-posn}\ {\tt 200}\ {\tt 10)}\ {\tt 50}\ {\tt GREEN)}\/}, which draws a
green disk of radius 50 and a center point at the height of the upper line
of the rectangle.  

\item {{\tt (stop)}\/}, which closes the canvas.
\end{enumerate}
Read the documentation for {\tt\bf draw.ss} in DrScheme's HelpDesk.~{\rule{3pt}{5pt}}


The definitions and expressions in figure~\ref{fig:draw-traffic} draw a
traffic light. The program fragment illustrates the use of global
definitions for specifying and computing constants. Here, the constants
represent the dimensions of the canvas, which is the outline of the traffic
light, and the positions of three light bulbs.

\hrule

\begin{htmlonly}
\end{htmlonly}

 \begin{rawhtml}  <table  cellspacing=20 bgcolor=beige>  <tr> <td> <pre> ;; dimensions of traffic light (define WIDTH 50) (define HEIGHT 160) (define BULB-RADIUS 20) (define BULB-DISTANCE 10) ;; the positions of the bulbs  (define X-BULBS (quotient WIDTH 2)) (define Y-RED (+ BULB-DISTANCE BULB-RADIUS)) (define Y-YELLOW (+ Y-RED BULB-DISTANCE (* 2 BULB-RADIUS))) (define Y-GREEN (+ Y-YELLOW BULB-DISTANCE (* 2 BULB-RADIUS))) ;; draw the light with the red bulb turned on (start WIDTH HEIGHT) (draw-solid-disk (make-posn X-BULBS Y-RED) BULB-RADIUS RED) (draw-circle (make-posn X-BULBS Y-YELLOW) BULB-RADIUS YELLOW) (draw-circle (make-posn X-BULBS Y-GREEN) BULB-RADIUS GREEN) </pre> </td> <td align=right><img src=../icons/red.gif></td>  </tr>  </table> \end{rawhtml}

\center{Figure: Drawing a traffic light} \label{fig:draw-traffic}
\hrule

{\bf Exercise 6.2.2}

 \label{ex:traffic-draw3}
Develop the function {{\tt clear-bulb}\/}. It consumes a symbol that denotes
one of the possible colors: {{\tt \char'047}{\tt green}\/}, {{\tt \char'047}{\tt yellow}\/}, or
{{\tt \char'047}{\tt red}\/}, and it produces {{\sf true}\/}.  Its effect is ``to turn off''
the matching bulb in the traffic light. Specifically, it should clear the
disk and display a circle instead.

\medskip\noindent{\bf Design Recipe:}:\  See section~\ref{sec:sym} for designing functions
that consume a fixed number of symbols.~{\rule{3pt}{5pt}}

\medskip\noindent{\bf Combining effects:}:\  The primitive operations for drawing and
clearing disks and circles produce {{\tt true}\/}. The natural way to
combine the values and the effects of these functions is to use the
{{\tt and}\/} operation. Later we will study effects in more detail and
learn different ways to combine effects.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/traffic-draw3.html}{\rule{3pt}{5pt}}


{\bf Exercise 6.2.3}

 \label{ex:traffic-draw2}
Develop a function {{\tt draw-bulb}\/}. It consumes a symbols that denotes
one of the possible colors: {{\tt \char'047}{\tt green}\/}, {{\tt \char'047}{\tt yellow}\/}, or
{{\tt \char'047}{\tt red}\/}, and produces {{\sf true}\/}. Its effect is ``to turn on'' the
matching bulb in the traffic light.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/traffic-draw2.html}{\rule{3pt}{5pt}}


{\bf Exercise 6.2.4}

 \label{ex:traffic-draw4}
Develop the function {{\tt switch}\/}. It consumes two symbols, each of
which stands for a traffic light color, and produces {{\sf true}\/}. Its
effects are to clear the bulb for the first color and then to draw the
second bulb.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/traffic-draw4.html}{\rule{3pt}{5pt}}

{\bf Exercise 6.2.5}

 \label{ex:traffic-draw5}
Here is the function {{\tt next}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt next}\ {\tt :}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt symbol}\/}}\\
\hbox{;; to switch a traffic light's current color and to return the next one }
\\
{\tt (d}\={\tt efine}\  {\tt (next}\  {\tt current-color)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (and}\  {\tt (symbol=?}\  {\tt current-color}\  {\tt \char'047}{\tt red)}\  {\tt (switch}\  {\tt \char'047}{\tt red}\  {\tt \char'047}{\tt green))}
\\
\>\>\>{\tt \char'047}{\tt green\char'135}
\\
\>\>{\tt \char'133}\={\tt (and}\  {\tt (symbol=?}\  {\tt current-color}\  {\tt \char'047}{\tt yellow)}\  {\tt (switch}\  {\tt \char'047}{\tt yellow}\  {\tt \char'047}{\tt red))}
\\
\>\>\>{\tt \char'047}{\tt red\char'135}
\\
\>\>{\tt \char'133}\={\tt (and}\  {\tt (symbol=?}\  {\tt current-color}\  {\tt \char'047}{\tt green)}\  {\tt (switch}\  {\tt \char'047}{\tt green}\  {\tt \char'047}{\tt yellow))}
\\
\>\>\>{\tt \char'047}{\tt yellow\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}    
It consumes the current color of a traffic-light (as a symbol) and produces
the next color that the traffic light shows. That is, if the input is
{{\tt \char'047}{\tt green}\/}, it produces {{\tt \char'047}{\tt yellow}\/}; if it is {{\tt \char'047}{\tt yellow}\/},
it produces {{\tt \char'047}{\tt red}\/}; and if it is {{\tt \char'047}{\tt red}\/}, it produces
{{\tt \char'047}{\tt green}\/}.  Its effect is to switch the traffic light from the input
color to the next color.

Use {{\tt draw-bulb}\/} to create a traffic light that is red. Then compose
{{\tt next}\/} with itself to switch the traffic light four
times.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/traffic-draw5.html}{\rule{3pt}{5pt}}
     
\subsection{Structure Definitions}

In the preceding section we explored one particular class of structures:
 the {{\tt posn}\/} structures. A {{\tt posn}\/} structure combines two
 numbers, and it is useful to represent pixels. If we wish to represent
 employee records or points in three-dimensional space, however,
 {{\tt posn}\/}s are useless.  DrScheme therefore permits programmers to
 define their own structures so that they can represent all kinds of
 objects with a fixed number of properties.  

A {\sc {structure definition}} is, as the term says, a new form of
 definition. Here is DrScheme's definition of {{\tt posn}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt posn}\  {\tt (x}\  {\tt y))}\  \  \end{tabbing}\end{minipage}\end{center}
When DrScheme evaluates this structure definition, it creates three
operations for us, which we can use to create data and to program: 
\begin{enumerate}
\item {{\tt make-posn}\/}, the {\sc {constructor}}, which creates
{{\tt posn}\/} structures;

\item {{\tt posn-x}\/}, a {\sc {selector}}, which extracts the $x$-coordinate; 

\item {{\tt posn-y}\/}, also a selector, which extracts the $y$-coordinate.
\end{enumerate}
In general, the names of these new operations are created by prefixing the
name of the structure with ``make-'' and by postfixing the name with all
the field names. This naming convention appears to be complicated but, with
some practice, it is easy to remember.

Now consider the following example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt entry}\  {\tt (name}\  {\tt zip}\  {\tt phone))}\end{tabbing}\end{minipage}\end{center}
The structure represents a simplified entry into an address book.  Each
{{\tt entry}\/} combines three values. We also say that each {{\tt entry}\/}
structure has {\em three\/} fields: {{\tt name}\/}, {{\tt zip}\/}, and
{{\tt phone}\/}.  Because there are three fields, the constructor
{{\tt make-entry}\/} consumes three values. For example,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-entry}\  {\tt \char'047}{\tt PeterLee}\  {\tt 15270}\  {\tt \char'047}{\tt 606-7771)}\end{tabbing}\end{minipage}\end{center}
creates an {{\tt entry}\/} structure with {{\tt \char'047}{\tt PeterLee}\/} in the
{{\tt name}\/}-field, {{\tt 15270}\/} in the {{\tt zip}\/}-field, and
{{\tt \char'047}{\tt 606-7771}\/} in the {{\tt z}\/}-field. 

One way to think of a structure is as a box with as many compartments as
there are fields:
\begin{center}
 \begin{picture}(180,30) 
\put(00,00){\line(+1,0){180}} 
\put(180,00){\line(0,+1){30}} 
\put(180,30){\line(-1,0){180}} 
\put(00,30){\line(0,-1){30}} 
\put(03,22){\mbox{\footnotesize{\emph{name:}}}} 
\put(60,00){\line(0,+1){30}} 
\put(63,22){\mbox{\footnotesize{\emph{zip:}}}} 
\put(120,00){\line(0,+1){30}} 
\put(123,22){\mbox{\footnotesize{\emph{number:}}}} 
\put(10,05){\mbox{\small{{\tt \char'047}{\tt PeterLee}\/}}} 
\put(70,05){\mbox{\small{{\tt 15270}\/}}} 
\put(130,05){\mbox{\small{{\tt 606-7771}\/}}} \end{picture}
\end{center}
The italicized labels name the fields. By putting values in the
compartments, we illustrate specific {{\tt entry}\/} structures.

The {{\tt define-struct}\/} definition of {{\tt entry}\/} also introduces new
selectors:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt entry-name}\  \  \  \  {\tt entry-zip}\  \  \  \  {\tt entry-phone}\end{tabbing}\end{minipage}\end{center}
Here is how we can use the first one: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (entry-name}\  {\tt (make-entry}\  {\tt \char'047}{\tt PeterLee}\  {\tt 15270}\  {\tt \char'047}{\tt 606-7771))}\\
{\tt =}\  {\tt \char'047}{\tt PeterLee}
\end{tabbing}\end{minipage}\end{center}
If we give the structure a name, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt phonebook}\  {\tt (make-entry}\  {\tt \char'047}{\tt PeterLee}\  {\tt 15270}\  {\tt \char'047}{\tt 606-7771))}\end{tabbing}\end{minipage}\end{center}
then we can use the selectors in the {\tt Interactions} window to
extract the data from the three fields: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (entry-name}\  {\tt phonebook)}\\
{\tt =}\  {\tt \char'047}{\tt PeterLee}
\\
\  \  {\tt (entry-zip}\  {\tt phonebook)}
\\
{\tt =}\  {\tt 15270}
\\
\  \  {\tt (entry-phone}\  {\tt phonebook)}
\\
{\tt =}\  {\tt \char'047}{\tt 606-7771}
\end{tabbing}\end{minipage}\end{center}
Put more graphically, a constructor creates a box with several compartments
and puts values in it.  A selector reveals the contents of a particular
compartment, but leaves the box alone. 

Here is one final example, a structure for representing rock stars: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt star}\  {\tt (last}\  {\it first\/}\  {\tt instrument}\  {\tt sales))}\end{tabbing}\end{minipage}\end{center}
It defines the class of {{\tt star}\/} structures, each of which has four
fields. Accordingly, we get five new primitive operations:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt make-star}\  \  \  {\tt star-last}\  \  \  {\tt star-first}\  \  \  \  {\tt star-instrument}\  \  \  \  {\tt star-sales}\end{tabbing}\end{minipage}\end{center}
The first is for constructing {{\tt star}\/} structures; the others are
selector operations for extracting values from a {{\tt star}\/} structure. 

To create a {{\tt star}\/} structure, we apply {{\tt make-star}\/} to three
symbols and a positive integer:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-star}\  {\tt \char'047}{\tt Friedman}\  {\tt \char'047}{\tt Dan}\  {\tt \char'047}{\tt ukelele}\  {\tt 19004)}\\
{\tt (make-star}\  {\tt \char'047}{\tt Talcott}\  {\tt \char'047}{\tt Carolyn}\  {\tt \char'047}{\tt banjo}\  {\tt 80000)}
\\
{\tt (make-star}\  {\tt \char'047}{\tt Harper}\  {\tt \char'047}{\tt Robert}\  {\tt \char'047}{\tt bagpipe}\  {\tt 27860)}
\end{tabbing}\end{minipage}\end{center}
To select the first name of a star structure called {{\tt E}\/}, we use 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (star-first}\  {\tt E)}\end{tabbing}\end{minipage}\end{center}
Other fields are extracted with other selectors. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 6.3.1}

 \label{ex:def-struct-names}
Consider the following structure definitions:
\begin{enumerate}
\item {{\tt (define-struct}\ {\tt movie}\ {\tt (title}\ {\tt producer))}\/}
\item {{\tt (define-struct}\ {\tt boyfriend}\ {\tt (name}\ {\tt hair}\ {\tt eyes}\ {\tt phone))}\/}
\item {{\tt (define-struct}\ {\tt cheerleader}\ {\tt (name}\ {\tt number))}\/}
\item {{\tt (define-struct}\ {\tt CD}\ {\tt (artist}\ {\tt title}\ {\tt price))}\/}
\item {{\tt (define-struct}\ {\tt sweater}\ {\tt (material}\ {\tt size}\ {\tt producer))}\/}
\end{enumerate}
What are the names of the constructors and the selectors that each of them
adds to Scheme? Draw box representations for each of these
structures.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/def-struct-names.html}{\rule{3pt}{5pt}} 


{\bf Exercise 6.3.2}

 \label{ex:eval-cp-x} \label{ex:eval-cp-x-r}
Consider the following structure definition
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt movie}\  {\tt (title}\  {\tt producer))}\end{tabbing}\end{minipage}\end{center}
and evaluate the following expressions:
\begin{enumerate}
\item  {{\tt (movie-title}\ {\tt (make-movie}\ {\tt \char'047}{\tt ThePhantomMenace}\ {\tt \char'047}{\tt Lucas))}\/}
\item  {{\tt (movie-producer}\ {\tt (make-movie}\ {\tt \char'047}{\tt TheEmpireStrikesBack}\ {\tt \char'047}{\tt Lucas))}\/}
\end{enumerate}

Now evaluate the following expressions, assuming {{\tt x}\/} and {{\tt y}\/}
stand for arbitrary symbols:
\begin{enumerate}
\item  {{\tt (movie-title}\ {\tt (make-movie}\ {\tt x}\ {\tt y))}\/}
\item  {{\tt (movie-producer}\ {\tt (make-movie}\ {\tt x}\ {\tt y))}\/}
\end{enumerate}

Formulate equations that state general laws concerning the relationships of
{{\tt movie-title}\/} and {{\tt movie-producer}\/} and
{{\tt make-movie}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/eval-cp-x.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Functions both consume and produce structures. Suppose we need to record an
increase of sales for one of our stars. This act should be recorded in the
star's record.  To do so, we should have a function that consumes a
{{\tt star}\/} structure and produces a {{\tt star}\/} structure with the
same information except for the sales component. Let's assume for now that
the function adds 20000 to the star's sales.

First, we write down a basic description of the function, using our
contract, header, and purpose format: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt increment-sales}\ {\tt :}\ {\tt star}\ {}{\tt ->}{}\ {\tt star}\/}}\\
\hbox{;; to produce a {{\tt star}\/} record like {{\tt a-star}\/} with {{\tt 20000}\/} more sales }
\\
{\tt (define}\  {\tt (increment-sales}\  {\tt a-star)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Here is an example of how the function should process {{\tt star}\/} structures: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (increment-sales}\  {\tt (make-star}\  {\tt \char'047}{\tt Abba}\  {\tt \char'047}{\tt John}\  {\tt \char'047}{\tt vocals}\  {\tt 12200))}\\
{\tt =}\  {\tt (make-star}\  {\tt \char'047}{\tt Abba}\  {\tt \char'047}{\tt John}\  {\tt \char'047}{\tt vocals}\  {\tt 32200))}
\end{tabbing}\end{minipage}\end{center}
The three sample {{\tt star}\/} structures from above are also good
examples of potential inputs. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt increment-sales}\ {\tt :}\ {\tt star}\ {}{\tt ->}{}\ {\tt star}\/}}\\
\hbox{;; to produce a {{\tt star}\/} record like {{\tt a-star}\/} with {{\tt 20000}\/} more sales}
\\
{\tt (d}\={\tt efine}\  {\tt (increment-sales}\  {\tt a-star)}
\\
\>{\tt (make-star}\  \={\tt (star-last}\  {\tt a-star)}
\\
\>\>{\tt (star-first}\  {\tt a-star)}
\\
\>\>{\tt (star-instrument}\  {\tt a-star)}
\\
\>\>{\tt (+}\  {\tt (star-sales}\  {\tt a-star)}\  {\tt 20000)))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The complete definition of {\it increment-sales\/}} \label{fig:increment-sales}
\hrule

The {{\tt increment-sales}\/} function must construct a new {{\tt star}\/}
structure with {{\tt make-star}\/}, but to do so, it must also extract the
data in {{\tt a-star}\/}. After all, almost all of the data in
{{\tt a-star}\/} is a part of the {{\tt star}\/} structure produced by
{{\tt increment-sales}\/}.  This suggests that the definition of
{{\tt increment-sales}\/} contains expressions that extract the four
fields of {{\tt a-star}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (increment-sales}\  {\tt a-star)}\  \\
\>{\tt ...}\  {\tt (star-last}\  {\tt a-star)}\  {\tt ...}
\\
\>{\tt ...}\  {\tt (star-first}\  {\tt a-star)}\  {\tt ...}
\\
\>{\tt ...}\  {\tt (star-instrument}\  {\tt a-star)}\  {\tt ...}
\\
\>{\tt ...}\  {\tt (star-sales}\  {\tt a-star)}\  {\tt ...}\  {\tt )}
\end{tabbing}\end{minipage}\end{center}
As we have seen with the examples, the function adds {{\tt 20000}\/} to
{{\tt (star-sales}\ {\tt a-star)}\/} and assembles the four pieces of data into a
{{\tt star}\/} structure with
{{\tt make-star}\/}. Figure~\ref{fig:increment-sales} contains the complete
definition.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 6.3.3}

 \label{ex:data-def1} \label{ex:data-def1-r}
Provide a structure definition that represents the airforce's jet fighters.
Assume that a fighter's essential properties are
\begin{enumerate}
\item designation ({{\tt \char'047}{\tt f22}\/}, {{\tt \char'047}{\tt tornado}\/}, or {{\tt \char'047}{\tt mig22}\/}), 
\item acceleration, 
\item top-speed, and
\item range. 
\end{enumerate}
Then develop the function {{\tt within-range}\/}. The function consumes a
fighter record and the distance of a target from the (fighter's) base. It
determines whether the fighter can reach the intended target. 

Also develop the function {{\tt reduce-range}\/}. The function consumes a
fighter record and produces a similar one, with range reduced to 80\% of
its original value.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/data-def1.html}{\rule{3pt}{5pt}}


\subsection{Data Definitions} 
\label{sec:records:cons-sel} 

Consider the following expression: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-posn}\  {\tt \char'047}{\tt Albert}\  {\tt \char'047}{\tt Meyer)}\end{tabbing}\end{minipage}\end{center}
It constructs a {{\tt posn}\/} structure from two symbols. If we now apply
{{\tt distance-to-0}\/} to this structure, the computation fails miserably: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (distance-to-0}\  {\tt (make-posn}\  {\tt \char'047}{\tt Albert}\  {\tt \char'047}{\tt Meyer))}\\
{\tt =}\  {\tt (s}\={\tt qrt}
\\
\>{\tt (+}\  \={\tt (square}\  {\tt (posn-x}\  {\tt (make-posn}\  {\tt \char'047}{\tt Albert}\  {\tt \char'047}{\tt Meyer)))}
\\
\>\>{\tt (square}\  {\tt (posn-y}\  {\tt (make-posn}\  {\tt \char'047}{\tt Albert}\  {\tt \char'047}{\tt Meyer)))))}
\\
{\tt =}\  {\tt (s}\={\tt qrt}
\\
\>{\tt (+}\  \={\tt (square}\  {\tt \char'047}{\tt Albert)}
\\
\>\>{\tt (square}\  {\tt (posn-y}\  {\tt (make-posn}\  {\tt \char'047}{\tt Albert}\  {\tt \char'047}{\tt Meyer)))))}
\\
{\tt =}\  {\tt (s}\={\tt qrt}
\\
\>{\tt (+}\  \={\tt (*}\  {\tt \char'047}{\tt Albert}\  {\tt \char'047}{\tt Albert)}
\\
\>\>{\tt (square}\  {\tt (posn-y}\  {\tt (make-posn}\  {\tt \char'047}{\tt Albert}\  {\tt \char'047}{\tt Meyer)))))}
\end{tabbing}\end{minipage}\end{center}
That is, it requires us to multiply {{\tt \char'047}{\tt Albert}\/} with itself. Similarly,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-star}\  {\tt \char'047}{\tt Albert}\  {\tt \char'047}{\tt Meyer}\  {\tt 10000}\  {\tt \char'047}{\tt electric-organ)}\end{tabbing}\end{minipage}\end{center}
does not produce a {{\tt star}\/} structure according to our intentions. In
particular, the structure is not suitable for processing by
{{\tt increment-sales}\/}. 

To avoid such problems and to assist with the development of functions, we
must add a {data definition} to each structure definition. A {\sc {data definition}} states, in a mixture of English and Scheme, how we intend to use
a class of structures. For example, here is a data definition for
{{\tt posn}\/} structures:

  \begin{quote} A {\sl {posn}} is a structure:\\
         \centerline{{{\tt (make-posn}\ {\tt x}\ {\tt y)}\/}}       where {{\tt x}\/} and {{\tt y}\/} are numbers. \end{quote}

\noindent It says that a valid {{\tt posn}\/} structure always contains two
numbers, and nothing else. Hence, when we use {{\tt make-posn}\/} to create
a {{\tt posn}\/} structure, we must apply it to two numbers; when a function
contains selector expressions for {{\tt posn}\/} structures, we may now
assume that their result is a number.

The data definition for {{\tt star}\/} structures is only slightly more
complicated: 

  \begin{quote} An {\sl {star}} is a structure:\\
         \centerline{{{\tt (make-star}\ {\tt last}\ {\it first\/}\ {\tt instrument}\ {\tt sales)}\/}}       where {{\tt last}\/}, {{\it first\/}\/}, and {{\tt instrument}\/} are symbols       and {{\tt sales}\/} is a number. \end{quote}

\noindent This data definition says that valid {{\tt star}\/} structures
contain symbols in the fields for {{\tt last}\/} name, {{\it first\/}\/} name,
and {{\tt instrument}\/}, and a number in the {{\tt sales}\/} field.

\hrule
\begin{center}
\begin{picture}(223,82)
\put(0,0){\thicklines\begin{picture}(223,82) 
\put(5,75){\mztpMeasure{the subcollection designated by a data definition}{T1QQpk7fHyNXZCPbzWqt-V1AOYZevtxWpsy35Vgok6j3QMEcCndFTquCDvK}} 
\put(0,0){\mztpMeasure{the collection of all Scheme data}{T7jhBgoZ-PpBNZde+swTVA67pIgkYuzbJOEcC7Fx}} \qbezier[35](173,40)(189,40)(189,32) \qbezier[35](189,32)(189,24)(173,24) \qbezier[22](173,24)(165,24)(165,32) \qbezier[22](165,32)(165,40)(173,40) \qbezier[80](199,64)(223,64)(223,32) \qbezier[90](223,32)(223,0)(191,0) \qbezier[124](191,0)(143,0)(143,40) \qbezier[121](143,40)(143,64)(199,64) 
\put(68,10){\vector(3,1){72.0}} 
\put(177,70){\vector(0,-1){27}} \end{picture}}
\end{picture}
\end{center}

\center{Figure: The meaning of data definitions} \label{fig:data-class}
\hrule

In general, a data definition identifies a subclass of Scheme's universe of
values: see figure~\ref{fig:data-class}. As we have seen so far, Scheme's
universe contains numbers, symbols, images, strings, chars, booleans, and
many different classes of structures. Our functions, however, are intended
to work only for a subclass of values. For example, {{\tt area-of-disk}\/}
consumes only numbers; {{\tt reply}\/} from section~\ref{sec:sym} consumes
only symbols.  A few subclasses, such as {{\tt number}\/}, already have
names, because they are useful for all kinds of programming tasks. Others
are only interesting in the context of a specific problem.  For those
cases, a programmer should introduce a data definition.

The most important role of a data definition is that of a covenant between
programmers and users. We expect both groups to respect such data
definitions, and we expect the programmer to exploit it for the function
construction. For example, when the programmer of {{\tt distance-to-0}\/}
specifies that all {{\tt posn}\/}s contain two numbers, a user must always
apply {{\tt distance-to-0}\/} to a {{\tt posn}\/} structure with two
numbers. Furthermore, as we will discuss over the next few sections, we
expect a programmer to exploit data definitions for function
developments. Naturally, a data definition in English and Scheme does not
prevent us from abusing {{\tt make-posn}\/}.  It is, however, a written
statement of intent, and a person who willingly violates or ignores this
covenant must face the consequences of ill-behaving
computations.\footnote{DrScheme provides an optional tool that permits programmers to check whether users and programmers respect the data definition for a particular structure. To do so, a programmer must state data definitions in a special language. Although checking the adherence to data definitions is important for large programs, an introductory course can avoid this topic.}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 6.4.1}

 \label{ex:def-struct-dd}
Provide data definitions for the following structure definitions:
\begin{enumerate}
\item {{\tt (define-struct}\ {\tt movie}\ {\tt (title}\ {\tt producer))}\/}
\item {{\tt (define-struct}\ {\tt boyfriend}\ {\tt (name}\ {\tt hair}\ {\tt eyes}\ {\tt phone))}\/}
\item {{\tt (define-struct}\ {\tt cheerleader}\ {\tt (name}\ {\tt number))}\/}
\item {{\tt (define-struct}\ {\tt CD}\ {\tt (artist}\ {\tt title}\ {\tt price))}\/}
\item {{\tt (define-struct}\ {\tt sweater}\ {\tt (material}\ {\tt size}\ {\tt producer))}\/}
\end{enumerate}
Make appropriate assumptions about what data goes with which
field.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/def-struct-dd.html}{\rule{3pt}{5pt}}


{\bf Exercise 6.4.2}

 \label{ex:time-dd}
Provide a structure definition and a data definition for representing
 points in time since midnight. A point in time consists of three numbers:
 hours, minutes, and seconds.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/time-dd.html}{\rule{3pt}{5pt}}


{\bf Exercise 6.4.3}

 \label{ex:word-dd} 
Provide a structure definition and a data definition for representing
 three-letter {{\tt word}\/}s. A word consists of letters, which we
 represent with the symbols {{\tt \char'047}{\tt a}\/} through
 {{\tt \char'047}{\tt z}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/word-dd.html}{\rule{3pt}{5pt}}
 

\subsection{Designing Functions for Compound Data} 
\label{sec:design3} 

Sections~\ref{sec:structures} through~\ref{sec:records:cons-sel} suggest
that the design of functions for compound data proceeds in a regular
manner. First, a programmer must recognize that structures are needed.  We
follow the simple rule of using structures whenever the description of some
object specifies several pieces of information. If we don't use structures
in these cases, we quickly lose track of which data belongs to which
object, especially when we write large functions which process massive
amounts of data.

Second, a programmer can use the structure and data definitions for the
organization of a function. We use the term ``template'' when we design
functions. As we will see in this and many future sections, the template
matches the data definition, and the template is {\em the\/} essential step
in the careful design of functions. 

To emphasize this point, we modify our function design recipe from
section~\ref{sec:design1} to accommodate compound data.  Most importantly,
working with compound data requires adjustments in a few of the basic
design steps and two new steps: data analysis and template design: 

\begin{description}
\item[Data Analysis \& Design:] Before we can develop a function, we must
understand how to represent the information in our problem statement within
our chosen programming language. To do so, we search the problem statement
for descriptions of (relevant) objects and then design a data
representation based on the results of our analysis.

Until now we could use Scheme's classes of atomic data (numbers, symbols,
images, etc.) to represent information. But they are not enough.  If we
discover that an object has $N$ properties, we introduce a structure
definition with $N$ fields and supply a data definition that specifies what
kind of data the fields may contain.

Let us consider functions that process student records at a school.  If a
student's interesting properties for a school are
\begin{enumerate}
\item the first name,
\item the last name,
and
\item the name of the home-room teacher, 
\end{enumerate}
then we should represent information about a student as a structure:

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt student}\  {\tt (last}\  {\it first\/}\  {\tt teacher))}\end{tabbing}\end{minipage}\end{center}

Here is the data definition that specifies the class of student
structures as precisely as possible: 

\begin{quote} A {\sl {student}} is a structure:\\
     \centerline{ {{\tt (make-student}\ {\tt l}\ {\tt f}\ {\tt t)}\/} }      where {{\tt l}\/}, {{\tt f}\/}, and {{\tt t}\/} are symbols. \end{quote}

The corresponding data class contains structures like these:

\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (make-student}\  {\tt \char'047}{\tt findler}\  {\tt \char'047}{\tt kathi}\  {\tt \char'047}{\tt matthias)}\\
\>{\tt (make-student}\  {\tt \char'047}{\tt fisler}\  {\tt \char'047}{\tt sean}\  {\tt \char'047}{\tt matthias)}
\\
\>{\tt (make-student}\  {\tt \char'047}{\tt flatt}\  {\tt \char'047}{\tt shriram}\  {\tt \char'047}{\tt matthias)}
\end{tabbing}\end{minipage}

\item[Contract:] For the formulation of contracts, we can use the names of
the atomic classes of data, such as {{\tt number}\/} and {{\tt symbol}\/},
and those names that we introduced in data definitions, such as
{{\tt student}\/}.

\item[Template:] A function that consumes compound data is likely to compute
its result from the components of its input. To remind ourselves of the
components, we first design a {template}. For compound data, a
{\sc {template}} consists of a header and a body that lists all possible
{selector expressions}. Each selector expression is the application of an
appropriate selector primitive to a parameter that stands for a structure.

In other words, a template expresses what we know about the inputs, and
nothing about the outputs. We can therefore use the same template for all
functions that consume the same kind of structure. Also, because a template
does not express anything about the purpose of the function, we can
formulate before or after we have developed examples that illustrate a
function's purpose. 

Consider a function that consumes a {{\tt student}\/} structure and a teacher
name: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt process-student}\ {\tt :}\ {\tt student}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt \char'077}{\tt \char'077}{\tt \char'077}\/}}\\
{\tt (define}\  {\tt (process-student}\  {\tt a-student}\  {\tt a-teacher)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center} 
Then {{\tt a-student}\/} is a parameter that stands for a structure and
{{\tt a-teacher}\/} stands for just a symbol. The template therefore has the
following shape:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt process-student}\ {\tt :}\ {\tt student}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt \char'077}{\tt \char'077}{\tt \char'077}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (process-student}\  {\tt a-student}\  {\tt a-teacher)}\  
\\
\>{\tt ...}\  {\tt (student-last}\  {\tt a-student)}\  {\tt ...}
\\
\>{\tt ...}\  {\tt (student-first}\  {\tt a-student)}\  {\tt ...}
\\
\>{\tt ...}\  {\tt (student-teacher}\  {\tt a-student)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center} 
The {{\tt \char'077}{\tt \char'077}{\tt \char'077}\/} output reminds us that we don't assume anything about the
output of the function. We design every function that consumes a
{{\tt student}\/} structure using this template. 

\item[Examples:] Let us study two examples of functions that consume
{{\tt student}\/} structures. The first function, {{\tt check}\/}, is supposed
to return the last name of the student if the teacher's name is equal to
{{\tt a-teacher}\/} and {{\tt \char'047}{\tt none}\/} otherwise: 

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (check}\  {\tt (make-student}\  {\tt \char'047}{\tt Wilson}\  {\tt \char'047}{\tt Fritz}\  {\tt \char'047}{\tt Harper)}\  {\tt \char'047}{\tt Harper)}\\
{\tt =}\  {\tt \char'047}{\tt Wilson}
\\
\  \  {\tt (check}\  {\tt (make-student}\  {\tt \char'047}{\tt Wilson}\  {\tt \char'047}{\tt Fritz}\  {\tt \char'047}{\tt Lee)}\  {\tt \char'047}{\tt Harper)}
\\
{\tt =}\  {\tt \char'047}{\tt none}
\end{tabbing}\end{minipage}\end{center}

The second function, {{\tt transfer}\/}, is supposed to produce a student
structure that contains the same information as {{\tt a-student}\/} except
for the teacher field, which should be {{\tt a-teacher}\/}: 

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (transfer}\  {\tt (make-student}\  {\tt \char'047}{\tt Wilson}\  {\tt \char'047}{\tt Fritz}\  {\tt \char'047}{\tt Harper)}\  {\tt \char'047}{\tt Lee)}\\
{\tt =}\  {\tt (make-student}\  {\tt \char'047}{\tt Wilson}\  {\tt \char'047}{\tt Fritz}\  {\tt \char'047}{\tt Lee)}
\\
\  \  {\tt (transfer}\  {\tt (make-student}\  {\tt \char'047}{\tt Woops}\  {\tt \char'047}{\tt Helen}\  {\tt \char'047}{\tt Flatt)}\  {\tt \char'047}{\tt Fisler)}
\\
{\tt =}\  {\tt (make-student}\  {\tt \char'047}{\tt Woops}\  {\tt \char'047}{\tt Helen}\  {\tt \char'047}{\tt Fisler)}
\end{tabbing}\end{minipage}\end{center}

\item[Body:] The template provides as many clues for the definition of the
function as the examples. As before, the goal of this step is to formulate
an expression that computes the answer from the available data using other
functions or Scheme's primitive. The template reminds us that the available
data are the parameters and the data computed by the selector expressions.
To determine what the selectors produce, we read the data definition for
the structure. 

Let us return to our first example, {{\tt check}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (check}\  {\tt a-student}\  {\tt a-teacher)}\  \\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (student-teacher}\  {\tt a-student)}\  {\tt a-teacher)}\  
\\
\>\>\>{\tt (student-last}\  {\tt a-student)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt \char'047}{\tt none\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
This particular function uses two of the three selector expressions from the
template. Specifically, it compares the result of the selector expression 
{{\tt (student-teacher}\ {\tt a-student)}\/} with {{\tt a-teacher}\/} and, if they
are equal, produces the result of {{\tt (student-last}\ {\tt a-student)}\/}. Just
naming the results of the selector expressions and reading the problem
statement makes the definition obvious. 

Similarly, the {{\tt transfer}\/} function is easy to define using the
template and the examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (transfer}\  {\tt a-student}\  {\tt a-teacher)}\  \\
\>{\tt (make-student}\  \={\tt (student-last}\  {\tt a-student)}\  
\\
\>\>{\tt (student-first}\  {\tt a-student)}
\\
\>\>{\tt a-teacher))}
\end{tabbing}\end{minipage}\end{center}
This second function uses the same two selector expressions as the first
example, but in a different way. The key observation, however, is that the
template reminds us of all the information that we have available. {\bf When we define the function, we must use and combine the available information.} 
\end{description}
Figure~\ref{fig:design3} presents the recipe for compound data in tabular
form. In practice, though, a function contains many functions that all work
on the same class of input data.  It is therefore normal to reuse the
template many times, which means that examples are often constructed after
the template is set up.  Compare it with the design recipe in
figure~\ref{fig:design1} and its extension in figure~\ref{fig:design2}. The
figure suggests to make up examples before setting up the template.

\htmladdimg{../icons/teacher.gif}~{\em Again, none of these skills are computer-specific; all of them are  needed to solve all kinds of problems. Even lawyers and doctors, artisans  and secretaries can benefit from laying out their work along these lines.  For example, a lawyer must analyze a case, lay out the data in the case,  state a purpose (goal), lay out a strategy (template), and fill in  gaps. The test is then conducted in the real world, and to avoid  unanticipated problems, good lawyers do mock tests.}

%% BEGIN INLINED FILE: idesign3.dl.tex
%% BEGIN INLINED FILE: design1.dl.tex






%% BEGIN INLINED FILE: design3.dl.tex





\hrule
\begin{center} \begin{tabular}{|l|l|l|}\hline Phase            &  Goal 		& Activity \\
 \hline\hline \begin{minipage}[t]{.60in} Data\\
~~Analysis\\
~~\& Design \end{minipage}  & \begin{minipage}[t]{1.6in}\raggedright to formulate a group of data definitions \rule{0pt}{6pt}\end{minipage} & \begin{minipage}[t]{2.2in}\raggedright {determine how many pieces of data describe the ``interesting'' aspects of a typical object mentioned in the problem statement $\bullet$\  add a structure definition and a data definition (for each class of problem object) }\rule{0pt}{6pt}\end{minipage}   \\
 \hline \begin{minipage}[t]{.60in} Contract\\
 Header~\&\\
 Purpose\\
 \end{minipage} 	&    \begin{minipage}[t]{1.6in}\raggedright to name the program,\\
  	 to specify its inputs \& \\
~~outputs,\\
 	 to describe its purpose,\\
 	 to formulate a program \\
~~header  \rule{0pt}{6pt}\end{minipage}  & \begin{minipage}[t]{2.2in}\raggedright {name the function, its inputs, its output, and specify its purpose: \\
   ~;; {\it name\/} : {\it in1\/} {\it in2\/} \ldots $\rightarrow$ {\it out\/} \\
   ~;; {\bf Purpose}: to compute \ldots\ {\it x1\/} \ldots {\it x2\/} \\
   ~({\bf define} ({\it name x1 x2 \ldots\/}) \ldots) }\rule{0pt}{6pt}\end{minipage} \\
 \hline Examples 	& \begin{minipage}[t]{1.6in}\raggedright to characterize the input-\\
 output relationship via examples \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {search the problem for examples $\bullet$\  add examples to function (input(s), output) $\bullet$\  validate the examples using the problem statement, if possible}\rule{0pt}{6pt}\end{minipage}   \\
 \hline Template        & \begin{minipage}[t]{1.6in}\raggedright to formulate an outline for a group of programs \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {for those parameters that stand for compound values, annotate the body with selector expressions $\bullet$\  if the function is conditional, annotate all appropriate branches }\rule{0pt}{6pt}\end{minipage}   \\
 \hline Body	 	& \begin{minipage}[t]{1.6in}\raggedright to define the program \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {develop a Scheme expression that uses Scheme's primitive operations, other functions, selector expressions, and the variables}\rule{0pt}{6pt}\end{minipage}   \\
 \hline Test		& \begin{minipage}[t]{1.6in}\raggedright to expose mistakes\\
~~(``typos'' and logic) \rule{0pt}{6pt}\end{minipage} & \begin{minipage}[t]{2.2in}\raggedright {apply the function to the inputs of the examples  $\bullet$\  check that the outputs are as predicted above}\rule{0pt}{6pt}\end{minipage}   \\
 \hline \end{tabular} \end{center}
\center{Figure: The revised design recipe for compound data} \label{fig:design3}
\centerline{(Refines the recipe in figure~\ref{fig:design1} (pg.~\pageref{fig:design1}))}
\hrule

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline Data Analysis \& Definitions}:}\\
{\tt (define-struct}\  {\tt student}\  {\tt (last}\  {\it first\/}\  {\tt teacher))}
\\
\hbox{;; A {{\tt student}\/} is a structure:}
\\
\hbox{;; {{\tt (make-student}\ {\tt l}\ {\tt f}\ {\tt t)}\/}}
\\
\hbox{;; where {{\tt f}\/}, {{\tt l}\/}, and {{\tt t}\/} are symbols.}
\\
\hbox{;; {\underline Contract}: {{\tt subst-teacher}\ {\tt :}\ {\tt student}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt student}\/}}
\\
\hbox{;; {\underline Purpose}: to create a {{\tt student}\/} structure with a new }
\\
\hbox{;; teacher name if the teacher's name matches {{\tt \char'047}{\tt Fritz}\/}}
\\
\hbox{;; {\underline Examples}:}
\\
\hbox{;; {{\tt (subst-teacher}\ {\tt (make-student}\ {\tt \char'047}{\tt Find}\ {\tt \char'047}{\tt Matthew}\ {\tt \char'047}{\tt Fritz)}\ {\tt \char'047}{\tt Elise)}\/}}
\\
\hbox{;; = }
\\
\hbox{;; {{\tt (make-student}\ {\tt \char'047}{\tt Find}\ {\tt \char'047}{\tt Matthew}\ {\tt \char'047}{\tt Elise)}\/}}
\\
\hbox{;; {{\tt (subst-teacher}\ {\tt (make-student}\ {\tt \char'047}{\tt Find}\ {\tt \char'047}{\tt Matthew}\ {\tt \char'047}{\tt Amanda)}\ {\tt \char'047}{\tt Elise)}\/}}
\\
\hbox{;; = }
\\
\hbox{;; {{\tt (make-student}\ {\tt \char'047}{\tt Find}\ {\tt \char'047}{\tt Matthew}\ {\tt \char'047}{\tt Amanda)}\/}}
\\
\hbox{;; {\underline Template}:}
\\
\hbox{;; {{\tt (define}\ {\tt (process-student}\ {\tt a-student}\ {\tt a-teacher)}\ \/}}
\\
\hbox{;; {\ {\tt ...}\ {\tt (student-last}\ {\tt a-student)}\ {\tt ...}\/}}
\\
\hbox{;; {\ {\tt ...}\ {\tt (student-first}\ {\tt a-student)}\ {\tt ...}\/}}
\\
\hbox{;; {\ {\tt ...}\ {\tt (student-teacher}\ {\tt a-student)}\ {\tt ...)}\/}}
\\
\hbox{;; {\underline Definition}: }
\\
{\tt (d}\={\tt efine}\  {\tt (subst-teacher}\  {\tt a-student}\  {\tt a-teacher)}\  
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (student-teacher}\  {\tt a-student)}\  {\tt \char'047}{\tt Fritz)}\  
\\
\>\>\>{\tt (make-student}\  \={\tt (student-last}\  {\tt a-student)}
\\
\>\>\>\>{\tt (student-first}\  {\tt a-student)}
\\
\>\>\>\>{\tt a-teacher)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt a-student\char'135}{\tt ))}
\\
\>
\\
\hbox{;; {\underline Tests}:}
\\
{\tt (subst-teacher}\  {\tt (make-student}\  {\tt \char'047}{\tt Find}\  {\tt \char'047}{\tt Matthew}\  {\tt \char'047}{\tt Fritz)}\  {\tt \char'047}{\tt Elise)}
\\
{\tt =}\  
\\
{\tt (make-student}\  {\tt \char'047}{\tt Find}\  {\tt \char'047}{\tt Matthew}\  {\tt \char'047}{\tt Elise)}
\\
{\tt (subst-teacher}\  {\tt (make-student}\  {\tt \char'047}{\tt Find}\  {\tt \char'047}{\tt Matthew}\  {\tt \char'047}{\tt Amanda)}\  {\tt \char'047}{\tt Elise)}
\\
{\tt =}\  
\\
{\tt (make-student}\  {\tt \char'047}{\tt Find}\  {\tt \char'047}{\tt Matthew}\  {\tt \char'047}{\tt Amanda)}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The design recipe for compound data: a complete example}
\label{fig:design3-example}
\hrule 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 6.5.1}

 \label{ex:data-def-ex1} \label{ex:data-def-ex1-r}
Develop templates for functions that consumes the following structures: 
\begin{enumerate}
\item {{\tt (define-struct}\ {\tt movie}\ {\tt (title}\ {\tt producer))}\/}
\item {{\tt (define-struct}\ {\tt boyfriend}\ {\tt (name}\ {\tt hair}\ {\tt eyes}\ {\tt phone))}\/}
\item {{\tt (define-struct}\ {\tt cheerleader}\ {\tt (name}\ {\tt number))}\/}
\item {{\tt (define-struct}\ {\tt CD}\ {\tt (artist}\ {\tt title}\ {\tt price))}\/}
\item {{\tt (define-struct}\ {\tt sweater}\ {\tt (material}\ {\tt size}\ {\tt producer))}\/}
\end{enumerate}
None of the functions consume any other data.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/data-def-ex1.html}{\rule{3pt}{5pt}}


{\bf Exercise 6.5.2}

 \label{ex:data-def-ex1-split}
Data definitions and templates are useful for classes of data other
than structures. Here is a simple example:

  \begin{quote} A {\sl {very-small-int}} is an integer between {{\tt 1}\/} and   {{\tt 4}\/} (inclusively). \end{quote} 

Develop a template for functions that consume
{{\tt very-small-int}\/}s.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/data-def-ex1-split.html}{\rule{3pt}{5pt}} 


{\bf Exercise 6.5.3}

 \label{ex:time}
Develop the function {\mbox{\it time\/}{{}{\tt ->}{}}\mbox{\it seconds\/}\/}, which consumes a {{\tt time}\/}
structure (see exercise~\ref{ex:time-dd}) and produces the number of seconds
since midnight that the {{\tt time}\/} structure represents:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (}\mbox{\it time\/}{{}{\tt ->}{}}\mbox{\it seconds\/}\  {\tt (make-time}\  {\tt 0}\  {\tt 0}\  {\tt 2))}\  {\tt =}\  {\tt 2}\\
{\tt (}\mbox{\it time\/}{{}{\tt ->}{}}\mbox{\it seconds\/}\  {\tt (make-time}\  {\tt 0}\  {\tt 30}\  {\tt 2))}\  {\tt =}\  {\tt 1802}
\\
{\tt (}\mbox{\it time\/}{{}{\tt ->}{}}\mbox{\it seconds\/}\  {\tt (make-time}\  {\tt 12}\  {\tt 30}\  {\tt 2))}\  {\tt =}\  {\tt 45002}\  
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/time.html}{\rule{3pt}{5pt}}


\subsection{Extended Exercise: Moving Circles and Rectangles}
\label{sec:move-circle} 

Implementing a computer game often requires that a picture move across a
computer monitor. In figure~\ref{fig:moving}, for example, a simplistic
face is moved from the left part of the canvas toward the right border.  We
can easily imagine more complex situations.

\hrule
  \begin{rawhtml}  <center>  <table  cellspacing=20 bgcolor=beige>    <tr><td align=center><img src=../icons/moving1.gif></td></tr>    <tr><td align=center><img src=../icons/moving2.gif></td></tr>  </table>  </center>  \end{rawhtml} 
\center{Figure: A moving face} \label{fig:moving}
\hrule

For simplicity, our pictures consist of many colored circles and rectangles. Here
we learn to move these basic shapes across a canvas. From
section~\ref{sec:traffic-draw}, we already know, for example, how to draw and
erase a circle. Here we learn to translate a circle, that is, to move its
representation in a particular direction. In sections~\ref{sec:move-shape},
\ref{sec:move-fig}, and~\ref{sec:abs-moving} we learn to move entire pictures
with compact programs.\footnote{This series of sections was inspired by Ms.\ Karen Buras and her son.}

\medskip\noindent{\bf A First Note on Iterative Refinement}:\  This method of developing large
programs is our first taste of {\sc {iterative refinement}}.  The basic idea
behind iterative refinement is to start with a simple version of the program,
that is, a version that deals with the most important part of the problem. Here
we start with functions that move the most basic shapes: circles and
rectangles. Then we refine the program to deal with more and more complex
situations.  For example, in section~\ref{sec:move-fig} we learn to deal with
pictures that consist of an arbitrary number of circles and rectangles. Once we
have a complete program, we edit it so that others can easily read and modify
it, too. Section~\ref{sec:abs-moving} covers this aspect of the ``moving
pictures'' example. 

Refining a program in this manner is the most prevalent method of designing
complex programs.  Of course, we must know the eventual goal for this method to
succeed, and we must always keep it in mind. It is therefore a good idea to
write down an action plan, and to re-consider the plan after each refinement
step. We will discuss this process again in section~\ref{sec:files}.~{\rule{3pt}{5pt}}

Following the design recipe, we start with structure and data definitions, then
move on to templates, and finally write the necessary functions. The first
sequence of exercises covers circles; the second one is about rectangles. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 6.6.1}

 \label{ex:circ-descr}
Provide a structure definition and a data definition for representing
colored circles.  A {\sl circle} is characterized by three pieces of
information: its center, its radius, and the color of its perimeter. The
first is a {{\tt posn}\/} structure, the second a number, and the third a
color defined by the teachpack {\tt\bf draw.ss}.

Develop the template {{\tt fun-for-circle}\/}, which outlines a function
that consumes {{\tt circle}\/}s and produces different classes of
data.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/circ-descr.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 6.6.2}

 \label{ex:circ-draw} \label{ex:circ-draw-r2}
Use the template {{\tt fun-for-circle}\/} to develop the function
{{\tt draw-a-circle}\/}. It consumes a {{\tt circle}\/} structure and draws
the corresponding circle on the screen.  Use {{\tt (start}\ {\tt 200}\ {\tt 200)}\/} to
create the canvas before testing.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/circ-draw.html}{\rule{3pt}{5pt}}


{\bf Exercise 6.6.3}

 \label{ex:circ-in}
Use the template {{\tt fun-for-circle}\/} to develop
{{\tt in-circle\char'077}\/}. The function consumes a {{\tt circle}\/} structure and
a {{\tt posn}\/} and determines whether or not the pixel is inside the
circle. All pixels whose distance to the center is less or equal to the
radius are inside the circle; the others are outside.

Consider the circle in figure~\ref{fig:geo-in}.  The circle's center is
{{\tt (make-posn}\ {\tt 6}\ {\tt 2)}\/}, its radius is {{\tt 1}\/}. The pixel labeled
{{\tt A}\/}, located at {{\tt (make-posn}\ {\tt 6}\ {\tt 1.5)}\/}, is inside the
circle. The pixel labeled {{\tt B}\/}, located at {{\tt (make-posn}\ {\tt 8}\ {\tt 6)}\/},
is outside.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/circ-in.html}{\rule{3pt}{5pt}}


{\bf Exercise 6.6.4}

 \label{ex:circ-trans} 
Use the template {{\tt fun-for-circle}\/} to develop
{{\tt translate-circle}\/}. The function consumes a {{\tt circle}\/}
structure and a number {{\tt delta}\/}. The result is a {{\tt circle}\/}
whose center is {{\tt delta}\/} pixels to the right of the input. Note that
the function has no effect on the canvas.

\medskip\noindent{\bf Translation}:\  Moving a geometric shape along a straight line is
referred to as a {\sl translation}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/circ-trans.html}{\rule{3pt}{5pt}}


{\bf Exercise 6.6.5}

 \label{ex:circ-clear} \label{ex:circ-clear-r}
Use the template {{\tt fun-for-circle}\/} to develop
{{\tt clear-a-circle}\/}. The function consumes a {{\tt circle}\/} structure
and clears the corresponding circle on the
canvas.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/circ-clear.html}{\rule{3pt}{5pt}}


{\bf Exercise 6.6.6}

 \label{ex:circ-draw-and-clear}
Define the function {{\tt draw-and-clear}\/}, which draws a {{\tt circle}\/}
structure, waits for a short time, and clears it. To implement a waiting
period, the teachpack {\tt\bf draw.ss} provides the function
{{\tt sleep-for-a-while}\/}. It consumes a number and puts the program to
sleep for that many seconds; its result is {{\tt true}\/}. For example,
{{\tt (sleep-for-a-while}\ {\tt 1)}\/} waits for one second.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/circ-draw-and-clear.html}{\rule{3pt}{5pt}}



Here is a function for moving circles across a canvas: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt move-circle}\ {\tt :}\ {\tt number}\ {\tt circle}\ {}{\tt ->}{}\ {\tt circle}\/}}\\
\hbox{;; to draw and clear a circle and then to move it by {{\tt delta}\/} pixels}
\\
{\tt (d}\={\tt efine}\  {\tt (move-circle}\  {\tt delta}\  {\tt a-circle)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (draw-and-clear}\  {\tt a-circle)}\  {\tt (translate-circle}\  {\tt a-circle}\  {\tt delta)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt a-circle\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The function {{\tt move-circle}\/} always produces the new {{\tt circle}\/}
structure so that we can move a circle across a canvas as follows:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (start}\  {\tt 200}\  {\tt 100)}\\
{\tt (d}\={\tt raw-a-circle}\  
\\
\>{\tt (m}\={\tt ove-circle}\  {\tt 10}
\\
\>\>{\tt (m}\={\tt ove-circle}\  {\tt 10}
\\
\>\>\>{\tt (m}\={\tt ove-circle}\  {\tt 10}
\\
\>\>\>\>{\tt (move-circle}\  {\tt 10}\  {\tt ...}\  {\tt a}\  {\tt circle}\  {\tt ...)))))}
\end{tabbing}\end{minipage}\end{center}
This expression moves the circle four times, by 10 pixels each, and also
shows this movement on the canvas. The last {{\tt draw-a-circle}\/} is
necessary because we wouldn't otherwise see the last move on the screen. 


\hrule
\begin{center}
{\framebox[\textwidth]{ \begin{picture}(300,220)(0,-200)  
\put(0,0){\line(1,0){300}} \multiput(0,-5)(25,0){12}{\line(0,1){10}} 
\put(-03,-18){\mbox{\relax}} 
\put(123,-18){\mbox{$5$}} 
\put(245,-18){\mbox{$10$}}  
\put(0,0){\line(0,-1){175}} \multiput(-5,0)(0,-25){7}{\line(1,0){10}} 
\put(-18,+03){\mbox{$0$}} 
\put(-18,-123){\mbox{$5$}}
\put(150,-50){\circle{40}} 
\put(150,-50){\circle*{3}}        
\put(137,-60){\mbox{\sl center}} 
\put(150,-50){\vector(-1,+1){14}} 
\put(139,-50){\mbox{$r$}} 
\put(050,-75){\line(1,00){75}} 
\put(125,-75){\line(0,-1){50}} 
\put(125,-125){\line(-1,0){75}} 
\put(050,-125){\line(0,+1){50}} 
\put(050,-75){\circle*{3}}
\put(+5,-76){\mbox{\sl upper left}} 
\put(75,-72){\mbox{\sl width}} 
\put(052,-100){\mbox{\sl height}} 
\put(153,-40){\circle*{3}}
\put(154,-45){\mbox{{\it A \/} (6,1.5)}} 
\put(200,-150){\circle*{3}}
\put(207,-145){\mbox{{\it B\/} (8,6)}} 
\put(100,-100){\circle*{3}}
\put(102,-95){\mbox{{\it C\/} (4,4)}} \end{picture}}}
\end{center}

\center{Figure: Circles, rectangles, and pixels} \label{fig:geo-in}
\hrule

{\bf Exercise 6.6.7}

 \label{ex:rect-descr}
Provide a structure definition and a data definition for representing
colored rectangles.  A {\sl rectangle} is characterized by four
pieces of information: its upper-left corner, its width, its height, and its
fill color. The first is a {{\tt posn}\/} structure, the second and third
quantities are plain numbers, and the last one is a color.

Develop the template {{\tt fun-for-rect}\/}, which outlines a function
that consumes {{\tt rectangle}\/}s and produces different classes of
data.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/rect-descr.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 6.6.8}

 \label{ex:rect-draw} \label{ex:rect-draw-r}
Use the template {{\tt fun-for-rect}\/} to develop
{{\tt draw-a-rectangle}\/}. The function consumes a {{\tt rectangle}\/}
structure and draws the corresponding rectangle on the screen. In contrast
to circles, the entire rectangle is painted in the matching color. Remember
to use {{\tt (start}\ {\tt num}\ {\tt num)}\/} to create the
canvas.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/rect-draw.html}{\rule{3pt}{5pt}}


{\bf Exercise 6.6.9}

 \label{ex:rect-in}
Use the template {{\tt fun-for-rect}\/} to develop {{\tt in-rectangle\char'077}\/}. The
function consumes a {{\tt rectangle}\/} structure and a {{\tt posn}\/} and
determines whether or not the pixel is inside the rectangle.  A pixel is within a
rectangle if its horizontal and vertical distances to the upper-left corner are
positive and smaller than the width and height of the rectangle.

Consider the rectangle in figure~\ref{fig:geo-in}. This rectangle's key
parameters are {{\tt (make-posn}\ {\tt 2}\ {\tt 3)}\/}, {{\tt 3}\/}, and {{\tt 2}\/}. The
pixel labeled {{\tt C}\/} is inside of the rectangle, {{\tt B}\/} is
outside.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/rect-in.html}{\rule{3pt}{5pt}} 


{\bf Exercise 6.6.10}

 \label{ex:rect-trans}
Use the template {{\tt fun-for-rect}\/} to develop
{{\tt translate-rectangle}\/}. The function consumes a {{\tt rectangle}\/}
structure and a number {{\tt delta}\/}. The result is a {{\tt rectangle}\/} whose
center is {{\tt delta}\/} pixels to the right of the
input.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/rect-trans.html}{\rule{3pt}{5pt}}


{\bf Exercise 6.6.11}

 \label{ex:rect-clear} \label{ex:rect-clear-r}
Use the template {{\tt fun-for-rect}\/} to develop
{{\tt clear-a-rectangle}\/}. The function consumes a {{\tt rectangle}\/}
structure and clears the corresponding rectangle on the
canvas.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/rect-clear.html}{\rule{3pt}{5pt}}


{\bf Exercise 6.6.12}

 \label{ex:rect-move} \label{ex:rect-move-r}
 Develop the function {{\tt move-rectangle}\/}. It consumes a number
 ({{\tt delta}\/}) and a {{\tt rectangle}\/} structure. The function draws the
 given rectangle, waits for one second, and clears the rectangle. If these
 actions produce {{\tt true}\/}, {{\tt move-rectangle}\/} translates the
 rectangle by {{\tt delta}\/} pixels and returns this new value as a
 result. 

 \noindent{\bf Hint:} \ If the actions fail to produce {{\tt true}\/}, they generate an
 error. Use the given rectangle for the answer of this
 {{\tt cond}\/}-line. Also see the definition of {{\tt move-circle}\/} and
 its uses.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/rect-move.html}{\rule{3pt}{5pt}} 


\subsection{Extended Exercise: Hangman} \label{sec:hangman}

\hrule
  \begin{rawhtml}  <table cellspacing=20 bgcolor=beige>  <tr> <td align=left>  <img src=../icons/hang0.gif></td>       <td align=center><img src=../icons/hang1.gif></td>       <td align=right> <img src=../icons/hang2.gif></td>  </tr>  </table>  \end{rawhtml}
\center{Figure: Three stages of the hangman picture}\label{fig:hangman}
\hrule

Hangman is a two-player, word-guessing game. One player thinks of a
 three-letter\footnote{In reality, we would want to play the game with  words of arbitrary length, but a game based on three-letter words is  easier to implement for now. We return to the problem in  exercise~\ref{ex:hangman-list}.} word and draws the noose of a gallows
 (see figure~\ref{fig:hangman}); the other player tries to guess the word,
 one letter at a time. For every wrong guess, the first player adds another
 part to the drawing (see figure~\ref{fig:hangman}): first the head, then
 the body, the arms, and the legs. If, however, a guess agrees with one or
 two letters in the chosen word, the first player reveals the position(s)
 where this letter occurs. The game is over when the second player guesses
 the complete word or when the first player has completed the stick figure.

Let's design a program that plays the role of the first player.  The
 program consists of two parts: one for drawing the figure, and another one
 for determining whether a guess occurs in the chosen word and where.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 6.7.1}

 \label{ex:draw-next} 
Develop the function {{\tt draw-next-part}\/}, which draws the pieces of a
hangman figure. The function consumes one of
the following seven symbols:
\begin{center}
    {{\tt \char'047}{\tt right-leg}\/} \hfill
    {{\tt \char'047}{\tt left-leg}\/} \hfill
    {{\tt \char'047}{\tt left-arm}\/} \hfill
    {{\tt \char'047}{\tt right-arm}\/} \hfill
    {{\tt \char'047}{\tt body}\/} \hfill
    {{\tt \char'047}{\tt head}\/} \hfill
    {{\tt \char'047}{\tt noose}\/} \hfill
\end{center}
It always returns {{\sf true}\/} and draws the matching part of the figure.
See figure~\ref{fig:hangman} for three snapshots of intermediate
stages.\footnote{Thanks to Mr.\ John Clements for the artistic version of {\it draw-next-part\/}.}

\noindent{\bf Hints:} \ Add {{\tt (start}\ {\tt 200}\ {\tt 200)}\/} to the top of the definition window;
it creates a new canvas. Start with the noose and develop one component at
a time. If a component of the stick figure requires more than one drawing
operation, combine the operations with {{\tt and}\/}, which evaluates both
expressions and ensure that they evaluate to {{\sf true}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/draw-next.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}



The second task of the first player is to determine whether a guess is
 among the letters of the chosen word and, if so, where it occurs. Our
 recipe requires that, before we design a function for this task, we need to
 analyze our data and provide data definitions. The key objects of the
 game are words and letters. A {\sl word\/} consists of three letters. A
 {\sl letter} is represented with the symbols {{\tt \char'047}{\tt a}\/} through
 {{\tt \char'047}{\tt z}\/}. Using just those letters, however, is not enough because the
 program also needs to maintain a word that records how much the second
 player has guessed. The solution is to add one extra ``letter'' to our
 alphabet that is distinct from the others; the hangman teachpack uses
 {{\tt \char'047}{\tt \char'137}\/} for this purpose.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 6.7.2}

 \label{ex:hangman-dd} 
Provide a structure definition and a data definition for representing
 three-letter words. ~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hangman-dd.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 6.7.3}

 \label{ex:hangman-simple} 
Develop the function {{\tt reveal}\/}. It consumes three arguments: 
\begin{enumerate}
\item the {\sl chosen} word, which is the word that we have to guess; 

\item the {\sl status} word, which shows how much of the word bas been
guessed so far; and

\item a letter, which is our current {\sl guess\/}. 
\end{enumerate}
The function produces a new status word, that is, a word that contains
ordinary letters and {{\tt \char'047}{\tt \char'137}\/}\,. The fields in the new status word are
determined by comparing the guess with each pair of letters from the status
word and the chosen word:
\begin{enumerate}
\item If the guess is equal to the letter in the chosen word, the guess is
 the corresponding letter in the new status word.

\item Otherwise, the new letter is the corresponding letter from the status
 word.
\end{enumerate}

Test the function with the following examples:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (reveal}\  {\tt (make-word}\  {\tt \char'047}{\tt t}\  {\tt \char'047}{\tt e}\  {\tt \char'047}{\tt a)}\  {\tt (make-word}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt e}\  {\tt \char'047}{\tt \char'137}{\tt )}\  {\tt \char'047}{\tt u)}\\
{\tt =}\  {\tt (make-word}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt e}\  {\tt \char'047}{\tt \char'137}{\tt )}
\\
\  \  {\tt (reveal}\  {\tt (make-word}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt l}\  {\tt \char'047}{\tt e)}\  {\tt (make-word}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt \char'137}\  \  {\tt \char'047}{\tt \char'137}{\tt )}\  {\tt \char'047}{\tt e)}
\\
{\tt =}\  {\tt (make-word}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt e)}
\\
\  \  {\tt (reveal}\  {\tt (make-word}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt l}\  {\tt \char'047}{\tt l)}\  {\tt (make-word}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}{\tt )}\  {\tt \char'047}{\tt l)}
\\
{\tt =}\  {\tt (make-word}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt l}\  {\tt \char'047}{\tt l)}
\end{tabbing}\end{minipage}\end{center}
The first one shows what happens when the {{\tt guess}\/} does not occur in
the word; the second one shows what happens when it does occur; and the
last one shows what happens when it occurs twice. 

\noindent{\bf Hints:} \ (1) Remember to develop auxiliary functions when a definition
becomes too large or too complex to manage.

(2) The function {{\tt reveal}\/} consumes two structures and one atomic
value (a letter). This suggests that we use the design recipe for compound
data (figure~\ref{fig:design3}). For the template, it is best to write down
the selector expressions in a two-column format, one column per
word.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hangman-simple.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


When the functions {{\tt draw-next-part}\/} and {{\tt reveal}\/} are properly
 tested, set teachpack to {\tt\bf hangman.ss} and play the game by evaluating
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (hangman}\  {\tt make-word}\  {\tt reveal}\  {\tt draw-next-part)}\end{tabbing}\end{minipage}\end{center}
The {{\tt hangman}\/} function chooses a three-letter word randomly and
displays a window with a pop-up menu for letters. Choose letters and, when
ready, click the {\tt Check} button to see whether the guess is correct.
Comment out the test cases for exercise~\ref{ex:draw-next} so that their
drawing effects don't interfere with those of {{\tt hangman}\/}.

\section{The Varieties of Data} \label{sec:mixed-data} 

The past section significantly expands our world of data.  We must now deal
with a universe that contains booleans, symbols, and structures of many
different kinds. Let's bring some order to this world. 

We start with a summary: 
\begin{description}
\item[numbers:]  representations of numeric information;
\item[booleans:]  truth and falsity;
\item[symbols:]  representations of symbolic information; and
\item[structures:] representations of compounds of information. 
\end{description}
Thus far, our functions always processed symbols, numbers, subclasses of
numbers, or classes of structures.  On occasion, however, a function must
process a class of data that includes both numbers and structures or
structures of several different kinds. We learn to design such functions in
this section. In addition, we learn how to protect functions from bad
uses. Here a bad use means that some user might accidentally apply a
function for drawing circles to a rectangle. Although we have agreed that
such users violate our data definitions, we should nevertheless know a
technique for protecting ourselves against them.

\subsection{Mixing and Distinguishing Data} 
\label{sec:mixed-data:pred} 

In the preceding section, we used {{\tt posn}\/} structures with exactly two
components to represent pixels. If many of the pixels are on the $x$-axis,
we can simplify the representation by using plain numbers for those pixels
and {{\tt posn}\/} structures for the remaining ones.

\hrule 
\begin{center}
\setlength{\unitlength}{.5pt}
{\framebox[3in]{ \begin{picture}(300,220)(0,-190)  
\put(0,0){\line(1,0){300}} \multiput(0,-5)(25,0){12}{\line(0,1){10}} 
\put(-03,-18){\mbox{\relax}} 
\put(123,-18){\mbox{$5$}} 
\put(245,-18){\mbox{$10$}}  
\put(0,0){\line(0,-1){175}} \multiput(-5,0)(0,-25){7}{\line(1,0){10}} 
\put(-18,+03){\mbox{$0$}} 
\put(-18,-123){\mbox{$5$}}
\put(25,0){\circle*{4}}    
\put(22,10){\mbox{\it C}} 
\put(50,0){\circle*{4}}    
\put(48,10){\mbox{\it D}} 
\put(75,0){\circle*{4}}    
\put(73,10){\mbox{\it E}} 
\put(150,-150){\circle*{4}}
\put(154,-145){\mbox{\it A}} 
\put(25,-50){\circle*{4}}  
\put(029,-045){\mbox{\it B}} \end{picture}}}
\end{center}

\center{Figure: A small collection of points} \label{fig:points}
\hrule

Figure~\ref{fig:points} contains a sample collection of such points. 
Three of the five points, namely {\it C\/}, {\it D\/}, and {\it E\/} are on
the $x$-axis. Only two points require two coordinates for an accurate
description: {\it A\/} and {\it B\/}. Our new idea for representing points
permits us to describe this class of points succinctly:
{{\tt (make-posn}\ {\tt 6}\ {\tt 6)}\/} for {{\tt A}\/}; {{\tt (make-posn}\ {\tt 1}\ {\tt 2)}\/} for
{{\tt B}\/}; and 1, 2, and 3 for {{\tt C}\/}, {{\tt D}\/}, and {{\tt E}\/},
respectively. 


If we now wish to define the function {{\tt distance-to-0}\/}, which consumes
such point representations and produces their distance to the origin, we
are confronted with a problem. The function may be applied to a number {\em or\/} a {{\tt posn}\/}. Depending on the class to which the input belongs,
{{\tt distance-to-0}\/} must employ a different method to calculate the
distance to the origin. Thus we need to use a {{\bf cond}-exp\-res\-sion} to
distinguish the two cases. Unfortunately, we don't have any operations to
formulate the appropriate conditions. 

To accommodate this kind of function, Scheme provides {\sc {predicates}},
which are operations that recognize a particular form of data. The
predicates for the classes of data we know are 
\begin{description}
\item[]{{{\tt number?}\/}}, which consumes an arbitrary value and produces {{\sf true}\/} if the value is a number and {{\sf false}\/} otherwise;
\item[]{{{\tt boolean?}\/}}, which consumes an arbitrary value and produces {{\sf true}\/} if the value is a Boolean value and {{\sf false}\/} otherwise;
\item[]{{{\tt symbol?}\/}}, which consumes an arbitrary value and produces {{\sf true}\/} if the value is a symbol and {{\sf false}\/} otherwise;
\item[]{{{\tt struct?}\/}}, which consumes an arbitrary value and produces {{\sf true}\/} if the value is a structure and {{\sf false}\/} otherwise.
\end{description}
For each structure definition, Scheme also introduces a separate predicate
so that we can distinguish among distinct classes of structures. Suppose
the {\tt Definitions} window contains the following structure
definitions:\footnote{The {{\tt posn}\/} structure is automatically provided in DrScheme's teaching languages and should never be defined.}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt posn}\  {\tt (x}\  {\tt y))}\\
{\tt (define-struct}\  {\tt star}\  {\tt (last}\  {\tt first}\  {\tt dob}\  {\tt ssn))}
\\
{\tt (define-struct}\  {\tt airplane}\  {\tt (kind}\  {\tt max-speed}\  {\tt max-load}\  {\tt price))}
\end{tabbing}\end{minipage}\end{center}
Then, Scheme also knows the following three predicates: 
\begin{description}
\item[]{{{\tt posn?}\/}}, which consumes an arbitrary value and produces {{\sf true}\/} if the value is a {{\tt posn}\/} structure and {{\sf false}\/} otherwise; 
\item[]{{{\tt star?}\/}}, which consumes an arbitrary value and produces {{\sf true}\/} if the value is a {{\tt star}\/} structure and {{\sf false}\/} otherwise;
\item[]{{{\tt airplane?}\/}}, which consumes an arbitrary value and produces {{\sf true}\/} if the value is a {{\tt airplane}\/} structure and {{\sf false}\/} otherwise.
\end{description}
Hence, a function can distinguish a structure from a number as well as a
{{\tt posn}\/} structure from an {{\tt airplane}\/} structure.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 7.1.1}

 \label{ex:predicates}
Evaluate the following expressions by hand: 
\begin{enumerate}
\item {{\tt (number?}\ {\tt (make-posn}\ {\tt 2}\ {\tt 3))}\/}
\item {{\tt (number?}\ {\tt (+}\ {\tt 12}\ {\tt 10))}\/}
\item {{\tt (posn?}\ {\tt 23)}\/}
\item {{\tt (posn?}\ {\tt (make-posn}\ {\tt 23}\ {\tt 3))}\/}
\item {{\tt (star?}\ {\tt (make-posn}\ {\tt 23}\ {\tt 3))}\/}
\end{enumerate}
Check the answers in DrScheme.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/predicates.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Now we can define {{\tt distance-to-0}\/}. Let's start with a data
definition: 

  \begin{quote} A {\sl {pixel-2}} is either        \begin{enumerate}         \item a {{\tt number}\/}, or         \item a {{\tt posn}\/} structure.         \end{enumerate} \end{quote}

\noindent Stating the contract, purpose, and header is straightforward: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt distance-to-0}\ {\tt :}\ {\tt pixel-2}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the distance of {{\tt a-pixel}\/} to the origin}
\\
{\tt (define}\  {\tt (distance-to-0}\  {\tt a-pixel)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

As mentioned before, the function must distinguish among its two kinds of
inputs, which can be accomplished with a {{\bf cond}-exp\-res\-sion}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (distance-to-0}\  {\tt a-pixel)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (number?}\  {\tt a-pixel)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (posn?}\  {\tt a-pixel)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The two conditions correspond to the two possible inputs of the new
{{\tt distance-to-0}\/} function.  If the first one holds, the input is a
pixel on the $x$-axis. Otherwise the pixel is a {{\tt posn}\/} structure. For
the second {{\tt cond}\/}-line, we also know that the input contains two
items: the {\it x\/} and {\it y\/} coordinates. To remind ourselves, we
annotate the template with two selector expressions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (distance-to-0}\  {\tt a-pixel)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (number?}\  {\tt a-pixel)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (posn?}\  {\tt a-pixel)}\  {\tt ...}\  {\tt (posn-x}\  {\tt a-pixel)}\  {\tt ...}\  {\tt (posn-y}\  {\tt a-pixel)}\  {\tt ...}\  {\tt \char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

Completing the function is easy. If the input is a number, it {\em is\/} the
distance to the origin. If it is a structure, we use the old formula for
determining the distance to the origin:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (distance-to-0}\  {\tt a-pixel)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (number?}\  {\tt a-pixel)}\  {\tt a-pixel\char'135}
\\
\>\>{\tt \char'133}{\tt (posn?}\  {\tt a-pixel)}\  {\tt (s}\={\tt qrt}
\\
\>\>\>{\tt (+}\  \={\tt (square}\  {\tt (posn-x}\  {\tt a-pixel))}
\\
\>\>\>\>{\tt (square}\  {\tt (posn-y}\  {\tt a-pixel))))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

Let us consider a second example. Suppose we are to write functions that
deal with geometric shapes. One function might have to compute the area
covered by a shape, another one the perimeter, and a third could draw the
shape.  For the sake of simplicity, let's assume that the class of shapes
only includes squares and circles and that their description includes their
location (a {{\tt posn}\/}) and their size (a
{{\tt number}\/}).\footnote{Thanks for Ms.\ Christa Brown for her persistent questioning of an early draft of this section.}


Information about both shapes must be represented with structures, because
both have several attributes. Here are the natural structure definitions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\it square\/}\  {\tt (nw}\  {\it length\/}{\tt ))}\\
{\tt (define-struct}\  {\tt circle}\  {\tt (center}\  {\tt radius))}
\end{tabbing}\end{minipage}\end{center}
and the matching data definition:

  \begin{quote} A {\sl {shape}} is either       \begin{enumerate}       \item a circle structure: \\
           \centerline{{{\tt (make-circle}\ {\tt p}\ {\tt s)}\/}}         where {{\tt p}\/} is a {{\tt posn}\/} and {{\tt s}\/} is a number; or       \item a square structure: \\
          \centerline{{{\tt (make-square}\ {\tt p}\ {\tt s)}\/}}          where {{\tt p}\/} is a {{\tt posn}\/} and {{\tt s}\/} is a number.       \end{enumerate} \end{quote}

\noindent Together, the two classes make up the class of shapes: 

The next step of our design recipe requires that we make up examples. Let's
start with input examples: 
\begin{enumerate}
\item {{\tt (make-square}\ {\tt (make-posn}\ {\tt 20}\ {\tt 20)}\ {\tt 3)}\/},
\item {{\tt (make-square}\ {\tt (make-posn}\ {\tt 2}\ {\tt 20)}\ {\tt 3)}\/}, and
\item {{\tt (make-circle}\ {\tt (make-posn}\ {\tt 10}\ {\tt 99)}\ {\tt 1)}\/}.
\end{enumerate}
To make up examples of input-output relationships, we need to know the
purpose of the function. So suppose we need the function {{\tt perimeter}\/},
which computes the {\sl perimeter} of a shape.  From geometry, we know that
the perimeter of a square is four times its side, the perimeter of a circle
is $\pi$ times the diameter, which is twice the radius.\footnote{The perimeter of a circle is also known as {\sl circumference}.} Thus, the perimeter
of the above three examples are: {{\tt 12}\/}, {{\tt 12}\/}, and (roughly)
{{\tt 6.28}\/}, respectively.

Following the design recipe and the precedent of {{\tt distance-to-0}\/}, we
start with the following skeleton of the function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt perimeter}\ {\tt :}\ {\tt shape}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the perimeter of {{\tt a-shape}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (perimeter}\  {\tt a-shape)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (square?}\  {\tt a-shape)}\  {\tt ...}\  {\tt \char'135}
\\
\>\>{\tt \char'133}{\tt (circle?}\  {\tt a-shape)}\  {\tt ...}\  {\tt \char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
because the function must first determine to which class {{\tt a-shape}\/}
belongs.

Furthermore, each possible input is a structure, so we can also add two
selector expressions to each {{\tt cond}\/}-clause:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt perimeter}\ {\tt :}\ {\tt shape}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the perimeter of {{\tt a-shape}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (perimeter}\  {\tt a-shape)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (square?}\  {\tt a-shape)}\  
\\
\>\>\>{\tt ...}\  {\tt (square-nw}\  {\tt a-shape)}\  {\tt ...}\  {\tt (square-length}\  {\tt a-shape)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}\={\tt (circle?}\  {\tt a-shape)}
\\
\>\>\>{\tt ...}\  {\tt (circle-center}\  {\tt a-shape)}\  {\tt ...}\  {\tt (circle-radius}\  {\tt a-shape)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The selector expressions remind us of the available data.

Now we are ready to finish the definition.  We fill the gaps in the two
answers by translating the mathematical formulae into Scheme notation:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (perimeter}\  {\tt a-shape)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (square?}\  {\tt a-shape)}\  {\tt (*}\  {\tt (square-length}\  {\tt a-shape)}\  {\tt 4)\char'135}
\\
\>\>{\tt \char'133}{\tt (circle?}\  {\tt a-shape)}\  {\tt (*}\  {\tt (*}\  {\tt 2}\  {\tt (circle-radius}\  {\tt a-shape))}\  {\tt pi)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Since the position of a shape does not affect its perimeter, the template's
selector expressions for {{\tt nw}\/} and {{\tt center}\/} disappear.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 7.1.2}

 \label{ex:circumference-test}
Test {{\tt perimeter}\/} with the examples. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/circumference-test.html}{\rule{3pt}{5pt}}


{\bf Exercise 7.1.3}

 \label{ex:shape-area}
Develop the function {{\tt area}\/}, which consume either a circle or a
square and computes the area. Is it possible to reuse the template for
{{\tt perimeter}\/} by changing the name to {{\tt area}\/}?
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/shape-area.html}{\rule{3pt}{5pt}}


\subsection{Designing Functions for Mixed Data} 
\label{sec:mixed-data:design} 

The function development in the preceding section suggests some amendments
to our design recipe. Specifically, the data analysis step, the template
construction step, and the definition of the function's body require
adjustments. 

\begin{description} 
\item[Data Analysis \& Design:] To determine whether a function consumes
several distinct classes of data, we must carefully inspect the problem
statement. In other words, the data analysis must take into account several
aspects now. First, we must determine how many distinct classes of
``problem objects'' exist and what their important attributes are. If there
are several different kinds of objects, we are mixing data. Second, we must
understand whether the various objects have several properties. If an
object has several attributes, we need a class of compound data for its
representation. The resulting data definition may have several clauses that
enumerate several possibilities, and indeed, we may develop a hierarchy of
data definitions.

The example of the preceding section deals with two distinct kinds of
shapes, each of which has several properties.  We captured this idea with
the following data definition: 
 
  \begin{quote} A {\sl {shape}} is either       \begin{enumerate}       \item a structure: \\
           \centerline{{{\tt (make-circle}\ {\tt p}\ {\tt s)}\/}}         where {{\tt p}\/} is a {{\tt posn}\/} and {{\tt s}\/} is a number; or       \item a structure: \\
          \centerline{{{\tt (make-square}\ {\tt p}\ {\tt s)}\/}}          where {{\tt p}\/} is a {{\tt posn}\/} and {{\tt s}\/} is a number.       \end{enumerate} \end{quote}

It specifies that every {{\tt shape}\/} belongs to one of two subclasses of
data.

For a data definition to make sense, it must be possible to formulate
conditions that distinguish the various subclasses in a definition. That
is, if {{\tt x}\/} stands for a piece of data in the defined class, we must
be able to use built-in and user-defined predicates to distinguish the
enumerated subclasses from each other. In our running example, the two
conditions would be {{\tt (square?}\ {\tt x)}\/} and {{\tt (circle?}\ {\tt x)}\/}.

\item[Template:] Recall that the template is a translation of the input
data definition into Scheme. Thus, imagine that we have a data definition
that enumerates several distinct possibilities. The first step is to write
down a {{\bf cond}-exp\-res\-sion} with as many clauses as there are
enumerated possibilities in the data definition.  The second step is to add
a condition to each line. Each condition should hold if the input belongs
to the corresponding subclass of data mentioned in the data definition.

Here is the template for our running example:

\begin{minipage}[t]{8in}\begin{tabbing}\  \  \=\hbox{;; {{\tt f}\ {\tt :}\ {\tt shape}\ {}{\tt ->}{}\ {\tt \char'077}{\tt \char'077}{\tt \char'077}\/}}\\
\>{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt a-shape)}
\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (square?}\  {\tt a-shape)}\  {\tt ...\char'135}
\\
\>\>\>{\tt \char'133}{\tt (circle?}\  {\tt a-shape)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}

The output specification and the purpose statement are missing to emphasize
that a template has no connection to the output or the purpose of a
function. 

Once we have formulated the template for the conditional, we refine the
template further, {{\tt cond}\/}-line by {{\tt cond}\/}-line. If the purpose
of a line is to process atomic information, we are done. If a line
processes compound data, we enrich the template with appropriate selector
expressions.

Let's illustrate the idea with our running example again:

\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (d}\={\tt efine}\  {\tt (f}\  {\tt a-shape)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (square?}\  {\tt a-shape)}\  
\\
\>\>\>{\tt ...}\  {\tt (square-nw}\  {\tt a-shape)}\  {\tt ...}\  {\tt (square-length}\  {\tt a-shape)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}\={\tt (circle?}\  {\tt a-shape)}
\\
\>\>\>{\tt ...}\  {\tt (circle-center}\  {\tt a-shape)}\  {\tt ...}\  {\tt (circle-radius}\  {\tt a-shape)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}

\item[Body:] Using the conditional template, we split the task into
simpler tasks. Specifically, we can focus on each {{\tt cond}\/}-line
separately, simply considering the question what is the output if we are
given this kind of input. All other cases are ignored as we work out one
particular clause. 

For example, if we wish to define a function that computes the perimeter of a
shape, we start from the template and fill in the gaps: 

\begin{minipage}[t]{8in}\begin{tabbing}\  \  \=\hbox{;; {{\tt perimeter}\ {\tt :}\ {\tt shape}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\>\hbox{;; to compute the perimeter of {{\tt a-shape}\/}}
\\
\>{\tt (d}\={\tt efine}\  {\tt (perimeter}\  {\tt a-shape)}
\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (square?}\  {\tt a-shape)}\  {\tt (*}\  {\tt (square-length}\  {\tt a-shape)}\  {\tt 4)\char'135}
\\
\>\>\>{\tt \char'133}{\tt (circle?}\  {\tt a-shape)}\  {\tt (*}\  {\tt (*}\  {\tt 2}\  {\tt (circle-radius}\  {\tt a-shape))}\  {\tt pi)\char'135}{\tt ))}
\end{tabbing}\end{minipage}

Figure~\ref{fig:design4-example} summarizes the development of our running
example. 
\end{description}
The remaining steps of the recipes in figures~\ref{fig:design1},
\ref{fig:design2}, and~\ref{fig:design3} should be followed on an as-is
basis. Figure~\ref{fig:design4} summarizes the design recipe, with all
steps included. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline Data Definition}:}\\
{\tt (define-struct}\  {\tt circle}\  {\tt (center}\  {\tt radius))}
\\
{\tt (define-struct}\  {\it square\/}\  {\tt (nw}\  {\it length\/}{\tt ))}
\\
\hbox{;; A {\sl {shape}} is either}
\\
\hbox{;; 1. a structure: {{\tt (make-circle}\ {\tt p}\ {\tt s)}\/}}
\\
\hbox{;; ~~ where {{\tt p}\/} is a {{\tt posn}\/}, {{\tt s}\/} a number;}
\\
\hbox{;; 2. a structure: {{\tt (make-square}\ {\tt p}\ {\tt s)}\/}}
\\
\hbox{;; ~~ where {{\tt p}\/} is a {{\tt posn}\/}, {{\tt s}\/} a number.}
\\
\hbox{;; {\underline Contract, Purpose, Header}: }
\\
\hbox{;; {{\tt perimeter}\ {\tt :}\ {\tt shape}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to compute the perimeter of {{\tt a-shape}\/}}
\\
\hbox{;; {\underline Examples}:}
\\
\hbox{;; {{\tt (perimeter}\ {\tt (make-square}\ {\tt ...}\ {\tt 3))}\/}}
\\
\hbox{;; = {{\tt 12}\/}}
\\
\hbox{;; {{\tt (perimeter}\ {\tt (make-circle}\ {\tt ...}\ {\tt 1))}\/}}
\\
\hbox{;; = {{\tt 6.28}\/}}
\\
\hbox{;; {\underline Template}:}
\\
\hbox{;; {{\tt (define}\ {\tt (f}\ {\tt a-shape)}\/}}
\\
\hbox{;; ~~{\ {\tt (cond}\/}}
\\
\hbox{;; \quad{\ {\tt \char'133}{\tt (square?}\ {\tt a-shape)}\/}}
\\
\hbox{;; \quad{\ {\tt ...}\ {\tt (square-nw}\ {\tt a-shape)}\ {\tt ...}\ {\tt (square-length}\ {\tt a-shape)}\ {\tt ...\char'135}\/}}
\\
\hbox{;; \quad{\ {\tt \char'133}{\tt (circle?}\ {\tt a-shape)}\/}}
\\
\hbox{;; \quad{\ {\tt ...}\ {\tt (circle-center}\ {\tt a-shape)}\ {\tt ...}\ {\tt (circle-radius}\ {\tt a-shape)}\ {\tt ...\char'135}{\tt ))}\/}}
\\
\hbox{;; {\underline Definition}: }
\\
{\tt (d}\={\tt efine}\  {\tt (perimeter}\  {\tt a-shape)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (circle?}\  {\tt a-shape)}
\\
\>\>\>{\tt (*}\  {\tt (*}\  {\tt 2}\  {\tt (circle-radius}\  {\tt a-shape))}\  {\tt pi)\char'135}
\\
\>\>{\tt \char'133}\={\tt (square?}\  {\tt a-shape)}
\\
\>\>\>{\tt (*}\  {\tt (square-length}\  {\tt a-shape)}\  {\tt 4)\char'135}{\tt ))}
\\
\hbox{;; {\underline Tests}: (same as examples)}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The design recipe for mixed data: a complete example}
\label{fig:design4-example}
\hrule

%% BEGIN INLINED FILE: idesign4.dl.tex
%% BEGIN INLINED FILE: design1.dl.tex






%% BEGIN INLINED FILE: design2.dl.tex




%% BEGIN INLINED FILE: design3.dl.tex





%% BEGIN INLINED FILE: design4.dl.tex





\hrule
\begin{center} \begin{tabular}{|l|l|l|}\hline Phase            &  Goal 		& Activity \\
 \hline\hline \begin{minipage}[t]{.60in} Data\\
~~Analysis\\
~~\& Design \end{minipage}  & \begin{minipage}[t]{1.6in}\raggedright to formulate a group of data definitions \rule{0pt}{6pt}\end{minipage} & \begin{minipage}[t]{2.2in}\raggedright {determine how many distinct classes of ``objects'' make up the collection of inputs $\bullet$\  enumerate the alternatives in a data definition $\bullet$\  formulate a data definition for each alternative, if it is a from of compound data}\rule{0pt}{6pt}\end{minipage}   \\
 \hline \begin{minipage}[t]{.60in} Contract\\
 Header~\&\\
 Purpose\\
 \end{minipage} 	&    \begin{minipage}[t]{1.6in}\raggedright to name the program,\\
  	 to specify its inputs \& \\
~~outputs,\\
 	 to describe its purpose,\\
 	 to formulate a program \\
~~header  \rule{0pt}{6pt}\end{minipage}  & \begin{minipage}[t]{2.2in}\raggedright {name the function, its inputs, its output, and specify its purpose: \\
   ~;; {\it name\/} : {\it in1\/} {\it in2\/} \ldots $\rightarrow$ {\it out\/} \\
   ~;; {\bf Purpose}: to compute \ldots\ {\it x1\/} \ldots {\it x2\/} \\
   ~({\bf define} ({\it name x1 x2 \ldots\/}) \ldots) }\rule{0pt}{6pt}\end{minipage} \\
 \hline Examples 	& \begin{minipage}[t]{1.6in}\raggedright to characterize the input-\\
 output relationship via examples \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {create examples of the input-output relationship $\bullet$\  make sure there is at least one example per alternative}\rule{0pt}{6pt}\end{minipage}   \\
 \hline Template        & \begin{minipage}[t]{1.6in}\raggedright to formulate an outline for a group of programs \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {introduce a {\bf cond}-expression with one clause per alternative $\bullet$\  formulate a condition for each case, using built-in and predefined predicates}\rule{0pt}{6pt}\end{minipage}   \\
 \hline Body	 	& \begin{minipage}[t]{1.6in}\raggedright to define the program \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {develop a Scheme expression (an answer) for each {\bf cond}-line, assuming that the condition holds}\rule{0pt}{6pt}\end{minipage}   \\
 \hline Test		& \begin{minipage}[t]{1.6in}\raggedright to expose mistakes\\
~~(``typos'' and logic) \rule{0pt}{6pt}\end{minipage} & \begin{minipage}[t]{2.2in}\raggedright {apply the function to the inputs of the examples  $\bullet$\  check that the outputs are as predicted above}\rule{0pt}{6pt}\end{minipage}   \\
 \hline \end{tabular} \end{center}
\center{Figure: The revised design recipe for mixed data} \label{fig:design4}
\centerline{(Refines the recipes in figures~\ref{fig:design1} (pg.~\pageref{fig:design1}) and~\ref{fig:design3} (pg.~\pageref{fig:design3}))}
\hrule

Even a cursory comparative reading of the design recipes in
sections~\ref{sec:design1}, ~\ref{sec:design2}, ~\ref{sec:design3}, and the
current one suggests that the data analysis and the template design steps
are becoming more and more important. If we do not understand what kind of
data a function consumes, we cannot design it and organize it properly. If,
however, we do understand the structure of the data definition and organize
our template properly, it is easy to modify or to extend a function. For
example, if we add new information to the representation of a
{{\tt circle}\/}, then only those {{\tt cond}\/}-clauses related to circles
may require changes. Similarly, if we add a new kind of shape to our data
definition, say, rectangles, we must add new {{\tt cond}\/}-clauses to our
functions.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 7.2.1}

 \label{ex:animals}
Develop structure and data definitions for a collection of zoo animals. The
collection includes 
\begin{description}
\item[spiders,] 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; 

\item[elephants,] whose only attributes are the space they need in case of
transport; 

\item[monkeys,] whose attributes are intelligence and space needed for
transportation. 
\end{description}
Then develop a template for functions that consume zoo animals. 

Develop the function {{\tt fits\char'077}\/}. The function consumes a zoo animal and
the volume of a cage. It determines whether the cage is large enough for
the animal.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/animals.html}{\rule{3pt}{5pt}}


{\bf Exercise 7.2.2}

 \label{ex:vehicles} \label{ex:vehicles-r}
The administrators of metropolitan transportation agencies manage fleets of
vehicles.  Develop structure and data definitions for a collection of such
vehicles. The collection should at least include buses, limos, cars,
and subways. Add at least two attributes per class of
vehicle. 

Then develop a template for functions that consume vehicles. 
~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vehicles.html}{\rule{3pt}{5pt}}


\subsection{Composing Functions, Revisited} \label{sec:compounding1} 

As we analyze a problem statement, we might wish develop the data
representation in stages. This is especially true when the problem
statement mentions several different kinds of objects. It is easier 
to understand several smaller data definitions than one larger one. 

Let's return to our shape problem again. Instead of the class of shapes in
a single data definition, we could start with two data definitions, one for
each basic shape:

  \begin{quote} A {\sl {circle}} is a structure: \\
           \centerline{{{\tt (make-circle}\ {\tt p}\ {\tt s)}\/}}          where {{\tt p}\/} is a {{\tt posn}\/} and {{\tt s}\/} is a number. \end{quote}

  \begin{quote} A {\sl {square}} is a structure: \\
          \centerline{{{\tt (make-square}\ {\tt p}\ {\tt s)}\/}}       where {{\tt p}\/} is a {{\tt posn}\/} and {{\tt s}\/} is a number. \end{quote}

\noindent Both assume the structure definitions of
section~\ref{sec:mixed-data:pred}.

Once we have developed and understood the basic data definitions, possibly
by playing with examples and by writing simple functions, we can introduce
data definitions that combine them. For example, we can introduce a data
definition for a class of shapes that refers to the two above: 

  \begin{quote} A {\sl {shape}} is either       \begin{enumerate}         \item a {{\tt circle}\/}, or          \item a {{\it square\/}\/}.       \end{enumerate} \end{quote}

Now suppose we need to develop a function that consumes {{\tt shape}\/}s.
For the first step, we form a {{\bf cond}-exp\-res\-sion} with conditions for
each part of the data definition:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt f}\ {\tt :}\ {\tt shape}\ {}{\tt ->}{}\ {\tt \char'077}{\tt \char'077}{\tt \char'077}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt a-shape)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (circle?}\  {\tt a-shape)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (square?}\  {\tt a-shape)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Given our guideline concerning the composition of functions (see
section~\ref{sec:basics-compose}) and given that the data definition refers
to two other data definitions, the natural second step is to pass the
argument to auxiliary functions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt f}\ {\tt :}\ {\tt shape}\ {}{\tt ->}{}\ {\tt \char'077}{\tt \char'077}{\tt \char'077}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt a-shape)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (circle?}\  {\tt a-shape)}\  {\tt (f-for-circle}\  {\tt a-shape)\char'135}
\\
\>\>{\tt \char'133}{\tt (square?}\  {\tt a-shape)}\  {\tt (f-for-square}\  {\tt a-shape)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
This, in turn, requires that we develop the two auxiliary functions,
{{\tt f-for-circle}\/} and {{\tt f-for-square}\/}, including their
templates. 


\hrule
\begin{center}
\begin{minipage}[b]{2.35in}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline Data Definition}:}\\
{\tt (define-struct}\  {\tt circle}\  {\tt (center}\  {\tt radius))}
\\
{\tt (define-struct}\  {\it square\/}\  {\tt (nw}\  {\it length\/}{\tt ))}
\\
\hbox{;; A {\sl {shape}} is either}
\\
\hbox{;; 1. a structure: {{\tt (make-circle}\ {\tt p}\ {\tt s)}\/}}
\\
\hbox{;; ~~ where {{\tt p}\/} is a {{\tt posn}\/}, {{\tt s}\/} a number;}
\\
\hbox{;; 2. a structure: {{\tt (make-square}\ {\tt p}\ {\tt s)}\/}}
\\
\hbox{;; ~~ where {{\tt p}\/} is a {{\tt posn}\/}, {{\tt s}\/} a number.}
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\end{tabbing}\end{minipage}
\end{minipage}~~~~~\rule{.1pt}{2.15in}~~~~~\begin{minipage}[b]{2.35in}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline Data Definitions}:}\\
{\tt (define-struct}\  {\tt circle}\  {\tt (center}\  {\tt radius))}
\\
\hbox{;; A {\sl {circle}} is a structure:}
\\
\hbox{;; \qquad {{\tt (make-circle}\ {\tt p}\ {\tt s)}\/}}
\\
\hbox{;; ~~ where {{\tt p}\/} is a {{\tt posn}\/}, {{\tt s}\/} a number;}
\\
\relax
\\
{\tt (define-struct}\  {\it square\/}\  {\tt (nw}\  {\it length\/}{\tt ))}
\\
\hbox{;; A {\sl {square}} is a structure:}
\\
\hbox{;; \qquad {{\tt (make-square}\ {\tt p}\ {\tt s)}\/}}
\\
\hbox{;; ~~ where {{\tt p}\/} is a {{\tt posn}\/}, {{\tt s}\/} a number.}
\\
\relax
\\
\hbox{;; A {\sl {shape}} is either}
\\
\hbox{;; 1. a circle, or}
\\
\hbox{;; 2. a structure. }
\end{tabbing}\end{minipage}
\end{minipage}

\smallskip \hrulefill \bigskip

\begin{minipage}[b]{2.35in}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline Final Definition}: }\\
\hbox{;; {{\tt perimeter}\ {\tt :}\ {\tt shape}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to compute the perimeter of {{\tt a-shape}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (perimeter}\  {\tt a-shape)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (circle?}\  {\tt a-shape)}
\\
\>\>\>{\tt (*}\  {\tt (*}\  {\tt 2}\  {\tt (circle-radius}\  {\tt a-shape))}\  {\tt pi)\char'135}
\\
\>\>{\tt \char'133}\={\tt (square?}\  {\tt a-shape)}
\\
\>\>\>{\tt (*}\  {\tt (square-length}\  {\tt a-shape)}\  {\tt 4)\char'135}{\tt ))}
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\end{tabbing}\end{minipage}
\end{minipage}~~~~~\rule{.1pt}{3.05in}~~~~~\begin{minipage}[b]{2.35in}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline Final Definitions}: }\\
\hbox{;; {{\tt perimeter}\ {\tt :}\ {\tt shape}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to compute the perimeter of {{\tt a-shape}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (perimeter}\  {\tt a-shape)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (circle?}\  {\tt a-shape)}
\\
\>\>\>{\tt (perimeter-circle}\  {\tt a-shape)\char'135}
\\
\>\>{\tt \char'133}\={\tt (square?}\  {\tt a-shape)}
\\
\>\>\>{\tt (perimeter-square}\  {\tt a-shape)\char'135}{\tt ))}
\\
\relax
\\
\hbox{;; {{\tt perimeter-circle}\ {\tt :}\ {\tt circle}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to compute the perimeter of {{\tt a-circle}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (perimeter-circle}\  {\tt a-circle)}
\\
\>{\tt (*}\  {\tt (*}\  {\tt 2}\  {\tt (circle-length}\  {\tt a-circle))}\  {\tt pi))}
\\
\relax
\\
\hbox{;; {{\tt perimeter-square}\ {\tt :}\ {\tt square}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to compute the perimeter of {{\tt a-square}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (perimeter-square}\  {\tt a-square)}
\\
\>{\tt (*}\  {\tt (square-length}\  {\tt a-square)}\  {\tt 4))}
\end{tabbing}\end{minipage}
\end{minipage}
\end{center}

\center{Figure: Two ways to define {\it perimeter\/}} \label{fig:compare-template}
\hrule

If we follow this suggestion, we arrive at a collection of three functions,
one per data definition. The essential points of the program development
are summarized in the right column of figure~\ref{fig:compare-template}.
For a comparison, the left column contains the corresponding pieces of the
original program development. In each case, we have as many functions as
there are data definitions. Furthermore, the references between the
functions in the right column directly match to the references among the
corresponding data definitions. While this symmetry between data
definitions and functions may seem trivial now, it becomes more and more
important as we study more complex ways of defining data. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 7.3.1}

 \label{ex:compare-template}
Modify the two versions of {{\tt perimeter}\/} so that they also process
rectangles.  For our purposes, the description of a rectangle includes its
upper-left corner, its width, and its height.
~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/compare-template.html}{\rule{3pt}{5pt}}


\subsection{Extended Exercise: Moving Shapes} \label{sec:move-shape}

In section~\ref{sec:move-circle}, we developed functions for drawing,
translating, and clearing circles and rectangles. As we have just seen, we
should think of the two classes of data as subclasses of a class of general
shapes so that we can just draw, translate, and clear shapes. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 7.4.1}

 \label{ex:moving-fig-dat} 
Provide a data definition for a general class of {\sl shape}s. The class
should at least subsume the classes of colored circles and rectangles from
section~\ref{sec:move-circle}. 

Develop the template {{\tt fun-for-shape}\/}, which outlines functions that
consume {{\tt shape}\/}s.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-fig-dat.html}{\rule{3pt}{5pt}}


{\bf Exercise 7.4.2}

 \label{ex:moving-fig-dr} 
Use the template {{\tt fun-for-shape}\/} to develop {{\tt draw-shape}\/}.
The function consumes a {{\tt shape}\/} and draws it on the
canvas.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-fig-dr.html}{\rule{3pt}{5pt}}


{\bf Exercise 7.4.3}

 \label{ex:moving-fig-tr} 
Use the template  {{\tt fun-for-shape}\/} to develop
{{\tt translate-shape}\/}.  The function consumes a {{\tt shape}\/} and a number
{{\tt delta}\/}, and produces a shape whose key position is moved by
{{\tt delta}\/} pixels in the $x$-direction.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-fig-tr.html}{\rule{3pt}{5pt}}


{\bf Exercise 7.4.4}

 \label{ex:moving-fig-cl} 
Use the template  {{\tt fun-for-shape}\/} to develop {{\tt clear-shape}\/}.
The function consumes a {{\tt shape}\/}, erases it from the canvas, and returns
{{\sf true}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-fig-cl.html}{\rule{3pt}{5pt}}


{\bf Exercise 7.4.5}

 \label{ex:shape-move}
Develop {{\tt move-shape}\/}, which moves a shape across the canvas. The function
consumes a number ({{\tt delta}\/}) and a {{\tt shape}\/} and moves it by
{{\tt delta}\/} pixels on a canvas. The function should wait for one second
between clearing and drawing the new rectangle.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/shape-move.html}{\rule{3pt}{5pt}}


\subsection{Input Errors} \label{sec:input-errors} 

Recall our first function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt area-of-disk}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the area of a disk with radius {{\tt r}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (area-of-disk}\  {\tt r)}\  
\\
\>{\tt (*}\  {\tt 3.14}\  {\tt (*}\  {\tt r}\  {\tt r)))}
\end{tabbing}\end{minipage}\end{center}
Clearly, our friends may wish to use this function, especially for some of
their geometry homework. Unfortunately, when our friends use this function,
they may accidentally apply it to a symbol rather than a number. When that
happens, the function stops with a whimsical and uninformative error
message:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt (area-of-disk}\  {\tt \char'047}{\tt my-disk)}\\
{\tt *:}\  {\tt expects}\  {\tt type}\  {\tt <number>}\  {\tt as}\  {\tt 1st}\  {\tt argument\char'054}\  {\tt given:}\  {\tt \char'047}{\tt my-disk}\hbox{; ... }
\end{tabbing}\end{minipage}\end{center}
Using predicates, we can do better. 

To prevent this kind of accident, we should define {checked} versions of
our functions, when we wish to hand them to our friends.  In general, a
{\sc {checked function}} inputs an arbitrary Scheme value: a number, a
boolean, a symbol, or a structure. For all those values that are in the
class of values for which the original function is defined, the checked
version applies the latter; for all others, it signals an
error. Concretely, {{\tt checked-area-of-disk}\/} consumes an arbitrary
Scheme value, uses {{\tt area-of-disk}\/} to compute the area of the a disk
if the input is a number, and stops with an error message otherwise.

Based on the enumeration of Scheme's values, the template for a checked
function is as follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt f}\ {\tt :}\ {\tt Scheme-value}\ {}{\tt ->}{}\ {\tt \char'077}{\tt \char'077}{\tt \char'077}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt v)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (number?}\  {\tt v)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (boolean?}\  {\tt v)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol?}\  {\tt v)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (struct?}\  {\tt v)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Each line corresponds to one possible class of input. If we need to
distinguish among the structures, we expand the last line appropriately. 

The first clause is the only one where we can use {{\tt area-of-disk}\/}.
For the others, however, we must signal an error. In Scheme we use the
operation {{\tt error}\/} to do so. It consumes a symbol and a string, which
is a sequence of keyboard characters enclosed in quotation marks
({{\tt ``...''}\/}). Here is an example:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (error}\  {\tt \char'047}{\tt checked-area-of-disk}\  {\tt ``number}\  {\tt expected'')}\end{tabbing}\end{minipage}\end{center}

Hence, the full definition of {{\tt checked-area-of-disk}\/} is:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (checked-area-of-disk}\  {\tt v)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (number?}\  {\tt v)}\  {\tt (area-of-disk}\  {\tt v)\char'135}
\\
\>\>{\tt \char'133}{\tt (boolean?}\  {\tt v)}\  {\tt (error}\  {\tt \char'047}{\tt checked-area-of-disk}\  {\tt ``number}\  {\tt expected'')\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol?}\  {\tt v)}\  {\tt (error}\  {\tt \char'047}{\tt checked-area-of-disk}\  {\tt ``number}\  {\tt expected'')\char'135}
\\
\>\>{\tt \char'133}{\tt (struct?}\  {\tt v)}\  {\tt (error}\  {\tt \char'047}{\tt checked-area-of-disk}\  {\tt ``number}\  {\tt expected'')\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Using {{\tt else}\/} we can greatly simplify the function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt checked-area-of-disk}\ {\tt :}\ {\tt Scheme-value}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to compute the area of a disk with radius {{\tt v}\/}, }
\\
\hbox{;; if {{\tt v}\/} is a number}
\\
{\tt (d}\={\tt efine}\  {\tt (checked-area-of-disk}\  {\tt v)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (number?}\  {\tt v)}\  {\tt (area-of-disk}\  {\tt v)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt checked-area-of-disk}\  {\tt ``number}\  {\tt expected'')\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Of course, such a simplification may not always be possible and may require
a re-ordering of the {{\tt cond}\/}-clauses first. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 7.5.1}

 \label{ex:checked2}
A checked version of {{\tt area-of-disk}\/} can also enforce that the
arguments to the function are positive numbers, not just arbitrary numbers. 
Modify {{\tt checked-area-of-disk}\/} in this way.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/checked2.html}{\rule{3pt}{5pt}}


{\bf Exercise 7.5.2}

 \label{ex:checked1}
Develop checked versions of the functions 
{{\tt profit}\/} (figure~\ref{fig:profit}), 
{{\tt is-between-5-6\char'077}\/} (section~\ref{sec:cond:prog}), 
{{\tt reply}\/} (section~\ref{sec:sym}), 
{{\tt distance-to-0}\/} (section~\ref{sec:structures}), 
and 
{{\tt perimeter}\/} (in the left column of
figure~\ref{fig:compare-template}).~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/checked1.html}{\rule{3pt}{5pt}}


{\bf Exercise 7.5.3}

 \label{ex:checked3}
Take a look at these structure and data definitions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt vec}\  {\tt (x}\  {\tt y))}\end{tabbing}\end{minipage}\end{center}

\begin{quote} A {\sl {speed vector}} ({\em vec\/}) is a structure: \\
       \centerline{ {{\tt (make-vec}\ {\tt x}\ {\tt y)}\/} }     where both {{\tt x}\/} and {{\tt y}\/} are positive numbers. \end{quote}

Develop the function {{\tt checked-make-vec}\/}, which should be understood
as a checked version of the primitive operation {{\tt make-vec}\/}. It 
ensures that the arguments to {{\tt make-vec}\/} are positive numbers, and
not just arbitrary numbers. In other words, {{\tt checked-make-vec}\/}
enforces our informal data definition.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/checked3.html}{\rule{3pt}{5pt}}


%% BEGIN INLINED FILE: curriculumAa.dl.tex

 \part{Intermezzo 1: Syntax and Semantics}  \section{Intermezzo 1: Syntax and Semantics}   \label{sec:synsem} 

\htmladdimg{../icons/teacher.gif}~{\em This section is a rigorous presentation of Scheme's grammar and evaluation rules. The goal is to show that there are two aspects to studying computer languages: syntax and semantics. Covering the section is not necessary but it does provide a first glimpse at what computer scientists study.}

Thus far we have approached Scheme as if it were a spoken language.  Like
toddlers, we learned the vocabulary of the language, we acquired an
intuitive understanding of its meaning, and we figured out some basic rules
of how to compose and not to compose sentences. Truly effective
communication, however, in any language---be it natural like English or
artificial like Scheme---eventually requires a formal study of its
vocabulary, its grammar, and the meaning of sentences.

A programming language is in many ways like a spoken language. It has a
vocabulary and a grammar. The vocabulary is the collection of those ``basic
words'' from which we can compose ``sentences'' in our language. A sentence
in a programming language is an expression or a function; the language's
grammar dictates how to form complete sentences from words. Programmers use
the terminology {\sc {syntax}} to refer to the vocabularies and grammars
of programming languages.

Not all grammatical sentences are meaningful---neither in English nor in a
programming language. For example, the English sentence ``the cat is
round'' is a meaningful sentence, but ``the brick is a car'' makes no
sense, even though it is completely grammatical. To determine whether or
not a sentence is meaningful, we must study the {\sc {meaning}}, or
{\sc {semantics}}, of words and sentences. For spoken languages, we
typically explain the meaning of words with sentences that use simpler
words; in the case of a foreign language, we sometimes explain a word with
simple sentences in the foreign language or we translate words to a known
language. For programming languages, there are also several ways to explain
the meaning of individual sentences.  In this book, we discuss the meaning
of Scheme programs through an extension of the familiar laws of arithmetic
and algebra. After all, computation starts with this form of simple
mathematics, and we should understand the connection between this
mathematics and computing. 

The first three sections present the vocabulary, grammar, and meaning of a
small, but powerful subset of Scheme. The fourth one resumes our discussion
of run-time errors in Scheme, based on our new understanding of its
meaning. The remaining three sections revisit {{\tt and}\/} and {{\tt or}\/}
expressions, variable definitions, and structures.


 \subsection{The Scheme Vocabulary} \label{sec:synsem:vocab} 

Scheme's basic vocabulary consists of five different categories of
words. The five lines in Figure~\ref{fig:lex-beginner} show how computer
scientists discuss the vocabulary of a language.\footnote{We use different fonts to distinguish the words of different categories. Constants and primitive operations are type set in {\sf sans serif}, variables in {\it italics\/}, and keywords in {\bf boldface}.}  All lines employ the same
notation. They enumerate some simple examples separated by a bar
(``$\,\,\vert\,\,$''). Dots indicate that there are more things of the same kind in
some category. 

\hrule
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{var}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$
  {{\tt x}\/} $\,\,\vert\,\,$  {{\tt area-of-disk}\/} $\,\,\vert\,\,$ {{\tt perimeter}\/} $\,\,\vert\,\,$\ldots\\
\mbox{{\tt <{con}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$
  {{\sf true}\/} $\,\,\vert\,\,$ {{\sf false}\/} \\
  && $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$
  {{\tt \char'047}{\tt a}\/} $\,\,\vert\,\,$ {{\tt \char'047}{\tt doll}\/} $\,\,\vert\,\,$ {{\tt \char'047}{\tt sum}\/} $\,\,\vert\,\,$ \ldots \\
  && $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$
  {{\tt 1}\/} $\,\,\vert\,\,$ {{\tt -1}\/} $\,\,\vert\,\,$ {{\tt 3/5}\/} $\,\,\vert\,\,$ 
  {{\tt 1.22}\/} $\,\,\vert\,\,$ \ldots \\
\mbox{{\tt <{prm}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$
  {{\tt +}\/} $\,\,\vert\,\,$ {{\tt -}\/} $\,\,\vert\,\,$ \ldots
\end{tabular}
\end{center}
\center{Figure: {\tt Beginning Student Scheme}: The vocabulary (core)} \label{fig:lex-beginner}
\hrule 

The first category is that of variables, which are the names of functions
and values. The second introduces constants: boolean, symbolic, and numeric
constants. As indicated before, Scheme has a powerful number system, which
is best introduced gradually by examples. The final category is that of
primitive operations, which are those basic functions that Scheme provides
from the very beginning. While it is possible to specify this collection in
its entirety, it is best introduced in pieces, as the need arises.

For the classification of Scheme sentences, we also need three
{{\tt keywords}\/}: {{\tt define}\/}, {{\tt cond}\/}, and {{\tt else}\/}.
These keywords have no meaning. Their role resembles that of punctuation
marks, especially that of commas and semicolons, in English writing; they
help programmers distinguish one sentence from another. No keyword may be
used as a variable.


 \subsection{The Scheme Grammar} \label{sec:synsem:grammar}

In contrast to many other programming languages, Scheme has a simple
grammar. It is shown in its entirety in
figure~\ref{fig:syn-beginner}.\footnote{This grammar describes only that portion of Scheme we have used so far (minus variable and structure definitions), which still covers a large subset of the full language. Scheme is a bit larger, and we will get to know more of it in the remaining parts of the book.} The grammar defines two categories of
sentences: Scheme definitions, \mbox{{\tt <{def}>}}, and expressions, \mbox{{\tt <{exp}>}}. While
the grammar does not dictate the use of white space between the items of
sentences, we follow the convention to put at least one blank space behind
each item unless an item is followed by a right parenthesis ``)''. Scheme is
flexible concerning blank space, and we can replace a single blank space by
many spaces, line breaks, and page breaks.\footnote{For the physical layout of programs, it is best to follow the examples in this text. DrScheme combines proper indentation with the {Return} or {Enter} key; a programmer can also use the {\tt Scheme|Indent} and {\tt Scheme|Indent All} menu items to indent parts or all of the {\tt Definitions} window.}

\hrule
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{def}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ ({\bf define} (\mbox{{\tt <{var}>}} \mbox{{\tt <{var}>}} \ldots \mbox{{\tt <{var}>}}) \mbox{{\tt <{exp}>}}) \\
\mbox{{\tt <{exp}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ \mbox{{\tt <{var}>}} \\
    && $\,\,\vert\,\,$ \mbox{{\tt <{con}>}} \\
    && $\,\,\vert\,\,$ (\mbox{{\tt <{prm}>}} \mbox{{\tt <{exp}>}} \ldots \mbox{{\tt <{exp}>}})\\
    && $\,\,\vert\,\,$ (\mbox{{\tt <{var}>}} \mbox{{\tt <{exp}>}} \ldots \mbox{{\tt <{exp}>}}) \\
 && $\,\,\vert\,\,$ ({\bf cond} (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}) \ldots (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}))\\
 && $\,\,\vert\,\,$ ({\bf cond} (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}) \ldots ({{\tt else}\/} \mbox{{\tt <{exp}>}}))
\end{tabular}
\end{center}
\center{Figure: {\tt Beginning Student Scheme}: The core grammar} \label{fig:syn-beginner}
\hrule

The two grammar definitions describe how to form atomic sentences and
compound sentences, which are sentences built from other sentences. For
example, a function definition is formed by using ``('', followed by the
keyword {{\tt define}\/}, followed by another ``('', followed by a non-empty
sequence of variables, followed by ``)'', followed by an expression, and
closed by a right parenthesis ``)'' that matches the very first one.  The
keyword {{\tt define}\/} distinguishes definitions from expressions.

The category of expressions consists of six alternatives: variables,
constants, primitive applications, (function) applications, and two
varieties of {{\tt cond}\/}itionals.  The last four are again composed of
other expressions.  The keyword {{\tt cond}\/} distinguishes conditional
expressions from primitive and function applications.

\htmladdimg{../icons/teacher.gif}~{\em Yes, grammars are similar to data definitions. We could use the language of grammars to define data. We don't, because that may confuse students.}

Here are three examples of expressions: {{\tt \char'047}{\tt all}\/}, {{\tt x}\/}, and
{{\tt (x}\ {\tt x)}\/}. The first one belongs to the class of symbols and is
therefore an expression. The second is a variable, and every variable is an
expression. Finally, the third is a function application, because {{\tt x}\/}
is a variable.

In contrast, the following parenthesized sentences are not expressions:
{{\tt (f}\ {\tt define)}\/}, {{\tt (cond}\ {\tt x)}\/}, and {{\tt ()}\/}.  The first one
partially matches the shape of a function application but it uses
{{\tt define}\/} as if it were a variable. The second one fails to be a
correct {{\bf cond}-exp\-res\-sion} because it contains a variable as the
second item and not a pair of expressions surrounded by parentheses. The
last one is just a pair of parentheses, but the grammar requires that all 
left parenthesis are followed by something other than a right parenthesis. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 8.2.1}

 \label{ex:legal-ex1}
Why are the sentences
\\
 \begin{tabular}{lll}
1. {{\tt x}\/} & 
2. {{\tt (=}\ {\tt y}\ {\tt z)}\/} & 
3. {{\tt (=}\ {\tt (=}\ {\tt y}\ {\tt z)}\ {\tt 0)}\/} 
\end{tabular}\\
syntactically legal expressions? 

\noindent Explain why the following sentences are illegal expressions: 
\\
 \begin{tabular}{lll}
1. {{\tt (3}\ {\tt +}\ {\tt 4)}\/} &
2. {{\tt empty?(l)}\/} &
3. {{\tt (x)}\/} \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/legal-ex1.html}{\rule{3pt}{5pt}}
\end{tabular}


{\bf Exercise 8.2.2}

 \label{ex:legal-def1}
Why are the sentences
\\
 \begin{tabular}{lll}
1. {{\tt (define}\ {\tt (f}\ {\tt x)}\ {\tt x)}\/} & 
2. {{\tt (define}\ {\tt (f}\ {\tt x)}\ {\tt y)}\/} &
3. {{\tt (define}\ {\tt (f}\ {\tt x}\ {\tt y)}\ {\tt 3)}\/} 
\end{tabular}\\
syntactically legal definitions? 

\noindent Explain why the following sentences are illegal definitions: 
\\
 \begin{tabular}{lll}
1. {{\tt (define}\ {\tt (f}\ {\tt \char'047}{\tt x)}\ {\tt x)}\/} & 
2. {{\tt (define}\ {\tt (f}\ {\tt x}\ {\tt y}\ {\tt z)}\ {\tt (x))}\/} & 
3. {{\tt (define}\ {\tt (f)}\ {\tt 10)}\/} \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/legal-def1.html}{\rule{3pt}{5pt}}
\end{tabular}


{\bf Exercise 8.2.3}

 \label{ex:illegal-exp1}
Distinguish syntactically legal from illegal sentences:
\\
 \begin{tabular}{lll}
1. {{\tt (x)}\/} &
2. {{\tt (+}\ {\tt 1}\ {\tt (not}\ {\tt x))}\/} &
3. {{\tt (+}\ {\tt 1}\ {\tt 2}\ {\tt 3)}\/} 
\end{tabular}\\
Explain why the sentences are legal or illegal.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/illegal-exp1.html}{\rule{3pt}{5pt}}


{\bf Exercise 8.2.4}

 \label{ex:illegal-def1}
Distinguish syntactically legal from illegal sentences:\\
\begin{tabular}{lll}
1. {{\tt (define}\ {\tt (f}\ {\tt x)}\ {\tt \char'047}{\tt x)}\/} &
2. {{\tt (define}\ {\tt (f}\ {\tt \char'047}{\tt x)}\ {\tt x)}\/} &
3. {{\tt (define}\ {\tt (f}\ {\tt x}\ {\tt y)}\ {\tt (+}\ {\tt \char'047}{\tt y}\ {\tt (not}\ {\tt x)))}\/} 
\end{tabular}\\
Explain why the sentences are legal definitions or why they 
fail to be legal definitions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/illegal-def1.html}{\rule{3pt}{5pt}}




\medskip\noindent{\bf Grammatical Terminology}:\  
The components of compound sentences have names. We have introduced some of
these names on an informal basis; for better communication, we introduce
all useful ones here. The second component of a definition, that is, the
non-empty sequence of variables, is a {\sc {header}}. Accordingly, the
expression component of a definition is called {\sc {body}}. The
variables that follow the first variable in a header are the
{\sc {parameters}} of a function.~{\rule{3pt}{5pt}}

\hrule
\begin{quote}
({\bf define} (\mbox{{\tt <{function-name}>}} \mbox{{\tt <{parameter}>}} \ldots \mbox{{\tt <{parameter}>}}) \mbox{{\tt <{body}>}})\\
[.5cm]
(\mbox{{\tt <{function}>}} \mbox{{\tt <{argument}>}} \ldots \mbox{{\tt <{argument}>}})\\
[.5cm]
({\bf cond} (\mbox{{\tt <{question}>}} \mbox{{\tt <{answer}>}}) \mbox{{\tt <{{\bf cond}-clause}>}} \ldots)
\end{quote}

\center{Figure: Syntactic naming conventions} \label{fig:terminology}
\hrule

People who think of definition as the definition of a mathematical function
also use the terminology {\sl left-hand side} for a definition's header
and {\sl right-hand side} for the body. For the same reason, the first
component in an application is called {\sl function} and the remaining
components are referred to as {\sl arguments}. Occasionally, we also
use {\sl actual arguments}.

Finally, a {{\bf cond}-exp\-res\-sion} consists of {{\tt cond}\/}-lines or
{{\tt cond}\/}-clauses. Each line consists of two expressions: the
{\sl question} and the {\sl answer}. A question is also called a
{\sl condition}. 

Figure~\ref{fig:terminology} provides a summary of the conventions.

 \subsection{The Meaning of Scheme} \label{sec:synsem:sem} 
\htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:synsem:sem}

A legal DrScheme program consists of two items: a sequence of function
definitions (in the {\tt Definitions} window) and a sequence of
interactions (in the {\tt Interactions} window). Each interaction is a
demand for the evaluation of one Scheme expression, which typically refers
to the functions defined in the upper part of DrScheme.

When DrScheme evaluates an expression, it uses nothing but the laws of
arithmetic and algebra to convert an expression into a value. In ordinary
mathematics courses, values are just numbers. We also include symbols,
booleans, and indeed all constants: 
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{val}>}} & = & \mbox{{\tt <{con}>}} 
\end{tabular}
\end{center}
The collection of values is thus just a subset of the collection of
expressions. 

Now that we have defined the set of values, it is easy to introduce and to
explain the evaluation rules. The rules come in two categories: those that
appeal to arithmetic knowledge and those that rely on a small amount of
algebra. First, we need an infinite number of rules like those of
arithmetic to evaluate applications of primitives:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (+}\  {\tt 1}\  {\tt 1)}\  {\tt =}\  {\tt 2}\\
{\tt (-}\  {\tt 2}\  {\tt 1)}\  {\tt =}\  {\tt 1}\  
\end{tabbing}\end{minipage}\end{center}
But Scheme ``arithmetic'' is more general than just number crunching. It
also includes rules for dealing with Boolean values, symbols, and
lists like these: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (not}\  {\sf true}{\tt )}\  {\tt =}\  {\sf false}\\
{\tt (symbol=?}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b)}\  {\tt =}\  {\sf false}
\\
{\tt (symbol=?}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt a)}\  {\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}

Second, we need one rule from algebra to understand how the application of
a user-defined function advances computation. Suppose the {\tt Definitions}
window contains the definition
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt x-1}\  {\tt ...}\  {\tt x-n)}\  \\
\>{\tt exp)}
\end{tabbing}\end{minipage}\end{center}
and {{\tt f\char'054}\ {\tt x-1\char'054}\ {\tt ...\char'054}\ {\tt x-n}\/} are variables and {{\tt exp}\/} is some
(legal) expression. Then an application of a function is governed by the
law: 
\begin{center}
\begin{tabular}{lcl}
{{{\tt (f}\ {\tt v-1}\ {\tt ...}\ {\tt v-n)}\/}}
& = &
{{{\tt exp}\/}    { with all } {{\tt x-1}\ {\tt ...}\ {\tt x-n}\/}    { replaced by } {{\tt v-1}\ {\tt ...}\ {\tt v-n}\/}}
\end{tabular}
\end{center}
where {{\tt v-1}\ {\tt ...}\ {\tt v-n}\/} is a sequence of values that is as long as 
{{\tt x-1}\ {\tt ...}\ {\tt x-n}\/}.

This rule is as general as possible, so it is best to look at a concrete
example. Say the definition is
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (poly}\  {\tt x}\  {\tt y)}\  \\
\>{\tt (+}\  {\tt (expt}\  {\tt 2}\  {\tt x)}\  {\tt y))}
\end{tabbing}\end{minipage}\end{center}
Then the application {{\tt (poly}\ {\tt 3}\ {\tt 5)}\/} can be evaluated as follows:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (poly}\  {\tt 3}\  {\tt 5)}\\
{\tt =}\  \={\tt (+}\  {\tt (expt}\  {\tt 2}\  {\tt 3)}\  {\tt 5))}
\\
\>\hbox{;; This line is {{\tt (+}\ {\tt (expt}\ {\tt 2}\ {\tt x)}\ {\tt y)}\/} where {{\tt x}\/} was replaced by {{\tt 3}\/} and {{\tt y}\/} by {{\tt 5}\/} .}
\\
{\tt =}\  {\tt (+}\  {\tt 8}\  {\tt 5)}
\\
{\tt =}\  {\tt 13}
\end{tabbing}\end{minipage}\end{center}
These last two steps follow from plain arithmetic. 

Third and finally, we need some rules that help us determine the value of
{{\bf cond}-exp\-res\-sion}s. These rules are algebraic rules but are not a
part of the standard algebra curriculum:

\begin{description}
\item[cond\_false:] when the first condition is {{\sf false}\/}: 

\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (c}\={\tt ond}\  \\
\>{\tt \char'133}{\sf false}\  {\tt ...\char'135}
\\
\>{\tt \char'133}{\tt exp1}\  {\tt exp2\char'135}
\\
\>{\tt ...)}
\end{tabbing}\end{minipage}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt =}\  {\tt (c}\={\tt ond}\\
\>\hbox{; The first line disappeared.}
\\
\>{\tt \char'133}{\tt exp1}\  {\tt exp2\char'135}
\\
\>{\tt ...)}
\end{tabbing}\end{minipage}

then the first {{\tt cond}\/}-line disappears; 

\item[cond\_true:] when the first condition is {{\sf true}\/}: 

\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (c}\={\tt ond}\  \\
\>{\tt \char'133}{\sf true}\  {\tt exp\char'135}\  
\\
\>{\tt ...)}\  
\end{tabbing}\end{minipage}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt =}\  {\tt exp}\\
\relax
\\
\relax
\end{tabbing}\end{minipage}

the entire {{\bf cond}-exp\-res\-sion}s is replaced by the first answer; 

\item[cond\_else:] when the only line left is the {{\tt else}\/}-line:

\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (c}\={\tt ond}\  \\
\>{\tt \char'133}{\tt else}\  {\tt exp\char'135}{\tt )}
\end{tabbing}\end{minipage}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt =}\  {\tt exp}\\
\relax
\end{tabbing}\end{minipage}

the {{\bf cond}-exp\-res\-sion}s is replaced by the answer in the
{{\tt else}\/}-clause. 

\end{description}
No other rules are needed to understand {{\tt cond}\/}.

Consider the following evaluation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\sf false}\  {\tt 1\char'135}
\\
\>{\tt \char'133}{\sf true}\  {\tt (+}\  {\tt 1}\  {\tt 1)\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt 3\char'135}{\tt )}
\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\sf true}\  {\tt (+}\  {\tt 1}\  {\tt 1)\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt 3\char'135}{\tt )}
\\
{\tt =}\  {\tt (+}\  {\tt 1}\  {\tt 1)}
\\
{\tt =}\  {\tt 2}
\end{tabbing}\end{minipage}\end{center}
It first eliminates a {{\tt cond}\/}-line and then equates the
{{\bf cond}-exp\-res\-sion} with~{{\tt (+}\ {\tt 1}\ {\tt 1)}\/}. The rest is plain
arithmetic again. 

The rules are equations of the form that we use in arithmetic and algebra
on a daily basis. Indeed, the same laws apply to this system of equations
as to those in mathematics. For example, if {{\tt a}\ {\tt =}\ {\tt b}\/} and \relax
{{\tt b}\ {\tt =}\ {\tt c}\/}, then we also know that {{\tt a}\ {\tt =}\ {\tt c}\/}. A consequence is
that as we get better at hand-evaluations, we can skip obvious steps and
combine several equational inferences into one. Here is one shorter version
of the previous evaluation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\sf false}\  {\tt 1\char'135}
\\
\>{\tt \char'133}{\sf true}\  {\tt (+}\  {\tt 1}\  {\tt 1)\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt 3\char'135}{\tt )}
\\
{\tt =}\  {\tt (+}\  {\tt 1}\  {\tt 1)}
\\
{\tt =}\  {\tt 2}
\end{tabbing}\end{minipage}\end{center}

Even more importantly, we can replace any expression by its equal in every
context---just as in algebra.  Here is a another {{\bf cond}-exp\-res\-sion}
and its evaluation:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (c}\={\tt ond}\\
\>\>{\tt \char'133}\hbox{$\underline{\mbox{{{\tt (=}\ {\tt 1}\ {\tt 0)}\/}}}$}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt 1}\  {\tt 1)\char'135}{\tt )}
\\
\>\hbox{;; The underlined expression is evaluated first. }
\\
{\tt =}\  \={\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\sf false}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt 1}\  {\tt 1)\char'135}{\tt )}
\\
\>\hbox{;; Here {\bf cond}\_{\bf false} applies. }
\\
{\tt =}\  \={\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt 1}\  {\tt 1)\char'135}{\tt )}
\\
\>\hbox{;; Using {\bf cond}\_{\bf else}, we now get an arithmetic expression. }
\\
{\tt =}\  {\tt (+}\  {\tt 1}\  {\tt 1)}
\\
{\tt =}\  {\tt 2}
\end{tabbing}\end{minipage}\end{center}
For the first step, we evaluated the nested, underlined expression, which
is clearly essential here, because no {{\tt cond}\/} rule would apply
otherwise. Of course, there is nothing unusual about this kind of
computing. We have done this many times in algebra and in the first few
sections of this book.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 8.3.1}

 \label{ex:eval1}
Evaluate the following expressions step by step: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$1.$}\  {\tt (+}\  {\tt (*}\  \={\tt (}\={\tt /}\  {\tt 12}\  {\tt 8)}\  {\tt 2/3)}\  \\
\>\>{\tt (-}\  {\tt 20}\  {\tt (sqrt}\  {\tt 4)))}
\\
\hbox{$2.$}\  {\tt (cond}
\\
\>{\tt \char'133}{\tt (=}\  {\tt 0}\  {\tt 0)}\  {\sf false}{\tt \char'135}
\\
\>{\tt \char'133}{\tt (>}\  {\tt 0}\  {\tt 1)}\  {\tt (symbol=?}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt a)\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (=}\  {\tt (/}\  \  {\tt 1}\  {\tt 0)}\  {\tt 9)\char'135}{\tt )}
\\
\hbox{$3.$}\  {\tt (cond}
\\
\>{\tt \char'133}{\tt (=}\  {\tt 2}\  {\tt 0)}\  {\sf false}{\tt \char'135}
\\
\>{\tt \char'133}{\tt (>}\  {\tt 2}\  {\tt 1)}\  {\tt (symbol=?}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt a)\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (=}\  {\tt (/}\  \  {\tt 1}\  {\tt 2)}\  {\tt 9)\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/eval1.html}{\rule{3pt}{5pt}}


{\bf Exercise 8.3.2}

 \label{ex:eval+def1}
Suppose the {\tt Definitions} window contains
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt f}\ {\tt :}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt x}\  {\tt y)}
\\
\>{\tt (+}\  {\tt (*}\  {\tt 3}\  {\tt x)}\  {\tt (*}\  {\tt y}\  {\tt y)))}
\end{tabbing}\end{minipage}\end{center}

\noindent Show how DrScheme evaluates the following expressions, step by step:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$1.$}\  {\tt (+}\  {\tt (f}\  {\tt 1}\  {\tt 2)}\  {\tt (f}\  {\tt 2}\  {\tt 1))}\\
\hbox{$2.$}\  {\tt (f}\  {\tt 1}\  {\tt (*}\  {\tt 2}\  {\tt 3))}
\\
\hbox{$3.$}\  {\tt (f}\  {\tt (f}\  {\tt 1}\  {\tt (*}\  {\tt 2}\  {\tt 3))}\  {\tt 19)}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/eval+def1.html}{\rule{3pt}{5pt}}


 \subsection{Errors} \label{sec:synsem:errors} 

Parenthesized sentences may or may not belong to Scheme, depending on
whether or not they are legal according to the grammar in
figure~\ref{fig:syn-beginner}.  If DrScheme verifies that a sentence does
not belong to the language dubbed {\tt Beginning Student}, it signals a
{\em syntax error\/}.

The remaining expressions are syntactically legal, but some of those may
still pose problems for our evaluation rules. We say that such legal
expressions contain {\em semantic\/} or {\sc {run-time}} errors.  Consider
the simplest example: {{\tt (/}\ {\tt 1}\ {\tt 0)}\/}. We already know from mathematics
that
$$\frac{1}{0}$$ does not have a value. Clearly, since Scheme's
calculations must be consistent with mathematics, it too must not equate
{{\tt (/}\ {\tt 1}\ {\tt 0)}\/} with a value. 

In general, if an expression is not a value and if the evaluation rules
allow no further simplification, we say that an error occurred or that the
function raises an error signal. Pragmatically this means that the
evaluation stops immediately with an appropriate error message, such as
{{\tt ``\char'057}{\tt :}\ {\tt divide}\ {\tt by}\ {\tt zero''}\/} for division by zero.  

For example, the evaluation of
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (+}\  {\tt (*}\  {\tt 20}\  {\tt 2)}\  {\tt (/}\  {\tt 1}\  {\tt (-}\  {\tt 10}\  {\tt 10)))}\end{tabbing}\end{minipage}\end{center}
proceeds as follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (+}\  {\tt (*}\  {\tt 20}\  {\tt 2)}\  {\tt (/}\  {\tt 1}\  {\tt (-}\  {\tt 10}\  {\tt 10)))}\\
{\tt =}\  {\tt (+}\  {\tt 40}\  {\tt (/}\  {\tt 1}\  {\tt 0))}
\\
{\tt =}\  {\tt /:}\  {\tt divide}\  {\tt by}\  {\tt zero}
\end{tabbing}\end{minipage}\end{center}
In particular, the error eliminates the context {{\tt (+}\ {\tt 40}\ {\tt ...)}\/} around
{{\tt (/}\ {\tt 1}\ {\tt 0)}\/}, which represents the remainder of the computation with
respect to the division. 

To understand how run-time errors are signaled, we must inspect the
evaluation rules again. Consider the function
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt my-divide}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (my-divide}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt 0)}\  {\tt \char'047}{\tt inf\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (/}\  {\tt 1}\  {\tt n)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Now suppose we apply {{\tt my-divide}\/} to {{\tt 0}\/}. Then the first step is: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (my-divide}\  {\tt 0)}\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (=}\  {\tt 0}\  {\tt 0)}\  {\tt \char'047}{\tt inf\char'135}
\\
\>{\tt \char'133}{\tt else}\  \hbox{$\underline{\mbox{{{\tt (/}\ {\tt 1}\ {\tt 0)}\/}}}$}{\tt \char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
It would obviously be wrong to say that the function signals the error ``/:
divide by zero'' now, even though an evaluation of the underlined
subexpression would demand it. After all, {{\tt (=}\ {\tt 0}\ {\tt 0)}\/} is
{{\sf true}\/} and therefore the application has a proper result: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (my-divide}\  {\tt 0)}\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (=}\  {\tt 0}\  {\tt 0)}\  {\tt \char'047}{\tt inf\char'135}
\\
\>{\tt \char'133}{\tt else}\  \hbox{$\underline{\mbox{{{\tt (/}\ {\tt 1}\ {\tt 0)}\/}}}$}{\tt \char'135}{\tt )}
\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\sf true}\  {\tt \char'047}{\tt inf\char'135}
\\
\>{\tt \char'133}{\tt else}\  \hbox{$\underline{\mbox{{{\tt (/}\ {\tt 1}\ {\tt 0)}\/}}}$}{\tt \char'135}{\tt )}
\\
{\tt =}\  {\tt \char'047}{\tt inf}
\end{tabbing}\end{minipage}\end{center}

Fortunately, our laws of evaluation take care of these situations
automatically. We just need to keep in mind when the laws apply. 
For example, in 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (+}\  {\tt (*}\  {\tt 20}\  {\tt 2)}\  {\tt (/}\  {\tt 20}\  {\tt 2))}\end{tabbing}\end{minipage}\end{center}
the addition cannot take place before the multiplication or
division. Similarly, the underlined division in the
{{\bf cond}-exp\-res\-sion}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (=}\  {\tt 0}\  {\tt 0)}\  {\tt \char'047}{\tt inf\char'135}
\\
\>{\tt \char'133}{\tt else}\  \hbox{$\underline{\mbox{{{\tt (/}\ {\tt 1}\ {\tt 0)}\/}}}$}{\tt \char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
cannot be evaluated until the corresponding line is the first condition in
the {{\bf cond}-exp\-res\-sion}. 

As a rule of thumb, it is best to keep the following in mind:

     \begin{rawhtml}     <table bgcolor=red align=center>       <tr><td><font align=center color=white size=+3>   \end{rawhtml} Guideline on Expression Evaluation   \begin{rawhtml}     <tr><td><font color=white><p>   \end{rawhtml} Evaluate the outermost and leftmost subexpressions first.    \begin{rawhtml}     </p></table>   \end{rawhtml} 

\noindent While this guideline is an oversimplification, it always explains Scheme's
results.

In some cases, programmers also want to define functions that raise errors. 
Recall the checked version of {{\tt area-of-disk}\/} from
section~\ref{sec:records}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt checked-area-of-disk}\ {\tt :}\ {\tt Scheme-value}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to compute the area of a disk with radius {{\tt v}\/}, if {{\tt v}\/} is a number}
\\
{\tt (d}\={\tt efine}\  {\tt (checked-area-of-disk}\  {\tt v)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (number?}\  {\tt v)}\  {\tt (area-of-disk}\  {\tt v)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt checked-area-of-disk}\  {\tt ``number}\  {\tt expected'')\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
If we were to apply {{\tt checked-area-of-disk}\/} to a symbol, we would get
the following evaluation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (-}\  \={\tt (checked-area-of-disk}\  {\tt \char'047}{\tt a)}\\
\>{\tt (checked-area-of-disk}\  {\tt 10))}
\\
{\tt =}\  {\tt (-}\  \={\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (number?}\  {\tt \char'047}{\tt a)}\  {\tt (area-of-disk}\  {\tt \char'047}{\tt a)\char'135}\  
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt checked-area-of-disk}\  {\tt ``number}\  {\tt expected'')\char'135}{\tt )}
\\
\>{\tt (checked-area-of-disk}\  {\tt 10))}
\\
{\tt =}\  {\tt (-}\  \={\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\sf false}\  {\tt (area-of-disk}\  {\tt \char'047}{\tt a)\char'135}\  \  \  
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt checked-area-of-disk}\  {\tt ``number}\  {\tt expected'')\char'135}{\tt )}
\\
\>{\tt (checked-area-of-disk}\  {\tt 10))}
\\
{\tt =}\  {\tt (-}\  \={\tt (error}\  {\tt \char'047}{\tt checked-area-of-disk}\  {\tt ``number}\  {\tt expected'')}
\\
\>{\tt (checked-area-of-disk}\  {\tt 10))}
\\
{\tt =}\  {\tt checked-area-of-disk}\  {\tt :}\  {\tt number}\  {\tt expected}
\end{tabbing}\end{minipage}\end{center}
In other words, when we evaluate an the {{\tt error}\/} expression, we
proceed as if we had encountered a division by zero.

 \subsection{Boolean Expressions} \label{sec:synsem:and-or} 

Our current definition of the Beginning Student Scheme language omits two
forms of expressions: {{\tt and}\/} and {{\tt or}\/} expressions. Adding them
provides a case study of how to study new language construct.  We must
first understand their syntax, then their semantics, and finally their
pragmatics.

Here is the revised grammar:
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{exp}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ ({\bf and} \mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}) \\
	  &&  $\,\,\vert\,\,$ ({\bf or} \mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}})
\end{tabular}
\end{center}
The grammar says that {{\tt and}\/} and {{\tt or}\/} are keywords, each
followed by two expressions. At first glance, the two look like
(primitive or function) applications. To understand why they are not, we
must look the pragmatics of these expressions first. 

Suppose we need to formulate a condition that determines whether the
{{\tt n}\/}-th fraction of {{\tt 1}\/} is {{\tt m}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  \={\tt (not}\  {\tt (=}\  {\tt n}\  {\tt 0))}\  \\
\>{\tt (=}\  {\tt (/}\  {\tt 1}\  {\tt n)}\  {\tt m))}
\end{tabbing}\end{minipage}\end{center}
We formulate the condition as an {{\tt and}\/} combination of two boolean
expressions, because we don't wish to divide by {{\tt 0}\/} accidentally.
Next, assume {{\tt n}\/} becomes {{\tt 0}\/} during the course of the
evaluation. Then the expression becomes 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  \={\tt (not}\  {\tt (=}\  {\tt 0}\  {\tt 0))}\  \\
\>{\tt (=}\  {\tt (/}\  {\tt 1}\  {\tt 0)}\  {\tt m))}
\end{tabbing}\end{minipage}\end{center}
Now, if {{\tt and}\/} were an ordinary operation, we would have to evaluate
both subexpressions, which would trigger an error in the second one. For
this reason, {{\tt and}\/} is not a primitive operation, but a special
expression. In short, we use {{\tt and}\/} and {{\tt or}\/} expressions to
combine boolean computations that may have to short-cut an evaluation.

Once we understand how {{\tt and}\/} and {{\tt or}\/} expressions should be
evaluated, it is easy to formulate macthing rules. Better still, we can 
formulate expressions in our first language that are equivalent to these
expressions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  \hbox{$\mbox{{\tt <{exp-1}>}}$}\  \hbox{$\mbox{{\tt <{exp-2}>}}$}{\tt )}\\
\hbox{$\equiv$}
\\
{\tt (c}\={\tt ond}
\\
\>{\tt \char'133}\hbox{$\mbox{{\tt <{exp-1}>}}$}\  \hbox{$\mbox{{\tt <{exp-2}>}}$}{\tt \char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt false\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
and
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (or}\  \hbox{$\mbox{{\tt <{exp-1}>}}$}\  \hbox{$\mbox{{\tt <{exp-2}>}}$}{\tt )}\\
\hbox{$\equiv$}
\\
{\tt (c}\={\tt ond}
\\
\>{\tt \char'133}\hbox{$\mbox{{\tt <{exp-1}>}}$}\  {\tt true\char'135}
\\
\>{\tt \char'133}{\tt else}\  \hbox{$\mbox{{\tt <{exp-2}>}}$}{\tt \char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
These equivalences simplify what actually takes place in DrScheme but they
are a perfectly appropriate model for now. 

 \subsection{Variable Definitions} \label{sec:synsem:var-defns} 
\htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:synsem:var-defns}

Programs consist not only of function definitions but also variable
definitions, but these weren't included in our first grammar. 

Here is the grammar rule for variable definitions:
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{def}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ ({\bf define} \mbox{{\tt <{var}>}} \mbox{{\tt <{exp}>}}) \\
\end{tabular}
\end{center}
The shape of a variable definition is similar to that of a function
definition.  It starts with a ``('', followed by the keyword
{{\tt define}\/}, followed by a variable, followed by an expression, and
closed by a right parenthesis ``)'' that matches the very first one.  The
keyword {{\tt define}\/} distinguishes variable definitions from
expressions, but not from function definitions. For that, a reader must look
at the second component of the definition.


Next we must understand what a variable definition means. A variable
definition like
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt RADIUS}\  {\tt 5)}\end{tabbing}\end{minipage}\end{center}
has a plain meaning. It says that wherever we encounter {{\tt RADIUS}\/}
during an evaluation, we may replace it with {{\tt 5}\/}. 

When DrScheme encounters a definition with a proper expression on the
right-hand side, it must evaluate that expression immediately. For example,
the right-hand side of the definition
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt DIAMETER}\  {\tt (*}\  {\tt 2}\  {\tt RADIUS))}\end{tabbing}\end{minipage}\end{center}
is the expression {{\tt (*}\ {\tt 2}\ {\tt RADIUS)}\/}. Its value is {{\tt 10}\/} because
{{\tt RADIUS}\/} stands for {{\tt 5}\/}. Hence, we can act as if we had written
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt DIAMETER}\  {\tt 10)}\end{tabbing}\end{minipage}\end{center}

In short, when DrScheme encounters a variable definition, it determines the
value of the right-hand side. For that step, it uses all those definitions
that {\em precede\/} the current definition but not those that follow. Once
DrScheme has a value for the right-hand side, it remembers that the name on
the left-hand side stands for this value. Whenever we evaluate an
expression, every occurrence of the {{\tt define}\/}d variable is replaced
by its value.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt RADIUS}\  {\tt 10)}\\
{\tt (define}\  {\tt DIAMETER}\  {\tt (*}\  {\tt 2}\  {\tt RADIUS))}
\\
\hbox{;; {{\tt area}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to compute the area of a disk with radius {{\tt r}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (area}\  {\tt r)}
\\
\>{\tt (*}\  {\tt 3.14}\  {\tt (*}\  {\tt r}\  {\tt r)))}
\\
{\tt (define}\  {\tt AREA-OF-RADIUS}\  {\tt (area}\  {\tt RADIUS))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: An example of variable definitions} \label{fig:defined-variables}
\hrule

Consider the sequence of definitions in figure~\ref{fig:defined-variables}.
As DrScheme steps through this sequence of definitions, it first determines
that {{\tt RADIUS}\/} stands for {{\tt 10}\/}, {{\tt DIAMETER}\/} for
{{\tt 20}\/}, and area is the name of a function. Finally, it evaluates
{{\tt (area}\ {\tt RADIUS)}\/} to {{\tt 314.0}\/} and associates
{{\tt AREA-OF-RADIUS}\/} with that value.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 8.6.1}

 \label{ex:def-var-syn}
Make up five examples of variable definitions. Use constants and
expressions on the right-hand side. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/def-var-syn.html}{\rule{3pt}{5pt}}


{\bf Exercise 8.6.2}

 \label{ex:def-var-sem2}
Evaluate the following sequence of definitions
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt RADIUS}\  {\tt 10)}\\
{\tt (define}\  {\tt DIAMETER}\  {\tt (*}\  {\tt 2}\  {\tt RADIUS))}
\\
{\tt (define}\  {\tt CIRCUMFERENCE}\  {\tt (*}\  {\tt 3.14}\  {\tt DIAMETER))}
\end{tabbing}\end{minipage}\end{center}
by hand.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/def-var-sem2.html}{\rule{3pt}{5pt}}


{\bf Exercise 8.6.3}

 \label{ex:def-var-sem3}
Evaluate the following sequence of definitions
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt PRICE}\  {\tt 5)}\\
{\tt (define}\  {\tt SALES-TAX}\  {\tt (*}\  {\tt .08}\  {\tt PRICE))}
\\
{\tt (define}\  {\tt TOTAL}\  {\tt (+}\  {\tt PRICE}\  {\tt SALES-TAX))}
\end{tabbing}\end{minipage}\end{center}
by hand.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/def-var-sem3.html}{\rule{3pt}{5pt}}


 \subsection{Structure Definitions} \label{sec:int1-structs} 

We still have to understand the syntax and semantics of one more Scheme
 construct: {{\tt define-struct}\/}. When we define a structure, we really
 define several primitive operations: a constructor, several selectors, and
 a predicate. Hence, {{\tt define-struct}\/} is by far the most complex
 Scheme construct we use. 

A structure definition is a third form of
 definition. The keyword {\bf define-struct} distinguishes this form of
 definition from function and variable definitions. The keyword is followed
 by a name and a sequence of names enclosed in parentheses:

\begin{center}
$\mbox{{\tt <{def}>}}$ = 
  ({{\tt define-struct}\/} $\mbox{{\tt <{var0}>}}$ ($\mbox{{\tt <{var-1}>}} \ldots \mbox{{\tt <{var-n}>}}$)) .
\end{center}
 The names in a {{\tt define-struct}\/} definition must be chosen as if they
 were variables or function names, though none of them are variables or
 function names. 

Here is a simple example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (define-struct}\  {\tt point}\  {\tt (x}\  {\tt y}\  {\tt z))}\  {\tt .}\  \end{tabbing}\end{minipage}\end{center}
 Since {{\tt point}\/}, {{\tt x}\/}, {{\tt y}\/}, and {{\tt z}\/} are variables and
 the parentheses are placed properly, it is a proper definition of a
 structure. In contrast,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (define-struct}\  {\tt (point}\  {\tt x}\  {\tt y}\  {\tt z))}\end{tabbing}\end{minipage}\end{center}
 and
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (define-struct}\  {\tt point}\  {\tt x}\  {\tt y}\  {\tt z)}\end{tabbing}\end{minipage}\end{center}
 are improper definitions because {{\tt define-struct}\/} is not followed
 by a single variable name and a sequence of variables in parentheses. 


A {{\tt define-struct}\/} definition introduces new primitive
 operations. The names of these operations are formed from those that occur
 in the definition.  Suppose a data structure definition has the following
 shape:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (define-struct}\  {\tt c}\  {\tt (s-1}\  {\tt ...}\  {\tt s-n))}\end{tabbing}\end{minipage}\end{center}
Then Scheme introduces the following primitive operations:  
\begin{enumerate}
\item {{\tt make-c}\/}: a {\em constructor\/};
\item {{\tt c-s-1}\ {\tt ...}\ {\tt c-s-n}\/}: a series of {\em selectors\/}; and
\item {{\tt c?}\/}: a {\em predicate\/}. 
\end{enumerate}
 These primitives have the same status as {{\tt +}\/}, {{\tt -}\/}, or
 {{\tt *}\/}. Before we can understand the rules that govern these new
 primitives, however, we must return to the definition of values. After
 all, the purpose of {{\tt define-struct}\/} is to introduce a new class of
 values: structures. 

Simply put, the set of values no longer consists of just constants, but
also of structures, which compound several values into one.  In terms of
our grammar of values, we must add one clause per {{\tt define-struct}\/}: 
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{val}>}} & = & ({\sf make-c} $\mbox{{\tt <{val}>}}$ ... $\mbox{{\tt <{val}>}}$)\\
\end{tabular}
\end{center}
Let us return to the {{\tt points}\/} structures.  Since the list of fields
 contains three names, {{\tt (make-point}\ {\tt u}\ {\tt v}\ {\tt w)}\/} is a value if
 {{\tt u}\/}, {{\tt v}\/}, and {{\tt w}\/} are values. 

Now we are in a position to understand the evaluation rules of the new
primitives. If {{\tt c-s-1}\/} is applied to a {{\tt c}\/} structure, it
returns the first component of the value. Similarly, the second selector
extracts the second component, the third selector the third component, and
so on. The relationship between the new data constructor and the selectors
is best characterized with $n$ equations:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c-s-1}\  {\tt (make-c}\  {\tt V-1}\=\  {\tt ...}\  {\tt V-n))}\  {\tt =}\  {\tt V-1}\\
\>\hbox{$\vdots$}
\\
{\tt (c-s-n}\  {\tt (make-c}\  {\tt V-1}\  {\tt ...}\  {\tt V-n))}\  {\tt =}\  {\tt V-n}
\end{tabbing}\end{minipage}\end{center}
where {{\tt V-1}\ {\tt ...}\ {\tt V-n}\/} is a sequence of values that is as long
as {{\tt s-1}\ {\tt ...}\ {\tt s-n}\/}. 

For our running example, we get the equations 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (point-x}\  {\tt (make-point}\  {\tt V}\  {\tt U}\  {\tt W))}\  {\tt =}\  {\tt V}\\
{\tt (point-y}\  {\tt (make-point}\  {\tt V}\  {\tt U}\  {\tt W))}\  {\tt =}\  {\tt U}
\\
{\tt (point-z}\  {\tt (make-point}\  {\tt V}\  {\tt U}\  {\tt W))}\  {\tt =}\  {\tt W}
\end{tabbing}\end{minipage}\end{center}
In particular, {{\tt (point-y}\ {\tt (make-point}\ {\tt 3}\ {\tt 4}\ {\tt 5))}\/} is equal to
{{\tt 4}\/}, and {{\tt (point-x}\ {\tt (make-point}\ {\tt (make-point}\ {\tt 1}\ {\tt 2}\ {\tt 3)}\ {\tt 4}\ {\tt 5))}\/}
evaluates to {{\tt (make-point}\ {\tt 1}\ {\tt 2}\ {\tt 3)}\/} because the latter is also a value. 

The predicate {{\tt c?}\/} can be applied to any value. It returns
{{\sf true}\/} if the value is of kind {{\tt c}\/} and {{\sf false}\/} otherwise.
We can translate both parts into equations. The first one,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c?}\  {\tt (make-c}\  {\tt V-1}\  {\tt ...}\  {\tt V-n))}\  {\tt =}\  {\sf true}\  {\tt \char'054}\  \end{tabbing}\end{minipage}\end{center}
relates {{\tt c?}\/} and values constructed with {{\tt make-c}\/}; the second one, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c?}\  {\tt V)}\  {\tt =}\  {\sf false}\hbox{; if {{\tt V}\/} is a value not constructed with {{\tt make-c}\/} , }\end{tabbing}\end{minipage}\end{center}
relates {{\tt c?}\/} to all other values. 

Again, the equations are best understood in terms of our example. Here are
the general equations:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (point?}\  {\tt (make-point}\  {\tt V}\  {\tt U}\  {\tt W))}\  {\tt =}\  {\sf true}\\
{\tt (point?}\  {\tt U)}\  {\tt =}\  {\sf false}\  \hbox{; if {{\tt U}\/} is value, but not a {{\tt point}\/} structure.}
\end{tabbing}\end{minipage}\end{center}
Thus, {{\tt (point?}\ {\tt (make-point}\ {\tt 3}\ {\tt 4}\ {\tt 5))}\/} is {{\sf true}\/} and
{{\tt (point?}\ {\tt 3)}\/} is {{\sf false}\/}.


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 8.7.1}

 \label{ex:define-struct-syn}
Distinguish legal from illegal sentences: 
\begin{enumerate}
\item {{\tt (define-struct}\ {\tt personnel-record}\ {\tt (name}\ {\tt salary}\ {\tt dob}\ {\tt ssn))}\/} 
\item {{\tt (define-struct}\ {\tt oops}\ {\tt ())}\/} 
\item {{\tt (define-struct}\ {\tt child}\ {\tt (dob}\ {\tt date}\ {\tt (-}\ {\tt date}\ {\tt dob)))}\/} 
\item {{\tt (define-struct}\ {\tt (child}\ {\tt person)}\ {\tt (dob}\ {\tt date))}\/} 
\item {{\tt (define-struct}\ {\tt child}\ {\tt (parents}\ {\tt dob}\ {\tt date))}\/}
\end{enumerate}

\noindent Explain why the sentences are legal {{\tt define-struct}\/}
 definitions or how they fail to be legal {{\tt define-struct}\/}
 definitions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/define-struct-syn.html}{\rule{3pt}{5pt}} 


{\bf Exercise 8.7.2}

 \label{ex:define-struct-value}
Which of the following are values and which are just applications of
{{\tt make-point}\/} to expressions? 
\begin{enumerate}
\item {{\tt (make-point}\ {\tt 1}\ {\tt 2}\ {\tt 3)}\/}
\item {{\tt (make-point}\ {\tt (make-point}\ {\tt 1}\ {\tt 2}\ {\tt 3)}\ {\tt 4}\ {\tt 5)}\/}
\item {{\tt (make-point}\ {\tt (+}\ {\tt 1}\ {\tt 2)}\ {\tt 3}\ {\tt 4)}\/}~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/define-struct-value.html}{\rule{3pt}{5pt}} 
\end{enumerate}




{\bf Exercise 8.7.3}

 \label{ex:define-struct2} \label{ex:define-struct2-r}
Suppose the {\tt Definitions} window contains 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt ball}\  {\tt (x}\  {\tt y}\  {\tt speed-x}\  {\tt speed-y))}\end{tabbing}\end{minipage}\end{center}
Determine the results of the following expressions: 
\begin{enumerate}
\item {{\tt (number?}\ {\tt (make-ball}\ {\tt 1}\ {\tt 2}\ {\tt 3}\ {\tt 4))}\/}
\item {{\tt (ball-speed-y}\ {\tt (make-ball}\ {\tt (+}\ {\tt 1}\ {\tt 2)}\ {\tt (+}\ {\tt 3}\ {\tt 3)}\ {\tt 2}\ {\tt 3))}\/}
\item {{\tt (ball-y}\ {\tt (make-ball}\ {\tt (+}\ {\tt 1}\ {\tt 2)}\ {\tt (+}\ {\tt 3}\ {\tt 3)}\ {\tt 2}\ {\tt 3))}\/}
\end{enumerate} 

\noindent Also check how DrScheme deals with the following expressions: 
\begin{enumerate}
\item {{\tt (number?}\ {\tt (make-ball}\ {\tt 1}\ {\tt 3}\ {\tt 4))}\/}
\item {{\tt (ball-x}\ {\tt (make-posn}\ {\tt 1}\ {\tt 2))}\/}
\item {{\tt (ball-speed-y}\ {\tt 5)}\/}
\end{enumerate} 
Verify your solutions with DrScheme.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/define-struct2.html}{\rule{3pt}{5pt}} 


\hrule
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{def}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ ({\bf define} (\mbox{{\tt <{var}>}} \mbox{{\tt <{var}>}} \ldots \mbox{{\tt <{var}>}}) \mbox{{\tt <{exp}>}}) \\
	  &&  $\,\,\vert\,\,$ ({\bf define} \mbox{{\tt <{var}>}} \mbox{{\tt <{exp}>}}) \\
          &&  $\,\,\vert\,\,$ ({\bf define-struct} \mbox{{\tt <{var0}>}} (\mbox{{\tt <{var-1}>}} \ldots \mbox{{\tt <{var-n}>}}))\\
\mbox{{\tt <{exp}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ \mbox{{\tt <{var}>}} \\
    && $\,\,\vert\,\,$ \mbox{{\tt <{con}>}} \\
    && $\,\,\vert\,\,$ (\mbox{{\tt <{prm}>}} \mbox{{\tt <{exp}>}} \ldots \mbox{{\tt <{exp}>}})\\
    && $\,\,\vert\,\,$ (\mbox{{\tt <{var}>}} \mbox{{\tt <{exp}>}} \ldots \mbox{{\tt <{exp}>}}) \\
    && $\,\,\vert\,\,$ ({\bf cond} (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}) \ldots (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}))\\
    && $\,\,\vert\,\,$ ({\bf cond} (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}) \ldots ({{\tt else}\/} \mbox{{\tt <{exp}>}}))\\
    && $\,\,\vert\,\,$ ({\bf and} \mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}})\\
    && $\,\,\vert\,\,$ ({\bf or} \mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}})\\
\end{tabular}
\end{center}
\center{Figure: {\tt Scheme Beginning Student}: The full grammar} \label{fig:syn-beginner-full}
\hrule


%% BEGIN INLINED FILE: curriculum1b.dl.tex





\newpage\part{Processing Arbitrarily Large Data}\thispagestyle{empty}\newpage \label{part:structural}

\section{Compound Data, Part 2: Lists} \label{sec:lists} 
\htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:records:lists}

\htmladdimg{../icons/teacher.gif}~{\em Programming is only necessary if we wish to process large amounts of data. To do so, we must develop data definitions that include data objects of all kinds of sizes. The key is that we don't know how large the data item is. We could refer to it as a ``data item of unknown size,'' but we prefer to use the mathematical term ``arbitrarily large.''  For motivation, have students play with the protect-the-wall game. Use the teachpack {\tt\bf protect-play.ss}.  Discuss how the game needs functions that manages a collection of balls: one function removes those balls that are no longer in the playing field, another one moves the remaining balls, etc. Each of these functions consumes a data item that describes an unknown number of balls.  In conventional languages, our first encounter with arbitrarily large data is about files. We read items from a file until no more items are left. This kind of function, however, involves io, which is what we don't want. Lists will play an analogous role in our world, but they are different, and because they are different, they generalize to other forms of data.}

Structures are one way to represent compound information, and lists are
another.  Forming lists is something that all of us do. Before we go
grocery shopping, we often write down a list of items that we want to
purchase. When we plan out a day in the morning, we write down a list of
things to do. During December, many children prepare Christmas wish lists.
To plan a party, we enumerate the people we want to invite. In short,
arranging information in the form of lists is a ubiquitous part of our
life, and we should learn to represents lists as Scheme data.  In this
section, we first learn to create lists and then move on to developing
functions that consume lists.

\subsection{Lists} \label{sec:lists:records} \htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:lists:long}

\htmladdimg{../icons/teacher.gif}~{\em The operation {{\tt cons}\/} is the analogue of enumeration in arithmetic. The most basic action of arithmetic is counting. When we count, we enumerate the numbers starting at 1 and continue with 2, 3, {\em etc\/}. In a sense, counting {\em creates\/} the numbers by adding 1 to 1, then 1 to 2, 1 to 3, {\it etc\/}.  Similarly, all lists in Scheme are created with {{\tt cons}\/}, starting from {{\tt empty}\/}. The difference is that we do not have short names for {{\tt cons}\/}tructed lists. Thus, instead of saying {{\tt (+}\ {\tt 1}\ {\tt 1)}\/}, we say 2. In contrast, list values like {{\tt (cons}\ {\tt 1}\ {\tt empty)}\/} are less common in daily life (and are more complex), so we don't have short names for {{\tt cons}\/}tructed lists. We use the parenthesized expressions as their names instead.}

When we form a list, we always start out with the empty list. In Scheme,
the word {{\tt empty}\/} represents the empty list. When we already have a
list, we can construct a longer list with the operation {{\tt cons}\/}. Here
is a simple example:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  {\tt \char'047}{\tt Mercury}\  {\tt empty)}\  \end{tabbing}\end{minipage}\end{center}
In this example, we {{\tt cons}\/}tructed a list from the {{\tt empty}\/}
list and the symbol {{\tt \char'047}{\tt Mercury}\/}. Figure~\ref{fig:build-a-list}
depicts this list in a pictorial manner, as a box with two fields:
{{\tt first}\/} and {{\tt rest}\/}. In this specific example the
{{\tt first}\/} field contains {{\tt \char'047}{\tt Mercury}\/} and the {{\tt rest}\/} field
contains {{\tt empty}\/}. 

\hrule






\begin{htmlonly}
\end{htmlonly}

 \begin{rawhtml} <table noborder cellspacing=20 align=center>  <!-- (cons 'Mercury empty) --> <tr><td align=left>(cons 'Mercury empty)</td><td>   <table border bgcolor=beige>   <tr> <td>'Mercury</td>        <td>empty</td>   </tr>   </table> </td></tr>  <!-- (cons 'Venus (cons 'Mercury empty)) --> <tr><td align=left>(cons 'Venus (cons 'Mercury empty))</td><td> <table border bgcolor=tan> <tr> <td>'Venus</td>      <td> 	<table border bgcolor=beige align=center> 	<tr> <td>'Mercury</td> 	     <td>empty</td> 	</tr> 	</table>      </td> </tr> </table> </td></tr>  <!-- (cons 'Earth (cons 'Venus (cons 'Mercury empty))) --> <tr ><td align=left>(cons 'Earth (cons 'Venus (cons 'Mercury empty)))</td><td> <table border bgcolor=earth> <tr> <td>'Earth</td>      <td> 	<table border bgcolor=tan align=center> 	<tr> <td>'Venus</td> 	     <td> 		<table border bgcolor=beige align=center> 		<tr> <td>'Mercury</td> 		     <td>empty</td> 		</tr> 		</table> 	     </td> 	</tr> 	</table>      </td> </tr> </table> </td></tr>  </table> \end{rawhtml}

\center{Figure: Building a list} \label{fig:build-a-list}
\hrule

Once we have a list with one item on it, we can {{\tt cons}\/}truct lists
with two items by using {{\tt cons}\/} again:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  {\tt \char'047}{\tt Venus}\  {\tt (cons}\  {\tt \char'047}{\tt Mercury}\  {\tt empty))}\end{tabbing}\end{minipage}\end{center}
The middle row of figure~\ref{fig:build-a-list} shows how we should imagine
the second list. It is also a box of two fields, but this time the
{{\tt rest}\/} field contains a box. Indeed, it contains the box from the
top row of the same figure. 

Finally, we construct a list with three items: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  {\tt \char'047}{\tt Earth}\  {\tt (cons}\  {\tt \char'047}{\tt Venus}\  {\tt (cons}\  {\tt \char'047}{\tt Mercury}\  {\tt empty)))}\end{tabbing}\end{minipage}\end{center}
The last row of figure~\ref{fig:build-a-list} illustrates the list with
three items. Its {{\tt rest}\/} field contains a box that contains a box
again. So, as we create lists we put boxes into boxes into boxes {\em etc\/}. While this may appear strange at first glance, it is nothing
different than the Chinese gift boxes or the nested drinking cups we get
for our early birthdays. The only difference is that we can nest lists much
deeper than any artist could nest physical boxes. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 9.1.1}

 \label{ex:list1}
Create Scheme lists that represent 
\begin{enumerate}
\item the list of all planets in our solar system; 
\item the following meal: steak, pommes-frites, beans, bread, water, juice
brie cheese, and ice-cream; 
\item and the list of basic colors.
\end{enumerate}
Sketch box representations of these lists, similar to those in
figure~\ref{fig:build-a-list}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list1.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


We can also make lists of numbers. As before, {{\tt empty}\/} is
the list without any items. Here is a list with 10 numbers: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ons}\  {\tt 0}\\
\>{\tt (c}\={\tt ons}\  {\tt 1}
\\
\>\>{\tt (c}\={\tt ons}\  {\tt 2}
\\
\>\>\>{\tt (c}\={\tt ons}\  {\tt 3}
\\
\>\>\>\>{\tt (c}\={\tt ons}\  {\tt 4}
\\
\>\>\>\>\>{\tt (c}\={\tt ons}\  {\tt 5}
\\
\>\>\>\>\>\>{\tt (c}\={\tt ons}\  {\tt 6}
\\
\>\>\>\>\>\>\>{\tt (c}\={\tt ons}\  {\tt 7}
\\
\>\>\>\>\>\>\>\>{\tt (c}\={\tt ons}\  {\tt 8}
\\
\>\>\>\>\>\>\>\>\>{\tt (cons}\  {\tt 9}\  {\tt empty))))))))))}
\end{tabbing}\end{minipage}\end{center}
To build it requires 10 {{\tt cons}\/}es and one {{\tt empty}\/}. 

In general a list does not have to contain values of one kind, but may
contain arbitrary values:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ons}\  {\tt \char'047}{\tt RobbyRound}\\
\>{\tt (c}\={\tt ons}\  {\tt 3}\  
\\
\>\>{\tt (c}\={\tt ons}\  {\sf true}
\\
\>\>\>{\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
Here the first item is a symbol, the second one is a number, and the last
one a Boolean. We could think of this list as the representation of a
personnel record that includes the name of the employee, the number of
years spent at the company, and whether the employee has health insurance
through the company plan.

Now suppose we are given a list of numbers. One thing we might wish to do
is add up the numbers on the list. To make this more concrete, let us
assume that we are only interested in lists of three numbers: 

\begin{quote} A {\sl {list-of-3-numbers}} is \\
       \centerline{ {{\tt (cons}\ {\tt x}\ {\tt (cons}\ {\tt y}\ {\tt (cons}\ {\tt z}\ {\tt empty)))}\/} }     where {{\tt x}\/}, {{\tt y}\/}, and {{\tt z}\/} are numbers. \end{quote}

\noindent We write down the contract, purpose, header, and examples as
before: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt add-up-3}\ {\tt :}\ {\tt list-of-3-numbers}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to add up the three numbers in {{\tt a-list-of-3-numbers}\/}}
\\
\hbox{;; examples: }
\\
\hbox{;;   {{\tt (add-up-3}\ {\tt (cons}\ {\tt 2}\ {\tt (cons}\ {\tt 1}\ {\tt (cons}\ {\tt 3}\ {\tt empty))))}\ {\tt =}\ {\tt 6}\/}}
\\
\hbox{;;   {{\tt (add-up-3}\ {\tt (cons}\ {\tt 0}\ {\tt (cons}\ {\tt 1}\ {\tt (cons}\ {\tt 0}\ {\tt empty))))}\ {\tt =}\ {\tt 1}\/}}
\\
{\tt (define}\  {\tt (add-up-3}\  {\tt a-list-of-3-numbers)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
To define the body, however, presents a problem. A {{\tt cons}\/}tructed
list is like a structure. Hence, we should layout a template with selector
expressions next. Unfortunately, we don't know how to select items from a
list.

In analogy to structure selectors, Scheme implements operations for
extracting the fields from a {{\tt cons}\/}tructed list: {{\tt first}\/} and
{{\tt rest}\/}.\footnote{The traditional names are {{\tt car}\/} and {{\tt cdr}\/}, but we will not use these nonsensical names.} The
{{\tt first}\/} operation extracts the item that we used to
{{\tt cons}\/}truct a list; the {{\tt rest}\/} operation extracts the list
field. 

To describe how {{\tt first}\/}, {{\tt rest}\/}, and {{\tt cons}\/} are
related, we can use equations that are similar to the equations that govern
addition and subtraction and structure creation and field extraction: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \hbox{$\underline{\mbox{{{\tt (first}\ {\tt (cons}\ {\tt 10}\ {\tt empty))}\/}}}$}\\
{\tt =}\  {\tt 10}
\\
\  \  \hbox{$\underline{\mbox{{{\tt (rest}\ {\tt (cons}\ {\tt 10}\ {\tt empty))}\/}}}$}
\\
{\tt =}\  {\tt empty}
\\
\  \  {\tt (first}\  \hbox{$\underline{\mbox{{{\tt (rest}\ {\tt (cons}\ {\tt 10}\ {\tt (cons}\ {\tt 22}\ {\tt empty)))}\/}}}$}{\tt )}
\\
{\tt =}\  \hbox{$\underline{\mbox{{{\tt (first}\ {\tt (cons}\ {\tt 22}\ {\tt empty))}\/}}}$}
\\
{\tt =}\  {\tt 22}
\end{tabbing}\end{minipage}\end{center}
The last one demonstrates how to evaluate nested expressions.  The key is
to think of {{\tt (cons}\ {\tt a-value}\ {\tt a-list)}\/} as a value. And, as always, we
start with the evaluation of the innermost parenthesized expressions that
can be reduced, just as in arithmetic. In the above calculations, the
expressions that are about to be reduced next are underlined.

Using {{\tt first}\/} and {{\tt rest}\/} we can now write down a template for
{{\tt add-up-3}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt add-up-3}\ {\tt :}\ {\tt list-of-3-numbers}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to add up the three numbers in {{\tt a-list-of-3-numbers}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (add-up-3}\  {\tt a-list-of-3-numbers)}\  
\\
\>{\tt ...}\  {\tt (first}\  {\tt a-list-of-3-numbers)}\  {\tt ...}\  
\\
\>{\tt ...}\  {\tt (first}\  {\tt (rest}\  {\tt a-list-of-3-numbers))}\  {\tt ...}
\\
\>{\tt ...}\  {\tt (first}\  {\tt (rest}\  {\tt (rest}\  {\tt a-list-of-3-numbers)))}\  {\tt ...}\  {\tt )}
\end{tabbing}\end{minipage}\end{center}
The three expressions remind us that the input, called
{{\tt a-list-of-3-numbers}\/}, contains three components and how to extract
them. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 9.1.2}

 \label{ex:list3}
Let {{\tt l}\/} be the list 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  {\tt 10}\  {\tt (cons}\  {\tt 20}\  {\tt (cons}\  {\tt 5}\  {\tt empty)))}\end{tabbing}\end{minipage}\end{center}
What are the values of the following expressions? 
\begin{enumerate}
\item {{\tt (rest}\ {\tt l)}\/}
\item {{\tt (first}\ {\tt (rest}\ {\tt l))}\/}
\item {{\tt (rest}\ {\tt (rest}\ {\tt l))}\/}
\item {{\tt (first}\ {\tt (rest}\ {\tt (rest}\ {\tt l)))}\/}
\item {{\tt (rest}\ {\tt (rest}\ {\tt (rest}\ {\tt l)))}\/}  \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list3.html}{\rule{3pt}{5pt}}
\end{enumerate}


{\bf Exercise 9.1.3}

 \label{ex:add3} 
Finish the development of {{\tt add-up-3}\/}, that is, define the body and
test the complete function on some examples. 

A list of three numbers is one possible representation for 3-dimensional
points. The distance of a 3-dimensional point to the origin of the
coordinate grid is computed in the same manner as that of 2-dimensional
point: by squaring the numbers, adding them up, and taking the square root.

Use the template for {{\tt add-up-3}\/} to develop {{\tt distance-to-0-for-3}\/}, 
which computes the distance of a 3-dimensional point to the origin.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/add3.html}{\rule{3pt}{5pt}}


{\bf Exercise 9.1.4}

 \label{ex:in2} 
Provide a data definition for lists of two symbols. Then develop the
function {{\tt contains-2-doll\char'077}\/}, which consumes a list of two symbols and
determines whether one of them is {{\tt \char'047}{\tt doll}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/in2.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt} 


\medskip\noindent{\bf On the Precise Relationship between Cons and Structures}:\  The
discussion of {{\tt cons}\/}, {{\tt first}\/}, and {{\tt rest}\/} suggests
that {{\tt cons}\/} creates a structure and {{\tt first}\/} and {{\tt rest}\/}
are ordinary selectors:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt pair}\  {\tt (left}\  {\tt right))}\\
{\tt (define}\  {\tt (our-cons}\  {\tt a-value}\  {\tt a-list)}\  {\tt (make-pair}\  {\tt a}\  {\tt d))}
\\
{\tt (define}\  {\tt (our-first}\  {\tt a-pair)}\  {\tt (pair-left}\  {\tt a-pair))}
\\
{\tt (define}\  {\tt (our-rest}\  {\tt a-pair)}\  {\tt (pair-right}\  {\tt a-pair))}
\\
{\tt (define}\  {\tt (our-cons\char'077}\  {\tt x)}\  {\tt (pair?}\  {\tt x))}
\end{tabbing}\end{minipage}\end{center}

Although these definitions are a good first approximation, they are
inaccurate in one important point. DrScheme's version of {{\tt cons}\/} is
really a checked version of {{\tt make-pair}\/}. Specifically, the
{{\tt cons}\/} operation ensures that the {{\tt right}\/} field is always a
list, {\it i.e.}, {{\tt cons}\/}tructed or {{\tt empty}\/}. This suggests the
following refinement: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (our-cons}\  {\tt a-value}\  {\tt a-list)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list)}\  {\tt (make-pair}\  {\tt any}\  {\tt a-list)\char'135}
\\
\>\>{\tt \char'133}{\tt (our-cons\char'077}\  {\tt a-list)}\  {\tt (make-pair}\  {\tt any}\  {\tt a-list)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt cons}\  {\tt ``list}\  {\tt as}\  {\tt second}\  {\tt argument}\  {\tt expected'')\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The definitions for {{\tt our-first}\/}, {{\tt our-rest}\/}, and
{{\tt our-cons\char'077}\/} remain the same. Finally, we must also promised not to
use {{\tt make-pair}\/} directly so that we don't accidentally build a bad
list.~{\rule{3pt}{5pt}}

\subsection{Data Definitions for Lists of Arbitrary Length} 
\label{sec:lists:long} 
\htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:lists:long}

Suppose we wish to represent the inventory of a toy store that sells such
things as dolls, make-up sets, clowns, bows, arrows, and soccer balls. To
make an inventory, a store owner would start with an empty sheet of paper
and slowly write down the names of the toys on the various shelves.

Representing a list of toys in Scheme is straightforward. We can simply use
Scheme's symbols for toys and then {{\tt cons}\/}truct lists from them. Here
are a few short samples:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt empty}\\
{\tt (cons}\  {\tt \char'047}{\tt ball}\  {\tt empty)}
\\
{\tt (cons}\  {\tt \char'047}{\tt arrow}\  {\tt (cons}\  {\tt \char'047}{\tt ball}\  {\tt empty))}
\\
{\tt (cons}\  {\tt \char'047}{\tt clown}\  {\tt empty)}
\\
{\tt (cons}\  {\tt \char'047}{\tt bow}\  {\tt (cons}\  {\tt \char'047}{\tt arrow}\  {\tt (cons}\  {\tt \char'047}{\tt ball}\  {\tt empty)))}
\\
{\tt (cons}\  {\tt \char'047}{\tt clown}\  {\tt (cons}\  {\tt \char'047}{\tt bow}\  {\tt (cons}\  {\tt \char'047}{\tt arrow}\  {\tt (cons}\  {\tt \char'047}{\tt ball}\  {\tt empty))))}
\end{tabbing}\end{minipage}\end{center}
For a real store, the list will contain many more items, and the list will
grow and shrink over time. In any case, we cannot say in advance how many
items these inventory lists will contain. Hence, if we wish to develop a
function that consumes such lists, we cannot simply say that the input is a
list with either one, two, three, or four items. We must be prepared to
think about lists of arbitrary length.

In other words, we need a data definition that precisely describes the
class of lists that contain an arbitrary number of symbols.  Unfortunately,
the data definitions we have seen so far can only describe classes of data
where each item is of a fixed size, such as a structure with a specific
number of components or a list with a specific number of items. So how can
we describe a class of lists of arbitrary size?

Looking back we see that all our examples fall into one of two
categories. The store owner starts with an empty list and
{{\tt cons}\/}tructs longer and longer lists. The construction proceeds by
{{\tt cons}\/}ing together a toy and another list of toys. Here is a
data definition that reflects this process: 

  \begin{quote} A {\sl {list of symbols}} ({{\tt list-of-symbols}\/}) is either     \begin{enumerate}       \item the empty list, {{\tt empty}\/}, or       \item {{\tt (cons}\ {\tt s}\ {\tt los)}\/} where {{\tt s}\/} is a symbol and          {{\tt los}\/} is a list of symbols.        \end{enumerate} \end{quote}

\noindent This definition is unlike any of the definitions we have seen so
far or that we encounter in high school English or mathematics. Those
definitions explain a new idea in terms of old, well-understood
concepts. In contrast, this definition refers to {\em itself\/} in the item
labeled~2, which implies that it explains what a list of symbols is in
terms of lists of symbols.  We call such definitions {\em self-referential\/} or {\em recursive\/}.

At first glance, a definition that explains or specifies something in terms
of itself does not seem to make much sense. This first impression, however,
is wrong. A recursive definition, like the one above, make sense as long as
we can construct some elements from it; the definition is correct if we
can construct all intended elements.\footnote{It is common that a data definition describes a class of data that contains more than the intended elements. This limitation is inherent and is just one of the many symptoms of the limits of computing.}

Let's check whether our specific data definition makes sense and contains
all the elements we are interested in. From the first clause we immediately
know that {{\tt empty}\/} is a list of symbols. From the second clause we
know that we can create larger lists with {{\tt cons}\/} from a symbol and a
list of symbols. Thus {{\tt (cons}\ {\tt \char'047}{\tt ball}\ {\tt empty)}\/} is a list of symbols
because we just determined that {{\tt empty}\/} is one and we know that
{{\tt \char'047}{\tt doll}\/} is a symbol. There is nothing special about
{{\tt \char'047}{\tt doll}\/}. Any other symbol could serve equally well to form a number
of one-item lists of symbols:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  {\tt \char'047}{\tt make-up-set}\  {\tt empty)}\\
{\tt (cons}\  {\tt \char'047}{\tt water-gun}\  {\tt empty)}
\\
{\tt ...}
\end{tabbing}\end{minipage}\end{center}
Once we have lists that contain one symbol, we can use the same method to
build lists with two items:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  {\tt \char'047}{\tt Barbie}\  {\tt (cons}\  {\tt \char'047}{\tt robot}\  {\tt empty))}\\
{\tt (cons}\  {\tt \char'047}{\tt make-up-set}\  {\tt (cons}\  {\tt \char'047}{\tt water-gun}\  {\tt empty))}
\\
{\tt (cons}\  {\tt \char'047}{\tt ball}\  {\tt (cons}\  {\tt \char'047}{\tt arrow}\  {\tt empty))}
\\
{\tt ...}
\end{tabbing}\end{minipage}\end{center}
From here, it is easy to see how we can form lists that contain an
arbitrary number of symbols. More importantly still for our problem, all
possible inventories are adequately described by our data
definition.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 9.2.1}

 \label{ex:list5} 
Show that all the inventory lists discussed at the beginning of this
section belong to the class {{\tt list-of-symbols}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list5.html}{\rule{3pt}{5pt}}


{\bf Exercise 9.2.2}

 \label{ex:list6} 
Do all lists of two symbols also belong to the class {{\tt list-of-symbols}\/}?
Provide a concise argument.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list6.html}{\rule{3pt}{5pt}}


{\bf Exercise 9.2.3}

 \label{ex:list7} 
Provide a data definition for the class of {list of booleans}. The
class contains all arbitrarily large lists of booleans.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list7.html}{\rule{3pt}{5pt}}


\subsection{Processing Lists of Arbitrary Length} 
\label{sec:lists:process} 

A real store will want to have a large inventory on-line, that is, put into
a computer, so that an employee can quickly determine whether a toy is
available or not. For simplicity, assume that we need
{{\tt contains-doll\char'077}\/}, a function that checks whether the store has a
{{\tt \char'047}{\tt doll}\/}. Translated into Scheme terminology, the function determines
whether {{\tt \char'047}{\tt doll}\/} occurs on some list of symbols.

Because we already have a rigorous definition of {{\tt contains-doll\char'077}\/}'s
input, we turn to the contract, header, and purpose statement: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt contains-doll\char'077}\ {\tt :}\ {\tt list-of-symbols}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether the symbol {{\tt \char'047}{\tt doll}\/} occurs on {{\tt a-list-of-symbols}\/}}
\\
{\tt (define}\  {\tt (contains-doll\char'077}\  {\tt a-list-of-symbols)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Following the design recipe, we next make up some examples that illustrate
{{\tt contains-doll\char'077}\/} purpose. First, we clearly need to determine the
output for the simplest input: {{\tt empty}\/}. Since the list does not
contain any symbol, it certainly does not contain {{\tt \char'047}{\tt doll}\/}, and the
answer should be {{\sf false}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (contains-doll\char'077}\  {\tt empty)}\  \\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
Next, we consider lists with a single item. Here are two examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (contains-doll\char'077}\  {\tt (cons}\  {\tt \char'047}{\tt ball}\  {\tt empty))}\  \\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (contains-doll\char'077}\  {\tt (cons}\  {\tt \char'047}{\tt doll}\  {\tt empty))}\\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
In the first case, the answer is {{\sf false}\/} because the single item on the
list is not {{\tt \char'047}{\tt doll}\/}; in the second case, the item is {{\tt \char'047}{\tt doll}\/},
and the answer is {{\sf true}\/}. Finally, here are two more general examples,
with lists of several items: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (contains-doll\char'077}\  {\tt (cons}\  {\tt \char'047}{\tt bow}\  {\tt (cons}\  {\tt \char'047}{\tt arrow}\  {\tt (cons}\  {\tt \char'047}{\tt ball}\  {\tt empty))))}\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (contains-doll\char'077}\  {\tt (cons}\  {\tt \char'047}{\tt arrow}\  {\tt (cons}\  {\tt \char'047}{\tt doll}\  {\tt (cons}\  {\tt \char'047}{\tt ball}\  {\tt empty))))}\\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
Again, the answer in the first case must be {{\sf false}\/} because the list
does not contain {{\tt \char'047}{\tt doll}\/}, and in the second case it must be
{{\sf true}\/} because {{\tt \char'047}{\tt doll}\/} is one of the items on the list provided
to the function. 

The next step is to design a function template that matches the data
definition.  Since the data definition for lists of symbols has two
clauses, the function's body must be a {{\bf cond}-exp\-res\-sion}. The
{{\bf cond}-exp\-res\-sion} determines which of the two kinds of lists the
function received: the {{\tt empty}\/} list or a {{\tt cons}\/}tructed list:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (contains-doll\char'077}\  {\tt a-list-of-symbols)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-symbols)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (cons?}\  {\tt a-list-of-symbols)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Instead of {{\tt (cons?}\ {\tt a-list-of-symbols)}\/}, we can use {{\tt else}\/} in
the second clause.

We can add one more hint to the template by studying each clause of the
{{\bf cond}-exp\-res\-sion} in turn. Specifically, recall that the design
recipe suggests to annotate each clause with selector expressions if the
corresponding class of inputs consists of compounds. In our case, we know
that {{\tt empty}\/} does not have compounds, so there are no
components. Otherwise the list is {{\tt cons}\/}tructed from a symbol and
another list of symbols, and we remind ourselves of this fact by adding
{{\tt (first}\ {\tt a-list-of-symbols)}\/} and {{\tt (rest}\ {\tt a-list-of-symbols)}\/} to
the template:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (contains-doll\char'077}\  {\tt a-list-of-symbols)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-symbols)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt a-list-of-symbols)}\  {\tt ...}\  {\tt (rest}\  {\tt a-list-of-symbols)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}


Now that we have a template based on our design recipes for mixed and
compound data, we turn to the definition of the function's body, dealing
with each {{\tt cond}\/}-clause separately.  If {{\tt (empty?}{ }{\tt a-list-of-symbols)}\/} is true, the input is the empty list, in which case the
function must produce the result {{\sf false}\/}.  In the second case,
{{\tt (cons?}\ {\tt a-list-of-symbols)}\/} is true. The annotations in the
template remind us that there is a first symbol and the rest of the list.
So let us consider an example that falls into this category:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ons}\  {\tt \char'047}{\tt arrow}\  \\
\>{\tt (con}\={\tt s}\  {\tt ...}
\\
\>\>{\tt ...}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
The function, just like a human being, must clearly compare the first item
with {{\tt \char'047}{\tt doll}\/}. In this example, the first symbol is {{\tt \char'047}{\tt arrow}\/}
and not {{\tt \char'047}{\tt doll}\/}, so the comparison will yield {{\sf false}\/}. If we had
considered some other example instead, say, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ons}\  {\tt \char'047}{\tt doll}\\
\>{\tt (con}\={\tt s}\  {\tt ...}
\\
\>\>{\tt ...}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
the function would determine that the first item on the input is
{{\tt \char'047}{\tt doll}\/}, and would therefore respond with {{\sf true}\/}.  All of this
implies that the second line in the {{\bf cond}-exp\-res\-sion} should contain
another {{\bf cond}-exp\-res\-sion}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (contains-doll\char'077}\  {\tt a-list-of-symbols)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-symbols)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (first}\  {\tt a-list-of-symbols)}\  {\tt \char'047}{\tt doll)}\  
\\
\>\>\>\>{\sf true}{\tt \char'135}
\\
\>\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>\>{\tt ...}\  {\tt (rest}\  {\tt a-list-of-symbols)}\  {\tt ...\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Furthermore, if the comparison of {{\tt (first}\ {\tt a-list-of-symbols)}\/} yields
{{\sf true}\/}, the function is done and produces {{\sf true}\/}, too. 

If the comparison yields {{\sf false}\/}, we are left with another list of
symbols: {{\tt (rest}\ {\tt a-list-of-symbols)}\/}. Clearly, we can't know the
final answer in this case, because depending on what ``\ldots'' represents,
the function must produce {{\sf true}\/} or {{\sf false}\/}. Put differently, if
the first item is not {{\tt \char'047}{\tt doll}\/}, we need some way to check whether the
rest of the list contains {{\tt \char'047}{\tt doll}\/}. 

Fortunately, we have just such a function: {{\tt contains-doll\char'077}\/}, which
according to its purpose statement determines whether a list contains
{{\tt \char'047}{\tt doll}\/}. The purpose statement implies that if {{\tt l}\/} is a list
of symbols, {{\tt (contains-doll\char'077}\ {\tt l)}\/} tells us whether {{\tt l}\/}
contains the symbol {{\tt \char'047}{\tt doll}\/}. Similarly, {{\tt (contains-doll\char'077}{ }{\tt (rest}\ {\tt l))}\/} determines whether the rest of {{\tt l}\/} contains
{{\tt \char'047}{\tt doll}\/}. And in the same vein, {{\tt (contains-doll\char'077}\ {\tt (rest}{ }{\tt a-list-of-symbols))}\/} determines whether or not {{\tt \char'047}{\tt doll}\/} is in
{{\tt (rest}\ {\tt a-list-of-symbols)}\/}, which is precisely what we need to know
now.

Here is the complete definition of the function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (contains-doll\char'077}\  {\tt a-list-of-symbols)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-symbols)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt (first}\  {\tt a-list-of-symbols)}\  {\tt \char'047}{\tt doll)}\  {\sf true}{\tt \char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (contains-doll\char'077}\  {\tt (rest}\  {\tt a-list-of-symbols))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
It consumes a list of symbols and determines whether or not it is empty. If
it is, the result is {{\sf false}\/}. Otherwise, the list is not empty and the
result of the function depends on the first item of the list. If the first
item is {{\tt \char'047}{\tt doll}\/}, the result is {{\sf true}\/}; if not, the function's
result is the result of searching the {{\tt rest}\/} of the input
list---whatever it is. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 9.3.1}

 \label{ex:doll-eval}
Use DrScheme to test the definition of {{\tt contains-doll\char'077}\/} on our examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt empty}\\
{\tt (cons}\  {\tt \char'047}{\tt ball}\  {\tt empty)}
\\
{\tt (cons}\  {\tt \char'047}{\tt arrow}\  {\tt (cons}\  {\tt \char'047}{\tt doll}\  {\tt empty))}
\\
{\tt (cons}\  {\tt \char'047}{\tt bow}\  {\tt (cons}\  {\tt \char'047}{\tt arrow}\  {\tt (cons}\  {\tt \char'047}{\tt ball}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/doll-eval.html}{\rule{3pt}{5pt}}




{\bf Exercise 9.3.2}

 \label{ex:doll-variant}
Another way of formulating the second {{\tt cond}\/}-clause in the function 
{{\tt contains-doll\char'077}\/} is to understand 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (contains-doll\char'077}\  {\tt (rest}\  {\tt a-list-of-symbols))}\end{tabbing}\end{minipage}\end{center}
as a condition that evaluates to either {{\tt true}\/} or {{\tt false}\/},
and to combine it appropriately with the condition
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (symbol=?}\  {\tt (first}\  {\tt a-list-of-symbols)}\  {\tt \char'047}{\tt doll)}\end{tabbing}\end{minipage}\end{center}
Reformulate the definition of {{\tt contains-doll\char'077}\/} according to this
observation.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/doll-variant.html}{\rule{3pt}{5pt}}


{\bf Exercise 9.3.3}

 \label{ex:containsp}
Develop the function {{\tt contains\char'077}\/}, which consumes a symbol and a list
of symbols and determines whether or not the symbol occurs on the
list.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/containsp.html}{\rule{3pt}{5pt}}


\subsection{Designing Functions for Self-Referential Data Definitions} 
\label{sec:lists:design} 

At first glance, self-referential data definitions seem to be far more
complex than those for compound or mixed data. But, as the example in the
preceding subsection shows, our design recipes still work. Nevertheless, in
this section we discuss a new design recipe that works better for
self-referential data definitions. As implied by the preceding section, the
new recipe generalizes those for compound and mixed data. The new parts
concern the process of discovering when a self-referential data definition
is needed, deriving a template, and defining the function body:

\begin{description}
\item[Data Analysis \& Design:] If a problem statement discusses
compound information of arbitrary size, we need a recursive or
self-referential data definition. At this point, we have only seen one such
class, {{\tt list-of-symbols}\/}, but it is easy to imagine other, yet
similar classes of lists. We will get to know many other examples in this
and the following part.\footnote{Numbers also seem to be arbitrarily large. For inexact numbers, this is an illusion. For precise integers, this is indeed the case, and we will discuss them later in this part.} 

For a recursive data definition to be valid, it must satisfy two
conditions. First, it must contain at least two clauses. Second, at least
one of the clauses must not refer back to the definition. It is good
practice to identify the self-references explicitly with arrows from the
references in the data definition back to its beginning. 

Our running example for this section are functions that consume lists of
symbols: 

\begin{picture}(200,110)
\put(100,75){\vector(-4,-1){30}}
\qbezier[100](260,25)(350,155)(100,75)
\put(-15,40){\begin{minipage}{5in}   \begin{quote} A {\sl {list of symbols}} ({{\tt list-of-symbols}\/}) is either     \begin{enumerate}     \item the empty list, {{\tt empty}\/}, or     \item {{\tt (cons}\ {\tt s}\ {\tt lof)}\/} where {{\tt s}\/} is a symbol and {{\tt lof}\/} is a \fbox{list of symbols}.     \end{enumerate} \end{quote} \end{minipage}}
\end{picture}

\item[Template:] A self-referential data definition specifies a mixed class
of data, and one of the clauses should specify a subclass of compound
data. Hence the design of the template can proceed according to the recipes
in sections~\ref{sec:design3}
and~\ref{sec:mixed-data:design}. Specifically, we formulate a
{{\bf cond}-exp\-res\-sion} with as many {{\tt cond}\/}-clauses as there are
clauses in the data definition, match each recognizing condition to the
corresponding clause in the data definition, and write down appropriate
selector expressions in all {{\tt cond}\/}-lines that process compound
values.

In addition, we inspect each selector expression. For each that extracts a
value of the same class of data as the input, we draw an arrow back to the
function parameter. At the end, we must have as many arrows as we have in
the data definition.

Let's return to the running example. The template for a list-process\-ing
function contains a {{\bf cond}-exp\-res\-sion} with two clauses and one arrow: 

\begin{picture}(200,130)
\put(100,75){\vector(-4,-1){25}}
\qbezier[100](260,30)(350,155)(100,75)
\put(-15,40){\begin{minipage}{5in} \begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (fun-for-los}\  {\tt a-list-of-symbols)}\\
\>{\tt (c}\={\tt ond} \\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-symbols)}\  {\tt ...\char'135} \\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt a-list-of-symbols)}\  {\tt ...}\  \hbox{$\fbox{\mbox{{{\tt (rest}\ {\tt a-list-of-symbols)}\/}}}$}\  {\tt ...\char'135}{\tt ))} \end{tabbing}\end{minipage}\end{center} \end{minipage}}
\end{picture}

For simplicity, this book will use a textual alternative to arrows. Instead
of drawing an arrow, the templates contain self-applications of the function
to the selector expression(s):
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (fun-for-los}\  {\tt a-list-of-symbols)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-symbols)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}\  {\tt ...}\  {\tt (first}\  {\tt a-list-of-symbols)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (fun-for-los}\  {\tt (rest}\  {\tt a-list-of-symbols))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
We refer to these self-applications as {\sc {natural recursions}}.

\item[Body:] For the design of the body we start with those
 {{\tt cond}\/}-lines that do not contain natural recursions. They are
 called {\sc {base cases}}. The corresponding answers are typically easy
 to formulate or are already given by the examples.

Then we deal with the self-referential cases. We start by reminding
ourselves what each of the expressions in the template line computes. For
the recursive application we assume that the function already works as
specified in our purpose statement. {\bf The rest is then a matter of combining the various values.}

Suppose we wish to define the function {{\tt how-many}\/}, which determines
how many symbols are on a list of symbols. Assuming we have followed the
design recipe, we have the following: 

\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt how-many}\ {\tt :}\ {\tt list-of-symbols}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to determine how many symbols are on {{\tt a-list-of-symbols}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (how-many}\  {\tt a-list-of-symbols)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-symbols)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}\  {\tt ...}\  {\tt (first}\  {\tt a-list-of-symbols)}\  {\tt ...}\  
\\
\>\>\>{\tt ...}\  {\tt (how-many}\  {\tt (rest}\  {\tt a-list-of-symbols))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}

The answer for the base case is {{\tt 0}\/} because the empty list contains
nothing. The two expressions in the second clause compute the
{{\tt first}\/} item and the number of symbols on the {{\tt (rest}{ }{\tt a-list-of-symbols)}\/}. To compute how many symbols there are on all of
{{\tt a-list-of-symbols}\/}, we just need to add {{\tt 1}\/} to the value of
the latter expression:

\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (how-many}\  {\tt a-list-of-symbols)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-symbols)}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (how-many}\  {\tt (rest}\  {\tt a-list-of-symbols))}\  {\tt 1)\char'135}{\tt ))}
\end{tabbing}\end{minipage}

\item[Combining Values:] In many cases, the combination step can be
expressed with Scheme's primitives, for example, {{\tt +}\/}, {{\tt and}\/},
or {{\tt cons}\/}. If the problem statement suggests that we ask questions
about the first item, we may need a nested
{{\tt cond}\/}-statement. Finally, in some cases, we may have to define
auxiliary functions.
\end{description}
Figure~\ref{fig:design5} summarizes this discussion in the usual format;
those design steps that we didn't discuss are performed as before.  The
following section discusses several examples in detail.

%% BEGIN INLINED FILE: idesign5.dl.tex
%% BEGIN INLINED FILE: design1.dl.tex






%% BEGIN INLINED FILE: design3.dl.tex





%% BEGIN INLINED FILE: design4.dl.tex





%% BEGIN INLINED FILE: design5.dl.tex




\hrule
\begin{center} \begin{tabular}{|l|l|l|}\hline Phase            &  Goal 		& Activity \\
 \hline\hline \begin{minipage}[t]{.60in} Data\\
~~Analysis\\
~~\& Design \end{minipage}  & \begin{minipage}[t]{1.6in}\raggedright to formulate a group of data definitions \rule{0pt}{6pt}\end{minipage} & \begin{minipage}[t]{2.2in}\raggedright {develop a data definition for mixed data with at least two alternatives $\bullet$\  one alternative must not refer to the definition $\bullet$\  explicitly identify all self-references in the data definition}\rule{0pt}{6pt}\end{minipage}   \\
 \hline \begin{minipage}[t]{.60in} Contract\\
 Header~\&\\
 Purpose\\
 \end{minipage} 	&    \begin{minipage}[t]{1.6in}\raggedright to name the program,\\
  	 to specify its inputs \& \\
~~outputs,\\
 	 to describe its purpose,\\
 	 to formulate a program \\
~~header  \rule{0pt}{6pt}\end{minipage}  & \begin{minipage}[t]{2.2in}\raggedright {name the function, its inputs, its output, and specify its purpose: \\
   ~;; {\it name\/} : {\it in1\/} {\it in2\/} \ldots $\rightarrow$ {\it out\/} \\
   ~;; {\bf Purpose}: to compute \ldots\ {\it x1\/} \ldots {\it x2\/} \\
   ~({\bf define} ({\it name x1 x2 \ldots\/}) \ldots) }\rule{0pt}{6pt}\end{minipage} \\
 \hline Examples 	& \begin{minipage}[t]{1.6in}\raggedright to characterize the input-\\
 output relationship via examples \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {create examples of the input-output relationship $\bullet$\  make sure there is at least one example per alternative}\rule{0pt}{6pt}\end{minipage}   \\
 \hline Template        & \begin{minipage}[t]{1.6in}\raggedright to formulate an outline for a group of programs \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {develop a {\bf cond}-expression with one clause per alternative $\bullet$\  add selector expressions to each clause $\bullet$\  annotate the body with {\em natural recursions\/} $\bullet$\  {\sc Test}: the self-references in this template and the data definition match!}\rule{0pt}{6pt}\end{minipage}   \\
 \hline Body	 	& \begin{minipage}[t]{1.6in}\raggedright to define the program \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {formulate a Scheme expression for each simple {\bf cond}-line $\bullet$\  explain for all other {\bf cond}-clauses what each natural recursion will compute $\bullet$\  formulate the answer, using selector expressions, parameters, {\it etc\/}.}\rule{0pt}{6pt}\end{minipage}   \\
 \hline Test		& \begin{minipage}[t]{1.6in}\raggedright to expose mistakes\\
~~(``typos'' and logic) \rule{0pt}{6pt}\end{minipage} & \begin{minipage}[t]{2.2in}\raggedright {apply the function to the inputs of the examples  $\bullet$\  check that the outputs are as predicted above}\rule{0pt}{6pt}\end{minipage}   \\
 \hline \end{tabular} \end{center}
\center{Figure: The revised design recipe for recursive data} \label{fig:design5}
\centerline{(Refines the recipes in  figures~\ref{fig:design1} (pg.~\pageref{fig:design1}),  \ref{fig:design3} (pg.~\pageref{fig:design3}) \ref{fig:design4} (pg.~\pageref{fig:design4}))}
\hrule

\subsection{More on Procssing Simple Lists} \label{sec:lists:more}

Let us now look at another aspect of inventory management: the cost of an
inventory. In addition to a list of the available toys, a store owner
should also maintain a list of the cost of each item. The cost list permits
the owner to determine how much the current inventory is worth or, given
the inventory at the beginning of the year and that of the end of the year,
how much profit the store makes.

A list of costs is most easily represented as a list of numbers:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt empty}\\
{\tt (cons}\  {\tt 1.22}\  {\tt empty)}
\\
{\tt (cons}\  {\tt 2.59}\  {\tt empty)}
\\
{\tt (cons}\  {\tt 1.22}\  {\tt (cons}\  {\tt 2.59}\  {\tt empty))}
\\
{\tt (cons}\  {\tt 17.05}\  {\tt (cons}\  {\tt 1.22}\  {\tt (cons}\  {\tt 2.59}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
Again, for a real store, we cannot place an arbitrary limit on the size of
such a list, and functions that process such cost lists must be prepared to 
consume lists of arbitrary size. 

Suppose the toy store needs a function that computes the value of an
inventory from the cost of the individual toys. We call this function
{{\tt sum}\/}. Before we can define {{\tt sum}\/}, we must figure out how to
describe all possible lists of numbers that the function may consume. In
short, we need a data definition that precisely defines what an arbitrarily
large list of numbers is. We can obtain this definition by replacing
``symbol'' with ``number'' in the definition of lists of symbols:

  \begin{quote} A {\sl {list of numbers}} ({{\tt list-of-numbers}\/}) is either     \begin{enumerate}       \item the empty list, {{\tt empty}\/}, or       \item {{\tt (cons}\ {\tt n}\ {\tt lon)}\/} where {{\tt n}\/} is a number and     {{\tt lon}\/} is a list of numbers.     \end{enumerate} \end{quote}

Given that this data definition is self-referential again, we must first
confirm that it actually defines some lists and that it defines all those
inventories that we wish to represent. All of the examples from above are
lists of numbers. The first one, {{\tt empty}\/}, is included
explicitly. The second and third are {{\tt cons}\/}tructed by adding the
numbers {{\tt 1.22}\/} and {{\tt 2.59}\/}, respectively, to the empty
list. The others are lists of numbers for similar reasons. 

As always, we start the development of the function with a contract, header,
and purpose statement: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sum}\ {\tt :}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the sum of the numbers on {{\tt a-list-of-nums}\/}}
\\
{\tt (define}\  {\tt (sum}\  {\tt a-list-of-nums)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Then we continue with function examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (sum}\  {\tt empty)}\  \\
{\tt =}\  {\tt 0}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (sum}\  {\tt (cons}\  {\tt 1.00}\  {\tt empty))}\\
{\tt =}\  {\tt 1.0}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (sum}\  {\tt (cons}\  {\tt 17.05}\  {\tt (cons}\  {\tt 1.22}\  {\tt (cons}\  {\tt 2.59}\  {\tt empty))))}\\
{\tt =}\  {\tt 20.86}
\end{tabbing}\end{minipage}\end{center}
If {{\tt sum}\/} is applied to {{\tt empty}\/}, the store has no inventory
and the result should be {{\tt 0}\/}. If the input is {{\tt (cons}\ {\tt 1.00}{ }{\tt empty)}\/}, the inventory contains only one toy, and the cost of the toy is
the cost of the inventory. Hence, the result is {{\tt 1.00}\/}. Finally, for
{{\tt (cons}\ {\tt 17.05}\ {\tt (cons}\ {\tt 1.22}\ {\tt (cons}\ {\tt 2.59}\ {\tt empty)))}\/}, {{\tt sum}\/} should
yield $$ 17.05 + 1.22 + 2.59 = 20.86 \ .$$

For the design of {{\tt sum}\/}'s template, we follow the design recipe,
step by step. First, we add the {{\bf cond}-exp\-res\-sion}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (sum}\  {\tt a-list-of-nums)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-nums)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (cons?}\  {\tt a-list-of-nums)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The second clause indicates with a comment that it deals with
{{\tt cons}\/}tructed lists. Second, we add the appropriate selector
expressions for each clause: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (sum}\  {\tt a-list-of-nums)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-nums)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}\={\tt (cons?}\  {\tt a-list-of-nums)}
\\
\>\>\>{\tt ...}\  {\tt (first}\  {\tt a-list-of-nums)}\  {\tt ...}\  {\tt (rest}\  {\tt a-list-of-nums)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Finally, we add the natural recursion of {{\tt sum}\/} that reflects the
self-reference in the data definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (sum}\  {\tt a-list-of-nums)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-nums)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt a-list-of-nums)}\  {\tt ...}\  {\tt (sum}\  {\tt (rest}\  {\tt a-list-of-nums))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The final template reflects almost every aspect of the data definition: the
two clauses, the {{\tt cons}\/}truction in the second clauses, and the
self-reference of the second clauses. The only part of the data definition
that the function template does not reflect is that the first item of a
{{\tt cons}\/}ed input is a number. 

Now that we have a template, let us define the answers for the
{{\bf cond}-exp\-res\-sion} on on a clause-by-clause basis. In the first
clause, the input is {{\tt empty}\/}, which means that the store has no
inventory. We already agreed that in this case the inventory is worth
nothing, which means the corresponding answer is {{\tt 0}\/}. In the second
clause of the template, we find two expressions:
\begin{enumerate}
\item {{\tt (first}\ {\tt a-list-of-nums)}\/}, which extracts the cost of the first
toy; and 

\item {{\tt (sum}\ {\tt (rest}\ {\tt a-list-of-nums))}\/}, which, according to the purpose statement of {{\tt sum}\/},
computes the sum of {{\tt (rest}\ {\tt a-list-of-nums)}\/}. 
\end{enumerate}
From these two reminders of what the expressions already compute for us, 
we see that the expression 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (+}\  {\tt (first}\  {\tt a-list-of-nums)}\  {\tt (sum}\  {\tt (rest}\  {\tt a-list-of-nums)))}\end{tabbing}\end{minipage}\end{center}
computes the answer in the second {{\tt cond}\/}-clause. 

Here is the complete definition of {{\tt sum}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (sum}\  {\tt a-list-of-nums)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-nums)}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (first}\  {\tt a-list-of-nums)}\  {\tt (sum}\  {\tt (rest}\  {\tt a-list-of-nums)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
A comparison of this definition with the template and the data definition
shows that the step from the data definition to the template is the major
step in the function development process. Once we have derived the template
from a solid understanding of the set of possible inputs, we can focus on
the creative part: combining values. For simple examples, this step is
easy; for others, it requires rigorous thinking.

We will see in future sections that this relationship between the shape of
the data definition and the function is not a coincidence. Defining the
class of data that a function consumes always determines to a large extent
the shape of the function.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 9.5.1}

 \label{ex:sum-eval}
Use DrScheme to test the definition of {{\tt sum}\/} on the following sample
lists of numbers: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt empty}\  \\
{\tt (cons}\  {\tt 1.00}\  {\tt empty)}
\\
{\tt (cons}\  {\tt 17.05}\  {\tt (cons}\  {\tt 1.22}\  {\tt (cons}\  {\tt 2.59}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
Compare the results with our specifications. Then apply {{\tt sum}\/} to the
following examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt empty}\\
{\tt (cons}\  {\tt 2.59}\  {\tt empty)}
\\
{\tt (cons}\  {\tt 1.22}\  {\tt (cons}\  {\tt 2.59}\  {\tt empty))}
\end{tabbing}\end{minipage}\end{center}
First determine what the result {\em should\/} be; then use DrScheme to
evaluate the expressions. 
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sum-eval.html}{\rule{3pt}{5pt}}


{\bf Exercise 9.5.2}

 \label{ex:how-manyS} \label{ex:how-manyS-r}
Develop the function {{\tt how-many-symbols}\/}, which consumes a list of
symbols and produces the number of items in the list.

Develop the function {{\tt how-many-numbers}\/}, which counts how many
numbers are in a list of numbers. How do {{\tt how-manyS}\/} and
{{\tt how-manyN}\/} differ?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/how-manyS.html}{\rule{3pt}{5pt}}


{\bf Exercise 9.5.3}

 \label{ex:dollar-store}
Develop the function {{\tt dollar-store\char'077}\/}, which consumes a list of prices
(numbers) and checks whether all of the prices are below {{\tt 1}\/}.  

\noindent Examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (dollar-store\char'077}\  {\tt empty)}\\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (dollar-store\char'077}\  {\tt (cons}\  {\tt .75}\  {\tt (cons}\  {\tt 1.95}\  {\tt (cons}\  {\tt .25}\  {\tt empty))))}\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (dollar-store\char'077}\  {\tt (cons}\  {\tt .75}\  {\tt (cons}\  {\tt 0.95}\  {\tt (cons}\  {\tt .25}\  {\tt empty))))}\\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}

Generalize the function so that it consumes a list of prices (numbers) and
a threshold price (number) and checks that all prices in the list are below
the threshold. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/dollar-store.html}{\rule{3pt}{5pt}}


{\bf Exercise 9.5.4}

 \label{ex:check-range1}
Develop the function {{\tt check-range1}\/}, which consumes a list of
temperature measurements and checks whether all measurements are between
$5^oC$ and $95^oC$. 

Generalize the function to {{\tt check-range}\/}, which consumes a list of
temperature measurements and a legal interval and checks whether all
measurements are within the legal interval.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/check-range1.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 9.5.5}

 \label{ex:guess3} 
Develop the function {{\tt convert}\/}.  It consumes a list of digits and
 produces the corresponding number. The first digit is the least
 significant, and so on.

Also develop the function {{\tt check-guess-for-list}\/}. It implements a
 general version of the number guessing game of exercise~\ref{ex:guess2}.
 The function consumes a list of digits, which represents the player's
 {{\tt guess}\/}, and a number, which represents the randomly chosen and
 hidden number.  Depending on how the {{\tt convert}\/}ed digits relate to
 {{\tt target}\/}, {{\tt check-guess-for-list}\/} produces one of the
 following three answers: {{\tt \char'047}{\tt TooSmall}\/}, {{\tt \char'047}{\tt Perfect}\/}, or
 {{\tt \char'047}{\tt TooLarge}\/}.

The rest of the game is implemented by {\tt\bf guess.ss}. To play the
 game, use the teachpack to {\tt\bf guess.ss} and evaluate the expression
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (repl-for-list}\  {\tt 5}\  {\tt check-guess-for-list)}\end{tabbing}\end{minipage}\end{center}
 {\em after\/} the functions have been thoroughly developed.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/guess3.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 9.5.6}

 \label{ex:delta}
Develop the function {{\tt delta}\/}, which consumes two price lists, that
is, lists of numbers. The first represents the inventory at the beginning
of a time period, the second one the inventory at the end. The function
outputs the difference in value. If the value of the inventory has
increased, the result is positive; if the value has decreased, it is
negative.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/delta.html}{\rule{3pt}{5pt}}


{\bf Exercise 9.5.7}

 \label{ex:average-price}
Define the function {{\tt average-price}\/}. It consumes a list of toy prices
and computes the average price of a toy. The average is the total of all
prices divided by the number of toys. 

\htmladdimg{../icons/teacher.gif}~{\em This exercise is important. It shows that we must think about the design before we plunge in.  One possibility is that students blindly follow the design recipe, without thinking about the concepts that the exercise involves. They should get stuck; some truly adventurous student may even solve it, but the solution will be overly complex.   The better solution is to think ``domain'' knowledge: the average requires us to sum up the numbers and to count them. Then we decide. Two concepts means two auxiliary functions. Up to this point, students may have ignored our advice on auxiliary functions; this exercise drives home the point that auxiliary functions are a good idea. It all goes back to the standard hint of the previous part: ``Remember to develop auxiliary functions when a definition becomes too large or too complex to manage.''}

\medskip\noindent{\bf Refinement}:\  First develop a function that works on non-empty lists.
Then produce a checked function (see section~\ref{sec:input-errors}) that
signals an error when the function is applied to an empty
list.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/average-price.html}{\rule{3pt}{5pt}}


{\bf Exercise 9.5.8}

 \label{ex:draw-list-shapes}
Develop the function {{\tt draw-circles}\/}, which consumes a {{\tt posn}\/}
{{\tt p}\/} and a list of numbers. Each number of the list represents the
radius of some circle. The function draws concentric red circles around
{{\tt p}\/} on a canvas, using the operation {{\tt draw-circle}\/}. Its
result is {{\sf true}\/}, if it can draw all of them; otherwise an error
occurred and the function does not need to produce a value.

Use the teachpack {\tt\bf draw.ss}; create the canvas with {{\tt (start}\ {\tt 300}{ }{\tt 300)}\/}. Recall that {\tt\bf draw.ss} provides the structure definition for
{{\tt posn}\/} (see section~\ref{sec:mixed-data:pred}).~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/draw-list-shapes.html}{\rule{3pt}{5pt}}


\section{More on Processing Lists}

The functions in section~\ref{sec:lists} {\em consume\/} lists that contain
atomic data, especially numbers, symbols, and booleans. But functions must
also be able to {\em produce\/} such lists. Furthermore, they must be able
to consume and produce lists that contain structures. We discuss these
cases in this section, and we continue practicing the use of the design
recipe.

\subsection{Functions that Produce Lists} \label{sec:list-produce} 

Recall the function {{\tt wage}\/} from section~\ref{sec:basics-word-probs}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt wage}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the total wage (at \$12 per hour)}
\\
\hbox{;; of someone who worked for {{\tt h}\/} hours}
\\
{\tt (d}\={\tt efine}\  {\tt (wage}\  {\tt h)}
\\
\>{\tt (*}\  {\tt 12}\  {\tt h))}
\end{tabbing}\end{minipage}\end{center}
The {{\tt wage}\/} function consumes the number of hours some employee worked
and produces the weekly wage payment. For simplicity, we assume that all
employees earn the same hourly rate, namely, \$12. A company, however,
isn't interested in a function like {{\tt wage}\/}, which computes the wage
of a single employee. Instead, it wants a function that computes the wages
for all of its employees, especially if there are a lot of them.

Call this new function {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\/}. It consumes a list that
represents how many hours the employees of the company worked and must
produce a list of the weekly wages they earned. We can represent both the
input and the output as Scheme lists of numbers. Since we already have a
data definition for the inputs and outputs, we can immediately start our
function development:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\ {\tt :}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
\hbox{;; to create a list of weekly wages from a list of weekly hours ({{\tt alon}\/})}
\\
{\tt (define}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt alon)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

Next we need some examples of inputs and the corresponding outputs: 
\begin{quote}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt empty}\\
{\tt (cons}\  {\tt 28}\  {\tt empty)}
\\
{\tt (cons}\  {\tt 40}\  {\tt (cons}\  {\tt 28}\  {\tt empty))}
\end{tabbing}\end{minipage}~\qquad~\begin{minipage}[t]{8in}\begin{tabbing}{\tt empty}\\
{\tt (cons}\  {\tt 336}\  {\tt empty)}
\\
{\tt (cons}\  {\tt 480}\  {\tt (cons}\  {\tt 336}\  {\tt empty))}
\end{tabbing}\end{minipage}
\end{quote}
The outputs are obtained by calculating the wage for each item on the list
to the left. 

Given that {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\/} consumes the same class of data as, say,
the function {{\tt sum}\/}, and given that the shape of a function template
depends only on the shape of the data definition, we can reuse the
{{\tt list-of-numbers}\/} template:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt alon)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt alon)}\  {\tt ...}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt (rest}\  {\tt alon))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

Starting with this template, we can turn to the most creative step of
function development: the definition of the function body. Following our
recipe, we consider each {{\tt cond}\/}-line in isolation, starting with the
simpler case.  First, assume {{\tt (empty?}\ {\tt alon)}\/} is true, which means
that the input is {{\tt empty}\/}. The answer in this case is {{\tt empty}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt alon)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt alon)}\  {\tt ...}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt (rest}\  {\tt alon))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

Second, assume that {{\tt alon}\/} was {{\tt cons}\/}tructed from a number
and a list of numbers.  The expressions in the second line remind us of
this assumption, and the recipe tells us that we should state explicitly
what they compute:
\begin{enumerate}
\item {{\tt (first}\ {\tt alon)}\/} yields the first number on {{\tt alon}\/}, which
is the first number of hours worked; and 

\item {{\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\ {\tt (rest}\ {\tt alon))}\/} reminds us that {{\tt (rest}{ }{\tt alon)}\/} is a list and can be processed by the very function we are
defining. According to the purpose statement, the expression computes the
list of wages for the rest of the list of hours, and we may assume this
relationship in our construction---even though the function is not yet
completely defined.  \end{enumerate}

From here it is a short step to the complete function definition.  Since we
already have the list of wages for all but the first item of {{\tt alon}\/},
the function must do two things to produce an output for the {\em entire\/}
list of hours:
\begin{enumerate}
\item Compute the weekly wage for the first number of hours. 

\item Construct a list that represents all weekly wages for {{\tt alon}\/},
using the first weekly wage and the list of weekly wages for {{\tt (rest}{ }{\tt alon)}\/}. 
\end{enumerate}
For the first part, we reuse {{\tt wage}\/}. For the second, we
{{\tt cons}\/} the two pieces of information together into one list:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  {\tt (wage}\  {\tt (first}\  {\tt alon))}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt (rest}\  {\tt alon)))}\end{tabbing}\end{minipage}\end{center}
And with that, we have a complete function, as shown in
figure~\ref{fig:hours->wages}; to finish the development, we must still
test the function.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\ {\tt :}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
\hbox{;; to create a list of weekly wages from a list of weekly hours ({{\tt alon}\/})}
\\
{\tt (d}\={\tt efine}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt (wage}\  {\tt (first}\  {\tt alon))}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\\
\hbox{;; {{\tt wage}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to compute the total wage (at \$12 per hour)}
\\
\hbox{;; of someone who worked for {{\tt h}\/} hours}
\\
{\tt (d}\={\tt efine}\  {\tt (wage}\  {\tt h)}
\\
\>{\tt (*}\  {\tt 12}\  {\tt h))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Computing weekly wages}
\label{fig:hours->wages}
\hrule

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 10.1.1}

 \label{ex:raises}
How do we have to change the function in figure~\ref{fig:hours->wages} if we
want to give everyone a raise to \$14?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/raises.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.1.2}

 \label{ex:fraud}
No employee could possibly work more than 100 hours per week. To protect
the company against fraud, the function should check that no item of the
input list of {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\/} exceeds 100. If one of them does, the
function should immediately signal the error {{\tt ``too}\ {\tt many}\ {\tt hours''}\/}.

How do we have to change the function in figure~\ref{fig:hours->wages} if we
want to perform this basic reality check?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/fraud.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.1.3}

 \label{ex:convertFC}
\htmladdimg{../icons/teacher.gif}~{\em For this and some of the following exercises, ask students to show the products of the various steps in the design recipe. Diagnose students weaknesses! Up to now, design recipes were easy to apply; but unless students have practiced them consistently, they will fail now.}

Develop {{\tt convertFC}\/}.  The function converts a list of Fahrenheit
measurements to a list of Celsius measurements. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/convertFC.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.1.4}

 \label{ex:convertDM} \label{ex:convertDM-r}
Develop the function {{\tt convertDM}\/}, which converts a list of US Dollar
amounts into a list of Deutsche Mark amounts. Assume the exchange rate
is 1.82 marks for each dollar. 

Generalize {{\tt convertDM}\/} to the function {{\tt convertDM1}\/}, which
consumes an exchange rate and a list of dollar amounts and converts the
latter into a list of Deutsche Mark accounts. 
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/convertDM.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.1.5}

 \label{ex:elim-expensive}
Develop the function {{\tt eliminate-exp}\/} to eliminate expensive toys. The
function consumes a number, called {{\tt ua}\/}, and a list of toy prices,
called {{\tt lotp}\/}, and produces a list of all those prices in
{{\tt lotp}\/} that are below {{\tt ua}\/}. For example,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (eliminate-exp}\  {\tt 1.0}\  {\tt (cons}\  {\tt 2.95}\  {\tt (cons}\  {\tt .95}\  {\tt (cons}\  {\tt 1.0}\  {\tt (cons}\  {\tt 5}\  {\tt empty)))))}\\
{\tt =}\  {\tt (cons}\  {\tt .95}\  {\tt (cons}\  {\tt 1.0}\  {\tt empty))}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/elim-expensive.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.1.6}

 \label{ex:subst}
Develop the function {{\tt name-robot}\/}, which consumes a list of toy
descriptions (names) and produces an equivalent list of more accurate
descriptions. Specifically, it replaces all occurrences of {{\tt \char'047}{\tt robot}\/}
with {{\tt \char'047}{\tt r2d3}\/} and otherwise retains the toy descriptions in the same
order.

Generalize {{\tt name-robot}\/} to the function {{\tt substitute}\/}. The new
function consumes two symbols, called {{\tt new}\/} and {{\tt old}\/}, and a
list of symbols. It produces a new list of symbols by substituting all
occurrences of {{\tt old}\/} by {{\tt new}\/}. For example, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (substitute}\  {\tt \char'047}{\tt Barbie}\  {\tt \char'047}{\tt doll}\  {\tt (cons}\  {\tt \char'047}{\tt robot}\  {\tt (cons}\  {\tt \char'047}{\tt doll}\  {\tt (cons}\  {\tt \char'047}{\tt dress}\  {\tt empty))))}\\
{\tt =}\  {\tt (cons}\  {\tt \char'047}{\tt robot}\  {\tt (cons}\  {\tt \char'047}{\tt Barbie}\  {\tt (cons}\  {\tt \char'047}{\tt dress}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/subst.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.1.7}

 \label{ex:recall}
Develop the function {{\tt recall}\/} to eliminate specific toys from a list.
The function consumes the name of a toy, called {{\tt ty}\/}, and a list of
names, called {{\tt lon}\/}, and produces a list of names that contains all
components of {{\tt lon}\/} with the exception of {{\tt ty}\/}.  For example,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (recall}\  {\tt \char'047}{\tt robot}\  {\tt (cons}\  {\tt \char'047}{\tt robot}\  {\tt (cons}\  {\tt \char'047}{\tt doll}\  {\tt (cons}\  {\tt \char'047}{\tt dress}\  {\tt empty))))}\\
{\tt =}\  {\tt (cons}\  {\tt \char'047}{\tt doll}\  {\tt (cons}\  {\tt \char'047}{\tt dress}\  {\tt empty))}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/recall.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.1.8}

 \label{ex:quad3} \htmlref{\htmladdimg{../icons/plt.gif}}{drn:ex:quad3}
Develop {{\tt quadratic-roots}\/}, which solves quadratic equations: see
exercises~\ref{ex:quad1} and~\ref{ex:quad2}. The function accepts the
coefficients {{\tt a}\/}, {{\tt b}\/}, and {{\tt c}\/}. The computations it
performs depend on the input:
\begin{enumerate}
\item if $a = 0$, its output is {{\tt \char'047}{\tt degenerate}\/}.

\item if $b^2 < 4 \cdot a \cdot c$, the quadratic equation has no solution;
{{\tt quadratic-roots}\/} produces {{\tt \char'047}{\tt none}\/} in this case.  

\item if $b^2 = 4 \cdot a \cdot c$, the equation has one solution:
$$ \frac{-b}{2 \cdot a} \ ;$$
the solution is the answer. 

\item if $b^2 > 4 \cdot a \cdot c$, the equation has two solutions:
$$ \frac{-b+\sqrt{b^2 - 4 \cdot a \cdot c}}{2 \cdot a} $$
and 
$$ \frac{-b-\sqrt{b^2 - 4 \cdot a \cdot c}}{2 \cdot a}\ ; $$
the result is a list of two numbers: the first solution followed by the
second solution.
\end{enumerate}
Test the function with the examples from exercises~\ref{ex:quad1}
and~\ref{ex:quad2}. First decide the answer for each example, then
determine it with DrScheme.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/quad3.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.1.9}

 \label{ex:speak}
The cash registers at many grocery stores talk to customers. The
register's computer receives the number of cents that the customer must pay
and then builds a list with the following five items:
\begin{enumerate}
\item the dollar amount;
\item the symbol {{\tt \char'047}{\tt dollar}\/} if the dollar amount is {{\tt 1}\/} 
and {{\tt \char'047}{\tt dollars}\/} otherwise; 
\item the symbol {{\tt \char'047}{\tt and}\/}
\item the cent amount; and
\item the symbol {{\tt \char'047}{\tt cent}\/} if the cent amount is 1 
and {{\tt \char'047}{\tt cents}\/} otherwise.
\end{enumerate}

Develop the function {{\tt controller}\/}, which consumes a number and
produces a list according to the above description.  For example, if the
amount is \$1.03, then the cash register evaluates \relax
{{\tt (controller}\ {\tt 103)}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (controller}\  {\tt 103)}\\
{\tt =}\  {\tt (cons}\  {\tt 1}\  {\tt (cons}\  {\tt \char'047}{\tt dollar}\  {\tt (cons}\  {\tt \char'047}{\tt and}\  {\tt (cons}\  {\tt 3}\  {\tt (cons}\  {\tt \char'047}{\tt cents}\  {\tt empty)))))}
\end{tabbing}\end{minipage}\end{center}

\noindent {\bf Hint}: Scheme provides the arithmetic operations
{{\tt quotient}\/} and {{\tt remainder}\/}, which produce the quotient and
remainder of the expression $n/m$ for integers {\it n\/} and {\it m\/},
respectively.

\htmladdimg{../icons/teacher.gif}~{\em The sound teachpack is available on Unix workstations only.}

Once the controller returns the correct list for amounts whose dollar and
cent amounts are between 0 and 20, test the controller with a computer that
can speak. Set the teachpack to {\tt\bf sound.ss}, which makes two
operations available: {{\tt speak-word}\/} and {{\tt speak-list}\/}.  The
first accepts a symbol or a number, the second a list of symbols and
numbers. Both pronounce the symbols they consume. Evaluate the following
expressions {{\tt (speak-word}\ {\tt 1)}\/}, {{\tt (speak-list}\ {\tt (cons}\ {\tt 1}\ {\tt (cons}{ }{\tt \char'047}{\tt dollar}\ {\tt empty)))}\/}, and {{\tt (speak-list}\ {\tt (cons}\ {\tt \char'047}{\tt beautiful}\ {\tt (cons}\ {\tt \char'047}{\tt lady}{ }{\tt empty)))}\/} to understand how the operations operate.

\medskip\noindent {\bf Simple Challenge}: The sound teachpack only contains
the sounds for the numbers {{\tt 0}\/} through {{\tt 20}\/} and {{\tt 30}\/},
{{\tt 40}\/}, {{\tt 50}\/}, {{\tt 60}\/}, {{\tt 70}\/}, {{\tt 80}\/}, and
{{\tt 90}\/}. Because of this restriction, the bonus problem only works on
amounts with cents and dollars between {{\tt 0}\/} to {{\tt 20}\/}. Implement
a controller that deals with arbitrary amounts between 0 and 99.99. 
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/speak.html}{\rule{3pt}{5pt}}


\subsection{Lists that Contain Structures} \label{sec:list-in-list} 

The representation of an inventory as a list of symbols or a list of prices
is naive. A sales clerk in a toy store not only needs to know the name of
the toy, but also its price, and possibly other attributes like warehouse
availability, delivery time, or even a picture. Similarly, representing a
company's personnel as a list of hours is a bad choice. Even the printing
of a paycheck requires more information about the employee than the hours
worked per week.

Fortunately, the items of lists do not have to be atomic values. Lists may
contain whatever values we want, especially structures.  Let's try to make
our toy store inventory functions more realistic. We start with the
structure and the data definition of a class of inventory records:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt ir}\  {\tt (name}\  {\tt price))}\end{tabbing}\end{minipage}\end{center}

   \begin{quote} An {\sl {inventory record}} ({{\tt ir}\/}) is a structure: \\
         \centerline{ {{\tt (make-ir}\ {\tt s}\ {\tt n)}\/} } \\
        where {{\tt s}\/} is a symbol and {{\tt n}\/} is a (positive) number. \end{quote}

\noindent Most importantly, we can define a class of lists that represent
an inventory much more realistically:

   \begin{quote} An {\sl {inventory}} is either       \begin{enumerate}        \item {{\tt empty}\/} or        \item {{\tt (cons}\ {\tt ir}\ {\tt inv)}\/} \\
          where {{\tt ir}\/} is an inventory record and {{\tt inv}\/} is an inventory.       \end{enumerate} \end{quote}

While the shape of the list definition is the same as before, its
components are defined in a separate data definition. Since this is our
first such data definition, we should make up some examples before we
proceed.

The simplest example of an inventory is {{\tt empty}\/}. To
create a larger inventory, we must create an inventory record and
{{\tt cons}\/} it onto another inventory:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ons}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 17.95)}\\
\>{\tt empty)}
\end{tabbing}\end{minipage}\end{center}
From here, we can create yet a larger inventory listing: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ons}\  {\tt (make-ir}\  {\tt \char'047}{\tt robot}\  {\tt 22.05)}\\
\>{\tt (c}\={\tt ons}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 17.95)}
\\
\>\>{\tt empty))}
\end{tabbing}\end{minipage}\end{center}

Now we can adapt our inventory-processing functions.  First look at
{{\tt sum}\/}, the function that consumes an inventory and produces its total
value. Here is a re-statement of the basic information about the function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sum}\ {\tt :}\ {\tt inventory}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the sum of prices on {{\tt an-inv}\/}}
\\
{\tt (define}\  {\tt (sum}\  {\tt an-inv)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
For our three sample inventories, the function should produce the following
results: {{\tt 0}\/}, {{\tt 17.95}\/}, and {{\tt 40.0}\/}. 

Since the data definition of inventories is basically that of lists, we can
again start from the template for list-processing functions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (sum}\  {\tt an-inv)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt an-inv)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt an-inv)}\  {\tt ...}\  {\tt (sum}\  {\tt (rest}\  {\tt an-inv))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Following our recipe, the template only reflects the data definition of the
input, not that of its constituents. Therefore the template for {{\tt sum}\/}
here is indistinguishable from that in section~\ref{sec:lists:more}. 

For the definition of the function body, we consider each {{\tt cond}\/}-line
in isolation. First, if {{\tt (empty?}\ {\tt an-inv)}\/} is true, {{\tt sum}\/} is
supposed to produce {{\tt 0}\/}. Hence, the answer expression in the first
{{\tt cond}\/}-line is obviously {{\tt 0}\/}.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (sum}\  {\tt an-inv)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt an-inv)}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (ir-price}\  {\tt (first}\  {\tt an-inv))}\  {\tt (sum}\  {\tt (rest}\  {\tt an-inv)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Computing the value of an inventory}
\label{fig:sum-ir}
\hrule

Second, if {{\tt (empty?}\ {\tt an-inv)}\/} is false, {\it i.e.}, {{\tt sum}\/} is applied
to a {{\tt cons}\/}tructed inventory, the recipe requires us to understand
the purpose of two expressions:
\begin{enumerate}
\item {{\tt (first}\ {\tt an-inv)}\/}, which extracts the first item of the list; and

\item {{\tt (sum}\ {\tt (rest}\ {\tt an-inv))}\/}, which extracts the rest of
{{\tt an-inv}\/} and then computes its cost with {{\tt sum}\/}.
\end{enumerate}
To compute the total cost of the entire input {{\tt an-inv}\/} in the second
case, we must determine the cost of the first item. The cost of
the first item may be obtained via the selector {{\tt ir-price}\/}, which
extracts the price from an inventory record. Now we just add the cost of
the first item and the cost of the rest of the inventory:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (+}\  \={\tt (ir-price}\  {\tt (first}\  {\tt an-inv))}\\
\>{\tt (sum}\  {\tt (rest}\  {\tt an-inv)))}
\end{tabbing}\end{minipage}\end{center}
The complete function definition is contained in figure~\ref{fig:sum-ir}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 10.2.1}

 \label{ex:contains-dollp}
Adapt the function {{\tt contains-doll\char'077}\/} so that it consumes inventories
instead of lists of symbols: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt contains-doll\char'077}\ {\tt :}\ {\tt inventory}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt an-inv}\/} contains a record for {{\tt \char'047}{\tt doll}\/}}
\\
{\tt (define}\  {\tt (contains-doll\char'077}\  {\tt an-inv)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

Also adapt the function {{\tt contains\char'077}\/}, which consumes a symbol and an
inventory and determines whether an inventory record with this symbol
occurs in the inventory: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt contains\char'077}\ {\tt :}\ {\tt symbol}\ {\tt inventory}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt inventory}\/} contains a record for {{\tt asymbol}\/}}
\\
{\tt (define}\  {\tt (contains\char'077}\  {\tt asymbol}\  {\tt an-inv)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/contains-dollp.html}{\rule{3pt}{5pt}}


\hrule
  \begin{rawhtml}  <center>  <table border=1 bgcolor=beige>  <tr>    <td align=center valign=bottom>Item</td>    <td align=center valign=bottom>Price</td>     <td align=center valign=bottom>Image</td>  </tr>   <tr>    <td align=center valign=bottom>robot</td>    <td align=center valign=bottom>29.95</td>     <td align=center valign=bottom><img src=../icons/robot.gif></td>  </tr>   <tr>    <td align=center valign=bottom>robot</td>    <td align=center valign=bottom>29.95</td>     <td align=center valign=bottom><img src=../icons/doll.gif></td>  </tr>   <tr>    <td align=center valign=bottom>robot</td>    <td align=center valign=bottom>29.95</td>     <td align=center valign=bottom><img src=../icons/rocket.gif></td>  </tr>  </table>  </center>  \end{rawhtml}
\center{Figure: A table of toys} \label{fig:toytable}
\hrule

{\bf Exercise 10.2.2}

 \label{ex:show-picture}
Provide a data definition and a structure definition for an inventory that
includes pictures with each objects. Show how to represent the inventory
listing in figure~\ref{fig:toytable}.\footnote{Thanks to Mr.\ John Greiner, who drew these figures.}

Develop the function {{\tt show-picture}\/}. The function consumes a symbol,
the name of a toy, and one of the new inventories. It produces the picture
of the named toy or {{\sf false}\/} if the desired item is not in the
inventory. Pictures of toys are available on the Web.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/show-picture.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.2.3}

 \label{ex:price-of}
Develop the function {{\tt price-of}\/}, which consumes the name of a toy and
an inventory and produces the toy's price. 
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/price-of.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.2.4}

 \label{ex:phone-directory}
A phone directory combines names with phone numbers. Develop a data
definition for phone records and directories. Using this data definition
develop the functions 
\begin{enumerate}
\item {{\tt whose-number}\/}, which determines the name that goes with some
given phone number and phone directory, and

\item {{\tt phone-number}\/}, which determines the phone number that goes
with some given name and phone directory. 
\end{enumerate}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/phone-directory.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


\bigskip

Suppose a business wishes to separate all those items that sell for a
dollar or less from all others. The goal might be to sell these items in a
separate department of the store. To perform this split, the business also
needs a function that can extract these items from its inventory listing,
{\it i.e.}, a function that produces a list of structures. 

Let us name the function {{\tt extract1}\/} because it creates an inventory
from all those inventory records whose price item is less than or equal to
{{\tt 1.00}\/}. The function consumes an inventory and produces one with
items of appropriate prices. Thus the contract for {{\tt extract1}\/} is
easy to formulate:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt extract1}\ {\tt :}\ {\tt inventory}\ {}{\tt ->}{}\ {\tt inventory}\/}}\\
\hbox{;; to create an {{\tt inventory}\/} from {{\tt an-inv}\/} for all}
\\
\hbox{;; those items that cost less than \$1}
\\
{\tt (define}\  {\tt (extract1}\  {\tt an-inv)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

We can reuse our old inventory examples to make examples of
{{\tt extract1}\/}'s input-output relationship. Unfortunately, it must
produce the empty inventory, for the three examples that we used above
because all prices are above one dollar. For a more interesting
input-output example, we need an inventory with more variety:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  \={\tt (make-ir}\  {\tt \char'047}{\tt dagger}\  {\tt .95)}\\
\>{\tt (cons}\  \={\tt (make-ir}\  {\tt \char'047}{\tt Barbie}\  {\tt 17.95)}
\\
\>\>{\tt (cons}\  \={\tt (make-ir}\  {\tt \char'047}{\tt key-chain}\  {\tt .55)}
\\
\>\>\>{\tt (cons}\  \={\tt (make-ir}\  {\tt \char'047}{\tt robot}\  {\tt 22.05)}
\\
\>\>\>\>{\tt empty))))}
\end{tabbing}\end{minipage}\end{center}
Out of the four items in this new inventory, two have prices below one
dollar. If given to {{\tt extract1}\/}, we should get the result 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  \={\tt (make-ir}\  {\tt \char'047}{\tt dagger}\  {\tt .95)}\\
\>{\tt (cons}\  \={\tt (make-ir}\  {\tt \char'047}{\tt key-chain}\  {\tt .55)}
\\
\>\>{\tt empty))}
\end{tabbing}\end{minipage}\end{center}
The new listing enumerates the items in the same order as the original, but
only contains those items whose prices match our condition. 

The contract also implies that the template for {{\tt extract1}\/} is identical
to that of {{\tt sum}\/}, except for a name change: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (extract1}\  {\tt an-inv)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt an-inv)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt an-inv)}\  {\tt ...}\  {\tt (extract1}\  {\tt (rest}\  {\tt an-inv))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
As always, the difference in outputs between {{\tt sum}\/} and
{{\tt extract1}\/} does not affect the template derivation.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt extract1}\ {\tt :}\ {\tt inventory}\ {}{\tt ->}{}\ {\tt inventory}\/}}\\
\hbox{;; to create an {{\tt inventory}\/} from {{\tt an-inv}\/} for all}
\\
\hbox{;; those items that cost less than \$1}
\\
{\tt (d}\={\tt efine}\  {\tt (extract1}\  {\tt an-inv)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt an-inv)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (<=}\  {\tt (ir-price}\  {\tt (first}\  {\tt an-inv))}\  {\tt 1.00)}
\\
\>\>\>\>{\tt (cons}\  {\tt (first}\  {\tt an-inv)}\  {\tt (extract1}\  {\tt (rest}\  {\tt an-inv)))\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (extract1}\  {\tt (rest}\  {\tt an-inv))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Extracting dollar items from an inventory}
\label{fig:extract1}
\hrule


For the definition of the function body, we again analyze each case
separately. First, if {{\tt (empty?}\ {\tt an-inv)}\/} is true, then the answer is
clearly {{\tt empty}\/}, because no item in an empty store costs less than
one dollar.  Second, if the inventory is not empty, we first determine what
the expressions in the matching {{\tt cond}\/}-clause compute. Since
{{\tt extract1}\/} is the first recursive function to produce a list of
structures, let us look at our interesting example:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  \={\tt (make-ir}\  {\tt \char'047}{\tt dagger}\  {\tt .95)}\\
\>{\tt (cons}\  \={\tt (make-ir}\  {\tt \char'047}{\tt Barbie}\  {\tt 17.95)}
\\
\>\>{\tt (cons}\  \={\tt (make-ir}\  {\tt \char'047}{\tt key-chain}\  {\tt .55)}
\\
\>\>\>{\tt (cons}\  \={\tt (make-ir}\  {\tt \char'047}{\tt robot}\  {\tt 22.05)}
\\
\>\>\>\>{\tt empty))))}
\end{tabbing}\end{minipage}\end{center}
If {{\tt an-inv}\/} stands for this inventory, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (first}\  {\tt an-inv)}\  {\tt =}\  {\tt (make-ir}\  {\tt \char'047}{\tt dagger}\  {\tt .95)}\\
{\tt (rest}\  {\tt an-inv)}\  {\tt =}\  {\tt (cons}\=\  {\tt (make-ir}\  {\tt \char'047}{\tt Barbie}\  {\tt 17.95)}
\\
\>{\tt (cons}\  \={\tt (make-ir}\  {\tt \char'047}{\tt key-chain}\  {\tt .55)}
\\
\>\>{\tt (c}\={\tt ons}\  {\tt (make-ir}\  {\tt \char'047}{\tt robot}\  {\tt 22.05)}
\\
\>\>\>{\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
Assuming {{\tt extract1}\/} works correctly, we also know that 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (extract1}\  {\tt (rest}\  {\tt an-inv))}\  {\tt =}\  {\tt (c}\={\tt ons}\  {\tt (make-ir}\  {\tt \char'047}{\tt key-chain}\  {\tt .55)}\\
\>{\tt empty)}
\end{tabbing}\end{minipage}\end{center}
In other words, the recursive application of {{\tt extract1}\/} produces the
appropriate selection from the rest of {{\tt an-inv}\/}, which is a list
with a single inventory record.

To produce an appropriate inventory for all of {{\tt an-inv}\/}, we must
decide what to do with the first item. Its price may be more or less than
one dollar, which suggest the following template for the second answer:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt ...}\  {\tt (c}\={\tt ond}\  \\
\>{\tt \char'133}{\tt (<=}\  {\tt (ir-price}\  {\tt (first}\  {\tt an-inv))}\  {\tt 1.00)}\  {\tt ...\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt ...\char'135}{\tt )}\  {\tt ...}
\end{tabbing}\end{minipage}\end{center}
If the first item's price is one dollar or less, it must be included in the
final output and, according to our example, should be the first item on the
output. Translated into Scheme, the output should be a list whose first
item is {{\tt (first}\ {\tt an-inv)}\/} and whose rest is whatever the recursion
produces. If the price is more than one dollar, the item should not be
included. That is, the result should be whatever the recursion produces for
the {{\tt rest}\/} of {{\tt an-inv}\/} and nothing else. The complete
definition is displayed in figure~\ref{fig:extract1}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 10.2.5}

 \label{ex:extract2}
Define the function {{\tt extract>1}\/}, which consumes an inventory and
creates an inventory from those records whose prices are above one
dollar.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/extract2.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.2.6}

 \label{ex:inventory1}
Develop a precise data definition for inventory1, which are inventory
listings of one-dollar stores.  Using the new data definition, the contract
for {{\tt extract1}\/} can be refined: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt extract1}\ {\tt :}\ {\tt inventory}\ {}{\tt ->}{}\ {\tt inventory1}\/}}\\
{\tt (define}\  {\tt (extract1}\  {\tt an-inv)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Does the refined contract affect the development of the function above? 
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/inventory1.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.2.7}

 \label{ex:raise-prices}
Develop the function {{\tt raise-prices}\/}, which consumes an inventory and
produces an inventory in which all prices are raised by 5\%. 
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/raise-prices.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.2.8}

 \label{ex:recall2}
Adapt the function {{\tt recall}\/} from exercise~\ref{ex:recall} for the new
data definition of inventory. The function consumes the name of a toy
{{\tt ty}\/} and an inventory and produces an inventory that contains all
items of the input with the exception of those labeled {{\tt ty}\/}.
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/recall2.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.2.9}

 \label{ex:name-robot}
Adapt the function {{\tt name-robot}\/} from exercise~\ref{ex:subst} for the
new data definition of inventory. The function consumes an inventory and
produces an inventory with more accurate names. Specifically, it replaces
all occurrences of {{\tt \char'047}{\tt robot}\/} with {{\tt \char'047}{\tt r2d3}\/}.

Generalize {{\tt name-robot}\/} to the function {{\tt substitute}\/}. The new
function consumes two symbols, called {{\tt new}\/} and {{\tt old}\/}, and an
inventory. It produces a new inventory by substituting all occurrences of
{{\tt old}\/} by {{\tt new}\/} and leaving all others alone.
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/name-robot.html}{\rule{3pt}{5pt}}


\subsection{Extended Exercise: Moving Pictures} \label{sec:move-fig} 

In sections~\ref{sec:move-circle} and~\ref{sec:move-shape}, we studied how to
move individual shapes. A picture, however, isn't just a single shape but a
whole collection of them. Considering that we have to draw, translate,
clear pictures, and that we may wish to change a picture or manage several
pictures at the same time, it is best to collect all of the parts of a
picture into a single piece of data. Because pictures may consist of a
varying number of items, a list representation for pictures suggests itself
naturally. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 10.3.1}

 \label{ex:moving-list-dat} 
Provide a data definition that describes the class of lists of {{\tt shape}\/}s.
The class of {{\tt shape}\/}s was defined in exercise~\ref{ex:moving-fig-dat}.

Create a sample list that represents the face of figure~\ref{ex:moving}
and name it {{\tt FACE}\/}. Its basic dimensions are gathered in the
following table:
\begin{center}
\begin{tabular}{lccl}
shape     & position & size(s)         & color \\
 \hline
circle    & (50,50)  &  40             & red \\
rectangle & (30,20)  &  $5 \times 5$   & blue \\
rectangle & (65,20)  &  $5 \times 5$   & blue \\
rectangle & (40,75)  &  $20 \times 10$ & red \\
rectangle & (45,35)  &  $10 \times 30$ & blue \\
\end{tabular}
\end{center}
The table assumes a canvas of size 300 by 100.

Develop the template {{\tt fun-for-losh}\/}, which outlines functions
that consume a {{\tt list-of-shapes}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-list-dat.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.3.2}

 \label{ex:moving-list-dr} 
Use the template {{\tt fun-for-losh}\/} to develop the function
{{\tt draw-losh}\/}. It consumes a {{\tt list-of-shapes}\/}, draws each item
on the list, and returns {{\sf true}\/}. Recall to use {{\tt (start}\ {\tt n}\ {\tt m)}\/} to
create the canvas before the function is
used.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-list-dr.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.3.3}

 \label{ex:moving-list-tr} 
Use the template {{\tt fun-for-losh}\/} to develop {{\tt translate-losh}\/}.
The function consumes a {{\tt list-of-shapes}\/} and a number
{{\tt delta}\/}. The result is a list of shapes where each of them has been
moved by {{\tt delta}\/} pixels in the
$x$-direction.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-list-tr.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.3.4}

 \label{ex:moving-list-cl} 
Use the template  {{\tt fun-for-losh}\/} to develop {{\tt clear-losh}\/}.  The
function consumes a {{\tt list-of-shapes}\/}, erases each item on the list from
the canvas, and returns {{\sf true}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-list-cl.html}{\rule{3pt}{5pt}}


{\bf Exercise 10.3.5}

 \label{ex:moving} \label{ex:moving-r} 
Develop the function {{\tt move-picture}\/}.  It consumes a number
({{\tt delta}\/}) and a {{\tt picture}\/} and translates it by {{\tt delta}\/}
pixels on a canvas. The function should wait for one second between drawing
and clearing the new picture. The result of the function is the translated
picture.

Test the function with expressions like these:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (start}\  {\tt 500}\  {\tt 100)}\\
{\tt (d}\={\tt raw-losh}
\\
\>{\tt (m}\={\tt ove-picture}\  {\tt -5}\  
\\
\>\>{\tt (m}\={\tt ove-picture}\  {\tt 23}
\\
\>\>\>{\tt (move-picture}\  {\tt 10}\  {\tt FACE))))}
\\
{\tt (stop)}\  
\end{tabbing}\end{minipage}\end{center} 
This moves {{\tt FACE}\/} (see exercise~\ref{ex:moving-list-dat}) by
{{\tt 10}\/}, {{\tt 23}\/}, and {{\tt -5}\/} pixels in the
$x$-direction.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt} 

When the function is fully tested, use the teachpack {\tt\bf arrow.ss} and
evaluate 
the expression:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (start}\  {\tt 500}\  {\tt 100)}\\
{\tt (control-left-right}\  {\tt FACE}\  {\tt 100}\  {\tt move}\  {\tt draw-losh)}
\end{tabbing}\end{minipage}\end{center}
The last one creates a graphical user interface that permits users to move the
shape {{\tt FACE}\/} by clicking on arrows. The shape then moves in increments of
{{\tt 100}\/} (right) and {{\tt -100}\/} (left) pixels. The teachpack also provides 
arrow controls for other directions. Use them to develop other moving pictures. 

\section{Natural Numbers} 
\label{sec:nat-num} 

\htmladdimg{../icons/teacher.gif}~{\em Natural numbers and recursion/looping over natural numbers play a  very limited role in practical programming once we understand how to  function with data structures directly rather than as array encodings.  It  therefore doesn't make much sense to spend a lot of time on natural  numbers. {\bf Recommendation}: Cover the first two sections to illustrate  an unusual example of a recursive data definition. Suggest to your  students to do the extended exercise on color grids on their own, because  it helps them with standardized math tests.}

The only self-referential data definitions we have seen thus far involved
{{\tt cons}\/} and lists of arbitrary length. We needed such data
definitions because the classes of lists that we wanted to process were of
of arbitrary size. Natural numbers are another class of data whose elements
are of arbitrary size; after all, there is no limit on how large a natural
number can be and, at least in principle, a function should be able to
process them all.

In this section, we study how to describe natural numbers with
self-referential data definitions and how to develop functions that process
natural numbers in a systematic fashion. Since such functions come in many
different flavors, we study several different flavors definitions. 

\subsection{Defining Natural Numbers} \label{sec:nat-num-def}

People normally introduce natural numbers via enumeration: {{\tt 0}\/},
{{\tt 1}\/}, {{\tt 2}\/}, {\em etc\/}.\footnote{It is important to start counting at 0 so that we can use the natural numbers for counting the number of items on a list or the members of a family tree.} The
abbreviation ``etc.'' at the end says that the series continues in this
manner. Mathematicians and mathematics teachers often use dots for the same
purpose. For us, however, neither the ``etc.'' nor the dots are good
enough, if we wish to design functions on natural numbers
systematically. So, the question is what it means to write down ``etc.'',
or put differently, what a complete, self-contained description of the
natural numbers is.

The only way to remove the informal ``etc.'' from the enumeration is to
describe the collection of numbers with a self-referential
description. Here is a first attempt:
\begin{quote}
0 is a natural number. \\
 
If $n$ is a natural number, then one more than $n$ is one, too.
\end{quote}
While this description is still not quite rigorous,\footnote{For that, we need to defer to a course on mathematical sets.} it is a good starting
point for a Scheme-style data description:

  \begin{quote} A {\sl {natural number}} ({{\bf N}\/}) is either        \begin{enumerate}         \item {{\tt 0}\/} or          \item {{\tt (add1}\ {\tt n)}\/} if {{\tt n}\/} is a natural number.       \end{enumerate} \end{quote}

\noindent The operation {{\tt add1}\/} adds {{\tt 1}\/} to a natural
number. Of course, we could use {{\tt (+}\ {\tt ...}\ {\tt 1)}\/} but {{\tt add1}\/}
stands out and signals ``natural number,'' as opposed to arbitrary number,
to the reader of a data definition and a function. 

Although we are familiar with natural numbers from school, it is
instructive to construct examples from the data definition. Clearly,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt 0}\end{tabbing}\end{minipage}\end{center} 
is the first natural number, so 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (add1}\  {\tt 0)}\end{tabbing}\end{minipage}\end{center} 
is the next one. From here, it is easy to see the pattern: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (add1}\  {\tt (add1}\  {\tt 0))}\\
{\tt (add1}\  {\tt (add1}\  {\tt (add1}\  {\tt 0)))}
\\
{\tt (add1}\  {\tt (add1}\  {\tt (add1}\  {\tt (add1}\  {\tt 0))))}
\end{tabbing}\end{minipage}\end{center} 
The examples should remind us of the lists construction process. We built
lists by starting with {{\tt empty}\/} and by {{\tt cons}\/}ing on more
items. Now we build natural natural numbers by starting with {{\tt 0}\/} and
by {{\tt add}\/}ing on {{\tt 1}\/}. In addition, natural numbers come with
century-old abbreviations. For example, {{\tt (add1}\ {\tt 0)}\/} is abbreviated as
{{\tt 1}\/}, {{\tt (add1}\ {\tt (add1}\ {\tt 0))}\/} as {{\tt 2}\/}, and so on. 

A function on natural numbers must extract the number that went into
positive natural number just like a function on lists must extract the list
that went into a {{\tt cons}\/}tructed list. The operation that performs
this ``extraction'' is called {{\tt sub1}\/}. It satisfies the law
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (sub1}\  {\tt (add1}\  {\tt n))}\  {\tt =}\  {\tt n}\  \end{tabbing}\end{minipage}\end{center}
just as the {{\tt rest}\/} operation satisfies the law 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (rest}\  {\tt (cons}\  {\tt a-value}\  {\tt a-list))}\  {\tt =}\  {\tt a-list}\end{tabbing}\end{minipage}\end{center}
Of course, {{\tt (-}\ {\tt n}\ {\tt 1)}\/} would also work, but {{\tt sub1}\/} stands out
and signals that the function processes natural numbers.

\subsection{Processing Natural Numbers of Arbitrary Size} \label{sec:procN}

Let us develop the function {{\tt hellos}\/}. It consumes a natural number
{{\tt n}\/} and produces a list of {{\tt n}\/} copies of {{\tt \char'047}{\tt hello}\/}. We
can write the contract for this function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt hellos}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt list-of-symbols}\/}}\\
\hbox{;; to create a list of {{\tt n}\/} copies of {{\tt \char'047}{\tt hello}\/}}
\\
{\tt (define}\  {\tt (hellos}\  {\tt n)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
And we can make up examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (hellos}\  {\tt 0)}\  \\
{\tt =}\  {\tt empty}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (hellos}\  {\tt 2)}\\
{\tt =}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt empty))}
\end{tabbing}\end{minipage}\end{center}

The design of a template for {{\tt hellos}\/} follows the design recipe for
self-referential data definitions.  We immediately see that {{\tt hellos}\/}
is a conditional function, that its {{\bf cond}-exp\-res\-sion} has two
clauses, and that the first clause must distinguish {{\tt 0}\/} from other
possible inputs:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (hellos}\  {\tt n)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

Furthermore, the data definition says that {{\tt 0}\/} is an atomic value,
and every other natural number is a compound value that ``contains'' the
predecessor to which {{\tt 1}\/} was added. Hence, if {{\tt n}\/} is not
{{\tt 0}\/}, we subtract {{\tt 1}\/} from {{\tt n}\/}. The result is also a
natural number, so according to the design recipe we wrap the expression
with {{\tt (hellos}\ {\tt ...)}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (hellos}\  {\tt n)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (hellos}\  {\tt (sub1}\  {\tt n))}\  {\tt ...}\  {\tt \char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Now we have exploited every hint in the data definition and are ready to
proceed with the definition. 

Assume {{\tt (zero?}\ {\tt n)}\/} evaluates to true. Then the answer must be
{{\tt empty}\/}, as the examples illustrate. So assume the input is greater
than {{\tt 0}\/}. For concreteness, let us say it is {{\tt 2}\/}. According
to the suggestion in the template, {{\tt hellos}\/} should use
{{\tt (hellos}\ {\tt 1)}\/} to compute a part of the answer. The purpose statement
specifies that {{\tt (hellos}\ {\tt 1)}\/} produces {{\tt (cons}\ {\tt \char'047}{\tt hello}\ {\tt empty)}\/}, a
list with one {{\tt \char'047}{\tt hello}\/}. In general, {{\tt (hellos}\ {\tt (sub1}\ {\tt n))}\/}
produces a list that contains $n - 1$ occurrences of {{\tt \char'047}{\tt hello}\/}.
Clearly, to produce a list with {{\tt n}\/} occurrences, we must
{{\tt cons}\/} another {{\tt \char'047}{\tt hello}\/} onto this list:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (hellos}\  {\tt n)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt (hellos}\  {\tt (sub1}\  {\tt n)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
As usual, the final definition is just the template with a few extras. 

Let's test {{\tt hellos}\/} with some hand-evaluations:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (hellos}\  {\tt 0)}\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (zero?}\  {\tt 0)}\  {\tt empty\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt (hellos}\  {\tt (sub1}\  {\tt 0)))\char'135}{\tt )}
\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\sf true}\  {\tt empty\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt (hellos}\  {\tt (sub1}\  {\tt 0)))\char'135}{\tt )}
\\
{\tt =}\  {\tt empty}
\end{tabbing}\end{minipage}\end{center}
It confirms that {{\tt hellos}\/} works properly for the first example.  

Here is another example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (hellos}\  {\tt 1)}\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (zero?}\  {\tt 1)}\  {\tt empty\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt (hellos}\  {\tt (sub1}\  {\tt 1)))\char'135}{\tt )}
\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\sf false}\  {\tt empty\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt (hellos}\  {\tt (sub1}\  {\tt 1)))\char'135}{\tt )}
\\
{\tt =}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt (hellos}\  {\tt (sub1}\  {\tt 1)))}
\\
{\tt =}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  \hbox{$\underline{\mbox{{{\tt (hellos}\ {\tt 0)}\/}}}$}{\tt )}
\\
{\tt =}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt empty)}
\end{tabbing}\end{minipage}\end{center}
For the last step in the calculation, we can exploit that we already know
that {{\tt (hellos}\ {\tt 0)}\/} evaluates to {{\tt empty}\/} and replace the
(underlined) expression with its result.

The last hand-evaluation shows that {{\tt hellos}\/} works for the second
example:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (hellos}\  {\tt 2)}\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (zero?}\  {\tt 2)}\  {\tt empty\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt (hellos}\  {\tt (sub1}\  {\tt 2)))\char'135}{\tt )}
\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\sf false}\  {\tt empty\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt (hellos}\  {\tt (sub1}\  {\tt 2)))\char'135}{\tt )}
\\
{\tt =}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt (hellos}\  {\tt (sub1}\  {\tt 2)))}
\\
{\tt =}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  \hbox{$\underline{\mbox{{{\tt (hellos}\ {\tt 1)}\/}}}$}{\tt )}
\\
{\tt =}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt (cons}\  {\tt \char'047}{\tt hello}\  {\tt empty))}
\end{tabbing}\end{minipage}\end{center}
We can again exploit what we know about {{\tt (hellos}\ {\tt 1)}\/}, which greatly
shortens the hand-evaluation. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 11.2.1}

 \label{ex:copy} \label{ex:copy-r}
Generalize {{\tt hellos}\/} to {{\tt repeat}\/}, which consumes a natural
number {{\tt n}\/} and a symbol and produces a list with {{\tt n}\/}
occurrences of the symbol.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/copy.html}{\rule{3pt}{5pt}}


{\bf Exercise 11.2.2}

 \label{ex:tabulate-f}
Develop the function {{\tt tabulate-f}\/}, which tabulates the values of
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt f}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt x)}
\\
\>{\tt (+}\  \={\tt (*}\  {\tt 3}\  {\tt (*}\  {\tt x}\  {\tt x))}\  
\\
\>\>{\tt (+}\  \={\tt (*}\  {\tt -6}\  {\tt x)}
\\
\>\>\>{\tt -1)))}
\end{tabbing}\end{minipage}\end{center}
for some natural numbers. Specifically, it consumes a natural number
{{\tt n}\/} and produces a list of {{\tt n}\/} {{\tt posn}\/}s. The first one
combines {{\tt n}\/} with {{\tt (f}\ {\tt n)}\/}, the second one {{\tt n-1}\/} with
{{\tt (f}\ {\tt n-1)}\/}, {\it etc\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tabulate-f.html}{\rule{3pt}{5pt}}


{\bf Exercise 11.2.3}

 \label{ex:apply-move} \label{ex:apply-move-r}
Develop {{\tt apply-n}\/}.  The function consumes a natural number,
{{\tt n}\/}. It applies the function {{\tt move}\/} from
exercise~\ref{ex:moving} {{\tt n}\/} times to {{\tt FACE}\/}, the list of
shapes from exercise~\ref{ex:moving-list-dat}. Each application should
translate the shape by one pixel. The purpose of the function is to
simulate a continuously moving shape on a canvas, the last missing piece of
the extended exercise~\ref{sec:move-fig}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/apply-move.html}{\rule{3pt}{5pt}}


{\bf Exercise 11.2.4}

 \label{ex:deep}
Lists may contain lists that contain lists and so on. Here is a data
definition that takes this idea to an extreme:

  \begin{quote} A {\sl {deep list}} is either       \begin{enumerate}        \item {{\tt s}\/} where {{\tt s}\/} is some symbol or         \item {{\tt (cons}\ {\tt dl}\ {\tt empty)}\/}, where {{\tt dl}\/} is a deep list.       \end{enumerate} \end{quote}

Develop the function {{\tt depth}\/}, which consumes a deep list and
determines how many {{\tt cons}\/}es were used to construct it. 

Develop the function {{\tt make-deep}\/}, which consumes a symbol
{{\tt s}\/} and a natural number and produces a deep list containing
{{\tt s}\/} and {{\tt cons}\/}tructed with {{\tt n}\/} {{\tt cons}\/}es. 
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/deep.html}{\rule{3pt}{5pt}}


\subsection{Extended Exercise: Creating Lists, Testing Functions} \label{sec:test1}

We often encounter situations where we would like to create lists of data
that involve namers. For example, we may wish to create large lists of
numbers to test a function like {{\tt extract1}\/} in
section~\ref{sec:list-in-list} on large lists instead of hand-coded small
ones. Sometimes we would like to visualize randomly picked data. We can
create such functions using recursion on natural numbers and a
random\footnote{For a precise discussion of what ``randomness'' means in computing, see \ldots} number generator.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 11.3.1}

 \label{ex:random} \label{ex:random-r-r}
Scheme provides the operation {{\tt random}\/}. It consumes a natural number,
{{\tt n}\/}, and produces a random integer between {{\tt 0}\/} and {{\tt n}\ {\tt -}\ {\tt 1}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt random}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to compute a natural number between {{\tt 0}\/} and {{\tt n-1}\/}}
\\
{\tt (define}\  {\tt (random}\  {\tt n}\  {\tt m)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Two successive uses of {{\tt (random}\ {\tt n)}\/} may produce two distinct results. 

Now consider the following definition:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt random-n-m}\ {\tt :}\ {\tt integer}\ {\tt integer}\ {}{\tt ->}{}\ {\tt integer}\/}}\\
\hbox{;; \ldots }
\\
\hbox{;; Assume: $n < m$}
\\
{\tt (d}\={\tt efine}\  {\tt (random-n-m}\  {\tt n}\  {\tt m)}
\\
\>{\tt (+}\  {\tt (random}\  {\tt (-}\  {\tt m}\  {\tt n))}\  {\tt n))}
\end{tabbing}\end{minipage}\end{center}
Formulate a succinct and precise purpose statement for {{\tt random-n-m}\/}.
Use a number line with an interval to explain the result of {{\tt (random}{ }{\tt n)}\/}.  Use a symbolic evaluation to support your
explanation.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/random.html}{\rule{3pt}{5pt}} 


{\bf Exercise 11.3.2}

 \label{ex:random3}
Develop the function {{\tt tie-dyed}\/}. It consumes a natural number and
produces a list of integers. Each of these integers should be between
{{\tt -20}\/} and {{\tt 120}\/}. Use {{\tt tie-dyed}\/} to test
{{\tt draw-circles}\/} from
exercise~\ref{ex:draw-list-shapes}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/random3.html}{\rule{3pt}{5pt}}


{\bf Exercise 11.3.3}

 \label{ex:random2}
Develop the function {{\tt create-temps}\/}.  It consumes a natural number {{\tt n}\/}
and two integers, called {{\tt low}\/} and {{\tt high}\/}. It produces a list
of {{\tt n}\/} integers that are between {{\tt low}\/} and {{\tt high}\/}.

Use {{\tt create-temps}\/} to test {{\tt check-range}\/} from
exercise~\ref{ex:check-range1}. 

Finally, discuss the following questions. Can we simply feed the result of
{{\tt create-temps}\/} into {{\tt check-range}\/} or do we need to know the
list that {{\tt create-temps}\/} produced? Are there values for {{\tt low}\/}
and {{\tt high}\/} such that we don't need to know the result of
{{\tt create-temps}\/} and yet we can predict the result of the test? Which
function tests which? What does this tell us about testing with
automatically generated test data?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/random2.html}{\rule{3pt}{5pt}}


{\bf Exercise 11.3.4}

 \label{ex:random4}
Develop the function {{\tt create-prices}\/}, which consumes a natural
number and produces a list with a corresponding of prices between \$.10 and
\$10.00 with increments of a dime. Use {{\tt create-prices}\/} to test
{{\tt dollar-store\char'077}\/} from exercise~\ref{ex:dollar-store}.

\noindent{\bf Hint:}  How many dimes are there between \$.10 and \$10.00?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/random4.html}{\rule{3pt}{5pt}}


{\bf Exercise 11.3.5}

 \label{ex:random1}
Develop a program that visualizes a student riot.  In preparation of a
student riot, a small group of students meets to make paint-filled
balloons. The typical riot uses {{\tt RED}\/} only. Then, on the evening of
the riot, the students enter a university's progressive theater with the
balloons and throw them all over the seats. 

The program's only input should be a natural number, which represents the
number of balloons thrown. The visualization should use a canvas that
contains a black grid and the positions of the balls: 

  \begin{rawhtml}  <center><img src=../icons/riot.gif></td></center>  \end{rawhtml}

\noindent Assume a random distribution of the balls over the theater's
seats. Each box in the grid represents a seat. Configure the program so
the change of one variable definition changes the number of columns in the
grid and a change to another changes the number of rows.

\noindent{\bf Hint:}  Develop auxiliary functions that draw some given number of lines in the
vertical and the horizontal direction.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/random1.html}{\rule{3pt}{5pt}}



\subsection{Alternative Data Definitions for {\bf N}} \label{sec:nat-num:alt} 

Using the above, standard data definition for natural numbers makes it
easy to develop all kinds of functions on numbers. Consider, for example,
a function that multiplies the first {{\tt n}\/} numbers. Put differently,
it consumes a natural number {{\tt n}\/} and multiplies all numbers
between {{\tt 0}\/} (exclusive) and {{\tt n}\/} (inclusive). The function
is called factorial and has the mathematical notation {{\tt !}\/}. Its
contract is easy to formulate:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt !}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to compute $n \cdot (n-1) \cdot \ldots \cdot 2 \cdot 1$}
\\
{\tt (define}\  {\tt (!}\  {\tt n)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
It consumes a natural number and produces one.

Specifying its input-output relationship is a bit more tricky. We
know, of course, what the product of {{\tt 1}\/}, {{\tt 2}\/}, and
{{\tt 3}\/} is, so we should have
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (!}\  {\tt 3)}\\
{\tt =}\  \={\tt 6}
\\
\hbox{;and, similarly, }
\\
\>{\tt (!}\  {\tt 10)}
\\
{\tt =}\  {\tt 3628800}
\end{tabbing}\end{minipage}\end{center}
The real question is what to do with the input {{\tt 0}\/}. According to the
informal description of the task, {{\tt !}\/} is supposed to produce the
product of all numbers between {{\tt 0}\/} (exclusive) and {{\tt n}\/}
(inclusive), the argument. Since {{\tt n}\/} is {{\tt 0}\/}, this request is
rather strange because there are no numbers between {{\tt 0}\/} (exclusive)
and {{\tt 0}\/} (inclusive). We solve the problem by following mathematical
convention and set that {{\tt (!}\ {\tt 0)}\/} evaluates to {{\tt 1}\/}.

From here, the rest is straightforward. The template for {{\tt !}\/}
is clearly that of a natural number-processing function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (!}\  {\tt n)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (!}\  {\tt (sub1}\  {\tt n))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The answer in the first {{\tt cond}\/}-clause is given: {{\tt 1}\/}. In the
second clause, the recursion produces the product of the first {{\tt n}\ {\tt -}{ }{\tt 1}\/} numbers, {\it e.g.}, for {{\tt n}\ {\tt =}\ {\tt 3}\/}, {{\tt (!}\ {\tt (sub1}\ {\tt n))}\/} evaluates to
{{\tt 2}\/}. To get the product of the first {{\tt n}\/} numbers, we just
need to multiply the (value of the) recursion by
{{\tt n}\/}. Figure~\ref{fig:fact} contains the complete definition of
{{\tt !}\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 11.4.1}

 \label{ex:test-fact}
Determine the value of {{\tt (!}\ {\tt 2)}\/} by hand and with DrScheme. 
Also test {{\tt !}\/} with {{\tt 10}\/}, {{\tt 100}\/}, and {{\tt 1000}\/}. \\

\noindent {\bf Note:} The results of these expressions are large numbers,
well beyond the native capacities of many other programming
languages. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/test-fact.html}{\rule{3pt}{5pt}}


Now suppose we wish to design the function {{\tt product-from-20}\/}, which computes the
product from {{\tt 20}\/} (exclusive) to some number {{\tt n}\/} (inclusive)
that is greater or equal to {{\tt 20}\/}.  We have several choices
here. First, we could define a function that computes {{\tt (!}\ {\tt n)}\/} and
{{\tt (!}\ {\tt 20)}\/} and divides the former by the latter. A simple mathematical
argument shows that this approach indeed yields the product of all numbers
between {{\tt 20}\/} (exclusive) and {{\tt n}\/} (inclusive):
\[
\frac{n \cdot (n-1) \cdot \ldots 21 \cdot 20 \cdot \ldots 1}
     {20 \cdot \ldots 1}
=
n \cdot (n-1) \cdot \ldots \cdot 21 \cdot
\frac{20 \cdot \ldots 1}
     {20 \cdot \ldots 1}
=
n \cdot (n-1) \cdot \ldots \cdot 21 \ .
\]

{\bf Exercise 11.4.2}

 \label{ex:pi1}
Use the idea to define {{\tt product}\/}, a function that consumes two natural
numbers, {{\tt n}\/} and {{\tt m}\/}, with {{\tt m}\ {\tt >}\ {\tt n}\/}, and that produces
the product of the numbers between {{\tt n}\/} (exclusive) and {{\tt m}\/}
(inclusive). \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/pi1.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Second, we can follow our design recipe, starting with a precise
characterization of the function's input.  Obviously, the inputs belong to
the natural numbers, but we know more than that. It belongs the following
collection of numbers: {{\tt 20}\/}, {{\tt 21}\/}, {{\tt 22}\/}, \ldots By now
we know how to describe such a set precisely with a data definition: 

   \begin{quote} A {\sl {natural number {{\tt \char'133}{\tt >=}\ {\tt 20\char'135}\/}}} is either       \begin{enumerate}        \item {{\tt 20}\/} or         \item {{\tt (add1}\ {\tt n)}\/} if {{\tt n}\/} is a natural number {{\tt \char'133}{\tt >=}\ {\tt 20\char'135}\/}.      \end{enumerate}  \noindent Notation: In contracts, we use {{\bf N}\ {\tt \char'133}{\tt >=}\ {\tt 20\char'135}\/} instead of ``natural numbers {{\tt \char'133}{\tt >=}\ {\tt 20\char'135}\/}.'' \end{quote}
 
Using the new data definition, we can formulate a contract for
{{\tt product-from-20}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt product-from-20:}\ {\bf N}\ {\tt \char'133}{\tt >=}\ {\tt 20\char'135}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to compute $n \cdot (n-1) \cdot \ldots \cdot 21 \cdot 1$}
\\
{\tt (define}\  {\tt (product-from-20}\  {\tt n-above-20)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Here is a first example for {{\tt product-from-20}\/}'s input-output specification: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (product-from-20}\  {\tt 21)}\\
{\tt =}\  {\tt 21}
\end{tabbing}\end{minipage}\end{center}
Since the function multiplies all numbers between {{\tt 20}\/} (exclusively)
and its input, {{\tt (product-from-20}\ {\tt 21)}\/} must produce {{\tt 21}\/}, which is the only
number in the interval. Similarly, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (product-from-20}\  {\tt 22)}\\
{\tt =}\  {\tt 462}
\end{tabbing}\end{minipage}\end{center}
for the same reason. Finally, we again follow mathematical convention and
agree that 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (product-from-20}\  {\tt 20)}\\
{\tt =}\  {\tt 1}
\end{tabbing}\end{minipage}\end{center}

The template for {{\tt product-from-20}\/} is a straightforward adaptation of the
template for {{\tt !}\/}, or any natural number-processing function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (product-from-20}\  {\tt n-above-20)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n-above-20}\  {\tt 20)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (product-from-20}\  {\tt (sub1}\  {\tt n-above-20))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The input {{\tt n-above-20}\/} is either {{\tt 20}\/} or larger. If it is
{{\tt 20}\/}, it does not have any components according to the data
definition. Otherwise, it is the result of adding {{\tt 1}\/} to a
natural number {{\tt \char'133}{\tt >=}\ {\tt 20\char'135}\/}, and we can recover this ``component'' by
subtracting {{\tt 1}\/}. The value of this selector expression belongs to 
the same class of data as the input and is thus a candidate for natural
recursion. 

Completing the template is equally straightforward. As specified, the
result of {{\tt (product-from-20}\ {\tt 20)}\/} is {{\tt 1}\/}, which determines the answer
for 
the first {{\tt cond}\/}-clauses. Otherwise, {{\tt (product-from-20}\ {\tt (sub1}\ {\tt n-above-20))}\/}
already produces the product of all the numbers between {{\tt 20}\/}
(exclusive) and {{\tt n-above-20}\ {\tt -}\ {\tt 1}\/}. The only number not included in this
range is {{\tt n-above-20}\/}. Hence, {{\tt (*}\ {\tt n-above-20}\ {\tt (product-from-20}\ {\tt (sub1}\ {\tt n-above-20)))}\/} is the
correct answer in the second clause. Figure~\ref{fig:fact} contains the
complete definition of {{\tt product-from-20}\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 11.4.3}

 \label{ex:pi-11}
Develop {{\tt product-from-minus-11}\/}.  The function consumes an integer
{{\tt n}\/} greater or equal to {{\tt -11}\/} and produces the product of all
the integers between {{\tt -11}\/} (exclusive) and {{\tt n}\/}
(inclusive). \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/pi-11.html}{\rule{3pt}{5pt}}


{\bf Exercise 11.4.4}

 \label{ex:tabulate-f20}
In exercise~\ref{ex:tabulate-f}, we developed a function that
tabulates some mathematical function {{\tt f}\/} for an interval 
of the shape $(0,n]$. 

Develop the function {{\tt tabulate-f20}\/}, which tabulates the values of
{{\tt f}\/} for natural numbers greater than {{\tt 20}\/}. Specifically,
the function consumes a natural number {{\tt n}\/} greater or equal to
{{\tt 20}\/} and produces a list of {{\tt posn}\/}s, each of which has the
shape {{\tt (make-posn}\ {\tt n}\ {\tt (f}\ {\tt n))}\/} for some {{\tt n}\/} between
{{\tt 20}\/} (exclusive) and {{\tt n}\/} (inclusive). \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tabulate-f20.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt !}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to compute $n \cdot (n-1) \cdot \ldots \cdot 2 \cdot 1$}
\\
{\tt (d}\={\tt efine}\  {\tt (!}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt 1\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (*}\  {\tt n}\  {\tt (!}\  {\tt (sub1}\  {\tt n)))\char'135}{\tt ))}
\\
\hbox{;; {{\tt product-from-20:}\ {\bf N}\ {\tt \char'133}{\tt >=}\ {\tt 20\char'135}\ {}{\tt ->}{}\ {\bf N}\/}}
\\
\hbox{;; to compute $n \cdot (n-1) \cdot \ldots \cdot 21 \cdot 1$}
\\
{\tt (d}\={\tt efine}\  {\tt (product-from-20}\  {\tt n-above-20)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n-above-20}\  {\tt 20)}\  {\tt 1\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (*}\  {\tt n-above-20}\  {\tt (product-from-20}\  {\tt (sub1}\  {\tt n-above-20)))\char'135}{\tt ))}
\\
\hbox{;; {{\tt product:}\ {\bf N}{\tt \char'133}{\tt limit\char'135}\ {\bf N}{\tt \char'133}{\tt >=}\ {\tt limit\char'135}\ {}{\tt ->}{}\ {\bf N}\/}}
\\
\hbox{;; to compute $n \cdot (n-1) \cdot \ldots \cdot (\mbox{\it limit\/}+1) \cdot 1$}
\\
{\tt (d}\={\tt efine}\  {\tt (product}\  {\tt limit}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt limit)}\  {\tt 1\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (*}\  {\tt n}\  {\tt (product}\  {\tt limit}\  {\tt (sub1}\  {\tt n)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Computing factorial, product-from-20, and product}
\label{fig:fact}
\hrule

A comparison of {{\tt !}\/} and {{\tt product-from-20}\/} suggests the
natural question of how to design a function that multiplies {\em all\/}
natural numbers in some range.  Roughly speaking, {{\tt product}\/} is like
{{\tt product-from-20}\/} except that the limit is not a part of the function
definition. Instead, it is another input, which suggests the following
contract:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt product:}\ {\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;;  to compute $n \cdot (n-1) \cdot \ldots \cdot (\mbox{\it limit\/}+1) \cdot 1$}
\\
{\tt (define}\  {\tt (product}\  {\tt limit}\  {\tt n)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
The intention is that {{\tt product}\/}, like {{\tt product-from-20}\/}, computes the
product from {{\tt limit}\/} (exclusive) to some number {{\tt n}\/}
(inclusive) that is greater or equal to {{\tt limit}\/}. 

Unfortunately, {{\tt product}\/}'s contract, in contrast {{\tt product-from-20}\/}'s, is
rather imprecise. In particular, it does not describe the collection of
numbers that {{\tt product}\/} consumes as the second input. Given its first
input, {{\tt limit}\/}, we know that the second input belongs to
{{\tt limit}\/}, {{\tt (add1}\ {\tt limit)}\/}, {{\tt (add1}\ {\tt (add1}\ {\tt limit))}\/},
{\it etc\/}. While it is easy to enumerate the possible second inputs, it also
shows that the description of the collection {\em depends on the first input\/}---an unusal situation that we have not encountered before. 

Still, if we assume limit is some number, the data description for the
second input is nearly obvious: 

  \begin{quote} Let {{\tt limit}\/} be a natural number. A {\sl {natural   number {{\tt \char'133}{\tt >=}\ {\tt limit\char'135}\/}}} ({{\bf N}{\tt \char'133}{\tt >=limit\char'135}\/}) is either     \begin{enumerate}   \item {{\tt limit}\/} or    \item {{\tt (add1}\ {\tt n)}\/} if {{\tt n}\/} is a natural number {{\tt \char'133}{\tt >=}\ {\tt limit\char'135}\/}.   \end{enumerate} \end{quote} 
\noindent In other words, the data definition is like that for natural numbers
{{\tt \char'133}{\tt >=}\ {\tt limit\char'135}\/} with {{\tt 20}\/} replaced by a variable
{{\tt limit}\/}. Of course, in high school, we refer to 
{{\bf N}{\tt \char'133}{\tt >=0\char'135}\/} as {\em the\/} natural numbers, and 
{{\bf N}{\tt \char'133}{\tt >=1\char'135}\/} as {\em the\/} positive integers. 

With this new data definition, we specify the contract for {{\tt product}\/} 
as follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt product:}\ {\bf N}{\tt \char'133}{\tt limit\char'135}\ {\bf N}\ {\tt \char'133}{\tt >=}\ {\tt limit\char'135}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;;  to compute $n \cdot (n-1) \cdot \ldots \cdot (\mbox{\it limit\/}+1) \cdot 1$}
\\
{\tt (define}\  {\tt (product}\  {\tt limit}\  {\tt n)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
That is, we name the first input, a natural number, with the notation
{{\tt \char'133}{\tt limit\char'135}\/} and specify the second input using the name for the
first one. 

The rest of the program development is straightforward. It is basically
the same as that for {{\tt product-from-20}\/} with {{\tt 20}\/} replaced by
{{\tt limit}\/} throughout. The only modification concerns the natural
recusion in the function template. Since the function consumes a
{{\tt limit}\/} and a {{\bf N}\ {\tt \char'133}{\tt >=}\ {\tt limit\char'135}\/}, the template
must contain an application of {{\tt product}\/} to {{\tt limit}\/} and
{{\tt (sub1}\ {\tt n)}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (product}\  {\tt limit}\  {\tt n)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt limit)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (product}\  {\tt limit}\  {\tt (sub1}\  {\tt n))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Otherwise things remain the same. The full function definition is
contained in figure~\ref{fig:fact}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 11.4.5}

 \label{ex:tabulate-f-limit}
In exercises~\ref{ex:tabulate-f} and \ref{ex:tabulate-f20}, we developed
functions that tabulate {{\tt f}\/} from some natural number or natural
number {{\tt \char'133}{\tt >=}\ {\tt 20\char'135}\/} down to {{\tt 0}\/} or {{\tt 20}\/} (exclusive),
respectively. 

Develop the function {{\tt tabulate-f-lim}\/}, which tabulates the values of
{{\tt f}\/} in an analoguos manner from some natural number {{\tt n}\/}
down to some other natural number {{\tt limit}\/}. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tabulate-f-limit.html}{\rule{3pt}{5pt}}


{\bf Exercise 11.4.6}

 \label{ex:tabulate-f-up-to-20}
In exercises~\ref{ex:tabulate-f}, \ref{ex:tabulate-f20},
and~\ref{ex:tabulate-f-limit}, we developed
functions that tabulate the mathematical function {{\tt f}\/} in various
ranges. In both cases, the final function produced a list of {{\tt posn}\/}s
that was ordered in {\em descending\/} order. That is, an expression like
{{\tt (tabulate-f}\ {\tt 3)}\/} yields 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ons}\  {\tt (make-posn}\  {\tt 3}\  {\tt 2.4)}\\
\>{\tt (c}\={\tt ons}\  {\tt (make-posn}\  {\tt 2}\  {\tt 3.4)}
\\
\>\>{\tt (c}\={\tt ons}\  {\tt (make-posn}\  {\tt 1}\  {\tt 3.6)}
\\
\>\>\>{\tt (c}\={\tt ons}\  {\tt (make-posn}\  {\tt 0}\  {\tt 3.0)}
\\
\>\>\>\>{\tt empty))))}
\end{tabbing}\end{minipage}\end{center}

If we prefer a list of {{\tt posn}\/}s in {\em ascending\/} order, we
must look at a  different data collection, natural numbers up to a
certain point in the chain: 

  \begin{quote} A {\sl {natural number {{\tt \char'133}{\tt <=}\ {\tt 20\char'135}\/}}} ({{\bf N}{\tt \char'133}{\tt <=20\char'135}\/})   is either    \begin{enumerate}   \item {{\tt 20}\/} or    \item {{\tt (sub1}\ {\tt n)}\/} if {{\tt n}\/} is a natural number {{\tt \char'133}{\tt <=}\ {\tt 20\char'135}\/}.     \end{enumerate} \end{quote}

\noindent Of course, in high school, we refer to {{\bf N}{\tt \char'133}{\tt <=-1\char'135}\/} 
as {\em the\/} negative integers.

Develop the function 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt tabulate-f-up-to-20}\ {\tt :}\ {\bf N}\ {\tt \char'133}{\tt <=}\ {\tt 20\char'135}\ {}{\tt ->}{}\ {\bf N}\/}}\\
{\tt (define}\  {\tt (tabulate-f-up-to-20}\  {\tt n-above-20)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
which tabulates the values of {{\tt f}\/} for natural numbers less than
{{\tt 20}\/}. Specifically, it consumes a natural number {{\tt n}\/} less
or equal to {{\tt 20}\/} and produces a list of {{\tt posn}\/}s, each of
which has the shape {{\tt (make-posn}\ {\tt n}\ {\tt (f}\ {\tt n))}\/} for some {{\tt n}\/}
between {{\tt 0}\/} and {{\tt n}\/} (inclusively). \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tabulate-f-up-to-20.html}{\rule{3pt}{5pt}}


{\bf Exercise 11.4.7}

 \label{ex:prime}
Develop the function {{\tt is-not-divisible-by<=i}\/}. It consumes a
natural number {{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\/}, {{\tt i}\/}, and a natural number
{{\tt m}\/}, with {{\tt i}\ {\tt <}\ {\tt m}\/}. If {{\tt m}\/} is not divisible by any
number between {{\tt 1}\/} (exclusive) and {{\tt i}\/} (inclusive), the
function produces {{\sf true}\/}; otherwise, its output is {{\sf false}\/}.

Use {{\tt is-not-divisible-by<=i}\/} to define {{\tt prime\char'077}\/}, which
consumes a natural number and determines whether or not it is prime. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/prime.html}{\rule{3pt}{5pt}} 




\subsection{More on the Nature of Natural Numbers} 
\label{sec:nat-num:nature} 

The natural numbers are a small subset of Scheme's numbers, not all of
them. Hence, the function template above {\em cannot\/} be used for
processing arbitrary numbers, {\it e.g.}, inexact numbers. Still, the template is
a good starting point for functions whose definitions involve both natural
numbers and other Scheme numbers. To illustrate this point, let us design
the function {{\tt add-to-pi}\/}, which consumes a natural number {{\tt n}\/}
and produces {{\tt n}\ {\tt +}\ {\tt 3.14}\/} without using {{\tt +}\/}.

Following the design recipe, we start with 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt add-to-pi}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute $n+3.14$ without using {{\tt +}\/}}
\\
{\tt (define}\  {\tt (add-to-pi}\  {\tt n)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Another easy step is to determine the output for a few sample inputs: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (add-to-pi}\  {\tt 0)}\  {\tt =}\  {\tt 3.14}\\
{\tt (add-to-pi}\  {\tt 2)}\  {\tt =}\  {\tt 5.14}
\\
{\tt (add-to-pi}\  {\tt 6)}\  {\tt =}\  {\tt 9.14}
\end{tabbing}\end{minipage}\end{center}

The difference between {{\tt hellos}\/}'s contract (see exercise~\ref{ex:copy})
and that of {{\tt add-to-pi}\/} is the output, but as we have seen before this
does not affect the template design.  We obtain the template for
{{\tt add-to-pi}\/} by renaming {{\tt hellos}\/} appropriately:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (add-to-pi}\  {\tt n)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (add-to-pi}\  {\tt (sub1}\  {\tt n))}\  {\tt ...}\  {\tt \char'135}{\tt )))}
\end{tabbing}\end{minipage}\end{center}
In combination with the examples, the template immediately suggests how to
complete the function. If the input is {{\tt 0}\/}, {{\tt add-to-pi}\/}'s
answer is {{\tt 3.14}\/}. Otherwise, {{\tt (add-to-pi}\ {\tt (sub1}\ {\tt n))}\/}
produces {{\tt (-}\ {\tt n}\ {\tt 1)}\ {\tt +}\ {\tt 3.14}\/}; since the correct answer is {{\tt 1}\/}
more than this value, the answer expression in the second
{{\tt cond}\/}-line is {{\tt (add1}\ {\tt (add-to-pi}\ {\tt (sub1}\ {\tt n)))}\/}. \relax
Figure~\ref{fig:add-to-pi} contains the complete function definition.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 11.5.1}

 \label{ex:add}
Define {{\tt add}\/}, which consumes two natural numbers, {{\tt n}\/} and
{{\tt x}\/}, and produces {{\tt n}\ {\tt +}\ {\tt x}\/} without using Scheme's
{{\tt +}\/}. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/add.html}{\rule{3pt}{5pt}}


{\bf Exercise 11.5.2}

  \label{ex:mult}
Develop the function {{\tt multiply-by-pi}\/}, which consumes a natural
number and multiplies it by {{\tt 3.14}\/} without using {{\tt *}\/}. For
example, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (multiply-by-pi}\  {\tt 0)}\  {\tt =}\  {\tt 0}\\
{\tt (multiply-by-pi}\  {\tt 2)}\  {\tt =}\  {\tt 6.28}
\\
{\tt (multiply-by-pi}\  {\tt 3)}\  {\tt =}\  {\tt 9.42}
\end{tabbing}\end{minipage}\end{center}

Define {{\tt multiply}\/}, which consumes two natural numbers, {{\tt n}\/}
and {{\tt x}\/}, and produces {{\tt n}\ {\tt *}\ {\tt x}\/} without using Scheme's
{{\tt *}\/}. Eliminate {{\tt +}\/} from these definitions, too.

\noindent{\bf Hint:} \ Recall that multipliplying {{\tt x}\/} by {{\tt n}\/} means adding
{{\tt x}\/} to itself {{\tt n}\/} times.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mult.html}{\rule{3pt}{5pt}}


{\bf Exercise 11.5.3}

 \label{ex:expt}
Develop the function {{\tt exponent}\/}, which consumes a natural number
{{\tt n}\/} and a number {{\tt x}\/} and computes $$x ^ {n} \ .$$ Eliminate
{{\tt *}\/} from the definition, too.

\noindent{\bf Hint:} \ Recall that exponentiating {{\tt x}\/} by {{\tt n}\/} means multiplying
{{\tt x}\/} with itself {{\tt n}\/} times.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/expt.html}{\rule{3pt}{5pt}}


{\bf Exercise 11.5.4}

 \label{ex:natnum-list}
Deep lists (see exercise~\ref{ex:deep}) are another representation for
natural numbers.  Show how to represent {{\tt 0}\/}, {{\tt 3}\/}, and
{{\tt 8}\/}.

Develop the function {{\tt addDL}\/}, which consumes two deep lists,
representing the natural numbers {{\tt n}\/} and {{\tt m}\/}, and which
produces a deep list representing {{\tt n}\ {\tt +}\ {\tt m}\/}. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/natnum-list.html}{\rule{3pt}{5pt}}


\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt add-to-pi}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute $n+3.14$ without using {{\tt +}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (add-to-pi}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt 3.14\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (add1}\  {\tt (add-to-pi}\  {\tt (sub1}\  {\tt n)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Adding a natural number to pi} \label{fig:add-to-pi}
\hrule


\section{Composing Functions, Revisited Again} 
\label{sec:compounding2} 

In section~\ref{sec:program+functions} we said that programs were
collections of function definitions and possibly some variable
definitions, too. To guide the division of labor among functions, we also
introduced a rough guideline: 
\begin{quote}
``Formulate auxiliary function definitions
for every dependency between quantities in the problem statement.'' 
\end{quote}
So far the guideline has been reasonably effective, but it is now time to
take a second look at it and to formulate some additional guidance
concerning auxiliary functions.

In the first subsection, we refine our original guideline concerning
auxiliary programs. The suggestions mostly put into words the experiences
that we made with the exercises. The second and third one illustrate two of
the ideas in more depth; the last one is an extended exercise.

\subsection{Designing Complex Programs}
\label{sec:compounding:td} 

When we develop a program, we may hope to implement it with a single
function definition but we should always be prepared to write auxiliary
functions. In particular, if the problem statement mentions several
dependencies, it is natural to express each of them as function. Others who
read the problem statement and the program can follow our reasoning more
easily that way. The movie-theater example in
section~\ref{sec:basics-compose} is a good example for this style of
development. 

Otherwise, we should follow the design recipe and start with a thorough
analysis of the input and output data. Using the data analysis we should
design a template and attempt to refine the template into a complete
function definition.  Turning a template into a complete function
definition means to combine the values of the template's subexpressions
into the final answer. As we do so, we might encounter several different
situations:
\begin{enumerate}
\item If the formulation of an answer requires a case analysis of the
   available values, use a {{\bf cond}-exp\-res\-sion}.

\item If a computation requires knowledge of a particular domain of
   application, for example, drawing on (computer) canvases, accounting,
   music, science, use an auxiliary function.

\item If a computation must process a list, a natural number, or some other
   piece of data of arbitrary size, use an auxiliary function.

\item If the natural formulation of the function isn't quite what we want,
   it is most likely a generalization of our target. In this case, the main
   function is a short definition that defers the computation to the
   generalized auxiliary program.
\end{enumerate}
The last two criteria are situations that we haven't discussed yet. The
following two subsections illustrate them with examples.

After we determine the need for an auxiliary function, we should add a
 contract, a header, and a purpose statement to a {\sc {wish list}} of
 functions.\footnote{The term ``wish list'' is due to Dr.\ John Stone.}

   \begin{rawhtml}     <table bgcolor=red align=center>       <tr><td><font align=center color=white size=+3>   \end{rawhtml} Guideline on Wish Lists   \begin{rawhtml}     <tr><td><font color=white><p>   \end{rawhtml} Maintain a list of functions that must be developed   to complete a program. Develop each function according to a design   recipe.    \begin{rawhtml}     </p></table>   \end{rawhtml}  

\noindent Before we put a function on the wish list, we must check whether
 something like the function already exists or is already on the wish
 list. Scheme provides many primitive operations and functions, and so do
 other languages. We should find out as much as possible about our working
 language, though only when we settle on one. For beginners, a superficial
 knowledge of a language is fine. 

If we follow these guidelines, we interleave the development of one
 function with that of others. As we finish a function that does not depend
 on anything on our wish list, we can test it. Once we have tested such
 basic functions, we can work our way backwards and test other functions
 until we have finished the wish list. By testing each of a function
 rigorously before we test those that depend on it, we greatly reduce the
 effort of searching for logical mistakes.  

\subsection{Recursive Auxiliary Functions} \label{sec:sortI} \htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:sortI}

People need to sort things all the time. Investment advisors sort
 portfolios by the profit each holding generates. Doctors sort lists of
 transplant patients. Mail programs sort messages.  More generally, sorting
 lists of values by some criteria is a task that many programs need to
 perform.

Here we study how to sort a list of numbers because it is important for
 many programming tasks, but also because it provides a good case study
 concerning the design of auxiliary programs. A sorting function consumes a
 list and produces one. Indeed, the two lists contains the same numbers,
 though the output list contains them in a different order. This is the
 essence of the contract and purpose statement:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
\hbox{;; {{\tt sort}\ {\tt :}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/} }
\\
\hbox{;; to create sorted list of numbers from all the numbers in {{\tt alon}\/}}
\\
{\tt (define}\  {\tt (sort}\  {\tt alon)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

Here is one example per clause in the data definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (sort}\  {\tt empty)}\\
{\tt =}\  {\tt empty}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (sort}\  {\tt (cons}\  {\tt 1297.04}\  {\tt (cons}\  {\tt 20000.00}\  {\tt (cons}\  {\tt -505.25}\  {\tt empty))))}\\
{\tt =}\  {\tt (cons}\  {\tt 20000.00}\  {\tt (cons}\  {\tt 1297.04}\  {\tt (cons}\  {\tt -505.25}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
The answer for the input {{\tt empty}\/} is {{\tt empty}\/}, because
 {{\tt empty}\/} contains the same items (none) and in sorted order. 

Next we must translate the data definition into a function template. Again,
we have dealt with lists of numbers before, so this step is easy:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (sort}\  {\tt alon)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt alon)}\  {\tt ...}\  {\tt (sort}\  {\tt (rest}\  {\tt alon))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

Using this template, we can finally turn to the interesting part of the
 program development. We consider each case of the {{\bf cond}-exp\-res\-sion}
 separately, starting with the simple case. If {{\tt sort}\/}'s input is
 {{\tt empty}\/}, then the answer is {{\tt empty}\/}, as specified by the
 example.  So let's assume that the input is not {{\tt empty}\/}. That is,
 let's deal with the second {{\tt cond}\/}-clause.  It contains two
 expressions and, following the design recipe, we must understand what they
 compute:
\begin{enumerate}
\item {{\tt (first}\ {\tt alon)}\/} extracts the first number from the input; 

\item {{\tt (sort}\ {\tt (rest}\ {\tt alon))}\/} produces a sorted version of
 {{\tt (rest}\ {\tt alon)}\/}, according to the purpose statement of the function. 
\end{enumerate}
Putting together these two values means to insert the first number into its
appropriate spot in the sorted rest of the list. 

Let's look at the second example in this context.  When {{\tt sort}\/}
consumes {{\tt (cons}\ {\tt 1297.04}\ {\tt (cons}\ {\tt 20000.00}\ {\tt (cons}\ {\tt -505.25}\ {\tt empty)))}\/}, then
\begin{enumerate}
\item {{\tt (first}\ {\tt alon)}\/} evaluates to {{\tt 1297.04}\/}, 

\item {{\tt (rest}\ {\tt alon)}\/} is {{\tt (cons}\ {\tt 20000.00}\ {\tt (cons}\ {\tt -505.25}\ {\tt empty))}\/}, and 

\item {{\tt (sort}\ {\tt (rest}\ {\tt alon))}\/} produces {{\tt (cons}\ {\tt 20000.00}\ {\tt (cons}\ {\tt -505.25}\ {\tt empty))}\/}. 
\end{enumerate}
 To produce the desired answer, we must insert {{\tt 1297.04}\/} in between
 the two numbers of the last list. More generally, the answer in the second
 {{\tt cond}\/}-line must be an expression that inserts {{\tt (first}{ }\ {\tt alon)}\/} into its proper place into the sorted list {{\tt (sort}\ {\tt (rest}{ }\ {\tt alon))}\/}.

Insert a number into a sorted list isn't a simple task. We may have to
 search through the entire list before we know what the proper place
 is. Searching through a list, however, can only be done with a function,
 because lists are of arbitrary size and processing such values requires
 recursive functions. Thus we must develop an auxiliary function that
 consumes the first number and a sorted list and create a sorted list from
 both. Let us call this function {{\tt insert}\/} and let us formulate a
 wish-list entry:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt insert}\ {\tt :}\ {\tt number}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
\hbox{;; to create a list of numbers from {{\tt n}\/} and the numbers on {{\tt alon}\/} }
\\
\hbox{;; that is sorted in descending order; {{\tt alon}\/} is already sorted}
\\
{\tt (define}\  {\tt (insert}\  {\tt n}\  {\tt alon)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

Using {{\tt insert}\/}, it is easy to complete the definition of
{{\tt sort}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (sort}\  {\tt alon)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (insert}\  {\tt (first}\  {\tt alon)}\  {\tt (sort}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The answer in the second line says that in order to produce the final
result, {{\tt sort}\/} extracts the first item of the non-empty list,
computes the sorted version of the rest of the list, and {{\tt insert}\/}s
the former into the latter at its appropriate place.

Of course, we are not really finished until we have developed
{{\tt insert}\/}.  We already have a contract, a header and a purpose
statement.  Next we need to make up function examples. Since the first input
of {{\tt insert}\/} is atomic, let's make up examples based on the data
definition for lists. That is, we first consider what {{\tt insert}\/}
should produce when given a number and {{\tt empty}\/}. According to
{{\tt insert}\/}'s purpose statement, the output must be a list, it must
contain all numbers from the second input, and it must contain the first
argument. This suggests the following: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (insert}\  {\tt 5}\  {\tt empty)}\\
{\tt =}\  {\tt (cons}\  {\tt 5}\  {\tt empty)}
\end{tabbing}\end{minipage}\end{center}
Instead of {{\tt 5}\/}, we could have used any number. 

The second example must use a non-empty list, but then, the idea for
{{\tt insert}\/} was suggested by just such an example when we studied how
{{\tt sort}\/} should deal with non-empty lists. Specifically, we said that
{{\tt sort}\/} had to insert {{\tt 1297.04}\/} into {{\tt (cons}\ {\tt 20000.00}{ }{\tt (cons}\ {\tt -505.25}\ {\tt empty))}\/} at its proper place: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (insert}\  {\tt 1297.04}\  {\tt (cons}\  {\tt 20000.00}\  {\tt (cons}\  {\tt -505.25}\  {\tt empty)))}\\
{\tt =}\  {\tt (cons}\  {\tt 20000.00}\  {\tt (cons}\  {\tt 1297.04}\  {\tt (cons}\  {\tt -505.25}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}

In contrast to {{\tt sort}\/}, the function {{\tt insert}\/} consumes {\em two\/} inputs. But we know that the first one is a number and atomic.  We
can therefore focus on the second argument, which is a list of numbers and
which suggests that we use the list-processing template one more time:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (insert}\  {\tt n}\  {\tt alon)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  \  {\tt ...}\  \  {\tt (first}\  {\tt alon)}\  {\tt ...}\  {\tt (insert}\  {\tt n}\  {\tt (rest}\  {\tt alon))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The only difference between this template and the one for {{\tt sort}\/} is
that this one needs to take into account the additional argument {{\tt n}\/}.

To fill the gaps in the template of {{\tt insert}\/}, we again proceed on a
 case-by-case basis.  The first case concerns the empty list. According to
 the purpose statement, {{\tt insert}\/} must now construct a list with one
 number: {{\tt n}\/}. Hence, the answer in the first case is {{\tt (cons}\ {\tt n}{ }\ {\tt alon)}\/}.

The second case is more complicated than that. When {{\tt alon}\/} is not
empty, 
\begin{enumerate}
\item {{\tt (first}\ {\tt alon)}\/} is the first number on {{\tt alon}\/} and 

\item {{\tt (insert}\ {\tt n}\ {\tt (rest}\ {\tt alon))}\/} produces a sorted list consisting of
{{\tt n}\/} and all numbers on {{\tt (rest}\ {\tt alon)}\/}.
\end{enumerate}
The problem is how to combine these pieces of data to get the answer. Let
us consider an example:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (insert}\  {\tt 3}\  {\tt (cons}\  {\tt 4}\  {\tt (cons}\  {\tt 5}\  {\tt (cons}\  {\tt 6}\  {\tt empty))))}\end{tabbing}\end{minipage}\end{center}
Here {{\tt n}\/} is {{\tt 3}\/} and smaller than any of the numbers in the
second input. Hence, it suffices if we just {{\tt cons}\/} {{\tt 3}\/} onto 
{{\tt (cons}\ {\tt 4}\ {\tt (cons}\ {\tt 5}\ {\tt (cons}\ {\tt 6}\ {\tt empty)))}\/}. In contrast, when the
application is something like 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (insert}\  {\tt 3}\  {\tt (cons}\  {\tt -1}\  {\tt (cons}\  {\tt 1}\  {\tt (cons}\  {\tt 2}\  {\tt (cons}\  {\tt 6}\  {\tt empty)))))}\end{tabbing}\end{minipage}\end{center}
{{\tt n}\/} must indeed be inserted into the rest of the list. More
concretely,
\begin{enumerate}
\item {{\tt (first}\ {\tt alon)}\/} is {{\tt -1}\/}

\item {{\tt (insert}\ {\tt n}\ {\tt (rest}\ {\tt alon))}\/} is {{\tt (cons}\ {\tt 1}\ {\tt (cons}\ {\tt 2}\ {\tt (cons}\ {\tt 3}\ {\tt (cons}\ {\tt 6}\ {\tt empty))))}\/}. 
\end{enumerate}
By {{\tt cons}\/}ing {{\tt -1}\/} onto this last list, we get the desired
answer. 

Here is how we generalize from these examples. The problem requires a
further case distinction. If {{\tt (first}\ {\tt alon)}\/} is smaller than (or
equal to) {{\tt n}\/}, all the items in {{\tt alon}\/} are smaller than
{{\tt n}\/}; after all, {{\tt alon}\/} is already sorted. The result is
{{\tt (cons}\ {\tt n}\ {\tt alon)}\/} for this case. If, however, {{\tt (first}\ {\tt alon)}\/} is
larger than {{\tt n}\/}, then we have not yet found the proper place to
insert {{\tt n}\/} into {{\tt alon}\/}.  We do know that the first item of
the result must be the {{\tt (first}\ {\tt alon)}\/} and that {{\tt n}\/} must be
inserted into {{\tt (rest}\ {\tt alon)}\/}. The final result in this case is
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  {\tt (first}\  {\tt alon)}\  {\tt (insert}\  {\tt n}\  {\tt (rest}\  {\tt alon)))}\end{tabbing}\end{minipage}\end{center}
because this list contains {{\tt n}\/} and all items of {{\tt alon}\/} in
sorted order---which is what we need.

The translation of this discussion into Scheme requires the formulation of
a conditional expression that distinguishes between the two possible
cases:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (<=}\  {\tt (first}\  {\tt alon)}\  {\tt n)}\  {\tt ...\char'135}
\\
\>{\tt \char'133}{\tt (>}\  {\tt (first}\  {\tt alon)}\  {\tt n)}\  {\tt ...\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
From here, we just need to put the proper answer expressions into the two
{{\tt cond}\/}-clauses. Figure~\ref{fig:sort} contains the complete
definitions of {{\tt insert}\/} and {{\tt sort}\/}.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sort}\ {\tt :}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
\hbox{;; to create a list of numbers with the same numbers as}
\\
\hbox{;; {{\tt alon}\/} sorted in descending order}
\\
{\tt (d}\={\tt efine}\  {\tt (sort}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt (cons?}\  {\tt alon)}\  {\tt (insert}\  {\tt (first}\  {\tt alon)}\  {\tt (sort}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\\
\hbox{;; {{\tt insert}\ {\tt :}\ {\tt number}\ {\tt list-of-numbers}\ {\tt (sorted)}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}
\\
\hbox{;; to create a list of numbers from {{\tt n}\/} and the numbers on}
\\
\hbox{;; {{\tt alon}\/} that is sorted in descending order; {{\tt alon}\/} is sorted}
\\
{\tt (d}\={\tt efine}\  {\tt (insert}\  {\tt n}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt (cons}\  {\tt n}\  {\tt empty)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (<=}\  {\tt (first}\  {\tt alon)}\  {\tt n)}\  {\tt (cons}\  {\tt n}\  {\tt alon)\char'135}
\\
\>\>\>{\tt \char'133}{\tt (>}\  {\tt (first}\  {\tt alon)}\  {\tt n)}\  {\tt (cons}\  {\tt (first}\  {\tt alon)}\  {\tt (insert}\  {\tt n}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Sorting lists of numbers}
\label{fig:sort}
\hrule

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 

{\bf Exercise 12.2.1}

 \label{ex:sort1}
Develop a program that sorts lists of mail messages by date. Mail
structures are defined as follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt mail}\  {\tt (from}\  {\tt date}\  {\tt message))}\end{tabbing}\end{minipage}\end{center}

\begin{quote} A {\sl {mail message}} is a structure: \\
    \centerline{{{\tt (make-structure}\ {\tt name}\ {\tt n}\ {\tt s)}\/}}  where {{\tt name}\/} is a string, {{\tt n}\/} is a number, and {{\tt s}\/} is  a string. \end{quote}

\noindent Also develop a program that sorts lists of mail messages by
name. To compare two strings alphabetically, use the {{\tt string<?}\/}
primitive.\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sort1.html}{\rule{3pt}{5pt}}


{\bf Exercise 12.2.2}

 \label{ex:sort2}
Here is the function {{\tt search}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt search}\ {\tt :}\ {\tt number}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (search}\  {\tt n}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt false\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (or}\  {\tt (=}\  {\tt (first}\  {\tt alon)}\  {\tt n)}\  {\tt (search}\  {\tt n}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
It determines whether some number occurs in a list of numbers. The function
may have to traverse the entire list to find out that the number of
interest isn't contained in the list.

Develop the function {{\tt search-sorted}\/}, which determines whether a 
number occurs in a sorted list of numbers. The function must take advantage
of the fact that the list is sorted.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sort2.html}{\rule{3pt}{5pt}}


\subsection{Generalizing Problems, Generalizing Functions} \label{sec:gen-funcs}

Consider the problem of drawing a {\sl polygon\/}, that is, a geometric
shape with an arbitrary number of corners.\footnote{Mr.\ Paul C.\ Fisher inspired this section.} A natural representation for a polygon is a list of
{{\tt posn}\/} structures:

  \begin{quote} A {\sl {list of posns}} ({{\tt list-of-posns}\/}) is either     \begin{enumerate}       \item the empty list, {{\tt empty}\/}, or       \item {{\tt (cons}\ {\tt p}\ {\tt lop)}\/} where {{\tt p}\/} is a {{\tt posn}\/}          structure and {{\tt lop}\/} is a list of posns.     \end{enumerate} \end{quote}

\noindent Each {{\tt posn}\/} represents one corner of the polygon. For
example, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ons}\  {\tt (make-posn}\  {\tt 10}\  {\tt 10)}\\
\>{\tt (c}\={\tt ons}\  {\tt (make-posn}\  {\tt 60}\  {\tt 60)}
\\
\>\>{\tt (c}\={\tt ons}\  {\tt (make-posn}\  {\tt 10}\  {\tt 60)}
\\
\>\>\>{\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
represents a triangle. The question is what {{\tt empty}\/} means as a
polygon. The answer is that {{\tt empty}\/} does not represent a polygon and
therefore shouldn't be included in the class of polygon representations. A
polygon should always have at least one corner, and the lists that represent
polygons should always contain at least one {{\tt posn}\/}. This suggest the
following data definition:

  \begin{quote} A {\sl {polygon}} is either     \begin{enumerate}       \item {{\tt (cons}\ {\tt p}\ {\tt empty)}\/} where {{\tt p}\/} is a {{\tt posn}\/}, or       \item {{\tt (cons}\ {\tt p}\ {\tt lop)}\/} where {{\tt p}\/} is a {{\tt posn}\/}          structure and {{\tt lop}\/} is a polygon.     \end{enumerate} \end{quote}

\noindent In short, a discussion of how the chosen set of data (lists of
{{\tt posn}\/}s) represents the intended information (geometric polygons)
revealed that our choice was inadequate. Revising the data definition
brought us closer to our intentions and makes it easier to design the
program. 

Because our drawing primitives always produce {{\tt true}\/} (if anything),
it is natural to suggest the following contract and purpose statement: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt draw-polygon}\ {\tt :}\ {\tt polygon}\ {}{\tt ->}{}\ {\tt true}\/}}\\
\hbox{;; to draw the polygon specified by {{\tt a-poly}\/} }
\\
{\tt (define}\  {\tt (draw-polygon}\  {\tt a-poly)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
In other words, the function draws the lines between the corners and, if
all primitive drawing steps work out, it produces {{\tt true}\/}. For
example, the above list of {{\tt posn}\/}s should produce a triangle. 

Although the data definition is not just a variant on our well-worn list
theme, the template is close to that of a list-processing function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
\hbox{;; {{\tt draw-polygon}\ {\tt :}\ {\tt polygon}\ {}{\tt ->}{}\ {\tt true}\/}}
\\
\hbox{;; to draw the polygon specified by {{\tt a-poly}\/} }
\\
{\tt (d}\={\tt efine}\  {\tt (draw-polygon}\  {\tt a-poly)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt (rest}\  {\tt a-poly))}\  {\tt ...}\  {\tt (first}\  {\tt a-poly)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt els}\={\tt e}\  {\tt ...}\  {\tt (first}\  {\tt a-poly)}\  {\tt ...}\  
\\
\>\>\>{\tt ...}\  {\tt (second}\  {\tt a-poly)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (draw-polygon}\  {\tt (rest}\  {\tt a-poly))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Given that both clauses in the data definition use {{\tt cons}\/}, the first
condition must inspect the rest of the list, which is {{\tt empty}\/} for
the first case and non-empty for the second one. Furthermore, in the first
clause, we can add {{\tt (first}\ {\tt a-poly)}\/}; and in the second case, we not
only have the first item on the list but the second one, too. After all,
polygons generated according to the second clause consist of at least two
{{\tt posn}\/}s.

Now we can replace the ``\ldots'' in the template to obtain a complete
function definition. For the first clause, the answer must be
{{\tt true}\/}, because we don't have two {{\tt posn}\/}s that we could
connect to form a line. For the second clause, we have two {{\tt posn}\/}s,
we can draw a line between them, and we know that {{\tt (draw-polygon}{ }{\tt (rest}\ {\tt a-poly))}\/} draws all the remaining lines. Put differently, we can
write
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (draw-solid-line}\  {\tt (first}\  {\tt a-poly)}\  {\tt (second}\  {\tt a-poly))}\end{tabbing}\end{minipage}\end{center}
in the second clause because we know that {{\tt a-poly}\/} has a second
item. Both {{\tt (draw-solid-line}\ {\tt ...)}\/} and {{\tt (draw-poly}\ {\tt ...)}\/} produce
{{\tt true}\/} if everything goes fine. By combining the two expressions
with {{\tt and}\/}, {{\tt draw-poly}\/} draws all lines. 

Here is the complete function definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (d}\={\tt efine}\  {\tt (draw-polygon}\  {\tt a-poly)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt (rest}\  {\tt a-poly))}\  {\tt true\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (and}\  \={\tt (draw-solid-line}\  {\tt (first}\  {\tt a-poly)}\  {\tt (second}\  {\tt a-poly))}
\\
\>\>\>{\tt (draw-polygon}\  {\tt (rest}\  {\tt a-poly)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Unfortunately, testing it with our triangle example immediately reveals a
flaw. Instead of drawing a polygon with three sides, the function draws
only an open curve, connecting all the corners but not closing the curve:
\begin{center}



\begin{picture}(125,100)\thicklines
\put(000,000){\line(0,+1){100}}
\put(000,100){\line(1,+0){125}}
\put(125,100){\line(0,-1){100}}
\put(125,000){\line(-1,0){125}}
\put(10,90){\circle*{3}} 
\put(10,90){\line(1,-1){50}} 
\put(15,87){$(10,10)$}
\put(60,40){\circle*{3}} 
\put(60,40){\line(-1,0){50}} 
\put(65,37){$(60,60)$}
\put(10,40){\circle*{3}} 
\put(10,30){$(10,60)$}
\end{picture}
\end{center}
Mathematically put, we have defined a more general function than the one we
wanted. The function we defined should be called ``connect-the-dots'' and
not {{\tt draw-polygon}\/}. 

To get from the more general function to what we want, we need to figure
out some way to connect the last dot to the first one. There are several
different ways to accomplish this goal, but all of them mean that we define
the main function in terms of the function we just defined or something
like it. In other words, we define one auxiliary function in terms of a
more general one. 

One way to define the new function is to add the first position of a
polygon to the end and to have this new list drawn. A symmetric method is
to pick the last one and add it to the front of the polygon. A third
alternative is to modify the above version of {{\tt draw-polygon}\/} so that
it connects the last {{\tt posn}\/} to the first one. Here we discuss the
first alternative; the exercises cover the other two. 

To add the last item of {{\tt a-poly}\/} at the beginning, we need something
like 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  {\tt (last}\  {\tt a-poly)}\  {\tt a-poly)}\end{tabbing}\end{minipage}\end{center}
where {{\tt last}\/} is some auxiliary function that extracts the last item
from a non-empty list. Indeed, this expression is the definition of
{{\tt draw-polygon}\/} assuming we define {{\tt last}\/}: see
figure~\ref{fig:draw-poly}. 

Formulating the wish list entry for {{\tt last}\/} is straightforward: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt last}\ {\tt :}\ {\tt polygon}\ {}{\tt ->}{}\ {\tt posn}\/}}\\
\hbox{;; to extract the last {{\tt posn}\/} on {{\tt a-poly}\/}}
\\
{\tt (define}\  {\tt (last}\  {\tt a-poly)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
And, because {{\tt last}\/} consumes a polygon, we can reuse the template
from above: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (d}\={\tt efine}\  {\tt (last}\  {\tt a-poly)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt (rest}\  {\tt a-poly))}\  {\tt ...}\  {\tt (first}\  {\tt a-poly)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt els}\={\tt e}\  {\tt ...}\  {\tt (first}\  {\tt a-poly)}\  {\tt ...}\  
\\
\>\>\>{\tt ...}\  {\tt (second}\  {\tt a-poly)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (last}\  {\tt (rest}\  {\tt a-poly))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

Turning the template into a complete function is a short step. If the list
is empty except for one item, this item is the desired result. If
{{\tt (rest}\ {\tt a-poly)}\/} is not empty, {{\tt (last}\ {\tt (rest}\ {\tt a-poly))}\/}
determines the last item of {{\tt a-poly}\/}. The complete definition of
{{\tt last}\/} is displayed at the bottom of figure~\ref{fig:draw-poly}. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt draw-polygon}\ {\tt :}\ {\tt polygon}\ {}{\tt ->}{}\ {\tt true}\/}}\\
\hbox{;; to draw the polygon specified by a-poly }
\\
{\tt (d}\={\tt efine}\  {\tt (draw-polygon}\  {\tt a-poly)}
\\
\>{\tt (connect-dots}\  {\tt (cons}\  {\tt (last}\  {\tt a-poly)}\  {\tt a-poly)))}
\\
\hbox{;; {{\tt connect-dots}\ {\tt :}\ {\tt polygon}\ {}{\tt ->}{}\ {\tt true}\/}}
\\
\hbox{;; to draw connections between the dots of {{\tt a-poly}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (connect-dots}\  {\tt a-poly)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt (rest}\  {\tt a-poly))}\  {\tt true\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (and}\  \={\tt (draw-solid-line}\  {\tt (first}\  {\tt a-poly)}\  {\tt (second}\  {\tt a-poly)}\  {\tt RED)}
\\
\>\>\>{\tt (connect-dots}\  {\tt (rest}\  {\tt a-poly)))\char'135}{\tt ))}
\\
\hbox{;; {{\tt last}\ {\tt :}\ {\tt polygon}\ {}{\tt ->}{}\ {\tt posn}\/}}
\\
\hbox{;; to extract the last {{\tt posn}\/} on {{\tt a-poly}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (last}\  {\tt a-poly)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt (rest}\  {\tt a-poly))}\  {\tt (first}\  {\tt a-poly)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (last}\  {\tt (rest}\  {\tt a-poly))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Drawing a polygon} \label{fig:draw-poly}
\hrule

In summary, the development of {{\tt draw-polygon}\/} naturally led us to
consider a more general problem: connecting a list of dots. We solved the
originally problem by defining a function that uses (a variant of) the more
general function. As we will see again and again, generalizing the purpose
of a function is often the best method to simplify the problem. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 12.3.1}

 \label{ex:draw-poly0}
Modify {{\tt draw-polygon}\/} so that it adds the first item of
{{\tt a-poly}\/} to its end. This requires a different auxiliary function:
{{\tt add-at-end}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/draw-poly0.html}{\rule{3pt}{5pt}}


{\bf Exercise 12.3.2}

 \label{ex:draw-poly1}
Modify {{\tt connect-dots}\/} so that it consumes an additional
{{\tt posn}\/} structure to which the last {{\tt posn}\/} is connected. 

Then modify {{\tt draw-polygon}\/} to use this new version of
{{\tt connect-dots}\/}. 

\medskip\noindent{\bf Accumulator:}:\  The new version of {{\tt connect-dots}\/} is a simple
instance of an accumulator-style function. In part~\ref{part:loops} we will
discuss an entire class of such problems.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/draw-poly1.html}{\rule{3pt}{5pt}}


\subsection{Extended Exercise: Rearranging Words} \label{sec:permute}

Newspapers often contain exercises that ask readers to find all possible words
made up from some letters.  One way to play this game is to form all possible
arrangements of the letters in a systematic manner and to see which
arrangements are dictionary words.  Suppose the letters ``a'', ``d'', ``e'',
and ``r'' are given. There are twenty-four possible arrangements of these
letters:
\begin{quote}\begin{tabular}{lllll}
\begin{tabular}{l}
ader\\
daer\\
dear\\
dera\\
aedr\\
\end{tabular}&
\begin{tabular}{l}
eadr\\
edar\\
edra\\
aerd\\
eard\\
\end{tabular}&
\begin{tabular}{l}
erad\\
erda\\
adre\\
dare\\
drae\\
\end{tabular}&
\begin{tabular}{l}
drea\\
arde\\
rade\\
rdae\\
rdea\\
\end{tabular}&
\begin{tabular}{l}
ared\\
raed\\
read\\
reda\\
\ \\
\end{tabular}\\
\end{tabular}
\end{quote}
The three legitimate words in this list are ``read'', ``dear'', and
``dare.''

The systematic enumeration of all possible arrangements is clearly a task
for a computer program. It consumes a word and produces a list of the
word's letter-by-letter rearrangements. 

A simple representation of a word is a list of symbols.  Each item in the
input represents a letter: {{\tt \char'047}{\tt a}\/}, {{\tt \char'047}{\tt b}\/}, \ldots, {{\tt \char'047}{\tt z}\/}.
Here is the data definition for the class of words: 

   \begin{quote} A {\sl {word}} is either     \begin{enumerate}    \item {{\tt empty}\/}, or     \item {{\tt (cons}\ {\tt a}\ {\tt w)}\/} where {{\tt a}\/} is a symbol ({{\tt \char'047}{\tt a}\/},    {{\tt \char'047}{\tt b}\/}, \ldots, {{\tt \char'047}{\tt z}\/}) and {{\tt w}\/} is a word.     \end{enumerate}     \end{quote}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 12.4.1}

 \label{ex:permutations1}
Formulate the data definition for lists of words. Systematically make up
examples of words and lists of words.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/permutations.html}{\rule{3pt}{5pt}}


Let us call the function {{\tt arrangements}\/}.\footnote{The mathematical term is {\em permutation\/}.} Its template is that of a list-processing
function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt arrangements}\ {\tt :}\ {\tt word}\ {}{\tt ->}{}\ {\tt list-of-words}\/}}\\
\hbox{;; to create a list of all re-arrangements of the letters in {{\tt a-word}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (arrangements}\  {\tt a-word)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-word)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt a-word)}\  {\tt ...}\  {\tt (arrangements}\  {\tt (rest}\  {\tt a-word))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

Given the contract, the supporting data definitions, and the examples, we
can now look at each {{\tt cond}\/}-line in the template:
\begin{enumerate}
\item If the input is {{\tt empty}\/}, there is only one possible re-arrangement of
the input: the {{\tt empty}\/} word. Hence, the result is {{\tt (cons}{ }{\tt empty}\ {\tt empty)}\/}, the list that contains the empty list as the only item.

\item Otherwise there is a first letter in the word, and {{\tt (first}{ }{\tt a-word)}\/} is that letter and the recursion produces the list of all possible
re-arrangements for the rest of the word. For example, if the list is
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  {\tt \char'047}{\tt d}\  {\tt (cons}\  {\tt \char'047}{\tt e}\  {\tt (cons}\  {\tt \char'047}{\tt r}\  {\tt empty)))}\end{tabbing}\end{minipage}\end{center}

\noindent then the recursion is {{\tt (arrangements}\ {\tt (cons}\ {\tt \char'047}{\tt e}\ {\tt (cons}\ {\tt \char'047}{\tt r}{ }{\tt empty)))}\/}. It will produce the result
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ons}\  {\tt (cons}\  {\tt \char'047}{\tt e}\  {\tt (cons}\  {\tt \char'047}{\tt r}\  {\tt empty))}\\
\>{\tt (c}\={\tt ons}\  {\tt (cons}\  {\tt \char'047}{\tt r}\  {\tt (cons}\  {\tt \char'047}{\tt e}\  {\tt empty))}
\\
\>\>{\tt empty))}
\end{tabbing}\end{minipage}\end{center}

\noindent To obtain all possible re-arrangements for the entire list, we
must now insert the first item, {{\tt \char'047}{\tt d}\/} in our case, into all of these
words between all possible letters and at the beginning and end.
\end{enumerate}

The task of inserting a letter into many different words requires
processing an arbitrarily large list. So, we need another function, call it
{{\tt insert-everywhere/in-all-words}\/}, to complete the definition of
{{\tt arrangements}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (arrangements}\  {\tt a-word)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-word)}\  {\tt (cons}\  {\tt empty}\  {\tt empty)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (i}\={\tt nsert-everywhere/in-all-words}\  {\tt (first}\  {\tt a-word)}\  
\\
\>\>\>{\tt (arrangements}\  {\tt (rest}\  {\tt a-word)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

{\bf Exercise 12.4.2}

 \label{ex:permutations}
Develop the function {{\tt insert-everywhere/in-all-words}\/}. It consumes a
symbol and a list of words. The result is a list of words like its second
argument, but with the first argument inserted between all letters and at
the beginning and the end of all words of the second argument.

\noindent{\bf Hint:} \ Re-consider the example from above. We stopped and decided that we
needed to insert {{\tt \char'047}{\tt d}\/} into the words {{\tt (cons}\ {\tt \char'047}{\tt e}\ {\tt (cons}\ {\tt \char'047}{\tt r}{ }{\tt empty))}\/} and {{\tt (cons}\ {\tt \char'047}{\tt r}\ {\tt (cons}\ {\tt \char'047}{\tt e}\ {\tt empty))}\/}. The following is therefore
a natural candidate
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (i}\={\tt nsert-everywhere/in-all-words}\  {\tt \char'047}{\tt d}\\
\>{\tt (c}\={\tt ons}\  {\tt (cons}\  {\tt \char'047}{\tt e}\  {\tt (cons}\  {\tt \char'047}{\tt r}\  {\tt empty))}
\\
\>\>{\tt (c}\={\tt ons}\  {\tt (cons}\  {\tt \char'047}{\tt r}\  {\tt (cons}\  {\tt \char'047}{\tt e}\  {\tt empty))}
\\
\>\>\>{\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
for the ``function examples'' step. Keep in mind that the second input
corresponds to the sequence of (partial) words ``er'' and
``re''. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/permutations.html}{\rule{3pt}{5pt}}

%% BEGIN INLINED FILE: curriculumAb.dl.tex

 \part{{Intermezzo 2: List Abbreviations}}  \section{{Intermezzo 2: List Abbreviations}}   \label{sec:int-list}

\htmladdimg{../icons/teacher.gif}~{\em This section introduces more convenient ways to express lists than {{\tt cons}\/}. In our experience, students have problems if these methods are taught before they understand {{\tt cons}\/} completely. Once they do,  the methods introduced here save a lot of work and start making Scheme  practical.   At the end, it is critical that students understand the difference between {{\tt (list}\ {\tt 1)}\/} and {{\tt (list}\ {\tt 1}\ {\tt empty)}\/}, which are distinct lists. The first contains a single item, {{\tt 1}\/}; the second contains two items: {{\tt 1}\/} and {{\tt empty}\/}.}

Using {{\tt cons}\/} to create lists is rather inconvenient if a list
contains many items. To make our life easier, Scheme provides the
{{\tt list}\/} operation, which consumes an arbitrary number of values and
creates a list. In other words, we extend Scheme's syntactic specification
with a new primitive:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$\mbox{{\tt <{prm}>}}$}\  {\tt =}\  {\tt list}\end{tabbing}\end{minipage}\end{center}
and extend the collection of values with a new clause: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$\mbox{{\tt <{val}>}}$}\  {\tt =}\  {\tt (list}\  \hbox{$\mbox{{\tt <{val}>}}$}\  {\tt ...}\  \hbox{$\mbox{{\tt <{val}>}}$}{\tt )}\end{tabbing}\end{minipage}\end{center}
The notation \mbox{{\tt <{val}>}} \ldots \mbox{{\tt <{val}>}} stands for an arbitrarily
long sequence of values.

A simpler way to understand {{\tt list}\/} expressions is to think
of them as abbreviations. Specifically, every expression of the
shape 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  {\tt exp-1}\  {\tt ...}\  {\tt exp-n)}\end{tabbing}\end{minipage}\end{center}
stands for a series of {\it n\/} {{\tt cons}\/} expressions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  {\tt exp-1}\  {\tt (cons}\  {\tt ...}\  {\tt (cons}\  {\tt exp-n}\  {\tt empty)))}\end{tabbing}\end{minipage}\end{center}
Recall that {{\tt empty}\/} is not an item of the list here, but the
rest of the list after {\it n\/} {{\tt cons}\/}es. Here are three
examples; 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (list}\  {\tt 1}\  {\tt 2)}\\
\>{\tt =}\  {\tt (cons}\  {\tt 1}\  {\tt (cons}\  {\tt 2}\  {\tt empty))}\  
\\
\  \  \={\tt (list}\  {\tt \char'047}{\tt Houston}\  {\tt \char'047}{\tt Dallas}\  {\tt \char'047}{\tt SanAntonio)}
\\
\>{\tt =}\  {\tt (cons}\  {\tt \char'047}{\tt Houston}\  {\tt (cons}\  {\tt \char'047}{\tt Dallas}\  {\tt (cons}\  {\tt \char'047}{\tt SanAntonio}\  {\tt empty)))}\  
\\
\  \  \={\tt (list}\  {\sf false}\  {\sf true}\  {\sf false}\  {\sf false}{\tt )}
\\
\>{\tt =}\  {\tt (cons}\  {\sf false}\  {\tt (cons}\  {\sf true}\  {\tt (cons}\  {\sf false}\  {\tt (cons}\  {\sf false}\  {\tt empty))))}
\end{tabbing}\end{minipage}\end{center}
They introduce lists with two, three, and four items,
respectively. 

Of course, we can apply {{\tt list}\/} not only to values but also
to expressions, but before the list is constructed, the expressions
must be evaluated:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list}\  {\tt (+}\  {\tt 0}\  {\tt 1)}\  {\tt (+}\  {\tt 1}\  {\tt 1))}\\
{\tt =}\  {\tt (list}\  {\tt 1}\  {\tt 2)}
\\
{\tt =}\  {\tt (cons}\  {\tt 1}\  {\tt (cons}\  {\tt 2}\  {\tt empty))}\  
\end{tabbing}\end{minipage}\end{center}
If during the evaluation of an expression an error occurs, the list
is never formed: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list}\  {\tt (/}\  {\tt 1}\  {\tt 0)}\  {\tt (+}\  {\tt 1}\  {\tt 1))}\\
{\tt =}\  {\tt /:}\  {\tt divide}\  {\tt by}\  {\tt zero}
\end{tabbing}\end{minipage}\end{center}
In short, {{\tt list}\/} behaves just like any other primitive operation. 

The use of {{\tt list}\/} greatly simplifies the notation for lists
with many items and lists that contains lists or structures. Here
is an example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list}\  {\tt 0}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5}\  {\tt 6}\  {\tt 7}\  {\tt 8}\  {\tt 9)}\end{tabbing}\end{minipage}\end{center}
This list contains 10 items and its formation with {{\tt cons}\/}
and {{\tt empty}\/} would require 10 uses of {{\tt cons}\/} and one 
instance of {{\tt empty}\/}. Similarly, the list 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list}\  \={\tt (list}\  {\tt \char'047}{\tt bob}\  {\tt 0}\  {\tt \char'047}{\tt a)}\  \\
\>{\tt (list}\  {\tt \char'047}{\tt carl}\  {\tt 1}\  {\tt \char'047}{\tt a)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt dana}\  {\tt 2}\  {\tt \char'047}{\tt b)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt erik}\  {\tt 3}\  {\tt \char'047}{\tt c)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt frank}\  {\tt 4}\  {\tt \char'047}{\tt a)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt grant}\  {\tt 5}\  {\tt \char'047}{\tt b)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt hank}\  {\tt 6}\  {\tt \char'047}{\tt c)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt ian}\  {\tt 8}\  {\tt \char'047}{\tt a)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt john}\  {\tt 7}\  {\tt \char'047}{\tt d)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt karel}\  {\tt 9}\  {\tt \char'047}{\tt e))}
\end{tabbing}\end{minipage}\end{center}
requires 11 uses of {{\tt list}\/} in contrast to 40 of {{\tt cons}\/} and 11
of {{\tt empty}\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 13.0.1}

 \label{ex:list-cons2}
Use {{\tt cons}\/} and {{\tt empty}\/} to construct the equivalent of the
following lists: 
\begin{enumerate}
\item {{\tt (list}\ {\tt 0}\ {\tt 1}\ {\tt 2}\ {\tt 3}\ {\tt 4}\ {\tt 5)}\/}
\item {{\tt (list}\ {\tt (list}\ {\tt \char'047}{\tt adam}\ {\tt 0)}\ {\tt (list}\ {\tt \char'047}{\tt eve}\ {\tt 1)}\ {\tt (list}\ {\tt \char'047}{\tt louisXIV}\ {\tt 2))}\/}
\item {{\tt (list}\ {\tt 1}\ {\tt (list}\ {\tt 1}\ {\tt 2)}\ {\tt (list}\ {\tt 1}\ {\tt 2}\ {\tt 3))}\/}.
\end{enumerate}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list-cons2.html}{\rule{3pt}{5pt}} 


{\bf Exercise 13.0.2}

 \label{ex:list-cons3}
Use {{\tt list}\/} to construct the equivalent of the following lists: 
\begin{enumerate}
\item {{\tt (cons}\ {\tt \char'047}{\tt a}\ {\tt (cons}\ {\tt \char'047}{\tt b}\ {\tt (cons}\ {\tt \char'047}{\tt c}\ {\tt (cons}\ {\tt \char'047}{\tt d}\ {\tt (cons}\ {\tt \char'047}{\tt e}\ {\tt empty)))))}\/}
\item {{\tt (cons}\ {\tt (cons}\ {\tt 1}\ {\tt (cons}\ {\tt 2}\ {\tt empty))}\ {\tt empty)}\/}
\item {{\tt (cons}\ {\tt \char'047}{\tt a}\ {\tt (cons}\ {\tt (cons}\ {\tt 1}\ {\tt empty)}\ {\tt (cons}\ {\sf false}\ {\tt empty)))}\/}.
\item {{\tt (cons}\ {\tt (cons}\ {\tt 1}\ {\tt (cons}\ {\tt 2}\ {\tt empty))}\ {\tt (cons}\ {\tt (cons}\ {\tt 2}\ {\tt (cons}\ {\tt 3}\ {\tt empty))}\ {\tt empty))}\/}
\end{enumerate}
Start by determining how many items each list and each nested list
contains. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list-cons3.html}{\rule{3pt}{5pt}} 


{\bf Exercise 13.0.3}

 \label{ex:list-cons4}
On rare occasions, it is necessary we may encounter lists formed with
{{\tt cons}\/} and {{\tt list}\/}. Reformulate the following lists using
{{\tt cons}\/} and {{\tt empty}\/} exclusively: 
\begin{enumerate}
\item {{\tt (cons}\ {\tt \char'047}{\tt a}\ {\tt (list}\ {\tt 0}\ {\sf false}{\tt ))}\/}
\item {{\tt (list}\ {\tt (cons}\ {\tt 1}\ {\tt (cons}\ {\tt 13}\ {\tt empty)))}\/}
\item {{\tt (list}\ {\tt empty}\ {\tt empty}\ {\tt (cons}\ {\tt 1}\ {\tt empty))}\/}
\item {{\tt (cons}\ {\tt \char'047}{\tt a}\ {\tt (cons}\ {\tt (list}\ {\tt 1)}\ {\tt (list}\ {\sf false}\ {\tt empty)))}\/}.
\end{enumerate}
Now formulate the lists using {{\tt list}\/}. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list-cons4.html}{\rule{3pt}{5pt}}


{\bf Exercise 13.0.4}

 \label{ex:list-cons1}
Determine the values of the following expressions: 
\begin{enumerate}
\item {{\tt (list}\ {\tt (symbol=?}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt b)}\ {\tt (symbol=?}\ {\tt \char'047}{\tt c}\ {\tt \char'047}{\tt c)}\ {\sf false}{\tt )}\/}
\item {{\tt (list}\ {\tt (+}\ {\tt 10}\ {\tt 20)}\ {\tt (*}\ {\tt 10}\ {\tt 20)}\ {\tt (/}\ {\tt 10}\ {\tt 20))}\/}
\item {{\tt (list}\ {\tt \char'047}{\tt dana}\ {\tt \char'047}{\tt jane}\ {\tt \char'047}{\tt mary}\ {\tt \char'047}{\tt laura)}\/}
\end{enumerate}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list-cons1.html}{\rule{3pt}{5pt}}


{\bf Exercise 13.0.5}

 \label{ex:list-cons5}
Determine the values of 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (first}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3))}\\
{\tt (rest}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3))}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list-cons5.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}



The use of {{\tt list}\/} makes it significantly easier to evaluate
expressions involving lists. Here are the recursive steps from an example
from section~\ref{sec:lists:more}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (sum}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt robot}\  {\tt 22.05)}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 17.95)))}\\
{\tt =}\  {\tt (+}\  \={\tt (ir-price}\  {\tt (first}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt robot}\  {\tt 22.05)}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 17.95))))}
\\
\>{\tt (sum}\  {\tt (rest}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt robot}\  {\tt 22.05)}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 17.95)))))}
\\
{\tt =}\  {\tt (+}\  \={\tt (ir-price}\  {\tt (make-ir}\  {\tt \char'047}{\tt robot}\  {\tt 22.05))}
\\
\>{\tt (sum}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 17.95))))}
\\
\hbox{;At this place, we use one of the equations governing }
\\
\hbox{;the new primitive operations for the first time: }
\\
{\tt =}\  {\tt (+}\  \={\tt 22.05}
\\
\>{\tt (sum}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 17.95))))}
\\
{\tt =}\  {\tt (+}\  \={\tt 22.05}
\\
\>{\tt (+}\  \={\tt (ir-price}\  {\tt (first}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 17.95))))}
\\
\>\>{\tt (sum}\  {\tt (rest}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 17.95))))))}
\\
{\tt =}\  {\tt (+}\  \={\tt 22.05}
\\
\>{\tt (+}\  \={\tt (ir-price}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 17.95))}
\\
\>\>{\tt (sum}\  {\tt empty)))}
\\
{\tt =}\  {\tt (+}\  {\tt 22.05}\  {\tt (+}\  {\tt 17.95}\  {\tt (sum}\  {\tt empty)))}
\\
{\tt =}\  {\tt (+}\  {\tt 22.05}\  {\tt (+}\  {\tt 17.95}\  {\tt 0))}
\end{tabbing}\end{minipage}\end{center}
Since the laws of {{\tt first}\/} and {{\tt rest}\/} carry over to
{{\tt list}\/} values in a natural manner, an evaluation using {{\tt list}\/}
does not need to expand {{\tt list}\/} into uses of {{\tt cons}\/} and
{{\tt empty}\/}. 


Following an old programming language convention,\footnote{The convention is due to LISP, an early but highly advanced programming language, designed in 1958. Scheme inherited many ideas from LISP, but it is really a separate language.} we may abbreviate lists and symbols even further. If a list is
formulated with {{\tt list}\/}, we can simply agree to drop list and that
each opening parenthesis stands for itself and the word {{\tt list}\/}. For
example,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt \char'047}{\tt (1}\  {\tt 2}\  {\tt 3)}\\
\hbox{;abbreviates }
\\
{\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3)}
\\
\hbox{;or}
\\
{\tt (cons}\  {\tt 1}\  {\tt (cons}\  {\tt 2}\  {\tt (cons}\  {\tt 3}\  {\tt empty)))}\  {\tt .}
\end{tabbing}\end{minipage}\end{center}
Similarly, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt \char'047}{\tt ((1}\  {\tt 2)}\  {\tt (3}\  {\tt 4)}\  {\tt (5}\  {\tt 6))}\\
\hbox{;stands for}
\\
{\tt (list}\  {\tt (list}\  {\tt 1}\  {\tt 2)}\  {\tt (list}\  {\tt 3}\  {\tt 4)}\  {\tt (list}\  {\tt 5}\  {\tt 6))\char'054}
\end{tabbing}\end{minipage}\end{center}
which can be further expanded into {{\tt cons}\/} and {{\tt empty}\/}
expressions. 

If we drop quotes in front of symbols and restore them where necessary,
abbreviating lists of symbols becomes a breeze: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt \char'047}{\tt (a}\  {\tt b}\  {\tt c)}\\
\hbox{;is an abbreviation for}
\\
{\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)}
\end{tabbing}\end{minipage}\end{center}
and 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt \char'047}{\tt (<}\={\tt html>}\  \\
\>{\tt (<title>}\  {\tt My}\  {\tt First}\  {\tt Web}\  {\tt Page)}
\\
\>{\tt (<body>}\  {\tt Oh!))}
\\
\hbox{;means}
\\
{\tt (l}\={\tt ist}\  {\tt \char'047}{\tt <html>}\  
\\
\>{\tt (list}\  {\tt \char'047}{\tt <title>}\  {\tt \char'047}{\tt My}\  {\tt \char'047}{\tt First}\  {\tt \char'047}{\tt Web}\  {\tt \char'047}{\tt Page)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt <body>}\  {\tt \char'047}{\tt Oh!))}\  {\tt .}
\end{tabbing}\end{minipage}\end{center}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 13.0.6}

 \label{ex:quote-list}
Restore {{\tt list}\/} and quotes where necessary: 
\begin{enumerate}
\item \quad\\
\begin{minipage}[t]{8in}\begin{tabbing}{\tt \char'047}{\tt (1}\  {\tt a}\  {\tt 2}\  {\tt b}\  {\tt 3}\  {\tt c)}\end{tabbing}\end{minipage}

\item \quad\\
\begin{minipage}[t]{8in}\begin{tabbing}{\tt \char'047}{\tt (}\={\tt (alan}\  {\tt 1000)}\\
\>{\tt (barb}\  {\tt 2000)}
\\
\>{\tt (carl}\  {\tt 1500)}
\\
\>{\tt (dawn}\  {\tt 2300))}
\end{tabbing}\end{minipage}

\item \quad\\
\begin{minipage}[t]{8in}\begin{tabbing}{\tt \char'047}{\tt (}\={\tt (My}\  {\tt First}\  {\tt Paper)}\\
\>{\tt (Sean}\  {\tt Fisler)}
\\
\>{\tt (S}\={\tt ection}\  {\tt 1}\  
\\
\>\>{\tt (Subsection}\  {\tt 1}\  {\tt Life}\  {\tt is}\  {\tt difficult)}
\\
\>\>{\tt (Subsection}\  {\tt 2}\  {\tt But}\  {\tt learning}\  {\tt things}\  {\tt makes}\  {\tt it}\  {\tt interesting))}
\\
\>{\tt (S}\={\tt ection}\  {\tt 2}
\\
\>\>{\tt Conclusion\char'077}\  {\tt What}\  {\tt conclusion\char'077}{\tt ))}\  \  
\end{tabbing}\end{minipage}
\end{enumerate}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/quote-list.html}{\rule{3pt}{5pt}}







%% BEGIN INLINED FILE: curriculum2.dl.tex


\newpage\part{More on Processing Arbitrarily Large Data}\thispagestyle{empty}\newpage \label{part:advanced}

\section{More Self-referential Data Definitions}

\htmladdimg{../icons/teacher.gif}~{\em This part introduces students to several interesting and important variations of the design recipe for recursive data definitions. It also illustrates how the design recipe generalizes to mutually referential data definitions. Finally, it covers functions that consume two (or more) complex arguments. This part is critical for understanding data structures.}

Lists and natural numbers are two classes of data whose description
requires self-referential data definitions. Both data definitions consist
of two clauses; both have a single self-reference. Many interesting classes
of data, however, require more complex definitions than that. Indeed, there
is no end to the variations. It is therefore necessary to learn how to
formulate data definitions on our own, starting with informal descriptions
of information. Once we have those, we can just follow a slightly modified
design recipe for self-referential data definitions.

\subsection{Structures in Structures} \label{sec:struct-in-struct} 

Medical researchers rely on family trees to do research on hereditary
diseases. They may, for example, search a family tree for a certain eye
color. Computers can help with these tasks, so it is natural to design
representations of family trees and functions for processing them.

One way to maintain a family tree of a family is to add a node to the tree
every time a child is born. From the node, we can draw connections to the
node for the father and the one for the mother, which tells us how the
people in the tree are related. For those people in the tree whose parents
are unknown, we do not draw any connections. The result is a so-called
ancestor tree because given any node in the tree, we can find the ancestors
of that person if we follow the arrows but not the descendants. 

As we record a family tree, we may also want to record certain pieces of
information. The birth date, birth weight, the color of the eyes, and the
color of the hair are the pieces of information that we care about. Others
record different information. 

\hrule \begin{picture}(300,200)(0,+20)
\put(000,180){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Carl~~(1926)}} 
\put(00,05){\mbox{~Eyes:~~ green}} \end{picture}}
\put(100,180){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Bettina~~(1926)}} 
\put(00,05){\mbox{~Eyes:~~ green}} \end{picture}} 
\put(000,100){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Adam~~(1950)}} 
\put(00,05){\mbox{~Eyes:~~ yellow}} \end{picture}}
  
\put(035,130){\vector(0,1){50}}              
\put(035,130){\vector(2,1){100}}
\put(100,100){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Dave~~(1955)}} 
\put(00,05){\mbox{~Eyes:~~ black}} \end{picture}}      
\put(135,130){\vector(-2,1){100}}            
\put(135,130){\vector(+0,1){50}}
\put(200,100){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Eva~~(1965)}} 
\put(00,05){\mbox{~Eyes:~~ blue}} \end{picture}}
  
\put(235,130){\vector(-2,1){100}}
  
\put(235,130){\vector(-4,1){200}}
\put(290,100){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Fred~~(1966)}} 
\put(00,05){\mbox{~Eyes:~~ pink}} \end{picture}}
\put(255,020){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Gustav~~(1988)}} 
\put(00,05){\mbox{~Eyes:~~ blush}} \end{picture}}   
\put(290,050){\vector(-1,1){50}}
  
\put(290,050){\vector(+1,1){50}}
\end{picture}

\center{Figure: A sample ancestor family tree}\label{fig:familyA}
\hrule 

See figure~\ref{fig:familyA} for a drawing of an ancestor family tree. Adam
is the child of Bettina and Carl; he has yellow eyes and was born in
1950. Similarly, Gustav is the child of Eva and Fred, has blush eyes, and
was born in 1988. To represent a child in a family tree means to combine
several pieces of information: information about the father, the mother,
the name, the birth date, and the eye colors. This suggests that we define
a new structure: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt child}\  {\tt (father}\  {\tt mother}\  {\tt name}\  {\tt date}\  {\tt eyes))}\end{tabbing}\end{minipage}\end{center}
The five fields of {{\tt child}\/} structures record the required
information, which suggests the following data definition:
$$\mbox{{\begin{minipage}{4.5in}   A {{\tt child}\/} is a structure:\\
   \centerline{{{\tt (make-child}\ {\tt f}\ {\tt m}\ {\tt na}\ {\tt da}\ {\tt ec)}\/}}   where {{\tt f}\/} and {{\tt m}\/} are {{\tt child}\/} structures; {{\tt na}\/}   and {{\tt ec}\/} are symbols; and {{\tt da}\/} is a number. \end{minipage}}}$$
While this data definition is simple, it is unfortunately also useless. The
definition refers to itself but, because it doesn't have any clauses, there
is no way to create a {{\tt child}\/} structure. If we tried to create a
{{\tt child}\/} structure, we would have to write
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (m}\={\tt ake-child}\  \\
\>{\tt (}\={\tt m}\={\tt ake-child}
\\
\>\>\>{\tt (m}\={\tt ake-child}\  
\\
\>\>\>\>{\tt (m}\={\tt ake-child}\  
\\
\>\>\>\>\>{\tt ...}\  
\\
\>\>\>\>\>{\tt )))}
\\
\>\>{\tt ...}\  {\tt ...}\  {\tt ...}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
without end. It is for this reason that we demand that all self-referential
data definitions consist of several clauses (for now) and that at least one
of them does not refer to the data definition.

Let's postpone the data definition for a moment and let's instead study how
we can use {{\tt child}\/} structures to represent family trees.  Suppose we
are about to add a child to an existing family tree, and furthermore
suppose that we already have representations for the parents. Then we can
just construct a new {{\tt child}\/} structure. For example, for Adam we
could create the following {{\tt child}\/} structure
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-child}\  {\tt Carl}\  {\tt Bettina}\  {\tt \char'047}{\tt Adam}\  {\tt 1950}\  {\tt \char'047}{\tt yellow)}\end{tabbing}\end{minipage}\end{center}
assuming {{\tt Carl}\/} and {{\tt Bettina}\/} stand for representations of
Adam's parents. 

The problem is that we don't always know a person's parents.  In the family
depicted in figure~\ref{fig:familyA}, we don't know Bettina's parents.
Yet, even if we don't know a person's father or mother, we must still use
some Scheme value for the two fields in a {{\tt child}\/} structure.  We
could use all kinds of values to signal a lack of information ({{\tt 5}\/},
{{\tt false}\/}, or {{\tt \char'047}{\tt none}\/}); here, we use {{\tt empty}\/}.  For
example, to construct a {{\tt child}\/} structure for Bettina, we do the
following:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Bettina}\  {\tt 1950}\  {\tt \char'047}{\tt green)}\end{tabbing}\end{minipage}\end{center}
Of course, if only one of the two parents is missing, we just fill that
field with {{\tt empty}\/}.

Our analysis suggests that a {{\tt child}\/} node has the following data
definition: 
\begin{quote}
 {A {\em child node\/} is {{\tt (make-child}\ {\tt f}\ {\tt m}\ {\tt na}\ {\tt da}\ {\tt ec)}\/} where   \begin{enumerate}    \item {{\tt f}\/} and {{\tt m}\/} are either     \begin{enumerate}      \item {{\tt empty}\/} or       \item  {{\tt child}\/} nodes;     \end{enumerate}    \item {{\tt na}\/} and {{\tt ec}\/} are symbols,     \item {{\tt da}\/} is a number.   \end{enumerate}}
\end{quote}
This definition is special in two regards.  First, it is a self-referential
data definition involving structures. Second, the data definition mentions
two alternatives for the first and second component. This violates our
conventions concerning on the shape of data definitions.

We can avoid this problem by defining the collection of nodes in a family
tree instead: 

  \begin{quote} A {\sl {family tree node}} (short: {{\tt ftn}\/}) is either      \begin{enumerate}     \item {{\tt empty}\/}, or     \item {{\tt (make-child}\ {\tt f}\ {\tt m}\ {\tt na}\ {\tt da}\ {\tt ec)}\/} \\
            where {{\tt f}\/} and {{\tt m}\/} are {\em ftn\/}s, {{\tt na}\/}\\
             and {{\tt ec}\/} are symbols, and {\em da\/} is a number.      \end{enumerate} \end{quote}

\noindent This new definition satisfies our conventions. It consists of two
clauses. One of the clauses is self-referential, the other is not. 

In contrast to previous data definitions involving structures, the
definition of {{\tt ftn}\/} is not a plain explanation of what kind of data
can show up in which field. Instead, it is multi-clausal and
self-referential. Considering that this is the first such data definition,
let us carefully translate the example from figure~\ref{fig:familyA} and
thus reassure ourselves that the new class of data can represent the
information of interest. 

\hrule \begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; Oldest Generation:}\\
{\tt (define}\  {\tt Carl}\  {\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Carl}\  {\tt 1926}\  {\tt \char'047}{\tt green))}
\\
{\tt (define}\  {\tt Bettina}\  {\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Bettina}\  {\tt 1926}\  {\tt \char'047}{\tt green))}
\\
\hbox{;; Middle Generation:}
\\
{\tt (define}\  {\tt Adam}\  {\tt (make-child}\  {\tt Carl}\  {\tt Bettina}\  {\tt \char'047}{\tt Adam}\  {\tt 1950}\  {\tt \char'047}{\tt yellow))}
\\
{\tt (define}\  {\tt Dave}\  {\tt (make-child}\  {\tt Carl}\  {\tt Bettina}\  {\tt \char'047}{\tt Dave}\  {\tt 1955}\  {\tt \char'047}{\tt black))}
\\
{\tt (define}\  {\tt Eva}\  {\tt (make-child}\  {\tt Carl}\  {\tt Bettina}\  {\tt \char'047}{\tt Eva}\  {\tt 1965}\  {\tt \char'047}{\tt blue))}
\\
{\tt (define}\  {\tt Fred}\  {\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Fred}\  {\tt 1966}\  {\tt \char'047}{\tt pink))}
\\
\hbox{;; Youngest Generation: }
\\
{\tt (define}\  {\tt Gustav}\  {\tt (make-child}\  {\tt Fred}\  {\tt Eva}\  {\tt \char'047}{\tt Gustav}\  {\tt 1988}\  {\tt \char'047}{\tt blush))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: A Scheme representation of the sample family tree} \label{fig:familyS}
\hrule

The information for Carl is easy to translate into a {{\tt ftn}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Carl}\  {\tt 1926}\  {\tt \char'047}{\tt green)}\end{tabbing}\end{minipage}\end{center}
Bettina and Fred are represented with similar nodes. Accordingly, the node
for Adam is created with 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-child}\  \={\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Carl}\  {\tt 1926}\  {\tt \char'047}{\tt green)}\  \\
\>{\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Bettina}\  {\tt 1950}\  {\tt \char'047}{\tt green)}
\\
\>{\tt \char'047}{\tt Adam}\  
\\
\>{\tt 1950}\  
\\
\>{\tt \char'047}{\tt yellow)}
\end{tabbing}\end{minipage}\end{center}

As the examples show, a simple-minded, node-by-node transliteration of
figure~\ref{fig:familyA} requires numerous repetitions of data. For
example, if we constructed the {{\tt child}\/} structure for Dave like the
one for Adam, we would get
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-child}\  \={\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Carl}\  {\tt 1926}\  {\tt \char'047}{\tt green)}\  \\
\>{\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Bettina}\  {\tt 1950}\  {\tt \char'047}{\tt green)}
\\
\>{\tt \char'047}{\tt Dave}
\\
\>{\tt 1955}
\\
\>{\tt \char'047}{\tt black)}
\end{tabbing}\end{minipage}\end{center}
Hence, it is a good idea to introduce a variable definition per node and to
use the variable thereafter. To make things easy, we use {{\tt Carl}\/} to
stand for the {{\tt child}\/} structure that describes Carl, and so on. 
The complete transliteration of the family tree into Scheme can be found in
figure~\ref{fig:familyS}. 

The structure definitions in figure~\ref{fig:familyS} naturally correspond
to an image of deeply nested boxes. Each box has five compartments. The
first two contain boxes again, which in turn contain boxes in their first
two compartments and so on. Thus, if we were to draw the structure
definitions for the family tree using nested boxes, we would quickly be
overwhelmed by the details of the picture. Furthermore, the picture would
copy certain portions of the tree just like our attempt to use
{{\tt make-child}\/} without variable definitions.  For these reasons, it is
better to imagine the structures as boxes and arrows, as originally drawn
in figure~\ref{fig:familyA}. In general, a programmer must flexibly switch
back and forth between both of these graphical illustrations. For
extracting values from structures, the boxes-in-boxes image works best; for
finding our way around large collections of interconnected structures, the
boxes-and-arrows image works better.


Equipped with a firm understanding of the family tree representation, we
can turn to the design of functions that consume family trees. Let us first
look at a generic function of this kind:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt fun-for-ftn}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt \char'077}{\tt \char'077}{\tt \char'077}\/}}\\
{\tt (define}\  {\tt (fun-for-ftn}\  {\tt a-ftree)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
After all, we should be able to construct the template without considering
the purpose of a function.

Since the data definition for {{\tt ftn}\/}s contains two clauses, the
template must consist of a {{\bf cond}-exp\-res\-sion} with two clauses. The
first deals with {{\tt empty}\/}, the second one with {{\tt child}\/}
structures:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt fun-for-ftn}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt \char'077}{\tt \char'077}{\tt \char'077}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (fun-for-ftn}\  {\tt a-ftree)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-ftree)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}\  \hbox{; {{\tt (child?}\ {\tt a-ftree)}\/}}
\\
\>\>\>{\tt ...}\  {\tt \char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Furthermore, for the first clause, the input is atomic so there is nothing
further to be done. For the second clause, though, the input contains five
pieces of information: two other family tree nodes, the person's name,
birth date, and eye color:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt fun-for-ftn}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt \char'077}{\tt \char'077}{\tt \char'077}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (fun-for-ftn}\  {\tt a-ftree)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-ftree)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt ...}\  {\tt (fun-for-ftn}\  {\tt (child-father}\  {\tt a-ftree))}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (fun-for-ftn}\  {\tt (child-mother}\  {\tt a-ftree))}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (child-name}\  {\tt a-ftree)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (child-date}\  {\tt a-ftree)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (child-eyes}\  {\tt a-ftree)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
We also apply {{\tt fun-for-ftn}\/} to the {{\tt father}\/} and
{{\tt mother}\/} fields because of the self-references in the second clause
of the data definition. 

Let us now turn to a concrete example: {{\tt blue-eyed-ancestor\char'077}\/}, the
function that determines whether anyone in some given family tree has blue
eyes:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt blue-eyed-ancestor\char'077}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt a-ftree}\/} contains a {{\tt child}\/}}
\\
\hbox{;; structure with {{\tt \char'047}{\tt blue}\/} in the {{\tt eyes}\/} field}
\\
{\tt (define}\  {\tt (blue-eyed-ancestor\char'077}\  {\tt a-ftree)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center} 

Following our recipe, we first develop some examples. Consider the family
tree node for Carl. He does not have blue eyes, and because he doesn't have
any (known) ancestors in our family tree, the family tree represented by
this node does not contain a person with blue eyes. In short,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (blue-eyed-ancestor\char'077}\  {\tt Carl)}\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
In contrast, the family tree represented by {{\tt Gustav}\/} contains a node
for Eva who does have blue eyes. Hence, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (blue-eyed-ancestor\char'077}\  {\tt Gustav)}\\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}

The function template is like that of {{\tt fun-for-ftn}\/}, except that we
use the name {{\tt blue-eyed-ancestor\char'077}\/}. As always, we use the template
to guide the function design. First we assume that {{\tt (empty?}\ {\tt a-ftree)}\/}
holds. In that case, the family tree is empty, and nobody has blue
eyes. Hence, the answer must be {{\sf false}\/}. 

The second clause of the template contains several expressions, which we
must interpret:
\begin{enumerate}
 \item {{\tt (blue-eyed-ancestor\char'077}\ {\tt (child-father}\ {\tt a-ftree))}\/}, which determines
    whether someone in the father's {{\tt ftn}\/} has blue eyes; 
 \item {{\tt (blue-eyed-ancestor\char'077}\ {\tt (child-mother}\ {\tt a-ftree))}\/}, which determines
    whether someone in the mother's {{\tt ftn}\/} has blue eyes; 
 \item {{\tt (child-name}\ {\tt a-ftree)}\/}, which extracts the {{\tt child}\/}'s name; 
 \item {{\tt (child-date}\ {\tt a-ftree)}\/}, which extracts the
    {{\tt child}\/}'s date of birth; and 
 \item {{\tt (child-eyes}\ {\tt a-ftree)}\/},  which extracts the
    {{\tt child}\/}'s eye colors. 
\end{enumerate}
It is now up to us to use these values properly. Clearly, if the
{{\tt child}\/} structure contains {{\tt \char'047}{\tt blue}\/} in the {{\tt eyes}\/}
field, the function's answer is {{\sf true}\/}. Otherwise, the function produces
{{\sf true}\/} if there is a blue-eyed person in either the father's or the
mother's family tree. The rest of the data is useless. 

Our discussion suggests that we formulate a conditional expression and that
the first condition is
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (symbol=?}\  {\tt (child-eyes}\  {\tt a-ftree)}\  {\tt \char'047}{\tt blue)}\end{tabbing}\end{minipage}\end{center}
The two recursions are the other two conditions. If either one produces
{{\sf true}\/}, the function produces {{\sf true}\/}. The {{\tt else}\/}-clause 
produces {{\sf false}\/}. 

In summary, the answer in the second clause is the following expression:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (symbol=?}\  {\tt (child-eyes}\  {\tt a-ftree)}\  {\tt \char'047}{\tt blue)}\  {\sf true}{\tt \char'135}
\\
\>{\tt \char'133}{\tt (blue-eyed-ancestor\char'077}\  {\tt (child-father}\  {\tt a-ftree))}\  {\sf true}{\tt \char'135}
\\
\>{\tt \char'133}{\tt (blue-eyed-ancestor\char'077}\  {\tt (child-mother}\  {\tt a-ftree))}\  {\sf true}{\tt \char'135}
\\
\>{\tt \char'133}{\tt else}\  {\sf false}{\tt \char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
The first definition in figure~\ref{fig:blue-eyes} pulls everything
together. The second definition shows how to formulate this
{{\bf cond}-exp\-res\-sion} as an equivalent {{\bf or}-exp\-res\-sion}, testing
one condition after the next, until one of them is {{\sf true}\/} or all of
them have evaluated to {{\sf false}\/}. 


The function {{\tt blue-eyed-ancestor\char'077}\/} is unusual in that it uses the
recursions as conditions in a {{\bf cond}-exp\-res\-sion}s. To understand how
this works, let us evaluate an application of {{\tt blue-eyed-ancestor\char'077}\/}
to {{\tt Carl}\/} by hand:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (blue-eyed-ancestor\char'077}\  {\tt Carl)}\\
{\tt =}\  {\tt (blue-eyed-ancestor\char'077}\  {\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Carl}\  {\tt 1926}\  {\tt \char'047}{\tt green))}
\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (empty?}\  {\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Carl}\  {\tt 1926}\  {\tt \char'047}{\tt green))}\  {\sf false}{\tt \char'135}
\\
\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (s}\={\tt ymbol=?}\  
\\
\>\>\>\>\>{\tt (child-eyes}\  {\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Carl}\  {\tt 1926}\  {\tt \char'047}{\tt green))}
\\
\>\>\>\>\>{\tt \char'047}{\tt blue)}
\\
\>\>\>\>{\sf true}{\tt \char'135}
\\
\>\>\>{\tt \char'133}\={\tt (b}\={\tt lue-eyed-ancestor\char'077}
\\
\>\>\>\>\>{\tt (child-father}\  {\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Carl}\  {\tt 1926}\  {\tt \char'047}{\tt green)))}
\\
\>\>\>\>{\sf true}{\tt \char'135}
\\
\>\>\>{\tt \char'133}\={\tt (b}\={\tt lue-eyed-ancestor\char'077}
\\
\>\>\>\>\>{\tt (child-mother}\  {\tt (make-child}\  {\tt empty}\  {\tt empty}\  {\tt \char'047}{\tt Carl}\  {\tt 1926}\  {\tt \char'047}{\tt green)))}
\\
\>\>\>\>{\sf true}{\tt \char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\sf false}{\tt \char'135}{\tt )\char'135}{\tt )}
\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt green}\  {\tt \char'047}{\tt blue)}\  {\sf true}{\tt \char'135}
\\
\>{\tt \char'133}{\tt (blue-eyed-ancestor\char'077}\  {\tt empty)}\  {\sf true}{\tt \char'135}
\\
\>{\tt \char'133}{\tt (blue-eyed-ancestor\char'077}\  {\tt empty)}\  {\sf true}{\tt \char'135}
\\
\>{\tt \char'133}{\tt else}\  {\sf false}{\tt \char'135}{\tt )}
\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\sf false}\  {\sf true}{\tt \char'135}
\\
\>{\tt \char'133}{\sf false}\  {\sf true}{\tt \char'135}
\\
\>{\tt \char'133}{\sf false}\  {\sf true}{\tt \char'135}
\\
\>{\tt \char'133}{\tt else}\  {\sf false}{\tt \char'135}{\tt )}
\\
{\tt =}\  {\sf false}\  
\end{tabbing}\end{minipage}\end{center}
The evaluation confirms that {{\tt blue-eyed-ancestor\char'077}\/} works properly
for {{\tt Carl}\/}, and it also illustrates how the function works. 


\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt blue-eyed-ancestor\char'077}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt a-ftree}\/} contains a {{\tt child}\/}}
\\
\hbox{;; structure with {{\tt \char'047}{\tt blue}\/} in the {{\tt eyes}\/} field}
\\
\hbox{;; version 1: using a nested {{\bf cond}-exp\-res\-sion}}
\\
{\tt (d}\={\tt efine}\  {\tt (blue-eyed-ancestor\char'077}\  {\tt a-ftree)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-ftree)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt (child-eyes}\  {\tt a-ftree)}\  {\tt \char'047}{\tt blue)}\  {\sf true}{\tt \char'135}
\\
\>\>\>{\tt \char'133}{\tt (blue-eyed-ancestor\char'077}\  {\tt (child-father}\  {\tt a-ftree))}\  {\sf true}{\tt \char'135}
\\
\>\>\>{\tt \char'133}{\tt (blue-eyed-ancestor\char'077}\  {\tt (child-mother}\  {\tt a-ftree))}\  {\sf true}{\tt \char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\sf false}{\tt \char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\hrulefill

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt blue-eyed-ancestor\char'077}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt a-ftree}\/} contains a {{\tt child}\/}}
\\
\hbox{;; structure with {{\tt \char'047}{\tt blue}\/} in the {{\tt eyes}\/} field}
\\
\hbox{;; version 2: using an {{\bf or}-exp\-res\-sion}}
\\
{\tt (d}\={\tt efine}\  {\tt (blue-eyed-ancestor\char'077}\  {\tt a-ftree)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-ftree)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (or}\  \={\tt (symbol=?}\  {\tt (child-eyes}\  {\tt a-ftree)}\  {\tt \char'047}{\tt blue)}
\\
\>\>\>{\tt (or}\  \={\tt (blue-eyed-ancestor\char'077}\  {\tt (child-father}\  {\tt a-ftree))}
\\
\>\>\>\>{\tt (blue-eyed-ancestor\char'077}\  {\tt (child-mother}\  {\tt a-ftree))))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Two functions for finding a blue-eyed ancestor}\label{fig:blue-eyes}
\hrule

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 14.1.1}

 \label{ex:ftn-or}
The second definition of {{\tt blue-eyed-ancestor\char'077}\/}  in
 figure~\ref{fig:blue-eyes} uses an {{\bf or}-exp\-res\-sion} instead of a
 nested conditional.  Use a hand-evaluation to show that this definition
 produces the same output for the inputs {{\tt empty}\/} and
 {{\tt Carl}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ftn-or.html}{\rule{3pt}{5pt}}


{\bf Exercise 14.1.2}

 \label{ex:bep-eval}
Confirm that 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (blue-eyed-ancestor\char'077}\  {\tt empty)}\  {\tt =}\  {\sf false}\  \end{tabbing}\end{minipage}\end{center}
with a hand-evaluation. 

Evaluate {{\tt (blue-eyed-ancestor\char'077}\ {\tt Gustav)}\/} by hand and with
DrScheme. For the hand-evaluation, skip those steps in the evaluation that
concern extractions, comparisons, and conditions involving
{{\tt empty?}\/}. Also reuse established equations where possible,
especially the one above.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/bep-eval.html}{\rule{3pt}{5pt}}


{\bf Exercise 14.1.3}

 \label{ex:count-ft}
Develop {{\tt count-persons}\/}.  The function consumes a family tree node
and produces the number of people in the corresponding family
tree.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/count-ft.html}{\rule{3pt}{5pt}}


{\bf Exercise 14.1.4}

 \label{ex:age-ft}
Develop the function {{\tt average-age}\/}. It consumes a family tree node
and the current year. It produces the average age of all people in the
family tree.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/age-ft.html}{\rule{3pt}{5pt}}


{\bf Exercise 14.1.5}

 \label{ex:ec-ft}
Develop the function {{\tt eye-colors}\/}, which consumes a family tree node
and produces a list of all eye colors in the tree. An eye color may occur
more than once in the list.

\noindent{\bf Hint:} \ Use the Scheme operation {{\tt append}\/}, which consumes two lists
and produces the concatenation of the two lists:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (append}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)}\  {\tt (list}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e))}\  \\
{\tt =}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e)}
\end{tabbing}\end{minipage}\end{center}
We discuss the development of functions like {{\tt append}\/} in
section~\ref{sec:simu}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ec-ft.html}{\rule{3pt}{5pt}}


{\bf Exercise 14.1.6}

 \label{ex:proper-bea}
Suppose we need the function {{\tt proper-blue-eyed-ancestor\char'077}\/}.  It is
like {{\tt blue-eyed-ancestor\char'077}\/} but only responds with {{\sf true}\/} when
some proper ancestor, not the given one, has blue eyes.

The contract for this new function is basically the same as for the old one:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt proper-blue-eyed-ancestor\char'077}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt a-ftree}\/} has a blue-eyed ancestor}
\\
{\tt (define}\  {\tt (proper-blue-eyed-ancestor\char'077}\  {\tt a-ftree)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

To appreciate the difference, we need to look at Eva, who is blue-eyed,
but does not have a blue-eyed ancestor. Hence,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (blue-eyed-ancestor\char'077}\  {\tt Eva)}\\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
but
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (proper-blue-eyed-ancestor\char'077}\  {\tt Eva)}\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
After all {{\tt Eva}\/} is not a proper ancestor of herself. 

Suppose a friend sees the purpose statement and comes up with this solution:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (proper-blue-eyed-ancestor\char'077}\  {\tt a-ftree)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-ftree)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (or}\  \={\tt (proper-blue-eyed-ancestor\char'077}\  {\tt (child-father}\  {\tt a-ftree))}
\\
\>\>\>{\tt (proper-blue-eyed-ancestor\char'077}\  {\tt (child-mother}\  {\tt a-ftree)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
What would be the result of {{\tt (proper-blue-eyed-ancestor\char'077}\ {\tt A)}\/} for {\em any\/} {{\tt ftn}\/} {{\tt A}\/}? 

\noindent Fix the friend's solution.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/proper-bea.html}{\rule{3pt}{5pt}}


\subsection{Extended Exercise: Binary Search Trees, Part 1} \label{sec:bst}

Programmers often work with trees, though rarely with family trees. A
particularly well-known form of tree is the {binary search tree}.  Many
applications employ binary search trees to store and to retrieve
information.

To be concrete, we discuss binary trees that manage information about
people. In this context, a binary tree is similar to a family tree but
instead of {{\tt child}\/} structures it contains {{\tt node}\/}s:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt node}\  {\tt (ssn}\  {\tt name}\  {\tt left}\  {\tt right))}\end{tabbing}\end{minipage}\end{center}
Here we have decided to record the social security number, the name, and
two other trees. The latter are like the parent fields of family trees,
though the relationship between a {{\tt node}\/} and its {{\tt left}\/} and
{{\tt right}\/} trees is not based on family relationships. 

The corresponding data definition is just like the one for family trees:

  \begin{quote} A {\sl {binary tree}} ({{\tt BT}\/}) is either       \begin{enumerate}         \item {{\sf false}\/} or          \item {{\tt (make-node}\ {\tt soc}\ {\tt pn}\ {\tt lft}\ {\tt rgt)}\/} \\
           where {{\tt soc}\/} is a number, {{\tt pn}\/} is a symbol,           and {{\tt lft}\/} and {{\tt rgt}\/} are {{\tt BT}\/}s.        \end{enumerate} \end{quote}

\noindent The choice of {{\sf false}\/} to indicate lack of information is 
arbitrary. We could have chosen {{\tt empty}\/} again, but {{\sf false}\/} is 
an equally good and an equally frequent choice that we should become
familiar with. 

Here are two binary trees: \medskip

\noindent\begin{minipage}[t]{8in}\begin{tabbing}\  \  \  {\tt (m}\={\tt ake-node}\\
\>{\tt 15}
\\
\>{\tt \char'047}{\tt d}\  
\\
\>{\sf false}
\\
\>{\tt (make-node}\  {\tt 24}\  {\tt \char'047}{\tt i}\  {\sf false}\  {\sf false}{\tt ))}
\end{tabbing}\end{minipage}~~\begin{minipage}[t]{8in}\begin{tabbing}{\tt (m}\={\tt ake-node}\\
\>{\tt 15}
\\
\>{\tt \char'047}{\tt d}
\\
\>{\tt (make-node}\  {\tt 87}\  {\tt \char'047}{\tt h}\  {\sf false}\  {\sf false}{\tt )}
\\
\>{\sf false}{\tt )}
\end{tabbing}\end{minipage}

\smallskip

\noindent Figure~\ref{fig:bst} shows how we should think about such
trees. The trees are drawn upside down, that is, with the root at the top
and the crown of the tree at the bottom. Each circle corresponds to a node,
labeled with the {{\tt ssn}\/} field of a corresponding {{\tt node}\/}
structure. The trees omit {{\sf false}\/}. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 14.2.1}

 \label{ex:bst1} \label{ex:bst1-r}
Draw the two trees above in the manner of figure~\ref{fig:bst}.

Develop {{\tt contains-bt}\/}. The function consumes a number and a
{{\tt BT}\/} and determines whether the number occurs in the tree. 

Develop {{\tt search-bt}\/}. The function consumes a number {{\tt n}\/} and a
{{\tt BT}\/}. If the tree contains a {{\tt node}\/} structure whose
{{\tt soc}\/} field is {{\tt n}\/}, the function produces the value of the
{{\tt pn}\/} field in that node. Otherwise, the function produces
{{\sf false}\/}.

\noindent{\bf Hint:} \ Use {{\tt contains-bt}\/} to determine whether to search in a subtree
of the given binary tree.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/bst1.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}
 




\hrule
\begin{htmlonly}
\begin{rawhtml}
<table bgcolor=beige>
<tr>
<td align=left>Tree A:</td>
<td align=right>Tree B:</td>
</tr>
<tr>
<td align=left><img src=../icons/bst1.gif alt="[bst]"></td>
<td align=right><img src=../icons/bst2.gif alt="[bst]"></td>
</tr>
</table>
\end{rawhtml}
\end{htmlonly}

\relax ~\\
 \bigskip

\center{Figure: A binary search tree and a binary tree} \label{fig:bst}
\hrule


Both trees in figure~\ref{fig:bst} are binary trees but they differ in a
significant way. If we read the numbers in the two trees from left to right
we obtain two sequences:
$$\begin{array}{l|c|c|c|c|c|c|c|c|c|c|c}
\mbox{Tree A:}  &10&15&24&29&63&77&89&95&99\\
 \hline
\mbox{Tree B:}  &87&15&24&29&63&33&89&95&99\\
 
\end{array}$$
The sequence for tree A is sorted in ascending order, the one for B is not.

A binary tree that has an ordered sequence of information is a
{\sc {binary search tree}}. Every binary search tree is a binary tree,
but not every binary tree is a binary search tree. We say that the class of
binary search trees is a {\sc {proper subclass}} of that of binary
trees. To define the class of binary search trees rigorously, we formulate
a condition that distinguishes a binary search tree from a binary tree:

  \begin{quote} \begin{quote}        \centerline{The BST Invariant}         A {\sl {binary search tree}} ({{\tt BST}\/}) is a {{\tt BT}\/}:        \begin{enumerate}         \item {{\sf false}\/} is always a {{\tt BST}\/};          \item {{\tt (make-node}\ {\tt soc}\ {\tt pn}\ {\tt lft}\ {\tt rgt)}\/} is a {{\tt BST}\/} if             \begin{enumerate}               \item {{\tt lft}\/} and {{\tt rgt}\/} are {{\tt BST}\/}s,                \item all {{\tt ssn}\/} numbers in {{\tt lft}\/} are smaller than {{\tt soc}\/}, and                \item all {{\tt ssn}\/} numbers in {{\tt rgt}\/} are larger than {{\tt soc}\/}.            \end{enumerate}          \end{enumerate}        \end{quote} \end{quote}

\noindent The second and third condition are different from what we have
 seen in previous data definitions. They place an additional and unusual
 burden on the construction  {{\tt BST}\/}s. We must inspect all numbers in
 these trees and ensure that they are smaller (or larger) than
 {{\tt soc}\/}. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 14.2.2}

 \label{ex:bst0}
Develop the function {{\tt inorder}\/}. It consumes a binary tree and
produces a list of all the {{\tt ssn}\/} numbers in the tree. The list 
contains the numbers in the left-to-right order we have used above. 

\noindent{\bf Hint:} \ Use the Scheme operation {{\tt append}\/}, which concatenates
lists. Here is an example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (append}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3)}\  {\tt (list}\  {\tt 4)}\  {\tt (list}\  {\tt 5}\  {\tt 6}\  {\tt 7))}\\
{\tt =}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5}\  {\tt 6}\  {\tt 7)}
\end{tabbing}\end{minipage}\end{center}
What does {{\tt inorder}\/} produce for a binary search tree?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/bst0.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}
 

Looking for a specific {{\tt node}\/} in a {{\tt BST}\/} takes fewer steps
than looking for the same {{\tt node}\/} in a {{\tt BT}\/}. To find out
whether a {{\tt BT}\/} contains a node with a specific {{\tt ssn}\/} field, a
function may have to look at every {{\tt node}\/} of the tree. In contrast,
to inspect a binary search tree requires far fewer inspections than
that. Suppose we are given the {{\tt BST}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-node}\  {\tt 66}\  {\tt \char'047}{\tt a}\  {\tt L}\  {\tt R)}\end{tabbing}\end{minipage}\end{center}
If we are looking for {{\tt 66}\/}, we have found it. Now suppose we are
looking for {{\tt 63}\/}. Given the above {{\tt node}\/}, we can focus the
search on {{\tt L}\/} because {\em all\/} {{\tt node}\/}s with {{\tt ssn}\/}s
smaller than {{\tt 65}\/} are in {{\tt L}\/}. Similarly, if we were to look
for {{\tt 99}\/}, we would ignore {{\tt L}\/} and focus on {{\tt R}\/} because
{\em all\/} {{\tt node}\/}s with {{\tt ssn}\/}s larger than {{\tt 65}\/} are
in {{\tt R}\/}. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 14.2.3}

 \label{ex:bst2} 
Develop {{\tt search-bst}\/}.  The function consumes a number {{\tt n}\/} and
a {{\tt BST}\/}. If the tree contains a {{\tt node}\/} structure whose
{{\tt soc}\/} field is {{\tt n}\/}, the function produces the value of the
{{\tt pn}\/} field in that node. Otherwise, the function produces
{{\sf false}\/}. The function organization must exploit the {\em BST Invariant\/} so that the function performs as few comparisons as
necessary. Compare searching in binary search trees with searching in
sorted lists (exercise~\ref{ex:sort2}).~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/bst2.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}
 

Building a binary tree is easy; building a binary search trees is a
complicated, error-prone affair.  To create a {{\tt BT}\/} we combine two
{{\tt BT}\/}s, a {{\tt ssn}\/} number and a {{\tt name}\/} with
{{\tt make-node}\/}. The result is, by definition, a {{\tt BT}\/}. To create
a {{\tt BST}\/}, this procedure fails because the result would typically not
be a {{\tt BST}\/}. For example, if one tree contains {{\tt 3}\/} and
{{\tt 5}\/}, and the other one contains {{\tt 2}\/} and {{\tt 6}\/}, there is
no way to join these two {{\tt BST}\/}s into a single binary search tree. 

We can overcome this problem in (at least) two different ways. First, given
a list of numbers and symbols, we can determine by hand what the
corresponding {{\tt BST}\/} should look like and then use {{\tt make-node}\/}
to build it. Second, we can write a function that builds a {{\tt BST}\/} from
the list, one {{\tt node}\/} after another.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 14.2.4}

 \label{ex:bst3}
Develop the function {{\tt create-bst}\/}. It consumes a {{\tt BST}\/}
{{\tt B}\/}, a number {{\tt N}\/}, and a symbol {{\tt S}\/}. It produces a
{{\tt BST}\/} that is just like {{\tt B}\/} and that in place of one
{{\sf false}\/} subtree contains the {{\tt node}\/} structure
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-node}\  {\tt N}\  {\tt S}\  {\sf false}\  {\sf false}{\tt )}\end{tabbing}\end{minipage}\end{center}

Test the function with {{\tt (create-bst}\ {\sf false}\ {\tt 66}\ {\tt \char'047}{\tt a)}\/}; this should create a
single {{\tt node}\/}. Then show that the following holds:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (create-bst}\  {\tt (create-bst}\  {\sf false}\  {\tt 66}\  {\tt \char'047}{\tt a)}\  {\tt 53}\  {\tt \char'047}{\tt b)}\\
{\tt =}\  {\tt (make-node}\  \={\tt 66}\  
\\
\>{\tt \char'047}{\tt a}
\\
\>{\tt (make-node}\  {\tt 53}\  {\tt \char'047}{\tt b}\  {\sf false}\  {\sf false}{\tt )}
\\
\>{\sf false}{\tt )}
\end{tabbing}\end{minipage}\end{center}
Finally, create tree A from figure~\ref{fig:bst} using
{{\tt create-bst}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/bst3.html}{\rule{3pt}{5pt}}


{\bf Exercise 14.2.5}

 \label{ex:bst4}
Develop the function {{\tt create-bst-from-list}\/}. It consumes a list of
numbers and names; it produces a {{\tt BST}\/} by repeatedly applying  
{{\tt create-bst}\/}. 

The data definition for a list of numbers and names is as follows: 

  \begin{quote} A {\sl {list of numbers and names}} ({{\tt list-of-number/name}\/}) is 	      either         \begin{enumerate}        \item {{\tt empty}\/} or         \item {{\tt (cons}\ {\tt (list}\ {\tt ssn}\ {\tt nom)}\ {\tt lonn)}\/}\\
           where {{\tt ssn}\/} is a number, {{\tt nom}\/} a symbol, \\
           and {{\tt lonn}\/} is a {{\tt list-of-number/name}\/}.        \end{enumerate} \end{quote}

\noindent Consider the following examples: 
\begin{quote}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt sample}\\
\>{\tt \char'047}{\tt (}\={\tt (99}\  {\tt o)}
\\
\>\>{\tt (77}\  {\tt l)}
\\
\>\>{\tt (24}\  {\tt i)}
\\
\>\>{\tt (10}\  {\tt h)}
\\
\>\>{\tt (95}\  {\tt g)}
\\
\>\>{\tt (15}\  {\tt d)}
\\
\>\>{\tt (89}\  {\tt c)}
\\
\>\>{\tt (29}\  {\tt b)}
\\
\>\>{\tt (63}\  {\tt a)))}
\end{tabbing}\end{minipage}\qquad\qquad\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt sample}\\
\>{\tt (list}\  \={\tt (list}\  {\tt 99}\  {\tt \char'047}{\tt o)}
\\
\>\>{\tt (list}\  {\tt 77}\  {\tt \char'047}{\tt l)}
\\
\>\>{\tt (list}\  {\tt 24}\  {\tt \char'047}{\tt i)}
\\
\>\>{\tt (list}\  {\tt 10}\  {\tt \char'047}{\tt h)}
\\
\>\>{\tt (list}\  {\tt 95}\  {\tt \char'047}{\tt g)}
\\
\>\>{\tt (list}\  {\tt 15}\  {\tt \char'047}{\tt d)}
\\
\>\>{\tt (list}\  {\tt 89}\  {\tt \char'047}{\tt c)}
\\
\>\>{\tt (list}\  {\tt 29}\  {\tt \char'047}{\tt b)}
\\
\>\>{\tt (list}\  {\tt 63}\  {\tt \char'047}{\tt a)))}
\end{tabbing}\end{minipage}
\end{quote}
They are equivalent, although the left one is defined with the quote
abbreviation, the right one using {{\tt list}\/}. The left tree in
figure~\ref{fig:bst} is the result of using {{\tt create-bst-from-list}\/}
on this list.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/bst4.html}{\rule{3pt}{5pt}}
 

\subsection{Lists in Lists} \label{sec:docs} 
The World Wide Web, or just ``the Web,'' has become the most interesting
and most rapidly expanding part of the Internet, a global network of
computers. Roughly speaking, the Web is a collection of Web pages. Each
Web page is a sequence of words, pictures, movies, audio messages, and
many more things. Most importantly, Web pages also contain links to other
Web pages. 

A Web browser enables people to view Web pages. It presents a Web page as a
sequence of words, images, and so on. Some of the words on a page may be
underlined. Clicking on underlined words leads to a new Web page.  Most
modern browsers also provide a Web page composer. These are tools that
helps people create collections of Web pages. A composer can, among other
things, search for words or replace one word by another.  In short, Web
pages are things that we should be able to represent on computers, and
there are many functions that process Web pages.

To simplify our problem, we consider only Web pages of words and nested Web
pages. One way of understanding such a page is as a sequence of words and
Web pages. This informal description suggests a natural representation of
Web pages as lists of symbols, which represent words, and Web pages, which
represent nested Web pages.  After all, we have emphasized before that a
list may contain different kinds of things. Still, when we spell this idea
out as data definition, we get something rather unusual:

   \begin{quote} A {\sl {Web page}} ({{\tt WP}\/}) is either      \begin{enumerate}      \item {{\tt empty}\/};      \item {{\tt (cons}\ {\tt s}\ {\tt wp)}\/} \\
          where {{\tt s}\/} is a symbol and {{\tt wp}\/} is a Web page; or      \item {{\tt (cons}\ {\tt ewp}\ {\tt wp)}\/} \\
           where both {{\tt ewp}\/} and {{\tt wp}\/} are Web pages.       \end{enumerate} \end{quote}

\noindent This data definition differs from that of a list of symbols in
that it has three clauses instead of two and that it has three
self-references instead of one. Of these self-references, the one at the
beginning of a {{\tt cons}\/}tructed list is the most unusual one. We refer
to such Web pages as {\sl immediately embedded} Web pages.

Because the data definition is unusual, we construct some examples of Web
pages before we continue. Here is a plain page:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt \char'047}{\tt (}\={\tt The}\  {\tt TeachScheme!}\  {\tt Project}\  {\tt aims}\  \\
\>{\tt to}\  {\tt improve}\  {\tt students\char'047}\  {\tt problem-solving}\  
\\
\>{\tt and}\  {\tt organization}\  {\tt skills.}\  {\tt It}\  {\tt provides}
\\
\>{\tt software}\  {\tt and}\  {\tt lecture}\  {\tt notes}\  {\tt as}\  {\tt well}\  {\tt as}\  
\\
\>{\tt exercises}\  {\tt and}\  {\tt solutions}\  {\tt for}\  {\tt teachers.)}
\end{tabbing}\end{minipage}\end{center}
It contains nothing but words. Here is a complex page:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt \char'047}{\tt (}\={\tt The}\  {\tt TeachScheme}\  {\tt Web}\  {\tt Page}\\
\>{\tt Here}\  {\tt you}\  {\tt can}\  {\tt find:}\  
\\
\>{\tt (LectureNotes}\  {\tt for}\  {\tt Teachers)}
\\
\>{\tt (Guidance}\  {\tt for}\  {\tt (DrScheme:}\  {\tt a}\  {\tt Scheme}\  {\tt programming}\  {\tt environment))}
\\
\>{\tt (Exercise}\  {\tt Sets)}
\\
\>{\tt (Solutions}\  {\tt for}\  {\tt Exercises)}
\\
\>{\tt For}\  {\tt further}\  {\tt information\char'054}\  {\tt write}\  {\tt to}\  {\tt scheme\char'100}{\tt cs)}
\end{tabbing}\end{minipage}\end{center}
The immediately embedded pages start with parentheses and the symbols
{{\tt \char'047}{\tt LectureNotes}\/}, {{\tt \char'047}{\tt Guidance}\/}, {{\tt \char'047}{\tt Exercises}\/}, and
{{\tt \char'047}{\tt Solutions}\/}. The second embedded Web page contains another embedded
page, which starts with the word {{\tt \char'047}{\tt DrScheme}\/}. We say this page is
{\sl embedded} with respect to the entire page.

Let's develop the function {{\tt size}\/}, which consumes a Web page and
produces the number of words that it and all of its embedded pages contain:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt size}\ {\tt :}\ {\tt WP}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to count the number of symbols that occur in {{\tt a-wp}\/}}
\\
{\tt (define}\  {\tt (size}\  {\tt a-wp)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

The two Web pages above suggest two good examples, but they are too
complex. Here are three simpler examples, one per clause in the data
definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (size}\  {\tt empty)}\\
{\tt =}\  {\tt 0}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (size}\  {\tt (cons}\  {\tt \char'047}{\tt One}\  {\tt empty))}\\
{\tt =}\  {\tt 1}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (size}\  {\tt (cons}\  {\tt (cons}\  {\tt \char'047}{\tt One}\  {\tt empty)}\  {\tt empty))}\\
{\tt =}\  {\tt 1}\  \  
\end{tabbing}\end{minipage}\end{center}
The first two examples are obvious. The third one deserves a short
explanation. It is a Web page that contains one immediately embedded Web
page, and nothing else. The embedded Web page is the one of the second
example, and it contains the one and only symbol of the third example. 

To develop the template for {{\tt size}\/}, let's carefully step through the
design recipe. The shape of the data definition suggests that we need three
{{\tt cond}\/}-clauses: one for the {{\tt empty}\/} page, one for a page that
starts with a symbol, and one for a page that starts with an embedded Web
page. While the first condition is the familiar test for {{\tt empty}\/},
the second and third need closer inspection because both clauses in the
data definition use {{\tt cons}\/} and a simple {{\tt cons?}\/} won't
distinguish between the two forms of data. 

If the page is not {{\tt empty}\/}, it is certainly {{\tt cons}\/}tructed,
and the distinguishing feature is the first item on the list. In other
words, the second condition must use a predicate that tests the first item
on {{\tt a-wp}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt size}\ {\tt :}\ {\tt WP}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to count the number of symbols that occur in a-wp}
\\
{\tt (d}\={\tt efine}\  {\tt (size}\  {\tt a-wp)}\  
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-wp)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol?}\  {\tt (first}\  {\tt a-wp))}\  {\tt ...}\  {\tt (first}\  {\tt a-wp)}\  {\tt ...}\  {\tt (size}\  {\tt (rest}\  {\tt a-wp))}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (size}\  {\tt (first}\  {\tt a-wp))}\  {\tt ...}\  {\tt (size}\  {\tt (rest}\  {\tt a-wp))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The rest of the template is as usual. The second and third {{\tt cond}\/}
clause contain selector expressions for the first item and the rest of the
list. Because {{\tt (rest}\ {\tt a-wp)}\/} is always a Web page and because
{{\tt (first}\ {\tt a-wp)}\/} is one in the third case, we also add a recursive
call to size for these selector expressions.

Using the examples and the template, we are ready to design {{\tt size}\/}:
see figure~\ref{fig:size}. The differences between the definition and the
template are minimal, which shows again how much of a function we can design
by merely thinking systematically about the data definition for its inputs.

\hrule 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt size}\ {\tt :}\ {\tt WP}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to count the number of symbols that occur in a-wp}
\\
{\tt (d}\={\tt efine}\  {\tt (size}\  {\tt a-wp)}\  
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-wp)}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol?}\  {\tt (first}\  {\tt a-wp))}\  {\tt (+}\  {\tt 1}\  {\tt (size}\  {\tt (rest}\  {\tt a-wp)))\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (size}\  {\tt (first}\  {\tt a-wp))}\  {\tt (size}\  {\tt (rest}\  {\tt a-wp)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: The definition of {\it size\/} for Web pages} \label{fig:size}
\hrule

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 14.3.1}

 \label{ex:web1}
Briefly explain how to define {{\tt size}\/} using its template and the
examples. Test {{\tt size}\/} using the examples from above. 


{\bf Exercise 14.3.2}

 \label{ex:web2}
Develop the function {{\tt occurs1}\/}. The function consumes a Web page and a
 symbol. It produces the number of times the symbol occurs in the Web page,
 ignoring the nested Web pages.

Develop the function {{\tt occurs2}\/}. It is like {{\tt occurs1}\/}, but
 counts {\em all\/} occurrences of the symbol, including in embedded Web
 pages.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/web2.html}{\rule{3pt}{5pt}}


{\bf Exercise 14.3.3}

 \label{ex:web3}
Develop the function {{\tt replace}\/}. The function consumes two symbols,
 {{\tt new}\/} and {{\tt old}\/}, and a Web page, {{\tt a-wp}\/}. It
 produces a page that is structurally identical to {{\tt a-wp}\/} but with
 all occurrences of {{\tt old}\/} replaced by {{\tt new}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/web3.html}{\rule{3pt}{5pt}}


{\bf Exercise 14.3.4}

 \label{ex:web4}
People do not like deep Web trees because they require too many page
 switches to reach useful information. For that reason a Web page designer
 may also want to measure the depth of a page. A page containing only
 symbols has depth {{\tt 0}\/}. A page with an immediately embedded page has
 the depth of the embedded page plus {{\tt 1}\/}. If a page has several
 immediately embedded Web pages, its depth is the maximum of the depths of
 embedded Web pages plus {{\tt 1}\/}. Develop {{\tt depth}\/}, which consumes
 a Web page and computes its depth.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/web4.html}{\rule{3pt}{5pt}}


\subsection{Extended Exercise: Evaluating Scheme} 
\label{sec:interpreter} 

\htmladdimg{../icons/teacher.gif}~{\em This subsection provides first glimpse at how to implement a Scheme  interpreter. Section~\ref{sec:interpreter2} refines this material.}

DrScheme is itself a program, consisting of several parts. One function
checks whether the definitions and expressions we wrote down are
grammatical Scheme expressions. Another one evaluates Scheme
expressions. With what we have learned in this section, we can now develop
simple versions of these functions.

Our first task is to agree on a data representation for Scheme programs. In
other words, we must figure out how to represent a Scheme expression as a
piece of Scheme data. This sounds unusual, but it is not difficult. Suppose
we just want to represent numbers, variables, additions, and
multiplications for a start. Clearly, numbers can stand for numbers and
symbols for variables. Additions and multiplications, however, call for a
class of compound data because they consist of an operator and two
subexpressions.

A straightforward way to represent additions and multiplications is to use
two structures: one for additions and another one for multiplications.
Here are the structure definitions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt add}\  {\tt (left}\  {\tt right))}\\
{\tt (define-struct}\  {\tt mul}\  {\tt (left}\  {\tt right))}
\end{tabbing}\end{minipage}\end{center}
Each structure has two components. One represents the left expression and
the other one the right expression of the operation. 

\begin{htmlonly}
\begin{rawhtml}
<center>
<p><table border=1  CELLPADDING=3 cellspacing=1 bgcolor=beige>
              <tr><th>Scheme expression</th> <th>representation of Scheme expression</th></tr>
<tr><td><code>3</code></td>                  <td><code>3</code></td></tr>
<tr><td><code>x</code></td>                  <td><code>'x</code></td></tr>
<tr><td><code>(* 3 10)</code></td>           <td><code>(make-mul 3 10)</code></td></tr>
<tr><td><code>(+ (* 3 3) (* 4 4))</code></td><td><code>(make-add (make-mul 3 3) (make-mul 4 4))</code></td></tr>
<tr><td><code>(+ (* x x) (* y y))</code></td><td><code>(make-add (make-mul 'x 'x) (make-mul 'y 'y))</code></td></tr>
<tr><td><code>(* 1/2 (* 3 3))</code></td>    <td><code>(make-mul 1/2 (make-mul 3 3))</code></td></tr>
</tr>
</table>
</center>
\end{rawhtml}
\end{htmlonly}

Let's look at some examples:

\relax

\noindent These examples cover all cases: numbers, variables, simple
expressions, and nested expressions.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 14.4.1}

 \label{ex:scheme-dd}
Provide a data definition for the representation of Scheme
expressions. Then translate the following expressions into representations:
\begin{enumerate}
\item {{\tt (+}\ {\tt 10}\ {\tt -10)}\/}
\item {{\tt (+}\ {\tt (*}\ {\tt 20}\ {\tt 3)}\ {\tt 33)}\/}
\item {{\tt (*}\ {\tt 3.14}\ {\tt (*}\ {\tt r}\ {\tt r))}\/} 
\item {{\tt (+}\ {\tt (*}\ {\tt 9/5}\ {\tt c)}\ {\tt 32)}\/}
\item {{\tt (+}\ {\tt (*}\ {\tt 3.14}\ {\tt (*}\ {\tt o}\ {\tt o))}\ {\tt (*}\ {\tt 3.14}\ {\tt (*}\ {\tt i}\ {\tt i)))}\/}~~~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/scheme-dd.html}{\rule{3pt}{5pt}}
\end{enumerate}


A Scheme evaluator is a function that consumes a representation of a Scheme
expression and produces its value. For example, the expression {{\tt 3}\/}
has the value {{\tt 3}\/}, {{\tt (+}\ {\tt 3}\ {\tt 5)}\/} has the value {{\tt 8}\/},
{{\tt (+}\ {\tt (*}\ {\tt 3}\ {\tt 3)}\ {\tt (*}\ {\tt 4}\ {\tt 4))}\/} has the value {{\tt 25}\/}, {\it etc\/}.  Since
we are ignoring definitions for now, an expression that contains a
variable, for example, {{\tt (+}\ {\tt 3}\ {\tt x)}\/}, does not have a value; after all,
we do not know what the variable stands for. In other words, our Scheme
evaluator should only be applied to representations of expressions that do
not contain variables. We say such expressions are {\sl numeric}.

{\bf Exercise 14.4.2}

 \label{ex:scheme-numeric}
Provide a data definition for numeric expressions.  Develop the function
 {{\tt numeric\char'077}\/}, which consumes (the representation of) a Scheme
 expression and determines whether it is numeric.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/scheme-numeric.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 14.4.3}

 \label{ex:scheme-eval}
Develop the function {{\tt evaluate}\/}. The function consumes (the
 representation of) a Scheme expression. If the expression is numeric (see
 exercise~\ref{ex:scheme-numeric}), it produces the number that DrScheme
 would produce for the actual Scheme expression. Otherwise, it returns
 {{\tt false}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/scheme-eval.html}{\rule{3pt}{5pt}}
 

When people evaluate expressions with variables, they first replace the
variables with values. Also, when people evaluate an application {{\tt (f}{ }{\tt a)}\/} they replace {{\tt f}\/}'s parameter with {{\tt a}\/} in {{\tt f}\/}'s
body. The following exercise concerns the substitution of variables with
values.

{\bf Exercise 14.4.4}

 \label{ex:scheme-subst}
Develop the function {{\tt subst}\/}. The function consumes (the
 representation of) a variable ({{\tt V}\/}), a number ({{\tt N}\/}), and
 (the representation of) a Scheme expression. It produces a structurally
 equivalent expression in which all occurrences of {{\tt V}\/} are
 substituted by {{\tt N}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/scheme-subst.html}{\rule{3pt}{5pt}}
 

\section{Mutually Referential Data Definitions} 
\label{sec:mutual} 
In the preceding section, we developed simple data representations of
family trees, Web pages, and Scheme expressions.  Developing functions for
the corresponding data definitions was based on one and the same design
recipe. If we wish to develop more realistic representations of Web pages
or Scheme expressions, or if we wish to study descendant family trees
rather than ancestor trees, we must learn to describe classes of data that
are interrelated. That is, we must formulate several data definitions at
once where the data definitions not only refer to themselves, but they also
refer to other data definitions.

\subsection{Lists of Structures, Lists in Structures} \label{sec:mut-ref-dd}

When we build a family tree retroactively, we often start from the child's
perspective and proceed from there to parents, grandparents, {\it etc\/}. As we construct the tree, we write down who is whose child rather
than who is whose parents. We build a {\em descendant family tree\/}. 

Drawing a descendant tree proceeds just like drawing an ancestor tree,
except that all arrows are reversed.  Figure~\ref{fig:bu-family} represents
the same family as that of figure~\ref{fig:familyA}, but drawn from the
descendant perspective. 

\hrule \begin{picture}(300,200)(0,+20)
\put(000,180){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Carl~~(1926)}} 
\put(00,05){\mbox{~Eyes:~~ green}} \end{picture}}
  
\put(035,180){\vector(0,-1){50}}           
  
\put(035,180){\vector(2,-1){100}}
  
\put(035,180){\vector(4,-1){200}}
\put(100,180){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Bettina~~(1926)}} 
\put(00,05){\mbox{~Eyes:~~ green}} \end{picture}} 
  
\put(135,180){\vector(0,-1){50}}           
  
\put(135,180){\vector(-2,-1){100}}
  
\put(135,180){\vector(+2,-1){100}}
\put(000,100){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Adam~~(1950)}} 
\put(00,05){\mbox{~Eyes:~~ yellow}} \end{picture}}
\put(100,100){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Dave~~(1955)}} 
\put(00,05){\mbox{~Eyes:~~ black}} \end{picture}}    
\put(200,100){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Eva~~(1965)}} 
\put(00,05){\mbox{~Eyes:~~ blue}} \end{picture}}
  
\put(235,100){\vector(+1,-1){50}}
\put(290,100){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Fred~~(1966)}} 
\put(00,05){\mbox{~Eyes:~~ pink}} \end{picture}}
  
\put(335,100){\vector(-1,-1){50}}
\put(255,020){\begin{picture}(75,30) 
\put(00,00){\line(+1,+0){70}} 
\put(70,00){\line(+0,+1){30}} 
\put(70,30){\line(-1,+0){70}} 
\put(00,30){\line(+0,-1){30}} 
\put(00,19){\mbox{~Gustav~~(1988)}} 
\put(00,05){\mbox{~Eyes:~~ blush}} \end{picture}} 
\end{picture}
\center{Figure: A descendant family tree}\label{fig:bu-family}
\hrule 

Representing these new kinds of family trees and their nodes in a computer
requires a different class of data than the ancestor family trees. This
time a node must include information about the children instead of the two
parents. Here is a structure definition:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt parent}\  {\tt (children}\  {\tt name}\  {\tt date}\  {\tt eyes))}\end{tabbing}\end{minipage}\end{center}
The last three fields in a parent structure contain the same basic
information as a corresponding child structure, but the contents of the
first one poses an interesting question. Since a parent may have an
arbitrary number of children, the {{\tt children}\/} field must contain an
undetermined number of nodes, each of which represents one child.

The natural choice is to insist that the {{\tt children}\/} field always
stands for a list of {{\tt parent}\/} structures. The list represents the
children; if a person doesn't have children, the list is {{\tt empty}\/}.
This decision suggests the following data definition:
\begin{quote}
  A {\em parent\/} is a structure:\\
     \begin{center}
       {{\tt (make-parent}\ {\tt loc}\ {\tt n}\ {\tt d}\ {\tt e)}\/}
     \end{center}
     where {{\tt loc}\/} is a list of children, \\
     {{\tt n}\/} and {{\tt e}\/} are symbols, \\
     and {{\tt d}\/} is a number. 
\end{quote}
Unfortunately, this data definition violates our criteria concerning
definitions. In particular, it mentions the name of a collection that is
not yet defined: list of children. 

Since it is impossible to define the class of parents without knowing what
a list of children is, let's start from the latter:
\begin{quote}
  A {\em list of children\/} is either
    \begin{enumerate}
      \item {{\tt empty}\/} or
      \item {{\tt (cons}\ {\tt p}\ {\tt loc)}\/} \\
        where {{\tt p}\/} is a parent \\
        and {{\tt loc}\/} is a list of children.
    \end{enumerate}
\end{quote}
This second definition looks standard, but it suffers from the same problem
as the one for {{\tt parents}\/}. The unknown class it refers to is that of
the class of parents, which cannot be defined without a definition for the
list of children, and so on. 

The conclusion is that the two data definitions refer to each other and are
only meaningful if introduced {\em together\/}:

  \begin{quote} A {\sl {parent}} is a structure:\\
        \centerline{{{\tt (make-parent}\ {\tt loc}\ {\tt n}\ {\tt d}\ {\tt e)}\/}}       where {{\tt loc}\/} is a list of children,       {{\tt n}\/} and {{\tt e}\/} are symbols,       and {{\tt d}\/} is a number.     \bigskip        A {\sl {list of children}} is either 	\begin{enumerate}           \item {{\tt empty}\/} or           \item {{\tt (cons}\ {\tt p}\ {\tt loc)}\/}                  where {{\tt p}\/} is a parent              and {{\tt loc}\/} is a list of children. 	\end{enumerate} \end{quote}

\noindent When two (or more) data definitions refer to each other, they are
said to be {\sc {mutually recursive}} or {\sc {mutually referential}}.

Now we can translate the family tree of figure~\ref{fig:bu-family} into our
Scheme data language. Before we can create a {{\tt parent}\/} structure, of
course, we must first define all of the nodes that represent children. And,
just as in section~\ref{sec:struct-in-struct}, the best way to do this is
to name a {{\tt parent}\/} structure before we reuse it in a list of
children. Here is an example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt Gustav}\  {\tt (make-parent}\  {\tt empty}\  {\tt \char'047}{\tt Gustav}\  {\tt 1988}\  {\tt blush))}\\
{\tt (make-parent}\  {\tt (list}\  {\tt Gustav)}\  {\tt \char'047}{\tt Fred}\  {\tt 1950}\  {\tt yellow)}
\end{tabbing}\end{minipage}\end{center}
To create a {{\tt parent}\/} structure for Fred, we first define one for
Gustav so that we can form {{\tt (list}\ {\tt Gustav)}\/}, the list of children for
Fred. 

Figure~\ref{fig:bu-familyS} contains the complete Scheme representation for
our descendant tree. To avoid repetitions, it also includes definitions for
lists of children. Compare the definitions with figure~\ref{fig:familyS}
(see page~\pageref{fig:familyS}), which represents the same family as an
ancestor tree. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; Youngest Generation:}\\
{\tt (define}\  {\tt Gustav}\  {\tt (make-parent}\  {\tt empty}\  {\tt \char'047}{\tt Gustav}\  {\tt 1988}\  {\tt blush))}
\\
{\tt (define}\  {\tt Fred\char'046}{\tt Eva}\  {\tt (list}\  {\tt Gustav))}
\\
\hbox{;; Middle Generation:}
\\
{\tt (define}\  {\tt Adam}\  {\tt (make-parent}\  {\tt empty}\  {\tt \char'047}{\tt Adam}\  {\tt 1950}\  {\tt yellow))}
\\
{\tt (define}\  {\tt Dave}\  {\tt (make-parent}\  {\tt empty}\  {\tt \char'047}{\tt Dave}\  {\tt 1955}\  {\tt black))}
\\
{\tt (define}\  {\tt Eva}\  {\tt (make-parent}\  {\tt Fred\char'046}{\tt Eva}\  {\tt \char'047}{\tt Eva}\  {\tt 1965}\  {\tt blue))}
\\
{\tt (define}\  {\tt Fred}\  {\tt (make-parent}\  {\tt Fred\char'046}{\tt Eva}\  {\tt \char'047}{\tt Fred}\  {\tt 1966}\  {\tt pink))}
\\
{\tt (define}\  {\tt Carl\char'046}{\tt Bettina}\  {\tt (list}\  {\tt Adam}\  {\tt Dave}\  {\tt Eva))}
\\
\hbox{;; Oldest Generation:}
\\
{\tt (define}\  {\tt Carl}\  {\tt (make-parent}\  {\tt Carl\char'046}{\tt Bettina}\  {\tt \char'047}{\tt Carl}\  {\tt 1926}\  {\tt green))}
\\
{\tt (define}\  {\tt Bettina}\  {\tt (make-parent}\  {\tt Carl\char'046}{\tt Bettina}\  {\tt \char'047}{\tt Bettina}\  {\tt 1926}\  {\tt green))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: A Scheme representation of the descendant family tree} \label{fig:bu-familyS}
\hrule

Let us now study the development of {{\tt blue-eyed-descendant\char'077}\/}, the
natural companion of {{\tt blue-eyed-ancestor\char'077}\/}. It consumes a
{{\tt parent}\/} structure and determines whether it or any of its
descendants have blue eyes. 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt blue-eyed-descendant\char'077}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt a-parent}\/} or any of its descendants (children, }
\\
\hbox{;; grandchildren, and so on) have {{\tt \char'047}{\tt blue}\/} in the {{\tt eyes}\/} field}
\\
{\tt (define}\  {\tt (blue-eyed-descendant\char'077}\  {\tt a-parent)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center} 
Here are three simple examples:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (blue-eyed-descendant\char'077}\  {\tt Gustav)}\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (blue-eyed-descendant\char'077}\  {\tt Eva)}\\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (blue-eyed-descendant\char'077}\  {\tt Bettina)}\\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
A glance at figure~\ref{fig:bu-family} explains the answers in each case.

According to our rules, the template for {{\tt blue-eyed-descendant\char'077}\/} is
simple. Since its input is a plain class of structures, the template
contains nothing but selector expressions for the fields in the structure:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (blue-eyed-descendant\char'077}\  {\tt a-parent)}\\
\>{\tt ...}\  {\tt (parent-children}\  {\tt a-parent)}\  {\tt ...}\  
\\
\>{\tt ...}\  {\tt (parent-name}\  {\tt a-parent)}\  {\tt ...}\  
\\
\>{\tt ...}\  {\tt (parent-date}\  {\tt a-parent)}\  {\tt ...}\  
\\
\>{\tt ...}\  {\tt (parent-eyes}\  {\tt a-parent)}\  {\tt ...}\  {\tt )}
\end{tabbing}\end{minipage}\end{center}
The structure definition for {{\tt parent}\/} specifies four fields so there
are four expressions. 

The expressions in the template remind us that the eye color of the parent
is available and can be checked. Hence, we add a {{\bf cond}-exp\-res\-sion} 
that compares {{\tt (parent-eyes}\ {\tt a-parent)}\/} to {{\tt \char'047}{\tt blue}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (blue-eyed-descendant\char'077}\  {\tt a-parent)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt (parent-eyes}\  {\tt a-parent)}\  {\tt \char'047}{\tt blue)}\  {\sf true}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}\  
\\
\>\>\>{\tt ...}\  {\tt (parent-children}\  {\tt a-parent)}\  {\tt ...}\  
\\
\>\>\>{\tt ...}\  {\tt (parent-name}\  {\tt a-parent)}\  {\tt ...}\  
\\
\>\>\>{\tt ...}\  {\tt (parent-date}\  {\tt a-parent)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The answer is obviously {{\sf true}\/} if the condition holds. The
{{\tt else}\/} clause contains the remaining expressions.  Of course, the
{{\tt name}\/} and {{\tt date}\/} field have nothing to do with the eye color
of a person, so we can ignore them. This leaves us with 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (parent-children}\  {\tt a-parent)}\end{tabbing}\end{minipage}\end{center}
an expression that extracts the list of children from the {{\tt parent}\/}
structure. 

If the eye color of some {{\tt parent}\/} structure is not {{\tt \char'047}{\tt blue}\/},
we must clearly search the list of children for a blue-eyed descendant.
Following our guidelines for complex functions, we add the function to our
wish list and continue from there. The function that we want to put on a
wish list consumes a list of children and checks whether any of these or
their grandchildren have blue eyes. Here are the contract, header, and
purpose statement:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt blue-eyed-children\char'077}\ {\tt :}\ {\tt list-of-children}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether any of the structures on {{\tt aloc}\/} is blue-eyed}
\\
\hbox{;; or has any blue-eyed descendant}
\\
{\tt (define}\  {\tt (blue-eyed-children\char'077}\  {\tt aloc)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

Using {{\tt blue-eyed-children\char'077}\/} we can complete the definition of 
{{\tt blue-eyed-descendant\char'077}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (blue-eyed-descendant\char'077}\  {\tt a-parent)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt (parent-eyes}\  {\tt a-parent)}\  {\tt \char'047}{\tt blue)}\  {\sf true}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (blue-eyed-children\char'077}\  {\tt (parent-children}\  {\tt a-parent))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
That is, if {{\tt a-parent}\/} doesn't have blue eyes, we just look through
the list of its children. 

Before we can test {{\tt blue-eyed-descendant\char'077}\/}, we must define the
function on our wish list. To make up examples for
{{\tt blue-eyed-children\char'077}\/}, we use the list-of-children definitions in
figure~\ref{fig:bu-familyS}. 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (blue-eyed-children\char'077}\  {\tt (list}\  {\tt Gustav))}\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (blue-eyed-children\char'077}\  {\tt (list}\  {\tt Adam}\  {\tt Dave}\  {\tt Eva))}\\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
Gustav doesn't have blue eyes and doesn't have any recorded descendants.
Hence, {{\tt blue-eyed-children\char'077}\/} produces {{\sf false}\/} for {{\tt (list}{ }{\tt Gustav)}\/}. In contrast, {{\tt Eva}\/} has blue eyes, and therefore
{{\tt blue-eyed-children\char'077}\/} produces {{\sf true}\/} for the second list of
children. 

Since the input for {{\tt blue-eyed-children\char'077}\/} is a list, the template is
the standard pattern: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (blue-eyed-children\char'077}\  {\tt aloc)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt aloc)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt ...}\  {\tt (first}\  {\tt aloc)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (blue-eyed-children\char'077}\  {\tt (rest}\  {\tt aloc))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

Next we consider the two cases. If {{\tt blue-eyed-children\char'077}\/}'s input is
{{\tt empty}\/}, the answer is {{\sf false}\/}. Otherwise we have two
expressions: 
\begin{enumerate}
\item {{\tt (first}\ {\tt aloc)}\/}, which extracts the first item, a
{{\tt parent}\/} structure, from the list; and

\item {{\tt (blue-eyed-children\char'077}\ {\tt (rest}\ {\tt aloc))}\/}, which determines whether
any of the structures on {{\tt aloc}\/} is blue-eyed or has any blue-eyed
descendant. 
\end{enumerate}
Fortunately we already have a function that determines whether a
{{\tt parent}\/} structure or any of its descendants has blue eyes:
{{\tt blue-eyed-descendant\char'077}\/}. This suggests that we check whether
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (blue-eyed-descendant\char'077}\  {\tt (first}\  {\tt aloc))}\end{tabbing}\end{minipage}\end{center}
holds and, if so, {{\tt blue-eyed-children\char'077}\/} can produce {{\sf true}\/}. If
not, the second expression determines whether we have more luck with the
rest of the list. 

Figure~\ref{fig:blue-eyes-bu} contains the complete definitions for both
functions: {{\tt blue-eyed-descendant\char'077}\/} and {{\tt blue-eyed-children\char'077}\/}.
Unlike any other group of functions, these two functions refer to each
other. They are {\sc {mutually recursive}}. Not surprisingly, the mutual
references in the definitions match the mutual references in data
definitions.  The figure also contains a pair of alternative definitions
that use {{\tt or}\/} instead of nested {{\bf cond}-exp\-res\-sion}s.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt blue-eyed-descendant\char'077}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt a-parent}\/} any of the descendants (children, }
\\
\hbox{;; grandchildren, and so on) have {{\tt \char'047}{\tt blue}\/} in the {{\tt eyes}\/} field}
\\
{\tt (d}\={\tt efine}\  {\tt (blue-eyed-descendant\char'077}\  {\tt a-parent)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt (parent-eyes}\  {\tt a-parent)}\  {\tt \char'047}{\tt blue)}\  {\sf true}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (blue-eyed-children\char'077}\  {\tt (parent-children}\  {\tt a-parent))\char'135}{\tt ))}
\\
\hbox{;; {{\tt blue-eyed-children\char'077}\ {\tt :}\ {\tt list-of-children}\ {}{\tt ->}{}\ {\tt boolean}\/}}
\\
\hbox{;; to determine whether any of the structures in {{\tt aloc}\/} is blue-eyed}
\\
\hbox{;; or has any blue-eyed descendant}
\\
{\tt (d}\={\tt efine}\  {\tt (blue-eyed-children\char'077}\  {\tt aloc)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt aloc)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (blue-eyed-descendant\char'077}\  {\tt (first}\  {\tt aloc))}\  {\sf true}{\tt \char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (blue-eyed-children\char'077}\  {\tt (rest}\  {\tt aloc))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\hrulefill

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt blue-eyed-descendant\char'077}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt a-parent}\/} any of the descendants (children, }
\\
\hbox{;; grandchildren, and so on) have {{\tt \char'047}{\tt blue}\/} in the {{\tt eyes}\/} field}
\\
{\tt (d}\={\tt efine}\  {\tt (blue-eyed-descendant\char'077}\  {\tt a-parent)}
\\
\>{\tt (or}\  \={\tt (symbol=?}\  {\tt (parent-eyes}\  {\tt a-parent)}\  {\tt \char'047}{\tt blue)}
\\
\>\>{\tt (blue-eyed-children\char'077}\  {\tt (parent-children}\  {\tt a-parent))))}
\\
\hbox{;; {{\tt blue-eyed-children\char'077}\ {\tt :}\ {\tt list-of-children}\ {}{\tt ->}{}\ {\tt boolean}\/}}
\\
\hbox{;; to determine whether any of the structures in {{\tt aloc}\/} is blue-eyed}
\\
\hbox{;; or has any blue-eyed descendant}
\\
{\tt (d}\={\tt efine}\  {\tt (blue-eyed-children\char'077}\  {\tt aloc)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt aloc)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (or}\  \={\tt (blue-eyed-descendant\char'077}\  {\tt (first}\  {\tt aloc))}
\\
\>\>\>{\tt (blue-eyed-children\char'077}\  {\tt (rest}\  {\tt aloc)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Two programs for finding a blue-eyed descendants}\label{fig:blue-eyes-bu}
\hrule 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 15.1.1}

 \label{ex:be-desc-eval}
Evaluate {{\tt (blue-eyed-descendant\char'077}\ {\tt Eva)}\/} by hand.  Then evaluate
{{\tt (blue-eyed-descendant\char'077}\ {\tt Bettina)}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/be-desc-eval.html}{\rule{3pt}{5pt}}


{\bf Exercise 15.1.2}

 \label{ex:desc-distance}
Develop the function {{\tt how-far-removed}\/}. It determines how far a
blue-eyed descendant, if one exists, is removed from the given parent. If
the given {{\tt parent}\/} has blue eyes, the distance is {{\tt 0}\/}; if
{{\tt eyes}\/} is not blue but some of the structure's children's eyes are,
the distance is {{\tt 1}\/}; and so on.  If no descendant of the given
{{\tt parent}\/} has blue eyes, the function returns {{\sf false}\/} when it is
applied to the corresponding family tree.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/desc-distance.html}{\rule{3pt}{5pt}}


{\bf Exercise 15.1.3}

 \label{ex:desc-count-ft}
Develop the function {{\tt count-descendants}\/}, which consumes a parent
and produces the number of descendants, including the parent. 

Develop the function {{\tt count-proper-descendants}\/}, which consumes a
parent and produces the number of proper descendants, that is, all nodes in
the family tree, not counting the parent.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/desc-count-ft.html}{\rule{3pt}{5pt}}


{\bf Exercise 15.1.4}

 \label{ex:desc-ec-ft}
Develop the function {{\tt eye-colors}\/}, which consumes a parent and
produces a list of all eye colors in the tree. An eye color may occur more
than once in the list. 

\noindent{\bf Hint:} \ Use the Scheme operation {{\tt append}\/}, which consumes two lists
and produces the concatenation of the two lists, {\it e.g.},
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (append}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)}\  {\tt (list}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e))}\  \\
{\tt =}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e)}
\end{tabbing}\end{minipage}\end{center}
We will discuss the development of functions like {{\tt append}\/} in the
last section of this part.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/desc-ec-ft.html}{\rule{3pt}{5pt}}



\subsection{Designing Functions for Mutually-Referential Definitions} 
\label{sec:mutual:design} 
The recipe for designing functions on mutually referential data definitions
generalizes that for self-referential data. Indeed, it offers only two
pieces of additional advice. First, we must create {\em several\/}
templates simultaneously, one for each data definition. Second, we must 
annotate templates with self-references and {\em cross-references\/}, that
is, references among different templates. 



Here is a more detailed explanation of the differences: 
\begin{description}
\item[The data analysis \& design:] If a problem statement discusses
several different kinds of compound information of arbitrary size, we need
a group of data definitions that are self-referential and that refer to
each other. We define such groups in one block. As usual, we identify the
self-references, but we also identify the cross-references.

In our example in the preceding section, the data definition consisted of
two sub-definitions: 
\begin{center}
\begin{picture}(200,120)(65,-20)
\put(40,90){\vector(-4,-1){10}}
\qbezier[500](180,-5)(240,120)(40,90)
\put(70,55){\vector(-2,-1){34}}
\put(60,40){\vector(-4,-1){10}}
\qbezier[500](260,-5)(140,60)(60,40)
\put(-15,40){\begin{minipage}{5in}   \begin{quote} A {\sl {parent}} is a structure:\\
        \centerline{{{\tt (make-parent}\ {\tt loc}\ {\tt n}\ {\tt d}\ {\tt e)}\/}}       where {{\tt loc}\/} is a list of children,       {{\tt n}\/} and {{\tt e}\/} are symbols,       and {{\tt d}\/} is a number.     \bigskip        A {\sl {list of children}} is either 	\begin{enumerate}           \item {{\tt empty}\/} or           \item {{\tt (cons}\ {\tt p}\ {\tt loc)}\/}                  where {{\tt p}\/} is a parent              and {{\tt loc}\/} is a list of children. 	\end{enumerate} \end{quote} \end{minipage}}
\end{picture}
\end{center}

The first one concerns {\em parents\/} and another one for {\em list of children\/}. The first (unconditionally) defines a parent in terms of symbols,
numbers, and a list of children, that is, it contains a cross-reference to the
second definition. This second definition is a conditional definition. Its first
clause is simple; its second clause references both the definition for
{{\tt parent}\/}s and {{\tt list-of-children}\/}. 

\item[Contract, Purpose, Header:] To process interrelated classes of data, we
typically need as many functions as there are class definitions. Hence, we must
formulate as many contracts, purpose statements, and headers in parallel as there
are data definitions. 

%% BEGIN INLINED FILE: idesign6.dl.tex
%% BEGIN INLINED FILE: design1.dl.tex






%% BEGIN INLINED FILE: design3.dl.tex





%% BEGIN INLINED FILE: design4.dl.tex





%% BEGIN INLINED FILE: design5.dl.tex




%% BEGIN INLINED FILE: design6.dl.tex





\hrule
 \begin{center} \begin{tabular}{|l|l|l|}\hline Phase            &  Goal 		& Activity \\
 \hline\hline \begin{minipage}[t]{.60in} Data\\
~~Analysis\\
~~\& Design \end{minipage}  & \begin{minipage}[t]{1.6in}\raggedright to formulate a group of data definitions \rule{0pt}{6pt}\end{minipage} & \begin{minipage}[t]{2.2in}\raggedright {develop a group of mutually recursive data definitions $\bullet$\  at least one definition or one alternative in a definition must refer to basic data $\bullet$\  {\em explicitly identify all references among the data definitions}}\rule{0pt}{6pt}\end{minipage}   \\
 \hline Template        & \begin{minipage}[t]{1.6in}\raggedright to formulate an outline for a group of programs \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {develop {\em as many templates as there are data definitions \em simultaneously} $\bullet$\  develop each templates according to the rules for compound and/or mixed data definitions as appropriate $\bullet$\  annotate the templates with recursions and cross-applications to match the (cross-)references in the data definitions}\rule{0pt}{6pt}\end{minipage}   \\
 \hline Body	 	& \begin{minipage}[t]{1.6in}\raggedright to define the program \rule{0pt}{6pt}\end{minipage}   & \begin{minipage}[t]{2.2in}\raggedright {formulate a Scheme expression for each template, and for each {\bf cond}-clause in a template $\bullet$\  start by explaining what each expression in each template computes $\bullet$\  use additional auxiliary functions where necessary}\rule{0pt}{6pt}\end{minipage}   \\
 \hline \end{tabular} \end{center}
\center{Figure: Designing programs for definitions with mutual references}
\label{fig:design6}
\centerline{The essential steps}
\centerline{(Refines the recipes in  figures~\ref{fig:design1} (pg.~\pageref{fig:design1}),  \ref{fig:design3} (pg.~\pageref{fig:design3}) \ref{fig:design4} (pg.~\pageref{fig:design4}))}
\hrule

\item[Templates:] The templates are created in parallel, following the advice
concerning compound data, mixed data, and self-referential data. Finally, we must
determine for each selector expression in each template whether it corresponds to a
cross-reference to some definition. If so, we annotate it in the same way we
annotate cross-references.

Here are the templates for our running example: 

\begin{picture}(200,150)(5,-30)
\put(60,110){\vector(-4,-1){10}}
\qbezier[500](90,-15)(260,140)(60,110)
\put(50,40){\vector(0,-1){14}}
\put(60,30){\vector(-4,-1){10}}
\qbezier[500](60,30)(140,60)(240,-15)
\put(-15,40){\begin{minipage}{5in} \begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (fun-parent}\  {\tt a-parent)}\\
\>{\tt ...}\  {\tt (parent-name}\  {\tt a-parent)}\  {\tt ...} \\
\>{\tt ...}\  {\tt (parent-date}\  {\tt a-parent)}\  {\tt ...} \\
\>{\tt ...}\  {\tt (parent-eyes}\  {\tt a-parent)}\  {\tt ...} \\
\>{\tt ...}\  {\tt (fun-children}\  {\tt (parent-children}\  {\tt a-parent))}\  {\tt ...)} \end{tabbing}\end{minipage}\end{center}   \begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (d}\={\tt efine}\  {\tt (fun-children}\  {\tt aloc)} \\
\>{\tt (c}\={\tt ond} \\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt aloc)}\  {\tt ...\char'135} \\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (fun-parent}\  {\tt (first}\  {\tt aloc))}\  {\tt ...}\  {\tt (fun-children}\  {\tt (rest}\  {\tt aloc))}\  {\tt ...\char'135}{\tt ))} \end{tabbing}\end{minipage}\end{center}  \end{minipage}}
\end{picture}

The {{\tt fun-for-parent}\/} template is unconditional because the data definition
for {{\tt parent}\/}s does not contain any clauses. It does contain one
cross-reference to the second template: to process the {{\tt children}\/} field of a
{{\tt parent}\/} structure. By the same rules, {{\tt fun-for-loc}\/} is
conditional. The second {{\tt cond}\/}-clause contains one self-reference, for the
{{\tt rest}\/} of the list, and one cross-reference for the {{\tt first}\/} item of
the list, which is a parent structure. 

A comparison of the data definitions and the templates shows how analogous the two
are. To emphasize the similarity in self-references and cross-references, the data
definitions and templates have been annotated with arrows. It is easy to see how
corresponding arrows have the same origin and destination in the two pictures. 

\item[The body:] As we proceed to create the final definitions, we start
with a template or a {{\tt cond}\/}-clause that does not contain
self-references to the template and cross-references to other templates.
The results are typically easy to formulate for such templates or
{{\tt cond}\/}-clauses.

The rest of this step proceeds as before. When we deal with other clauses
or functions, we remind ourselves what each expression in the template
computes, assuming that {\em all\/} functions already work as specified in
the contracts. Then we decide how to combine these pieces of data into a
final answer. As we do that, we must not forget the guidelines concerning
the composition of complex functions (sections~\ref{sec:compounding1}
and~\ref{sec:compounding2}).  
\end{description}
Figure~\ref{fig:design6} summarizes these pieces of advice; it should be
read in conjunction with figures~\ref{fig:design1}, \ref{fig:design3},
\ref{fig:design4}, and~\ref{fig:design5}.

\subsection{Extended Exercise: More on Web Pages} \label{sec:more-wp}

With mutually-referential data definitions we can represent Web pages in a
better manner than in section~\ref{sec:docs}. Here is the basic structure
definition:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-structure}\  {\tt wp}\  {\tt (header}\  {\tt body))}\end{tabbing}\end{minipage}\end{center}
The two fields, {{\tt header}\/} and {{\tt body}\/}, contain the two
essential pieces of data in a Web page: a header (title) and a body. 
The data definition specifies that a body is a list of words and Web pages: 

  \begin{quote} A {\sl {Web page}} is a structure:\\
           \centerline{{{\tt (make-wp}\ {\tt h}\ {\tt p)}\/}}       where {{\tt h}\/} is a symbol and {{\tt p}\/} is a (Web) document.        \bigskip        A ({\em Web\/}) {\sl {document}} is either 	\begin{enumerate}           \item {{\tt empty}\/},           \item {{\tt (cons}\ {\tt s}\ {\tt p)}\/} \\
            where {{\tt s}\/} is a symbol and {{\tt p}\/} is a document, or            \item {{\tt (cons}\ {\tt w}\ {\tt p)}\/} \\
            where {{\tt w}\/} is a Web page and {{\tt p}\/} is a document.  	\end{enumerate} \end{quote}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 15.3.1}

 \label{ex:wp-count}
Develop the function {{\tt size}\/}, which consumes a Web page and produces
the number of symbols (words) it contains. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/wp-count.html}{\rule{3pt}{5pt}}


{\bf Exercise 15.3.2}

 \label{ex:wp-to-file}
\htmladdimg{../icons/teacher.gif}~{\em Advanced students may wish to develop an alternative version of the  function that produces lists of symbols containing the following URL tags:  {{\tt \char'047}{\tt <html>}\/}, {{\tt \char'047}{\tt </html>}\/}, {{\tt \char'047}{\tt <head>}\/}, {{\tt \char'047}{\tt </head>}\/},  {{\tt \char'047}{\tt <body>}\/}, {{\tt \char'047}{\tt </body>}\/}, {{\tt \char'047}{\tt <a}\ {\tt href=>}\/}, and  {{\tt \char'047}{\tt </a>}\/}.}

Develop the function {{\tt wp-to-file}\/}. The function consumes a Web page
and produces a list of symbols. The list contains all the words in a body
and all the headers of embedded Web pages. The bodies of immediately
embedded Web pages are ignored.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/wp-to-file.html}{\rule{3pt}{5pt}}


{\bf Exercise 15.3.3}

 \label{ex:wp-occurs}
Develop the function {{\tt occurs}\/}. It consumes a symbol and a Web page
and determines whether the former occurs anywhere in the latter, including
the embedded Web pages.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/wp-occurs.html}{\rule{3pt}{5pt}}


{\bf Exercise 15.3.4}

 \label{ex:wp-find}
Develop the function {{\tt find}\/}. The function consumes a Web page and a
symbol. It produces {{\sf false}\/}, if the symbol does not occur in the body
of the page or its embedded Web pages. If the symbol occurs at least once, 
it produces a list of the headers that are encountered on the way to the
symbol. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/wp-find.html}{\rule{3pt}{5pt}}


\section{Development through Iterative Refinement} 
\label{sec:files} 
When we develop real functions, we are often confronted with the task of designing a
data representation for complicated forms of information. The best strategy to
approach this task is apply a well-known scientific technique: {\sc {iterative refinement.}} A scientist's problem is to represent a part of the real world using
mathematics. The result of the effort is called a {\sc {model}}. The scientist
then tests the model in many ways, in particular by predicting certain properties
of events. If the model truly captured the essential elements of the real world,
the prediction will be accurate; otherwise, there will be discrepancies between the
predictions and the actual outcomes.  For example, a physicist may start by
representing a jet plane as a point and by predicting its movement in a straight
line using Newton's equations. Later, if there is a need to understand the plane's
friction, the physicist may add certain aspects of the jet plane's contour to the
model. In general, a scientist refines a model and re-test its usefulness until it
is sufficiently accurate.

A programmer or a computing scientist should proceed like a scientist. Since the
representation of data plays a central role in the work of a programmer, the key is
to find an accurate data representation of the real-world information. The best way
to get there in complicated situations is to develop the representation in an
iterative manner, starting with the essential elements and adding more attributes
when the current model is fully understood. 

In this book, we have encountered iterative refinement in many of our extended
exercises. For example, the exercise on moving pictures started with simple circles
and rectangles; later on we developed programs for moving entire lists of shapes.
Similarly, we first introduced Web pages as a list of words and embedded Web pages;
in section~\ref{sec:more-wp} we refined the representation of embedded Web pages.
For all of these exercises, however, the refinement was built into the
presentation.

This section illustrates iterative refinement as a principle of program
development. The goal is to model a file system.  A file system is that
part of the computer that remembers programs and data when the computer is
turned off. We first discuss files in more details and then iteratively
develop three data representations. The last part of the section suggests
some programming exercises for the final model. We will use iterative
refinement again in later sections

\subsection{Data Analysis} \label{sec:files-what}

When we turn a computer off, it should remember the functions and the data we worked
on. Otherwise we have to re-enter everything when we turn it on again. Things that
a computer is to remember for a long time are put into {\em files\/}. A file is a
sequence of small pieces of data. For our purposes, a file resembles a list; we
ignore why and how a computer stores a file in a permanent manner. 

\hrule


\begin{picture}(350,180)
\put(000,000){\begin{picture}(58,20) 
\put(00,00){\line(+1,+0){58}} 
\put(58,00){\line(+0,+1){20}} 
\put(58,20){\line(-1,+0){58}} 
\put(00,20){\line(+0,-1){20}} 
\put(02,05){\mbox{{\tt TS}~(DIR)}} \end{picture}}
  
\put(060,010){\line(1,0){260}}
\put(320,010){\vector(0,1){14}}
  
\put(030,020){\vector(0,1){29}}
  
\put(030,035){\line(1,0){200}}
\put(230,035){\vector(0,1){14}}
\put(000,050){\begin{picture}(58,20) 
\put(00,00){\line(+1,+0){58}} 
\put(58,00){\line(+0,+1){20}} 
\put(58,20){\line(-1,+0){58}} 
\put(00,20){\line(+0,-1){20}} 
\put(02,05){\mbox{{\tt Text}~(DIR)}} \end{picture}}
    
\put(060,060){\line(1,0){35}}
\put(095,060){\vector(0,1){39}}
    
\put(095,085){\line(+1,0){60}}
\put(155,085){\vector(0,1){14}}
    
\put(095,085){\line(-1,0){60}}
\put(035,085){\vector(0,1){14}}
  
\put(000,100){\begin{picture}(58,20) 
\put(00,00){\line(+1,+0){58}} 
\put(58,00){\line(+0,+1){20}} 
\put(58,20){\line(-1,+0){58}} 
\put(00,20){\line(+0,-1){20}} 
\put(02,05){\mbox{{\tt part1}~(99)}} \end{picture}}
  
\put(065,100){\begin{picture}(58,20) 
\put(00,00){\line(+1,+0){58}} 
\put(58,00){\line(+0,+1){20}} 
\put(58,20){\line(-1,+0){58}} 
\put(00,20){\line(+0,-1){20}} 
\put(02,05){\mbox{{\tt part2}~(52)}} \end{picture}}
  
\put(130,100){\begin{picture}(58,20) 
\put(00,00){\line(+1,+0){58}} 
\put(58,00){\line(+0,+1){20}} 
\put(58,20){\line(-1,+0){58}} 
\put(00,20){\line(+0,-1){20}} 
\put(02,05){\mbox{{\tt part3}~(17)}} \end{picture}}
\put(200,050){\begin{picture}(58,20) 
\put(00,00){\line(+1,+0){58}} 
\put(58,00){\line(+0,+1){20}} 
\put(58,20){\line(-1,+0){58}} 
\put(00,20){\line(+0,-1){20}} 
\put(02,05){\mbox{{\tt Libs}~(DIR)}} \end{picture}}
  
\put(230,070){\vector(0,1){29}}
  
\put(230,085){\line(1,0){60}}
\put(290,085){\vector(0,1){14}}
  
\put(200,100){\begin{picture}(58,20) 
\put(00,00){\line(+1,+0){58}} 
\put(58,00){\line(+0,+1){20}} 
\put(58,20){\line(-1,+0){58}} 
\put(00,20){\line(+0,-1){20}} 
\put(02,05){\mbox{{\tt Code}~(DIR)}} \end{picture}}
      
\put(260,110){\line(1,0){1}}
\put(261,110){\line(0,1){20}}
      
\put(261,130){\line(+1,0){20}}
\put(281,130){\vector(0,1){19}}
      
\put(261,130){\line(-1,0){20}}
\put(241,130){\vector(0,1){19}}
    
\put(200,150){\begin{picture}(58,20) 
\put(00,00){\line(+1,+0){58}} 
\put(58,00){\line(+0,+1){20}} 
\put(58,20){\line(-1,+0){58}} 
\put(00,20){\line(+0,-1){20}} 
\put(02,05){\mbox{{\tt hang}~(8)}} \end{picture}}
    
\put(265,150){\begin{picture}(58,20) 
\put(00,00){\line(+1,+0){58}} 
\put(58,00){\line(+0,+1){20}} 
\put(58,20){\line(-1,+0){58}} 
\put(00,20){\line(+0,-1){20}} 
\put(02,05){\mbox{{\tt draw}~(2)}} \end{picture}}
  
\put(265,100){\begin{picture}(58,20) 
\put(00,00){\line(+1,+0){58}} 
\put(58,00){\line(+0,+1){20}} 
\put(58,20){\line(-1,+0){58}} 
\put(00,20){\line(+0,-1){20}} 
\put(02,05){\mbox{{\tt Docs}~(DIR)}} \end{picture}}
    
\put(325,110){\line(1,0){10}}
\put(335,110){\vector(0,1){14}}
  
\put(300,125){\begin{picture}(58,20) 
\put(00,00){\line(+1,+0){58}} 
\put(58,00){\line(+0,+1){20}} 
\put(58,20){\line(-1,+0){58}} 
\put(00,20){\line(+0,-1){20}} 
\put(02,05){\mbox{{\tt read!}~(19)}} \end{picture}}
\put(300,025){\begin{picture}(58,20) 
\put(00,00){\line(+1,+0){58}} 
\put(58,00){\line(+0,+1){20}} 
\put(58,20){\line(-1,+0){58}} 
\put(00,20){\line(+0,-1){20}} 
\put(02,05){\mbox{{\tt read!}~(10)}} \end{picture}}
\end{picture}

\center{Figure: A sample directory tree}\label{fig:accountant}
\hrule

It is more important to us that, on most computer systems, the collection of files
is organized in {\em directories\/}.\footnote{On some computers, a directory is called a {\em folder\/}.}  Roughly speaking, a directory contains some files and
some more directories. The latter are called subdirectories and may contain yet
more subdirectories and files, and so on. The entire collection is collectively
called a {\em file system\/} or a {\em directory tree\/}.

Figure~\ref{fig:accountant} contains a graphical sketch of a small directory
tree.\footnote{The picture explains why computer scientists call such directories trees.} The tree's {\em root directory\/} is {\tt TS}. It contains one file, called
{\tt read!}\relax, and two subdirectories, called {\tt Text} and {\tt Libs}. The first
subdirectory, {\tt Text}, contains only three files; the latter, {\tt Libs},
contains only two subdirectories, each of which contains at least one file. Each
box has one of two annotations. A directory is annotated with DIR, and a file is
annotated with a number, which signifies the file's size. Altogether {\tt TS}
contains seven files and consists of five (sub)directories. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 16.1.1}

 \label{ex:dir0}
How many times does the file name {\tt read!}\relax\ occur in the directory tree {\tt TS}? 
What is the total size of all the files in the tree? How deep is the tree (how many
levels does it contain)?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/dir0.html}{\rule{3pt}{5pt}}


\subsection{Defining Data Classes and Refining Them} \label{sec:files-models}

Let's develop a data representation for file systems using the method of iterative
refinement. The first decision we need to make is what we wish to focus on and what
we wish to ignore. 

Consider the directory tree in figure~\ref{fig:accountant} and let's imagine how it
is created. When a user first creates a directory, it is empty. As time goes by,
the user adds files and directories. In general, a user refers to files by names
but thinks of directories as containers of other things. 

\noindent{\bf Model 1}: Our thought experiment suggests that our first, most primitive
model should focus on files as atomic entities, say a symbol that represents a
file's name, and on the directories' nature as containers. More concretely, we
should think of a directory as just a list that contains files and directories. 

All of this suggests the following two data definitions:

   \begin{quote} A {\sl {file}} is a symbol. \end{quote}

   \begin{quote} A {\sl {directory}} ({{\tt dir}\/}) is either      \begin{enumerate}      \item {{\tt empty}\/};      \item {{\tt (cons}\ {\tt f}\ {\tt d)}\/}         where {{\tt f}\/} is a {{\tt file}\/}         and {{\tt d}\/} is a {{\tt dir}\/}; or      \item {{\tt (cons}\ {\tt d1}\ {\tt d2)}\/}         where {{\tt d1}\/} and {{\tt d2}\/} are {{\tt dir}\/}s.       \end{enumerate} \end{quote}

\noindent The first data definition says that files are represented by
their names. The second one captures how a directory is gradually
{{\tt cons}\/}tructed by adding files and directories.

A closer look at the second data definition shows that the class of
directories is the class of Web pages of section~\ref{sec:docs}. Hence, we
can reuse the template for Web-page processing functions to process
directory trees. If we were to write a function that consume a directory
(tree) and counts how many files are contained, it would be identical to a
function that counts the number of words in a Web tree.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 16.2.1}

 \label{ex:file-example1}
Translate the file system in figure~\ref{fig:accountant} into a Scheme
representation according to model~1.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/file-example1.html}{\rule{3pt}{5pt}}


{\bf Exercise 16.2.2}

 \label{ex:dir-how-many1}
Develop the function {{\tt how-many}\/}, which consumes a {{\tt dir}\/} and
produces the number of files in the {{\tt dir}\/}
tree.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/dir-how-many1.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt} 


\noindent{\bf Model 2}: While the first data definition is familiar to us and
easy to use, it obscures the nature of directories. In particular, it hides
the fact that a directory is not just a collection of files and directories
but has several interesting attributes. To model directories in a more
faithful manner, we must introduce a structure that collects all relevant
properties of a directory. Here is a minimal structure definition:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt dir}\  {\tt (name}\  {\tt content))}\end{tabbing}\end{minipage}\end{center}
It suggests that a directory has a name and a content; other attributes can now be
added as needed. 

The intention of the new definition is that a directory has two attributes:
a name, which is a symbol, and a content, which is a list of files and
directories.  This, in turn, suggests the following data definitions: 

    \begin{quote} A {\sl {directory}} ({{\tt dir}\/}) is a structure:\\
             \centerline{{{\tt (make-dir}\ {\tt n}\ {\tt c)}\/}}         where {{\tt n}\/} is a symbol          and   {{\tt c}\/} is a list of files and directories.  \bigskip          A {\sl {list of files and directories}} ({{\tt LOFD}\/}) is either 	  \begin{enumerate}             \item {{\tt empty}\/};             \item {{\tt (cons}\ {\tt f}\ {\tt d)}\/}                where {{\tt f}\/} is a file and {{\tt d}\/} is a {{\tt LOFD}\/}; or             \item {{\tt (cons}\ {\tt d1}\ {\tt d2)}\/}                where {{\tt d1}\/} is a {{\tt dir}\/} and {{\tt d2}\/} is a {{\tt LOFD}\/}.  	  \end{enumerate} \end{quote}

\noindent Since the data definition for {{\tt dir}\/} refers to the
definition for {{\tt LOFD}\/}s, and the definition for {{\tt LOFD}\/}s refers
back to that of {{\tt dir}\/}s, the two are mutually recursive definitions
and must be introduced together. 

Roughly speaking, the two definitions are related like those of {{\tt parent}\/} and
{{\tt list-of-children}\/} in section~\ref{sec:mut-ref-dd}. This, in turn, means
that the design recipe for programming from section~\ref{sec:mutual:design}
directly apply to {{\tt dir}\/}s and {{\tt LOFD}\/}s. More concretely, to design a
function that processes {{\tt dir}\/}s, we must develop templates for
{{\tt dir}\/}-processing functions {\sl and} {{\tt LOFD}\/}-processing functions {\sl in parallel\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 16.2.3}

 \label{ex:alt-dir1}
Show how to model a directory with two more attributes: a size and a
systems attribute. The former measures how much space the directory itself
(as opposed to its files and subdirectories) consumes; the latter specifies
whether the directory is recognized by the operating system.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/alt-dir1.html}{\rule{3pt}{5pt}}


{\bf Exercise 16.2.4}

 \label{ex:file-example2}
Translate the file system in figure~\ref{fig:accountant} into a Scheme
representation according to model~2.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/file-example2.html}{\rule{3pt}{5pt}}


{\bf Exercise 16.2.5}

 \label{ex:dir-how-many2}
Develop the function {{\tt how-many}\/}, which consumes a {{\tt dir}\/} according to
model~2 and produces the number of files in the {{\tt dir}\/}
tree.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/dir-how-many2.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


\noindent{\bf Model 3}: The second data definition refined the first one with the
introduction of attributes for directories. Files also have attributes. To model 
those, we proceed just as above. First, we define a structure for files: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt file}\  {\tt (name}\  {\tt size}\  {\tt content))}\end{tabbing}\end{minipage}\end{center}
Second, we provide a data definition: 

    \begin{quote} A {\sl {file}} is a structure:\\
         \centerline{{{\tt (make-file}\ {\tt n}\ {\tt s}\ {\tt x)}\/}}         where {{\tt n}\/} is a symbol,               {{\tt s}\/} is a number,                and {{\tt x}\/} is some Scheme value. \end{quote}

\noindent For now, we think of the {{\tt content}\/} field of a file as set
to {{\tt empty}\/}. Later, we will discuss how to get access to the data in
a file.

Finally, let's split the {{\tt content}\/} field of {{\tt dir}\/}s into two
pieces: one for a list of files and one for a list of subdirectories. The
data definition for a list of files is straightforward and relies on
nothing but the definition for {{\tt file}\/}s:

    \begin{quote} A {\sl {list of files}} is either 	  \begin{enumerate}             \item {{\tt empty}\/}, or             \item {{\tt (cons}\ {\tt s}\ {\tt lof)}\/}               where {{\tt s}\/} is a {{\tt file}\/}               and   {{\tt lof}\/} is a list of files.   	  \end{enumerate} \end{quote}

In contrast, the data definitions for {{\tt dir}\/}s and its list of subdirectories
still refer to each other and must therefore be introduced together. Of course, we
first need a structure definition for {{\tt dir}\/}s that has a field for files and
another one for subdirectories:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt dir}\  {\tt (name}\  {\tt dirs}\  {\tt files))}\end{tabbing}\end{minipage}\end{center}
Here are the data definitions: 
    \begin{quote} A {\sl {dir}} is a structure:\\
         \centerline{{{\tt (make-dir}\ {\tt n}\ {\tt ds}\ {\tt fs)}\/}}         where {{\tt n}\/} is a symbol,         {{\tt ds}\/} is a list of directories,         and {{\tt fs}\/} is a list of files.      \bigskip  	A {\em list of directories\/} is either 	  \begin{enumerate}             \item {{\tt empty}\/} or             \item {{\tt (cons}\ {\tt s}\ {\tt lod)}\/}                where {{\tt s}\/} is a {{\tt dir}\/}                and   {{\tt lod}\/} is a list of directories.   	  \end{enumerate} \end{quote}

\noindent This third model (or data representation) of a directory
hierarchy captures the nature of a file system as a user typically
perceives it. With two structure definitions and four data definitions, it
is, however, far more complicated than the first model. But, by starting
with a the simple representation of the first model and refining it step by
step, we have gained a good understanding of how to work with this complex
web of classes.  It is now our job to use the design recipe from
section~\ref{sec:mutual:design} for developing functions on this set of data
definitions. Otherwise, we cannot hope to understand our functions at all.

\subsection{Refining Functions and Programs}

The goal of the following sequence of exercises is to develop several common
utility functions for directory and file systems, using our third and most refined
model. Even though these functions process Scheme-based representation of files and
directories, they give us a good idea how such real-world functions work. 

{\bf Exercise 16.3.1}

 \label{ex:file-example3}
Translate the file system in figure~\ref{fig:accountant} into a Scheme
representation. Remember to use {{\tt empty}\/} for the content of the
files.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/file-example3.html}{\rule{3pt}{5pt}}

To make the exercise more realistic, DrScheme supports the teachpack
{\tt\bf dir.ss}. It introduces the two necessary structure definitions
and a function to create representations of directories according to our
third model:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt create-dir}\ {\tt :}\ {\tt string}\ {}{\tt ->}{}\ {\tt dir}\/}}\\
\hbox{;; to create a representation of the directory that {{\tt a-path}\/} specifies:}
\\
\hbox{;; 1. Windows: {{\tt (create-dir}\ {\tt ``c:\char'134}{\tt \char'134}{\tt windows'')}\/} }
\\
\hbox{;; 2. Mac: {{\tt (create-dir}\ {\tt ``My}\ {\tt Disk:'')}\/} }
\\
\hbox{;; 3. Unix: {{\tt (create-dir}\ {\tt ``\char'057}{\tt home\char'057}{\tt scheme\char'057}{\tt '')}\/}}
\\
{\tt (define}\  {\tt (create-dir}\  {\tt a-path)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Use the function to create some small and large examples based on the
directories in a real computer. {\bf Warning}: For large directory trees,
DrScheme may need a lot of time to build a representation. Use
{{\tt create-dir}\/} on small directory trees first. 


{\bf Exercise 16.3.2}

 \label{ex:dir-how-many3}
Develop the function {{\tt how-many}\/}, which consumes a {{\tt dir}\/} (according to
model~3) and produces the number of files in the {{\tt dir}\/} tree. Test the
function on the directories created in exercise~\ref{ex:file-example3}. Why are we
confident that the function produces correct results?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/dir-how-many3.html}{\rule{3pt}{5pt}}


{\bf Exercise 16.3.3}

 \label{ex:file-du}
Develop the function {{\tt du-dir}\/}. The function consumes a directory and
computes the total size of all the files in the entire directory tree. This
function approximates a true disk-usage meter in that it assumes that
directory don't require storage.

Refine the function to compute approximate sizes for subdirectories. Let's assume
that storing a file and a directory in the {{\tt content}\/} field costs {{\tt 1}\/}
storage unit.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/file-du.html}{\rule{3pt}{5pt}}


{\bf Exercise 16.3.4}

 \label{ex:file-find}
Develop the function {{\tt find\char'077}\/}, which consumes a {{\tt dir}\/} and a file name
and determines whether or not a file with this name occurs in the directory tree.

\noindent {\bf Challenge}: Develop the function {{\tt find}\/}. It consumes a
directory {{\tt d}\/} and a file name {{\tt f}\/}. If {{\tt (find\char'077}\ {\tt d}\ {\tt f)}\/} is true,
it produces a path to the file; otherwise it produces {{\sf false}\/}. A path is a list
of directory names. The first one is that of the given directory; the last one is
that of the subdirectory whose {{\tt files}\/} list contains {{\tt f}\/}. For
example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (find}\  {\tt TS}\  {\tt \char'047}{\tt part3)}\\
{\tt =}\  {\tt (list}\  {\tt \char'047}{\tt TS}\  {\tt \char'047}{\tt Text)}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (find}\  {\tt TS}\  {\tt \char'047}{\tt read!)}\\
{\tt =}\  {\tt (list}\  {\tt \char'047}{\tt TS)}
\end{tabbing}\end{minipage}\end{center}
assuming {{\tt TS}\/} is {{\tt define}\/}d to be the directory in
figure~\ref{fig:accountant}. 

Which {\tt read!}\relax\ file in figure~\ref{fig:accountant} should {{\tt find}\/}
discover? Generalize the function to return a list of paths if the file name occurs
more than once. Each path should lead to a different occurrence, and there should
be a path for each occurrence.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/file-find.html}{\rule{3pt}{5pt}}


\section{Processing Two Complex Pieces of Data} \label{sec:simu}

\htmladdimg{../icons/teacher.gif}~{\em Functions that process two arbitrarily large values come up once in a  while but not too often. Even when they show up, they often belong to the  first two cases, not the third one where there is no obvious  connection. Indeed, the third one is so rare that it is difficult to find  enough interesting examples.}



On occasion, a function consumes two arguments that belong to classes with
non-trivial data definitions. In some cases, one of the arguments should be
treated as if it were atomic; a precisely formulated purpose statement
typically clarifies this. In other cases, the two arguments must be
processed in lockstep. Finally, in a few rare cases, the function must take
into account all possible cases and process the arguments accordingly. This
section illustrates the three cases with examples and provides an augmented
design recipe for the last one. The last section discusses the equality of
compound data and its relationship to testing; it is essential for
automating test suites for functions.

\subsection{Processing Two Lists Simultaneously: Case 1} 
\label{sec:two-inputs:case1}

Consider the following contract, purpose statement, and header: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt replace-eol-with}\ {\tt :}\ {\tt list-of-numbers}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
\hbox{;; to construct a new list by replacing {{\tt empty}\/} in alon1 with alon2}
\\
{\tt (define}\  {\tt (replace-eol-with}\  {\tt alon1}\  {\tt alon2)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
The contract indicates that the function consumes two lists, something that
we have not seen in the past. Let's see how the design recipe works with
this kind of problem.

To begin with, we need to make up examples. Suppose the first input is
{{\tt empty}\/}. Then {{\tt replace-eol-with}\/} should produce the second
argument, no matter what it is:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (replace-eol-with}\  {\tt empty}\  {\tt L)}\  \\
{\tt =}\  {\tt L}
\end{tabbing}\end{minipage}\end{center}
In this equation, {{\tt L}\/} stands for an arbitrary list of numbers, for
example, {{\tt empty}\/}, {{\tt (cons}\ {\tt 1}\ {\tt empty)}\/}, or {{\tt (list}\ {\tt 10}\ {\tt 6}\ {\tt 11}{ }{\tt 9}\ {\tt 1)}\/}. Now suppose the first argument is not {{\tt empty}\/}. Then, as the
name of function and the purpose statement require, we replace the end of the list
with the second argument:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (replace-eol-with}\  {\tt (cons}\  {\tt 1}\  {\tt empty)}\  {\tt L)}\  \\
{\tt =}\  {\tt (cons}\  {\tt 1}\  {\tt L)}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (replace-eol-with}\  {\tt (cons}\  {\tt 2}\  {\tt (cons}\  {\tt 1}\  {\tt empty))}\  {\tt L)}\  \\
{\tt =}\  {\tt (cons}\  {\tt 2}\  {\tt (cons}\  {\tt 1}\  {\tt L))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (replace-eol-with}\  {\tt (cons}\  {\tt 2}\  {\tt (cons}\  {\tt 11}\  {\tt (cons}\  {\tt 1}\  {\tt empty)))}\  {\tt L)}\  \\
{\tt =}\  {\tt (cons}\  {\tt 2}\  {\tt (cons}\  {\tt 11}\  {\tt (cons}\  {\tt 1}\  {\tt L)))}
\end{tabbing}\end{minipage}\end{center}
Again, {{\tt L}\/} stands for any list of numbers we can think of in these
examples. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt replace-eol-with}\ {\tt :}\ {\tt list-of-numbers}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
\hbox{;; to construct a new list by replacing {{\tt empty}\/} in alon1 with alon2}
\\
{\tt (d}\={\tt efine}\  {\tt (replace-eol-with}\  {\tt alon1}\  {\tt alon2)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt ((empty?}\  {\tt alon1)}\  {\tt alon2)}
\\
\>\>{\tt (else}\  {\tt (cons}\  {\tt (first}\  {\tt alon1)}\  {\tt (replace-eol-with}\  {\tt (rest}\  {\tt alon1)}\  {\tt alon2)))))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The complete definition of {\it replace-eol-with\/}} 
\label{fig:append}
\hrule

The examples suggest that it doesn't matter what the second argument
is---as long as it is a list. Otherwise, it doesn't even make sense to
replace {{\tt empty}\/} with the second argument. This implies that the
template should be that of a list-processing function with respect to the
first argument: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (replace-eol-with}\  {\tt alon1}\  {\tt alon2)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt ((empty?}\  {\tt alon1)}\  {\tt ...)}
\\
\>\>{\tt (else}\  {\tt ...}\  {\tt (first}\  {\tt alon1)}\  {\tt ...}\  {\tt (replace-eol-with}\  {\tt (rest}\  {\tt alon1)}\  {\tt alon2)}\  {\tt ...}\  {\tt )))}
\end{tabbing}\end{minipage}\end{center}
The second argument is treated as it were an atomic piece of data. 

Let's fill the gaps in the template, following the design recipe and using
our examples. If {{\tt alon1}\/} is {{\tt empty}\/},
{{\tt replace-eol-with}\/} produces {{\tt alon2}\/} according to our
examples. For the second {{\tt cond}\/}-clause, when {{\tt alon1}\/} is not
{{\tt empty}\/}, we must proceed by inspecting the available expressions: 
\begin{enumerate}
\item {{\tt (first}\ {\tt alon1)}\/} evaluates to the first item on the list, and 

\item {{\tt (replace-eol-with}\ {\tt (rest}\ {\tt alon1)}\ {\tt alon2)}\/} replaces
{{\tt empty}\/} in {{\tt (rest}\ {\tt alon1)}\/} with {{\tt alon2}\/}. 
\end{enumerate}
To gain a better understanding of what this means, consider one of the
examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (replace-eol-with}\  {\tt (cons}\  {\tt 2}\  {\tt (cons}\  {\tt 11}\  {\tt (cons}\  {\tt 1}\  {\tt empty)))}\  {\tt L)}\  \\
{\tt =}\  {\tt (cons}\  {\tt 2}\  {\tt (cons}\  {\tt 11}\  {\tt (cons}\  {\tt 1}\  {\tt L)))}
\end{tabbing}\end{minipage}\end{center}
Here {{\tt (first}\ {\tt alon1)}\/} is {{\tt 2}\/}, {{\tt (rest}\ {\tt alon1)}\/} is
{{\tt (cons}\ {\tt 11}\ {\tt (cons}\ {\tt 1}\ {\tt empty))}\/}, and {{\tt (replace-eol-with}\ {\tt (rest}{ }{\tt alon1)}\ {\tt alon2)}\/} is {{\tt (cons}\ {\tt 11}\ {\tt (cons}\ {\tt 1}\ {\tt alon2))}\/}. We can combine
{{\tt 2}\/} and the latter with {{\tt cons}\/} and can thus obtain the
desired result. More generally, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  {\tt (first}\  {\tt alon1)}\  {\tt (replace-eol-with}\  {\tt (rest}\  {\tt alon1)}\  {\tt alon2)}\  \end{tabbing}\end{minipage}\end{center}
is the answer in the second {{\tt cond}\/}-clause. Figure~\ref{fig:append}
contains the complete definition. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 17.1.1}

 \label{ex:append}
In several exercises, we have used the Scheme operation {{\tt append}\/},
which consumes three lists and juxtaposes their items: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (append}\  {\tt (list}\  {\tt \char'047}{\tt a)}\  {\tt (list}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)}\  {\tt (list}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e}\  {\tt \char'047}{\tt f))}\\
{\tt =}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e}\  {\tt \char'047}{\tt f)}
\end{tabbing}\end{minipage}\end{center}

Use {{\tt replace-eol-with}\/} to define {{\tt our-append}\/}, which acts
just like Scheme's {{\tt append}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/append.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.1.2}

 \label{ex:cross}
Develop {{\tt cross}\/}.  The function consumes a list of symbols and a list
of numbers and produces all possible pairs of symbols and numbers. 

\noindent Example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (cross}\  {\tt \char'047}{\tt (a}\  {\tt b}\  {\tt c)}\  {\tt \char'047}{\tt (1}\  {\tt 2))}\\
{\tt =}\  {\tt (list}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt 1)}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt 2)}\  {\tt (list}\  {\tt \char'047}{\tt b}\  {\tt 1)}\  {\tt (list}\  {\tt \char'047}{\tt b}\  {\tt 2)}\  {\tt (list}\  {\tt \char'047}{\tt c}\  {\tt 1)}\  {\tt (list}\  {\tt \char'047}{\tt c}\  {\tt 2))}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/cross.html}{\rule{3pt}{5pt}}


\subsection{Processing Two Lists Simultaneously: Case 2}
\label{sec:two-inputs:case2}

In section~\ref{sec:list-produce}, we developed the function
{\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\/} for the computation of weekly wages. It consumed a
list of numbers---hours worked per week---and produced a list of weekly
wages. We had based the function on the simplifying assumption that all
employees received the same pay rate. Even a small company, however,
employs people at different rate levels. Typically, the company's
accountant also maintains two collections of information: a permanent one
that, among other things, includes an employee's personal pay-rate, and a
temporary one that records how much time an employee has worked during the
past week. 

The revised problem statement means that the function should consume {\em two\/} lists. To simplify the problem, let us assume that the lists are
just lists of numbers, pay rates and weekly hours. Then here is the problem
statement: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\ {\tt :}\ {\tt list-of-numbers}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
\hbox{;; to construct a new list by multiplying the corresponding items on}
\\
\hbox{;; {{\tt alon1}\/} and {{\tt alon2}\/}}
\\
\hbox{;; {\sc Assumption}: the two lists are of equal length }
\\
{\tt (define}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt alon1}\  {\tt alon2)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
We can think of {{\tt alon1}\/} as the list of pay-rates and of
{{\tt alon2}\/} as the list of hours worked per week. To get the list of
weekly wages, we must multiply the corresponding numbers in the two input
lists. 

Let's look at some examples:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt empty}\  {\tt empty)}\\
{\tt =}\  {\tt empty}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  \={\tt (cons}\  {\tt 5.65}\  {\tt empty)}\  \\
\>{\tt (cons}\  {\tt 40}\  {\tt empty))}
\\
{\tt =}\  {\tt (cons}\  {\tt 226.0}\  {\tt empty)}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  \={\tt (cons}\  {\tt 5.65}\  {\tt (cons}\  {\tt 8.75}\  {\tt empty))}\  \\
\>{\tt (cons}\  {\tt 40}\  {\tt (cons}\  {\tt 30}\  {\tt empty)))}
\\
{\tt =}\  {\tt (cons}\  {\tt 226.0}\  {\tt (cons}\  {\tt 262.5}\  {\tt empty))}
\end{tabbing}\end{minipage}\end{center}
For all three examples the function is applied to two lists of equal
length. As stated in the addendum to the purpose statement, the function
assumes this and, indeed, using the function makes no sense if the condition
is violated. 

The condition on the inputs can also be exploited for the development of
the template. Put more concretely, the condition says that {{\tt (empty?}{ }{\tt alon1)}\/} is true if, and only if, {{\tt (empty?}\ {\tt alon2)}\/} is true; and
furthermore, {{\tt (cons?}\ {\tt alon1)}\/} is true if, and only if, {{\tt (cons?}{ }{\tt alon2)}\/} is true. In other words, the condition simplifies the design of the
template's {{\tt cond}\/}-structure, because it says the template is similar
to that of a plain list-processing function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt alon1}\  {\tt alon2)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt ((empty?}\  {\tt alon1)}\  {\tt ...)}
\\
\>\>{\tt (else}\  {\tt ...}\  {\tt )))}
\end{tabbing}\end{minipage}\end{center}

In the first {{\tt cond}\/}-clause, both {{\tt alon1}\/} and {{\tt alon2}\/}
are {{\tt empty}\/}. Hence, no selector expressions are needed. In the
second clause, both {{\tt alon1}\/} and {{\tt alon2}\/} are
{{\tt cons}\/}tructed lists, which means we need four selector expressions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt alon1}\  {\tt alon2)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt ((empty?}\  {\tt alon1)}\  {\tt ...)}
\\
\>\>{\tt (e}\={\tt lse}
\\
\>\>\>{\tt ...}\  {\tt (first}\  {\tt alon1)}\  {\tt ...}\  {\tt (first}\  {\tt alon2)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (rest}\  {\tt alon1)}\  {\tt ...}\  {\tt (rest}\  {\tt alon2)}\  {\tt ...}\  {\tt )))}
\end{tabbing}\end{minipage}\end{center}
Finally, because the last two are lists of equal length, they make up a
natural candidate for the natural recursion of {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt alon1}\  {\tt alon2)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt ((empty?}\  {\tt alon1)}\  {\tt ...)}
\\
\>\>{\tt (e}\={\tt lse}
\\
\>\>\>{\tt ...}\  {\tt (first}\  {\tt alon1)}\  {\tt ...}\  {\tt (first}\  {\tt alon2)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt (rest}\  {\tt alon1)}\  {\tt (rest}\  {\tt alon2))}\  {\tt ...}\  {\tt )))}
\end{tabbing}\end{minipage}\end{center}
The only unusual aspect of this template is that the recursive application
consists of two expressions, both selector expressions for the two
arguments. But, as we have seen, the idea is easily explicable due to the
assumption that {{\tt alon1}\/} and {{\tt alon2}\/} are of equal length.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\ {\tt :}\ {\tt list-of-numbers}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
\hbox{;; to construct a new list by multiplying the corresponding items on}
\\
\hbox{;; {\sc Assumption}: the two lists are of equal length }
\\
\hbox{;; {{\tt alon1}\/} and {{\tt alon2}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt alon1}\  {\tt alon2)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt ((empty?}\  {\tt alon1)}\  {\tt empty)}
\\
\>\>{\tt (else}\  {\tt (cons}\  \={\tt (weekly-wage}\  {\tt (first}\  {\tt alon1)}\  {\tt (first}\  {\tt alon2))}
\\
\>\>\>{\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt (rest}\  {\tt alon1)}\  {\tt (rest}\  {\tt alon2))))))}
\\
\hbox{;; {{\tt weekly-wage}\ {\tt :}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to compute the weekly wage from {{\tt pay-rate}\/} and {{\tt hours-worked}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (weekly-wage}\  {\tt pay-rate}\  {\tt hours-worked)}
\\
\>{\tt (*}\  {\tt pay-rate}\  {\tt hours-worked))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The complete definition of {\it hours\/}$\longrightarrow${\it wage\/}}
\label{fig:hours-wage}
\hrule

To define the function from here, we follow the design recipe. The first
example implies that the answer for the first {{\tt cond}\/}-clause is
{{\tt empty}\/}. In the second one, we have three values available: 
\begin{enumerate}
\item {{\tt (first}\ {\tt alon1)}\/} evaluates to the first item on the list of
pay-rates; 

\item {{\tt (first}\ {\tt alon2)}\/} evaluates to the first item on the list of
hours worked; and 

\item {{\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\ {\tt (rest}\ {\tt alon1)}\ {\tt (rest}\ {\tt alon2))}\/} computes the list
of weekly wages for the remainders of {{\tt alon1}\/} and {{\tt alon2}\/}. 
\end{enumerate}
We merely need to combine these values to get the final answer. More
specifically, given the purpose statement, we must compute the weekly wage
for the first employee and {{\tt cons}\/}truct a list from that wage and the
rest of the wages. This suggests the following answer for the second
{{\tt cond}\/}-clause: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (cons}\  \={\tt (weekly-wage}\  {\tt (first}\  {\tt alon1)}\  {\tt (first}\  {\tt alon2))}\\
\>{\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt (rest}\  {\tt alon1)}\  {\tt (rest}\  {\tt alon2)))}
\end{tabbing}\end{minipage}\end{center}
The auxiliary function {{\tt weekly-wage}\/} consumes the two first items and
computes the weekly wage. Figure~\ref{fig:hours-wage} contains the complete
definitions. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 17.2.1}

 \label{ex:weekly-wage}
In the real world, {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\/} consumes lists of employee
structures and lists of work structures. An employee structure contains an
employee's name, social security number, and pay rate. A work structure
contains an employee's name and the number of hours worked in a week. The
result is a list of structures that contain the name of the employee and
the weekly wage. 

Modify the function in figure~\ref{fig:hours-wage} so that it works on these
classes of data. Provide the necessary structure definitions and data
definitions. Use the design recipe to guide the modification
process.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/weekly-wage.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.2.2}

 \label{ex:zip}
Develop the function {{\tt zip}\/}, which combines a list of names and a list
phone numbers into a list of phone records. Assuming the following structure
definition: \\
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt phone-record}\  {\tt (name}\  {\tt number))}\  {\tt \char'054}\end{tabbing}\end{minipage}\end{center}
a phone record is constructed with {{\tt (make-phone-record}\ {\tt s}\ {\tt n)}\/} where 
{{\tt s}\/} is a symbol and {{\tt n}\/} is a number. Assume the lists are of
equal length. Simplify the definition, if possible. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/zip.html}{\rule{3pt}{5pt}}



\subsection{Processing Two Lists Simultaneously: Case 3}
\label{sec:two-inputs:case3}

Here is a third problem statement, given as in the form of a function
contract, purpose statement, and header: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt list-pick}\ {\tt :}\ {\tt list-of-symbols}\ {\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\ {}{\tt ->}{}\ {\tt symbol}\/}}\\
\hbox{;; to determine the {{\tt n}\/}th symbol from {{\tt alos}\/}, counting from {{\tt 1}\/};}
\\
\hbox{;; signals an error if there is no {{\tt n}\/}th item}
\\
{\tt (define}\  {\tt (list-pick}\  {\tt alos}\  {\tt n)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
That is, the problem is to develop a function that a natural number and a
list of symbols. Both belong to classes with complex data definitions,
though, unlike for the previous two problems, the classes are distinct. 
Figure~\ref{fig:list-pick-dd} recalls the two definitions. 

\hrule
\noindent{\bf The data definitions:}

  \begin{quote} A {\sl {natural number {{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\/}}} ({{\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\/})    is either     \begin{enumerate}   \item {{\tt 1}\/} or    \item {{\tt (add1}\ {\tt n)}\/} if {{\tt n}\/} is a {{\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\/}.    \end{enumerate} \end{quote}

  \begin{quote} A {\sl {list of symbols}} is either   \begin{enumerate}   \item the empty list, {{\tt empty}\/}, or   \item {{\tt (cons}\ {\tt s}\ {\tt lof)}\/} where {{\tt s}\/} is a symbol and {{\tt lof}\/}   is a list of symbols.     \end{enumerate} \end{quote}

\center{Figure: Data definitions for {\it list-pick\/}}
\label{fig:list-pick-dd}
\hrule


Because the problem is non-standard, we should ensure that our examples
cover all important cases. We usually accomplish this goal by picking one
item per clause in the definition and by choosing elements from basic forms
of data on a random basis. In this example, this procedure implies that we
pick at least two elements from {{\tt list-of-symbols}\/} and two from
{{\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\/}. Let's choose {{\tt empty}\/} and
{{\tt (cons}\ {\tt \char'047}{\tt a}\ {\tt empty)}\/} for the former, and {{\tt 1}\/} and {{\tt 3}\/} for
the latter.  But two choices per argument means four examples total; after
all, there is no immediately obvious connection between the two arguments
and no restriction in the contract:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list-pick}\  {\tt empty}\  {\tt 1)}\  \\
{\tt =}\  {\tt (error}\  {\tt \char'047}{\tt list-pick}\  {\tt ``...'')}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list-pick}\  {\tt (cons}\  {\tt \char'047}{\tt a}\  {\tt empty)}\  {\tt 1)}\\
{\tt =}\  {\tt \char'047}{\tt a}\  
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list-pick}\  {\tt empty}\  {\tt 3)}\  \\
{\tt =}\  {\tt (error}\  {\tt \char'047}{\tt list-pick}\  {\tt ``...'')}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list-pick}\  {\tt (cons}\  {\tt \char'047}{\tt a}\  {\tt empty)}\  {\tt 3)}\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
Only one of the four results is a symbol; in the other cases, we see an
error, indicating that the list doesn't contain enough items.

The discussion on examples indicates that there are indeed four possible,
independent cases that we must consider for the design of the function. We
can discover the four cases by arranging the necessary conditions in a
table format: 
\begin{center}
\begin{tabular}{|l|l|l|}
\hline
                      & {{\tt (empty?}\ {\tt alos)}\/} 
                         & {{\tt (cons?}\ {\tt alos)}\/} 
			    \\
 \hline
{{\tt (=}\ {\tt n}\ {\tt 1)}\/} &  &  \\
 \hline
{{\tt (>}\ {\tt n}\ {\tt 1)}\/} &  &  \\
 \hline
\end{tabular}
\end{center}
The horizontal dimension of the table lists those questions that
{{\tt list-pick}\/} must ask about the list argument; the vertical dimension
lists the questions about the natural number. Furthermore, the partitioning
of the table yields four squares. Each square represents the case when both 
the condition on the horizontal and the one on the vertical are true. We
can express this fact with {{\bf and}-exp\-res\-sion}s in the squares: 
\begin{center}
\begin{tabular}{|l|l|l|}
\hline
                      & {{\tt (null\char'077}\ {\tt alos)}\/} 
                         & {{\tt (cons?}\ {\tt alos)}\/} 
			    \\
 \hline
{{\tt (=}\ {\tt n}\ {\tt 1)}\/} &  
{\rule{0pt}{22pt} \begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  \={\tt (=}\  {\tt n}\  {\tt 1)}\\
\>{\tt (null\char'077}\  {\tt alos))} \end{tabbing}\end{minipage}}       &
{\rule{0pt}{22pt} \begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  \={\tt (=}\  {\tt n}\  {\tt 1)}\\
\>{\tt (cons?}\  {\tt alos))} \end{tabbing}\end{minipage}}\\
\hline
{{\tt (>}\ {\tt n}\ {\tt 1)}\/} &  
{\rule{0pt}{22pt} \begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  \={\tt (>}\  {\tt n}\  {\tt 1)}\\
\>{\tt (null\char'077}\  {\tt alos))} \end{tabbing}\end{minipage}}       &
{\rule{0pt}{22pt} \begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  \={\tt (>}\  {\tt n}\  {\tt 1)}\\
\>{\tt (cons?}\  {\tt alos))} \end{tabbing}\end{minipage}}\\
\hline
\end{tabular}
\end{center}
It is straightforward to check that for any given pair of arguments exactly
one of the four composite claims must evaluate to {{\sf true}\/}.

Using our cases analysis, we can now design the first part of the template,
the conditional expression: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (list-pick}\  {\tt alos}\  {\tt n)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (empty?}\  {\tt alos))}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (>}\  {\tt n}\  {\tt 1)}\  {\tt (empty?}\  {\tt alos))}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (cons?}\  {\tt alos))}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (>}\  {\tt n}\  {\tt 1)}\  {\tt (cons?}\  {\tt alos))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The {{\bf cond}-exp\-res\-sion} asks all four questions, thus distinguishing
all possibilities. Next we must add selector expressions to each
{{\tt cond}\/}-clause if possible:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (list-pick}\  {\tt n}\  {\tt alos)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (and}\  {\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (empty?}\  {\tt alos))}
\\
\>\>\>{\tt ...\char'135}
\\
\>\>{\tt \char'133}\={\tt (and}\  {\tt (>}\  {\tt n}\  {\tt 1)}\  {\tt (empty?}\  {\tt alos))}
\\
\>\>\>{\tt ...}\  {\tt (sub1}\  {\tt n)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}\={\tt (and}\  {\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (cons?}\  {\tt alos))}
\\
\>\>\>{\tt ...}\  {\tt (first}\  {\tt alos)}\  {\tt ...}\  {\tt (rest}\  {\tt alos)...\char'135}
\\
\>\>{\tt \char'133}\={\tt (and}\  {\tt (>}\  {\tt n}\  {\tt 1)}\  {\tt (cons?}\  {\tt alos))}\  
\\
\>\>\>{\tt ...}\  {\tt (sub1}\  {\tt n)}\  {\tt ...}\  {\tt (first}\  {\tt alos)}\  {\tt ...}\  {\tt (rest}\  {\tt alos)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
For {{\tt n}\/}, a natural number, the template contains at most one
selector expression, which determine the predecessor of {{\tt n}\/}.  For
{{\tt alos}\/}, it might contain two. In those cases where either {{\tt (=}{ }{\tt n}\ {\tt 1)}\/} or {{\tt (empty?}\ {\tt alos)}\/} holds, one of the two arguments is atomic
and there is no need for a corresponding selector expression. 

The final step of the template construction demands that we annotate the
template with recursions where the results of selector expressions belong
to the same class as the inputs. In the template for {{\tt list-pick}\/},
this only makes sense in the last {{\tt cond}\/}-clause, which contains 
both a selector expression for {{\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\/} and
one for {{\tt list-of-symbols}\/}. All other clauses contain at most one
relevant selector expression. It is, however, unclear how to form the
natural recursions. If we disregard the purpose of the function, and the
template construction step asks us to do just that, there are at three
possible recursions: 
\begin{enumerate}
\item {{\tt (list-pick}\ {\tt (rest}\ {\tt alos)}\ {\tt (sub1}\ {\tt n))}\/}
\item {{\tt (list-pick}\ {\tt alos}\ {\tt (sub1}\ {\tt n))}\/}
\item {{\tt (list-pick}\ {\tt (rest}\ {\tt alos)}\ {\tt n)}\/}
\end{enumerate}
Since we cannot know which one matters or whether all three matter, we move
on to the next development stage. 

Following the design recipe, let us analyze each {{\tt cond}\/}-clause in
the template and decide what a proper answer is: 

\begin{enumerate}
\item If {{\tt (and}\ {\tt (=}\ {\tt n}\ {\tt 1)}\ {\tt (empty?}\ {\tt alos))}\/} holds, {{\tt list-pick}\/} was
asked to pick the first item from an empty list, which is impossible. The
answer must be an application of error. 

\item If {{\tt (and}\ {\tt (>}\ {\tt n}\ {\tt 1)}\ {\tt (empty?}\ {\tt alos))}\/} holds, {{\tt list-pick}\/} was
again asked to pick an item from an empty list. The answer is also
an error. 

\item If {{\tt (and}\ {\tt (=}\ {\tt n}\ {\tt 1)}\ {\tt (cons?}\ {\tt alos))}\/} holds, {{\tt list-pick}\/} is
supposed to produce the first item from some list. The selector expression 
{{\tt (first}\ {\tt alos)}\/} reminds us how to get this item. It is the answer. 

\item For the final clause, if {{\tt (and}\ {\tt (>}\ {\tt n}\ {\tt 1)}\ {\tt (cons?}\ {\tt alos))}\/} holds,
we must analyze what the selector expressions compute: 


\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt list-pick}\ {\tt :}\ {\tt list-of-symbols}\ {\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\ {}{\tt ->}{}\ {\tt symbol}\/}}\\
\hbox{;; to determine the {{\tt n}\/}th symbol from {{\tt alos}\/}, counting from {{\tt 1}\/};}
\\
\hbox{;; signals an error if there is no {{\tt n}\/}th item}
\\
{\tt (d}\={\tt efine}\  {\tt (list-pick}\  {\tt n}\  {\tt alos)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (empty?}\  {\tt alos))}\  {\tt (error}\  {\tt \char'047}{\tt list-pick}\  {\tt ``list}\  {\tt too}\  {\tt short'')\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (>}\  {\tt n}\  {\tt 1)}\  {\tt (empty?}\  {\tt alos))}\  {\tt (error}\  {\tt \char'047}{\tt list-pick}\  {\tt ``list}\  {\tt too}\  {\tt short'')\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (cons?}\  {\tt alos))}\  {\tt (first}\  {\tt alos)\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (>}\  {\tt n}\  {\tt 1)}\  {\tt (cons?}\  {\tt alos))}\  {\tt (list-pick}\  {\tt (rest}\  {\tt alos)}\  {\tt (sub1}\  {\tt n))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The complete definition of {\it list-pick\/}}
\label{fig:list-pick}
\hrule

\begin{enumerate}
\item {{\tt (first}\ {\tt alos)}\/} selects the first item from the list of
symbols; 
\item {{\tt (rest}\ {\tt alos)}\/} is the rest of the list; and 
\item {{\tt (sub1}\ {\tt n)}\/} is one less that the original given list index. 
\end{enumerate}
Let us consider an example to illustrate the meaning of these
expressions. Suppose {{\tt list-pick}\/} is applied to {{\tt (cons}\ {\tt \char'047}{\tt a}{ }{\tt (cons}\ {\tt \char'047}{\tt b}\ {\tt empty))}\/} and {{\tt 2}\/}:

\begin{minipage}[t]{8in}\begin{tabbing}\  \  \  {\tt (list-pick}\  {\tt (cons}\  {\tt \char'047}{\tt a}\  {\tt (cons}\  {\tt \char'047}{\tt b}\  {\tt empty))}\  {\tt 2)}\end{tabbing}\end{minipage}

The answer must be {{\tt \char'047}{\tt b}\/}, {{\tt (first}\ {\tt alos)}\/} is {{\tt \char'047}{\tt a}\/}, and
{{\tt (sub1}\ {\tt n)}\/} is {{\tt 1}\/}.  Here is what the three natural recursions
would compute with these values:
\begin{enumerate}
\item {{\tt (list-pick}\ {\tt (cons}\ {\tt \char'047}{\tt b}\ {\tt empty)}\ {\tt 1)}\/} produces {{\tt \char'047}{\tt b}\/}, the
desired answer; 
\item {{\tt (list-pick}\ {\tt (cons}\ {\tt \char'047}{\tt a}\ {\tt (cons}\ {\tt \char'047}{\tt b}\ {\tt empty))}\ {\tt 1)}\/} evaluates to 
{{\tt \char'047}{\tt a}\/}, which is a symbol, but the the wrong answer for the original
problem; and 
\item {{\tt (list-pick}\ {\tt (cons}\ {\tt \char'047}{\tt b}\ {\tt empty)}\ {\tt 2)}\/} signals an error because
the index is larger than the length of the list. 
\end{enumerate}
This suggests that we use {{\tt (list-pick}\ {\tt (rest}\ {\tt alos)}\ {\tt (sub1}\ {\tt n))}\/} as the
answer in the last {{\tt cond}\/}-clause. But, example-based reasoning is
often treacherous, so we should try to understand why the expression works
in general. 

Recall that, according to the purpose statement, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list-pick}\  {\tt (rest}\  {\tt alos)}\  {\tt (sub1}\  {\tt n))}\end{tabbing}\end{minipage}\end{center}
picks the ${(n-1)}$st item from {{\tt (rest}\ {\tt alos)}\/}. In other words, for
the second application, we have decreased the index by {{\tt 1}\/},
shortened the list by {\em one\/} item, and now look for an item. Clearly,
the second application always produces the same answer as the first one,
assuming {{\tt alos}\/} and {{\tt n}\/} are ``compound'' values. Hence, our
choice for the last clause is truly justified.
\end{enumerate}


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 17.3.1}

 \label{ex:list-ref}
Develop {{\tt list-pick0}\/}, which picks items from a list
like {{\tt list-pick0}\/} but starts counting at {{\tt 0}\/}.

\noindent Examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list-pick0}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c}\  {\tt \char'047}{\tt d)}\  {\tt 3)}\\
{\tt =}\  {\tt \char'047}{\tt d}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list-pick0}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c}\  {\tt \char'047}{\tt d)}\  {\tt 4)}\\
{\tt =}\  {\tt (error}\  {\tt \char'047}{\tt list-pick0}\  {\tt ``the}\  {\tt list}\  {\tt is}\  {\tt too}\  {\tt short'')}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list-ref.html}{\rule{3pt}{5pt}}


\subsection{Function Simplification}
\label{sec:two-inputs:simplify}

The {{\tt list-pick}\/} function in figure~\ref{fig:list-pick} is more
complicated than necessary. Both the first and the second
{{\tt cond}\/}-clause produce the same answer: {{\sf false}\/}. In other words,
if either 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  {\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (empty?}\  {\tt alos))}\end{tabbing}\end{minipage}\end{center}
or 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  {\tt (>}\  {\tt n}\  {\tt 1)}\  {\tt (empty?}\  {\tt alos))}\end{tabbing}\end{minipage}\end{center}
evaluates to {{\sf true}\/}, the answer is {{\sf false}\/}. We can translate this
observation into a simpler {{\bf cond}-exp\-res\-sion}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (list-pick}\  {\tt n}\  {\tt alos)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (or}\  \={\tt (and}\  {\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (empty?}\  {\tt alos))}\  
\\
\>\>\>{\tt (and}\  {\tt (>}\  {\tt n}\  {\tt 1)}\  {\tt (empty?}\  {\tt alos)))}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (cons?}\  {\tt alos))}\  {\tt (first}\  {\tt alos)\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (>}\  {\tt n}\  {\tt 1)}\  {\tt (cons?}\  {\tt alos))}\  {\tt (list-pick}\  {\tt (rest}\  {\tt alos)}\  {\tt (sub1}\  {\tt n))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The new expression is a direct transliteration of our English observation. 

To simplify this function even more, we need to get acquainted with an
algebraic law concerning booleans: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (or}\  \={\tt (and}\  {\tt condition1}\  {\tt a-condition)}\  \\
\>{\tt (and}\  {\tt condition2}\  {\tt a-condition))}
\\
{\tt =}\  {\tt (and}\  \={\tt a-condition}
\\
\>{\tt (or}\  {\tt condition1}\  {\tt condition2))}
\end{tabbing}\end{minipage}\end{center}
The law is called de Morgan's law of distributivity. Applying it to our
function yields the following: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (list-pick}\  {\tt n}\  {\tt alos)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (and}\  \={\tt (empty?}\  {\tt alos)}
\\
\>\>\>{\tt (or}\  {\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (>}\  {\tt n}\  {\tt 1)))}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (cons?}\  {\tt alos))}\  {\tt (first}\  {\tt alos)\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (>}\  {\tt n}\  {\tt 1)}\  {\tt (cons?}\  {\tt alos))}\  {\tt (list-pick}\  {\tt (rest}\  {\tt alos)}\  {\tt (sub1}\  {\tt n))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
That is, if {{\tt alos}\/} is {{\tt empty}\/} and {{\tt n}\/} is either
{{\tt 1}\/} or greater than {{\tt 1}\/}, the answer is {{\sf false}\/}.

Now consider the second part of the condition: {{\tt (or}\ {\tt (=}\ {\tt n}\ {\tt 1)}\ {\tt (>}\ {\tt n}{ }{\tt 1))}\/}. Because {{\tt n}\/} belongs to {{\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\/}, the
condition is always true. But, if we replace it we true we get 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  \={\tt (empty?}\  {\tt alos)}\\
\>{\tt true)}\  \  \  \  \  \  
\end{tabbing}\end{minipage}\end{center}
which is clearly equivalent to {{\tt (empty?}\ {\tt alos)}\/}. In other words, the
function can be written as 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (list-pick}\  {\tt n}\  {\tt alos)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alos)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (cons?}\  {\tt alos))}\  {\tt (first}\  {\tt alos)\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (>}\  {\tt n}\  {\tt 1)}\  {\tt (cons?}\  {\tt alos))}\  {\tt (list-pick}\  {\tt (rest}\  {\tt alos)}\  {\tt (sub1}\  {\tt n))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
which is already significantly simpler than that in
figure~\ref{fig:list-pick}. 

Still, we can do even better than that. The first condition in the
latest version of {{\tt list-pick}\/} filters out all those cases when
{{\tt alos}\/} is empty. Hence, {{\tt (cons?}\ {\tt alos)}\/} in the next two
clauses is always going to evaluate to {{\sf true}\/}. If we replace the
condition by {{\sf true}\/} and simplify the {{\bf and}-exp\-res\-sion}s, we get
the simplest possible version of {{\tt list-pick}\/}, which is displayed in
figure~\ref{fig:list-pick2}. While this last function is simpler than the
original, it is important to understand that we designed both the original
and the simplified version in a systematic manner and that we can therefore
trust both. If we try to find the simple versions directly, we sooner or
later fail to consider a case and produce flawed functions. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt list-pick}\ {\tt :}\ {\tt list-of-symbols}\ {\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\ {}{\tt ->}{}\ {\tt symbol}\/}}\\
\hbox{;; to determine the {{\tt n}\/}th symbol from {{\tt alos}\/}, counting from {{\tt 1}\/};}
\\
\hbox{;; signals an error if there is no {{\tt n}\/}th item}
\\
{\tt (d}\={\tt efine}\  {\tt (list-pick}\  {\tt n}\  {\tt alos)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alos)}\  {\tt (error}\  {\tt \char'047}{\tt list-pick}\  {\tt ``list}\  {\tt too}\  {\tt short'')\char'135}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt (first}\  {\tt alos)\char'135}
\\
\>\>{\tt \char'133}{\tt (>}\  {\tt n}\  {\tt 1)}\  {\tt (list-pick}\  {\tt (rest}\  {\tt alos)}\  {\tt (sub1}\  {\tt n))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The simplified definition of {\it list-pick\/}}
\label{fig:list-pick2}
\hrule

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 17.4.1}

 \label{ex:replace-eol-simp}
Develop the function {{\tt replace-eol-with}\/} following the strategy of
section~\ref{sec:two-inputs:case2}. Then simplify it
systematically.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/replace-eol-simp.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.4.2}

 \label{ex:list-ref-simpl}
Simplify the function {{\tt list-pick0}\/} from exercise~\ref{ex:list-ref} or
explain why it can't be simplified.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list-ref-simpl.html}{\rule{3pt}{5pt}}


\subsection{Designing Functions that Consume Two Complex Inputs}
\label{sec:two-inputs:design}

On occasion, we will encounter problems that require functions on two
 complex classes of inputs. The most interesting situation occurs when both
 inputs are of unknown size. As we have seen in the three first
 subsections, we may have to deal with such functions in three different
 ways. 

The proper approach to this problem is to follow the general design
 recipe. In particular, we must conduct a data analysis and we must define
 the relevant classes of data. Then we can state the contract and the
 purpose of the function, which, in turn, puts us into a position where we
 can think ahead. Before we continue from this point, we should decide
 which one of the following three situations we are facing:

\begin{enumerate}
\item In some cases, one of the parameters plays a dominant
 role. Conversely, we can think of one of the parameters as an atomic piece
 of data as far as the function is concerned. 

\item In some other cases, the two parameters are synchronized.  They must
 range over the same class of values, and they must have the same
 structure. For example, if we are given two lists, they must have the same
 length. If we are given two Web pages, they must have the same length, and
 where one of them contains an embedded page, the other one does, too. If
 we decide that the two parameters have this equal status and must be
 processed in a synchronized manner, then we can pick one of them and
 organize the function around it. 

\item Finally, in some rare cases, there may not be any obvious connection
 between the two parameters. In this case, we must analyze all possible
 cases before we pick examples and design the template. 
\end{enumerate}
For the first two cases, we use an existing design recipe. The last case
deserves some special consideration. 

After we have decided that a function falls into the third category and
 still before we develop examples and the function template, we develop a
 two-dimensional table. Here is the table for {{\tt list-pick}\/} again:

\begin{center}
\begin{tabular}{l|l|l|l|}
& \multicolumn{3}{|c}{{{\tt alos}\/}}\\
 \hline
&                 & {{\tt (empty?}\ {\tt alos)}\/} & {{\tt (cons?}\ {\tt alos)}\/} \\
 \cline{2-4}
{{\tt n}\/} 
&{{\tt (=}\ {\tt n}\ {\tt 1)}\/} &  &  \\
 \cline{2-4}
&{{\tt (>}\ {\tt n}\ {\tt 1)}\/} &  &  \\
 \cline{2-4}
\end{tabular}
\end{center}

\noindent Along the horizontal direction we enumerate the conditions that
 recognize the subclasses for the first parameter, and along the vertical
 direction we enumerate the conditions for the second parameter. 

The table guides the development of both the set of function examples and
 the function template. As far as the examples are concerned, they must
 cover all possible cases. That is, there must be at least one example for
 each cell in the table. 

As far as the template is concerned, it must have one {{\tt cond}\/}-clause
 per cell. Each {{\tt cond}\/}-clause, in turn, must contain all feasible
 selector expressions for both parameters. If one of the parameters is
 atomic, there is no need for a selector expression. Finally, instead of a
 single natural recursion, we might have several. For {{\tt list-pick}\/},
 we discovered three cases. In general, all possible combinations of
 selector expressions are candidates for a natural recursion. Because we
 can't know which ones are necessary and which ones aren't, we write them
 all down and pick the proper ones for the actual function definition. 

In summary, the design of multi-parameter functions is just a variation on
 the old design-recipe theme. The key idea is to translate the data
 definitions into a table that shows all feasible and interesting
 combinations. The development of function examples and the template
 exploit the table as much as possible. Filling in the gaps in the template
 takes practice, just as with everything else. 

\subsection{Exercises on Processing Two Complex Inputs}
\label{sec:two-inputs:ex}

\htmladdimg{../icons/teacher.gif}~{\em A student's solutions should state which situation was picked and should show a recipe-based solution {\em and\/} a simplified solution, if possible. Ask students to explain simplifications.  }

{\bf Exercise 17.6.1}

 \label{ex:merge} 
Develop the function {{\tt merge}\/}. It consumes two lists of numbers,
sorted in ascending order. It produces a single sorted list of numbers 
that contains all the numbers on both inputs lists (and nothing else). A
number occurs in the output as many times as it occurs on the two input
lists together. 

Here are two examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (merge}\  {\tt (list}\  {\tt 1}\  {\tt 3}\  {\tt 5}\  {\tt 7}\  {\tt 9)}\  {\tt (list}\  {\tt 0}\  {\tt 2}\  {\tt 4}\  {\tt 6}\  {\tt 8))}\\
{\tt =}\  {\tt (list}\  {\tt 0}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5}\  {\tt 6}\  {\tt 7}\  {\tt 8}\  {\tt 9)}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (merge}\  {\tt (list}\  {\tt 1}\  {\tt 8}\  {\tt 8}\  {\tt 11}\  {\tt 12)}\  {\tt (list}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 8}\  {\tt 13}\  {\tt 14))}\\
{\tt =}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 8}\  {\tt 8}\  {\tt 8}\  {\tt 11}\  {\tt 12}\  {\tt 13}\  {\tt 14)}
\end{tabbing}\end{minipage}\end{center}
Make up some more. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/merge.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.6.2}

 \label{ex:hangman-list}
The goal of this exercise is to develop a version of the Hangman game of
section~\ref{sec:hangman} for words of arbitrary length.

Provide a data definition data definition for representing words of
 arbitrary length with lists. A {\sl letter} is represented with the
 symbols {{\tt \char'047}{\tt a}\/} through {{\tt \char'047}{\tt z}\/} plus {{\tt \char'047}{\tt \char'137}\/}\,.

Develop the function {{\tt reveal-list}\/}. It consumes three arguments: 
\begin{enumerate}
\item the {\sl chosen} word, which is the word that we have to guess; 

\item the {\sl status} word, which states how much of the word we have
guessed so far; and

\item a letter, which is our current {\sl guess\/}. 
\end{enumerate}
It produces a new status word, that is, a word that contains
ordinary letters and {{\tt \char'047}{\tt \char'137}\/}\,. The fields in the new status word are
determined by comparing the guess with each pair of letters from the status
word and the chosen word:
\begin{enumerate}
\item If the guess is equal to the letter in the chosen word, the guess is
 the corresponding letter in the new status word.

\item Otherwise, the new letter is the corresponding letter from the status
 word.
\end{enumerate}

Test the function with the following examples:
\begin{enumerate}
\item {{\tt (reveal}\ {\tt (list}\ {\tt \char'047}{\tt t}\ {\tt \char'047}{\tt e}\ {\tt \char'047}{\tt a)}\ {\tt (list}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt e}\ {\tt \char'047}{\tt \char'137}{\tt )}\ {\tt \char'047}{\tt u)}\/}
\item {{\tt (reveal}\ {\tt (list}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt e)}\ {\tt (list}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt \char'137}{\tt )}\ {\tt \char'047}{\tt e)}\/}
\item {{\tt (reveal}\ {\tt (list}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\ {\tt (list}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt \char'137}{\tt )}\ {\tt \char'047}{\tt l)}\/}
\end{enumerate}
First determine what the result should be. 

Use the teachpack {\tt\bf hangman.ss} and the functions {{\tt draw-next-part}\/}
 (from exercise~\ref{ex:draw-next}) and {{\tt reveal-list}\/} to play the
 Hangman game. Evaluate the following expression:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (hangman-list}\  {\tt reveal-list}\  {\tt draw-next-part)}\end{tabbing}\end{minipage}\end{center}
The function {{\tt hangman-list}\/} choses a word randomly and pops up a
window with a choice menu for letters. Choose letters and, when ready,
click on the Check button to see whether your guess is
correct. Enjoy!~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hangman-list.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.6.3}

 \label{ex:wage-general} 
In a factory, employees punch time cards as they arrive in the morning and
leave in the evening. In the modern age of electronic punch cards, a punch
card contains an employee number and the number of hours worked. Also, 
employee records always contain the name of the employee, an employee
number, and a pay rate. 

Develop the function {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages2\/}\/}. The function consumes a list of
employee records and a list of (electronic) punch cards. It computes the
weekly wage for each employee by matching the employee record with a punch
card based on employee numbers. If a pair is missing or if a pair's
employee numbers are mismatched, the function stops with an appropriate
error message. Assume that there is at most one card per employee and
employee number. 

\noindent{\bf Hint:} \ An accountant would sort the two lists by employee number
first.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/wage-general.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.6.4}

 \label{ex:polyn}
A {\em linear combination\/} is the sum of many linear terms, {\it i.e.}, products
of variables and numbers. The latter are called coefficients in this
context. Here are some examples:
$$\begin{array}{ll}
5 \cdot x & \\
5 \cdot x & + 17 \cdot y \\
5 \cdot x & + 17 \cdot y + 3 \cdot z 
\end{array}$$
In all three examples, the coefficient of {\it x\/} is 5, that of
{\it y\/} is 17, and the one for {\it z\/} is 3. 

If we are given values for variables, we can determine the value of a
polynomial. For example, if $x = 10$, the value of $5 \cdot x$ is $50$; if
$x = 10$ and $y = 1$, the value of $5 \cdot x + 17 \cdot y$ is $67$; and if
$x = 10$, $y = 1$, and $z = 2$, the value of $5 \cdot x + 17 \cdot y + 3
\cdot z$ is $73$. 

In the past, we would have developed functions to compute the values of
linear combinations for specific values. An alternative representation is a
list of its coefficients. The above combinations would be represented as: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  {\tt 5)}\\
{\tt (list}\  {\tt 5}\  {\tt 17)}
\\
{\tt (list}\  {\tt 5}\  {\tt 17}\  {\tt 3)}
\end{tabbing}\end{minipage}\end{center}
This representation assumes that we always agree on using variables in a
fixed order. 

Develop the function {{\tt value}\/}. It consumes the representation of a
polynomial and a list of numbers. The lists are of equal length. It
produces the value of the polynomial for these values.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/polyn.html}{\rule{3pt}{5pt}} 


{\bf Exercise 17.6.5}

 \label{ex:perm-xmas} 
\htmladdimg{../icons/teacher.gif}~{\em This exercise requires \ref{ex:permutations} and \ref{ex:random}. Provide students with solutions.}
Louise, Jane, Laura, Dana, and Mary are sisters who would like to save money
and work on Christmas gifts. So they decide to hold a lottery, which assigns
to each one of them a single gift recipient. Since Jane is a computer
programmer, they ask her to write a program that performs the lottery in an
impartial manner. Of course, the program must not assign any of the sisters
to herself.

Here is the definition of {{\tt gift-pick}\/}. It consumes a list of
distinct names (symbols) and randomly picks one of those arrangements of
the list that do not agree with the original list at any position.
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt gift-pick:}\ {\tt list-of-names}\ {}{\tt ->}{}\ {\tt list-of-names}\/}}\\
\hbox{;; to pick a ``random'' non-identity arrangement of {{\tt names}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (gift-pick}\  {\tt names)}
\\
\>{\tt (r}\={\tt andom-pick}
\\
\>\>{\tt (non-same}\  {\tt names}\  {\tt (arrangements}\  {\tt names))))}
\end{tabbing}\end{minipage}\end{center}
Recall that {{\tt arrangements}\/} (see exercise~\ref{ex:permutations})
consumes a list of symbols and produces the list of all re-arrangements of
the items in the list. 

Develop the auxiliary functions
\begin{enumerate}
\item {{\tt random-pick}\ {\tt :}\ {\tt list-of-list-of-names}\ {}{\tt ->}{}\ {\tt list-of-names}\/}, which
consumes a list of items and randomly picks one of them as the result;

\item 
{{\tt non-same}\ {\tt :}\ {\tt list-of-names}\ {\tt list-of-list-of-names}\ {}{\tt ->}{}\ {\tt list-of-list-of-names}\/}, 
which consumes a list of names {{\tt L}\/} and a list of arrangements and
produces the list of those that do not agree with {{\tt L}\/} at any
position.

Two permutations agree at some position if we can extract the same name
from both lists by applying {{\tt first}\/} and the same number of
{{\tt rest}\/} operations to both. For example, {{\tt (list}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt c)}\/} 
and {{\tt (list}\ {\tt \char'047}{\tt c}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt b)}\/} do not agree, but {{\tt (list}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt c)}\/}
and {{\tt (list}\ {\tt \char'047}{\tt c}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt a)}\/} agree at the second position. We can prove
that by applying {{\tt rest}\/} followed by {{\tt first}\/} to both lists.
\end{enumerate}
Follow the appropriate recipe in each case carefully. 

\noindent{\bf Hint:} \ Recall that {{\tt (random}\ {\tt n)}\/} picks a random number between
{{\tt 0}\/} and {{\tt n}\/} (cmp. exercise~\ref{ex:random}).~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/perm-xmas.html}{\rule{3pt}{5pt}}



{\bf Exercise 17.6.6}

 \label{ex:dna-prefix}
Develop the function {{\tt DNAprefix}\/}. The function takes two arguments,
both lists of symbols (only {{\tt \char'047}{\tt a}\/}, {{\tt \char'047}{\tt c}\/}, {{\tt \char'047}{\tt g}\/}, and
{{\tt \char'047}{\tt t}\/} occur in DNA, but we can safely ignore this issue here). The
first list is called a {\em pattern\/}, the second one a {\em search-string\/}. The function returns {{\sf true}\/} if the pattern is a
prefix of the search-string.  In all other cases, the function returns
{{\sf false}\/}, {\it e.g.},
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (DNAprefix}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt t)}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt t}\  {\tt \char'047}{\tt c))}\  \\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (DNAprefix}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt t)}\  {\tt (list}\  {\tt \char'047}{\tt a))}\  \\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (DNAprefix}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt t)}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt t))}\  \\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (DNAprefix}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt c}\  {\tt \char'047}{\tt g}\  {\tt \char'047}{\tt t)}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt g))}\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (DNAprefix}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt c}\  {\tt \char'047}{\tt c)}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt c))}\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
Simplify {{\tt DNAprefix}\/}, if possible.

Modify {{\tt DNAprefix}\/} so that it returns the first item beyond the
pattern in the search-string if it the pattern is a proper prefix of the
search-string. If the lists do not match or if the pattern is no shorter
than the search-string, the modified function should still return
{{\sf false}\/}. Similarly, if the lists are equally long and match, the result
is still {{\sf true}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (DNAprefix}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt t)}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt t}\  {\tt \char'047}{\tt c))}\  \\
{\tt =}\  {\tt \char'047}{\tt c}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (DNAprefix}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt t)}\  {\tt (list}\  {\tt \char'047}{\tt a))}\  \\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (DNAprefix}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt t)}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt t))}\  \\
{\tt =}\  {\sf true}
\end{tabbing}\end{minipage}\end{center}
Can this variant of {{\tt DNAprefix}\/} be simplified?  If so, do it. If
not, explain why. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/dna-prefix.html}{\rule{3pt}{5pt}}


\subsection{Extended Exercise: Evaluating Scheme, Part 2} 
\label{sec:interpreter2} 

The goal of this section is to extend the interpreter of
section~\ref{sec:interpreter} so that it can cope with function
applications and function definitions. In other words, the new interpreter
simulates what happens in DrScheme when we enter an expression in the
{\tt Interactions} window after clicking {\tt Execute}. To make
things simple, we assume that all functions in the {\tt Definitions}
window consume one argument. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 17.7.1}

 \label{ex:scheme-one-def}
Extend the data definition of exercise~\ref{ex:scheme-dd} so that we can
represent the application of a function to an expression. The application
should be represented as a structure with two fields. The first field 
contains the name of the function, the second one the representation of the 
argument expression.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/scheme-one-def.html}{\rule{3pt}{5pt}}
 

A full-fledged evaluator can also deal with function definitions.  

{\bf Exercise 17.7.2}

 \label{ex:scheme-prog-dd}
Provide a structure definition and a data definition for definitions. 
Recall that a function definition has three essential attributes: 
\begin{enumerate}
\item the function's name, 
\item the parameter name, and 
\item the function's body. 
\end{enumerate}
This suggests the introduction of a structure with three fields. The first
two contain symbols, the last one a representation of the function's body,
which is an expression. 

Translate the following definitions into Scheme values:  
\begin{enumerate}
\item {{\tt (define}\ {\tt (f}\ {\tt x)}\ {\tt (+}\ {\tt 3}\ {\tt x))}\/}
\item {{\tt (define}\ {\tt (g}\ {\tt x)}\ {\tt (*}\ {\tt 3}\ {\tt x))}\/}
\item {{\tt (define}\ {\tt (h}\ {\tt u)}\ {\tt (f}\ {\tt (*}\ {\tt 2}\ {\tt u)))}\/}
\item {{\tt (define}\ {\tt (i}\ {\tt v)}\ {\tt (+}\ {\tt (*}\ {\tt v}\ {\tt v)}\ {\tt (*}\ {\tt v}\ {\tt v)))}\/}
\item {{\tt (define}\ {\tt (k}\ {\tt w)}\ {\tt (*}\ {\tt (h}\ {\tt w)}\ {\tt (i}\ {\tt w)))}\/}
\end{enumerate}
Make up more examples and translate them, too.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/scheme-prog-dd.html}{\rule{3pt}{5pt}}


\htmladdimg{../icons/teacher.gif}~{\em These exercises are challenging. The evaluation of an application is a {\em generative\/} recursion and does not fit the design recipes we have seen so far. We will deal with this form of recursion in part~\ref{part:recursion}.}

{\bf Exercise 17.7.3}

 \label{ex:scheme-eval-def1}
Develop {{\tt interpret-with-one-def}\/}. The function consumes (the
representation of) a Scheme expression and (the representation of) a single
function definition, {{\tt P}\/}. 

The remaining expressions from exercise~\ref{ex:scheme-dd} are interpreted
as before. For (the representation of) a variable, the function signals an
error.  For an application of the function {{\tt P}\/},
{{\tt interpret-with-one-def}\/}
\begin{enumerate}
\item evaluates the argument, 
\item substitutes the value of the argument for the function parameter in the
function's body; and 
\item evaluates the new expression via recursion. Here is a sketch of the
idea:\footnote{We discuss this form of recursion in detail in part~\ref{part:recursion}.}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (interpret-with-one-def}\  \={\tt (substitute}\  {\tt val-of-arg}\  {\tt fun-para}\  {\tt fun-body)}\\
\>{\tt a-fun-def)}
\end{tabbing}\end{minipage}\end{center}
\end{enumerate}
For all other function applications, {{\tt interpret-with-one-def}\/} signals
an error.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/scheme-eval-def1.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.7.4}

 \label{ex:scheme-eval-def}
Develop the function {{\tt interpret-with-defs}\/}. The function consumes (the
 representation of) a Scheme expression and a list of (representations of)
 function definitions, {{\tt defs}\/}. The function produces the number that
 DrScheme would produce if we were to evaluate the actual Scheme expression
 in the {\tt Interactions} window and if the {\tt Definitions} window
 contained the actual definitions.

The remaining expressions from exercise~\ref{ex:scheme-dd} are interpreted
as before. For an application of the function {{\tt P}\/},
{{\tt interpret-with-defs}\/}
\begin{enumerate}
\item evaluates the argument, 
\item looks up the definition of the function in {{\tt defs}\/},
\item substitutes the value of the argument for the function parameter in the
function's body; and 
\item evaluates the new expression via recursion.
\end{enumerate}
Like DrScheme, {{\tt interpret-with-defs}\/} signals an error for a function
application whose function name is not on the list and for (the
representation of) a variable.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/scheme-eval-def.html}{\rule{3pt}{5pt}}



\subsection{Equality and Testing} \label{sec:equal-test}

Many of the functions we designed produce lists. When we test these
functions, we must compare their result with the predicted value, which are
both lists. Comparing lists by hand is tedious and error-prone. Let's
develop a function that consumes two lists of numbers and determines
whether they are equal: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt list=\char'077}\ {\tt :}\ {\tt list-of-numbers}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt a-list}\/} and {{\tt another-list}\/} }
\\
\hbox{;; contain the same numbers in the same order}
\\
{\tt (define}\  {\tt (list=\char'077}\  {\tt a-list}\  {\tt another-list)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
The purpose statement refines our general claim and reminds us that, for
example, shoppers may consider two lists equal if they contain the same
items, regardless of the order, but programmers are more specific and
include the order in the comparison. The contract and the purpose statement
also show that {{\tt list=\char'077}\/} is a function that processes two complex
values, and indeed, it is an interesting case study. 

Comparing two lists means to look at each item in both lists. This rules
out designing {{\tt list=\char'077}\/} along the lines of {{\tt replace-eol-with}\/}
in section~\ref{sec:two-inputs:case1}. At first glance, there is also no
connection between the two lists, which suggests that we should use the
modified design recipe. 

Let's start with the table: 
\begin{center}
\begin{tabular}{|l|l|l|}
\hline
                      & {{\tt (empty?}\ {\tt a-list)}\/} 
                         & {{\tt (cons?}\ {\tt a-list)}\/} 
			    \\
 \hline
{{\tt (empty?}\ {\tt another-list)}\/} &  &  \\
 \hline
{{\tt (cons?}\ {\tt another-list)}\/}  &  &  \\
 \hline
\end{tabular}
\end{center}
It has four cells, which implies that we need (at least) four tests and
four {{\tt cond}\/}-clauses in the template. 

Here are five tests: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list=\char'077}\  {\tt empty}\  {\tt empty)}\  \\
{\tt =}\  {\tt true}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list=\char'077}\  {\tt empty}\  {\tt (cons}\  {\tt 1}\  {\tt empty))}\\
{\tt =}\  {\tt false}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list=\char'077}\  {\tt (cons}\  {\tt 1}\  {\tt empty)}\  {\tt empty)}\\
{\tt =}\  {\tt false}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list=\char'077}\  \={\tt (cons}\  {\tt 1}\  {\tt (cons}\  {\tt 2}\  {\tt (cons}\  {\tt 3}\  {\tt empty)))}\  \\
\>{\tt (cons}\  {\tt 1}\  {\tt (cons}\  {\tt 2}\  {\tt (cons}\  {\tt 3}\  {\tt empty))))}
\\
{\tt =}\  {\tt true}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list=\char'077}\  \={\tt (cons}\  {\tt 1}\  {\tt (cons}\  {\tt 2}\  {\tt (cons}\  {\tt 3}\  {\tt empty)))}\  \\
\>{\tt (cons}\  {\tt 1}\  {\tt (cons}\  {\tt 3}\  {\tt empty)))}
\\
{\tt =}\  {\tt false}
\end{tabbing}\end{minipage}\end{center}
The second and third show that {{\tt list=\char'077}\/} must deal with its arguments
in a symmetric fashion. The last two show how {{\tt list=\char'077}\/} can produce
{{\tt true}\/} and {{\tt false}\/}. 

Three of the template's four {{\tt cond}\/}-clauses contain selector
expressions and one contains natural recursions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (list=\char'077}\  {\tt a-list}\  {\tt another-list)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (empty?}\  {\tt a-list)}\  {\tt (empty?}\  {\tt another-list))}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}\={\tt (and}\  {\tt (cons?}\  {\tt a-list)}\  {\tt (empty?}\  {\tt another-list))}
\\
\>\>\>{\tt ...}\  {\tt (first}\  {\tt a-list)}\  {\tt ...}\  {\tt (rest}\  {\tt a-list)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (empty?}\  {\tt a-list)}\  {\tt (cons?}\  {\tt another-list))}\  
\\
\>\>{\tt ...}\  {\tt (first}\  {\tt another-list)}\  {\tt ...}\  {\tt (rest}\  {\tt another-list)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}\={\tt (and}\  {\tt (cons?}\  {\tt a-list)}\  {\tt (cons?}\  {\tt another-list))}
\\
\>\>\>{\tt ...}\  {\tt (first}\  {\tt a-list)}\  {\tt ...}\  {\tt (first}\  {\tt another-list)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (list=\char'077}\  {\tt (rest}\  {\tt a-list)}\  {\tt (rest}\  {\tt another-list))}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (list=\char'077}\  {\tt a-list}\  {\tt (rest}\  {\tt another-list))}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (list=\char'077}\  {\tt (rest}\  {\tt a-list)}\  {\tt another-list)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
There are three natural recursions in the fourth clause because we can pair
the two selector expressions and we can pair each parameter with one
selector expression. 

From the template to the complete definition is only a small step. Two
lists can only contain the same items if they are both empty or
{{\tt cons}\/}tructed. This immediately implies {{\tt true}\/} as the answer
for the first {{\tt clause}\/} and {{\tt false}\/} for the next two.  In the
last clause, we have two numbers, the first of both lists, and three
natural recursions. We must compare the two numbers. Furthermore,
{{\tt (list=\char'077}\ {\tt (rest}\ {\tt a-list)}\ {\tt (rest}\ {\tt another-list))}\/} computes whether the
rests of the two lists are equal. The two lists are equal if, and only if,
both conditions hold, which means we must combine them with an
{{\tt and}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (list=\char'077}\  {\tt a-list}\  {\tt another-list)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (empty?}\  {\tt a-list)}\  {\tt (empty?}\  {\tt another-list))}\  {\tt true\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (cons?}\  {\tt a-list)}\  {\tt (empty?}\  {\tt another-list))}\  {\tt false\char'135}
\\
\>\>{\tt \char'133}{\tt (and}\  {\tt (empty?}\  {\tt a-list)}\  {\tt (cons?}\  {\tt another-list))}\  {\tt false\char'135}
\\
\>\>{\tt \char'133}\={\tt (and}\  {\tt (cons?}\  {\tt a-list)}\  {\tt (cons?}\  {\tt another-list))}
\\
\>\>\>{\tt (and}\  \={\tt (=}\  {\tt (first}\  {\tt a-list)}\  {\tt (first}\  {\tt another-list))}
\\
\>\>\>\>{\tt (list=\char'077}\  {\tt (rest}\  {\tt a-list)}\  {\tt (rest}\  {\tt another-list)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The other two natural recursions play no role. 

Let us now take a second look at the connection between the two
parameters. The first development suggests that the second parameter must
have the same shape as the first one, if the two lists are to be equal. Put
differently, we could develop the function based on the structure of the
first parameter and check structure of the other one as needed. 

The first parameter is a list of numbers, so we can reuse the template for
list-processing functions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (list=\char'077}\  {\tt a-list}\  {\tt another-list)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}\={\tt (cons?}\  {\tt a-list)}\  
\\
\>\>\>{\tt ...}\  {\tt (first}\  {\tt a-list)}\  {\tt ...}\  {\tt (first}\  {\tt another-list)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (list=\char'077}\  {\tt (rest}\  {\tt a-list)}\  {\tt (rest}\  {\tt another-list))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The only difference is that the second clause processes the second
parameter in the same way as the first one. This mimics the development of
{\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\/} in section~\ref{sec:two-inputs:case2}. 

Filling the gaps in this template is more difficult than for the first
development of {{\tt list=\char'077}\/}. If {{\tt a-list}\/} is {{\tt empty}\/}, the
answer depends on {{\tt another-list}\/}. As the examples show, the answer
is {{\tt true}\/} if, and only if, {{\tt another-list}\/} is also
{{\tt empty}\/}. Translated into Scheme this means that the answer in the
first {{\tt cond}\/}-clause is {{\tt (empty?}\ {\tt another-list)}\/}. 

If {{\tt a-list}\/} is not empty, the template suggests that we compute the
answer from 
\begin{enumerate}
\item {{\tt (first}\ {\tt a-list)}\/}, the first number of {{\tt a-list}\/}; 

\item {{\tt (first}\ {\tt another-list)}\/}, the first number on
{{\tt another-list}\/}; and 

\item {{\tt (list=\char'077}\ {\tt (rest}\ {\tt a-list)}\ {\tt (rest}\ {\tt another-list))}\/}, which determines
whether the rests of the two lists are equal. 
\end{enumerate}
Given the purpose of the function and the examples, we now simply compare
{{\tt (first}\ {\tt a-list)}\/} and {{\tt (first}\ {\tt another-list)}\/} and combine the
result with the natural recursion in an {{\bf and}-exp\-res\-sion}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  \={\tt (=}\  {\tt (first}\  {\tt a-list)}\  {\tt (first}\  {\tt another-list))}\\
\>{\tt (list=\char'077}\  {\tt (rest}\  {\tt a-list)}\  {\tt (rest}\  {\tt another-list)))}
\end{tabbing}\end{minipage}\end{center}
While this step appears to be simple and straightforward, the result is an
improper definition. The purpose of spelling out the conditions in a 
{{\bf cond}-exp\-res\-sion} is to ensure that all selector expressions are
appropriate. Nothing in the specification of {{\tt list=\char'077}\/}, however,
suggests that {{\tt another-list}\/} is {{\tt cons}\/}tructed if
{{\tt a-list}\/} is {{\tt cons}\/}tructed. 

We can overcome this problem with an additional condition:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (list=\char'077}\  {\tt a-list}\  {\tt another-list)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list)}\  {\tt (empty?}\  {\tt another-list)\char'135}
\\
\>\>{\tt \char'133}\={\tt (cons?}\  {\tt a-list)}\  
\\
\>\>\>{\tt (and}\  \={\tt (cons?}\  {\tt another-list)}
\\
\>\>\>\>{\tt (and}\  \={\tt (=}\  {\tt (first}\  {\tt a-list)}\  {\tt (first}\  {\tt another-list))}
\\
\>\>\>\>\>{\tt (list=\char'077}\  {\tt (rest}\  {\tt a-list)}\  {\tt (rest}\  {\tt another-list))))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The additional condition is {{\tt (cons?}\ {\tt another-list)}\/}, which means that
{{\tt list=\char'077}\/} produces {{\tt false}\/} if {{\tt (cons?}\ {\tt a-list)}\/} is true
and {{\tt (cons?}\ {\tt another-list)}\/} is empty. As the examples show, this is
the desired outcome. 

In summary, {{\tt list=\char'077}\/} shows that, on occasion, we can use more than one
design recipe to develop a function. The outcomes are different, though closely
related; indeed, we could prove that the two always produce the same results for
the same inputs. Also, the second development benefited from the first one.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 17.8.1}

 \label{ex:list=4} 
Test both versions of {{\tt list=\char'077}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list=4.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.8.2}

 \label{ex:list=2} 
Simplify the first version of {{\tt list=\char'077}\/}.  Simplifying means to merge
neighboring {{\tt cond}\/}-clauses with the same result by combining their
conditions in an {{\bf or}-exp\-res\-sion}; to switch {{\tt cond}\/}-clauses;
or to use {{\tt else}\/} in the last clause of the final
version.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list=2.html}{\rule{3pt}{5pt}} 


{\bf Exercise 17.8.3}

 \label{ex:list=1} 
Develop {{\tt sym-list=\char'077}\/}.  The function determines whether two lists of
symbols are equal.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list=1.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.8.4}

 \label{ex:list=5} 
Develop {{\tt contains-same-numbers}\/}.  The function determines whether
two lists of numbers contain the numbers, regardless of the ordering. Thus,
for example,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (contains-same-numbers}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3)}\  {\tt (list}\  {\tt 3}\  {\tt 2}\  {\tt 1))}\end{tabbing}\end{minipage}\end{center}
evaluates to {{\tt true}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list=5.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.8.5}

 \label{ex:list=3} 
The class of numbers, symbols, and booleans are sometimes called
atoms:\footnote{Some people also include {{\tt empty}\/} and keyboard characters ({{\tt char}\/}s).}

  \begin{quote} An {\sl {atom}} is either      \begin{enumerate}       \item a number        \item a boolean       \item a symbol      \end{enumerate} \end{quote}

\noindent Develop the function {{\tt list-equal\char'077}\/}, which consumes two
lists of atoms and determines whether they are
equal.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/list=3.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}



A comparison between the two versions of {{\tt list=\char'077}\/} suggests that the
second one is easier to understand than the first. It says that two compound
values are equal if the second is made from the same constructor and the
components are equal. In general, this idea is a good guide for the development
of other equality functions. 

Let's look at an equality function for simple Web pages to confirm this
conjecture:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt web=\char'077}\ {\tt :}\ {\tt web-page}\ {\tt web-page}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt a-wp}\/} and {{\tt another-wp}\/} have the same tree shape}
\\
\hbox{;; and contain the same symbols in the same order}
\\
{\tt (define}\  {\tt (web=\char'077}\  {\tt a-wp}\  {\tt another-wp)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Recall the data definition for simple Web pages: 

   \begin{quote} A {\sl {Web page}} ({{\tt WP}\/}) is either      \begin{enumerate}      \item {{\tt empty}\/};      \item {{\tt (cons}\ {\tt s}\ {\tt wp)}\/} \\
          where {{\tt s}\/} is a symbol and {{\tt wp}\/} is a Web page; or      \item {{\tt (cons}\ {\tt ewp}\ {\tt wp)}\/} \\
           where both {{\tt ewp}\/} and {{\tt wp}\/} are Web pages.       \end{enumerate} \end{quote}

\noindent The data definition has three clauses, which means that if we were to
develop {{\tt web=\char'077}\/} with the modified design recipe, we would need to study
nine cases. By using the insight gained from the development of {{\tt list=\char'077}\/}
instead, we can start from the plain web-processing template: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (web=\char'077}\  {\tt a-wp}\  {\tt another-wp)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-wp)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}\={\tt (symbol?}\  {\tt (first}\  {\tt a-wp))}
\\
\>\>\>{\tt ...}\  {\tt (first}\  {\tt a-wp)}\  {\tt ...}\  {\tt (first}\  {\tt another-wp)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (web=\char'077}\  {\tt (rest}\  {\tt a-wp)}\  {\tt (rest}\  {\tt another-wp))}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}\={\tt else}\  
\\
\>\>\>{\tt ...}\  {\tt (web=\char'077}\  {\tt (first}\  {\tt a-wp)}\  {\tt (first}\  {\tt another-wp))}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (web=\char'077}\  {\tt (rest}\  {\tt a-wp)}\  {\tt (rest}\  {\tt another-wp))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}     
In the second {{\tt cond}\/}-clause, we follow the example of
{\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\/} and {{\tt list=\char'077}\/} again. That is, we say that
{{\tt another-wp}\/}  must have the same shape as {{\tt a-wp}\/} if it is to
be equal and process the two pages in an analogous manner. The reasoning
for the third clause is similar. 

As we refine this template into a full definition now, we must again add
conditions on {{\tt another-wp}\/} to ensure that the selector expressions
are justified: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (web=\char'077}\  {\tt a-wp}\  {\tt another-wp)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-wp)}\  {\tt (empty?}\  {\tt another-wp)\char'135}
\\
\>\>{\tt \char'133}\={\tt (symbol?}\  {\tt (first}\  {\tt a-wp))}
\\
\>\>\>{\tt (and}\  \={\tt (and}\  {\tt (cons?}\  {\tt another-wp)}\  {\tt (symbol?}\  {\tt (first}\  {\tt another-wp)))}
\\
\>\>\>\>{\tt (and}\  \={\tt (symbol=?}\  {\tt (first}\  {\tt a-wp)}\  {\tt (first}\  {\tt another-wp))}
\\
\>\>\>\>\>{\tt (web=\char'077}\  {\tt (rest}\  {\tt a-wp)}\  {\tt (rest}\  {\tt another-wp))))\char'135}
\\
\>\>{\tt \char'133}\={\tt else}\  
\\
\>\>\>{\tt (and}\  \={\tt (and}\  {\tt (cons?}\  {\tt another-wp)}\  {\tt (list?}\  {\tt (first}\  {\tt another-wp)))}
\\
\>\>\>\>{\tt (and}\  \={\tt (web=\char'077}\  {\tt (first}\  {\tt a-wp)}\  {\tt (first}\  {\tt another-wp))}
\\
\>\>\>\>\>{\tt (web=\char'077}\  {\tt (rest}\  {\tt a-wp)}\  {\tt (rest}\  {\tt another-wp))))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}     
In particular, we must ensure in the second and third clause that
{{\tt another-wp}\/} is a {{\tt cons}\/}tructed list and that the first item
is a symbol or a list, respectively. Otherwise the function is analogous to
{{\tt list=\char'077}\/} and works in the same way.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 17.8.6}

 \label{ex:web=1}
Draw the table based on the data definition for simple Web pages. Develop
(at least) one example for each of the nine cases. Test {{\tt web=\char'077}\/} with
these examples.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/web=1.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.8.7}

 \label{ex:web=3}
Develop the function {{\tt posn=\char'077}\/}, which consumes two binary
{{\tt posn}\/} structures and determines whether they are
equal.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/web=3.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.8.8}

 \label{ex:web=5}
Develop the function {{\tt tree=\char'077}\/}, which consumes two binary trees and
determines whether they are equal.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/web=5.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.8.9}

 \label{ex:web=2}
Consider the following two, mutually recursive data definitions: 

\begin{quote} A {\sl {Slist}} is either      \begin{enumerate}         \item {{\tt empty}\/}         \item {{\tt (cons}\ {\tt s}\ {\tt sl)}\/} where {{\tt s}\/} is a {{\tt Sexpr}\/} and               {{\tt sl}\/} is a {{\tt Slist}\/}.     \end{enumerate}   \bigskip    A {\sl {Sexpr}} is either      \begin{enumerate}       \item a number        \item a boolean       \item a symbol        \item a {{\tt Slist}\/}     \end{enumerate}  \smallskip    \end{quote}     

\noindent Develop the function {{\tt Slist=\char'077}\/}, which consumes two
{{\tt Slists}\/} and determines whether they are equal. Like lists of
numbers, two Slists are equal is they contain the same item at analogous
positions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/web=2.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Now that we have explored the idea of equality of values, we can return to
the original motivation of the section: testing functions. Suppose we wish
to test {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\/} from section~\ref{sec:two-inputs:case2}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  \={\tt (cons}\  {\tt 5.65}\  {\tt (cons}\  {\tt 8.75}\  {\tt empty))}\  \\
\>{\tt (cons}\  {\tt 40}\  {\tt (cons}\  {\tt 30}\  {\tt empty)))}
\\
{\tt =}\  {\tt (cons}\  {\tt 226.0}\  {\tt (cons}\  {\tt 262.5}\  {\tt empty))}
\end{tabbing}\end{minipage}\end{center}
If we just type in the application into {\tt Interactions} window or add
it to the bottom of the {\tt Definitions} window, we must compare the
result and the predicted value by inspection. For short lists, like the ones
above, this is feasible; for long lists, deep Web pages, or other large
compound data, manual inspection is error-prone. 

Using equality functions like {{\tt list=\char'077}\/}, we can greatly reduce the need
for manual inspection of test results. In our running example, we can add the 
expression 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ist=\char'077}\  \\
\>{\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  \={\tt (cons}\  {\tt 5.65}\  {\tt (cons}\  {\tt 8.75}\  {\tt empty))}\  
\\
\>\>{\tt (cons}\  {\tt 40}\  {\tt (cons}\  {\tt 30}\  {\tt empty)))}
\\
\>{\tt (cons}\  {\tt 226.0}\  {\tt (cons}\  {\tt 262.5}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
to the bottom of the {\tt Definitions} window. When we click the
{\tt Execute} button now, we just need to make sure that all test cases
produce {{\tt true}\/} as their results are displayed in the
{\tt Interactions} window.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\mbox{\it test-hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\ {\tt :}\ {\tt list-of-numbers}\ {\tt list-of-numbers}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt test-result}\/}}\\
\hbox{;; to test {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (}\mbox{\it test-hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt a-list}\  {\tt another-list}\  {\tt expected-result)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (list=\char'077}\  {\tt (}\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\  {\tt a-list}\  {\tt another-list)}\  {\tt expected-result)}
\\
\>\>\>{\tt true\char'135}
\\
\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>{\tt (list}\  {\tt ``bad}\  {\tt test}\  {\tt result:''}\  {\tt a-list}\  {\tt another-list}\  {\tt expected-result)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: A test function} \label{fig:test-function}
\hrule

Indeed, we can go even further. We can write a test function like the one
in figure~\ref{fig:test-function}.  The class of {{\tt test-result}\/}s
consists of the value {{\tt true}\/} and lists of four items: the string
{{\tt ``bad}\ {\tt test}\ {\tt result:''}\/} followed by three lists. Using this new
auxiliary function, we can test {\mbox{\it hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\/} as follows:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (}\mbox{\it test-hours\/}{{}{\tt ->}{}}\mbox{\it wages\/}\=\  \\
\>{\tt (cons}\  {\tt 5.65}\  {\tt (cons}\  {\tt 8.75}\  {\tt empty))}\  
\\
\>{\tt (cons}\  {\tt 40}\  {\tt (cons}\  {\tt 30}\  {\tt empty))}
\\
\>{\tt (cons}\  {\tt 226.0}\  {\tt (cons}\  {\tt 262.5}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
If something goes wrong with the test cases, the four-item list will stand
out and specify precisely which test case failed. 

The designers of Scheme anticipated the need of a general equality
procedure and provide 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt equal?}\ {\tt :}\ {\tt any-value}\ {\tt any-value}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether two values are structurally equivalent }
\\
\hbox{;; and contain the same atomic values in analogous positions }
\end{tabbing}\end{minipage}\end{center}
When {{\tt equal?}\/} is applied to two lists, it compares them in the same
manner as {{\tt list=\char'077}\/}; when it encounters a pair of structures, it
compares their corresponding fields, if they are the same kind of
structures; and when it consumes a pair of atomic values, it compares them
with {{\tt =}\/}, {{\tt symbol=?}\/}, or {{\tt boolean=?}\/}, whatever is
appropriate. 

\medskip\noindent{\bf Unordered Lists}:\  On some occasions, we use lists even though the
ordering of the items doesn't play a role. For those cases, it is important
to have functions such as {{\tt contains-same-numbers}\/} (see
exercise~\ref{ex:list=5}) if we wish to determine whether the result of
some function application contains the proper items.~{\rule{3pt}{5pt}}
             
  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 17.8.10}

 \label{ex:test2}
Define a test function for {{\tt replace-eol-with}\/} from
section~\ref{sec:two-inputs:case1} using {{\tt equal?}\/} and formulate the
examples as test cases using this function.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/test2.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.8.11}

 \label{ex:test3}
Define the function {{\tt test-list-pick}\/}, which manages test cases for
the {{\tt list-pick}\/} function from
section~\ref{sec:two-inputs:case1}. Formulate the examples from the section
as test cases using {{\tt test-list-pick}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/test3.html}{\rule{3pt}{5pt}}


{\bf Exercise 17.8.12}

 \label{ex:test4}
Define {{\tt test-interpret}\/}, which tests {{\tt interpret-with-defs}\/}
from exercise~\ref{ex:scheme-eval-def}, using {{\tt equal?}\/}. Re-formulate
the test cases using this function.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/test4.html}{\rule{3pt}{5pt}}



%% BEGIN INLINED FILE: curriculumB.dl.tex



 \part{{Intermezzo 3: Local Definitions and Lexical Scope}}  \section{{Intermezzo 3: Local Definitions and Lexical Scope}}   
\label{sec:int-local}
\htmlref{\htmladdimg{../icons/plt.gif}}{dr:sec:int-local}

Programs do not just consist of single definitions. In many cases, a
program requires the definition of auxiliary functions or of functions with
mutual references. Indeed, as we become more experienced, we write programs
that consist of numerous auxiliary functions. If we are not careful, these
large collections of functions overwhelm us. As the size of our functions
grows, we need to organize them so that we (and other readers) can quickly
identify the relationship among parts.

This section introduces {{\tt local}\/}, a simple construct for organizing
collections of functions. With {{\tt local}\/}, a programmer can group
function definitions that belong together so that readers immediately
recognize the connection among the functions. Finally, the introduction of
{{\tt local}\/} also forces us to discuss the concept of variable
binding. While the variable and function definitions of {\tt Beginning Student} Scheme already introduce bindings into a program, a good
{{\tt local}\/} definitions is only possible with a thorough familiarity of
this concept.

 \subsection{{Organizing Programs: {\bf local}}}
\label{sec:local} 

A {{\bf local}-exp\-res\-sion} groups together an arbitrarily long sequence of definitions
similar to those found in the {\tt Definitions} window.  Following our
established rules, we first introduce the syntax and then the semantics and
pragmatics of {{\bf local}-exp\-res\-sion}s.


\hrule
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{def}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ ({\bf define} (\mbox{{\tt <{var}>}} \mbox{{\tt <{var}>}} \ldots \mbox{{\tt <{var}>}}) \mbox{{\tt <{exp}>}}) \\
	  &&  $\,\,\vert\,\,$ ({\bf define} \mbox{{\tt <{var}>}} \mbox{{\tt <{exp}>}}) \\
          &&  $\,\,\vert\,\,$ ({\bf define-struct} \mbox{{\tt <{var}>}} (\mbox{{\tt <{var}>}} \ldots \mbox{{\tt <{var}>}}))
\end{tabular}
\end{center}
\center{Figure: Scheme definitions} \label{fig:defs-in-scheme}
\hrule

\subsubsection*{Syntax of {\bf local}} 

A {{\bf local}-exp\-res\-sion} is another form of expression: 
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{exp}>}} & = & ({\bf local} (\mbox{{\tt <{def-1}>}} \ldots \mbox{{\tt <{def-n}>}}) \mbox{{\tt <{exp}>}})
\end{tabular}
\end{center}
As usual, \mbox{{\tt <{def-1}>}} \ldots\ \mbox{{\tt <{def-n}>}} is an arbitrarily long sequence
of definitions (see figure~\ref{fig:defs-in-scheme}) and \mbox{{\tt <{exp}>}} is an
arbitrary expression. In other words, a {{\bf local}-exp\-res\-sion} consists
of the keyword {{\tt local}\/}, followed by a sequence of definitions
grouped with {{\tt (}\/} and {{\tt )}\/}, followed by an expression. 

The keyword {\bf local} distinguishes this new class of expressions from
other expressions, just like {{\tt cond}\/} distinguishes conditional
expressions from applications.  The parenthesized sequence that follows
{{\tt local}\/} is referred to as the {\sc {local definition}}. The
left-hand sides of the definitions are called the {\sl locally defined\/}
variables, functions, or structures. All those in the {\tt Definitions}
window are called {\sc {top-level definitions}}. Each name may occur at
most once on the left-hand side, be it in a variable definition or a
function definition.  The expression in each definition is called the {\sl right-hand} side. The expression that follows the definitions is the {\sl body} of the {{\bf local}-exp\-res\-sion}.

Let us take a look at an example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt (f}\  {\tt x)}\  {\tt (+}\  {\tt x}\  {\tt 5))}\\
\>\>{\tt (d}\={\tt efine}\  {\tt (g}\  {\tt alon)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt (f}\  {\tt (first}\  {\tt alon))}\  {\tt (g}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt )))}
\\
\>{\tt (g}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3)))}
\end{tabbing}\end{minipage}\end{center}
The locally defined functions are {{\tt f}\/} and {{\tt g}\/}. The right-hand
side of the first function definition is {{\tt (+}\ {\tt x}\ {\tt 5)}\/}; the second one is
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt (f}\  {\tt (first}\  {\tt alon))}\  {\tt (g}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
Finally, the body of the {{\bf local}-exp\-res\-sion} is {{\tt (g}\ {\tt (list}\ {\tt 1}\ {\tt 2}\ {\tt 3))}\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 18.1.1}

 \label{ex:syn-local1}
Circle the locally defined variables and functions in red, the right-hand
sides in green, and the body of the following {\bf local}-exp\-res\-sion\ in blue:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$1.$}\  {\tt (local}\=\  {\tt ((define}\  {\tt x}\  {\tt (*}\  {\tt y}\  {\tt 3)))}\\
\>{\tt (*}\  {\tt x}\  {\tt x))}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$2.$}\  {\tt (local}\=\  {\tt ((def}\={\tt in}\={\tt e}\  {\tt (odd}\  {\tt an)}\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt an)}\  {\sf false}{\tt \char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (even}\  {\tt (sub1}\  {\tt an))\char'135}{\tt ))}
\\
\>\>{\tt (d}\={\tt efine}\  {\tt (even}\  {\tt an)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt an)}\  {\sf true}{\tt \char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (odd}\  {\tt (sub1}\  {\tt an))\char'135}{\tt )))}
\\
\>{\tt (even}\  {\tt a-nat-num))}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$3.$}\  {\tt (local}\=\  {\tt ((def}\={\tt ine}\  {\tt (f}\  {\tt x)}\  {\tt (g}\  {\tt x}\  {\tt (+}\  {\tt x}\  {\tt 1)))}\\
\>\>{\tt (define}\  {\tt (g}\  {\tt x}\  {\tt y)}\  {\tt (f}\  {\tt (+}\  {\tt x}\  {\tt y))))}
\\
\>{\tt (+}\  {\tt (f}\  {\tt 10)}\  {\tt (g}\  {\tt 10}\  {\tt 20)))}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/syn-local1.html}{\rule{3pt}{5pt}} 


{\bf Exercise 18.1.2}

 \label{ex:syn-local2}
The following phrases are {\em not\/} syntactically legal: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$1.$}\  {\tt (local}\=\  {\tt ((def}\={\tt ine}\  {\tt x}\  {\tt 10)}\\
\>\>{\tt (y}\  {\tt (+}\  {\tt x}\  {\tt x)))}
\\
\>{\tt y)}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$2.$}\  {\tt (local}\=\  {\tt ((def}\={\tt ine}\  {\tt (f}\  {\tt x)}\  {\tt (+}\  {\tt (*}\  {\tt x}\  {\tt x)}\  {\tt (*}\  {\tt 3}\  {\tt x)}\  {\tt 15))}\\
\>\>{\tt (define}\  {\tt x}\  {\tt 100)}
\\
\>\>{\tt (define}\  {\tt f\char'100}{\tt 100}\  {\tt (f}\  {\tt x)))}
\\
\>{\tt f\char'100}{\tt 100}\  {\tt x)}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$3.$}\  {\tt (local}\=\  {\tt ((def}\={\tt ine}\  {\tt (f}\  {\tt x)}\  {\tt (+}\  {\tt (*}\  {\tt x}\  {\tt x)}\  {\tt (*}\  {\tt 3}\  {\tt x)}\  {\tt 14))}\\
\>\>{\tt (define}\  {\tt x}\  {\tt 100)}
\\
\>\>{\tt (define}\  {\tt f}\  {\tt (f}\  {\tt x)))}
\\
\>{\tt f)}
\end{tabbing}\end{minipage}\end{center}
Explain why! \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/syn-local2.html}{\rule{3pt}{5pt}}


{\bf Exercise 18.1.3}

 \label{ex:syn-local3}
Determine which of the following definitions or expressions is legal and
which one is not: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$1.$}\  {\tt (defin}\={\tt e}\  {\tt A-CONSTANT}\\
\>{\tt (n}\={\tt ot}\  
\\
\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (d}\={\tt efine}\  {\tt (odd}\  {\tt an)}
\\
\>\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt (=}\  {\tt an}\  {\tt 0)}\  {\sf false}{\tt \char'135}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (even}\  {\tt (-}\  {\tt an}\  {\tt 1))\char'135}{\tt ))}
\\
\>\>\>\>{\tt (d}\={\tt efine}\  {\tt (even}\  {\tt an)}
\\
\>\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt (=}\  {\tt an}\  {\tt 0)}\  {\sf true}{\tt \char'135}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (odd}\  {\tt (-}\  {\tt an}\  {\tt 1))\char'135}{\tt )))}
\\
\>\>\>{\tt (even}\  {\tt a-nat-num))))}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$2.$}\  {\tt (+}\  {\tt (loc}\={\tt al}\=\  {\tt ((def}\={\tt ine}\  {\tt (f}\  {\tt x)}\  {\tt (+}\  {\tt (*}\  {\tt x}\  {\tt x)}\  {\tt (*}\  {\tt 3}\  {\tt x)}\  {\tt 15))}\\
\>\>\>{\tt (define}\  {\tt x}\  {\tt 100)}
\\
\>\>\>{\tt (define}\  {\tt f\char'100}{\tt 100}\  {\tt (f}\  {\tt x)))}
\\
\>\>{\tt f\char'100}{\tt 100)}
\\
\>{\tt 1000)}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$3.$}\  {\tt (local}\=\  {\tt ((def}\={\tt ine}\  {\tt CONST}\  {\tt 100)}\\
\>\>{\tt (define}\  {\tt f}\  {\tt x}\  {\tt (+}\  {\tt x}\  {\tt CONST)))}
\\
\>{\tt (define}\  {\tt (g}\  {\tt x}\  {\tt y}\  {\tt z)}\  {\tt (f}\  {\tt (+}\  {\tt x}\  {\tt (*}\  {\tt y}\  {\tt z)))))}
\end{tabbing}\end{minipage}\end{center}
Explain why each expression is legal or illegal. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/syn-local3.html}{\rule{3pt}{5pt}}


\subsubsection*{Semantics of {\bf local}}

The purpose of a {{\bf local}-exp\-res\-sion} is to define a variable, a
function, or a structure for the evaluation of the {body}
expression. Outside of the {{\bf local}-exp\-res\-sion} the definitions have
no effect. Consider the following expression:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (local}\  {\tt ((define}\  {\tt (f}\  {\tt x)}\  {\tt exp-1)))}\  {\tt exp)}\end{tabbing}\end{minipage}\end{center}
It defines the function {{\tt f}\/} during the evaluation of {{\tt exp}\/}.
The result of {{\tt exp}\/} is the result of the entire
{{\bf local}-exp\-res\-sion}.  Similarly,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (local}\  {\tt ((define}\  {\tt PI}\  {\tt 3)))}\  {\tt exp)}\end{tabbing}\end{minipage}\end{center}
temporarily lets the variable {{\tt PI}\/} stand for {{\tt 3}\/} during the
evaluation of {{\tt exp}\/}. 

We can describe the evaluation of {{\bf local}-exp\-res\-sion}s with a single rule, but the
rule is extremely complex. More specifically, the rule requires two steps
in a hand-evaluation.  First, we must systematically replace all locally
defined variables, functions, and structures so that the names do not
overlap with those used in the {\tt Definitions} window.  Second, we
move the entire sequence of definitions to the top-level and proceed as if
we had just created a new function.

Here is the evaluation rule, stated symbolically: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \  \={\tt def-1}\  {\tt ...}\  {\tt def-n}\  \\
\>{\tt E\char'133}{\tt (local}\  {\tt ((define}\  {\tt (f-1}\  {\tt x)}\  {\tt exp-1)}\  {\tt ...}\  {\tt (define}\  {\tt (f-2}\  {\tt x)}\  {\tt exp-n))}\  {\tt exp)\char'135}\  
\\
{\tt =}\  
\\
\>{\tt def-1}\  {\tt ...}\  {\tt def-n}\  {\tt (define}\  {\tt (f-1}\hbox{$'$}\  {\tt x)}\  {\tt exp-1}\hbox{$'$}{\tt )}\  {\tt ...}\  {\tt (define}\  {\tt (f-n}\hbox{$'$}\  {\tt x)}\  {\tt exp-n}\hbox{$'$}{\tt )}\  
\\
\>{\tt E\char'133}{\tt exp}\hbox{$'$}{\tt \char'135}
\end{tabbing}\end{minipage}\end{center}
For simplicity, the {{\bf local}-exp\-res\-sion} in this rule defines only
one-argument functions, but it is straightforward to generalize from here.
As usual, the sequence {{\tt def-1}\ {\tt ...}\ {\tt def-n}\/} represents top-level
definitions.

The unusual part of the rule is the notation {{\tt E\char'133}{\tt exp\char'135}\/}. It represents
an expression {{\tt exp}\/} and its context {{\tt E}\/}. More specifically,
{{\tt exp}\/} is the next expression that must and can be
evaluated;\footnote{We could develop a rigorous definition of this notion, but it is not useful for our purposes.} {{\tt E}\/} is called its
{\sc {evaluation context}}.

For example, the expression
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \  {\tt (+}\  {\tt (local}\  {\tt ((define}\  {\tt (f}\  {\tt x)}\  {\tt 10))}\  {\tt (f}\  {\tt 13))}\  {\tt 5)}\end{tabbing}\end{minipage}\end{center}
is an addition. Before we can compute its result, we must evaluate the two
subexpressions to numbers. Since the first subexpression is not a number,
we focus on it: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \  {\tt (local}\  {\tt ((define}\  {\tt (f}\  {\tt x)}\  {\tt 10))}\  {\tt (f}\  {\tt 13))}\end{tabbing}\end{minipage}\end{center}
This {{\bf local}-exp\-res\-sion} must and can be evaluated, so 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \  \={\tt exp}\  {\tt =}\  {\tt (local}\  {\tt ((define}\  {\tt (f}\  {\tt x)}\  {\tt 10))}\  {\tt (f}\  {\tt 13))}\\
\>{\tt E}\  {\tt =}\  {\tt (+}\  {\tt ...}\  {\tt 5)}
\end{tabbing}\end{minipage}\end{center}
for the above expression. 

On the right-hand side of the rule for {{\tt local}\/}, we can see several
primed names and expressions. The primed names {{\tt f-1}\hbox{$'$}\/}, \ldots,
{{\tt f-n}\hbox{$'$}\/} are new function names, distinct from all other names in
top-level definitions; the primes on the expressions {{\tt exp-1}\hbox{$'$}\/},
\ldots, {{\tt exp-n}\hbox{$'$}\/} indicate that these expressions are structurally
identical to {{\tt exp-1}\/}, \ldots, {{\tt exp-n}\/} but contain
{{\tt f-1}\hbox{$'$}\/} instead of {{\tt f-11}\/}, and so on.

The evaluation rule for {{\bf local}-exp\-res\-sion}s is the most complex rule
that we have encountered so far, and indeed, it is the most complex rule
that we will ever encounter. Each of the two steps is important and serves
a distinct purpose. Their purpose is best illustrated by a series of simple
examples.


The first part of the rule eliminates name clashes between {names}
that are already defined in the top-level environment and those that will
be inserted there. Consider the following example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt y}\  {\tt 10)}\\
{\tt (+}\  \={\tt y}\  
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt y}\  {\tt 10)}
\\
\>\>\>{\tt (define}\  {\tt z}\  {\tt (+}\  {\tt y}\  {\tt y)))}
\\
\>\>{\tt z))}
\end{tabbing}\end{minipage}\end{center}
The expression introduces a local definition for {{\tt y}\/}, adds
{{\tt y}\/} to itself to get {{\tt z}\/} and returns the value of
{{\tt z}\/}. 

The informal description of {{\tt local}\/} says that the result should be
{{\tt 30}\/}. Let's verify this with our rule. If we simply added the
definitions in {{\tt local}\/} to the top-level, the two definitions for
{{\tt y}\/} would clash. The renaming step prevents this clash and clarifies
which of {{\tt y}\/}'s belong together:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  \={\tt (define}\  {\tt y}\  {\tt 10)}\\
\>{\tt (+}\  {\tt y}\  {\tt (local}\  {\tt ((define}\  {\tt y1}\  {\tt 10)}\  {\tt (define}\  {\tt z1}\  {\tt (+}\  {\tt y1}\  {\tt y1)))}\  {\tt z1))}
\\
{\tt =}\  \={\tt (define}\  {\tt y}\  {\tt 10)}
\\
\>{\tt (define}\  {\tt y1}\  {\tt 10)}
\\
\>{\tt (define}\  {\tt z1}\  {\tt (+}\  {\tt y1}\  {\tt y1))}
\\
\>{\tt (+}\  {\tt y}\  {\tt z1)}
\\
{\tt =}\  \={\tt (define}\  {\tt y}\  {\tt 10)}
\\
\>{\tt (define}\  {\tt y1}\  {\tt 10)}
\\
\>{\tt (define}\  {\tt z1}\  {\tt 20)}
\\
\>{\tt (+}\  {\tt 10}\  {\tt z1)}
\\
{\tt =}\  \={\tt (define}\  {\tt y}\  {\tt 10)}
\\
\>{\tt (define}\  {\tt y1}\  {\tt 10)}
\\
\>{\tt (define}\  {\tt z1}\  {\tt 20)}
\\
\>{\tt (+}\  {\tt 10}\  {\tt z1)}
\\
{\tt =}\  \={\tt (define}\  {\tt y}\  {\tt 10)}
\\
\>{\tt (define}\  {\tt y1}\  {\tt 10)}
\\
\>{\tt (define}\  {\tt z1}\  {\tt 20)}
\\
\>{\tt (+}\  {\tt 10}\  {\tt 20)}
\end{tabbing}\end{minipage}\end{center}
As expected, the result is {{\tt 30}\/}. 


Since {{\bf local}-exp\-res\-sion}s may occur inside of function bodies,
renaming is important if such functions are applied more than once. The
following second example illustrates this point: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (d}\={\tt efine}\  {\tt (D}\  {\tt x}\  {\tt y)}\\
\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt x2}\  {\tt (*}\  {\tt x}\  {\tt x))}
\\
\>\>\>\>{\tt (define}\  {\tt y2}\  {\tt (*}\  {\tt y}\  {\tt y)))}
\\
\>\>\>{\tt (sqrt}\  {\tt (+}\  {\tt x2}\  {\tt y2))))}
\\
\>{\tt (+}\  {\tt (D}\  {\tt 0}\  {\tt 1)}\  {\tt (D}\  {\tt 3}\  {\tt 4))}
\end{tabbing}\end{minipage}\end{center}
The function {{\tt D}\/} computes the square root of the sum of the squares
of its arguments. Hence the result of {{\tt (+}\ {\tt (D}\ {\tt 0}\ {\tt 1)}\ {\tt (D}\ {\tt 3}\ {\tt 4))}\/} should
be {{\tt 6}\/}. 

As {{\tt D}\/} computes its answer, it introduces two local variables:
{{\tt x2}\/} and {{\tt y2}\/}. Since {{\tt D}\/} is applied twice, a modified
version of its body is evaluated twice and therefore its local definitions
must be added to the top-level twice. The renaming step ensures that no
matter how often we lift such definitions, they never interfere with each
other. Here is how this works:  
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  \={\tt (d}\={\tt efine}\  {\tt (D}\  {\tt x}\  {\tt y)}\\
\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt x2}\  {\tt (*}\  {\tt x}\  {\tt x))}
\\
\>\>\>\>{\tt (define}\  {\tt y2}\  {\tt (*}\  {\tt y}\  {\tt y)))}
\\
\>\>\>{\tt (sqrt}\  {\tt (+}\  {\tt x2}\  {\tt y2))))}
\\
\>{\tt (+}\  \={\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt x2}\  {\tt (*}\  {\tt 0}\  {\tt 0))}
\\
\>\>\>\>{\tt (define}\  {\tt y2}\  {\tt (*}\  {\tt 1}\  {\tt 1)))}
\\
\>\>\>{\tt (sqrt}\  {\tt (+}\  {\tt x2}\  {\tt y2)))}
\\
\>\>{\tt (D}\  {\tt 3}\  {\tt 4))}
\end{tabbing}\end{minipage}\end{center}
The expression {{\tt (D}\ {\tt 0}\ {\tt 1)}\/} is evaluated according to the regular rules. 
Now we rename and lift the {{\tt local}\/} definitions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  \={\tt (d}\={\tt efine}\  {\tt (D}\  {\tt x}\  {\tt y)}\\
\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt x2}\  {\tt (*}\  {\tt x}\  {\tt x))}
\\
\>\>\>\>{\tt (define}\  {\tt y2}\  {\tt (*}\  {\tt y}\  {\tt y)))}
\\
\>\>\>{\tt (sqrt}\  {\tt (+}\  {\tt x2}\  {\tt y2))))}
\\
\>{\tt (define}\  {\tt x21}\  {\tt (*}\  {\tt 0}\  {\tt 0))}
\\
\>{\tt (define}\  {\tt y21}\  {\tt (*}\  {\tt 1}\  {\tt 1))}
\\
\>{\tt (+}\  \={\tt (sqrt}\  {\tt (+}\  {\tt x21}\  {\tt y21))}
\\
\>\>{\tt (D}\  {\tt 3}\  {\tt 4))}
\end{tabbing}\end{minipage}\end{center}
From here, the evaluation proceeds according to the standard rules 
until we encounter a second nested
{{\bf local}-exp\-res\-sion} in the expression that we are evaluating: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  \={\tt (d}\={\tt efine}\  {\tt (D}\  {\tt x}\  {\tt y)}\\
\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt x2}\  {\tt (*}\  {\tt x}\  {\tt x))}
\\
\>\>\>\>{\tt (define}\  {\tt y2}\  {\tt (*}\  {\tt y}\  {\tt y)))}
\\
\>\>\>{\tt (sqrt}\  {\tt (+}\  {\tt x2}\  {\tt y2))))}
\\
\>{\tt (define}\  {\tt x21}\  {\tt 0)}
\\
\>{\tt (define}\  {\tt y21}\  {\tt 1)}
\\
\>{\tt (+}\  {\tt 1}\  {\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt x2}\  {\tt (*}\  {\tt 3}\  {\tt 3))}
\\
\>\>\>{\tt (define}\  {\tt y2}\  {\tt (*}\  {\tt 4}\  {\tt 4)))}
\\
\>\>{\tt (sqrt}\  {\tt (+}\  {\tt x2}\  {\tt y2))))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  \={\tt (d}\={\tt efine}\  {\tt (D}\  {\tt x}\  {\tt y)}\\
\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt x2}\  {\tt (*}\  {\tt x}\  {\tt x))}
\\
\>\>\>\>{\tt (define}\  {\tt y2}\  {\tt (*}\  {\tt y}\  {\tt y)))}
\\
\>\>\>{\tt (sqrt}\  {\tt (+}\  {\tt x2}\  {\tt y2))))}
\\
\>{\tt (define}\  {\tt x21}\  {\tt 0)}
\\
\>{\tt (define}\  {\tt y21}\  {\tt 1)}
\\
\>{\tt (define}\  {\tt x22}\  {\tt 9)}
\\
\>{\tt (define}\  {\tt y22}\  {\tt 16)}
\\
\>{\tt (+}\  {\tt 1}\  {\tt (sqrt}\  {\tt (+}\  {\tt x22}\  {\tt y22)))}
\end{tabbing}\end{minipage}\end{center}
By renaming {{\tt x2}\/} and {{\tt y2}\/} again, we avoided clashes. From
here, the evaluation of the expression is straightforward: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (+}\  {\tt 1}\  {\tt (sqrt}\  {\tt (+}\  {\tt x22}\  {\tt y22)))}\\
{\tt =}\  {\tt (+}\  {\tt 1}\  {\tt (sqrt}\  {\tt (+}\  {\tt 9}\  {\tt y22)))}
\\
{\tt =}\  {\tt (+}\  {\tt 1}\  {\tt (sqrt}\  {\tt (+}\  {\tt 9}\  {\tt 16)))}
\\
{\tt =}\  {\tt (+}\  {\tt 1}\  {\tt (sqrt}\  {\tt 25))}
\\
{\tt =}\  {\tt (+}\  {\tt 1}\  {\tt 5)}
\\
{\tt =}\  {\tt 6}
\end{tabbing}\end{minipage}\end{center}
The result is 6, as expected.\footnote{As we evaluate expressions in this manner, our list of definitions grows longer and longer. Fortunately, DrScheme knows how to manage such growing lists. Indeed, it occasionally throws out definitions that will never be used again.}


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 18.1.4}

 \label{ex:sem-local0}
Since {{\tt local}\/} definitions are added to the {\tt Definitions}
window during an evaluation, we might wish to try to see their values by
just typing in the variables into the {\tt Interactions} window. Is this
possible? Why or why not?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sem-local0.html}{\rule{3pt}{5pt}} 


{\bf Exercise 18.1.5}

 \label{ex:sem-local1}
Evaluate the following expressions by hand: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$1.$}\  {\tt (local}\=\  {\tt ((define}\  {\tt (x}\  {\tt y)}\  {\tt (*}\  {\tt 3}\  {\tt y)))}\\
\>{\tt (*}\  {\tt (x}\  {\tt 2)}\  {\tt 5))}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$2.$}\  {\tt (local}\=\  {\tt ((define}\  {\tt (f}\  {\tt c)}\  {\tt (+}\  {\tt (*}\  {\tt 9/5}\  {\tt c)}\  {\tt 32)))}\\
\>{\tt (-}\  {\tt (f}\  {\tt 0)}\  {\tt (f}\  {\tt 10)))}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$3.$}\  {\tt (local}\=\  {\tt ((def}\={\tt in}\={\tt e}\  {\tt (odd\char'077}\  {\tt n)}\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\sf false}{\tt \char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (even\char'077}\  {\tt (sub1}\  {\tt n))\char'135}{\tt ))}
\\
\>\>{\tt (d}\={\tt efine}\  {\tt (even\char'077}\  {\tt n)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\sf true}{\tt \char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (odd\char'077}\  {\tt (sub1}\  {\tt n))\char'135}{\tt )))}
\\
\>{\tt (even\char'077}\  {\tt 1))}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$4.$}\  {\tt (+}\  {\tt (loc}\={\tt al}\=\  {\tt ((def}\={\tt ine}\  {\tt (f}\  {\tt x)}\  {\tt (g}\  {\tt (+}\  {\tt x}\  {\tt 1)))}\\
\>\>\>{\tt (define}\  {\tt (g}\  {\tt x}\  {\tt y)}\  {\tt (+}\  {\tt x}\  {\tt y)))}
\\
\>\>{\tt (f}\  {\tt 10))}
\\
\>{\tt 555)}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$5.$}\  {\tt (def}\={\tt in}\={\tt e}\  {\tt (h}\  {\tt n)}\  \\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt 0)}\  {\tt empty\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt r}\  {\tt (*}\  {\tt n}\  {\tt n)))}
\\
\>\>\>\>{\tt (cons}\  {\tt r}\  {\tt (h}\  {\tt (-}\  {\tt n}\  {\tt 1))))\char'135}{\tt ))}
\\
\>{\tt (h}\  {\tt 2)}
\end{tabbing}\end{minipage}\end{center}
The evaluations should show all
{{\tt local}\/}-reductions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sem-local1.html}{\rule{3pt}{5pt}} 


\subsubsection*{Pragmatics of {\bf local}, Part 1}

The most important use of {{\bf local}-exp\-res\-sion}s is to group
collections of functions that serve one purpose. Consider for an example the
definitions for our sort function from section~\ref{sec:sortI}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sort}\ {\tt :}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (sort}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt (cons?}\  {\tt alon)}\  {\tt (insert}\  {\tt (first}\  {\tt alon)}\  {\tt (sort}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\\
\hbox{;; {{\tt insert}\ {\tt :}\ {\tt number}\ {\tt list-of-numbers}\ {\tt (sorted)}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (insert}\  {\tt an}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt (list}\  {\tt an)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (>}\  {\tt an}\  {\tt (first}\  {\tt alon))}\  {\tt (cons}\  {\tt an}\  {\tt alon)\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt (first}\  {\tt alon)}\  {\tt (insert}\  {\tt an}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The first definition defines {{\tt sort}\/} per se, the second one defines
an auxiliary function that inserts a number into a sorted list of numbers. 
The first one uses the second one to construct the result from a natural
recursion, a sorted version of the rest of the list, and the first
item.  

The two functions together form the program that sorts a list of numbers. To
indicate this intimate relationship between the functions, we can, and should,
use a {{\bf local}-exp\-res\-sion}. Specifically, we define a program
{{\tt sort}\/} that immediately introduces the two functions as auxiliary
definitions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sort}\ {\tt :}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (sort}\  {\tt alon)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (d}\={\tt efine}\  {\tt (sort}\  {\tt alon)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt (cons?}\  {\tt alon)}\  {\tt (insert}\  {\tt (first}\  {\tt alon)}\  {\tt (sort}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (insert}\  {\tt an}\  {\tt alon)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt (list}\  {\tt an)\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt (>}\  {\tt an}\  {\tt (first}\  {\tt alon))}\  {\tt (cons}\  {\tt an}\  {\tt alon)\char'135}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt (first}\  {\tt alon)}\  {\tt (insert}\  {\tt an}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt )\char'135}{\tt )))}
\\
\>\>{\tt (sort}\  {\tt alon)))}
\end{tabbing}\end{minipage}\end{center}
Here the body of {{\bf local}-exp\-res\-sion}s simply passes on the argument
to the locally defined function {{\tt sort}\/}. 

   \begin{rawhtml}     <table bgcolor=red align=center>       <tr><td><font align=center color=white size=+3>   \end{rawhtml} Guideline on the Use of local   \begin{rawhtml}     <tr><td><font color=white><p>   \end{rawhtml} Develop a function following the design recipes. If the function requires the use of auxiliary definitions, group them in a {{\bf local}-exp\-res\-sion} and put the {{\bf local}-exp\-res\-sion} into a new function definition. The body of the {{\tt local}\/} should apply the main function to the arguments of the newly defined function.    \begin{rawhtml}     </p></table>   \end{rawhtml} 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 

{\bf Exercise 18.1.6}

 \label{ex:local-prag-eval}
Evaluate {{\tt (sort}\ {\tt (list}\ {\tt 2}\ {\tt 1}\ {\tt 3))}\/} by hand until the locally defined
{{\tt sort}\/} function is used. Do the same for {{\tt (equal?}\ {\tt (sort}\ {\tt (list}{ }{\tt 1))}\ {\tt (sort}\ {\tt (list}\ {\tt 2)))}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:local-prag-eval.html}{\rule{3pt}{5pt}}


{\bf Exercise 18.1.7}

 \label{ex:move-pic-local}
Use a {{\tt local}\/} expression to organize the functions for moving
pictures from section~\ref{sec:move-fig}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/move-pic-local.html}{\rule{3pt}{5pt}}


{\bf Exercise 18.1.8}

 \label{ex:draw-poly-local}
Use a {{\tt local}\/} expression to organize the functions for drawing a
polygon in figure~\ref{fig:draw-poly}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/draw-poly-local.html}{\rule{3pt}{5pt}}


{\bf Exercise 18.1.9}

 \label{ex:permute-local}
Use a {{\tt local}\/} expression to organize the functions for rearranging
words from section~\ref{sec:permute}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/permute-local.html}{\rule{3pt}{5pt}}


{\bf Exercise 18.1.10}

 \label{ex:blue-eye-local}
Use a {{\tt local}\/} expression to organize the functions for finding blue
eyed descendants from section~\ref{sec:mut-ref-dd}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/blue-eye-local.html}{\rule{3pt}{5pt}}


\subsubsection*{Pragmatics of {\bf local}, Part 2}

Suppose we need a function that produces the last occurrence of some item
in a list. To be precise, assume we have lists of records on rock stars. 
For simplicity, each star is represented as a pair of values: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}\end{tabbing}\end{minipage}\end{center}

\begin{quote} A {\sl {star}} (record) is a structure:\\
    \centerline{{{\tt (make-star}\ {\tt s}\ {\tt t)}\/}}  where {{\tt s}\/} and {{\tt t}\/} are symbols. \end{quote}

\noindent Here is an example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt alos}\  \\
\>{\tt (list}\  \={\tt (make-star}\  {\tt \char'047}{\tt Chris}\  {\tt \char'047}{\tt saxophone)}
\\
\>\>{\tt (make-star}\  {\tt \char'047}{\tt Robby}\  {\tt \char'047}{\tt trumpet)}
\\
\>\>{\tt (make-star}\  {\tt \char'047}{\tt Matt}\  {\tt \char'047}{\tt violin)}
\\
\>\>{\tt (make-star}\  {\tt \char'047}{\tt Wen}\  {\tt \char'047}{\tt guitar)}
\\
\>\>{\tt (make-star}\  {\tt \char'047}{\tt Matt}\  {\tt \char'047}{\tt radio)))}
\end{tabbing}\end{minipage}\end{center}
This list contains two occurrences of {{\tt \char'047}{\tt Matt}\/}. So, if we wanted to
determine the instrument that goes with the last occurrence of
{{\tt \char'047}{\tt Matt}\/}, we would want {{\tt \char'047}{\tt radio}\/}. For {{\tt \char'047}{\tt Sean}\/}, on the
other hand, our function would produce {{\tt \char'047}{\tt guitar}\/}. Of course, looking
for the instrument of {{\tt \char'047}{\tt Kate}\/} should yield {{\tt false}\/} to
indicate that there is no record for {{\tt \char'047}{\tt Kate}\/}. 

Let's write down a contract, a purpose statement, and a header: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt last-occurrence}\ {\tt :}\ {\tt symbol}\ {\tt (listof}\ {\tt star)}\ {}{\tt ->}{}\ {\tt star}\ {\tt or}\ {\tt false}\/}}\\
\hbox{;; to find the last star record in {{\tt alostars}\/} that contains {{\tt s}\/} in {{\tt name}\/} field}
\\
{\tt (define}\  {\tt (last-occurrence}\  {\tt s}\  {\tt alostars)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
The contract is unusual because it mentions two classes of data to the
right of the arrow: {{\tt star}\/} and {{\tt false}\/}. Although we haven't
seen this kind of contract before, its meaning is obvious. The function may
produce a {{\tt star}\/} or {{\tt false}\/}.

We have already developed some examples, so we can move directly to the
template stage of our design recipe: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (last-occurrence}\  {\tt s}\  {\tt alostars)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alostars)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt alostars)}\  {\tt ...}\  {\tt (last-occurrence}\  {\tt s}\  {\tt (rest}\  {\tt alostars))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The real problem with this function, of course, shows up only when we want
to fill in the gaps in this templates. The answer in the first case is
{{\tt false}\/}, per specification. How to form the answer in the second case
is far from clear. Here is what we have: 
\begin{enumerate}
\item {{\tt (first}\ {\tt alostars)}\/} is the first {{\tt star}\/} record on the
given list. If its name field is equal to {{\tt s}\/}, it may or may not
be the final result. It all depends on the records in the rest of the
list. 

\item {{\tt (last-occurrence}\ {\tt s}\ {\tt (rest}\ {\tt alostars))}\/} evaluates to one of two
things: a {{\tt star}\/} record with {{\tt s}\/} as the name field or
{{\tt false}\/}. In the first case, the {{\tt star}\/} record is the result;
in the second case, the result is either {{\tt false}\/} or the first
{{\tt record}\/}. 
\end{enumerate}
The second point implies that we need to use the result of the natural
recursion twice. First to check whether it is a {{\tt star}\/} or a
{{\tt boolean}\/}. Second, to use it as the answer if it is a
{{\tt star}\/}. 

The dual-use of the natural recursion is best expressed with a
{{\bf local}-exp\-res\-sion}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (last-occurrence}\  {\tt s}\  {\tt alostars)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alostars)}\  {\tt false\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt r}\  {\tt (last-occurrence}\  {\tt s}\  {\tt (rest}\  {\tt alostars))))}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (star?}\  {\tt r)}\  {\tt r\char'135}
\\
\>\>\>\>{\tt ...))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The nested {{\bf local}-exp\-res\-sion} gives a name to the result of the
natural recursion. The {{\bf cond}-exp\-res\-sion} uses it twice. We could 
eliminate the {{\bf local}-exp\-res\-sion} by replacing {{\tt r}\/} with the
right-hand side: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (last-occurrence}\  {\tt s}\  {\tt alostars)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alostars)}\  {\tt false\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (star?}\  {\tt (last-occurrence}\  {\tt s}\  {\tt (rest}\  {\tt alostars)))}\  
\\
\>\>\>\>{\tt (last-occurrence}\  {\tt s}\  {\tt (rest}\  {\tt alostars))\char'135}
\\
\>\>\>{\tt ...)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
But even a superficial glance shows that reading a natural recursion twice
is difficult. The version with {{\tt local}\/} is superior. 

From the partially refined template it is only a short step to the full
definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt last-occurrence}\ {\tt :}\ {\tt symbol}\ {\tt (listof}\ {\tt star)}\ {}{\tt ->}{}\ {\tt star}\ {\tt or}\ {\tt false}\/}}\\
\hbox{;; to find the last star record in alostars that contains {{\tt s}\/} in {{\tt name}\/} field}
\\
{\tt (d}\={\tt efine}\  {\tt (last-occurrence}\  {\tt s}\  {\tt alostars)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alostars)}\  {\tt false\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt r}\  {\tt (last-occurrence}\  {\tt s}\  {\tt (rest}\  {\tt alostars))))}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (star?}\  {\tt r)}\  {\tt r\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt (star-name}\  {\tt (first}\  {\tt alostars))}\  {\tt s)}\  {\tt (first}\  {\tt alostars)\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\sf false}{\tt \char'135}{\tt ))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The second clause in the nested {{\bf cond}-exp\-res\-sion} compares the first
record's {{\tt name}\/} field with {{\tt s}\/} if {{\tt r}\/} is not a
{{\tt star}\/} record. In that case, there is no record with the matching
name in the rest of the list and, if the first record is the appropriate
one, it is the result. Otherwise, the entire list does not contain the name
we're looking for and the result is {{\tt false}\/}. 


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 18.1.11}

 \label{ex:local-interm0}
Evaluate the following test by hand: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ast-occurrence}\  {\tt \char'047}{\tt Matt}\\
\>{\tt (list}\  \={\tt (make-star}\  {\tt \char'047}{\tt Matt}\  {\tt \char'047}{\tt violin)}
\\
\>\>{\tt (make-star}\  {\tt \char'047}{\tt Matt}\  {\tt \char'047}{\tt radio)))}
\end{tabbing}\end{minipage}\end{center}
How many {{\bf local}-exp\-res\-sion}s are lifted?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/local-interm0.html}{\rule{3pt}{5pt}}


{\bf Exercise 18.1.12}

 \label{ex:local-interm2}
Consider the following function definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt max}\ {\tt :}\ {\tt non-empty-lon}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to determine the largest number on {{\tt alon}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (max}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt (rest}\  {\tt alon))}\  {\tt (first}\  {\tt alon)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (>}\  {\tt (first}\  {\tt alon)}\  {\tt (max}\  {\tt (rest}\  {\tt alon)))}\  {\tt (first}\  {\tt alon)\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (max}\  {\tt (rest}\  {\tt alon))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Both clauses in the nested {{\bf cond}-exp\-res\-sion} compute {{\tt (max}{ }{\tt (rest}\ {\tt an-inv))}\/}. 

\noindent Introduce a {{\bf local}-exp\-res\-sion} for this expression. Test
both versions of {{\tt max}\/} on 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5}\  {\tt 6}\  {\tt 7}\  {\tt 8}\  {\tt 9}\  {\tt 10}\  {\tt 11}\  {\tt 12}\  {\tt 13}\  {\tt 14}\  {\tt 15}\  {\tt 16}\  {\tt 17}\  {\tt 18}\  {\tt 19}\  {\tt 20)}\end{tabbing}\end{minipage}\end{center}
Explain the effect.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/local-interm2.html}{\rule{3pt}{5pt}}  


{\bf Exercise 18.1.13}

 \label{ex:local-interm3}
Develop the function {{\tt to-blue-eyed-ancestor}\/}. The function consumes
a family tree ({{\tt ftn}\/}) (see section~\ref{sec:struct-in-struct}) and
produces a list that explains how to get to a blue-eyed ancestor. If there
is no blue-eyed ancestor, the function produces {{\tt false}\/}.

The function's contract, purpose statement, and header are as follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt to-blue-eyed-ancestor}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt path}\ {\tt or}\ {\tt false}\/} }\\
\hbox{;; to compute the path from {{\tt a-ftn}\/} tree to a blue-eyed ancestor}
\\
{\tt (define}\  {\tt (to-blue-eyed-ancestor}\  {\tt a-ftn)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
A path is a list of {{\tt \char'047}{\tt father}\/} and {{\tt \char'047}{\tt mother}\/}: 

  \begin{quote} A {\sl {path}} is either     \begin{enumerate}       \item {{\tt empty}\/}, or         \item {{\tt (cons}\ {\tt s}\ {\tt los)}\/} where {{\tt s}\/} is {{\tt \char'047}{\tt father}\/} or          {{\tt \char'047}{\tt mother}\/} and {{\tt los}\/} is a path.        \end{enumerate} \end{quote}

\noindent The empty path indicates that {{\tt a-ftn}\/} has {{\tt \char'047}{\tt blue}\/}
in the {{\tt eyes}\/} field. If the first item is {{\tt \char'047}{\tt mother}\/}, we may
search in the mother's family tree for a blue-eyed ancestor using the rest
of the path. Similarly, we search in the father's family tree if the first
item is {{\tt \char'047}{\tt father}\/} and use the rest of the path for further
directions.

Examples: 
\begin{enumerate}
\item {{\tt (to-blue-eyed-ancestor}\ {\tt Gustav)}\/} produces {{\tt (list}{ }\ {\tt \char'047}{\tt mother)}\/} for the family tree in in figure~\ref{fig:familyA}; 

\item {{\tt (to-blue-eyed-ancestor}\ {\tt Adam)}\/} produces {{\tt false}\/} in the
same setting; and 

\item if we added 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt Hanna}\  {\tt (make-child}\  {\tt Gustav}\  {\tt Eva}\  {\tt \char'047}{\tt Gustav}\  {\tt 1988}\  {\tt \char'047}{\tt hazel))}\end{tabbing}\end{minipage}\end{center}
then 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (to-blue-eyed-ancestor}\  {\tt Hanna)}\end{tabbing}\end{minipage}\end{center}
would yield {{\tt (list}\ {\tt \char'047}{\tt father}\ {\tt \char'047}{\tt mother)}\/}.
\end{enumerate}
Build test cases from these examples. Formulate them as boolean
expressions, using the strategy of
section~\ref{sec:equal-test}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/local-interm3.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


\medskip\noindent{\bf Backtracking}:\  The functions {{\tt last-occurrence}\/} and
{{\tt to-blue-eyed-ancestor}\/} produce two kinds of results: one to
indicate a successful search and another one to indicate a failure. Both
are recursive. If a natural recursion fails to find the desired result,
each tries to compute a result in a different manner. Indeed,
{{\tt to-blue-eyed-ancestor}\/} may use another natural recursion. 

This strategy of computing an answer is a simple form of
{\sc {backtracking}}. In the world of data that we have dealt with so
far, backtracking is simple and just a device to save computing steps. It
is always possible to write to separate recursive functions that accomplish
the same purpose as one of the backtracking functions here. 

We will take an even closer look at backtracking in
section~\ref{sec:backtrack}. Also, we will discuss counting computing steps
in intermezzo~5.~{\rule{3pt}{5pt}}

\subsubsection*{Pragmatics of {\bf local}, Part 3}

Consider the following function definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt mult10}\ {\tt :}\ {\tt list-of-digits}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
\hbox{;; to create a list of numbers by multiplying each digit on {{\tt alod}\/} }
\\
\hbox{;; by {{\tt (expt}\ {\tt 10}\ {\tt p)}\/} where {{\tt p}\/} is the number of digits that follow}
\\
{\tt (d}\={\tt efine}\  {\tt (mult10}\  {\tt alod)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alod)}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ons}\  {\tt (*}\  {\tt (expt}\  {\tt 10}\  {\tt (length}\  {\tt (rest}\  {\tt alod)))}\  {\tt (first}\  {\tt alod))}
\\
\>\>\>{\tt (mult10}\  {\tt (rest}\  {\tt alod)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Here are some sample application: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt (mult10}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3))}\\
{\tt (list}\  {\tt 100}\  {\tt 20}\  {\tt 3)}
\\
{\tt >}\  {\tt (mult10}\  {\tt (list}\  {\tt 9}\  {\tt 8}\  {\tt 7))}
\\
{\tt (list}\  {\tt 900}\  {\tt 80}\  {\tt 7)}
\end{tabbing}\end{minipage}\end{center}
Clearly, the function could be used to convert a list of digits into a number. 

A small problem with the definition of {{\tt mult10}\/} is the computation
of the first item of the result in the second clause. It is a large
expression and doesn't quite correspond to the purpose statement. By using
a {{\bf local}-exp\-res\-sion} in the second clause, we can introduce names
for some intermediate values in the computation of the answer:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt mult10}\ {\tt :}\ {\tt list-of-digits}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
\hbox{;; to create a list of numbers by multiplying each digit on {{\tt alod}\/} }
\\
\hbox{;; by {{\tt (expt}\ {\tt 10}\ {\tt p)}\/} where {{\tt p}\/} is the number of digits that follow}
\\
{\tt (d}\={\tt efine}\  {\tt (mult10}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt a-digit}\  {\tt (first}\  {\tt alon))}
\\
\>\>\>\>{\tt (define}\  {\tt p}\  {\tt (length}\  {\tt (rest}\  {\tt alon))))}
\\
\>\>\>\hbox{;; ------------------------------------------------------}
\\
\>\>\>{\tt (cons}\  {\tt (*}\  {\tt (expt}\  {\tt 10}\  {\tt p)}\  {\tt a-digit)}\  {\tt (mult10}\  {\tt (rest}\  {\tt alon))))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The use of names helps us understand the expression when we read the
definition again because we can study one {{\tt local}\/}-definition at a
time. 

The use of {{\tt local}\/} for such cases is most appropriate when a value
is computed twice as, for example, the expression {{\tt (rest}\ {\tt alon)}\/} in
{{\tt mult10}\/}. By introducing names for repeated expressions, we might
also avoid some (small) effort on DrScheme's side:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (mult10}\  {\tt alon)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt a-digit}\  {\tt (first}\  {\tt alon))}
\\
\>\>\>\>{\tt (define}\  {\tt the-rest}\  {\tt (rest}\  {\tt alon))}
\\
\>\>\>\>{\tt (define}\  {\tt p}\  {\tt (length}\  {\tt the-rest)))}
\\
\>\>\>\hbox{;; ------------------------------------------------------}
\\
\>\>\>{\tt (cons}\  {\tt (*}\  {\tt (expt}\  {\tt 10}\  {\tt p)}\  {\tt a-digit)}\  {\tt (mult10}\  {\tt the-rest)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

For the programs that we have developed, this third usage of {{\tt local}\/}
 is hardly ever useful. An auxiliary function is almost always better. We
 will, however, encounter many different styles of functions in the
 remaining parts of the book and with them the opportunity, and sometimes
 the necessity, to use {{\bf local}-exp\-res\-sion}s like the one for
 {{\tt mult10}\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 18.1.14}

 \label{ex:local-interm1}
Consider the following function definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt extract1}\ {\tt :}\ {\tt inventory}\ {}{\tt ->}{}\ {\tt inventory}\/}}\\
\hbox{;; to create an {{\tt inventory}\/} from {{\tt an-inv}\/} for all}
\\
\hbox{;; those items that cost less than \$1}
\\
{\tt (d}\={\tt efine}\  {\tt (extract1}\  {\tt an-inv)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt an-inv)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (<=}\  {\tt (ir-price}\  {\tt (first}\  {\tt an-inv))}\  {\tt 1.00)}
\\
\>\>\>\>{\tt (cons}\  {\tt (first}\  {\tt an-inv)}\  {\tt (extract1}\  {\tt (rest}\  {\tt an-inv)))\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (extract1}\  {\tt (rest}\  {\tt an-inv))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Both clauses in the nested {{\bf cond}-exp\-res\-sion} extract the first item
from {{\tt an-inv}\/} and both compute {{\tt (extract1}\ {\tt (rest}\ {\tt an-inv))}\/}. 

\noindent Introduce a {{\bf local}-exp\-res\-sion} for these
expressions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/local-interm1.html}{\rule{3pt}{5pt}} 


 \subsection{{Lexical Scope and Block Structure}} 
\label{sec:scope} 
\htmlref{\htmladdimg{../icons/plt.gif}}{drn:sec:scope}

The introduction of {{\tt local}\/} requires some additional terminology
concerning the syntax of Scheme and the structure of
functions. Specifically, we need words to discuss the usage of names, {\it e.g.},
variables, function names, and structure names.  For a simple example,
consider the following two definitions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt (f}\  \hbox{$\underline{\mbox{{{\tt x}\/}}}$}{\tt )}\  {\tt (+}\  {\tt (*}\  \hbox{$\underline{\mbox{{{\tt x}\/}}}$}\  \hbox{$\underline{\mbox{{{\tt x}\/}}}$}{\tt )}\  {\tt 25))}\\
{\tt (define}\  {\tt (g}\  {\tt x)}\  {\tt (*}\  {\tt 12}\  {\tt (expt}\  {\tt x}\  {\tt 5)))}
\end{tabbing}\end{minipage}\end{center}
Clearly, the underlined occurrences of {{\tt x}\/} in {{\tt f}\/} are
completely unrelated to the occurrences of {{\tt x}\/} in {{\tt g}\/}. As
mentioned before, if we systematically replaced the underlined occurrences
with {{\tt y}\/}, the function would still compute the exact same
numbers. In short, the underlined occurrences of {{\tt x}\/} only mean
something in the definition of {{\tt f}\/} and nowhere else.

At the same time, the first occurrence of {{\tt x}\/} is different from the
others. When we apply {{\tt f}\/} to a number {{\tt n}\/}, this occurrence
completely disappears; in contrast, the others are replaced with
{{\tt n}\/}. To distinguish these two forms of variable occurrences, we call
the one to the right of the {\sc {binding}} occurrence of {{\tt x}\/} and
those in the body the {\sc {bound}} occurrences. We also say that the
binding occurrence of {{\tt x}\/} binds all occurrences of {{\tt x}\/} in the
body of {{\tt f}\/}, and from the discussion above, the body of {{\tt f}\/}
is clearly the only textual region of the function where the underlined
binding occurrence of {{\tt x}\/} can bind other occurrences. The name of
this region is {{\tt x}\/}'s {\sc {lexical scope}}. We also say that the
definitions of {{\tt f}\/} and {{\tt g}\/} (or other definitions in the
{\tt Definitions} window) have {\sc {global scope}}. 

The description of an application of {{\tt f}\/} to a number
{{\tt n}\/} suggests the following pictorial representation of the
definition: 
\begin{center}
\begin{picture}(300,30)
\put(0,0){{{\tt (define}\ {\tt (f}\ {\tt x)}\ {\tt (+}\ {\tt (*}\ {\tt x}\ {\tt x)}\ {\tt 25))}\/}}
\put(52,10){\line(0,1){12}}
\put(52,10){\circle*{3}}
\put(52,22){\line(1,0){45}}
\put(90,22){\vector(0,-1){12}}
\put(97,22){\vector(0,-1){12}}
\end{picture}
\end{center}

The bullet over the first occurrence indicates that it is a binding
occurrence. The arrow that originates from the bullet suggests the
flow of values. That is, when the value of a binding occurrence
becomes known, the bound occurrences receive their values from
there. Put differently, when we know the binding occurrence of a
variable occurrence, we know where the value will come from during
an evaluation. 

Along similar lines, the scope of a variable also dictates where we
can rename it. If we wish to rename a parameter, say,  from {{\tt x}\/}
to {{\tt y}\/}, we search for all bound occurrences in the scope of the
parameter and replace them with {{\tt y}\/}. For example, if the
function definition is the one from above:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt (f}\  {\tt x)}\  {\tt (+}\  {\tt (*}\  {\tt x}\  {\tt x)}\  {\tt 25))}\end{tabbing}\end{minipage}\end{center}
renaming {{\tt x}\/} to {{\tt y}\/} affects two bound occurrences: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt (f}\  {\tt y)}\  {\tt (+}\  {\tt (*}\  {\tt y}\  {\tt y)}\  {\tt 25))}\end{tabbing}\end{minipage}\end{center}
No other occurrences of {{\tt x}\/} outside of the definitions need to
be changed. 

Obviously function definitions also introduce a binding occurrence
for the function name. If a definition introduces a function named
{{\tt f}\/}, the scope of {{\tt f}\/} is the entire sequence of
definitions: 
\begin{center}
\begin{picture}(300,80)
\put(0,70){{{\tt (define}\ {\tt (e}\ {\tt z)}\ {\tt (f}\ {\tt (*}\ {\tt z}\ {\tt z)))}\/}}
\put(65,57){\vector(0,1){10}}
\put(42,57){\line(1,0){23}}
\put(42,45){\line(0,1){12}}
\put(42,45){\circle*{3}}
\put(0,35){{{\tt (define}\ {\tt (f}\ {\tt x)}\ {\tt (+}\ {\tt (*}\ {\tt x}\ {\tt x)}\ {\tt 25))}\/}}
\put(42,30){\line(0,-1){08}}
\put(42,30){\circle*{3}}
\put(42,22){\line(1,0){90}}
\put(82,22){\vector(0,-1){12}}
\put(132,22){\vector(0,-1){12}}
\put(0,0){{{\tt (define}\ {\tt (g}\ {\tt y)}\ {\tt (+}\ {\tt (f}\ {\tt (+}\ {\tt y}\ {\tt 1))}\ {\tt (f}\ {\tt (-}\ {\tt y}\ {\tt 1)))}\/}}
\end{picture}
\end{center}

That is, the scope of {{\tt f}\/} includes all definitions above and
below the definition of {{\tt f}\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 18.2.1}

 \label{ex:scope1}
Here is a simple Scheme program: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (p1}\  {\tt x}\  {\tt y)}\  \\
\>{\tt (+}\  \={\tt (*}\  {\tt x}\  {\tt y)}
\\
\>\>{\tt (+}\  \={\tt (*}\  {\tt 2}\  {\tt x)}
\\
\>\>\>{\tt (+}\  {\tt (*}\  {\tt 2}\  {\tt y)}\  {\tt 22))))}
\\
{\tt (d}\={\tt efine}\  {\tt (p2}\  {\tt x)}
\\
\>{\tt (+}\  {\tt (*}\  {\tt 55}\  {\tt x)}\  {\tt (+}\  {\tt x}\  {\tt 11)))}
\\
{\tt (d}\={\tt efine}\  {\tt (p3}\  {\tt x)}
\\
\>{\tt (+}\  \={\tt (p1}\  {\tt x}\  {\tt 0)}
\\
\>\>{\tt (+}\  {\tt (p1}\  {\tt x}\  {\tt 1)}\  {\tt (p2}\  {\tt x))))}
\end{tabbing}\end{minipage}\end{center}
Draw arrows from {{\tt p1}\/}'s {{\tt x}\/} parameter to all its bound
occurrences. Draw arrows from {{\tt p1}\/} to all bound occurrences of 
{{\tt p1}\/}.

Copy the function and rename the parameter {{\tt x}\/} of {{\tt p1}\/} to
{{\tt a}\/} and the parameter {{\tt x}\/} of {{\tt p3}\/} to
{{\tt b}\/}.

Check the results with DrScheme's {\tt Check Syntax} button.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/scope1.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


In contrast to top-level function definitions, the scope of the definitions
in a {{\tt local}\/} are limited. Specifically, the scope of local
definitions {\em is\/} the {{\bf local}-exp\-res\-sion}. 
Consider the definition of an auxiliary function {{\tt f}\/} in
a {{\bf local}-exp\-res\-sion}. It binds all occurrences within the
{{\bf local}-exp\-res\-sion} but none that occur outside: 
\begin{center}
\begin{picture}(300,140)(0,-40)
\put(0,70){{{\tt (local}\ {\tt (}\/}}
\put(36,0)
{ \begin{picture}(160,80)    
\put(0,70){{{\tt (define}\ {\tt (e}\ {\tt z)}\ {\tt (f}\ {\tt (*}\ {\tt z}\ {\tt z)))}\/}}  
\put(65,57){\vector(0,1){10}}  
\put(42,57){\line(1,0){23}}  
\put(42,45){\line(0,1){12}}  
\put(42,45){\circle*{3}}  
\put(0,35){{{\tt (define}\ {\tt (f}\ {\tt x)}\ {\tt (+}\ {\tt (*}\ {\tt x}\ {\tt x)}\ {\tt 25))}\/}}  
\put(42,30){\line(0,-1){08}}  
\put(42,30){\circle*{3}}  
\put(42,22){\vector(-1,-1){38}}  
\put(42,22){\line(1,0){90}}  
\put(82,22){\vector(0,-1){12}}  
\put(132,22){\vector(0,-1){12}}  
\put(0,0){{{\tt (define}\ {\tt (g}\ {\tt y)}\ {\tt (+}\ {\tt (f}\ {\tt (+}\ {\tt y}\ {\tt 1))}\ {\tt (f}\ {\tt (-}\ {\tt y}\ {\tt 1)))}\/}} \end{picture}}
\put(22,-24){\mbox{\ldots\ ({{\tt f}\/} \ldots) \ldots}}
\put(00,-50){\mbox{\ldots\ ({{\tt f}\/} \ldots) \ldots}}
\put(00,+90){\mbox{\ldots\ ({{\tt f}\/} \ldots) \ldots}}
\end{picture}
\end{center}
The two occurrences outside of {{\tt local}\/} are not bound by the local
definition of {{\tt f}\/}.

As always, the parameters of a function definition, local or not, is only
bound in the function's body and nowhere else: 
\begin{center}
\begin{picture}(300,50)(0,-30)
\put(0,0){{{\tt (local}\ {\tt (}\/}}
\put(36,0)
{ \begin{picture}(120,30) 
\put(0,0){{{\tt (define}\ {\tt (f}\ {\tt x)}\ {\tt (+}\ {\tt (*}\ {\tt x}\ {\tt x)}\ {\tt 25))}\/}} 
\put(52,10){\line(0,1){12}} 
\put(52,10){\circle*{3}} 
\put(52,22){\line(1,0){45}} 
\put(90,22){\vector(0,-1){12}} 
\put(97,22){\vector(0,-1){12}} \end{picture} }
\put(22,-20){\mbox{{{\tt ...}\ {\tt x}\/} \ldots)}}
\end{picture}
\end{center}


\setbox1=\hbox{\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt (f}\  {\tt x)}\  {\tt (+}\  {\tt (*}\  {\tt x}\  {\tt x)}\  {\tt 55))}\\
\>\>{\tt (define}\  {\tt (g}\  {\tt y)}\  {\tt (+}\  {\tt (f}\  {\tt y)}\  {\tt 10)))} \\
\>{\tt (f}\  {\tt z))} \end{tabbing}\end{minipage}}
\newbox{\newp}\sbox{\newp}{\fbox{\box1}}

\setbox2=\hbox{\begin{minipage}[t]{8in}\begin{tabbing}{\tt (+}\  {\tt (*}\  {\tt 2}\  {\tt x)}\  {\tt 10)}\end{tabbing}\end{minipage}}
\newbox{\newpp}\sbox{\newpp}{\fbox{\box2}}

Since the scope of a function name or a function parameter is a textual
region, people often draw a box to indicate some scope. More precisely, for
parameters a box is drawn around the body of a function:

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt x)}\\
\>\usebox{\newpp}{\tt )}
\end{tabbing}\end{minipage}\end{center}
\noindent In the case of a local definition, the box surrounds the entire
local expression:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (h}\  {\tt z)}\\
\>\usebox{\newp}\  {\tt )}
\end{tabbing}\end{minipage}\end{center}
\noindent In this example, the box describes the scope of the definitions
of {{\tt f}\/} and {{\tt g}\/}.

\setbox3=\hbox{\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt (f}\  {\tt x}\  {\tt y)}\  {\tt (+}\  {\tt (*}\  {\tt x}\  {\tt y)}\  {\tt (+}\  {\tt x}\  {\tt y)))}\\
\>\>{\tt (d}\={\tt efine}\  {\tt (g}\  {\tt z)} \\
\>\>\>\usebox{\newp}\  {\tt )} \\
\>\>{\tt (define}\  {\tt (h}\  {\tt x)}\  {\tt (f}\  {\tt x}\  {\tt (g}\  {\tt x))))} \\
\>{\tt (h}\  {\tt y))} \end{tabbing}\end{minipage}}
\newbox{\newp}\sbox{\newp}{\fbox{\box3}}

Using a box for a scope, we can also easily understand what it means to
re-use the name of function inside of a {{\bf local}-exp\-res\-sion}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (a-function}\  {\tt y)}\\
\>\usebox{\newp}\  {\tt )}
\end{tabbing}\end{minipage}\end{center}
\noindent The inner box describes the scope of the inner definition of
{{\tt f}\/}; the outer box is the scope of the outer definition of
{{\tt f}\/}.  Accordingly, all occurrences of {{\tt f}\/} in the inner box
refer to the inner {{\tt local}\/}; all those in the outer box refer to the
definition in the outer {{\tt local}\/}.In other words, the scope of the
outer definition of {{\tt f}\/} has a hole: the inner box, which is the
scope of the inner definition of {{\tt f}\/}.

\setbox1=\hbox{\begin{minipage}[t]{8in}\begin{tabbing}{\tt (+}\  {\tt x}\  {\tt (*}\  {\tt x}\  {\tt 2))}\end{tabbing}\end{minipage}}
\newbox{\newp}\sbox{\newp}{\fbox{\box1}}

\setbox2=\hbox{\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt (g}\  {\tt x)}\  \usebox{\newp}{\tt ))}\\
\>{\tt (g}\  {\tt x))} \end{tabbing}\end{minipage}}
\newbox{\newpp}\sbox{\newpp}{\fbox{\box2}}

Holes can also occur in the scope of a parameter definition. Here is an
example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt x)}\\
\>\usebox{\newpp}{\tt )}
\end{tabbing}\end{minipage}\end{center}
\noindent In this function, the parameter {{\tt x}\/} is used twice: for the
function {{\tt f}\/} and for {{\tt g}\/}. The scope of the latter is nested 
in the scope of the former and is thus a hole for the scope of the outer
use of {{\tt x}\/}. 



In general, if the same name occurs more than once in a function, the boxes
that describe the corresponding scopes never overlap. In some cases the
boxes are nested within each other, which gives rise to holes. Still, the
picture is always that of a hierarchy of smaller and smaller nested boxes. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 
{\bf Exercise 18.2.2}

 \label{ex:scope2}
Here is a simple Scheme function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sort}\ {\tt :}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (sort}\  {\tt alon)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (d}\={\tt efine}\  {\tt (sort}\  {\tt alon)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt (cons?}\  {\tt alon)}\  {\tt (insert}\  {\tt (first}\  {\tt alon)}\  {\tt (sort}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (insert}\  {\tt an}\  {\tt alon)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt (list}\  {\tt an)\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt (>}\  {\tt an}\  {\tt (first}\  {\tt alon))}\  {\tt (cons}\  {\tt an}\  {\tt alon)\char'135}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt (first}\  {\tt alon)}\  {\tt (insert}\  {\tt an}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt )\char'135}{\tt )))}
\\
\>\>{\tt (sort}\  {\tt alon)))}
\end{tabbing}\end{minipage}\end{center}
Draw a box around the scope of each binding occurrence of {{\tt sort}\/} and
{{\tt alon}\/}. Then draw arrows from each occurrence of {{\tt sort}\/} to
the matching binding occurrence.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/scope2.html}{\rule{3pt}{5pt}}


{\bf Exercise 18.2.3}

 \label{ex:scope-meaning}
Recall that each occurrence of a variable receives its value from the
corresponding binding occurrence. Consider the following definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt (cons}\  {\tt 1}\  \hbox{$\underline{\mbox{{{\tt x}\/}}}$}{\tt ))}\end{tabbing}\end{minipage}\end{center}
Where is the underlined occurrence of {{\tt x}\/} bound? Since the
definition is a variable definition and not a function definition, we need
to evaluate the right-hand side if we wish to work with this function. What
is the value of the right-hand side according to our rules?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/scope-meaning.html}{\rule{3pt}{5pt}}



%% BEGIN INLINED FILE: curriculum3.dl.tex




\newpage\part{Abstracting Designs}\thispagestyle{empty}\newpage \label{part:abstract}

\section{Similarities in Definitions} \label{sec:similarities}

Many of our data definitions and function definitions look alike. For
example, the definition for a list of symbols differs from that of a list
of numbers in only two regards: the name of the class of data and the words
``symbol'' and ``number.'' Similarly, a function that looks for a specific
symbol in a list of symbols is nearly indistinguishable from one that looks
for a specific number in a list of numbers.

Repetitions are the source of many programming mistakes.  Therefore good
programmers try to avoid repetitions as much as possible. As we develop a
set functions, especially functions derived from the same template, we soon
learn to spot similarities.  It is then time to revise the functions so as
to eliminate the repetitions as much as possible. Put differently, a set of
functions is just like an essay or a memo or a novel or some other piece of
writing. The first draft is just a { draft\/}. Unless we edit the essay
several times, it does not express our ideas clearly and concisely. It is a
pain to read for others. Because functions are read by many other people
and because real functions are modified after reading, we must learn ``to
edit'' functions.

The elimination of repetitions is the most important step in the (program)
editing process.  In this section, we discuss similarities in function
definitions and in data definitions and how to eliminate
them.\footnote{Different programming languages support different ways of eliminating repetitions, but they are all based on similar principles.}

\subsection{Similarities in Functions}
 \label{sec:prog-similarities} 
The use of our design recipes entirely determines a function's template---or
basic organization---from the data definition for the input. Indeed, the
template is an alternative method of expressing what we know about the input
data. Not surprisingly, functions that consume the same kind of data look
alike.

\hrule
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt contains-doll\char'077}\ {\tt :}\ {\tt los}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt alos}\/} contains }
\\
\hbox{;; the symbol {{\tt \char'047}{\tt doll}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (contains-doll\char'077}\  {\tt alos)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alos)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (first}\  {\tt alos)}\  \fbox{{\sf 'doll}}{\tt )}
\\
\>\>\>\>\>{\sf true}{\tt \char'135}
\\
\>\>\>\>{\tt \char'133}\={\tt else}\  
\\
\>\>\>\>\>{\tt (contains-doll\char'077}\  {\tt (rest}\  {\tt alos))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt contains-car\char'077}\ {\tt :}\ {\tt los}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt alos}\/} contains }
\\
\hbox{;; the symbol {{\tt \char'047}{\tt car}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (contains-car\char'077}\  {\tt alos)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alos)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (first}\  {\tt alos)}\  \fbox{{\sf 'car}}{\tt )}
\\
\>\>\>\>\>{\sf true}{\tt \char'135}
\\
\>\>\>\>{\tt \char'133}\={\tt else}\  
\\
\>\>\>\>\>{\tt (contains-car\char'077}\  {\tt (rest}\  {\tt alos))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\end{center}
\center{Figure: Two similar functions}\label{fig:cmp-doll-car}
\hrule

Take a look at the two functions in figure~\ref{fig:cmp-doll-car}, which
consume lists of symbols (names of toys) and look for specific toys.  The
function on the left looks for {{\tt \char'047}{\tt doll}\/}, the one on the right for
{{\tt \char'047}{\tt car}\/} in a list of symbols (los). The two functions are nearly
indistinguishable.  Each consumes lists of symbols; each function body
consists of a {{\bf cond}-exp\-res\-sion}s with two clauses. Each produces
{{\sf false}\/} if the input is {{\tt empty}\/}; each uses a second, nested
{{\bf cond}-exp\-res\-sion} to determine whether the first item is the desired
item. The only difference is the symbol that is used in the comparison of
the nested {{\bf cond}-exp\-res\-sion}: {{\tt contains-doll\char'077}\/} uses
{{\tt \char'047}{\tt doll}\/} and {{\tt contains-car\char'077}\/} uses {{\tt \char'047}{\tt car}\/}, of course. To
highlight the differences, the two symbols are boxed.

Good programmers are too lazy to define several closely related
functions. Instead they define a single function that can look for both a
{{\tt \char'047}{\tt doll}\/} and a {{\tt \char'047}{\tt car}\/} in a list of toys. This more general
function consumes an additional piece of data, the symbol that we are
looking for, but is otherwise like the two original functions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt contains\char'077}\ {\tt :}\ {\tt symbol}\ {\tt los}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt alos}\/} contains the symbol {{\tt s}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (contains\char'077}\  {\tt s}\  {\tt alos)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alos)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (first}\  {\tt alos)}\  \fbox{{\it s\/}}{\tt )}
\\
\>\>\>\>{\sf true}{\tt \char'135}
\\
\>\>\>{\tt \char'133}\={\tt else}\  
\\
\>\>\>\>{\tt (contains\char'077}\  {\tt s}\  {\tt (rest}\  {\tt alos))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
We can now look for {{\tt \char'047}{\tt doll}\/} by applying {{\tt contains\char'077}\/} to
{{\tt \char'047}{\tt doll}\/} and a list of symbols. But {{\tt contains\char'077}\/} works for any
other symbol, too. Defining the single version has solved many related
problems at once.

The process of combining two related functions into a single definition is
called {\sc {functional abstraction}}. Defining abstract versions of
functions is highly beneficial. The first benefit is that a single function
can perform many different tasks. In our first example, {{\tt contains\char'077}\/}
can search for many different symbols instead of just one concrete
symbol.\footnote{Computing borrows the term ``abstract'' from mathematics. A mathematician refers to ``6'' as an abstract number because it only represents all different ways of naming six things.  In contrast, ``6 inches'' or ``6 eggs'' are concrete instances of ``6'' because they express a measurement and a count.}

\hrule
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt below}\ {\tt :}\ {\tt lon}\ {\tt number}\ {}{\tt ->}{}\ {\tt lon}\/}}\\
\hbox{;; to construct a list of those numbers }
\\
\hbox{;; on {{\tt alon}\/} that are below {{\tt t}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (below}\  {\tt alon}\  {\tt t)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}\={\tt (}\fbox{$<$}\  {\tt (first}\  {\tt alon)}\  {\tt t)}
\\
\>\>\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt alon)}
\\
\>\>\>\>\>\>{\tt (below}\  {\tt (rest}\  {\tt alon)}\  {\tt t))\char'135}
\\
\>\>\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>\>\>{\tt (below}\  {\tt (rest}\  {\tt alon)}\  {\tt t)\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt above}\ {\tt :}\ {\tt lon}\ {\tt number}\ {}{\tt ->}{}\ {\tt lon}\/}}\\
\hbox{;; to construct a list of those numbers }
\\
\hbox{;; on {{\tt alon}\/} that are above {{\tt t}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (above}\  {\tt alon}\  {\tt t)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}\={\tt (}\fbox{$>$}\  {\tt (first}\  {\tt alon)}\  {\tt t)}\  
\\
\>\>\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt alon)}
\\
\>\>\>\>\>\>{\tt (above}\  {\tt (rest}\  {\tt alon)}\  {\tt t))\char'135}
\\
\>\>\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>\>\>{\tt (above}\  {\tt (rest}\  {\tt alon)}\  {\tt t)\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\end{center}
\center{Figure: Two similar functions}\label{fig:cmp-above-below}
\hrule

In the case of {{\tt contains-doll\char'077}\/} and {{\tt contains-car\char'077}\/},
abstraction is uninteresting. There are, however, other, more interesting
cases: see figure~\ref{fig:cmp-above-below}.  The function on the left
consumes a list of numbers and a threshold and produces a list of all those
numbers that are below the threshold; the one on the right produces all
those that are above a threshold.

The difference between the two functions is the comparison operator. The
left uses $<$, the right one $>$. Following the first example, we abstract
over the two functions with an additional parameter that stands for the
concrete relational operator in {{\tt below}\/} and {{\tt above}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (filter1}\  {\tt rel-op}\  {\tt alon}\  {\tt t)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (}\fbox{{\it rel-op}}\  {\tt (first}\  {\tt alon)}\  {\tt t)}\  
\\
\>\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt alon)}
\\
\>\>\>\>\>{\tt (filter1}\  {\tt rel-op}\  {\tt (rest}\  {\tt alon)}\  {\tt t))\char'135}
\\
\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>{\tt (filter1}\  {\tt rel-op}\  {\tt (rest}\  {\tt alon)}\  {\tt t)\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
To apply this new function, we must supply three arguments: a relational
operator {{\tt R}\/} that compares two numbers, a list {{\tt L}\/} of
numbers, and a number {{\tt N}\/}. The function then extracts all those items
{{\tt i}\/} in {{\tt L}\/} for which {{\tt (R}\ {\tt i}\ {\tt N)}\/} evaluates to
true. Since we do not know how to write down contracts for functions like
{{\tt filter1}\/}, we omit the contract for now. We will discuss the problem
of contracts in section~\ref{sec:abs-contracts} below.

Let us see how {{\tt filter1}\/} works with an example. Clearly, as long as
the input list is {{\tt empty}\/}, the result is {{\tt empty}\/}, too, no
matter what the other arguments are:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (filter1}\  {\tt <}\  {\tt empty}\  {\tt 5)}\\
{\tt =}\  {\tt empty}
\end{tabbing}\end{minipage}\end{center}
So next we look at a slightly more complicated case: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (filter1}\  {\tt <}\  {\tt (cons}\  {\tt 4}\  {\tt empty)}\  {\tt 5)}\end{tabbing}\end{minipage}\end{center}
The result should be {{\tt (cons}\ {\tt 4}\ {\tt empty)}\/} because the only item of this
list is {{\tt 4}\/} and {{\tt (<}\ {\tt 4}\ {\tt 5)}\/} is true. 

The first step of the hand-evaluation is based on the standard rule of
application: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (filter1}\  {\tt <}\  {\tt (cons}\  {\tt 4}\  {\tt empty)}\  {\tt 5)}\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (empty?}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt empty\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (<}\  {\tt (first}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5)}\  
\\
\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}
\\
\>\>\>\>{\tt (filter1}\  {\tt <}\  {\tt (rest}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5))\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (filter1}\  {\tt <}\  {\tt (rest}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5)\char'135}{\tt )\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
That is, it is the body of {{\tt filter1}\/} with all occurrences of
{{\tt rel-op}\/} replaced by {{\tt <}\/}, t replaced by {{\tt 5}\/}, and
{{\tt alon}\/} replaced by {{\tt (cons}\ {\tt 6}\ {\tt empty)}\/}. 

The rest of the evaluation is straightforward and uses only the basic laws
of Scheme: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (empty?}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt empty\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (<}\  {\tt (first}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5)}\  
\\
\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}
\\
\>\>\>\>{\tt (filter1}\  {\tt <}\  {\tt (rest}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5))\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (filter1}\  {\tt <}\  {\tt (rest}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5)\char'135}{\tt )\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (c}\={\tt ond}\\
\>{\tt \char'133}\={\tt (<}\  {\tt (first}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5)}
\\
\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}
\\
\>\>\>{\tt (filter1}\  {\tt <}\  {\tt (rest}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5))\char'135}
\\
\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>{\tt (filter1}\  {\tt <}\  {\tt (rest}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5)\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (c}\={\tt ond}\\
\>{\tt \char'133}\={\tt (<}\  {\tt 4}\  {\tt 5)}
\\
\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}
\\
\>\>\>{\tt (filter1}\  {\tt <}\  {\tt (rest}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5))\char'135}
\\
\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>{\tt (filter1}\  {\tt <}\  {\tt (rest}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5)\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (c}\={\tt ond}\\
\>{\tt \char'133}\={\sf true}
\\
\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}
\\
\>\>\>{\tt (filter1}\  {\tt <}\  {\tt (rest}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5))\char'135}
\\
\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>{\tt (filter1}\  {\tt <}\  {\tt (rest}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5)\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (cons}\  {\tt 4}\  {\tt (filter1}\  {\tt <}\  {\tt (rest}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5))}\\
{\tt =}\  {\tt (cons}\  {\tt 4}\  {\tt (filter1}\  {\tt <}\  {\tt empty}\  {\tt 5))}
\\
{\tt =}\  {\tt (cons}\  {\tt 4}\  {\tt empty)}
\end{tabbing}\end{minipage}\end{center}
The last step is the equation we discussed as our first case.

Our final example is an application of {{\tt filter1}\/} to a list of two
items:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (filter1}\  {\tt <}\  {\tt (cons}\  {\tt 6}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5)}\\
{\tt =}\  {\tt (filter1}\  {\tt <}\  {\tt (cons}\  {\tt 4}\  {\tt empty)}\  {\tt 5)}
\\
{\tt =}\  {\tt (cons}\  {\tt 4}\  {\tt (filter1}\  {\tt <}\  {\tt empty}\  {\tt 5))}
\\
{\tt =}\  {\tt (cons}\  {\tt 4}\  {\tt empty)}
\end{tabbing}\end{minipage}\end{center}
The only new step is the first one. It says that {{\tt filter1}\/}
determines that the first item on the list is not less than the threshold,
and that it therefore is not {{\tt cons}\/}ed onto the result of the natural
recursion.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 19.1.1}

 \label{ex:filter-eval1}
Verify the equation 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (filter1}\  {\tt <}\  {\tt (cons}\  {\tt 6}\  {\tt (cons}\  {\tt 4}\  {\tt empty))}\  {\tt 5)}\\
{\tt =}\  {\tt (filter1}\  {\tt <}\  {\tt (cons}\  {\tt 4}\  {\tt empty)}\  {\tt 5)}
\end{tabbing}\end{minipage}\end{center}
with a hand-evaluation that shows every step. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/filter-eval1.html}{\rule{3pt}{5pt}}


{\bf Exercise 19.1.2}

 \label{ex:filter-eval2}
Evaluate the expression 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (filter1}\  {\tt >}\  {\tt (cons}\  {\tt 8}\  {\tt (cons}\  {\tt 6}\  {\tt (cons}\  {\tt 4}\  {\tt empty)))}\  {\tt 5)}\end{tabbing}\end{minipage}\end{center}
by hand. Show only the essential steps.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/filter-eval2.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


The calculations show that {{\tt (filter1}\ {\tt <}\ {\tt alon}\ {\tt t)}\/} computes the same
result as {{\tt (below}\ {\tt alon}\ {\tt t)}\/}, which is what we expected. Similar
reasoning shows that {{\tt (filter1}\ {\tt >}\ {\tt alon}\ {\tt t)}\/} produces the same output
as {{\tt (above}\ {\tt alon}\ {\tt t)}\/}. So suppose we define the following: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt below1}\ {\tt :}\ {\tt lon}\ {\tt number}\ {}{\tt ->}{}\ {\tt lon}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (below1}\  {\tt alon}\  {\tt t)}
\\
\>{\tt (filter1}\  {\tt <}\  {\tt alon}\  {\tt t))}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt above1}\ {\tt :}\ {\tt lon}\ {\tt number}\ {}{\tt ->}{}\ {\tt lon}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (above1}\  {\tt alon}\  {\tt t)}\  
\\
\>{\tt (filter1}\  {\tt >}\  {\tt alon}\  {\tt t))}
\end{tabbing}\end{minipage}
\end{center}
Clearly, {{\tt below1}\/} produces the same results as {{\tt below}\/} when
given the same inputs, and {{\tt above1}\/} is related to {{\tt above}\/} in
the same manner. In short, we have defined {{\tt below}\/} and
{{\tt above}\/} as one-liners using {{\tt filter1}\/}. 

Better yet: once we have an {abstract} function like {{\tt filter1}\/}, we
can put it to other uses, too. Here are three of them:
\begin{enumerate}
\item {{\tt (filter1}\ {\tt =}\ {\tt alon}\ {\tt t)}\/}: This expression extracts all those
numbers in {{\tt alon}\/} that are equal to {{\tt t}\/}. 

\item {{\tt (filter1}\ {\tt <=}\ {\tt alon}\ {\tt t)}\/}: This one produces the list of numbers
in {{\tt alon}\/} that are less or equal to {{\tt t}\/}. 

\item {{\tt (filter1}\ {\tt >=}\ {\tt alon}\ {\tt t)}\/}: This last expression computes the list
of numbers that are greater or equal to the threshold.
\end{enumerate}

In general, {{\tt filter1}\/}'s first argument need not even be one of
Scheme's predefined operations; it can be any function that consumes two
numbers and produces a Boolean value. Consider the following example:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt squared>\char'077}\ {\tt :}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (squared>\char'077}\  {\tt x}\  {\tt c)}
\\
\>{\tt (>}\  {\tt (*}\  {\tt x}\  {\tt x)}\  {\tt c))}
\end{tabbing}\end{minipage}\end{center}
The function produces {{\sf true}\/} whenever the area of a square with side
{{\tt x}\/} is larger than some threshold {{\tt c}\/}, that is, the function
tests whether the claim $ x^2 > c $ holds. We now apply {{\tt filter1}\/} to
this function and a list of numbers: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (filter1}\  {\tt squared>\char'077}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5)}\  {\tt 10)}\end{tabbing}\end{minipage}\end{center}
This particular application extracts those numbers in {{\tt (list}\ {\tt 1}\ {\tt 2}\ {\tt 3}\ {\tt 4}{ }{\tt 5)}\/} whose square is larger than {{\tt 10}\/}.

Here is the beginning of a simple hand-evaluation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (filter1}\  {\tt squared>\char'077}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5)}\  {\tt 10)}\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (empty?}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5))}\  {\tt empty\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (squared>\char'077}\  {\tt (first}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5))}\  {\tt 10)}\  
\\
\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5))}
\\
\>\>\>\>{\tt (filter1}\  {\tt squared>\char'077}\  {\tt (rest}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5))}\  {\tt 10))\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (filter1}\  {\tt squared>\char'077}\  {\tt (rest}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5))}\  {\tt 10)\char'135}{\tt )\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
That is, we apply our standard law of application and calculate otherwise
as usual: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (c}\={\tt ond}\\
\>{\tt \char'133}\={\tt (squared>\char'077}\  {\tt 1}\  {\tt 10)}\  
\\
\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5))}
\\
\>\>\>{\tt (filter1}\  {\tt squared>\char'077}\  {\tt (rest}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5))}\  {\tt 10))\char'135}
\\
\>{\tt \char'133}\={\tt else}
\\
\>\>{\tt (filter1}\  {\tt squared>\char'077}\  {\tt (rest}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5))}\  {\tt 10)\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (c}\={\tt ond}\\
\>{\tt \char'133}\={\sf false}\  
\\
\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5))}
\\
\>\>\>{\tt (filter1}\  {\tt squared>\char'077}\  {\tt (rest}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5))}\  {\tt 10))\char'135}
\\
\>{\tt \char'133}\={\tt else}
\\
\>\>{\tt (filter1}\  {\tt squared>\char'077}\  {\tt (rest}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5))}\  {\tt 10)\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
The last step consists of several steps concerning {{\tt squared>\char'077}\/}, which
we can skip at this point:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (filter1}\  {\tt squared>\char'077}\  {\tt (list}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5)}\  {\tt 10)}\\
{\tt =}\  {\tt (filter1}\  {\tt squared>\char'077}\  {\tt (list}\  {\tt 3}\  {\tt 4}\  {\tt 5)}\  {\tt 10)}
\\
{\tt =}\  {\tt (filter1}\  {\tt squared>\char'077}\  {\tt (list}\  {\tt 4}\  {\tt 5)}\  {\tt 10)}
\end{tabbing}\end{minipage}\end{center}
We leave the remainder of the evaluation to the exercises. 


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 19.1.3}

 \label{ex:squared}
Show that 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (filter1}\  {\tt squared>\char'077}\  {\tt (list}\  {\tt 4}\  {\tt 5)}\  {\tt 10)}\\
{\tt =}\  {\tt (cons}\  {\tt 4}\  {\tt (filter1}\  {\tt squared>\char'077}\  {\tt (list}\  {\tt 5)}\  {\tt 10))}
\end{tabbing}\end{minipage}\end{center}
with a hand-evaluation. Act as if {{\tt squared>\char'077}\/} were
primitive. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/squared.html}{\rule{3pt}{5pt}} 


{\bf Exercise 19.1.4}

 \label{ex:filter}
The use of {{\tt squared>\char'077}\/} also suggests that the following 
function will work, too:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt squared10\char'077}\ {\tt :}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (squared10\char'077}\  {\tt x}\  {\tt c)}
\\
\>{\tt (>}\  {\tt (*}\  {\tt x}\  {\tt x)}\  {\tt 10))}
\end{tabbing}\end{minipage}\end{center}
In other words, the relational function that {{\tt filter1}\/} uses may
ignore its second argument. After all, we already know it and it stays
the same throughout the evaluation of {{\tt (filter1}\ {\tt squared>\char'077}\ {\tt alon}\ {\tt t)}\/}.

This, in turn, implies another simplification of the function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (d}\={\tt efine}\  {\tt (filter}\  {\tt predicate}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (predicate}\  {\tt (first}\  {\tt alon))}\  
\\
\>\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt alon)}
\\
\>\>\>\>\>{\tt (filter}\  {\tt predicate}\  {\tt (rest}\  {\tt alon)))\char'135}
\\
\>\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>\>{\tt (filter}\  {\tt predicate}\  {\tt (rest}\  {\tt alon))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The function {{\tt filter}\/} consumes only a relational function, called
{{\tt predicate}\/}, and a list of numbers. Every item {{\tt i}\/} on the
list is checked with {{\tt predicate}\/}. If {{\tt (predicate}\ {\tt i)}\/} holds,
{{\tt i}\/} is included in the output; if not, {{\tt i}\/} does not appear in
the result.

Show how to use {{\tt filter}\/} to define functions that are equivalent to
{{\tt below}\/} and {{\tt above}\/}. Test the definitions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/filter.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


So far we have seen that abstracted function definitions are more flexible
and more widely usable than specialized definitions. A second, and in
practice equally important, advantage of abstracted definitions is that we
can change a single definition to fix and improve many different uses.
Consider the two variants of {{\tt filter1}\/} in
figure~\ref{fig:filter1-mod}.  The first variant flattened the nested
{{\bf cond}-exp\-res\-sion}, something that an experienced programmer may wish
to do. The second variant uses a {{\bf local}-exp\-res\-sion} that makes the
nested {{\bf cond}-exp\-res\-sion} more readable.

\hrule 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (filter1}\  {\tt rel-op}\  {\tt alon}\  {\tt t)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}\={\tt (rel-op}\  {\tt (first}\  {\tt alon)}\  {\tt t)}\  
\\
\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt alon)}
\\
\>\>\>\>{\tt (filter1}\  {\tt rel-op}\  {\tt (rest}\  {\tt alon)}\  {\tt t))\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (filter1}\  {\tt rel-op}\  {\tt (rest}\  {\tt alon)}\  {\tt t)\char'135}{\tt ))}
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (filter1}\  {\tt rel-op}\  {\tt alon}\  {\tt t)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt first-item}\  {\tt (first}\  {\tt alon))}
\\
\>\>\>\>\>{\tt (d}\={\tt efine}\  {\tt rest-filtered}
\\
\>\>\>\>\>\>{\tt (filter1}\  {\tt rel-op}\  {\tt (rest}\  {\tt alon)}\  {\tt t)))}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (rel-op}\  {\tt first-item}\  {\tt t)}\  
\\
\>\>\>\>\>\>{\tt (cons}\  {\tt first-item}\  {\tt rest-filtered)\char'135}
\\
\>\>\>\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>\>\>\>{\tt rest-filtered\char'135}{\tt ))\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\end{center}

\center{Figure: Two modifications of {\it filter1}}
\label{fig:filter1-mod}
\hrule

Although both of these changes are trivial, the key is that { all\/}
uses of {{\tt filter1}\/}, including those to define the functions
{{\tt below1}\/} and {{\tt above1}\/}, benefit from this change. Similarly,
if the modification had fixed a logical mistake, all uses of the function
would be improved. Finally, it is even possible to add new tasks to
abstracted functions, for example, a mechanism for counting how many
elements are filtered. In that case all uses of the function would benefit
from the new functionality. We will encounter this form of improvement
later.

\htmladdimg{../icons/teacher.gif}~{\em These effects of abstraction are crucial for large, industrial programming projects. For that reason, programming language and software engineering research has focused on how to create single points of control in large projects. Of course, the same idea applies to all kinds of computer designs (word documents, spread sheets) and organizations in general.}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 19.1.5}

 \label{ex:abs-min-max}
\htmladdimg{../icons/teacher.gif}~{\em These functions are designed by strictly following the design recipe and by ignoring the advice on using auxiliary functions. The problem is that they recur { twice\/} over the rest of the list, which means they perform up to $n^2$ comparisons if the list contains {\it n\/} items in the wrong order. By improving the abstract function, we automatically get both correct.}

Abstract the following two functions into a single function: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt min}\ {\tt :}\ {\tt nelon}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to determine the smallest number}
\\
\hbox{;; on {{\tt alon}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (min}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt (rest}\  {\tt alon))}\  {\tt (first}\  {\tt alon)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (<}\  \={\tt (first}\  {\tt alon)}\  
\\
\>\>\>\>\>{\tt (min}\  {\tt (rest}\  {\tt alon)))}
\\
\>\>\>\>{\tt (first}\  {\tt alon)\char'135}
\\
\>\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>\>{\tt (min}\  {\tt (rest}\  {\tt alon))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt max}\ {\tt :}\ {\tt nelon}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to determine the largest number}
\\
\hbox{;; on {{\tt alon}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (max}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt (rest}\  {\tt alon))}\  {\tt (first}\  {\tt alon)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (>}\  \={\tt (first}\  {\tt alon)}
\\
\>\>\>\>\>{\tt (max}\  {\tt (rest}\  {\tt alon)))}
\\
\>\>\>\>{\tt (first}\  {\tt alon)\char'135}
\\
\>\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>\>{\tt (max}\  {\tt (rest}\  {\tt alon))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\end{center}
Both consume non-empty lists of numbers and produce a single number.  The
left one produces the smallest number in the list, the right one the
largest.

Define {{\tt min1}\/} and {{\tt max1}\/} in terms of the abstracted
function. Test each of them with the following two lists:
\begin{enumerate}
\item {{\tt (list}\ {\tt 3}\ {\tt 7}\ {\tt 6}\ {\tt 2}\ {\tt 9}\ {\tt 8)}\/}
\item {{\tt (list}\ {\tt 20}\ {\tt 19}\ {\tt 18}\ {\tt 17}\ {\tt 16}\ {\tt 15}\ {\tt 14}\ {\tt 13}\ {\tt 12}\ {\tt 11}\ {\tt 10}\ {\tt 9}\ {\tt 8}\ {\tt 7}\ {\tt 6}\ {\tt 5}\ {\tt 4}\ {\tt 3}\ {\tt 2}\ {\tt 1)}\/}
\item {{\tt (list}\ {\tt 1}\ {\tt 2}\ {\tt 3}\ {\tt 4}\ {\tt 5}\ {\tt 6}\ {\tt 7}\ {\tt 8}\ {\tt 9}\ {\tt 10}\ {\tt 11}\ {\tt 12}\ {\tt 13}\ {\tt 14}\ {\tt 15}\ {\tt 16}\ {\tt 17}\ {\tt 18}\ {\tt 19}\ {\tt 20)}\/}
\end{enumerate}
Why are they slow on the long lists? 

Improve the abstracted function. First, introduce a local name for the
result of the natural recursion. Then introduce a local, auxiliary function
that picks the ``interesting'' one of two numbers.  Test {{\tt min1}\/} and
{{\tt max1}\/} with the same inputs again.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/abs-min-max.html}{\rule{3pt}{5pt}}


{\bf Exercise 19.1.6}

 \label{ex:abs-sort}
Recall the definition of {{\tt sort}\/}, which consumes a list of numbers
and produces a sorted version: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sort}\ {\tt :}\ {\tt list-of-numbers}\ {}{\tt ->}{}\ {\tt list-of-numbers}\/}}\\
\hbox{;; to construct a list with all items from {{\tt alon}\/} in descending order}
\\
{\tt (d}\={\tt efine}\  {\tt (sort}\  {\tt alon)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (d}\={\tt efine}\  {\tt (sort}\  {\tt alon)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (insert}\  {\tt (first}\  {\tt alon)}\  {\tt (sort}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (insert}\  {\tt an}\  {\tt alon)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt (list}\  {\tt an)\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt (>}\  {\tt an}\  {\tt (first}\  {\tt alon))}\  {\tt (cons}\  {\tt an}\  {\tt alon)\char'135}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt (first}\  {\tt alon)}\  {\tt (insert}\  {\tt an}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt )\char'135}{\tt )))}
\\
\>\>{\tt (sort}\  {\tt alon)))}
\end{tabbing}\end{minipage}\end{center}
Define an abstract version of {{\tt sort}\/} that consumes the comparison
operation in addition to the list of numbers. Use the abstract version to
sort {{\tt (list}\ {\tt 2}\ {\tt 3}\ {\tt 1}\ {\tt 5}\ {\tt 4)}\/} in ascending and descending
order.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/abs-sort.html}{\rule{3pt}{5pt}}




\subsection{Similarities in Data Definitions} 
\label{sec:dd-similarities} 
Inspect the following two data definitions: 
\begin{center}
\fbox{\parbox[t]{2.2in}{A {\sl {list of numbers}} is either \begin{enumerate} \item{{\tt empty}\/} or  \item {{\tt (cons}\ {\tt s}\ {\tt l)}\/}  where       \begin{enumerate}         \item {{\tt s}\/} is \fbox{a number} and         \item {{\tt l}\/} is a \\
 list of numbers.~\rule[-8pt]{0pt}{15pt}       \end{enumerate} \end{enumerate}}}\hfill\fbox{\parbox[t]{2.2in}{A {\sl {list of IRs}} is either \begin{enumerate} \item{{\tt empty}\/} or  \item {{\tt (cons}\ {\tt s}\ {\tt l)}\/}  where       \begin{enumerate}         \item {{\tt s}\/} is \fbox{an IR} and         \item {{\tt l}\/} is a \\
 list of IRs.~\rule[-8pt]{0pt}{15pt}       \end{enumerate} \end{enumerate}}}
\end{center}

\noindent Both define a class of lists. The one on the left is the data
definition for lists of numbers; the one on the right describes lists of
inventory records, which we represent with structures:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt ir}\  {\tt (name}\  {\tt price))}\end{tabbing}\end{minipage}\end{center}

  \begin{quote} An {\sl {IR}} is a structure:\\
         \centerline{{{\tt (make-ir}\ {\tt n}\ {\tt p)}\/}}       where {{\tt n}\/} is a symbol and {{\tt p}\/} is a number. \end{quote}

Given the similarity between the data definitions, functions that consume
elements of these classes are similar, too.  Take a look at the
illustrative example in figure~\ref{fig:cmp-above-below2}. The function on
the left is the function {{\tt below}\/}, which filters numbers from a list
of numbers. The one on the right is {{\tt below-ir}\/}, which extracts those
inventory records from a list whose prices are below a certain threshold.
Except for the name of the function, which is arbitrary, the two
definitions differ in only one point: the relational operator.

\hrule
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt below}\ {\tt :}\ {\tt number}\ {\tt lon}\ {}{\tt ->}{}\ {\tt lon}\/}}\\
\hbox{;; to construct a list of those numbers}
\\
\hbox{;; on {{\tt alon}\/} that are below {{\tt t}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (below}\  {\tt alon}\  {\tt t)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (}\fbox{$<$}\  {\tt (first}\  {\tt alon)}\  {\tt t)}
\\
\>\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt alon)}
\\
\>\>\>\>\>{\tt (below}\  {\tt (rest}\  {\tt alon)}\  {\tt t))\char'135}
\\
\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>{\tt (below}\  {\tt (rest}\  {\tt alon)}\  {\tt t)\char'135}{\tt )\char'135}{\tt ))}
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt below-ir}\ {\tt :}\ {\tt number}\ {\tt loIR}\ {}{\tt ->}{}\ {\tt loIR}\/}}\\
\hbox{;; to construct a list of those records }
\\
\hbox{;; on {{\tt aloir}\/} that contain a price below {{\tt t}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (below}\  {\tt aloir}\  {\tt t)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt aloir)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (}\fbox{$<_{ir}$}\  {\tt (first}\  {\tt aloir)}\  {\tt t)}\  
\\
\>\>\>\>{\tt (}\={\tt cons}\  {\tt (first}\  {\tt aloir)}
\\
\>\>\>\>\>{\tt (below-ir}\  {\tt (rest}\  {\tt aloir)}\  {\tt t))\char'135}
\\
\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>{\tt (below-ir}\  {\tt (rest}\  {\tt aloir)}\  {\tt t)\char'135}{\tt )\char'135}{\tt ))}
\\
\relax
\\
\hbox{;; {$<_{ir}$\ {\tt :}\ {\tt IR}\ {\tt number}\ {}{\tt ->}{}\ {\tt boolean}\/}}
\\
{\tt (def}\={\tt ine}\  {\tt (}$<_{ir}$\  {\tt ir}\  {\tt p)}
\\
\>{\tt (<}\  {\tt (ir-price}\  {\tt ir)}\  {\tt p))}
\end{tabbing}\end{minipage}
\end{center}
\center{Figure: Marking the differences in similar functions}
\label{fig:cmp-above-below2}
\hrule

If we abstract the two functions, we obviously obtain {{\tt filter1}\/}.
Conversely, we can define {{\tt below-ir}\/} in terms of {{\tt filter1}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (below-ir1}\  {\tt aloir}\  {\tt t)}\\
\>{\tt (filter1}\  $<_{ir}$\  {\tt aloir}\  {\tt t))}
\end{tabbing}\end{minipage}\end{center}
It should not surprise us to discover yet another use for
{{\tt filter1}\/}---after all, we already argued that abstraction promotes
the re-use of functions for different purposes. Here we see that
{{\tt filter1}\/} not only filters lists of numbers but lists of arbitrary
things---as long as we can define a function that compares these arbitrary
things with numbers.

Indeed, all we need is a function that compares items on the list with the
items we pass to {{\tt filter1}\/} as the second argument. Here is a function
that extracts all items with the same label from a list of inventory
records: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt find}\ {\tt :}\ {\tt loIR}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt aloir}\/} contains a record for {{\tt t}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (find}\  {\tt aloir}\  {\tt t)}
\\
\>{\tt (cons?}\  {\tt (filter1}\  {\tt eq-ir\char'077}\  {\tt aloir}\  {\tt t)))}
\\
\hbox{;; {{\tt eq-ir\char'077}\ {\tt :}\ {\tt IR}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt boolean}\/}}
\\
\hbox{;; to compare {{\tt ir}\/}'s name and {{\tt p}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (eq-ir\char'077}\  {\tt ir}\  {\tt p)}
\\
\>{\tt (symbol=?}\  {\tt (ir-name}\  {\tt ir)}\  {\tt p))}
\end{tabbing}\end{minipage}\end{center}
This new relational operator compares the name in an inventory record with
some other symbol. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 19.2.1}

 \label{ex:filter-eval3}
Determine the values of 
\begin{enumerate}
\item
{{\tt (below-ir1}\ {\tt 10}\ {\tt (list}\ {\tt (make-ir}\ {\tt \char'047}{\tt doll}\ {\tt 8)}\ {\tt (make-ir}\ {\tt \char'047}{\tt robot}\ {\tt 12)))}\/}
\item
{{\tt (find}\ {\tt \char'047}{\tt doll}\ {\tt (list}\ {\tt (make-ir}\ {\tt \char'047}{\tt doll}\ {\tt 8)}\ {\tt (make-ir}\ {\tt \char'047}{\tt robot}\ {\tt 12)}\ {\tt (make-ir}\ {\tt \char'047}{\tt doll}\ {\tt 13)))}\/}
\end{enumerate}
by hand and with DrScheme. Show only those lines that introduce new
applications of {{\tt filter1}\/} to values. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/filter-eval3.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


In short, {{\tt filter1}\/} uniformly works on many different shapes of
input data.  The word ``uniformly'' means that if {{\tt filter1}\/} is
applied to a list of {{\tt X}\/}, its result is also a list of
{{\tt X}\/}---no matter what kind of Scheme data {{\tt X}\/} is.  Such
functions are called {\sc {polymorphic}} or {\sc {generic}} functions. 

Of course, {{\tt filter1}\/} is not the only function that can process
arbitrary lists. There are many other functions that process lists
independently of what they contain. Here are two functions that determine
the length of lists of numbers and {{\tt IR}\/}s: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt length-lon}\ {\tt :}\ {\tt lon}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (length-lon}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}\  
\\
\>\>\>{\tt (+}\  {\tt (length-lon}\  {\tt (rest}\  {\tt alon))}\  {\tt 1)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt length-ir}\ {\tt :}\ {\tt loIR}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (length-ir}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (+}\  {\tt (length-ir}\  {\tt (rest}\  {\tt alon))}\  {\tt 1)\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\end{center}
The two functions only differ in their names. If we had chosen the same
name, say {{\tt length}\/}, the two definitions would be identical. 

To write precise contracts for functions such as {{\tt length}\/}, we need
{parametric data definitions}. A {\sc {parametric data definition}} does
not specify all pieces of data; instead it uses variables to say that { any} form of Scheme data can be used in a certain place. Roughly speaking,
a parametric data definition abstracts from a reference to a particular
collection of data in the same manner as a function abstracts from a
particular value.

Here is a parametric definition of lists of {{\tt ITEM}\/}s:

   \begin{quote} A {\sl {list of ITEM}} is either \begin{enumerate} \item{{\tt empty}\/} or  \item {{\tt (cons}\ {\tt s}\ {\tt l)}\/}  where       \begin{enumerate}         \item {{\tt s}\/} is \fbox{an ITEM} and         \item {{\tt l}\/} is a list of ITEM.~\rule[-8pt]{0pt}{15pt}       \end{enumerate} \end{enumerate} \end{quote}

\noindent The token {{\tt ITEM}\/} is a {\sc {type variable}} that stands
for any arbitrary collection of Scheme data: symbols, numbers, booleans,
{{\tt IR}\/}s, etc. By replacing {{\tt ITEM}\/} with one of these names, we
get a concrete instance of this abstract data definition for lists of
symbols, numbers, booleans, {{\tt IR}\/}s, etc. To make the language of
contracts more concise, we introduce an additional abbreviation:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (listof}\  {\tt ITEM)}\end{tabbing}\end{minipage}\end{center}
We use {{\tt (listof}\ {\tt ITEM)}\/} as the name of abstract data definitions like
the above. In other words, we define {{\tt (listof}\ {\tt ITEM)}\/} as follows: 

   \begin{quote} A {\sl {{{\tt (listof}\ {\tt ITEM)}\/}}} is either \begin{enumerate} \item{{\tt empty}\/} or  \item {{\tt (cons}\ {\tt s}\ {\tt l)}\/}  where       \begin{enumerate}         \item {{\tt s}\/} is \fbox{an {{\tt ITEM}\/}} and         \item {{\tt l}\/} is a {{\tt (listof}\ {\tt ITEM)}\/}.~\rule[-8pt]{0pt}{15pt}       \end{enumerate} \end{enumerate} \end{quote}

\noindent Then we can use {{\tt (listof}\ {\tt symbol)}\/} for the class of all
lists of symbols, {{\tt (listof}\ {\tt number)}\/} for the class of all lists of
numbers, {{\tt (listof}\ {\tt (listof}\ {\tt number))}\/} for the class of all lists of
lists of numbers, {\it etc\/}.

In contracts, we use {{\tt (listof}\ {\tt X)}\/} to say that the function works
on arbitrary lists: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt length}\ {\tt :}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the length of a list }
\\
{\tt (d}\={\tt efine}\  {\tt (length}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (length}\  {\tt (rest}\  {\tt alon))}\  {\tt 1)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The {{\tt X}\/} is just a variable, a name that stands for some class of
data.  If we now apply {{\tt length}\/} to an element of, say,
{{\tt (listof}\ {\tt symbol)}\/} or {{\tt (listof}\ {\tt IR)}\/}, we get a number.

The function {{\tt length}\/} is an example of simple polymorphism. It works
on all classes of lists. While there are other useful examples of simple
polymorphic functions, the more common cases require that we define functions
like {{\tt filter1}\/}, which consume a parametric form of data and functions
that work on this data. This combination is extremely powerful and greatly
facilitates the construction and maintenance of software systems. To
understand it better, we will next discuss a revision of Scheme's grammar
and new ways to write contracts.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 19.2.2}

 \label{ex:abs-sort2}
Show how to use the abstracted version of {{\tt sort}\/} from
exercise~\ref{ex:abs-sort} to sort a list of {{\tt IR}\/}s in ascending and
descending order. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/abs-sort2.html}{\rule{3pt}{5pt}}


{\bf Exercise 19.2.3}

 \label{ex:para-pair}
Here is a structure definition for pairs
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt pair}\  {\tt (left}\  {\tt right))}\end{tabbing}\end{minipage}\end{center}
and its parametric data definition: 

  \begin{quote} A {\sl {(pair {{\tt X}\/} {{\tt Y}\/})}} is a structure:\\
         \centerline{{{\tt (make-pair}\ {\tt l}\ {\tt r)}\/}}      where {{\tt l}\/} is an {{\tt X}\/} and {{\tt r}\/} is a {{\tt Y}\/}. \end{quote}

\noindent Using this abstract data definition, we can describe many
different forms of pairs:
\begin{enumerate}
\item {{\tt (pair}\ {\tt number}\ {\tt number)}\/}, which is the class of pairs that
combine two numbers;

\item {{\tt (pair}\ {\tt symbol}\ {\tt number)}\/}, which is the class of pairs that
combine a number with a symbol; and

\item {{\tt (pair}\ {\tt symbol}\ {\tt symbol)}\/}, which is the class of pairs that 
combine two symbols. 
\end{enumerate}
Still, in all of these examples, each pair contains two values that are
accessible via {{\tt pair-left}\/} and {{\tt pair-right}\/}.

By combining the abstract data definition for lists and pairs we can
describe lists of parametric pairs with a single line:\\

\centerline{ {{\tt (listof}\ {\tt (pair}\ {\tt X}\ {\tt Y))}\/} \ .}

\noindent Some concrete examples of this abstract class of data are: 
\begin{enumerate}
\item {{\tt (listof}\ {\tt (pair}\ {\tt number}\ {\tt number))}\/}, the list of pairs of numbers; 
\item {{\tt (listof}\ {\tt (pair}\ {\tt symbol}\ {\tt number))}\/}, the list of pairs of symbols
and numbers;  
\item {{\tt (listof}\ {\tt (pair}\ {\tt symbol}\ {\tt symbol))}\/}, the list of pairs of symbols. 
\end{enumerate}
Make an example for each of these classes. 

Develop the function {{\tt lefts}\/}, which consumes a list of
{{\tt (pair}\ {\tt X}\ {\tt Y)}\/} and produces a corresponding list of {{\tt X}\/}'s; that
is, it extracts the {{\tt left}\/} part of each item in its
input.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/para-pair.html}{\rule{3pt}{5pt}}


{\bf Exercise 19.2.4}

 \label{ex:para-non-empty}
Here is a parametric data definition of non-empty lists:

   \begin{quote} {A {\sl {{{\tt (non-empty-listof}\ {\tt ITEM)}\/}}} is either      \begin{enumerate}      \item {{\tt (cons}\ {\tt s}\ {\tt empty)}\/}, or      \item {{\tt (cons}\ {\tt s}\ {\tt l)}\/}         where {{\tt l}\/} is a {{\tt (non-empty-listof}\ {\tt ITEM)}\/}       \end{enumerate}}      and {{\tt s}\/} is always an {{\tt ITEM}\/}. \end{quote}

Develop the function {{\tt last}\/}, which consumes a
{{\tt (non-empty-listof}\ {\tt ITEM)}\/} and produces the last {{\tt ITEM}\/} in
that list.

\noindent{\bf Hint:} \ Replace {{\tt ITEM}\/} with a fixed class of data to develop an
initial draft of {{\tt last}\/}. When finished, replace the class with
{{\tt ITEM}\/} throughout the function
development.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/para-non-empty.html}{\rule{3pt}{5pt}}



\section{Functions are Values} \label{sec:programs-as-values} 

The functions of section~\ref{sec:similarities} stretch our understanding of
evaluation. It is easy to understand how functions consume numbers and
symbols; structures and lists are a bit more complicated, but still within
our grasp; but functions consuming functions is a strange idea. As a matter
of fact, the functions of section~\ref{sec:similarities} violate the Scheme
grammar of section~\ref{sec:synsem}. 

In this section, we discuss how to adjust Scheme's grammar and evaluation
rules so that they cover these functions. Without a good understanding of
these ideas, we cannot hope to abstract functions. Once we understand these
ideas, we can turn to the problem of writing contracts for such functions.
Finally, the last part of the section introduces functions that produce
functions, another powerful abstraction technique.

\subsection{Syntax and Semantics} \label{sec:func-as-values}

The abstract functions of section~\ref{sec:similarities} violate Scheme's
basic grammar in two ways. First, the names of functions and primitive
operations are used as arguments in applications.  An argument, though, is
an expression, and the class of expressions does not contain primitive
operations and function names. It does contain variables, but we agreed that
they are only those variables mentioned in variable definitions and as
function parameters.  Second, parameters are used as if they were functions,
that is, the first position of applications. But the grammar of
section~\ref{sec:synsem} allows only the names of functions and primitive
operations in this place. 

\hrule
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{def}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ ({\bf define} (\mbox{{\tt <{var}>}} \mbox{{\tt <{var}>}} \ldots \mbox{{\tt <{var}>}}) \mbox{{\tt <{exp}>}}) \\
	  &&  $\,\,\vert\,\,$ ({\bf define} \mbox{{\tt <{var}>}} \mbox{{\tt <{exp}>}}) \\
	  &&  $\,\,\vert\,\,$ ({\bf define-struct} \mbox{{\tt <{var}>}} (\mbox{{\tt <{var}>}} \mbox{{\tt <{var}>}} \ldots \mbox{{\tt <{var}>}})) \\
\mbox{{\tt <{exp}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ \mbox{{\tt <{var}>}} \\
    && $\,\,\vert\,\,$ \mbox{{\tt <{boo}>}} \\
    && $\,\,\vert\,\,$ \mbox{{\tt <{sym}>}} \\
    && $\,\,\vert\,\,$ \mbox{{\tt <{prm}>}} \\
    && $\,\,\vert\,\,$ {\sf empty} \\
    && $\,\,\vert\,\,$ (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}} \ldots \mbox{{\tt <{exp}>}})\\
    && $\,\,\vert\,\,$ ({\bf cond} (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}) \ldots (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}))\\
    && $\,\,\vert\,\,$ ({\bf cond} (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}) \ldots ({{\tt else}\/} \mbox{{\tt <{exp}>}}))\\
    && $\,\,\vert\,\,$ ({\bf local} (\mbox{{\tt <{def}>}} \ldots \mbox{{\tt <{def}>}}) \mbox{{\tt <{exp}>}})\\
\ \\
\mbox{{\tt <{var}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$
  {{\tt x}\/} $\,\,\vert\,\,$  {{\tt area-of-disk}\/} $\,\,\vert\,\,$ {{\tt circumference}\/} $\,\,\vert\,\,$\ldots\\
\mbox{{\tt <{boo}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$
  {{\sf true}\/} $\,\,\vert\,\,$ {{\sf false}\/} \\
\mbox{{\tt <{sym}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$
  {{\tt \char'047}{\tt a}\/} $\,\,\vert\,\,$ {{\tt \char'047}{\tt doll}\/} $\,\,\vert\,\,$ {{\tt \char'047}{\tt sum}\/} $\,\,\vert\,\,$ \ldots \\
\mbox{{\tt <{num}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$
  {{\tt 1}\/} $\,\,\vert\,\,$ {{\tt -1}\/} $\,\,\vert\,\,$ {{\tt 3/5}\/} $\,\,\vert\,\,$ 
  {{\tt 1.22}\/} $\,\,\vert\,\,$ \ldots \\
\mbox{{\tt <{prm}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$
  {{\tt +}\/} $\,\,\vert\,\,$ {{\tt -}\/} $\,\,\vert\,\,$ 
  {{\tt cons}\/} $\,\,\vert\,\,$ {{\tt first}\/} $\,\,\vert\,\,$ {{\tt rest}\/} $\,\,\vert\,\,$ \ldots
\end{tabular}
\end{center}

\center{Figure: Scheme with functions as values}\label{fig:grammar-prog}
\hrule

Spelling out the problem suggests the necessary changes. First, we should
include the names of functions and primitive operations in the definition of
\mbox{{\tt <{exp}>}}. Second, the first position in an application should allow other
things than function names and primitive operations; at a minimum, it must
allow variables that play the role of function parameters. In anticipation
of other uses of functions, we agree on allowing expressions in that
position. 

Here is a summary of the three changes:
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{exp}>}} &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ \mbox{{\tt <{var}>}} \\
    && $\,\,\vert\,\,$ \mbox{{\tt <{prm}>}} \\
    && $\,\,\vert\,\,$ (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}} \ldots \mbox{{\tt <{exp}>}})
\end{tabular}
\end{center}
Figure~\ref{fig:grammar-prog} displays the entire Scheme grammar, with all
the extensions we have encountered so far. It shows that the accommodation of
abstract functions does not lengthen the grammar, but makes it
simpler.\footnote{Computing scientists say the grammar has become ``more regular.''}

The same is true of the evaluation rules. Indeed, they don't change at
all. What changes is the set of values. To accommodate functions as
arguments of functions, the simplest change is to say that the set of values
includes the names of functions and primitive operations: 
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{val}>}} & = & 
   $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ \mbox{{\tt <{boo}>}} $\,\,\vert\,\,$ \mbox{{\tt <{sym}>}} $\,\,\vert\,\,$ \mbox{{\tt <{num}>}} $\,\,\vert\,\,$ {\sf empty} $\,\,\vert\,\,$ \mbox{{\tt <{lst}>}} \\
&& $\,\,\vert\,\,$    \mbox{{\tt <{var}>}} \mbox{(names of {{\tt define}\/}d functions)}\\
&& $\,\,\vert\,\,$    \mbox{{\tt <{prm}>}} \\
\mbox{{\tt <{lst}>}} & = & 
 {\sf empty} $\,\,\vert\,\,$ ({\sf cons} \mbox{{\tt <{val}>}} \mbox{{\tt <{lst}>}}) 
\end{tabular}
\end{center}
Put differently, if we now wish to decide whether we can apply the
substitution rule for functions, we must still ensure that all arguments are
values, but we must recognize that function names and primitive operations
count as values, too. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 20.1.1}

 \label{ex:sem-funcs}
Assume the {\tt Definitions} window in DrScheme contains {{\tt (define}{ }{\tt (f}\ {\tt x)}\ {\tt x)}\/}.  Identify the values among the following expressions:
\begin{enumerate}
\item {{\tt (cons}\ {\tt f}\ {\tt empty)}\/} 
\item {{\tt (f}\ {\tt f)}\/} 
\item {{\tt (cons}\ {\tt f}\ {\tt (cons}\ {\tt 10}\ {\tt (cons}\ {\tt (f}\ {\tt 10)}\ {\tt empty)))}\/} 
\end{enumerate}
Explain why they are values and why the remaining expressions are
not values.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sem-funcs.html}{\rule{3pt}{5pt}}


{\bf Exercise 20.1.2}

 \label{ex:syn-funcs}
Argue why the following sentences are legal definitions: 
\begin{enumerate}
\item {{\tt (define}\ {\tt (f}\ {\tt x)}\ {\tt (x}\ {\tt 10))}\/} 
\item {{\tt (define}\ {\tt (f}\ {\tt x)}\ {\tt f)}\/} 
\item {{\tt (define}\ {\tt (f}\ {\tt x}\ {\tt y)}\ {\tt (x}\ {\tt \char'047}{\tt a}\ {\tt y}\ {\tt \char'047}{\tt b))}\/} 
\end{enumerate}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/syn-funcs.html}{\rule{3pt}{5pt}}



{\bf Exercise 20.1.3}

 \label{ex:sem2-funcs}
Develop {{\tt a-function=\char'077}\/}.  The function determines whether two functions
from numbers to numbers produce the same results for {{\tt 1.2}\/},
{{\tt 3}\/}, and {{\tt -5.7}\/}.

Can we hope to produce {{\tt function=\char'077}\/}, which determines whether two
functions from numbers are equal?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sem2-funcs.html}{\rule{3pt}{5pt}}


\subsection{Contracts for Abstract and Polymorphic Functions} 
\label{sec:abs-contracts}

When we first abstracted {{\tt below}\/} and {{\tt above}\/} into
{{\tt filter1}\/}, we did not formulate a contract.  Unlike the functions we
had defined before, {{\tt filter1}\/} consumed a type of values that we
never before used as data: primitive operations and other functions. Still,
we eventually agreed in plain English writing that {{\tt filter1}\/}'s first
argument, {{\tt rel-op}\/}, would always be a function that consumes two
numbers and produces a Boolean value.

If, in the past, we had been asked to write a contract for {{\tt rel-op}\/},
we would have written 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt rel-op}\ {\tt :}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt boolean}\/}}\end{tabbing}\end{minipage}\end{center}
Considering that functions and primitive operations are values, this
contract says that an arrow symbol, {{}{\tt ->}{}\/}, describes a class of
values: functions and primitive operations.  The names on the left of
{{}{\tt ->}{}\/} specify what each value in the class of functions must be
applied to; the name on the right says what each value is going to produce
if it is applied to proper values. In general, we say that 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (A}\  {\tt B}\  {}{\tt ->}{}\  {\tt C)}\end{tabbing}\end{minipage}\end{center}
means the class of all functions and primitives that consume an element in
{{\tt A}\/} and an element in {{\tt B}\/} and produce an element in
{{\tt C}\/}. Or more succinctly, they are functions ``from {{\tt A}\/} and
{{\tt B}\/} to {{\tt C}\/}.''

The arrow notation is like the {{\tt (listof}\ {\tt ...)}\/} notation from the
previous section. Both specify a class of data via a combination of other
classes. For {{\tt listof}\/}, we used data definitions to agree on what
they mean. Others can follow the example and introduce their own
abbreviations based on data definitions.  For arrows, we just made an
agreement, and it stays with us for good.

Using the arrow notation, we can formulate a first contract and a proper
purpose statement for {{\tt filter1}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt filter1}\ {\tt :}\ {\tt (number}\ {\tt number}\ {}{\tt ->}{}\ {\tt boolean)}\ {\tt lon}\ {\tt number}\ {}{\tt ->}{}\ {\tt lon}\/} }\\
\hbox{;; to construct the list of those numbers {{\tt n}\/} on {{\tt alon}\/} for which}
\\
\hbox{;; {{\tt (rel-op}\ {\tt n}\ {\tt t)}\/} evaluates to {{\tt true}\/}}
\\
{\tt (define}\  {\tt (filter1}\  {\tt rel-op}\  {\tt alon}\  {\tt t)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
The unusual part of the contract is that it specifies the class to which
the first argument must belong not with a name introduced by a data
definition but with a direct data definition, using the arrow notation.
More concretely, it specifies that the first argument must be a function or
a primitive operation and, as discussed, what kind of arguments it consumes
and what kind of value it produces.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 20.2.1}

 \label{ex:arrows-dd}
Explain the following classes of functions: 
\begin{enumerate}
\item {{\tt (number}\ {}{\tt ->}{}\ {\tt boolean)}\/}, 
\item {{\tt (boolean}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt boolean)}\/}, 
\item {{\tt (number}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number)}\/}, 
\item {{\tt (number}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt number))}\/}, and
\item {{\tt ((listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt boolean)}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/arrows-dd.html}{\rule{3pt}{5pt}}
\end{enumerate}


{\bf Exercise 20.2.2}

 \label{ex:fancy-contracts}
Formulate contracts for the following functions: 
\begin{enumerate}
\item {{\tt sort}\/}, which consumes a list of numbers and a function that
consumes two numbers and produces a boolean; {{\tt sort}\/} produces a list
of numbers.

\item {{\tt map}\/}, which consumes a function from numbers to numbers and a
list of numbers; it also produces a list of numbers. 

\item {{\tt project}\/}, which consumes a list of lists of symbols and a
function from lists of symbols to symbols; it produces a list of
symbols.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/fancy-contracts.html}{\rule{3pt}{5pt}}
\end{enumerate}
\nopagebreak\noindent\rule{\textwidth}{1pt}


The second version of {{\tt filter1}\/} was the result of abstracting
{{\tt below}\/} and {{\tt below-ir}\/}. Its definition did not differ from
the first version, but the process of abstracting from {{\tt below-ir}\/}
clarified that {{\tt filter1}\/} could be applied to all kinds of lists, not
just lists of numbers.

To describe {all kinds of lists\/}, we use {{\tt (listof}\ {\tt X)}\/}.
Here is a first attempt at a contract for {{\tt filter1}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt filter1}\ {\tt :}\ {\tt ...}\ {\tt (listof}\ {\tt X)}\ {\tt number}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/} }\end{tabbing}\end{minipage}\end{center}
The key to using {{\tt filter1}\/} with different classes of lists is to use
a comparison function that can compare the items on the list with the
second argument, which is a number. That is, the first argument is a
function in the class
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (X}\  {\tt number}\  {}{\tt ->}{}\  {\tt boolean)}\end{tabbing}\end{minipage}\end{center}
which means it consumes an element of {{\tt X}\/} and a number, and
produces a boolean. Put together, we get the following contract for
{{\tt filter1}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt filter1}\ {\tt :}\ {\tt (X}\ {\tt number}\ {}{\tt ->}{}\ {\tt boolean)}\ {\tt (listof}\ {\tt X)}\ {\tt number}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/} }\end{tabbing}\end{minipage}\end{center}

As in our contract for {{\tt length}\/}, {{\tt X}\/} here stands for an
arbitrary collection of Scheme data. We can replace it with anything, as
long as all three occurrences are replaced by the same thing. Hence, by
using {{\tt X}\/} in both the description of the first parameter, the
second one, and the result, we ensure that {{\tt rel-op}\/} consumes
elements of {{\tt X}\/}, which are the items on the second argument and
that furthermore the result of {{\tt filter1}\/} is a list that contains
{{\tt X}\/}s.

When we wish to apply {{\tt filter1}\/}, we must check that the arguments
make sense.  Consider we wish to evaluate
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (filter1}\  {\tt <}\  {\tt (list}\  {\tt 3}\  {\tt 8}\  {\tt 10)}\  {\tt 2)}\end{tabbing}\end{minipage}\end{center}
Before we do that, we should confirm that {{\tt filter1}\/} can indeed
consume {{\tt <}\/} and {{\tt (list}\ {\tt 3}\ {\tt 8}\ {\tt 10)}\/}, given its contract. A quick
check shows that {{\tt <}\/} makes sense because it belongs to the class 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (number}\  {\tt number}\  {}{\tt ->}{}\  {\tt boolean)}\end{tabbing}\end{minipage}\end{center}
and {{\tt (list}\ {\tt 3}\ {\tt 8}\ {\tt 10)}\/} makes sense because it belongs to 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (listof}\  {\tt number)}\end{tabbing}\end{minipage}\end{center}
The two classes are identical to the first two argument parts of
{{\tt filter1}\/}'s contract if {{\tt X}\/} is replaced by
{{\tt number}\/}. More generally, to ensure that arguments make sense, we
must find replacements of the variables in contracts so that the functions
contract and the classes of the arguments match.

For a second example, consider 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (filter1}\  $<_{ir}$\  {\tt LOIR}\  {\tt 10)}\end{tabbing}\end{minipage}\end{center}
Here, we must replace {{\tt X}\/} with {{\tt IR}\/}, because {$<_{ir}$\/}
has the contract 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (IR}\  {\tt number}\  {}{\tt ->}{}\  {\tt boolean)}\end{tabbing}\end{minipage}\end{center}
and {{\tt LOIR}\/} belongs to {{\tt (listof}\ {\tt IR)}\/}. Again, the application
is legitimate because all the arguments belong to the required collections
of data.

Let us look at one more example: the use of {{\tt filter1}\/} to extract all
toys with the same name from a list of inventory records:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt find}\ {\tt :}\ {\tt (listof}\ {\tt IR)}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt IR)}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (find}\  {\tt aloir}\  {\tt t)}
\\
\>{\tt (filter1}\  {\tt eq-ir\char'077}\  {\tt aloir}\  {\tt t))}
\\
\hbox{;; {{\tt eq-ir\char'077}\ {\tt :}\ {\tt IR}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt boolean}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (eq-ir\char'077}\  {\tt ir}\  {\tt p)}
\\
\>{\tt (symbol=?}\  {\tt (ir-name}\  {\tt ir)}\  {\tt p))}
\end{tabbing}\end{minipage}\end{center}
It is straightforward to check with examples that the function works
properly. Our task here is to understand how this agrees with
{{\tt filter1}\/}'s contract. The obvious problem is that the ``threshold''
argument is a symbol, not a number. The use of {{\tt filter1}\/} is
therefore in conflict with its current contract. To overcome this
deficiency, we must introduce another variable, say {{\tt TH}\/} for
thresholds, that stands for some collection of data:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt filter1}\ {\tt :}\ {\tt (X}\ {\tt TH}\ {}{\tt ->}{}\ {\tt boolean)}\ {\tt (listof}\ {\tt X)}\ {\tt TH}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/} }\end{tabbing}\end{minipage}\end{center}

Now we can replace {{\tt X}\/} with the name of one data collection and
{{\tt TH}\/} with that of a second one or possibly the same. In particular,
the application 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (filter1}\  {\tt eq-ir\char'077}\  {\tt LOIR}\  {\tt \char'047}{\tt doll)}\end{tabbing}\end{minipage}\end{center}
works because a replacement of {{\tt X}\/} by {{\tt IR}\/} and {{\tt TH}\/} by
symbol in {{\tt filter1}\/}'s contract shows that the arguments are
legitimate. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 20.2.3}

 \label{ex:filter-contract}
Use {{\tt filter1}\/} to develop a function that consumes a list of symbols
and extracts all those that are { not\/} equal to {{\tt \char'047}{\tt car}\/}. Give
{{\tt filter1}\/}'s corresponding contract.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/filter-contract.html}{\rule{3pt}{5pt}}


{\bf Exercise 20.2.4}

 \label{ex:fancy-contracts-poly}
Formulate { general\/} contracts for the following functions: 
\begin{enumerate}
\item {{\tt sort}\/}, which consumes a list and a function that consumes two
items from the list and produces a boolean; it produces a list of numbers.

\item {{\tt map}\/}, which consumes a function from list items to {{\tt X}\/}s and a
list; it produces a list of {{\tt X}\/}s.

\item {{\tt project}\/}, which consumes a list of lists and a function from
lists to {{\tt X}\/}s; it produces a list of {{\tt X}\/}s.
\end{enumerate}
Compare with
exercise~\ref{ex:fancy-contracts}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/fancy-contracts-poly.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt} 


\medskip\noindent{\bf Contracts and Types}:\  In summary, the contracts for functions are made
up of types. A {\sc {type}} is either
\begin{enumerate}
\item a basic type such as number, symbol, boolean, or {{\tt empty}\/};

\item a defined type such as {{\tt inventory-record}\/}, 
{{\tt list-of-numbers}\/}, or {{\tt family-tree}\/}; 

\item a function type such as {{\tt (number}\ {}{\tt ->}{}\ {\tt number)}\/} or
{{\tt (boolean}\ {}{\tt ->}{}\ {\tt symbol)}\/}; 

\item a parametric type, which is either a defined type or a function type
with type variables.
\end{enumerate}
When we wish to use a function with a parametric type, we must first find a
replacement for all the variables in the function's contract so that we
know the arguments belong to proper classes. If this cannot be done, we
must either revise the contract or question our decision to reuse this
function.~{\rule{3pt}{5pt}}


\section{Designing Abstractions from Examples}
\label{sec:design-abstract} 
When we first learn to add, we use concrete examples. Later on, we study
how to add two arbitrary numbers; that is, we form an abstraction of the
addition operation. Much later still, we learn to formulate abstractions
directly as expressions: expressions that compute the wage of some
employee, expressions that convert temperatures, or expressions
that determine the area of a geometric shape. In short, we initially go from
concrete examples to abstraction but eventually we learn to form
abstractions directly without thinking (much) about concrete instances.

In this section, we discuss a design recipe for creating abstractions from
concrete examples. Later, in sections~\ref{sec:apriori-abs}
and~\ref{sec:design-abstract-1st} we study additional approaches to
function abstraction.

\subsection{Abstracting from Examples} \label{sec:post-abs}

Forming abstractions from examples is easy. As we have seen in
section~\ref{sec:similarities}, we start from two concrete function
definitions, compare them, mark the differences, and abstract. Let us
formulate these steps as a recipe:
\begin{description}
\item[The comparison:] When we find two function definitions that are
almost the same except at a few places and for their names, we compare them
and mark the differences with boxes. If the boxes contain only values, we
can abstract.

\medskip\noindent{\bf Warning: Abstracting over Non-values}:\  {The recipe requires a substantial modification for non-values.}~{\rule{3pt}{5pt}}

Here is a pair of similar function definitions: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt convertCF}\ {\tt :}\ {\tt lon}\ {}{\tt ->}{}\ {\tt lon}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (convertCF}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ons}\  {\tt (}\fbox{{\mbox{\it C\/}{{}{\tt ->}{}}\mbox{\it F\/}}}\  {\tt (first}\  {\tt alon))}
\\
\>\>\>\>{\tt (convertCF}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt names}\ {\tt :}\ {\tt loIR}\ {}{\tt ->}{}\ {\tt los}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (names}\  {\tt aloIR)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt aloIR)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ons}\  {\tt (}\fbox{{\it IR-name}}\  {\tt (first}\  {\tt aloIR))}
\\
\>\>\>\>{\tt (names}\  {\tt (rest}\  {\tt aloIR)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\end{center}
The two functions apply a function to each item in a list. They differ in
only one aspect: what they apply to each item on the list. The two boxes
emphasize the difference. Each contains a functional value, so we can
abstract. 

\item[The abstraction:] Next we replace the contents of corresponding pairs
of boxes with new names and add these names to the parameter list. For
example, if there are three pairs of boxes, we need three new names.  The
two definitions must now be the same, except for the function name. To
obtain the abstraction, we systematically replace the function names with
one new name.

For our running example, we obtain the following pair of functions: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (convertCF}\  {\tt f}\  {\tt alon)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ons}\  {\tt (}\fbox{{\it f}}\  {\tt (first}\  {\tt alon))}
\\
\>\>\>\>{\tt (convertCF}\  {\tt f}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (names}\  {\tt f}\  {\tt aloIR)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt aloIR)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ons}\  {\tt (}\fbox{{\it f}}\  {\tt (first}\  {\tt aloIR))}
\\
\>\>\>\>{\tt (names}\  {\tt f}\  {\tt (rest}\  {\tt aloIR)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\end{center}
We have replaced the boxed names with {{\tt f}\/} and added {{\tt f}\/} as a
parameter. Now we replace {{\tt convertCF}\/} and {{\tt names}\/} with a new
name and thus obtain the abstract function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (map}\  {\tt f}\  {\tt lon)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt lon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ons}\  {\tt (f}\  {\tt (first}\  {\tt lon))}
\\
\>\>\>{\tt (map}\  {\tt f}\  {\tt (rest}\  {\tt lon)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
We use the name {{\tt map}\/} for the result in our running example, because
it is the traditional name in programming languages for this specific
function. 

\item[The test:] Now we must validate that the new function is a correct
abstraction of the original concrete functions. The very definition of
abstraction suggests that we define the original functions in terms of the
abstract one and test the new versions with the original examples. 

In most cases, defining the original function based on the abstract one is
straightforward. Suppose the abstract function is called {{\tt f-abstract}\/}
and furthermore suppose that one original function is called
{{\tt f-original}\/} and consumes one argument. If {{\tt f-original}\/}
differs from the other concrete function in the use of one value, say
{{\tt boxed-value}\/}, then we define the following function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (f-from-abstract}\  {\tt x)}\\
\>{\tt (f-abstract}\  {\tt boxed-value}\  {\tt x))}
\end{tabbing}\end{minipage}\end{center}
For every proper value {{\tt V}\/}, {{\tt (f-from-abstract}\ {\tt V)}\/} now
produces the same answer as {{\tt (f-original}\ {\tt V)}\/}. 

Let us return to our example. Here are the two new definitions:
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt convertCF-from-map}\ {\tt :}\ {\tt lon}\ {}{\tt ->}{}\ {\tt lon}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (convertCF-from-map}\  {\tt alon)}
\\
\>{\tt (map}\  \mbox{\it C\/}{{}{\tt ->}{}}\mbox{\it F\/}\  {\tt alon))}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt names-from-map}\ {\tt :}\ {\tt loIR}\ {}{\tt ->}{}\ {\tt los}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (names-from-map}\  {\tt aloIR)}
\\
\>{\tt (map}\  {\tt IR-name}\  {\tt aloIR))}
\end{tabbing}\end{minipage}
\end{center}
To ensure that these two definitions are equivalent to the old one and,
indirectly, that {{\tt map}\/} is a correct abstraction, we now apply these
two functions to the examples that we specified for the development of 
{{\tt convertCF}\/} and {{\tt names}\/}. 

\item[The contract:] To make the abstraction truly useful, we must also
formulate a contract. If the boxed values in stage~2 of our recipe are
functions, a contract requires the use of arrow types. Furthermore, to
obtain a widely usable contract, we may have to develop or use parametric
data definitions and formulate a parametric type.

A case in point is the contract for {{\tt map}\/}. On one hand, if we view
{{\tt map}\/} as an abstraction of {{\tt convertCF}\/}, the contract could be
construed as
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt map}\ {\tt :}\ {\tt (number}\ {}{\tt ->}{}\ {\tt number)}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt number)}\/}}\end{tabbing}\end{minipage}\end{center}
On the other hand, if we view {{\tt map}\/} as an abstraction of
 {{\tt names}\/}, the contract could be construed as
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt map}\ {\tt :}\ {\tt (IR}\ {}{\tt ->}{}\ {\tt symbol)}\ {\tt (listof}\ {\tt IR)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt symbol)}\/}}\end{tabbing}\end{minipage}\end{center}
But the first contract would be useless in the second case, and {\it vice versa\/}. To accommodate both cases, we must understand what {{\tt map}\/}
does and then fix a contract. 

By looking at the definition, we can see that map applies its first
argument, a function, to every item on the second argument, a list. This
implies that the function must consume the class of data that the list
contains. That is, we know {{\tt f}\/} has the contract 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt f}\ {\tt :}\ {\tt X}\ {}{\tt ->}{}\ {\tt \char'077}{\tt \char'077}{\tt \char'077}\/}}\end{tabbing}\end{minipage}\end{center}
if {{\tt lon}\/} contains {{\tt X}\/}s. Furthermore, {{\tt map}\/} creates a
list from the results of applying {{\tt f}\/} to each item. Thus, if
{{\tt f}\/} produces {{\tt Y}\/}s, then {{\tt map}\/} produces a list of
{{\tt Y}\/}s. Translated into our language of contracts we get this: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt map}\ {\tt :}\ {\tt (X}\ {}{\tt ->}{}\ {\tt Y)}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt Y)}\/}}\end{tabbing}\end{minipage}\end{center}
This contract says that {{\tt map}\/} can produce a list of {{\tt Y}\/}s from
a list of {{\tt X}\/}s and a function from {{\tt X}\/} to {{\tt Y}\/}---no
matter for what collection of {{\tt X}\/} and {{\tt Y}\/} stand.  
\end{description}

Once we have abstracted two (or more) functions, we should check whether
there are other uses for the abstract function. In many cases, an abstract
function is useful in a much broader array of contexts than we first
anticipate and makes functions easier to read, understand, and maintain.
For example, we can now use {{\tt map}\/} every time we need a function to
produce a new list by processing all items on an existing list. If that
function is a primitive operation or a function we have defined, we don't
even write a function. Instead, we simply write an expression that performs
the task. Unfortunately, there is no recipe that guides this discovery
process.  We must practice it and develop an eye for matching abstract
functions to situations.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 21.1.1}

 \label{ex:abs-tabulate}
Define {{\tt tabulate}\/}, which is the abstraction of the following two
functions:  
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt tabulate-sin}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt lon}\/}}\\
\hbox{;; to tabulate {{\tt sin}\/} between {{\tt n}\/} }
\\
\hbox{;; and {{\tt 0}\/} (inclusive) in a list}
\\
{\tt (d}\={\tt efine}\  {\tt (tabulate-sin}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt 0)}\  {\tt (list}\  {\tt (sin}\  {\tt 0))\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ons}\  {\tt (sin}\  {\tt n)}
\\
\>\>\>\>{\tt (tabulate-sin}\  {\tt (sub1}\  {\tt n)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt tabulate-sqrt}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt lon}\/}}\\
\hbox{;; to tabulate {{\tt sqrt}\/} between {{\tt n}\/} }
\\
\hbox{;; and {{\tt 0}\/} (inclusive) in a list}
\\
{\tt (d}\={\tt efine}\  {\tt (tabulate-sqrt}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt 0)}\  {\tt (list}\  {\tt (sqrt}\  {\tt 0))\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ons}\  {\tt (sqrt}\  {\tt n)}
\\
\>\>\>\>{\tt (tabulate-sqrt}\  {\tt (sub1}\  {\tt n)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\end{center}
Make sure to define the two functions in terms of {{\tt tabulate}\/}. 
Also use {{\tt tabulate}\/} to define a tabulation function for
{{\tt square}\/} and {{\tt tan}\/}. What would be a good, general contract?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/abs-tabulate.html}{\rule{3pt}{5pt}}


{\bf Exercise 21.1.2}

 \label{ex:abs-sum-prod}
Define {{\tt fold}\/}, which is the abstraction of the following two functions: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sum}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the sum of }
\\
\hbox{;; the numbers on {{\tt alon}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (sum}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  \={\tt (first}\  {\tt alon)}
\\
\>\>\>{\tt (sum}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt product}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the product of }
\\
\hbox{;; the numbers on {{\tt alon}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (product}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt 1\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (*}\  \={\tt (first}\  {\tt alon)}
\\
\>\>\>{\tt (product}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\end{center}
Don't forget to test {{\tt fold}\/}. 

After {{\tt fold}\/} is defined and tested, use it to define
{{\tt append}\/}, which juxtaposes the items of two lists or, 
equivalently, replaces {{\tt empty}\/} in the first list with 
the second list: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (append}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3)}\  {\tt (list}\  {\tt 4}\  {\tt 5}\  {\tt 6}\  {\tt 7}\  {\tt 8))}\\
{\tt =}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5}\  {\tt 6}\  {\tt 7}\  {\tt 8)}
\end{tabbing}\end{minipage}\end{center}
Finally, define {{\tt map}\/} using {{\tt fold}\/}. 

\noindent Compare the four examples to formulate a
contract.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/abs-sum-prod.html}{\rule{3pt}{5pt}}


{\bf Exercise 21.1.3}

 \label{ex:N-fold}
Define {{\tt natural-f}\/}, which is the abstraction of the following two
functions:  
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt copy}\ {\tt :}\ {\bf N}\ {\tt X}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/}}\\
\hbox{;; to create a list that contains}
\\
\hbox{;; {{\tt obj}\/} {{\tt n}\/} times}
\\
{\tt (d}\={\tt efine}\  {\tt (copy}\  {\tt n}\  {\tt obj)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  \={\tt obj}\  
\\
\>\>\>{\tt (copy}\  {\tt (sub1}\  {\tt n)}\  {\tt obj))\char'135}{\tt ))}
\end{tabbing}\end{minipage}~\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt n-adder}\ {\tt :}\ {\bf N}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to add {{\tt n}\/} to {{\tt x}\/} using}
\\
\hbox{;; {{\tt (+}\ {\tt 1}\ {\tt ...)}\/} only}
\\
{\tt (d}\={\tt efine}\  {\tt (n-adder}\  {\tt n}\  {\tt x)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt x\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  \={\tt 1}
\\
\>\>\>{\tt (n-adder}\  {\tt (sub1}\  {\tt n)}\  {\tt x))\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\end{center}
Don't forget to test {{\tt natural-n}\/}.  Also use {{\tt natural-n}\/} to
define {{\tt n-multiplier}\/}, which consumes {{\tt n}\/} and {{\tt x}\/} and
produces {{\tt n}\/} times {{\tt x}\/} with additions only.  Use the examples
to formulate a contract.

\noindent{\bf Hint:} \ The two function differ more than, say, the functions {{\tt sum}\/}
and {{\tt product}\/} in exercise~\ref{ex:abs-sum-prod}. In particular,
the base case in one instance is a argument of the function, in the other
it is just a constant value. 
~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/N-fold.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt} 


\medskip\noindent{\bf Formulating General Contracts}:\  To increase the usefulness of an
abstract function, we must formulate a contract that describes its
applicability in the most general terms possible. In principle, abstracting
contracts follows the same recipe that we use for abstracting functions. We
compare and contrast the old contracts; then we replace the differences by
variables. But the process is complicated and requires a lot of practice. 

Let us start with our running example: {{\tt convertCF}\/} and {{\tt names}\/}:
\begin{htmlonly}
\begin{rawhtml}
<NOcenter>
<table>
<tr><td><tt>(listof number)</tt></td><td><tt>-></tt></td><td><tt>(listof number)</tt></td></tr>
<tr><td><tt>(listof IR)</tt>    </td><td><tt>-></tt></td><td><tt>(listof symbol)</tt></td></tr>
</table>
</center>
\end{rawhtml}
\end{htmlonly}
\relax
Comparing the two contracts shows that they differ in two places. To the
left of {{}{\tt ->}{}\/}, we have {{\tt number}\/} and {{\tt IR}\/}; to the right,
it is {{\tt number}\/} versus {{\tt symbol}\/}.

Consider the second stage of our abstraction recipe. The most natural
contracts are as follows: 
\begin{htmlonly}
\begin{rawhtml}
<NOcenter>
<table>
<tr><td><tt>(number -> number)</tt></td>
    <td><tt>(listof number)</tt></td><td><tt>-></tt></td><td><tt>(listof number)</tt></td></tr>
<tr><td><tt>(IR -> symbol)</tt></td>
    <td><tt>(listof IR)</tt>    </td><td><tt>-></tt></td><td><tt>(listof symbol)</tt></td></tr>
</table>
</center>
\end{rawhtml}
\end{htmlonly}
\relax
These new contracts suggest a pattern. Specifically, they suggest that the
first argument, a function, consumes the items on the second argument, a
list, and furthermore, that the results produced by these applications make
up the output. The second contract is particularly telling. If we replace
{{\tt IR}\/} and {{\tt symbol}\/} with variables, we get an abstract
contract, and it is indeed a contract for {{\tt map}\/}:
\begin{center}
{{\tt map}\ {\tt :}\ {\tt (X}\ {}{\tt ->}{}\ {\tt Y)}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt Y)}\/}
\end{center}
It is straightforward to check that by replacing {{\tt X}\/} with
{{\tt number}\/} and {{\tt Y}\/} with {{\tt number}\/}, we get the first
of the intermediate contracts. 

Here is a second pair of examples:
\begin{htmlonly}
\begin{rawhtml}
<NOcenter>
<table>
<tr><td><tt>number</tt></td>
    <td><tt>(listof number)</tt></td><td><tt>-></tt></td><td><tt>(listof number)</tt></td></tr>
<tr><td><tt>number</tt></td>
    <td><tt>(listof IR)</tt>    </td><td><tt>-></tt></td><td><tt>(listof IR)</tt></td></tr>
</table>
</center>
\end{rawhtml}
\end{htmlonly}
\relax
They are the contracts for {{\tt below}\/} and {{\tt below-ir}\/}. The
contracts differ in two places: the lists consumed and produced.  As usual,
the functions of the second stage consume an additional argument:
\begin{htmlonly}
\begin{rawhtml}
<NOcenter>
<table>
<tr><td><tt>(number number -> boolean)</tt></td>
    <td><tt>number</tt></td>
    <td><tt>(listof number)</tt></td><td><tt>-></tt></td><td><tt>(listof number)</tt></td></tr>
<tr><td><tt>(number IR -> boolean)</tt></td>
    <td><tt>number</tt></td>
    <td><tt>(listof IR)</tt>    </td><td><tt>-></tt></td><td><tt>(listof IR)</tt></td></tr>
</table>
</center>
\end{rawhtml}
\end{htmlonly}
\relax
The new argument is a function, which in the first case consumes a number,
and in the second case an {{\tt IR}\/}.

A comparison of the two contracts suggests that {{\tt number}\/} and
{{\tt IR}\/} occupy related positions and that we should replace them with a
variable. Doing so makes the two contracts equal: 
\begin{htmlonly}
\begin{rawhtml}
<NOcenter>
<table>
<tr><td><tt>(number X -> boolean)</tt></td>
    <td><tt>number</tt></td>
    <td><tt>(listof X)</tt></td><td><tt>-></tt></td><td><tt>(listof X)</tt></td></tr>
<tr><td><tt>(number X -> boolean)</tt></td>
    <td><tt>number</tt></td>
    <td><tt>(listof X)</tt></td><td><tt>-></tt></td><td><tt>(listof X)</tt></td></tr>
</table>
</center>
\end{rawhtml}
\end{htmlonly}
\relax
A closer inspection of {{\tt filter1}\/}'s definition shows that we can also
replace {{\tt number}\/} with {{\tt Y}\/} because the second argument is
always just the first argument of {{\tt filter1}\/}'s first argument. 

Here is the new contract: 
\begin{center}
{{\tt filter1}\ {\tt :}\ {\tt (Y}\ {\tt X}\ {}{\tt ->}{}\ {\tt boolean)}\ {\tt Y}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/}
\end{center}
The result of the first argument must be {{\tt boolean}\/}, because it is
used as a condition. Hence, we have found the most general contract
possible.

The two examples illustrate how to find general contracts. We compare the
contracts of the examples from which we create abstractions. By replacing
specific, distinct classes in corresponding positions, one at a time, we
make the contract gradually more general. To ensure that our generalized
contract works, we check that the contract describes the specific instances
of the abstracted function properly.~{\rule{3pt}{5pt}}

\subsection{Finger Exercises with Abstract List Functions}

Scheme provides a number of abstract functions for processing lists. 
Figure~\ref{fig:scheme-ho-list} collects the specification of the
most important ones. Using these functions greatly simplifies many 
programming tasks and helps readers understand programs quickly. The
following exercises provide an opportunity to get acquainted with these 
functions. 

\hrule 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt build-list}\ {\tt :}\ {\bf N}\ {\tt (}{\bf N}\ {}{\tt ->}{}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/}}\\
\hbox{;; to construct {{\tt (list}\ {\tt (f}\ {\tt 0)}\ {\tt ...}\ {\tt (f}\ {\tt (-}\ {\tt n}\ {\tt 1)))}\/}}
\\
{\tt (define}\  {\tt (build-list}\  {\tt n}\  {\tt f)}\  {\tt ...)}
\\
\hbox{;; {{\tt filter}\ {\tt :}\ {\tt (X}\ {}{\tt ->}{}\ {\tt boolean)}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/}}
\\
\hbox{;; to construct a list from all those items on {{\tt alox}\/} for which {{\tt p}\/} holds }
\\
{\tt (define}\  {\tt (filter}\  {\tt p}\  {\tt alox)}\  {\tt ...)}
\\
\hbox{;; {{\tt sort}\ {\tt :}\ {\tt (X}\ {\tt X}\ {}{\tt ->}{}\ {\tt boolean)}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/}}
\\
\hbox{;; to construct a list from all items on {{\tt alox}\/} in an order according to {{\tt cmp}\/}}
\\
{\tt (define}\  {\tt (sort}\  {\tt cmp}\  {\tt alox)}\  {\tt ...)}
\\
\hbox{;; {{\tt map}\ {\tt :}\ {\tt (X}\ {}{\tt ->}{}\ {\tt Y)}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt Y)}\/}}
\\
\hbox{;; to construct a list by applying {{\tt f}\/} to each item on {{\tt alox}\/}}
\\
\hbox{;; that is, {{\tt (map}\ {\tt f}\ {\tt (list}\ {\tt x-1}\ {\tt ...}\ {\tt x-n))}\/} = {{\tt (list}\ {\tt (f}\ {\tt x-1)}\ {\tt ...}\ {\tt (f}\ {\tt x-n))}\/}}
\\
{\tt (define}\  {\tt (map}\  {\tt f}\  {\tt alox)}\  {\tt ...)}
\\
\hbox{;; {{\tt andmap}\ {\tt :}\ {\tt (X}\ {}{\tt ->}{}\ {\tt boolean)}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt boolean}\/}}
\\
\hbox{;; to determine whether {{\tt p}\/} holds for every item on {{\tt alox}\/}}
\\
\hbox{;; that is, {{\tt (andmap}\ {\tt p}\ {\tt (list}\ {\tt x-1}\ {\tt ...}\ {\tt x-n))}\/} = {{\tt (and}\ {\tt (p}\ {\tt x-1)}\ {\tt (and}\ {\tt ...}\ {\tt (p}\ {\tt x-n)))}\/}}
\\
{\tt (define}\  {\tt (andmap}\  {\tt p}\  {\tt alox)}\  {\tt ...)}
\\
\hbox{;; {{\tt ormap}\ {\tt :}\ {\tt (X}\ {}{\tt ->}{}\ {\tt boolean)}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt boolean}\/}}
\\
\hbox{;; to determine whether {{\tt p}\/} holds for at least one item on {{\tt alox}\/}}
\\
\hbox{;; that is, {{\tt (ormap}\ {\tt p}\ {\tt (list}\ {\tt x-1}\ {\tt ...}\ {\tt x-n))}\/} = {{\tt (or}\ {\tt (p}\ {\tt x-1)}\ {\tt (or}\ {\tt ...}\ {\tt (p}\ {\tt x-n)))}\/}}
\\
{\tt (define}\  {\tt (ormap}\  {\tt p}\  {\tt alox)}\  {\tt ...)}
\\
\hbox{;; {{\tt foldr}\ {\tt :}\ {\tt (X}\ {\tt Y}\ {}{\tt ->}{}\ {\tt Y)}\ {\tt Y}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt Y}\/}}
\\
\hbox{;; {{\tt (foldr}\ {\tt f}\ {\tt base}\ {\tt (list}\ {\tt x-1}\ {\tt ...}\ {\tt x-n))}\ {\tt =}\ {\tt (f}\ {\tt x-1}\ {\tt ...}\ {\tt (f}\ {\tt x-n}\ {\tt base))}\/} }
\\
{\tt (define}\  {\tt (foldr}\  {\tt f}\  {\tt base}\  {\tt alox)}\  {\tt ...)}
\\
\hbox{;; {{\tt foldl}\ {\tt :}\ {\tt (X}\ {\tt Y}\ {}{\tt ->}{}\ {\tt Y)}\ {\tt Y}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt Y}\/}}
\\
\hbox{;; {{\tt (foldl}\ {\tt f}\ {\tt base}\ {\tt (list}\ {\tt x-1}\ {\tt ...}\ {\tt x-n))}\ {\tt =}\ {\tt (f}\ {\tt x-n}\ {\tt ...}\ {\tt (f}\ {\tt x-1}\ {\tt base))}\/} }
\\
{\tt (define}\  {\tt (foldl}\  {\tt f}\  {\tt base}\  {\tt alox)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Scheme's built-in abstract functions for list-processing}
\label{fig:scheme-ho-list}
\hrule 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 21.2.1}

 \label{ex:build-list1}
Use {{\tt build-list}\/} 
\begin{enumerate}
\item to create the lists {{\tt (list}\ {\tt 0}\ {\tt ...}\ {\tt 3)}\/} and {{\tt (list}\ {\tt 1}\ {\tt ...}\ {\tt 4)}\/};
\item to create the list {{\tt (list}\ {\tt .1}\ {\tt .01}\ {\tt .001}\ {\tt .0001)}\/};
\item to define {{\tt evens}\/}, which consumes a natural numbers {{\tt n}\/}
and creates the list of the first {{\tt n}\/} even numbers; 
\item to define {{\tt tabulate}\/} from exercise~\ref{ex:abs-tabulate}; and
\item to define {{\tt diagonal}\/}, which consumes a natural number {{\tt n}\/}
and creates a list of lists of {{\tt 0}\/} and {{\tt 1}\/}. \\
Example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (diagonal}\  {\tt 3)}\\
{\tt =}\  {\tt (l}\={\tt ist}\  
\\
\>{\tt (list}\  {\tt 1}\  {\tt 0}\  {\tt 0)}
\\
\>{\tt (list}\  {\tt 0}\  {\tt 1}\  {\tt 0)}
\\
\>{\tt (list}\  {\tt 0}\  {\tt 0}\  {\tt 1))}
\end{tabbing}\end{minipage}\end{center}
\end{enumerate}
Use local if function definitions require auxiliary functions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/build-list1.html}{\rule{3pt}{5pt}}


{\bf Exercise 21.2.2}

 \label{ex:use-map}
Use {{\tt map}\/} to define the following functions:
\begin{enumerate}
\item {{\tt convertDM}\/}, which converts a list of US Dollar amounts into a
list of Deutsche Mark amounts based on an exchange rate of 1.82 marks for
each dollar;

\item {{\tt convertFC}\/}, which converts a list of Fahrenheit measurements
to a list of Celsius measurements; 

\item {{\tt move-all}\/}, which consumes a list of {{\tt posn}\/} structures and
translates each by adding {{\tt 3}\/} to the $x$-component.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/use-map.html}{\rule{3pt}{5pt}}
\end{enumerate}


{\bf Exercise 21.2.3}

 \label{ex:use-filter}
Here is the version of {{\tt filter}\/} that DrScheme provides: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
\hbox{;; {{\tt filter}\ {\tt :}\ {\tt (X}\ {}{\tt ->}{}\ {\tt boolean)}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/}}
\\
\hbox{;; to construct a list of {{\tt X}\/} from all those items on {{\tt alon}\/}}
\\
\hbox{;; for which {{\tt predicate\char'077}\/} holds }
\\
{\tt (d}\={\tt efine}\  {\tt (filter}\  {\tt predicate\char'077}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (predicate\char'077}\  {\tt (first}\  {\tt alon))}\  
\\
\>\>\>\>{\tt (cons}\  {\tt (first}\  {\tt alon)}\  {\tt (filter}\  {\tt predicate\char'077}\  {\tt (rest}\  {\tt alon)))\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (filter}\  {\tt predicate\char'077}\  {\tt (rest}\  {\tt alon))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

Use {{\tt filter}\/} to define the following functions:
\begin{enumerate}
\item {{\tt eliminate-exp}\/}, which consumes a number, {{\tt ua}\/}, and a
list of toy structures (containing name and price) and produces a list of
all those descriptions whose price is below {{\tt ua}\/};

\item {{\tt recall}\/}, which consumes the name of a toy, called
{{\tt ty}\/}, and a list of names, called {{\tt lon}\/}, and produces a list
of names that contains all components of {{\tt lon}\/} with the exception of
{{\tt ty}\/}; 

\item {{\tt selection}\/}, which consumes two lists of names and selects all
those from the second one that are also on the first.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/use-filter.html}{\rule{3pt}{5pt}}
\end{enumerate}


\subsection{Abstraction and a Single Point of Control}

Just like editing papers, abstracting programs has many
advantages. Creating an abstraction often simplifies other definitions. The
process of abstracting may uncover problems with existing functions. But,
the single-most important advantage of abstraction is that it creates a
{\sc {single point of control}} for the functionality in a program. In
other words, it puts the definitions (as much as possible) related to some
specific task in one place.

Putting the definitions for a specific task in one place makes it easier to
maintain a program. Roughly put, program maintenance means fixing the
program so that it functions properly in previously untested cases;
extending the program so that it can deal with new or unforeseen
situations; or changing the representation of some information as data (for
example, calendar dates). With everything in one place, fixing an error
means fixing it in one function, not four or five similar
versions. Extending a function's capabilities, means fixing one function,
not its related copies. And, changing a data representation means changing
a general data-traversal function, not all those that came from the same
template. Translated into a guideline, we say: 

   \begin{rawhtml}     <table bgcolor=red align=center>       <tr><td><font align=center color=white size=+3>   \end{rawhtml} Guideline on Creating Abstractions   \begin{rawhtml}     <tr><td><font color=white><p>   \end{rawhtml} Form an abstraction instead of copying and modifying a piece of a program (for example, function, data definition).    \begin{rawhtml}     </p></table>   \end{rawhtml} 

Experience teaches us that maintaining software is expensive. Programmers
can reduce the maintenance cost by organizing programs correctly. The
first principle of function organization is to match the function's
structure to the structure of its input data. If every programmer
follows this rule, it is easy to modify and extend functions when the
set of possible input data changes. The second principle is to
introduce proper abstractions. Every abstracted function creates a
single point of control for at least two different functions, often for
several more.  After we have abstracted, we often find more uses of the
new function. 

Our design recipe for abstracting functions is the most basic tool to
create abstractions. To use it requires practice. As we practice, we expand
our capabilities for building and using abstractions. The best programmers
are those who actively edit their program to build new abstractions so that
they collect things related to a task at a single point. Here we use
functional abstraction to study this practice. While not all languages
provide the freedom to abstract functions as easily as Scheme, modern
languages often support similar concepts and practicing in powerful
languages like Scheme is the best possible perparation. \footnote{A currently popular method of abstraction is {\sl inheritance\/} in class-based object-oriented programming languages. Inheritance is quite similar to functional abstraction, though it emphasizes those aspects that change over those that stay the same.}

\subsection{Extended Exercise: Moving Pictures, Again} \label{sec:abs-moving} 

In sections~\ref{sec:move-circle}, \ref{sec:move-shape},
and~\ref{sec:move-fig}, we studied the problem of moving pictures across a
canvas. The problem had two parts: moving individual shapes and moving a
picture, which is a list of shapes. For the first part, we need functions
to draw, clear, and translate a shape. For the second part, we need
functions that draw all shapes on a list, that clear all shapes on a list,
and that translate all shapes on a list. Even the most cursory look at the
functions shows many repetitions. The following exercises aim to eliminate
these repetitions via manual abstraction and Scheme's built-in operations.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 21.4.1}

 \label{ex:moving-circle-abs} 
Abstract the functions {{\tt draw-circle}\/} and {{\tt clear-circle}\/} into a
single function {{\tt process-circle}\/}. 

Define {{\tt translate-circle}\/} using {{\tt process-shape}\/}.  \noindent{\bf Hint:} \ If a
primitive function doesn't quite fit an abstraction, we have to define
auxiliary functions. For now, use {{\tt define}\/} to do so. Intermezzo~4
introduces a handy and important short-hand for that
purpose.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-circle-abs.html}{\rule{3pt}{5pt}} 

{\bf Exercise 21.4.2}

 \label{ex:moving-rect-abs} 
Abstract the functions {{\tt draw-rectangle}\/} and {{\tt clear-rectangle}\/} into a
single function {{\tt process-rectangle}\/}. 

Define {{\tt translate-rectangle}\/} using
{{\tt process-shape}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-rect-abs.html}{\rule{3pt}{5pt}} 


{\bf Exercise 21.4.3}

 \label{ex:moving-fig-abs} 
Abstract the functions {{\tt draw-shape}\/} and {{\tt clear-shape}\/} into a
single function {{\tt process-shape}\/}. Compare the function with the template
{{\tt fun-for-shape}\/}.

Define {{\tt translate-shape}\/} using {{\tt process-shape}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-fig-abs.html}{\rule{3pt}{5pt}}


{\bf Exercise 21.4.4}

 \label{ex:moving-list-map} 
Use Scheme's {{\tt map}\/} and {{\tt andmap}\/} to define {{\tt draw-losh}\/},
{{\tt clear-losh}\/}, and {{\tt translate-losh}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-list-map.html}{\rule{3pt}{5pt}}


\hrule
\begin{htmlonly}
\end{htmlonly}

   \begin{rawhtml}   <center><img src=../icons/ll.jpg alt=lunar lander></center>   \end{rawhtml}

\center{Figure: NASA's manned lunar lander}\label{fig:lunar-lander}
\hrule

{\bf Exercise 21.4.5}

 \label{ex:moving-updown} 
Modify the functions of exercises~\ref{ex:moving-fig-abs}
and~\ref{ex:moving-list-map} so that pictures move up and down on a canvas.

Modify the functions so that a shape can also be a line with a start position, an
end position, and a color.

Create a lunar lander picture (see figure~\ref{fig:lunar-lander}) using a list of
rectangles, circles, and lines.

Develop the program {{\tt lunar-lander}\/}, which shows the drop of a lunar lander
from a certain height. More specifically, the program creates a canvas and moves
the lunar lander from the top to the bottom. 

Use the teachpack {\tt\bf arrow.ss} to give users control over how fast and when
the lunar lander should move: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (start}\  {\tt 500}\  {\tt 100)}\\
{\tt (draw}\  {\tt LUNAR)}
\\
{\tt (control-up-down}\  {\tt LUNAR}\  {\tt 10}\  {\tt lunar-lander)}
\end{tabbing}\end{minipage}\end{center}
If time permits, modify the function so that a player can move the lander up,
down, left or right. Use {{\tt controller}\/} from {\tt\bf arrow.ss} to control the
movements.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/moving-updown.html}{\rule{3pt}{5pt}}


\subsection{Note: Designing Abstractions from Templates}
\label{sec:apriori-abs}

At the very beginning of this part of the book, we discussed how we design
 sets of functions from the same template. More specifically, when we
 design a set of functions that all consume the same kind of data, we reuse
 the same template over and over again. It is therefore not surprising that
 the function definitions look similar and that we will abstract them
 later. 

Indeed, we could abstract from the templates directly. While this topic is
 highly advanced and still subject of research in the area of programming
 languages, we can discuss it with a short example. Consider the template
 for lists: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (fun-for-l}\  {\tt l)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt l)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt l)}\  {\tt ...}\  {\tt (fun-for-l}\  {\tt (rest}\  {\tt l))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
It contains two gaps, one in each clause. When we define a list-processing
functions, we fill these gaps. In the first clause, we typically place a
plain value. For the second one, we combine {{\tt (first}\ {\tt l)}\/} and
{{\tt (f}\ {\tt (rest}\ {\tt l))}\/} where {{\tt f}\/} is the recursive function. 

We can abstract over this programming task with the following function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reduce}\ {\tt :}\ {\tt X}\ {\tt (X}\ {\tt Y}\ {}{\tt ->}{}\ {\tt Y)}\ {\tt (listof}\ {\tt Y)}\ {}{\tt ->}{}\ {\tt Y}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (reduce}\  {\tt base}\  {\tt combine}\  {\tt l)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt l)}\  {\tt base\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ombine}\  {\tt (first}\  {\tt l)}
\\
\>\>\>{\tt (reduce}\  {\tt base}\  {\tt combine}\  {\tt (rest}\  {\tt l)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
It consumes two extra arguments: {{\tt base}\/}, which is the value for the
base case, and {{\tt combine}\/}, which is a function that performs the
value combination for the second clause. 

Using {{\tt reduce}\/} we can define many plain list-processing functions as
well as almost all the functions of figure~\ref{fig:scheme-ho-list}. Here
are two of them: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sum}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (define}\  {\tt (sum}\  {\tt l)}\  {\tt (reduce}\  {\tt 0}\  {\tt +}\  {\tt l))}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt product}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (define}\  {\tt (product}\  {\tt l)}\  {\tt (reduce}\  {\tt 1}\  {\tt *}\  {\tt l))}
\end{tabbing}\end{minipage}
\end{center}
For {{\tt sum}\/}, the base case always produces {{\tt 0}\/}; adding the
first item and the result of the natural recursion combines the values of
the second clause. Analogous reasoning explains {{\tt product}\/}. 

To define {{\tt sort}\/}, we need to define an auxiliary function first: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sort}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt number)}\/}}\\
{\tt (d}\={\tt efine}\  {\tt sort}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (insert}\  {\tt an}\  {\tt alon)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt (list}\  {\tt an)\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (>}\  {\tt an}\  {\tt (first}\  {\tt alon))}\  {\tt (cons}\  {\tt an}\  {\tt alon)\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt (first}\  {\tt alon)}\  {\tt (insert}\  {\tt an}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt )\char'135}{\tt )))}
\\
\>\>{\tt (reduce}\  {\tt empty}\  {\tt insert)))}
\end{tabbing}\end{minipage}\end{center}
Other list-processing functions can be defined in a similar manner. 


\section{Designing Abstractions with First-Class Functions}
\label{sec:design-abstract-1st}

\htmladdimg{../icons/teacher.gif}~{\em This section requires {{\bf local}-exp\-res\-sion}s: see intermezzo~\ref{sec:int-local}. Learning to function with first-class functions is critical for all aspects of GUI programming.  All GUIs, including Java AWT-based ones, use functions as values, though they are called objects in that world.}

We have seen that functions can consume functions and how important that
is for creating single points of control in a function. But functions cannot
only consume functions, they can also { produce\/} them. More precisely,
expressions in the new Scheme can evaluate to functions.  Because the body
of a function definition is also an expression, a function can produce a
function. In this section, we first discuss this surprising idea and then
show how it is useful for abstracting functions and in other contexts. 

\subsection{Functions that Produce Functions} \label{sec:1st-prog}

While the idea of producing a function may seem strange at first, it is
extremely useful. Before we can discuss the usefulness of the idea, though,
we must explore how a function can produce a function. Here are three
examples:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt (f}\  {\tt x)}\  {\tt first)}\\
{\tt (define}\  {\tt (g}\  {\tt x)}\  {\tt f)}
\\
{\tt (d}\={\tt efine}\  {\tt (h}\  {\tt x)}\  
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt ((empty?}\  {\tt x)}\  {\tt f)}
\\
\>\>{\tt ((cons?}\  {\tt x)}\  {\tt g)))}
\end{tabbing}\end{minipage}\end{center}
The body of {{\tt f}\/} is {{\tt first}\/}, a primitive operation, so
applying {{\tt f}\/} to any argument always evaluates to
{{\tt first}\/}. Similarly, the body of {{\tt g}\/} is {{\tt f}\/}, so
applying {{\tt g}\/} to any argument always evaluates to {{\tt f}\/}.
Finally, depending on what kind of list we supply as an argument to
{{\tt h}\/}, it produces {{\tt f}\/} or {{\tt g}\/}. 

None of these examples are useful but they illustrate the basic idea. In
the first two cases, the body of the function definition { is\/} a
function. In the last case, it { evaluates to\/} a function.  The examples
are useless, however, because the results do not contain or refer to the
argument. For a function {{\tt f}\/} to produce a function that contains one
of {{\tt f}\/}'s arguments, {{\tt f}\/} must define a function and return it
as the result. In short, {{\tt f}\/}'s body must be a {{\bf local}-exp\-res\-sion}.

Recall that {{\bf local}-exp\-res\-sion}s group definitions and ask DrScheme
to evaluate a single expression in the context of these definitions.  They
can occur wherever an expression may occur, which means the following
definition is legal:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (add}\  {\tt x)}\\
\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt (x-adder}\  {\tt y)}\  {\tt (+}\  {\tt x}\  {\tt y)))}
\\
\>\>{\tt x-adder))}
\end{tabbing}\end{minipage}\end{center}
The function {{\tt add}\/} consumes a number; after all, {{\tt x}\/} is added
to {{\tt y}\/}. It then defines the function {{\tt x-adder}\/} with a
{{\bf local}-exp\-res\-sion}. The body of the {{\bf local}-exp\-res\-sion} is {{\tt x-adder}\/}, which means the
result of {{\tt add}\/} is {{\tt x-adder}\/}.

To understand {{\tt add}\/} better, let us look at how an application of
{{\tt add}\/} to some number evaluates: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (define}\  {\tt f}\  {\tt (add}\  {\tt 5))}\\
{\tt =}\  {\tt (define}\  {\tt f}\  {\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt (x-adder}\  {\tt y)}\  {\tt (+}\  {\tt 5}\  {\tt y)))}
\\
\>{\tt x-adder))}
\\
{\tt =}\  {\tt (define}\  {\tt f}\  {\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt (x-adder5}\  {\tt y)}\  {\tt (+}\  {\tt 5}\  {\tt y)))}
\\
\>{\tt x-adder5))}
\\
{\tt =}\  \={\tt (define}\  {\tt (x-adder5}\  {\tt y)}\  {\tt (+}\  {\tt 5}\  {\tt y))}
\\
\>{\tt (define}\  {\tt f}\  {\tt x-adder5)}
\end{tabbing}\end{minipage}\end{center}
That is, the last step adds the function {{\tt x-adder5}\/} to the
collection of our definitions; the evaluation continues with the body of
the {{\bf local}-exp\-res\-sion}, {{\tt x-adder5}\/}, which is the name of a
function and thus a value. Now {{\tt f}\/} is defined and we can use it: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (f}\  {\tt 10)}\\
{\tt =}\  {\tt (x-adder5}\  {\tt 10)}
\\
{\tt =}\  {\tt (+}\  {\tt 5}\  {\tt 10)}
\\
{\tt =}\  {\tt 15}
\end{tabbing}\end{minipage}\end{center}
That is, {{\tt f}\/} stands for {{\tt x-adder5}\/}, a function, which adds
{{\tt 5}\/} to its argument. 

Using this example, we can write {{\tt add}\/}'s contract and a purpose
statement:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt add}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt (number}\ {}{\tt ->}{}\ {\tt number)}\/}}\\
\hbox{;; to create a function that adds {{\tt x}\/} to its input}
\\
{\tt (d}\={\tt efine}\  {\tt (add}\  {\tt x)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt (x-adder}\  {\tt y)}\  {\tt (+}\  {\tt x}\  {\tt y)))}
\\
\>\>{\tt x-adder))}
\end{tabbing}\end{minipage}\end{center}
The most interesting property of {{\tt add}\/} is that its result
``remembers'' the value of {{\tt x}\/}. For example, every time we use
{{\tt f}\/}, it uses {{\tt 5}\/}, the value of {{\tt x}\/}, when {{\tt add}\/}
was used to define {{\tt f}\/}. This form of ``memory'' is the key to our
simple recipe for defining abstract functions, which we discuss in the next
section.

\subsection{Designing Abstractions with Functions-as-Values} 
\label{sec:design-abs-1st}

The combination of {{\bf local}-exp\-res\-sion}s and functions-as-values
simplifies our recipe for creating abstract functions. Consider our very
first example in figure~\ref{fig:cmp-above-below} again.  If we replace the
contents of the boxes with {{\tt rel-op}\/}, we get a function that has a
free variable. To avoid this, we can either add {{\tt rel-op}\/} to the
parameters or we can wrap the definition in a {{\tt local}\/} and prefix it
with a function that consumes {{\tt rel-op}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (filter2}\  {\tt rel-op)}\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (abs-fun}\  {\tt alon}\  {\tt t)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>{\tt \char'133}\={\tt (}\fbox{{\it rel-op\/}}\  {\tt (first}\  {\tt alon)}\  {\tt t)}
\\
\>\>\>\>\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt alon)}
\\
\>\>\>\>\>\>\>\>{\tt (abs-fun}\  {\tt (rest}\  {\tt alon)}\  {\tt t))\char'135}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt el}\={\tt se}
\\
\>\>\>\>\>\>\>{\tt (abs-fun}\  {\tt (rest}\  {\tt alon)}\  {\tt t)\char'135}{\tt )\char'135}{\tt )))}
\\
\>\>{\tt abs-fun))}
\end{tabbing}\end{minipage}\end{center}
If we also make the {{\tt local}\/}ly defined function the result of the
function, we have defined an abstraction of the two original functions. 

Put differently, we follow the example of {{\tt add}\/} in the preceding
section. Like {{\tt add}\/}, {{\tt filter2}\/} consumes an argument, defines
a function, and returns this function as a result. The result remembers 
the {{\tt rel-op}\/} argument for good as the following evaluation shows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (filter2}\  {\tt <)}\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (abs-fun}\  {\tt alon}\  {\tt t)}\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (<}\  {\tt (first}\  {\tt alon)}\  {\tt t)}
\\
\>\>\>\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt alon)}
\\
\>\>\>\>\>\>\>{\tt (abs-fun}\  {\tt (rest}\  {\tt alon)}\  {\tt t))\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt el}\={\tt se}
\\
\>\>\>\>\>\>{\tt (abs-fun}\  {\tt (rest}\  {\tt alon)}\  {\tt t)\char'135}{\tt )\char'135}{\tt )))}
\\
\>{\tt abs-fun)}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (}\={\tt d}\={\tt efine}\  {\tt (below3}\  {\tt alon}\  {\tt t)}\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (<}\  {\tt (first}\  {\tt alon)}\  {\tt t)}
\\
\>\>\>\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt alon)}
\\
\>\>\>\>\>\>\>{\tt (below3}\  {\tt (rest}\  {\tt alon)}\  {\tt t))\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>\>\>{\tt (below3}\  {\tt (rest}\  {\tt alon)}\  {\tt t)\char'135}{\tt )\char'135}{\tt ))}
\\
\>{\tt below3}
\end{tabbing}\end{minipage}\end{center}
Remember that as we lift a {{\tt local}\/} definition to the top-level
definitions, we also rename the function in case the same {{\tt local}\/} is
evaluated again. Here we choose the name {{\tt below3}\/} to indicate what
the function does. And indeed, a comparison between {{\tt below}\/} and
{{\tt below3}\/} reveals that the only difference is the name of the
function. 

From the calculation, it follows that we can give the result of
{{\tt (filter2}\ {\tt <)}\/} a name and use it as if it were {{\tt below}\/}. More
succinctly, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt below2}\  {\tt (filter2}\  {\tt <))}\end{tabbing}\end{minipage}\end{center}
is equivalent to
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (def}\={\tt ine}\  {\tt (below3}\  {\tt alon}\  {\tt t)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}\={\tt (<}\  {\tt (first}\  {\tt alon)}\  {\tt t)}
\\
\>\>\>\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt alon)}
\\
\>\>\>\>\>\>{\tt (below3}\  {\tt (rest}\  {\tt alon)}\  {\tt t))\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>\>{\tt (below3}\  {\tt (rest}\  {\tt alon)}\  {\tt t)\char'135}{\tt )\char'135}{\tt ))}
\\
{\tt (define}\  {\tt below2}\  {\tt below3)}
\end{tabbing}\end{minipage}\end{center}
which means {{\tt below2}\/} is just another name for {{\tt below3}\/} and
which directly proves that our abstract function correctly implements {{\tt below}\/}.

The example suggests a variant of the abstraction recipe from
section~\ref{sec:design-abstract}: 
\begin{description}
\item[The comparison:] The new recipe still requires us to compare and to
mark the differences. 


\item[The abstraction:] The new step concerns the way we define the
abstract function. We place one of the functions into a
{{\bf local}-exp\-res\-sion} and use the name of the function as the body of
the {{\tt local}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (concrete-fun}\  {\tt x}\  {\tt y}\  {\tt z)}\\
\>\>{\tt ...}\  \fbox{{\it op1\/}}\  {\tt ...}\  \fbox{{\it op2\/}}\  {\tt ...))}
\\
\>{\tt concrete-fun)}
\end{tabbing}\end{minipage}\end{center}
From that, we can create the abstract function by listing the names in the
boxes as parameters: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (abs-fun}\  {\tt op1}\  {\tt op2)}\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (concrete-fun}\  {\tt x}\  {\tt y}\  {\tt z)}
\\
\>\>\>{\tt ...}\  \fbox{{\it op1\/}}\  {\tt ...}\  \fbox{{\it op2\/}}\  {\tt ...))}
\\
\>\>{\tt concrete-fun))}
\end{tabbing}\end{minipage}\end{center}
If {{\tt op1}\/} or {{\tt op2}\/} is a special symbol, say {{\tt <}\/}, we
rename it into something that is more meaningful in the new context.

\item[The test:] To test the abstract function, we define the concrete
functions again, as before. Consider the example of {{\tt below}\/} and
{{\tt above}\/}. Obtaining {{\tt below}\/} and {{\tt above}\/} as instances of
{{\tt filter2}\/} is now straightforward: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt below2}\  {\tt (filter2}\  {\tt <))}\\
{\tt (define}\  {\tt above2}\  {\tt (filter2}\  {\tt >))}
\end{tabbing}\end{minipage}\end{center}
We simply apply {{\tt filter2}\/} to the contents of the box in the
respective concrete function and that application produces the old
function. 

\item[The contract:] The contract of an abstract function contains two
arrows. After all, the function produces a function, and to describe this
relationship the type to the right of the first arrow must contain another
arrow. 

Here is the contract for {{\tt filter2}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt filter2}\ {\tt :}\ {\tt (X}\ {\tt Y}\ {}{\tt ->}{}\ {\tt boolean)}\ {}{\tt ->}{}\ {\tt (Y}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X))}\/}}\end{tabbing}\end{minipage}\end{center}
It consumes a comparison function and produces a concrete filter-style
function. 

The generalization of the contract works as before. 
\end{description}
Given our experience with the first design recipe, the second one is only a
question of practice.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 22.2.1}

 \label{ex:map-local}
Define an abstraction of the functions {{\tt convertCF}\/} and
{{\tt names}\/} from section~\ref{sec:post-abs} using the new recipe for
abstraction.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/map-local.html}{\rule{3pt}{5pt}}


{\bf Exercise 22.2.2}

 \label{ex:sort-local}
Define an abstract version of {{\tt sort}\/} (see
exercise~\ref{ex:abs-sort}) using the new recipe for
abstraction.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sort-local.html}{\rule{3pt}{5pt}}


{\bf Exercise 22.2.3}

 \label{ex:fold-local}
Define {{\tt fold}\/} using the new recipe for abstraction. Recall that
{{\tt fold}\/} abstracts the following pair of functions:
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sum}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the sum of {{\tt alon}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (sum}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  \={\tt (first}\  {\tt alon)}
\\
\>\>\>{\tt (sum}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt product}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the product of {{\tt alon}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (product}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt 1\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (*}\  \={\tt (first}\  {\tt alon)}
\\
\>\>\>{\tt (product}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/fold-local.html}{\rule{3pt}{5pt}}


\subsection{A First Look at Graphical User Interfaces}
\label{sec:gui1}


\htmladdimg{../icons/teacher.gif}~{\em The section's examples and exercises require the teachpack  {\tt\bf gui.ss}, which provides some basic operations for building and managing GUI items. Like {\tt\bf draw.ss}, this teachpack simplifies this process but also gives much less control over the details of the process than the actual DrScheme toolbox.}

Functions as first-class values play a central role in the design of
 graphical user interfaces. The term ``interface'' refers to the boundary
 between the program and a user. As long as we are the only users, we can
 apply functions to data in DrScheme's {\tt Interaction} window. If we
 want others to use our programs though, we must provide a way to interact
 with the program that does not require any programming knowledge. The
 interaction between a program and a casual user is the {\sc {user  interface}}.

A {\sc {graphical user interface}} (GUI) is the most convenient interface
 for casual users.  A GUI is a window that contains GUI items. Some of
 these items permit users to enter text; others are included so that users
 can apply a specific function; and yet others exist to display a
 function's results.  Examples include {\sl buttons\/}, which the user can
 click with the mouse and which trigger a function application; {\sl choice  menus\/}, from which the user can choose one of a collection of values;
 {\sl text fields\/}, into which the user can type arbitrary text; and {\sl  message fields\/}, into which a program can draw text.

\hrule 
  \begin{rawhtml}  <table cellspacing=20 bgcolor=beige>  <tr> <td align=left>  <img src=../icons/lookup-gui0.gif></td>       <td align=center><img src=../icons/lookup-gui1.gif></td>       <td align=right> <img src=../icons/lookup-gui2.gif></td>  </tr>  </table>  \end{rawhtml}

\center{Figure: A simple GUI for looking up phone number}\label{fig:lookup-gui}
\hrule

Take a look at the simple GUI in figure~\ref{fig:lookup-gui}. The left-most
picture shows its initial state. In that state, the GUI contains a text
field labeled ``Name'' and a message field labeled ``Number'', plus a
``LookUp'' button.  In the second picture, the user has entered the name
``Sean'' but hasn't yet clicked the ``LookUp'' button.\footnote{The program has also cleared the result field to avoid any misunderstanding. Similarly, the user could also just hit the enter key instead of clicking the button. We ignore such subtleties here.} Finally, the right-most picture
shows how the GUI displays the phone number of ``Sean'' after the user
clicks the ``LookUp'' button.

The core of the program is a function that looks up a phone number for a
name in a list. We wrote several versions of this function in
part~\ref{part:structural} but always used it with DrScheme's
{\tt Interaction} window. Using the GUI of figure~\ref{fig:lookup-gui},
people who know nothing about Scheme can now use our function, too.

To build a graphical user interface, we construct structures\footnote{More precisely, we construct an object, though we do not need to understand the distinction between a structure and an object here.} that correspond to the
GUI items and hand them over to a GUI manager. The latter builds the
visible window from these items. Some of the structures' fields describe
the visual properties of the GUI's elements, such as the label of a button,
the initial content of a message field, or the available choices on a
menu. Other fields stand for functions. They are called {\sl call-back functions} because the GUI manager calls---or applies---these functions
when the user manipulates the corresponding GUI element. Upon application,
a call-back function obtains strings and (natural) numbers from the
elements of the GUI and then applies the function proper. This last step
computes answers, which the call-back function can place into GUI elements
just like graphics functions draw shapes onto a canvas.

\hrule



\begin{center}
\begin{picture}(350,100)
\put(0,0){\begin{picture}(100,100) 
\put(0,0){\line(1,0){100}} 
\put(100,0){\line(0,1){100}} 
\put(100,100){\line(-1,0){100}} 
\put(0,100){\line(0,-1){100}} 
\put(10,50){{Interface Program}} 
\put(30,36){{(View)}} \end{picture}}
\put(250,0){\begin{picture}(100,100) 
\put(0,0){\line(1,0){100}} 
\put(100,0){\line(0,1){100}} 
\put(100,100){\line(-1,0){100}} 
\put(0,100){\line(0,-1){100}} 
\put(20,50){{Core Program}} 
\put(30,36){{(Model)}} \end{picture}}
\put(240,90){\vector(-1,0){140}} 
\put(120,93){{{\tt (draw-message}\ {\tt a-message)}\/}}
\put(240,90){\circle*{3}}
\put(240,70){\vector(-1,0){140}} 
\put(120,73){{{\tt (text-contents}\ {\tt a-text-field)}\/}}
\put(240,70){\circle*{3}}
\put(100,30){\vector(1,0){150}}   
\put(120,33){{{\tt (call-back}\ {\tt an-event)}\/}}
\put(240,30){\line(0,1){60}}
\put(240,30){\circle*{3}}
\put(250,10){\vector(-1,0){150}} 
\put(120,13){{{\tt (make-button}\ {\tt label}\ {\tt call-back)}\/}}
\end{picture}
\end{center}

\center{Figure: The model-view arrangement} \label{fig:mvc}
\hrule

The ideal program consists of two completely separate components: the
{\sc {model}}, which is the kind of program we are learning to design,
and a {\sc {view}}, which is the GUI program that manages the display of
information and the user's mouse and keyboard manipulations. The bridge
between the two is the {\sc {control}} expression. Figure~\ref{fig:mvc}
graphically illustrates the organization, known as the
{\sc {model-view-control}} architecture. The lowest arrow indicates how a
program makes up a button along with a call-back function.  The
left-to-right arrow depicts the mouse-click event and how it triggers an
application of the call-back function. It, in turn, uses other GUI
functions to obtain obtain user input before it applies a core function or
to display results of the core function.

The separation of the program into two parts means that the definitions for
the model contain no references to the view, and that the definitions for
the view contain no references to the data or the functionality of the
model.  The organization principle evolved over two decades from many good
and bad experiences.  It has the advantage that, with an adaptation of just
the bridge expression, we can use one and the same program with different
GUIs and {\it vice versa\/}. Furthermore, the construction of views
requires different tools than the construction of models. Constructing
views is a labor-intensive effort and graphical design, but fortunately, it
is often possible to generate large portions automatically.  The
construction of models, in contrast, will always demand a serious program
design effort.

\hrule 
\begin{quote} A {\sl {gui-item}} is either  \begin{enumerate} \item {{\tt (make-button}\ {\tt string}\ {\tt (X}\ {}{\tt ->}{}\ {\tt true))}\/} \item {{\tt (make-text}\ {\tt string)}\/} \item {{\tt (make-choices}\ {\tt (listof}\ {\tt string))}\/} or \item {{\tt (make-message}\ {\tt string)}\/}. \end{enumerate} \end{quote}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt create-window}\ {\tt :}\ {\tt (listof}\ {\tt (listof}\ {\tt gui-item))}\ {}{\tt ->}{}\ {\tt true}\/} }\\
\hbox{;; to add gui-items to the window and to show the window}
\\
\hbox{;; each list of gui-items defines one row of gui items in the window}
\\
\hbox{;; {{\tt hide-window}\ {\tt :}\ {}{\tt ->}{}\ {\tt true}\/} }
\\
\hbox{;; to hide the window}
\\
\hbox{;; {{\tt make-button}\ {\tt :}\ {\tt string}\ {\tt (event\char'045}\ {}{\tt ->}{}\ {\tt true)}\ {}{\tt ->}{}\ {\tt gui-item}\/} }
\\
\hbox{;; to create a button with label and call-back function }
\\
\hbox{;; {{\tt make-message}\ {\tt :}\ {\tt string}\ {}{\tt ->}{}\ {\tt gui-item}\/} }
\\
\hbox{;; to create an item that displays a message}
\\
\hbox{;; {{\tt draw-message}\ {\tt :}\ {\tt gui-item\char'133}{\tt message\char'045}{\tt \char'135}\ {}{\tt ->}{}\ {\tt true}\/} }
\\
\hbox{;; to display a message in a message item }
\\
\hbox{;; it erases the current message }
\\
\hbox{;; {{\tt make-text}\ {\tt :}\ {\tt string}\ {}{\tt ->}{}\ {\tt gui-item}\/} }
\\
\hbox{;; to create an item (with label) that allows users to enter text}
\\
\hbox{;; {{\tt get-text}\ {\tt :}\ {\tt gui-item\char'133}{\tt text\char'045}{\tt \char'135}\ {}{\tt ->}{}\ {\tt string}\/} }
\\
\hbox{;; to determine the contents of a text field }
\\
\hbox{;; {{\tt make-choice}\ {\tt :}\ {\tt (listof}\ {\tt string)}\ {}{\tt ->}{}\ {\tt gui-item}\/} }
\\
\hbox{;; to create a choice menu that permits users to choose from some}
\\
\hbox{;; string alternatives }
\\
\hbox{;; {{\tt get-choice}\ {\tt :}\ {\tt gui-item\char'133}{\tt choice\char'045}{\tt \char'135}\ {}{\tt ->}{}\ {\tt num}\/} }
\\
\hbox{;; to determine which choice is currently selected in a choice-item }
\\
\hbox{;; the result is the 0-based index in the choice menu }
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The {\tt\bf gui.ss} operations} \label{fig:gui-tp}
\hrule 


Here we study the simplified GUI world of the teachpack
{\tt\bf gui.ss}. Figure~\ref{fig:gui-tp} specifies the operations that the
teachpack provides.\footnote{The {{\tt gui-item}\/}s aren't really structures, which explains the font of the operations' names.} The GUI
manager is represented by the function {{\tt create-window}\/}. Its contract
and purpose statement are instructive. They explain that we create a window
from a list. The function arranges these lists in a corresponding number of
rows on the visible window. Each row is specified as a list of
{{\tt gui-item}\/}s. The data definition for {{\tt gui-item}\/}s in
figure~\ref{fig:gui-tp} shows that there are four different kinds:
\begin{description}
\item[text fields,] which are created with {{\tt (make-text}\ {\tt a-string)}\/} and
allow users to enter arbitrary text into an area in the window; 

\item[buttons,] which are created with {{\tt (make-button}\ {\tt a-string}{ }{\tt a-function)}\/} and allow users to apply a function with the click of a mouse
button;

\item[choice menus,] which are created with {{\tt (make-choice}{ }{\tt a-list-of-strings)}\/} and allow users to pick a choice from a specified set
of choices; and

\item[message fields,] which are created with {{\tt (make-message}{ }{\tt a-string)}\/} and enable the model to inform users of results. 
\end{description}
The function that goes with a button is a function of one argument: an
event. For most uses, we can ignore the event; it is simply a token that
signals the user's click action.

How all this works is best illustrated with examples.
Our first example is a canonical GUI program: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (create-window}\  {\tt (list}\  {\tt (list}\  {\tt (make-button}\  {\tt ``Close''}\  {\tt hide-window))))}\end{tabbing}\end{minipage}\end{center}
It creates a window with a single button and equips it with the simplest of
all call-backs: {{\tt hide-window}\/}, the function that hides the window. When
the user clicks the button labeled {{\tt ``Close''}\/}, the window disappears.

The second sample GUI copies what the user enters into a {text} field to a
{message} field. We first create a {text} field and a {message} field:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt a-text-field}\\
\>{\tt (make-text}\  {\tt ``Enter}\  {\tt Text:''))}
\\
{\tt (d}\={\tt efine}\  {\tt a-message}
\\
\>{\tt (make-message}\  {\tt ``\char'140}{\tt Hello}\  {\tt World\char'047}\  {\tt is}\  {\tt a}\  {\tt silly}\  {\tt program.''))}
\end{tabbing}\end{minipage}\end{center}
Now we can refer to these fields in a call-back function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt echo-message}\ {\tt :}\ {\tt X}\ {}{\tt ->}{}\ {\tt true}\/}}\\
\hbox{;; to extract the contents of {{\tt a-text-field}\/} and to draw it into {{\tt a-message}\/} }
\\
{\tt (d}\={\tt efine}\  {\tt (echo-message}\  {\tt e)}
\\
\>{\tt (draw-message}\  {\tt a-message}\  {\tt (text-contents}\  {\tt a-text-field)))}
\end{tabbing}\end{minipage}\end{center}
The definition of the call-back function is based on our (domain) knowledge
about the {{\tt gui-item}\/}s. Specifically, the function
{{\tt echo-message}\/} obtains the current contents of the {text} field with
{{\tt text-contents}\/} as a string, and it draws this string into the
{message} field with the {{\tt draw-message}\/} function. To put everything
together, we create a window with two rows:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (}\={\tt create-window}\\
\>{\tt (list}\  \={\tt (list}\  {\tt a-text-field}\  {\tt a-message)}
\\
\>\>{\tt (list}\  {\tt (make-button}\  {\tt ``Copy}\  {\tt Now''}\  {\tt echo-message))))}
\end{tabbing}\end{minipage}\end{center}
The first row contains the {text} and the {message} field; the second one
contains a button with the label {{\tt ``Copy}\ {\tt Now''}\/} whose call-back
function is {{\tt echo-message}\/}. The user can now enter text into the
{text} field, click the button, and see the text appear in the {message}
field of the window.

The purpose of the third and last example is to create a window with a
choice menu, a message field, and a button. Clicking the button puts the
current choice into the message field. As before, we start by defining the
input and output {{\tt gui-item}\/}s:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt THE-CHOICES}\\
\>{\tt (list}\  {\tt ``green''}\  {\tt ``red''}\  {\tt ``yellow''))}
\\
{\tt (d}\={\tt efine}\  {\tt a-choice}
\\
\>{\tt (make-choice}\  {\tt THE-CHOICES))}
\\
{\tt (d}\={\tt efine}\  {\tt a-message}
\\
\>{\tt (make-message}\  {\tt (first}\  {\tt THE-CHOICES)))}
\end{tabbing}\end{minipage}\end{center}
Because the list of choices is used more than once in the program, it is
specified in a separate variable definition.  

As before, the call-back function for the button interacts with
{{\tt a-choice}\/} and {{\tt a-message}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt echo-choice}\ {\tt :}\ {\tt X}\ {}{\tt ->}{}\ {\tt true}\/}}\\
\hbox{;; to determine the current choice of {{\tt a-choice}\/} and}
\\
\hbox{;; to draw the corresponding string into {{\tt a-message}\/} }
\\
{\tt (d}\={\tt efine}\  {\tt (echo-choice}\  {\tt e)}
\\
\>{\tt (draw-message}\  \={\tt a-message}
\\
\>\>{\tt (list-ref}\  \={\tt THE-CHOICES}
\\
\>\>\>{\tt (choice-index}\  {\tt a-choice))))}
\end{tabbing}\end{minipage}\end{center}
Specifically, the call-back functionq determines the {{\tt 0}\/}-based index
of the user's current choice with {{\tt choice-index}\/}, uses Scheme's
{{\tt list-ref}\/} function to extract the corresponding {string} from
{{\tt THE-CHOICES}\/}, and then draws the result into the message field of
the window. To create the window, we arrange {{\tt a-choice}\/} and
{{\tt a-message}\/} in one row and the button in a row below:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (}\={\tt create-window}\\
\>{\tt (list}\  \={\tt (list}\  {\tt a-choice}\  {\tt a-message)}
\\
\>\>{\tt (list}\  {\tt (make-button}\  {\tt ``Confirm}\  {\tt Choice''}\  {\tt echo-choice))))}
\end{tabbing}\end{minipage}\end{center}                          


\hrule 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline Model}:}\\
\hbox{;; {{\tt build-number}\ {\tt :}\ {\tt (listof}\ {\tt digit)}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to translate a list of digits into a number}
\\
\hbox{;; example: {{\tt (build-number}\ {\tt (list}\ {\tt 1}\ {\tt 2}\ {\tt 3))}\ {\tt =}\ {\tt 123}\/}}
\\
{\tt (define}\  {\tt (build-number}\  {\tt x)}\  {\tt ...)}
\\
\hbox{;; \rule{4in}{.1pt}}
\\
\hbox{;; {\underline View}:}
\\
\hbox{;; the ten digits as strings }
\\
{\tt (d}\={\tt efine}\  {\tt DIGITS}
\\
\>{\tt (build-list}\  {\tt 10}\  {\mbox{{\it number\/}}{{}{\tt ->}{}}\mbox{{\it string\/}}}{\tt ))}
\\
\hbox{;; a list of three digit choice menus }
\\
{\tt (d}\={\tt efine}\  {\tt digit-choosers}
\\
\>{\tt (build-list}\  {\tt 3}\  {\tt (lambda}\  {\tt (i)}\  {\tt (make-choice}\  {\tt DIGITS))))}
\\
\hbox{;; a {message} field for saying hello and displaying the number }
\\
{\tt (d}\={\tt efine}\  {\tt a-msg}
\\
\>{\tt (make-message}\  {\tt ``Welcome''))}
\\
\hbox{;; \rule{4in}{.1pt}}
\\
\hbox{;; {\underline Controller}: }
\\
\hbox{;; {{\tt check-call-back}\ {\tt :}\ {\tt X}\ {}{\tt ->}{}\ {\tt true}\/}}
\\
\hbox{;; to get the current choices of digits, convert them to a number, }
\\
\hbox{;; and to draw this number as a string into the {message} field }
\\
{\tt (d}\={\tt efine}\  {\tt (check-call-back}\  {\tt b)}
\\
\>{\tt (draw-message}\  \={\tt a-msg}\  
\\
\>\>{\tt (}\={\mbox{{\it number\/}}{{}{\tt ->}{}}\mbox{{\it string\/}}}
\\
\>\>\>{\tt (}\={\tt build-number}\  
\\
\>\>\>\>{\tt (map}\  {\tt choice-index}\  {\tt digit-choosers)))))}
\\
{\tt (}\={\tt create-window}\  
\\
\>{\tt (}\={\tt list}\  
\\
\>\>{\tt (append}\  {\tt digit-choosers}\  {\tt (list}\  {\tt a-msg))}
\\
\>\>{\tt (list}\  {\tt (make-button}\  {\tt ``Check}\  {\tt Guess''}\  {\tt check-call-back))))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: A GUI for echoing digits as numbers} \label{fig:copy-many}
\hrule 

Now that we have examined some basic GUI programs, we can study a program
with full-fledged core and GUI components. Take a look at the definitions
in figure~\ref{fig:copy-many}. The program's purpose is to echo the values
of several digit choice menus as a number into some message field. The
model consists of the {{\tt build-number}\/} function, which converts a list
of (three) digits into a number. We have developed several such functions,
so the figure only mentions what it does. The GUI component of the program
sets up three choice menus, a message field, and a button.  The control
part consists of a single call-back function, which is attached to the
single button in the window. It determines the (list of) current choice
indices, hands them over to {{\tt build-number}\/}, and draws the result as
a string into the message field.

Let's study the organization of the call-back functions in more detail. It
composes three kinds of functions:
\begin{enumerate}
\item The innermost function determines the current state of the
{{\tt gui-item}\/}s. This is the user's input. With the given functions, we
can either determine the string that the user entered into a text field or
the 0-based index of a choice menu. 

\item This user input is consumed by the main function of the model. The
call-back function may convert the user's string into some other data, say,
a symbol or a number.

\item The result of the model function, in turn, is drawn into a message
field, possibly after converting it to a string first.
\end{enumerate}
The control component of a program is also responsible for the visual
composition of the window. The teachpack provides only one function for
this purpose: {{\tt create-window}\/}. Standard GUI toolboxes provide many
more functions, though all of these toolboxes differ from each other and
are changing rapidly. 


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 22.3.1}

 \label{ex:gui-guess}
Modify the program of figure~\ref{fig:copy-many} so that it implements the
number-guessing game from exercises~\ref{ex:guess1}, \ref{ex:guess2},
and~\ref{ex:guess3}. Make sure that the number of digits that the player
must guess can be changed by editing a single definition in the program.

\noindent{\bf Hint:}  Recall that exercise~\ref{ex:random} introduces a function that
generates random numbers.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/gui-guess.html}{\rule{3pt}{5pt}}


{\bf Exercise 22.3.2}

 \label{ex:gui-phone}
Develop a program for looking up phone numbers. The program's GUI should
consist of a {text} field, a message field, and a button. The {text} field
permits users to enter names. The message field should display the number
that the model finds or the message {{\tt ``name}\ {\tt not}\ {\tt found''}\/}, if the model
produces {{\tt false}\/}.

Generalize the program so that a user can also enter a phone number (as a
sequence of digits containing no other characters). 

\medskip\noindent{\bf Real-world GUIs}:\  The graphical user interface in
figure~\ref{fig:lookup-gui} was not constructed from the items provided by
the teachpack. GUIs constructed with the teachpack's {{\tt gui-item}\/}s are 
primitive. They are sufficient, however, to study the basic principles.~{\rule{3pt}{5pt}}

\noindent{\bf Hints:}  (1) Scheme provides the function {\mbox{{\it string\/}}{{}{\tt ->}{}}\mbox{{\it symbol\/}}\/} for
converting a string to a symbol. (2) It also provides the function
{\mbox{{\it string\/}}{{}{\tt ->}{}}\mbox{{\it number\/}}\/}, which converts a string to a number if
possible. If the function consumes a string that doesn't represent a
number, it produces {{\tt false}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (}\mbox{{\it string\/}}{{}{\tt ->}{}}\mbox{{\it number\/}}\  {\tt ``6670004'')}\\
{\tt =}\  {\tt 6670004}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (}\mbox{{\it string\/}}{{}{\tt ->}{}}\mbox{{\it number\/}}\  {\tt ``667-0004'')}\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
The generalization demonstrates how one and the same GUI can use two
distinct models.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/gui-phone.html}{\rule{3pt}{5pt}}


{\bf Exercise 22.3.3}

 \label{ex:gui-guipad}
Develop {\mbox{{\it pad\/}}{{}{\tt ->}{}}\mbox{{\it gui\/}}\/}. The function consumes a title (string) and a
{{\tt gui-table}\/}. It turns the table into a list of lists of
{{\tt gui-item}\/}s that {{\tt create-window}\/} can consume. Here is the
data definition for {{\tt gui-table}\/}s:

\begin{quote} A {\sl {gui-table}} is a {{\tt (listof}\ {\tt (listof}\ {\tt cell))}\/} . \end{quote}

\begin{quote} A {\sl {cell}} is either    \begin{enumerate}    \item a number,     \item a symbol.    \end{enumerate} \end{quote}

Here are two examples: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt pad}\\
\>{\tt \char'047}{\tt (}\={\tt (1}\  {\tt 2}\  {\tt 3)}
\\
\>\>{\tt (4}\  {\tt 5}\  {\tt 6)}
\\
\>\>{\tt (7}\  {\tt 8}\  {\tt 9)}
\\
\>\>{\tt (\char'134}{\tt \char'043}\  {\tt 0}\  {\tt *)))}
\end{tabbing}\end{minipage}\hspace{2in}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt pad2}\  \\
\>{\tt \char'047}{\tt (}\={\tt (1}\  {\tt 2}\  {\tt 3}\  \  {\tt +)}
\\
\>\>{\tt (4}\  {\tt 5}\  {\tt 6}\  \  {\tt -)}
\\
\>\>{\tt (7}\  {\tt 8}\  {\tt 9}\  \  {\tt *)}
\\
\>\>{\tt (0}\  {\tt =}\  {\tt \char'134}{\tt .}\  {\tt /)))}
\end{tabbing}\end{minipage}
\end{center}
The table on the left lays out a virtual phone pad, the right one a
calculator pad. 

The function {\mbox{{\it pad\/}}{{}{\tt ->}{}}\mbox{{\it gui\/}}\/} should turn each cell into a button. The
resulting list should be prefixed with two messages. The first one displays
the title and never changes. The second one displays the latest button that
the user clicked. The two examples above should produce the following two 
GUIs: 

  \begin{rawhtml}  <table cellspacing=20 bgcolor=beige>  <tr> <td align=left>  <img src=../icons/vp.gif></td>       <td align=right> <img src=../icons/cal.gif></td>  </tr>  </table>  \end{rawhtml}

\noindent{\bf Hint:}  The second message header requires a short string, for example,
{{\tt ``N''}\/}, as the initial value.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/gui-guipad.html}{\rule{3pt}{5pt}}


\section{Mathematical Examples} 
\label{sec:math-abstract} 

Applying mathematics to real-world problems requires programs that
implement mathematical functions. In many cases, the programs also employ
functions that consume and produce functions. Mathematics is therefore a
great starting point for practicing programming with functions and, more
generally, for creating abstract functions.

The first subsection covers sequences and series, a key element of
mathematics. The second section discusses integration, which heavily relies
on series. Finally, the third section introduces function differentiation.

\subsection{Sequences and Series} \label{sec:seqs-func}

In pre-algebra and algebra, we encounter {\sl sequences\/} (also known as
progressions) of numbers. Here are three examples:
\begin{enumerate}
\item 0, 2, 4, 6, 8;
\item 1, 3, 5, 7, 9;
\item 5, 10, 15, 20, 25.
\end{enumerate}
The first two enumerate the first five even and odd natural numbers,
respectively; the last one lists the first five positive integers, evenly
divisible by 5. Sequences can also be infinite:
\begin{enumerate}
\item 0, 2, 4, 6, 8, \ldots;
\item 1, 3, 5, 7, 9, \ldots;
\item 5, 10, 15, 20, 25, \ldots
\end{enumerate}
Following mathematical tradition, infinite sequences end in ``\ldots'' and
the reader must determine how to find more of the terms in the sequence.

One way to understand sequences of numbers, especially infinite ones, is to
match them up with an enumeration of the natural numbers. For example,
the even and odd (natural) numbers match up like this: 
$$ \begin{array}{l|r|r|r|r|r|r|r|r|r|r|r}
\mbox{index} & 0 & 1 & 2 & 3 & 4 & 5  & 6  & 7  & 8  & 9  &  \ldots \\
 \hline
\mbox{evens} & 0 & 2 & 4 & 6 & 8 & 10 & 12 & 14 & 16 & 18 &  \ldots \\
\mbox{odds}  & 1 & 3 & 5 & 7 & 9 & 11 & 13 & 15 & 17 & 19 &  \ldots 
\end{array} $$
It is easy to see from this table that the every even number is $2 \cdot i$
for its index {\it i\/} and that an odd number is $2 \cdot i + 1$.

Both statements can be translated into simple Scheme functions:
\begin{center}\small
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt make-even}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\bf N}{\tt \char'133}{\tt even\char'135}\/}}\\
\hbox{;; to compute the {{\tt i}\/}-th even number}
\\
{\tt (d}\={\tt efine}\  {\tt (make-even}\  {\tt i)}
\\
\>{\tt (*}\  {\tt 2}\  {\tt i))}
\end{tabbing}\end{minipage}\qquad\quad\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt make-odd}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\bf N}{\tt \char'133}{\tt odd\char'135}\/}}\\
\hbox{;; to compute the {{\tt i}\/}-th odd number}
\\
{\tt (d}\={\tt efine}\  {\tt (make-odd}\  {\tt i)}
\\
\>{\tt (+}\  {\tt (*}\  {\tt 2}\  {\tt i)}\  {\tt 1))}
\end{tabbing}\end{minipage}
\end{center}
In short, functions from natural numbers to numbers are representations of
infinite sequences. 

A mathematical {\sl series\/} is the sum of a sequence. The three finite
sequences have the sums 20, 25, and 75, respectively. In the case of
infinite sequences it is often interesting to consider a finite portion,
staring with the first one.\footnote{In some cases, an infinite sequence may also have a sum. Specifically, adding up more and more of the terms of a sequence produces numbers that are closer and closer to some number, which we call the sum. For example, the sum of the sequence $$ 1, \frac12, \frac14, \frac18, \ldots $$ is 2. In contrast, the sequence  $$ 1, \frac12, \frac13, \frac14, \ldots $$ does not have a sum.} For example, adding first 10 even numbers yields 90,
and adding the first 10 odd numbers yields 100. Computing a series is
clearly a job for a computer. Here are functions that add up the first
{{\tt n}\/} odd or even numbers, respectively, using {{\tt make-even}\/} and
{{\tt make-odd}\/} to compute the required numbers: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt series-even}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to sum up the first\\
;; {{\tt n}\/} even numbers}
\\
{\tt (d}\={\tt efine}\  {\tt (series-even}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt 0)}\  {\tt (make-even}\  {\tt n)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  \={\tt (make-even}\  {\tt n)}\  
\\
\>\>\>{\tt (series-even}\  {\tt (-}\  {\tt n}\  {\tt 1)))\char'135}{\tt ))}\  \  
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt series-odd}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to sum up the first\\
;; {{\tt n}\/} odd numbers}
\\
{\tt (d}\={\tt efine}\  {\tt (series-odd}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt 0)}\  {\tt (make-odd}\  {\tt n)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  \={\tt (make-odd}\  {\tt n)}
\\
\>\>\>{\tt (series-odd}\  {\tt (-}\  {\tt n}\  {\tt 1)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}
\end{center}

The two functions are natural candidates for abstraction and here is the
result of following our basic abstraction recipe: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt series}\ {\tt :}\ {\bf N}\ {\tt (}{\bf N}\ {}{\tt ->}{}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to sum up the first {{\tt n}\/} numbers in the sequence {{\tt a-term}\/},}
\\
{\tt (d}\={\tt efine}\  {\tt (series}\  {\tt n}\  {\tt a-term)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt 0)}\  {\tt (a-term}\  {\tt n)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  \={\tt (a-term}\  {\tt n)}
\\
\>\>\>{\tt (series}\  {\tt (-}\  {\tt n}\  {\tt 1)}\  {\tt a-term))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The first argument specifies where the addition starts.  The second
argument of {{\tt series}\/} is a function that maps a natural number to the
corresponding term in the series. To test {{\tt series}\/}, we apply it to
{{\tt make-even}\/} and {{\tt make-odd}\/}: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt series-even1}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (series-even1}\  {\tt n)}
\\
\>{\tt (series}\  {\tt n}\  {\tt make-even))}\  
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt series-odd1}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (series-odd1}\  {\tt n)}
\\
\>{\tt (series}\  {\tt n}\  {\tt make-odd))}
\end{tabbing}\end{minipage}
\end{center}

For over a century, mathematicians have used the Greek symbol $\Sigma$ to
communicate about series. The two series above would be expressed as
$$
  \qquad\sum_{i=0}^{i=n}\mbox{\it make-even(i)\/} 
  \qquad\qquad\qquad\qquad
  \sum_{i=0}^{i=n}\mbox{\it make-odd(i)\/}\qquad
$$
A true (lazy) mathematician would also replace {{\tt make-even}\/} and
{{\tt make-odd}\/} by their definitions, {\it i.e.}, $2 \cdot i$ and $2 \cdot i +
1$, but we refrain from doing so to emphasize the analogy to our
(well-organized) functions. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 23.1.1}

 		\label{ex:local-series}
Use {{\tt local}\/} to create {{\tt series-local}\/} from
{{\tt series-even}\/} and {{\tt series-odd}\/}. Show with a hand-evaluation 
that {{\tt (series-local}\ {\tt make-even)}\/} is equivalent to
{{\tt series-even}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/local-series.html}{\rule{3pt}{5pt}} 


\subsection{Arithmetic Sequences and Series} 

In an arithmetic sequence 
\[ a_0, a_1, a_2, a_3, \ldots, a_n, a_{n+1}, \ldots \]
each successor term $a_{n+1}$ is the result of adding a fixed constant to
$a_n$. Here is a concrete example, matched up with the natural numbers: 
$$
\begin{array}{l|r|r|r|r|r|r|r|r|r|r|r}
 \mbox{{\it index\/}}       & 0 & 1 & 2  & 3  & 4  & \ldots \\
 \hline 
 \mbox{arithmetic sequence} & 8 & 13 & 18 & 23 &     \ldots 
\end{array} 
$$
Here the starting point is 3 and the constant is 5. From these two facts,
called {\sl starting point\/} and {\sl summand\/}, respectively, all other
terms in the sequence can be determined.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 23.2.1}

 \label{ex:alg-ser2}
Develop the recursive function {{\tt a-fives}\/}, which consumes a natural
number and recursively determines the corresponding term in the above
series.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/alg-ser2.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.2.2}

 \label{ex:alg-ser3}
Develop the non-recursive function {{\tt a-fives-closed}\/}. It consumes
a natural number and determines the corresponding term in the above
series. A non-recursive function is sometimes called a {\sl closed form.}~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/alg-ser3.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.2.3}

 \label{ex:alg-ser4}
Use {{\tt series}\/} to determine the sum of the {{\tt a-fives}\/} sequence
for the bounds 3, 7, and 88.  Can an infinite arithmetic series have a
sum?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/alg-ser4.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.2.4}

 \label{ex:alg-ser1}
Develop the function {{\tt seq-a-fives}\/}, which consumes a natural number
{{\tt n}\/} and creates the sequence of the first {{\tt n}\/} terms according
to {{\tt a-fives}\/} or {{\tt a-fives-closed}\/}. \noindent{\bf Hint:}  Use
{{\tt build-list}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/alg-ser1.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.2.5}

 \label{ex:alg-ser-gen}
Develop {{\tt arithmetic-series}\/}.  The function consumes two numbers:
{{\tt start}\/} and {{\tt s}\/}.  Its result is a function that represents
the arithmetic series whose starting point is {{\tt start}\/} and whose
summand is {{\tt s}\/}.  For example, {{\tt (arithmetic-series}\ {\tt 3}\ {\tt 5)}\/}
yields {{\tt a-fives}\/} (or {{\tt a-fives-closed}\/}). Similarly,
{{\tt (arithmetic-series}\ {\tt 0}\ {\tt 2)}\/} produces a function that represents the
series of even numbers.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/alg-ser-gen.html}{\rule{3pt}{5pt}}


\subsection{Geometric Sequences and Series} 

In a geometric sequence 
$$ g_0, g_1, g_2, g_3, \ldots, g_n, g_{n+1}, \ldots $$
each succesor term $g_{n+1}$ is the result of multiplying a fixed constant wit
$g_n$. Here is a concrete example, matched up with the natural numbers: 
$$ 
\begin{array}{l|r|r|r|r|r|r|r|r|r|r|r}
 \mbox{\it index\/}        & 0 & 1  & 2  & 3   & 4    & \ldots \\
 \hline
 \mbox{geometric sequence} & 3 & 15 & 75 & 375 & 1875 & \ldots 
\end{array} 
$$
Here the starting point is 3 and the constant is 5. From these, called
starting point and factor, respectively, every other term in the sequence
is determined.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 23.3.1}

 \label{ex:geo-ser1}
Develop the recursive function {{\tt g-fives}\/}, which consumes a natural
number and recursively determines the corresponding term in the above
geometric sequence.\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/geo-ser1.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.3.2}

 \label{ex:geo-ser2}
Develop the non-recursive function {{\tt g-fives-closed}\/}. It consumes
a natural number and determines the corresponding term in the above
series. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/geo-ser2.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.3.3}

 \label{ex:geo-ser9}
Develop the function {{\tt seq-g-fives}\/}, which consumes a natural number
{{\tt n}\/} and creates the sequence of the first {{\tt n}\/} terms according
to {{\tt g-fives}\/} or {{\tt g-fives-closed}\/}. \noindent{\bf Hint:}  Use
{{\tt build-list}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/geo-ser9.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.3.4}

 \label{ex:geo-ser-gen}
Develop {{\tt geometric-series}\/}. The function consumes two numbers:
{{\tt start}\/} and {{\tt s}\/}.  Its result is a function that represents
the geometric series whose starting point is {{\tt start}\/} and whose
factor is {{\tt s}\/}.  For example, {{\tt (geometric-series}\ {\tt 3}\ {\tt 5)}\/} yields
{{\tt g-fives}\/} (or {{\tt g-fives-closed}\/}).~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/geo-ser-gen.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.3.5}

 \label{ex:geo-ser4}
Use {{\tt series}\/} to determine the sum of the {{\tt g-fives}\/} sequence
for the bounds 3, 7, and 88. 
Use {{\tt series}\/} to determine the sum of {{\tt (geometric-series}\ {\tt 1}{ }{\tt .1)}\/} for the bounds 3, 7, 88.  
Can an infinite geometric series have a sum?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/geo-ser4.html}{\rule{3pt}{5pt}}


\subsubsection*{Taylor Series}

Mathematical constants like $\pi$ and $e$ or functions like {\it sin\/},
{\it cos\/}, {\it log\/} are difficult to compute. Since these functions
are important for many daily engineering applications, mathematicians have
spent a lot of time and energy looking for better ways to compute these
functions. One method is to replace a function with its Taylor series,
which is, roughly speaking, an infinitely long polynomial. 

A Taylor series is the sum of a sequence of terms. In contrast to arithmetic
or geometric sequences, the terms of a Taylor series depend on two
unknowns: some variable {{\tt x}\/} and the position {{\tt i}\/} in the
sequence. Here is the Taylor series for the exponential function: 
$$ e^x = 1 + \frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \ldots  $$
That is, if we wish to compute $e^x$ for any specific {{\it x\/}}, we
replace {\it x\/} with the number and determine the value of the series.
In other words, for a specific value of {{\tt x}\/}, say 1, the Taylor
series becomes an ordinary series, {\it i.e.}, a sum of some sequence of numbers:
$$ e^1 = 1 + \frac{1}{1!} + \frac{1^2}{2!} + \frac{1^3}{3!} + \ldots  $$
While this series is the sum of an infinitely long sequence, it actually is
a number, and it often suffices to add just a few of the first few terms to
have an idea what the number is. 

The key to computing a Taylor series is to formulate each term in the
underlying sequence as a function of {{\tt x}\/} and its position
{{\tt i}\/}. In our running example, the Taylor sequence for the exponential
function, has the shape 
$$ \frac{x^i}{i!} \ . $$
Assuming a fixed {{\tt x}\/}, here is an equivalent Scheme definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt e-taylor}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (e-taylor}\  {\tt i)}
\\
\>{\tt (/}\  {\tt (expt}\  {\tt x}\  {\tt i)}\  {\tt (!}\  {\tt i)))}
\\
\hbox{;; {{\tt !}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (!}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt 0)}\  {\tt 1\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (*}\  {\tt n}\  {\tt (!}\  {\tt (sub1}\  {\tt n)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The first function computes the term; the second computes the factorial of
a natural number. To compute the value of $e^x$, we now just need to ask
for {{\tt (series}\ {\tt 10}\ {\tt e-taylor)}\/}, assuming we want the first {{\tt 10}\/}
items of the sequence included. 

Putting everything together, we can define a function that computes the
{{\tt x}\/}th power of {{\tt e}\/}. Since the function requires two
auxiliaries, we use a {{\tt local}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (e-power}\  {\tt x)}\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (d}\={\tt efine}\  {\tt (e-taylor}\  {\tt i)}
\\
\>\>\>\>{\tt (/}\  {\tt (expt}\  {\tt x}\  {\tt i)}\  {\tt (!}\  {\tt i)))}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (!}\  {\tt n)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt 0)}\  {\tt 1\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (*}\  {\tt n}\  {\tt (!}\  {\tt (sub1}\  {\tt n)))\char'135}{\tt )))}
\\
\>\>{\tt (series}\  {\tt 10}\  {\tt e-taylor)))}
\end{tabbing}\end{minipage}\end{center}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 23.3.6}

 \label{ex:e-taylor}
Replace {{\tt 10}\/} by {{\tt 3}\/} in the definition of {{\tt e-power}\/} and
evaluate {{\tt (e-power}\ {\tt 1)}\/} by hand. Show only those lines that contain
new applications of {{\tt e-taylor}\/} to a number.

The results of {{\tt e-power}\/} are fractions with large numerators and
denominators. In contrast, Scheme's built-in scheme{exp} function produces
an inexact number. We can turn exact fractions into inexact numbers with
the following function 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\mbox{{\sf exact}}{{}{\tt ->}{}}\mbox{{\sf inexact}}}\ {\tt :}\ {\tt number}\ {\tt \char'133}{\tt exact\char'135}\ {}{\tt ->}{}\ {\tt number}\ {\tt \char'133}{\tt inexact\char'135}\ \/}}\end{tabbing}\end{minipage}\end{center}
Test the function and add it to {{\tt e-power}\/}'s body. Then compare the
results of {{\tt exp}\/} and {{\tt e-power}\/}. Increase the number of items
in the series until the difference between the results is
small.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/e-taylor.html}{\rule{3pt}{5pt}} 


{\bf Exercise 23.3.7}

 \label{ex:ln-taylor}
Develop the function {{\tt ln}\/}, which computes the Taylor series for the
natural logarithm. The mathematical definition of the series is 
$$
  {\it ln\/}(x)   =   2 \cdot [(\frac{x-1}{x+1}) 
		+ \frac13\cdot(\frac{x-1}{x+1})^3
		+ \frac15\cdot(\frac{x-1}{x+1})^5 \ldots] 
$$
This Taylor series has a value for all {{\tt x}\/} that are greater than
{{\tt 0}\/}. 

DrScheme also provides {{\tt log}\/}, a primitive for computing the natural
logarithm. Compare the results for {{\tt ln}\/} and {{\tt log}\/}. Then use
{{\mbox{{\sf exact}}{{}{\tt ->}{}}\mbox{{\sf inexact}}}\/} (see exercise~\ref{ex:e-taylor}) to get results
that are easier to compare.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ln-taylor.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.3.8}

 \label{ex:sin-taylor}
Develop the function {{\tt my-sin}\/}, which computes the Taylor series for
{{\tt sin}\/}, one of the trigonometric functions. The Taylor series is
defined as follows: 
$$
   {\it sin\/}(x)   =      \frac{x}{1!} 
	                 - \frac{x^3}{3!} 
	  	         + \frac{x^5}{5!} 
		         - \frac{x^7}{7!} 
			 \ldots
$$
It is defined for all {{\tt x}\/}. 

\noindent{\bf Hint:} \ The sign of a term is positive if the index is even and negative
otherwise. Programmers use {{\tt cond}\/} to write this down; mathematicians
use the following trick: $(-1)^i$ to compute the
sign.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sin-taylor.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.3.9}

 \label{ex:pi-taylor}
Mathematicians have used series to determine the value of $\pi$ for many
centuries. Here is the first such sequence, discovered by Gregory (1638--1675):
$$ \pi = 4 \cdot [1 - \frac{1}3 + \frac15 - \frac{1}7 \ldots ] \ . $$
Define the function {{\tt greg}\/}, which maps a natural number to the
corresponding term in this sequence. Then use {{\tt series}\/} to determine
approximations of the value of $\pi$.

\medskip\noindent{\bf Note on $\pi$}:\  The approximation improves as we increase the number of items
in the series. Unfortunately, it is not practical to compute $\pi$ with
this definition.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/pi-taylor.html}{\rule{3pt}{5pt}}


\subsection{The Area Under a Function} \label{sec:integrate1}

Consider the function graph in figure~\ref{fig:func-int}. Suppose we wish
to know the area between the {\it x\/}-axis, the fat lines labeled {\it a\/} and {\it b\/}, and the graph.  Determining the area under the graph of
a function for some specific interval is called {\sl integrating a function\/}. Since engineers had to solve this kind of problem before
computers were available, mathematicians have studied it extensively. For a
small class of functions, it is indeed possible to determine the area
exactly. For the other cases, mathematicians have developed several methods
to determine close estimates.  Since these methods involve lots of
mechanical calculations, they are natural candidates for computer
functions. 

\hrule
\ \ \ {\htmladdimg{../icons/integrate.jpg}}\\
\center{Figure: Integrating a function {\it f\/} between {\it a\/} and {\it b\/}} \label{fig:func-int}
\hrule

A general integration function must consume three inputs: {{\tt a}\/},
{{\tt b}\/}, and the function {{\tt f}\/}. The fourth part, the {\it x\/}-axis, is implied.  This suggests the following contract:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt integrate}\ {\tt :}\ {\tt (number}\ {}{\tt ->}{}\ {\tt number)}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the area under the graph of {{\tt f}\/} between {{\tt a}\/} and {{\tt b}\/}}
\\
{\tt (define}\  {\tt (integrate}\  {\tt f}\  {\tt a}\  {\tt b)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

Kepler suggested one simple integration method. It consists of three steps:
\begin{enumerate}
\item divide the interval into two parts: 
	$[a,\frac{a+b}2]$ and $[\frac{a+b}2,b]$;
\item compute the area of each {\sl trapezoid\/}; and
\item add the two areas to get an estimate at the integral. 
\end{enumerate}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 23.4.1}

 \label{ex:integral-kepler}
Develop the function {{\tt integrate-kepler}\/}. It coomputes the area under
some the graph of some function {{\tt f}\/} between {{\tt left}\/} and
{{\tt right}\/} using Kepler's rule.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/integral-kepler.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Another simple method is to think of the area as a sequence of many small
rectangles. Each rectangle is as tall as the function graph in, say, the
middle of the rectangle. Figure~\ref{fig:func-int} shows two examples. By
adding up the area of the rectangles, we get a good estimate at the area
under the graph. The more rectangles we consider, the closer the estimate
is to the actual area.

Let us agree that {{\tt R}\/} stands for the number of rectangles that we
wish to consider. To determine how large these rectangles are, we need to
figure out how large their sides are. The length of the side on the {\it x\/}-axis is the length of the interval divided by the number of
rectangles: 
$$ \mbox{{\it width\/}} = \frac{(b - a)}R  \ . $$
For the height of the rectangle, we need to determine its midpoint and then
the value of {\it f\/} at the midpoint. The first midpoint is clearly at 
$a$ plus half of the width of the rectangle, {\it i.e.}, if 
$$ \mbox{{\it step\/} } = \frac{\mbox{\it width\/}} 2 \ ,$$
the area is 
$$ W \cdot f(a + S) \ , $$
where {\it W\/} stands for {\it width\/} and {\it S\/} for {\it step\/}
from now on. 

To get from the rectangle starting at {{\tt a}\/} to the next one on the
right, we must add the width of one rectangle. That is, the next midpoint
(called $x_1$ in figure~\ref{fig:func-int}) is at 
$$ a + W + S \ ,$$ 
the third one at
$$ a + 2 \cdot W + S \ , $$
and so on. 
The following table explains the three sequences that are involved in the
usual manner: 
$$ 
\begin{array}{l|c|c|c|c}
 \mbox{\it index\/}        & 0     & 1                 & 2                 &  \ldots \\
 \hline
 \mbox{{\it M\/}}   & a + S & a + 1 \cdot W + S & a + 2 \cdot W + S &  \ldots \\
 
 \mbox{\it f at M\/}      & f(a+S)& f(a+ 1\cdot W +S) & f(a+ 2\cdot W +S) &  \ldots \\
 \mbox{\it area \/}        & W \cdot f(a+S)& W \cdot f(a+ 1\cdot W +S) & W \cdot f(a+ 2\cdot W +S) &  \ldots 
\end{array} 
$$
In the second row, {\it M\/} stands for midpoint. The first rectangle has
index 0, the last one $R - 1$.  

Using this sequence of rectangles, we can now determine the area under the
graph as a series: 
$$ \sum_{i=0}^{i=R-1} \mbox{\it area-of-rectangle\/}(i) \ . $$

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 23.4.2}

 \label{ex:integral-rect}
Develop the function {{\tt integrate}\/}. It coomputes the area under
some the graph of some function {{\tt f}\/} between {{\tt left}\/} and
{{\tt right}\/} using the rectangle-series method.

Use test cases for {{\tt f}\/}, {{\tt a}\/}, and {{\tt b}\/} for which one can
determine the area exactly and easily by hand, {\it e.g.}, {{\tt (define}\ {\tt (id}\ {\tt x)}{ }{\tt x)}\/}.  Compare the results with those of {{\tt integrate}\/} from
exercise~\ref{ex:integral-kepler}.

Make {{\tt R}\/} a top-level constant: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt R}\/} : number of rectangles to approximate integral}\\
{\tt (define}\  {\tt R}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Test {{\tt integrate}\/} on {{\tt sin}\/} and increase {{\tt R}\/}
gradually from {{\tt 10}\/} to {{\tt 10000}\/}. What happens to the
result?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/integral-rect.html}{\rule{3pt}{5pt}}


\subsection{The Slope of a Function} \label{sec:diff1}

Let us take another look at the function graph in
figure~\ref{fig:func-int}.  \htmladdimg{../icons/teacher.gif}~{\em This subsection introduces the notion of derivative in a computational manner. It relies on a good understanding of lines (as functions) and a basic understanding of functions and function graphs. The exercises are simpler than the concepts.}  For many problems,
we need to be able to draw a line that has the same slope as some curve at
a certain point. Indeed, computing the slope is often the true goal. In
economic problems, the slope is the growth rate of a company if the curve
represents the income over time. In a physcial problem, the curve could
represent the acceleration of some object; its slope, at any point, is then
the current velocity of the object.

Determining the slope of some function $f$ at some point $x$ means to {\sl differentiate\/} the function.  The differential operator (also called a
functional) returns a function $f'$ (pronounced ``f prime''). It tells us
for any $x$ what the slope of $f$ is at that point. Computing $f'$ is
complicated, so it is again a good task for a computer program. The program
consumes some function $f$ and produces $f'$.

\hrule
\ \ \ {\htmladdimg{../icons/differentiate.jpg}}\\
\center{Figure: The graph of some function} \label{fig:func-diff}
\hrule


To design a ``differentiator'' we must study how we could construct lines
that have the same slope as a curve. In principle, such a line touches the
curve at just that point.  But suppose we relax this constraint for a
moment and look at straight lines that intersect the curve { close to\/}
the point of interest. We pick two points that are equally far away from
{\it x\/}, say, $x - \mbox{\it eps\/}$ and $x + \mbox{\it eps\/}$; the constant $\mbox{\it eps\/}$, pronounced
epsilon, represents some small distance. Using the two corresponding points
on the curve, we can determine a straight line that has the proper slope. 

The situation is sketched in figure~\ref{fig:func-diff}. If the point
of interest has coordinate {\it x\/}, the two points are $(x,f(x-\mbox{\it eps\/}))$
and $(x,f(x+\mbox{\it eps\/}))$. Hence, the slope of the line is 
$$ 
   \begin{array}{c}
	f(x+\mbox{\it eps\/}) - f(x-\mbox{\it eps\/}) \\
 
        \hline
        2 \cdot \mbox{\it eps\/}
   \end{array} 
$$
that is, the difference between the height of the right point and the left
point divided by their horizontal distance.  Determining the line from the
slope and one of the points or even from two points is an exercise.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 23.5.1}

 \label{ex:tangent1}
The equation for a line is 
$$ y(x) = a \cdot x + b \ . $$
By now, it is straightforward to translate this equation into Scheme: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (y}\  {\tt x)}\\
\>{\tt (+}\  {\tt (*}\  {\tt a}\  {\tt x)}\  {\tt b))}
\end{tabbing}\end{minipage}\end{center}
To obtain a concrete line we must replace {{\tt a}\/} and {{\tt b}\/} with
numbers. 

The teachpack {\tt\bf graphing.ss} provides two operations for drawing lines:
{{\tt graph-line}\/}. The operation consumes a line like {{\tt y}\/} and a
color, say, {{\tt RED}\/}. Use {{\tt graph-line}\/} to draw the graphs of the
following lines:
\begin{enumerate}
\item $y_1(x) = x + 4$
\item $y_2(x) = 4 - x$
\item $y_3(x) = x + 10$
\item $y_4(x) = 10 - x$
\item $y_5(x) = 12$
\end{enumerate}
~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tangent1.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.5.2}

 \label{ex:tangent2}
It is a standard mathematical exercise to develop the equation for a line
from a point on the line and its slope. Look up the method in your
mathematics book. Then develop the function {{\tt line-from-point+slope}\/},
which implements the method. The function consumes a {{\tt posn}\/} (the
point) and a number (the slope). It produces a function that represents the
line in the spirit of exercise~\ref{ex:tangent1}. 

Testing a function-producing function like {{\tt line-from-point+slope}\/}
can be done in two ways. Suppose we apply the function to $(0,4)$ and 1. The
result should be line $y_1$ from exercise~\ref{ex:tangent1}. To check this,
we can either apply the result of
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (line-from-point+slope}\  {\tt (make-posn}\  {\tt 0}\  {\tt 4)}\  {\tt 1)}\end{tabbing}\end{minipage}\end{center}
to some numbers, or we can draw the result using the operations in
{\tt\bf graphing.ss}. In the first case, we must use $y_1$ to compare
outputs; in the second case we can draw the result in one color and the
hand-constructed line in a different one and observe the
effect.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tangent2.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Once we have an intersecting line through $(x,f(x-\mbox{\it eps\/}))$ and
$(x,f(x+\mbox{\it eps\/}))$, it is basically obvious how to get a line with the proper
slope. By decreasing $\mbox{\it eps\/}$ until it is (almost) indistinguishable from 0,
the two intersection points move closer and closer until they are one,
namely, $(x,f(x))$, the point for which we wish to know the
slope.\footnote{The process of decreasing $\mbox{\it eps\/}$ to 0 is a convergence (or limit) process. It does not always succeed, that is, for some function graphs, the process does not end properly. We ignore those cases, but they are common and require special attention.}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 23.5.3}

 \label{ex:tangent3}
Use the operation {{\tt graph-fun}\/} in the teachpack {\tt\bf graphing.ss}
to draw the mathematical function
$$ y(x) = x^2 - 4 \cdot x + 7 \ .$$
The operation works just like {{\tt draw-line}\/} (see
exercise~\ref{ex:tangent1}. 

Suppose we wish to determine the slope at $x = 2$.  Pick an $\mbox{\it eps\/} > 0$ and
determine the slope of the line that goes through $(x,f(x-\mbox{\it eps\/}))$ and
$(x,f(x+\mbox{\it eps\/}))$ with the above formula. Compute the line with
{{\tt line-from-point+slope}\/} from exercise~\ref{ex:tangent2} and use
{{\tt draw-line}\/} to draw it into the same coordinate system as
{{\tt y}\/}. Repeat the process with ${\mbox{\it eps\/}}/2$ and then with
${\mbox{\it eps\/}}/4$.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tangent3.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


If our goal is to define the differential operator as a Scheme function, 
we can approximate it by setting $\mbox{\it eps\/}$ to a small number and by
translating the mathematical formula into a Scheme expression: 
\htmladdimg{../icons/teacher.gif}~{\em We use an unusal name here: {{\tt d/dx}\/}. As we have mentioned before, Scheme permits programmers to put many more symbols into a variable name than other languages. Since mathematics uses  $$\frac{{\rm d}}{{\rm dx}}$$ as the name for the differential operator, we choose a name that is as close as possible.}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt d/dx}\ {\tt :}\ {\tt (num}\ {}{\tt ->}{}\ {\tt num)}\ {}{\tt ->}{}\ {\tt (num}\ {}{\tt ->}{}\ {\tt num)}\/}}\\
\hbox{;; to compute the derivative function of {{\tt f}\/} numerically}
\\
{\tt (d}\={\tt efine}\  {\tt (d/dx}\  {\tt f)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (d}\={\tt efine}\  {\tt (fprime}\  {\tt x)}
\\
\>\>\>\>{\tt (/}\  \={\tt (-}\  {\tt (f}\  {\tt (+}\  {\tt x}\  \hbox{$\mbox{\it eps\/}$}{\tt ))}\  {\tt (f}\  {\tt (-}\  {\tt x}\  \hbox{$\mbox{\it eps\/}$}{\tt )))}
\\
\>\>\>\>\>{\tt (*}\  {\tt 2}\  \hbox{$\mbox{\it eps\/}$}{\tt )))}
\\
\>\>\>{\tt (define}\  \hbox{$\mbox{\it eps\/}$}\  {\tt ...))}
\\
\>\>{\tt fprime))}
\end{tabbing}\end{minipage}\end{center}
explain the right-hand side of arrow

As mentioned in the introduction to this section, the differential operator
computes the function $f'$ from some function $f$. The former computes the
slope of $f$ for any {\it x\/}. For straight lines, the slope is always
known. Hence, a function that represents a straight line is an ideal test
case for {{\tt d/dx}\/}. Let us consider
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (a-line}\  {\tt x)}\\
\>{\tt (+}\  {\tt (*}\  {\tt 3}\  {\tt x)}\  {\tt 1))}
\end{tabbing}\end{minipage}\end{center}
The evaluation of {{\tt (d/dx}\ {\tt a-line)}\/} proceeds as follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (d/dx}\  {\tt a-line)}\\
{\tt =}\  {\tt (l}\={\tt ocal}\  {\tt (}\={\tt (d}\={\tt efine}\  {\tt (fprime}\  {\tt x)}
\\
\>\>\>{\tt (/}\  \={\tt (-}\  {\tt (a-line}\  {\tt (+}\  {\tt x}\  \hbox{$\mbox{\it eps\/}$}{\tt ))}\  {\tt (a-line}\  {\tt (-}\  {\tt x}\  \hbox{$\mbox{\it eps\/}$}{\tt )))}
\\
\>\>\>\>{\tt (*}\  {\tt 2}\  \hbox{$\mbox{\it eps\/}$}{\tt )))}
\\
\>\>{\tt (define}\  \hbox{$\mbox{\it eps\/}$}\  {\tt ...))}
\\
\>{\tt fprime)}
\\
{\tt =}\  \={\tt (d}\={\tt efine}\  {\tt (fprime}\  {\tt x)}
\\
\>\>{\tt (/}\  \={\tt (-}\  {\tt (a-line}\  {\tt (+}\  {\tt x}\  \hbox{$\mbox{\it eps\/}$}{\tt ))}\  {\tt (a-line}\  {\tt (-}\  {\tt x}\  \hbox{$\mbox{\it eps\/}$}{\tt )))}
\\
\>\>\>{\tt (*}\  {\tt 2}\  \hbox{$\mbox{\it eps\/}$}{\tt )))}
\\
\>{\tt (define}\  \hbox{$\mbox{\it eps\/}$}\  {\tt ...)}
\\
\>{\tt fprime}
\end{tabbing}\end{minipage}\end{center}

Now, if we think of {{\tt (+}\ {\tt x}\ \hbox{$\mbox{\it eps\/}$}{\tt )}\/} and {{\tt (-}\ {\tt x}\ \hbox{$\mbox{\it eps\/}$}{\tt )}\/} as
numbers, we can evaluate the application of {{\tt a-line}\/} in the
definition of {{\tt fprime}\/}:\footnote{If {{\tt x}\/} is a number, then adding or subtracting $\mbox{\it eps\/}$ yields a number. If, by accident, we apply {{\tt fprime}\/} to something else, both expressions signal an error. It is therefore acceptable to act as if the expressions were values. In general, this is not true.}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (d}\={\tt efine}\  {\tt (fprime}\  {\tt x)}\\
\>{\tt (/}\  \={\tt (-}\  {\tt (+}\  {\tt (*}\  {\tt 3}\  {\tt (+}\  {\tt x}\  \hbox{$\mbox{\it eps\/}$}{\tt ))}\  {\tt 1)}\  {\tt (+}\  {\tt (*}\  {\tt 3}\  {\tt (-}\  {\tt x}\  \hbox{$\mbox{\it eps\/}$}{\tt ))}\  {\tt 1))}
\\
\>\>{\tt (*}\  {\tt 2}\  \hbox{$\mbox{\it eps\/}$}{\tt )))}
\\
{\tt =}\  {\tt (d}\={\tt efine}\  {\tt (fprime}\  {\tt x)}
\\
\>{\tt (/}\  \={\tt (-}\  {\tt (*}\  {\tt 3}\  {\tt (+}\  {\tt x}\  \hbox{$\mbox{\it eps\/}$}{\tt ))}\  {\tt (*}\  {\tt 3}\  {\tt (-}\  {\tt x}\  \hbox{$\mbox{\it eps\/}$}{\tt )))}
\\
\>\>{\tt (*}\  {\tt 2}\  \hbox{$\mbox{\it eps\/}$}{\tt )))}
\\
{\tt =}\  {\tt (d}\={\tt efine}\  {\tt (fprime}\  {\tt x)}
\\
\>{\tt (/}\  \={\tt (*}\  {\tt 3}\  {\tt (-}\  {\tt (+}\  {\tt x}\  \hbox{$\mbox{\it eps\/}$}{\tt )}\  {\tt (-}\  {\tt x}\  \hbox{$\mbox{\it eps\/}$}{\tt )))}
\\
\>\>{\tt (*}\  {\tt 2}\  \hbox{$\mbox{\it eps\/}$}{\tt )))}
\\
{\tt =}\  {\tt (d}\={\tt efine}\  {\tt (fprime}\  {\tt x)}
\\
\>{\tt (/}\  \={\tt (*}\  {\tt 3}\  {\tt (*}\  {\tt 2}\  \hbox{$\mbox{\it eps\/}$}{\tt ))}
\\
\>\>{\tt (*}\  {\tt 2}\  \hbox{$\mbox{\it eps\/}$}{\tt )))}
\\
{\tt =}\  {\tt (d}\={\tt efine}\  {\tt (fprime}\  {\tt x)}
\\
\>{\tt 3)}
\end{tabbing}\end{minipage}\end{center}
In other words, the result of {{\tt (d/dx}\ {\tt a-line)}\/} always returns 3,
which is the slope of {{\tt a-line}\/}. In short, we not only got a close
approximation because $\mbox{\it eps\/}$ is small, we actually got the correct answer.
In general, however, the answer will be depend on {\hbox{$\mbox{\it eps\/}$}\/} and will
not be precise. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 23.5.4}

 \label{ex:tangent4}
Pick a small $\mbox{\it eps\/}$ and use {{\tt d/dx}\/} to compute the slope of 
$$ y(x) = x^2 - 4 \cdot x + 7 $$
at $x = 2$. How does the result compare with your calculation in
exercise~\ref{ex:tangent3}?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tangent4.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.5.5}

 \label{ex:line1}
Develop the function {{\tt line-from-two-points}\/}. It consumes two points
{{\tt p1}\/} and {{\tt p2}\/}. Its result is a Scheme function that
represents the line through {{\tt p1}\/} and {{\tt p2}\/}.

\noindent Question: Are there any situations for which this function may
fail to compute a function? If so, refine the definition to produce a
proper error message in this case.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/line1.html}{\rule{3pt}{5pt}}


{\bf Exercise 23.5.6}

 \label{ex:tangent5}
Compute the slope of the following function 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt x)}\  \\
\>{\tt (+}\  \={\tt (*}\  {\tt 1/60}\  {\tt (*}\  {\tt x}\  {\tt x}\  {\tt x))}
\\
\>\>{\tt (*}\  {\tt -1/10}\  {\tt (*}\  {\tt x}\  {\tt x))}
\\
\>\>{\tt 5))}
\end{tabbing}\end{minipage}\end{center}
at $x = 4$. Set $\mbox{\it eps\/}$ to {{\tt 2}\/}, {{\tt 1}\/}, {{\tt .5}\/}. Try the same
for some other value of {{\tt x}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tangent5.html}{\rule{3pt}{5pt}}







%% BEGIN INLINED FILE: curriculumC.dl.tex

 \part{{Intermezzo 4: Defining Functions on the Fly}}  \section{{Intermezzo 4: Defining Functions on the Fly}}  
 \label{sec:int-lambda}
Many uses of abstract functions require the definition of auxiliary
 functions. Consider {{\tt filter1}\/}, which consumes a filtering function,
 a list, and a filtering item. In the previous section alone, we
 encountered three uses of {{\tt filter1}\/} with three different auxiliary
 functions: {{\tt squared\char'077}\/}, {$<_{ir}$\/}, and {{\tt eq-ir\char'077}\/}.

Since these auxiliary functions are only used as arguments to
 {{\tt filter1}\/}, we should employ the program organization guidelines
 from the preceding intermezzo~(\ref{sec:int-local}). That is, we should
 use a {{\bf local}-exp\-res\-sion} to indicate that the application of
 {{\tt filter1}\/} and the auxiliary function definition belong together.
 Here is one possible arrangement for the {{\tt filter1}\/}{{\tt -eq-ir\char'077}\/}
 combination: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt find}\ {\tt :}\ {\tt list-of-IRs}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (find}\  {\tt aloir}\  {\tt t)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (eq-ir\char'077}\  {\tt ir}\  {\tt p)}
\\
\>\>\>{\tt (symbol=?}\  {\tt (ir-name}\  {\tt ir)}\  {\tt p)))}
\\
\>\>{\tt (filter1}\  {\tt eq-ir\char'077}\  {\tt aloir}\  {\tt t)))}
\end{tabbing}\end{minipage}\end{center}

An alternative arrangement places the {{\bf local}-exp\-res\-sion} where the
 function is needed:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt find}\ {\tt :}\ {\tt list-of-IRs}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (find}\  {\tt aloir}\  {\tt t)}
\\
\>{\tt (filter1}\  \={\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (eq-ir\char'077}\  {\tt ir}\  {\tt p)}
\\
\>\>\>\>{\tt (symbol=?}\  {\tt (ir-name}\  {\tt ir)}\  {\tt p)))}
\\
\>\>\>{\tt eq-ir\char'077}{\tt )}
\\
\>\>{\tt aloir}\  {\tt t))}
\end{tabbing}\end{minipage}\end{center}
 This alternative is feasible because the names of functions---like
 {{\tt eq-ir\char'077}\/}---are now legitimate expressions and can play the 
 role of {{\tt local}\/}'s body. Thus, the {{\bf local}-exp\-res\-sion}
 introduces a function definition and returns the function as its result. 

Because good programmers use abstract functions and organize their programs
 in a tidy manner, it is not surprising that Scheme provides a short-hand
 for this particular, frequent use of {{\tt local}\/}. The short-hand is
 called a {{\bf lambda}-exp\-res\-sion} and greatly facilitates the
 introduction of functions like {{\tt eq-ir\char'077}\/}, {{\tt squared\char'077}\/}, or
 {$<_{ir}$\/}. The following two subsections introduce the syntax and
 semantics of {{\bf lambda}-exp\-res\-sion}s. The last subsection discusses
 its pragmatics. 

 \subsection{Syntax of {\bf lambda}} \label{sec:syn-lambda} 
A {{\bf lambda}-exp\-res\-sion} is just a new form of expression:
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{exp}>}} &=& ({{\tt lambda}\/} (\mbox{{\tt <{var}>}} ... \mbox{{\tt <{var}>}}) \mbox{{\tt <{exp}>}})\\
\end{tabular}
\end{center}
 Its distinguishing characteristic is the keyword {\bf lambda}. It is
 followed by a sequence of variables, enclosed in {{\tt (}\/} and
 {{\tt )}\/}. The last component is an expression.

Here are three useful examples: 
\begin{enumerate}
\item {{\tt (lambda}\ {\tt (x}\ {\tt c)}\ {\tt (>}\ {\tt (*}\ {\tt x}\ {\tt x)}\ {\tt c))}\/}
\item {{\tt (lambda}\ {\tt (ir}\ {\tt p)}\ {\tt (<}\ {\tt (ir-price}\ {\tt ir)}\ {\tt p))}\/}
\item {{\tt (lambda}\ {\tt (ir}\ {\tt p)}\ {\tt (symbol=?}\ {\tt (ir-name}\ {\tt ir)}\ {\tt p))}\/}
\end{enumerate}
 They correspond to {{\tt squared\char'077}\/}, {$<_{ir}$\/}, and {{\tt eq-ir\char'077}\/},
 respectively, the three motivating examples discussed above.

A {{\bf lambda}-exp\-res\-sion} defines an anonymous function, that is, a
 function without a name. The sequence of variables behind {\bf  lambda} are the function's parameters; the third component is the
 function's body.

 \subsection{Scope and Semantics of {\bf lambda}} \label{sec:sem-lambda} 
As discussed in the introduction, a {{\bf lambda}-exp\-res\-sion} is just a
 short-hand for a {{\bf local}-exp\-res\-sion}. In general, we can think of 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (lambda}\  {\tt (x-1}\  {\tt ...}\  \  {\tt x-n)}\  {\tt exp)}\end{tabbing}\end{minipage}
\end{center}
 as
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt (a-new-name}\  {\tt x-1}\  {\tt ...}\  {\tt x-n)}\  {\tt exp))}\\
\>{\tt a-new-name)}
\end{tabbing}\end{minipage}
\end{center}
 The name of the function, {{\tt a-new-name}\/} may not occur in
 {{\tt exp}\/}.

The short-hand explanation suggests that 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (lambda}\  {\tt (x-1}\  {\tt ...}\  \  {\tt x-n)}\  {\tt exp)}\end{tabbing}\end{minipage}
\end{center}
 introduces {{\tt x-1}\/} \ldots {{\tt x-n}\/} as binding occurrences and that 
 the scope of parameters is {{\tt exp}\/}. Of course, if {{\tt exp}\/} contains
 further binding constructs (say a nested {{\bf local}-exp\-res\-sion}), then
 the scope of the variables may have a hole. 

Similarly, the explanation implies what basic facts govern the evaluation of
{{\bf lambda}-exp\-res\-sion}s:
\begin{enumerate}
 \item A {{\bf lambda}-exp\-res\-sion} is a value because functions are values.

 \item The application of {{\bf lambda}-exp\-res\-sion}s to values proceeds
 according to our usual laws of function application, assuming
 we expand the short-hand first.
\end{enumerate}
 Here is a sample use of {{\tt lambda}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (filter1}\  \={\tt (lambda}\  {\tt (ir}\  {\tt p)}\  {\tt (<}\  {\tt (ir-price}\  {\tt ir)}\  {\tt p))}\\
\>{\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 10))}
\\
\>{\tt 8)}
\end{tabbing}\end{minipage}\end{center}
 The application of {{\tt filter1}\/} consumes the
 {{\bf lambda}-exp\-res\-sion}, a (short) list of inventory records, and a
 threshold. Given our suggestion, the evaluation can be understood by an
 expansion of the {{\bf lambda}-exp\-res\-sion} into a corresponding
 {{\bf local}-exp\-res\-sion}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt ...}\\
{\tt =}\  {\tt (filter1}\  \={\tt (local}\  {\tt ((define}\  {\tt (}$<_{ir}$\  {\tt ir}\  {\tt p)}\  {\tt (<}\  {\tt (ir-price}\  {\tt ir)}\  {\tt p)))}\  $<_{ir}${\tt )}
\\
\>{\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 10))}
\\
\>{\tt 8)}
\\
{\tt =}\  {\tt (filter1}\  \=$<_{ir}$\  
\\
\>{\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 10))}
\\
\>{\tt 8)}
\end{tabbing}\end{minipage}\end{center}
 For the last step, the local definition of {$<_{ir}$\/} is lifted and
 added to the top-level definitions. From here, the evaluation proceeds 
 as in section~\ref{sec:dd-similarities}.


While it is natural to think of {{\tt lambda}\/} as a short-hand, the last
 two points also suggest a way of understanding {{\bf lambda}-exp\-res\-sion}s
 directly. In particular, we can adapt the law of application to
 {{\bf lambda}-exp\-res\-sion}s:\footnote{Indeed, one can study a simplified  version of Scheme ({\it i.e.}, a model) with just expressions and this single law  of application of {\bf lambda}-exp\-res\-sion. This model is known as (a  variant of) Church's $\lambda$-calculus and was invented in the late 1920s  as a tool to study the foundations of mathematics and computation.}

\begin{center}
  \begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (}\={\tt (lambda}\  {\tt (x-1}\  {\tt ...}\  {\tt x-n)}\  {\tt exp)}\  \  \  \\
\>\>{\tt val-1}\  {\tt ..}\={\tt .}\  {\tt val-n)}\  \  
\\
\>\relax
\>\end{tabbing}\end{minipage}~\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt =}\  \={\tt ex}\={\tt p}\\
\>\hbox{$\mbox{with all occurrences of {{\tt x-1}\ {\tt ...}\ {\tt x-n}\/}}$}
\\
\>\hbox{$\mbox{replaced by {{\tt val-1}\ {\tt ...}\ {\tt val-n}\/}}$}
\>\end{tabbing}\end{minipage}
\end{center}
 That is, the application of a {{\bf lambda}-exp\-res\-sion} proceeds just
 like that of an ordinary function. We replace the parameters of the
 function with the actual argument values and compute the value of the
 function body. 

Let us reconsider the above example in this light: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (filter1}\  \={\tt (lambda}\  {\tt (ir}\  {\tt p)}\  {\tt (<}\  {\tt (ir-price}\  {\tt ir)}\  {\tt p))}\\
\>{\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 10))}
\\
\>{\tt 8)}
\\
\hbox{;As usually, this application is replaced by the body of {{\tt filter1}\/}}
\\
\hbox{;with all parameters replaced by their values. This step places a}
\\
\hbox{;{{\bf lambda}-exp\-res\-sion} into the function position of an application:}
\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}\={\tt (}\={\tt (lambda}\  {\tt (ir}\  {\tt p)}\  {\tt (<}\  {\tt (ir-price}\  {\tt ir)}\  {\tt p))}
\\
\>\>\>{\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 10)}\  {\tt 8)}\  
\\
\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 10)))}
\\
\>\>\>{\tt (filter1}\  \={\tt (lambda}\  {\tt (ir}\  {\tt p)}\  {\tt (<}\  {\tt (ir-price}\  {\tt ir)}\  {\tt p))}
\\
\>\>\>\>{\tt (rest}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 10)))}
\\
\>\>\>\>{\tt 8))\char'135}
\\
\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>{\tt (filter1}\  \={\tt (lambda}\  {\tt (ir}\  {\tt p)}\  {\tt (<}\  {\tt (ir-price}\  {\tt ir)}\  {\tt p))}
\\
\>\>\>{\tt (rest}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 10)))}
\\
\>\>\>{\tt 8)\char'135}{\tt )}
\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}\={\tt (<}\  {\tt (ir-price}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 10))}\  {\tt 8)}
\\
\>\>{\tt (c}\={\tt ons}\  {\tt (first}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 10)))}
\\
\>\>\>{\tt (filter1}\  \={\tt (lambda}\  {\tt (ir}\  {\tt p)}\  {\tt (<}\  {\tt (ir-price}\  {\tt ir)}\  {\tt p))}
\\
\>\>\>\>{\tt (rest}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 10)))}
\\
\>\>\>\>{\tt 8))\char'135}
\\
\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>{\tt (filter1}\  \={\tt (lambda}\  {\tt (ir}\  {\tt p)}\  {\tt (<}\  {\tt (ir-price}\  {\tt ir)}\  {\tt p))}
\\
\>\>\>{\tt (rest}\  {\tt (list}\  {\tt (make-ir}\  {\tt \char'047}{\tt doll}\  {\tt 10)))}
\\
\>\>\>{\tt 8)\char'135}{\tt )}
\\
{\tt =}\  {\tt ...}
\end{tabbing}\end{minipage}\end{center}
 From here, the evaluation proceeds as usual. Still, even this short
 hand-evaluation shows that, while using {{\bf lambda}-exp\-res\-sion}s in
 programs is convenient, replacing it with a named function (often)
 simplifies calculations.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 24.2.1}

 \label{ex:lambda1}
Decide which of the following phrases are legal {{\bf lambda}-exp\-res\-sion}s: 
\begin{enumerate}
\item {{\tt (lambda}\ {\tt (x}\ {\tt y)}\ {\tt (x}\ {\tt y}\ {\tt y))}\/} 
\item {{\tt (lambda}\ {\tt ()}\ {\tt 10)}\/} 
\item {{\tt (lambda}\ {\tt (x)}\ {\tt x)}\/}
\item {{\tt (lambda}\ {\tt (x}\ {\tt y)}\ {\tt x)}\/}
\item {{\tt (lambda}\ {\tt x}\ {\tt 10)}\/}
\end{enumerate}
Explain why they are legal or illegal. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/lambda1.html}{\rule{3pt}{5pt}}


{\bf Exercise 24.2.2}

 \label{ex:lambda2}
Draw arrows from the underlined occurrences of {{\tt x}\/} to their binding
occurrences in each of the following three {{\bf lambda}-exp\-res\-sion}s: 
\begin{enumerate}
\item ~~\\
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ambda}\  {\tt (x}\  {\tt y)}\\
\>{\tt (+}\  \hbox{$\underline{x}$}\  {\tt (*}\  {\tt x}\  {\tt y)))}
\end{tabbing}\end{minipage}

\item ~~\\
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ambda}\  {\tt (x}\  {\tt y)}\\
\>{\tt (+}\  \=\hbox{$\underline{x}$}
\\
\>\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt x}\  {\tt (*}\  {\tt y}\  {\tt y)))}
\\
\>\>\>{\tt (+}\  \={\tt (*}\  {\tt 3}\  \hbox{$\underline{x}$}{\tt )}
\\
\>\>\>\>{\tt (/}\  {\tt 1}\  {\tt x)))))}
\end{tabbing}\end{minipage}

\item ~~\\
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ambda}\  {\tt (x}\  {\tt y)}\\
\>{\tt (+}\  \=\hbox{$\underline{x}$}
\\
\>\>{\tt (}\={\tt (l}\={\tt ambda}\  {\tt (x)}
\\
\>\>\>\>{\tt (+}\  \={\tt (*}\  {\tt 3}\  \hbox{$\underline{x}$}{\tt )}
\\
\>\>\>\>\>{\tt (/}\  {\tt 1}\  {\tt x)))}
\\
\>\>\>{\tt (*}\  {\tt y}\  {\tt y))))}
\end{tabbing}\end{minipage}
\end{enumerate}
Also draw a box for the corresponding scope of each underlined {{\tt x}\/}
and holes in the scope where necessary. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/lambda2.html}{\rule{3pt}{5pt}}


{\bf Exercise 24.2.3}

 \label{ex:lambda3}
Evaluate the following expressions by hand: 
\begin{enumerate}
\item ~~\\
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (}\={\tt (l}\={\tt ambda}\  {\tt (x}\  {\tt y)}\\
\>\>{\tt (+}\  \hbox{$\underline{x}$}\  {\tt (*}\  {\tt x}\  {\tt y)))}
\\
\>{\tt 1}\  {\tt 2)}
\end{tabbing}\end{minipage}

\item ~~\\
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (}\={\tt (l}\={\tt ambda}\  {\tt (x}\  {\tt y)}\\
\>\>{\tt (+}\  \=\hbox{$\underline{x}$}
\\
\>\>\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt x}\  {\tt (*}\  {\tt y}\  {\tt y)))}
\\
\>\>\>\>{\tt (+}\  \={\tt (*}\  {\tt 3}\  \hbox{$\underline{x}$}{\tt )}
\\
\>\>\>\>\>{\tt (/}\  {\tt 1}\  {\tt x)))))}
\\
\>{\tt 1}\  {\tt 2)}
\end{tabbing}\end{minipage}

\item ~~\\
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (}\={\tt (l}\={\tt ambda}\  {\tt (x}\  {\tt y)}\\
\>\>{\tt (+}\  \=\hbox{$\underline{x}$}
\\
\>\>\>{\tt (}\={\tt (l}\={\tt ambda}\  {\tt (x)}
\\
\>\>\>\>\>{\tt (+}\  \={\tt (*}\  {\tt 3}\  \hbox{$\underline{x}$}{\tt )}
\\
\>\>\>\>\>\>{\tt (/}\  {\tt 1}\  {\tt x)))}
\\
\>\>\>\>{\tt (*}\  {\tt y}\  {\tt y))))}
\\
\>{\tt 1}\  {\tt 2)}
\end{tabbing}\end{minipage}
\end{enumerate}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/lambda3.html}{\rule{3pt}{5pt}}


 \subsection{Pragmatics of {\bf lambda}} \label{sec:pra-lambda} 
The guideline for using {{\bf lambda}-exp\-res\-sion}s is straightforward: 

   \begin{rawhtml}     <table bgcolor=red align=center>       <tr><td><font align=center color=white size=+3>   \end{rawhtml} Guideline on Lambda Expressions   \begin{rawhtml}     <tr><td><font color=white><p>   \end{rawhtml} Use {{\bf lambda}-exp\-res\-sion}s when a   function is not recursive and is only referred to once (in an argument   position).    \begin{rawhtml}     </p></table>   \end{rawhtml} 

\noindent 
 If we were to apply the guideline to the programs in the preceding
 sections, we would quickly see how much {{\tt lambda}\/} simplifies the use
 of abstracted functions. For that reason, Scheme offers many abstracted
 functions in its libraries. In future sections, we will encounter many
 more examples where {{\bf lambda}-exp\-res\-sion}s are convenient.


%% BEGIN INLINED FILE: curriculum4.dl.tex

\newpage\part{Generative Recursion}\thispagestyle{empty}\newpage \label{part:recursion}

\section{Generative Recursion and Algorithms} \label{sec:gen-rec-intro}

The functions we have developed so far fall into two broad categories. On one
 hand, we have the category of functions that encapsulate domain knowledge.
 On the other hand, we have functions that consume structures or lists. These
 functions typically decompose their arguments into their immediate
 structural components and then process those components.  If one of the
 immediate components belong to the same class of data as the input, the
 function is recursive. For that reason, we refer to these functions as
 {\sc {structurally (recursive) functions}}.  In some cases, however, we
 also need functions based on {generative recursion}.  The study of
 generative recursion is as old as mathematics and is often called the study
 of {\sc {algorithms}}.

The inputs of an algorithm represent a {\sl problem\/}.  Except for rare
 occasions, the problem is an instance of a large class of problems and the
 algorithm works for all of these problems. In general, an algorithm
 partitions a problem into other, hopefully smaller problems and solves
 those. For example, a algorithm for planning a vacation trip requires
 arrangements for a trip from our home to a nearby airport, a flight o an
 airport near our vacation spot, and a trip from that airport to our vacation
 hotel. The entire problem is solved by combining the solutions for these
 problems.

Designing an algorithm distinguishes two kinds of problems: those that are
 trivially solvable\footnote{For this part of the book, {\sl trivial\/} is  a technical term. It will be explained in section~\ref{sec:design-algo}.}
 and those that are not. If a given problem is trivially solvable, an
 algorithm produces the matching solution. For example, the problem of
 getting from our home to a nearby airport might be trivially solvable. We
 can drive there, take a cab, or ask a friend to drop us off.  If not, the
 algorithm generates a new problem and solves those new problems. A
 multistage trip is an example of a problem that is non-trivial and can be
 solved by generating new, smaller problems.  In a computational setting
 one of the smaller problems often belongs to the same class of problems as
 the original one, and it is for this reason that we call the approach
 {\sc {generative recursion}}.



In this part of the book, we study the design of algorithms, that is,
 functions based on generative recursion. From the description of the idea,
 we know that this process is much more of an {\em ad hoc\/} activity than
 the data-driven design of structurally recursive functions. Indeed, it is
 almost better to call it inventing an algorithm than designing
 one. Inventing an algorithm requires a new insight---a ``eureka.'' Sometimes
 very little insight is required. For example, solving a ``problem'' might
 just require the enumeration of a series of numbers. At other times,
 however, it may rely on a mathematical theorem concerning numbers. Or, it
 may exploit a series of mathematical results on systems of equations.  To
 acquire a good understanding of the design process, it is necessary to study
 examples and to develop a sense for the various classes of examples. In
 practice, new complex algorithms are often developed by mathematicians and
 mathematical computer scientists; programmers, though, must throughly
 understand the underlying ideas so that they can invent the simple
 algorithms on their own and communicate with scientists about the others.

The two subsections illustrate two vastly different algorithms. The first one
 is an example of something programmers invent on a daily basis. The second
 one describes a fast sorting algorithm, one of the first applications of
 generative recursion in computing.

\medskip\noindent{\bf Terminology}:\  Mathematical computer scientists often do not distinguish
 between structural recursion and generative recursion and refer to both
 kinds of functions as algorithms. Instead they use the terminology of {\em  recursive\/} and {\em iterative\/} methods. The latter refers to a subclass
 of function definitions whose recursive function applications are in a
 particular position in the definition. We will strictly adhere to the
 terminology of algorithm and generative recursion when we
 work with this class of functions because this classification matches our
 thinking of design recipes better than the purely syntactic classification
 of applied mathematicians.~{\rule{3pt}{5pt}}

\subsection{Modeling a Ball on a Table} \label{sec:out-of-bounds}


Let's consider the simple-looking problem of modeling the moves of a ball
across a table.  Assume the ball rolls at a constant speed until drops. We
can model the table with a canvas of some fixed width and height. The ball
is a disk that moves across the canvas, which we express with drawing the
disk, waiting, and clearing it, until it is out of bounds. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; TeachPack: draw.ss }\\
{\tt (define-struct}\  {\tt ball}\  {\tt (x}\  {\tt y}\  {\tt delta-x}\  {\tt delta-y))}
\\
\hbox{;; A {\sl {ball}} is a structure: }
\\
\hbox{;;   (make-ball number number number number)}
\\
\hbox{;; {{\tt draw-and-clear}\ {\tt :}\ {\tt a-ball}\ {}{\tt ->}{}\ {\tt true}\/}}
\\
\hbox{;; draw, sleep, clear a disk from the canvas }
\\
\hbox{;; structural design, Scheme knowledge}
\\
{\tt (d}\={\tt efine}\  {\tt (draw-and-clear}\  {\tt a-ball)}
\\
\>{\tt (}\={\tt and}
\\
\>\>{\tt (draw-solid-disk}\  {\tt (make-posn}\  {\tt (ball-x}\  {\tt a-ball)}\  {\tt (ball-y}\  {\tt a-ball))}\  {\tt 5}\  {\tt RED)}
\\
\>\>{\tt (sleep-for-a-while}\  {\tt DELAY)}
\\
\>\>{\tt (clear-solid-disk}\  {\tt (make-posn}\  {\tt (ball-x}\  {\tt a-ball)}\  {\tt (ball-y}\  {\tt a-ball))}\  {\tt 5}\  {\tt RED)))}
\\
\hbox{;; {{\tt move-ball}\ {\tt :}\ {\tt ball}\ {}{\tt ->}{}\ {\tt ball}\/}}
\\
\hbox{;; to create a new ball, modeling a move by {{\tt a-ball}\/}}
\\
\hbox{;; structural design, physics knowledge}
\\
{\tt (d}\={\tt efine}\  {\tt (move-ball}\  {\tt a-ball)}\  
\\
\>{\tt (make-ball}\  \={\tt (+}\  {\tt (ball-x}\  {\tt a-ball)}\  {\tt (ball-delta-x}\  {\tt a-ball))}
\\
\>\>{\tt (+}\  {\tt (ball-y}\  {\tt a-ball)}\  {\tt (ball-delta-y}\  {\tt a-ball))}
\\
\>\>{\tt (ball-delta-x}\  {\tt a-ball)}
\\
\>\>{\tt (ball-delta-y}\  {\tt a-ball)))}
\\
\hbox{;; Dimension of canvas }
\\
{\tt (define}\  {\tt WIDTH}\  {\tt 100)}
\\
{\tt (define}\  {\tt HEIGHT}\  {\tt 100)}
\\
{\tt (define}\  {\tt DELAY}\  {\tt .1)}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Auxiliaries for {\it move-until-out\/}} \label{fig:move-out}
\hrule

Figure~\ref{fig:move-out} collects the function, structure, data, and
variable definitions that model the ball:
\begin{enumerate}
\item A ball is a structure with four fields, which are the current
position and the velocity in each direction. That is, the first two numbers
in a {{\tt ball}\/} structure are the current position on the canvas, and
the next two numbers describe how far the ball moves in the two directiones
per step. 

\item The function {{\tt move-ball}\/} models the physical movement of the
ball. It consumes a ball and creates a new one, modeling one step.

\item The function {{\tt draw-and-clear}\/} draws the ball at its current
position, then waits for a short time, and clears it again.
\end{enumerate}
The variable definitions specify the dimensions of the canvas and the delay
time.

To move the ball a few times we can write 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt the-ball}\  {\tt (make-ball}\  {\tt 10}\  {\tt 20}\  {\tt -5}\  {\tt +17))}\\
{\tt (}\={\tt and}
\\
\>{\tt (draw-and-clear}\  {\tt the-ball)}
\\
\>{\tt (a}\={\tt nd}\  
\\
\>\>{\tt (draw-and-clear}\  {\tt (move-ball}\  {\tt the-ball))}
\\
\>\>{\tt ...))}
\end{tabbing}\end{minipage}\end{center}
though this gets tedious after a while. We should instead develop a
function that moves the ball until it is out of bounds.

The easy part is to define {{\tt out-of-bounds\char'077}\/}, a function that
determines whether a given ball is still visible on the canvas:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt out-of-bounds\char'077}\ {\tt :}\ {\tt a-ball}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt a-ball}\/} is outside of the bounds}
\\
\hbox{;; domain knowledge, geometry}
\\
{\tt (d}\={\tt efine}\  {\tt (out-of-bounds\char'077}\  {\tt a-ball)}
\\
\>{\tt (}\={\tt not}
\\
\>\>{\tt (a}\={\tt nd}
\\
\>\>\>{\tt (<=}\  {\tt 0}\  {\tt (ball-x}\  {\tt a-ball)}\  {\tt WIDTH)}
\\
\>\>\>{\tt (<=}\  {\tt 0}\  {\tt (ball-y}\  {\tt a-ball)}\  {\tt HEIGHT))))}
\end{tabbing}\end{minipage}\end{center}     
We have defined numerous functions like {{\tt out-of-bounds\char'077}\/} in the
first few sections of the book. 

In contrast, writing a function that draws the ball on the canvas until it
is out of bounds belongs to a group of programs that we haven't encountered
thusfar. Let's start with the basics of the function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt move-until-out}\ {\tt :}\ {\tt a-ball}\ {}{\tt ->}{}\ {\tt true}\/}}\\
\hbox{;; to model the movement of a ball until it goes out of bounds}
\\
{\tt (define}\  {\tt (move-until-out}\  {\tt a-ball)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Because the function consumes a ball and draws its movement on a canvas, it
produces {{\tt true}\/} like all other functions that draw into a canvas.
Designing it with the recipe for structures makes no sense, however. After
all, it is already clear how to {{\tt draw-and-clear}\/} the ball and how to
move it, too. What is needed instead, is a case distinction that checks
whether the ball is out of bounds or not. 


Let us refine the function header with an appropriate {{\bf cond}-exp\-res\-sion}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (move-until-out}\  {\tt a-ball)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (out-of-bounds\char'077}\  {\tt a-ball)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}     
We have already defined the function {{\tt out-of-bounds\char'077}\/} because it was
clear from the problem description that ``being out of bounds'' was a
separate concept. 

If the ball consumed by {{\tt move-until-out}\/} is outside of the canvas's
boundaries, the function can produce {{\tt true}\/}, following the
contract. If the ball is still inside the boundaries, two things must
happen. First, the ball must be drawn and cleared from the canvas. Second,
the ball must be moved and then we must do things all over again. This
implies that after moving the ball, we apply {{\tt move-until-out}\/} again,
which means the function is recursive: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt move-until-out}\ {\tt :}\ {\tt a-ball}\ {}{\tt ->}{}\ {\tt true}\/}}\\
\hbox{;; to model the movement of a ball until it goes out of bounds}
\\
{\tt (d}\={\tt efine}\  {\tt (move-until-out}\  {\tt a-ball)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (out-of-bounds\char'077}\  {\tt a-ball)}\  {\tt true\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (and}\  \={\tt (draw-and-clear}\  {\tt a-ball)}
\\
\>\>\>{\tt (move-until-out}\  {\tt (move-ball}\  {\tt a-ball)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}     
Both {{\tt (draw-and-clear}\ {\tt a-ball)}\/} and {{\tt (move-until-out}{ }{\tt (move-ball}\ {\tt a-ball))}\/} produce true, and both expressions must be
evaluated. So we combine them with an {{\bf and}-exp\-res\-sion}. 

We can now test the function as follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (start}\  {\tt WIDTH}\  {\tt HEIGHT)}\\
{\tt (move-until-out}\  {\tt (make-ball}\  {\tt 10}\  {\tt 20}\  {\tt -5}\  {\tt +17))}
\\
{\tt (stop)}
\end{tabbing}\end{minipage}\end{center}     
This creates a canvas of proper size and a ball that moves left and down.

A close look at the function definition reveals two peculiarities. First,
although the function is recursive, its body consists of a
{{\bf cond}-exp\-res\-sion} whose conditions have nothing to do with the input
data. Second, the recursive application in the body does not consume a part
of the input. Instead, {{\tt move-until-out}\/} generates an entirely new
and different {{\tt ball}\/} structure, which represents the original ball
after one step, and uses it for the recursion. Clearly, none of our design
recipes could possibly produce such a definition. We have encountered a new
way of programming.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 25.1.1}

 \label{ex:move-until1}
What happens if we place the following three expressions 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (start}\  {\tt WIDTH}\  {\tt HEIGHT)}\\
{\tt (move-until-out}\  {\tt (make-ball}\  {\tt 10}\  {\tt 20}\  {\tt 0}\  {\tt 0))}
\\
{\tt (stop)}
\end{tabbing}\end{minipage}\end{center}     
at the bottom of the {\tt Definitions} window and click
{\tt Execute}? Does the second expression ever produce a 
value so that the third expression is evaluated and the canvas disappears?
Could this happen with any of the functions designed according to our old
recipes?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/move-until1.html}{\rule{3pt}{5pt}}


{\bf Exercise 25.1.2}

 \label{ex:move-until2}
Develop {{\tt move-balls}\/}.  The function consumes a list of balls and
moves each one until all of them have moved out of bounds.

\noindent{\bf Hint:}  It is best to write this function using {{\tt filter}\/},
{{\tt andmap}\/}, and similar abstract functions from
part~\ref{part:abstract}.\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/move-until2.html}{\rule{3pt}{5pt}}



\subsection{Sorting Quickly} \label{sec:sortII}

Hoare's quicksort algorithm is the classic example of generative recursion
in computing. Like {{\tt sort}\/} in section~\ref{sec:sortI},
{{\tt quicksort}\/} is a function that consumes a list of numbers and
produces a version that contains the same numbers in ascending order.  The
difference between the two functions is that {{\tt sort}\/} is based on
structural recursion and {{\tt quicksort}\/} is based on generative
recursion.

The underlying idea of the generative step is a time-honored strategy:
divide and conquer. That is, we divide the non-trivial instances of the
problem into two smaller, related problems, solve those smaller problems,
and combine their solutions into a solution for the original problem. In
the case of {{\tt quicksort}\/}, the intermediate goal is to divide the list
of numbers into two lists: one that contains all the items that are
strictly smaller than the first item, and another one with all those items
that are strictly larger than the first item. Then the two smaller lists
are sorted using the same procedure. Once the two lists are sorted, we
simply juxtapose the pieces.  Due to its special role, the first item on
the list is often called the {\sl pivot item\/}.



\hrule



\begin{htmlonly}
\begin{rawhtml}
<center>
<table border bgcolor = white>
<tr><td align=center colspan=3><tt>(list 11 8 14 7)</tt> </td></tr>
<tr>
<td valign = top> 
<table border bgcolor = white>
<tr><td align=center colspan=3><tt>(list 8 7)</tt> </td></tr>
<tr>
<td valign = top> 
<table border bgcolor = white>
<tr><td align=center colspan=3><tt>(list 7)</tt> </td></tr>
<tr>
<td valign = top> <tt>empty</tt> </td>
<td bgcolor=orange valign = top><table><tt>7</tt></table></td>
<td valign = top> <tt>empty</tt> </td>
</tr>
<tr><td align=center colspan=3><tt>(list 7)</tt> </td></tr>
</table>
 </td>
<td bgcolor=orange valign = top><table><tt>8</tt></table></td>
<td valign = top> <tt>empty</tt> </td>
</tr>
<tr><td align=center colspan=3><tt>(list 7 8)</tt> </td></tr>
</table>
 </td>
<td bgcolor=orange valign = top><table><tt>11</tt></table></td>
<td valign = top> 
<table border bgcolor = white>
<tr><td align=center colspan=3><tt>(list 14)</tt> </td></tr>
<tr>
<td valign = top> <tt>empty</tt> </td>
<td bgcolor=orange valign = top><table><tt>14</tt></table></td>
<td valign = top> <tt>empty</tt> </td>
</tr>
<tr><td align=center colspan=3><tt>(list 14)</tt> </td></tr>
</table>
 </td>
</tr>
<tr><td align=center colspan=3><tt>(list 7 8 11 14)</tt> </td></tr>
</table>
</center>
\end{rawhtml}
\end{htmlonly}

\centerline{\relax}
\center{Figure: A tabular illustration of {\it quick-sort\/}} \label{fig:sort-ill}
\hrule

To develop a better understanding of the process, let's walk through one
step of the evaluation by hand. Suppose the input is
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  {\tt 11}\  {\tt 8}\  {\tt 14}\  {\tt 7)}\end{tabbing}\end{minipage}\end{center}
The pivot item is {{\tt 11}\/}. Partioning the list into items larger and
smaller than {{\tt 11}\/} produces two lists:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  {\tt 8}\  {\tt 7)}\end{tabbing}\end{minipage}\end{center}
and 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  {\tt 14)}\end{tabbing}\end{minipage}\end{center}
The second one is already sorted in ascending order; sorting the first one
produces {{\tt (list}\ {\tt 7}\ {\tt 8)}\/}. This leaves us with three pieces from the
original list: 
\begin{enumerate}
\item {{\tt (list}\ {\tt 7}\ {\tt 8)}\/}, the sorted version of the list with the smaller numbers; 
\item {{\tt 11}\/};
and 
\item {{\tt (list}\ {\tt 14)}\/}, the sorted version of the list with the larger
numbers. 
\end{enumerate}
To produce a sorted version of the original list, we concatenate the three
pieces, which yields the desired result: {{\tt (list}\ {\tt 7}\ {\tt 8}\ {\tt 11}\ {\tt 14)}\/}.

Our illustration leaves open how {{\tt quicksort}\/} knows when to
stop. Since it is a function based on generative recursion, the general
answer is when the sorting problem has become trivial. Clearly,
{{\tt empty}\/} is one trivial input for {{\tt quicksort}\/}, because the
only sorted version of it is {{\tt empty}\/}. For now, this answer suffices;
we will return to this question in the next section.

Figure~\ref{fig:sort-ill} provides tabular overview of the entire sorting
process for {{\tt (list}\ {\tt 11}\ {\tt 8}\ {\tt 14}\ {\tt 7)}\/}. Each box has three compartments:
\begin{center}
\begin{htmlonly}
\begin{rawhtml}
<center>
<table border bgcolor = white>
<tr><td align=center colspan=3>
list to be sorted
</td></tr>
<tr>
<td valign = top> 
sort process for partition with items <em>smaller</em> than pivot
</td>
<td bgcolor=orange valign = top><table>
pivot item
</table></td>
<td valign = top> 
sort process for partition with items <em>larger</em> than pivot 
</td>
</tr>
<tr><td align=center colspan=3>
sorted list
</td></tr>
</table>
</center>
\end{rawhtml}

\end{htmlonly}

\relax
\end{center}

\noindent The top compartment shows the list that we wish to sort, the
bottommost contains the result. The three columns in the middle display the
sorting process for the two partitions and the pivot item.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 25.2.1}

 \label{ex:quicksort-hand}
Simulate all {{\tt quicksort}\/} steps for {{\tt (list}\ {\tt 11}\ {\tt 9}\ {\tt 2}\ {\tt 18}\ {\tt 12}\ {\tt 14}\ {\tt 4}\ {\tt 1)}\/}.
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:quicksort-hand.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Now that we have a good understanding of the generative step, we can
translate the process description into Scheme. The description suggests that
{{\tt quicksort}\/} distinguishes two cases. If the input is {{\tt empty}\/}, 
it produces {{\tt empty}\/}. Otherwise, it performs a generative recursion. 
This case-split suggests a {{\bf cond}-exp\-res\-sion}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt quick-sort}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt number)}\/}}\\
\hbox{;; to create a list of numbers with the same numbers as}
\\
\hbox{;; {{\tt alon}\/} sorted in ascending order}
\\
{\tt (d}\={\tt efine}\  {\tt (quick-sort}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The answer for the first case is given. For the second case, when
{{\tt quicksort}\/}'s input is non-{{\tt empty}\/}, the algorithm uses the
first item to partition the rest of the list into two sublists: a list with
all items smaller than the pivot item and another one with those larger
than the pivot item.

Since the rest of the list is of unknown size, we leave the task of
partitioning the list to two auxiliary functions: {{\tt smaller-items}\/}
and {{\tt larger-items}\/}. They process the list and filter out those items
that are smaller and larger, respectively, than the first one. Hence, each
auxiliary function accepts two arguments, namely, a list of numbers and a
number. Developing these functions is, of course, an exercise in structural
recursion; their definitions are shown in figure~\ref{fig:qsort}.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt quick-sort}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt number)}\/}}\\
\hbox{;; to create a list of numbers with the same numbers as}
\\
\hbox{;; {{\tt alon}\/} sorted in ascending order}
\\
{\tt (d}\={\tt efine}\  {\tt (quick-sort}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (a}\={\tt ppend}\  
\\
\>\>\>{\tt (quick-sort}\  {\tt (smaller-items}\  {\tt alon}\  {\tt (first}\  {\tt alon)))}\  
\\
\>\>\>{\tt (list}\  {\tt (first}\  {\tt alon))}\  
\\
\>\>\>{\tt (quick-sort}\  {\tt (larger-items}\  {\tt alon}\  {\tt (first}\  {\tt alon))))\char'135}{\tt ))}
\\
\hbox{;; {{\tt larger-items}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {\tt number}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt number)}\/}}
\\
\hbox{;; to create a list with all those numbers on {{\tt alon}\/}  }
\\
\hbox{;; that are larger than {{\tt threshold}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (larger-items}\  {\tt alon}\  {\tt threshold)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (if}\  \={\tt (>}\  {\tt (first}\  {\tt alon)}\  {\tt threshold)}\  
\\
\>\>\>{\tt (cons}\  {\tt (first}\  {\tt alon)}\  {\tt (larger-items}\  {\tt (rest}\  {\tt alon)}\  {\tt threshold))}
\\
\>\>\>{\tt (larger-items}\  {\tt (rest}\  {\tt alon)}\  {\tt threshold))\char'135}{\tt ))}
\\
\hbox{;; smaller-items : {{\tt (listof}\ {\tt number)}\ {\tt number}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt number)}\/}}
\\
\hbox{;; to create a list with all those numbers on {{\tt alon}\/}  }
\\
\hbox{;; that are smaller than {{\tt threshold}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (smaller-items}\  {\tt alon}\  {\tt threshold)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (if}\  \={\tt (<}\  {\tt (first}\  {\tt alon)}\  {\tt threshold)}\  
\\
\>\>\>{\tt (cons}\  {\tt (first}\  {\tt alon)}\  {\tt (smaller-items}\  {\tt (rest}\  {\tt alon)}\  {\tt threshold))}
\\
\>\>\>{\tt (smaller-items}\  {\tt (rest}\  {\tt alon)}\  {\tt threshold))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: The quick-sort algorithm}\label{fig:qsort}
\hrule

Each sublist is sorted separately, using {{\tt quick-sort}\/}. This implies 
the use of recursion and, more specifically, the following two expressions: 
\begin{enumerate}
\item {{\tt (quick-sort}\ {\tt (smaller-items}\ {\tt alon}\ {\tt (first}\ {\tt alon)))}\/},
which sorts the list of items smaller than the pivot; and 
\item {{\tt (quick-sort}\ {\tt (larger-items}\ {\tt alon}\ {\tt (first}\ {\tt alon)))}\/}, 
which sorts the list of items larger than the pivot; and 
\end{enumerate}

Once we get the sorted versions of the two lists, we need a function that
combines the two lists and the pivot item. Scheme's {{\tt append}\/}
function accomplishes this:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (append}\  \={\tt (quick-sort}\  {\tt (smaller-items}\  {\tt alon}\  {\tt (first}\  {\tt alon)))}\\
\>{\tt (list}\  {\tt (first}\  {\tt alon))}
\\
\>{\tt (quick-sort}\  {\tt (larger-items}\  {\tt alon}\  {\tt (first}\  {\tt alon))))}
\end{tabbing}\end{minipage}\end{center}
Clearly, all items in list~1 are smaller than the pivot and the pivot is
smaller than all items in list~2, so the result is a sorted list. 
Figure~\ref{fig:qsort} contains the full function. It includes the
definition of {{\tt quick-sort}\/}, {{\tt smaller-items}\/}, and
{{\tt larger-items}\/}. 

Let's take a look at the beginning of a sample hand evaluation:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (quick-sort}\  {\tt (list}\  {\tt 11}\  {\tt 8}\  {\tt 14}\  {\tt 7))}\\
{\tt =}\  {\tt (append}\  \={\tt (quick-sort}\  {\tt (list}\  {\tt 8}\  {\tt 7))}
\\
\>{\tt (list}\  {\tt 11)}
\\
\>{\tt (quick-sort}\  {\tt (list}\  {\tt 14)))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (append}\  \={\tt (append}\  \={\tt (quick-sort}\  {\tt (list}\  {\tt 7))}\\
\>\>{\tt (list}\  {\tt 8)}
\\
\>\>{\tt (quick-sort}\  {\tt empty))}
\\
\>{\tt (list}\  {\tt 11)}
\\
\>{\tt (quick-sort}\  {\tt (list}\  {\tt 14)))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (append}\  \={\tt (append}\  \={\tt (append}\  \={\tt (quick-sort}\  {\tt empty)}\\
\>\>\>{\tt (list}\  {\tt 7)}
\\
\>\>\>{\tt (quick-sort}\  {\tt empty))}
\\
\>\>{\tt (list}\  {\tt 8)}
\\
\>\>{\tt (quick-sort}\  {\tt empty))}
\\
\>{\tt (list}\  {\tt 11)}
\\
\>{\tt (quick-sort}\  {\tt (list}\  {\tt 14)))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (append}\  \={\tt (append}\  \={\tt (append}\  \={\tt empty}\\
\>\>\>{\tt (list}\  {\tt 7)}
\\
\>\>\>{\tt empty)}
\\
\>\>{\tt (list}\  {\tt 8)}
\\
\>\>{\tt empty)}
\\
\>{\tt (list}\  {\tt 11)}
\\
\>{\tt (quick-sort}\  {\tt (list}\  {\tt 14)))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (append}\  \={\tt (append}\  \={\tt (list}\  {\tt 7)}\\
\>\>{\tt (list}\  {\tt 8)}
\\
\>\>{\tt empty)}
\\
\>{\tt (list}\  {\tt 11)}
\\
\>{\tt (quick-sort}\  {\tt (list}\  {\tt 14)))}
\\
{\tt =}\  {\tt ...}\  
\end{tabbing}\end{minipage}\end{center}
The calculation shows the essential steps of the sorting process, that is,
the partitioning steps, the recursive sorting steps, and the concatenation
of the three parts. From this calculation, we can see that
{{\tt quick-sort}\/} implements the process illustrated in
figure~\ref{fig:sort-ill}


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 25.2.2}

 \label{ex:quick-sort-terminal}
Complete the above hand-evaluation. 

The hand-evaluation of {{\tt (quick-sort}\ {\tt (list}\ {\tt 11}\ {\tt 8}\ {\tt 14}\ {\tt 7))}\/} suggests an
additional trivial case for {{\tt quick-sort}\/}. Every time
{{\tt quick-sort}\/} consumes a list of one item, it produces the very same
list. After all, the sorted version of a list of one item is the list
itself.

Modify the definition of {{\tt quick-sort}\/} to take advantage of this
observation.

Hand-evaluate the same example again. How many steps does the extended
algorithm save? \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/quick-sort-terminal.html}{\rule{3pt}{5pt}}


{\bf Exercise 25.2.3}

 \label{ex:quick-sort-switch}
While {{\tt quick-sort}\/} quickly reduces the size of the problem in many
cases, it is inappropriately slow for small problems. Hence, people often
use {{\tt quick-sort}\/} to reduce the size of the problem and switch to a
different sort function when the list is small enough. 

Develop a version of {{\tt quick-sort}\/} that uses {{\tt sort}\/} from
section~\ref{sec:sortI} if the length of the input is below some
threshold. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/quick-sort-switch.html}{\rule{3pt}{5pt}}


{\bf Exercise 25.2.4}

 \label{ex:quick-sort-drop}
If the input to {{\tt quick-sort}\/} contains the same number several times,
the algorithm returns a list that is strictly shorter than the input. Why?
Fix the problem so that the output is as long as the input. 
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/quick-sort-drop.html}{\rule{3pt}{5pt}}


{\bf Exercise 25.2.5}

 \label{ex:quick-sort-filter}
Use the {{\tt filter}\/} function to define {{\tt smaller-items}\/} and
{{\tt larger-items}\/} as one-liners. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/quick-sort-filter.html}{\rule{3pt}{5pt}}


{\bf Exercise 25.2.6}

 \label{ex:quick-sort-abstract}
Develop a variant of {{\tt quick-sort}\/} that uses only one comparison
function, say, {{\tt <}\/}. Its partitioning step divides the given list
{{\tt alon}\/} into a list that contains the items of {{\tt alon}\/} smaller
than {{\tt (first}\ {\tt alon)}\/} and another one with those that are not smaller.

Use {{\tt local}\/} to combine the functions into a single function. 
Then abstract the new version to consume a list and a comparison function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt general-quick-sort}\ {\tt :}\ {\tt (X}\ {\tt X}\ {}{\tt ->}{}\ {\tt bool)}\ {\tt (list}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (list}\ {\tt X)}\/}}\\
{\tt (define}\  {\tt (general-quick-sort}\  {\tt a-predicate}\  {\tt a-list)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/quick-sort-abstract.html}{\rule{3pt}{5pt}}




\section{Designing Algorithms} \label{sec:design-algo}

At first glance, the algorithms {{\tt move-until-out}\/} and
{{\tt quick-sort}\/} have little in common. One processes structures, the
other one lists. One creates a new structure for the generative step, the
other one splits up a list into three pieces and recurs on two of them. In
short, a comparison of the two examples of generative recursion suggests
that the design of algorithms is an {\em ad hoc\/} activity and that it is
impossible to come up with a general design recipe. A close look, however,
suggests a different picture.

First, even though we speak of ``algorithms as processes that solve
problems'' they are still functions that consume and produce data. In other
words, we still choose data to represent a problem, and we must definitely
understand the nature of our data if we wish to understand the
process. Second, we describe the processes in terms of data, for example,
``creating a new structure'' or ``partitioning a list of numbers.'' Third,
we always distinguish between input data for which it is trivial to produce
a solution and those for which it is not. Fourth, the generation of
problems is the key to the design of algorithms. Although the idea of how
to generate a new problem might be independent of a data representation, it
must certainly be implemented for whatever form of representation we choose
for our problem. Finally, once the generated problems have been solved, the
solutions must be combined with other values. 

Let us examine the six general stages of our structural design recipe in
light of our discussion: 
\begin{description}
\item[Data analysis \& design:] The choice of a data representation for
a problem often affects our thinking about the process. Sometimes the
description of a process dictates a particular choice of representation. On
other occasions, it is possible and worthwhile to explore alternatives. In
any case, we must analyze and define our data collections. 

\item[Contract, Purpose, Header:] We also need a contract, a definition
header, and a purpose statement. Since the generative step has no
connection to the structure of the data definition, the purpose statement
should not just specify {\bf what} the function does but should also
include a comment that explains in general terms {\bf how} the it works.

\item[Function examples:] In our previous design recipes, the function
examples merely specified which output the function should produce for some
given input. For algorithms, examples should illustrate {\bf how\/} the
algorithm proceeds for some given input. This helps us design, and readers
understand, the algorithm. For functions such as {{\tt move-until-out}\/}
the process is trivial and doesn't need more than a few words. For others,
including, {{\tt quick-sort}\/}, the process relies on a non-trivial idea
for its generative step, and its explanation requires a good example such
as the one in figure~\ref{fig:sort-ill}.

\item[Template:] Our discussion suggests a general template for algorithms: 

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (generative-recursive-fun}\  {\tt problem)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (trivially-solvable\char'077}\  {\tt problem)}
\\
\>\>\>{\tt (determine-solution}\  {\tt problem)\char'135}
\\
\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>{\tt (c}\={\tt ombine-solutions}
\\
\>\>\>\>{\tt ...}\  {\tt problem}\  {\tt ...}
\\
\>\>\>\>{\tt (generative-recursive-fun}\  {\tt (generate-problem-1}\  {\tt problem))}
\\
\>\>\>\>\hbox{$\vdots$}
\\
\>\>\>\>{\tt (generative-recursive-fun}\  {\tt (generate-problem-n}\  {\tt problem)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\item[Definition:] Of course, this template is only a suggestive blueprint,
not a definitive shape.  Each function in the template is to remind us that
we need to think about the following four questions: 

  \begin{enumerate}
    \item What is a trivially solvable problem? 

    \item What is a corresponding solution? 

    \item How do we generate new problems that are more easily solvable
      than the original problem? Is there one new problem that we generate
      or are there several?

    \item Is the solution of the given problem the same as the solution of
      (one of) the new problems? Or, do we need to combine the solutions to
      create a solution for the original problem? And, if so, do we need
      anything from the original problem data?
 
  \end{enumerate} 

To define the algorithm, we must express the answers to these four
questions in terms of our chosen data representation.

\item[Test:] Once we have a complete function, we must also test it. As
before, the goal of testing is to discover bugs and to eliminate them. 
Remember that testing cannot validate that the function works correctly 
for all possible inputs. Also remember that it is best to formulate tests
as boolean-valued expressions that automatically compare the expected value
with the computed value (see section~\ref{sec:equal-test}).
\end{description}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 26.0.1}

 \label{ex:gen-fun-instance1}
Formulate informal answers to the four key questions for the problem of
modeling a ball's movement across a canvas until it is out of bounds.
~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/gen-fun-instance1.html}{\rule{3pt}{5pt}}


{\bf Exercise 26.0.2}

 \label{ex:gen-fun-instance2}
Formulate informal answers to the four key questions for the
{{\tt quick-sort}\/} problem. How many different instances of
{{\tt generate-problem}\/} are needed?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/gen-fun-instance2.html}{\rule{3pt}{5pt}}


\subsection{Termination}

Unfortunately, the standard recipe is not good enough for the design of
 algorithms.  Up to now, a function has always produced an output for any
 legitimate input. That is, the evaluation has always stopped. After all,
 by the nature of our recipe, each natural recursion consumes an immediate
 piece of the input, not the input itself.  Because data is constructed in
 a hierarchical manner, this means that the input shrinks at every
 stage. Hence, the function sooner or later consumes an atomic piece of
 data and stops.

With functions based on generative recursion, this is no longer true. The
 internal recursions don't consume an immediate component of the input but
 some new piece of data, which is generated from the input. As
 exercise~\ref{ex:move-until1} shows, this step may produce the input over
 and over again and thus prevent the evaluation from ever producing a
 result. We say that the program {\sc {loops}} or is in an
 {\sc {infinite loop}}. 

In addition, even the slightest mistake in translating the process
 description into a function definition may cause an infinite loop.  The
 problem is most easily understood with an example. Consider the following
 definition of {{\tt smaller-items}\/}, one of the two ``problem
 generators'' for {{\tt quick-sort}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt smaller-items}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {\tt number}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt number)}\/}}\\
\hbox{;; to create a list with all those numbers on {{\tt alon}\/}  }
\\
\hbox{;; that are smaller than or equal to {{\tt threshold}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (smaller-items}\  {\tt alon}\  {\tt threshold)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (if}\  \={\tt (<=}\  {\tt (first}\  {\tt alon)}\  {\tt threshold)}\  
\\
\>\>\>{\tt (cons}\  {\tt (first}\  {\tt alon)}\  {\tt (smaller-items}\  {\tt (rest}\  {\tt alon)}\  {\tt threshold))}
\\
\>\>\>{\tt (smaller-items}\  {\tt (rest}\  {\tt alon)}\  {\tt threshold))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Instead of {{\tt <}\/} it employs {{\tt <=}\/} to compare numbers. As a
result, this function produces {{\tt (list}\ {\tt 5)}\/} when applied to
{{\tt (list}\ {\tt 5)}\/} and {{\tt 5}\/}.

Worse, if the {{\tt quick-sort}\/} function from figure~\ref{fig:qsort} is
combined with this new version of {{\tt smaller-items}\/}, it doesn't produce
any output for {{\tt (list}\ {\tt 5)}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (quick-sort}\  {\tt (list}\  {\tt 5))}\\
{\tt =}\  {\tt (append}\  \={\tt (quick-sort}\  {\tt (smaller-items}\  {\tt 5}\  {\tt (list}\  {\tt 5)))}
\\
\>{\tt (list}\  {\tt 5)}
\\
\>{\tt (quick-sort}\  {\tt (larger-items}\  {\tt 5}\  {\tt (list}\  {\tt 5))))}
\\
{\tt =}\  {\tt (append}\  \=\hbox{$\underline{\mbox{{{\tt (quick-sort}\ {\tt (list}\ {\tt 5))}\/}}}$}
\\
\>{\tt (list}\  {\tt 5)}
\\
\>{\tt (quick-sort}\  {\tt (larger-items}\  {\tt 5}\  {\tt (list}\  {\tt 5))))}
\end{tabbing}\end{minipage}\end{center}
The first recursive use demands that {{\tt quick-sort}\/} solve the problem
of sorting {{\tt (list}\ {\tt 5)}\/}---but that is the exact problem that we
started from. Since this is a circular evaluation, {{\tt (quick-sort}{ }{\tt (list}\ {\tt 5))}\/} never produces a result.  More generally, there is no guarantee
that the size of the input for a recursive call brings us closer to a
solution than the original input.

The lesson from this example is that the design of algorithms requires one
more step in our design recipe: a {\sc {termination argument}}, which
explains why the process produces an output for every input and how the
function implements this idea; or a {\sc {termination warning}}, which
explains when the process may not terminate. 
For {{\tt quick-sort}\/}, the argument might look like this:
\begin{quote}
At each step, {{\tt quick-sort}\/} partitions the list into two sublists
using {{\tt smaller-items}\/} and {{\tt larger-items}\/}. Each function
produces a list that is smaller than the input (the second argument), even
if the threshold (the first argument) is an item on the list.  Hence, each
recursive application of {{\tt quick-sort}\/} consumes a strictly shorter
list than the given one.  Eventually, {{\tt quick-sort}\/} receives
and returns {{\tt empty}\/}.
\end{quote}
Without such an argument an algorithm must be considered incomplete. 

A good termination argument may on occasion also reveal additional
termination cases. For example, {{\tt (smaller-items}\ {\tt N}\ {\tt (list}\ {\tt N))}\/} and
{{\tt (larger-items}\ {\tt N}\ {\tt (list}\ {\tt N))}\/} always produce {{\tt empty}\/} for any
{{\tt N}\/}. Therefore we know that {{\tt quick-sort}\/}'s answer for
{{\tt (list}\ {\tt N)}\/} is {{\tt (list}\ {\tt N)}\/}.\footnote{Of course, we could have just argued that the sorted version of a one-item list  is the list, which is the basis of exercise~\ref{ex:quick-sort-terminal}.} To add this
knowledge to {{\tt quick-sort}\/}, we simply add a {{\tt cond}\/}-clause:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (quick-sort}\  {\tt alon)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt (rest}\  {\tt alon))}\  {\tt alon\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (a}\={\tt ppend}\  
\\
\>\>\>{\tt (quick-sort}\  {\tt (smaller-items}\  {\tt alon}\  {\tt (first}\  {\tt alon)))}\  
\\
\>\>\>{\tt (list}\  {\tt (first}\  {\tt alon))}\  
\\
\>\>\>{\tt (quick-sort}\  {\tt (larger-items}\  {\tt alon}\  {\tt (first}\  {\tt alon))))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The condition {{\tt (empty?}\ {\tt (rest}\ {\tt alon))}\/} is one way to ask the question
whether {{\tt alon}\/} contains one item. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 26.1.1}

 \label{ex:prime-tab}
Define the function {{\tt tabulate-div}\/}, which accepts a number {{\tt n}\/}
and tabulates the list of all of its divisors, starting with
{{\tt 1}\/} and ending in {{\tt n}\/}. A number {{\tt d}\/} is a divisior of a
number {{\tt n}\/} if the remainder of dividing {{\tt n}\/} by {{\tt d}\/} is
{{\tt 0}\/}, that is, {{\tt (=}\ {\tt (remainder}\ {\tt n}\ {\tt d)}\ {\tt 0)}\/} is true. The smallest
divisior of any number is {{\tt 1}\/}; the largest one is the number
itself.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/prime-tab.html}{\rule{3pt}{5pt}} 

{\bf Exercise 26.1.2}

  \label{ex:merge-sort}
Develop the function {{\tt merge-sort}\/}, which sorts a list of numbers in
ascending order, using the following two auxiliary functions: 
\begin{enumerate}
\item The first one, {{\tt make-singles}\/}, constructs a list of
one-item lists from the given list of numbers. For example, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (make-singles}\  {\tt (list}\  {\tt 2}\  {\tt 5}\  {\tt 9}\  {\tt 3))}\\
{\tt =}\  {\tt (list}\  {\tt (list}\  {\tt 2)}\  {\tt (list}\  {\tt 5)}\  {\tt (list}\  {\tt 9)}\  {\tt (list}\  {\tt 3))}
\end{tabbing}\end{minipage}\end{center}

\item The second one, {{\tt merge-all-neighbors}\/}, merges pairs of neighboring
lists. More specifically, it consumes a list of lists (of numbers) and
merges neighbors. For example, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (merge-all-neighbors}\  {\tt (list}\  {\tt (list}\  {\tt 2)}\  {\tt (list}\  {\tt 5)}\  {\tt (list}\  {\tt 9)}\  {\tt (list}\  {\tt 3)))}\\
{\tt =}\  {\tt (list}\  {\tt (list}\  {\tt 2}\  {\tt 5)}\  {\tt (list}\  {\tt 3}\  {\tt 9))}
\\
\  \  {\tt (merge-all-neighbors}\  {\tt (list}\  {\tt (list}\  {\tt 2}\  {\tt 5)}\  {\tt (list}\  {\tt 3}\  {\tt 9)))}
\\
{\tt =}\  {\tt (list}\  {\tt (list}\  {\tt 2}\  {\tt 3}\  {\tt 5}\  {\tt 9))}
\end{tabbing}\end{minipage}\end{center}
In general, this function yields a list that is approximately half as long as
the input. Why is the output not always half as long as the input? 
\end{enumerate}
Make sure to develop the functions independently. 

The function {{\tt merge-sort}\/} first uses {{\tt make-singles}\/} to create
a list of single lists; then it relies on {{\tt merge-all-neighbors}\/} to
shorten the list of lists until it contains a single list. The latter is
the result.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/merge-sort.html}{\rule{3pt}{5pt}}



\subsection{Structural versus Generative Recursion}

The template for algorithms is so general that it even covers functions
based on structural recursion. Consider the version with one termination 
clause and one generation step: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (generative-recursive-fun}\  {\tt problem)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (trivially-solvable\char'077}\  {\tt problem)}
\\
\>\>\>{\tt (determine-solution}\  {\tt problem)\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ombine-solutions}
\\
\>\>\>\>{\tt problem}
\\
\>\>\>\>{\tt (generative-recursive-fun}\  {\tt (generate-problem}\  {\tt problem)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
If we replace {{\tt trivially-solvable\char'077}\/} with {{\tt empty?}\/} and 
{{\tt generate-problem}\/} with {{\tt rest}\/}, the outline {{\tt is}\/} a
template for a list-processing function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (generative-recursive-fun}\  {\tt problem)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt problem)}\  {\tt (determine-solution}\  {\tt problem)\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (c}\={\tt ombine-solutions}
\\
\>\>\>\>{\tt problem}
\\
\>\>\>\>{\tt (generative-recursive-fun}\  {\tt (rest}\  {\tt problem)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 26.2.1}

 \label{ex:generative-length}
Define {{\tt determine-solution}\/} and {{\tt combine-solutions}\/} so that
the function {{\tt generative-recursive-fun}\/} computes the length of its
input.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/generative-length.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


This discussion raises the question whether there is a difference between
between functions based on structural recursion and those based on
generative recursion. The answer is ``it depends.'' Of course, we could say
that all functions using structural recursion are just special cases of
generative recursion. This ``everything is equal'' attitude, however, is of
no help if we wish to understand the process of designing functions. It
confuses two classes of functions that are designed with different
approaches and that have different consequences. One relies on a systematic
data analysis and not much more; the other requires a deep, often
mathematical, insight into the problem-solving process itself. One
leads programmers to naturally terminating functions; the other requires a
termination argument. 

A simple inspection of a function's definition quickly shows whether a
function uses structural or generative recursion. All self-applications of
a structurally recursive function always receive an immediate component of
the current input for further processing. For example, for a
{{\tt cons}\/}tructed list, the immediate components are the {{\tt first}\/}
item and the {{\tt rest}\/} of the list. Hence, if a function consumes a
plain list and its natural recursion does not consume the rest of the list,
its definition is not structural but generative. Or, put positively,
properly recursive algorithms consume newly generated input, which may or
may not contain components of the input. In any case, the new piece of data
represents a different problem than the given one, but still a problem of
the same general class of problems.

\subsection{Making Choices} \label{sec:choice}

A user cannot distinguish {{\tt sort}\/} and {{\tt quick-sort}\/}. Both
 consume a list of numbers; both produce a list that consists of the same
 numbers but is arranged in ascending order. To an observer, the functions
 are completely equivalent.\footnote{The concept of observably equivalent  functions and expressions plays a central role in the study of programming  languages and their meaning.}  This raises the question which of the two a
 programmer should provide. More generally, if we can develop a function
 using structural recursion and an equivalent one using generative
 recursion, what should we do?

To understand this choice better, let's discuss another classical example of
 generative recursion from mathematics: the problem of finding the greatest
 common divisor of two positive natural numbers.\footnote{The material on  the greatest common divisor was suggested by John Stone (Grinnell  College).} All such numbers have at least one divisor in common: 1. On
 occasion, this is also the only common divisor. For example, 2 and 3 have
 only 1 as common divisor because 2 and 3, respectively, are the only other
 divisors. Then again, 6 and 25 are both numbers with several divisors:
\begin{enumerate}
\item 6 is evenly divisible by 1, 2, 3, and 6;
\item 25 is evenly divisible by 1, 5, and 25.
\end{enumerate}
Still, the greatest common divisior of 25 and 6 is 1. In contrast, 
18 and 24 have many common divisors: 
\begin{enumerate}
\item 18 is evenly divisible by 1, 2, 3, 6, and 9; 
\item 24 is evenly divisible by 1, 2, 3, 4, 6, 8, and 12.
\end{enumerate}
The greatest common divisor is 6. 

Following the design recipe, we start with a contract and a purpose statement: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt gcd}\ {\tt :}\ {\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\ {\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to find the greatest common divisior of {{\tt n}\/} and {{\tt m}\/}}
\\
{\tt (define}\  {\tt (gcd}\  {\tt n}\  {\tt m)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
The contract specifies the precise inputs: natural numbers that are greater
or equal to {{\tt 1}\/} (not {{\tt 0}\/}). 

Now we need to make a decision whether we want to pursue a design based on
structural or on generative recursion. Since the answer is by no means
obvious, we develop both. For the structural version, we must consider
which input the function should process: {{\tt n}\/}, {{\tt m}\/}, or both.
A monent's consideration suggests that what we really need is a function
that starts with the smaller of the two and outputs the first
number smaller or equal to this input that evenly divides both {{\tt n}\/}
and {{\tt m}\/}.

We use {{\tt local}\/} to define an appropriate auxiliary function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt gcd-structural}\ {\tt :}\ {\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\ {\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to find the greatest common divisior of {{\tt n}\/} and {{\tt m}\/}}
\\
\hbox{;; {\bf structural} recursion using data definition of {{\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (gcd-structural}\  {\tt n}\  {\tt m)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (first-divisior-<=}\  {\tt i)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (=}\  {\tt i}\  {\tt 1)}\  {\tt 1\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (and}\  \={\tt (=}\  {\tt (remainder}\  {\tt n}\  {\tt i)}\  {\tt 0)}\  
\\
\>\>\>\>\>\>\>{\tt (=}\  {\tt (remainder}\  {\tt m}\  {\tt i)}\  {\tt 0))}
\\
\>\>\>\>\>\>{\tt i\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (first-divisior-<=}\  {\tt (-}\  {\tt i}\  {\tt 1))\char'135}{\tt )\char'135}{\tt )))}
\\
\>\>{\tt (first-divisior-<=}\  {\tt (min}\  {\tt m}\  {\tt n))))}
\end{tabbing}\end{minipage}\end{center}
The conditions ``evenly divisible'' have been encoded as {{\tt (=}{ }{\tt (remainder}\ {\tt n}\ {\tt i)}\ {\tt 0)}\/} and {{\tt (=}\ {\tt (remainder}\ {\tt m}\ {\tt i)}\ {\tt 0)}\/}. The two ensure that
{{\tt i}\/} divides {{\tt n}\/} and {{\tt m}\/} without a remainder.  Testing
{{\tt gcd-structural}\/} with the examples shows that it finds the expected
answers.

Although the design of {{\tt gcd-structural}\/} is rather straightforward,
it is also naive. It simply tests every number whether it divides 
both {{\tt n}\/} and {{\tt m}\/} evenly and returns the first such number. 
For small natural numbers, this process works just fine. Consider the
following example, however: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (gcd-structural}\  {\tt 101135853}\  {\tt 45014640)}\end{tabbing}\end{minipage}\end{center}
The result is {{\tt 177}\/} and to get there {{\tt gcd-structural}\/} had to
compare 101135676, that is, $101135853 - 177$, numbers. This is a large effort
and even reasonably fast computers spend several minutes on this task.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 26.3.1}

 \label{ex:gcd-struct-time}
Enter the definition of {{\tt gcd-structural}\/} into the
{\tt Definitions} window and evaluate {{\tt (time}\ {\tt (gcd-structural}{ }{\tt 101135853}\ {\tt 45014640))}\/} in the {\tt Interactions} window.

\noindent{\bf Hint:}  Once {{\tt gcd-structural}\/} is tested, switch to the MzScheme
language (without debugging). It runs programs faster than the lower
language levels but offers less protection. Add 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (require-library}\  {\tt ``macro.ss'')}\end{tabbing}\end{minipage}\end{center}
to the top of the {\tt Definitions} window. Have some reading
handy!~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/gcd-struct-time.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


\relax 
Since mathematicians recognized the inefficiency of the ``structural
algorithm'' a long time ago, they studied the problem of finding divisiors
in more depth. The essential insight is that for two natural numbers
{{\tt larger}\/} and {{\tt smaller}\/}, their greatest common divisor is
equal to the greatest common divisior of {{\tt smaller}\/} and the
{{\tt remainder}\/} of {{\tt larger}\/} divided into {{\tt smaller}\/}. Here
is how we can put this insight into equational form: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (gcd}\  {\tt larger}\  {\tt smaller)}\  \\
{\tt =}\  {\tt (gcd}\  {\tt smaller}\  {\tt (remainder}\  {\tt larger}\  {\tt smaller))}
\end{tabbing}\end{minipage}\end{center}
Since {{\tt (remainder}\ {\tt larger}\ {\tt smaller)}\/} is smaller than both
{{\tt larger}\/} and {{\tt smaller}\/}, the right-hand side use of
{{\tt gcd}\/} consumes {{\tt smaller}\/} first. 

Here is how this insight applies to our small example: 
\begin{enumerate}
\item The given numbers are {{\tt 18}\/} and {{\tt 24}\/}. 

\item According to the mathematicians' insight, they have the same greatest
common divisor as {{\tt 18}\/} and {{\tt 6}\/}.

\item And these two have the same greatest common divisor as {{\tt 6}\/} and
{{\tt 0}\/}.
\end{enumerate}
And here we seem stuck because {{\tt 0}\/} is nothing expected. But,
{{\tt 0}\/} can be evenly divided by every number, so we have 
found our answer: {{\tt 6}\/}.

Working through the example not only explains the idea but also suggests
how to discover the case with a trivial solution. When the smaller of the 
two numbers is {{\tt 0}\/}, the result is the larger number. Putting
everything together, we get the following definition:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt gcd-generative}\ {\tt :}\ {\bf N}{\tt \char'133}{\tt >=}\ {\tt 1\char'135}\ {\bf N}{\tt \char'133}{\tt >=1\char'135}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to find the greatest common divisior of {{\tt n}\/} and {{\tt m}\/}}
\\
\hbox{;; {\bf generative} recursion: {{\tt (gcd}\ {\tt n}\ {\tt m)}\/} = {{\tt (gcd}\ {\tt n}\ {\tt (remainder}\ {\tt m}\ {\tt n))}\/} if {{\tt (<=}\ {\tt m}\ {\tt n)}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (gcd-generative}\  {\tt n}\  {\tt m)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (clever-gcd}\  {\tt larger}\  {\tt smaller)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (=}\  {\tt smaller}\  {\tt 0)}\  {\tt larger\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (clever-gcd}\  {\tt smaller}\  {\tt (remainder}\  {\tt larger}\  {\tt smaller))\char'135}{\tt )))}
\\
\>\>{\tt (clever-gcd}\  {\tt (max}\  {\tt m}\  {\tt n)}\  {\tt (min}\  {\tt m}\  {\tt n))))}
\end{tabbing}\end{minipage}\end{center}
The {{\tt local}\/} definition introduces the workhorse of the function:
{{\tt clever-gcd}\/}, a function based on generative recursion.  Its first
line discovers the trivially solvable case by comparing {{\tt smaller}\/} to
{{\tt 0}\/} and produces the matching solution.  The generative step uses
{{\tt smaller}\/} as the new first argument and {{\tt (remainder}\ {\tt larger}{ }{\tt smaller)}\/} as the new second argument to {{\tt clever-gcd}\/}, exploiting the
above equation.

If we now use {{\tt gcd-generative}\/} with our complex example from above:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (gcd-generative}\  {\tt 101135853}\  {\tt 45014640)}\end{tabbing}\end{minipage}\end{center}
we see that the response is nerly instantaneous. A hand-evaluation shows
that {{\tt clever-gcd}\/} recurs only nine times before it produces the
solution: {{\tt 177}\/}. In short, the use of generative recursion has 
helped find us a much faster solution to our problem. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 26.3.2}

 \label{ex:gcd-gen-questions}
Formulate informal answers to the four key questions for
{{\tt gcd-generative}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/gcd-gen-questions.html}{\rule{3pt}{5pt}}


{\bf Exercise 26.3.3}

 \label{ex:gcd-gen-time}
Enter {{\tt gcd-generative}\/} into the {\tt Definitions} window and
evaluate {{\tt (time}\ {\tt (gcd-generative}\ {\tt 101135853}\ {\tt 45014640))}\/} in the
{\tt Interactions} window.

Evaluate {{\tt (clever-gcd}\ {\tt 101135853}\ {\tt 45014640)}\/} by hand. Show only those lines
that introduce a new recursive call to
{{\tt clever-gcd}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/gcd-gen-time.html}{\rule{3pt}{5pt}}  


{\bf Exercise 26.3.4}

 \label{ex:gcd-gen-term}
Formulate a termination argument for
{{\tt gcd-generative}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/gcd-gen-term.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt} 


Considering the above example, it is tempting to develop functions using
generative recursion. After all, they produce answers faster! This
judgement is too rash for three reasons. First, even a well-designed
algorithm isn't always faster than an equivalent structurally recursive
function. For example, {{\tt quick-sort}\/} wins only for large lists; for
small ones, the standard {{\tt sort}\/} function is faster. Worse, a badly
designed algorithm can wreck havoc on the performance of a program. Second,
it is typically easier to design a function using the recipe for structural
recursion. Conversely, designing an algorithm requires an idea of how to
generate new, smaller problems, a step that often requires deep
mathematical insight. Finally, people who read functions can easily
understand structurally recursive functions, even without much
documentation. To understand an algorithm, the generative step must be
well-explained, and even with a good explanation, it may still be difficult
to grasp the idea. 

Experience shows that most functions in a program employ structural
recursion; only a few exploit generative recursion. When we encounter a
situation where a design could use either the recipe for structural or
generative recursion, the best approach is often to start with a structural
version. If it turns out to be too slow, the alternative design using
generative recursion should be explored. If it is chosen, it is important
to document the problem generation with good examples and to give a good
termination argument. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 26.3.5}

 \label{ex:quick-sort-time1}
Evaluate  
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (quick-sort}\  {\tt (list}\  {\tt 10}\  {\tt 6}\  {\tt 8}\  {\tt 9}\  {\tt 14}\  {\tt 12}\  {\tt 3}\  {\tt 11}\  {\tt 14}\  {\tt 16}\  {\tt 2))}\end{tabbing}\end{minipage}\end{center}
by hand. Show only those lines that introduce a new recursive call to
{{\tt quick-sort}\/}. How many recursive applications of {{\tt quick-sort}\/}
are required? How many recursive applications of {{\tt append}\/}? Suggest a
general rule for a list of length {{\tt N}\/}.

Evaluate 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (quick-sort}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4}\  {\tt 5}\  {\tt 6}\  {\tt 7}\  {\tt 8}\  {\tt 9}\  {\tt 10}\  {\tt 11}\  {\tt 12}\  {\tt 13}\  {\tt 14)}\end{tabbing}\end{minipage}\end{center}
by hand. How many recursive applications of {{\tt quick-sort}\/} are
required? How many recursive applications of {{\tt append}\/}?  Does this
contradict the first part of the exercise?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/quick-sort-time1.html}{\rule{3pt}{5pt}}


{\bf Exercise 26.3.6}

 \label{ex:quick-sort-time2}
Enter {{\tt sort}\/} and {{\tt quick-sort}\/} into the {\tt Definitions}
window. Test the functions and then explore how fast each works on various
lists. The experiment should confirm the claim that the plain {{\tt sort}\/}
function wins (in many comparisons) over {{\tt quick-sort}\/} for short
lists and vice versa. Determine the cross-over point. Then build a
{{\tt sort-quick-sort}\/} function that behaves like {{\tt quick-sort}\/} for
large lists and switches over to the plain {{\tt sort}\/} function for lists
below the cross-over point.

\noindent{\bf Hints:}  (1) Use the ideas of exercise~\ref{ex:quick-sort-time1} to create
test cases. (2) Develop {{\tt create-tests}\/}, a function that creates
large test cases randomly. Then evaluate 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt test-case}\  {\tt (create-tests}\  {\tt 10000))}\\
{\tt (collect-garbage)}
\\
{\tt (time}\  {\tt (sort}\  {\tt test-case))}
\\
{\tt (collect-garbage)}
\\
{\tt (time}\  {\tt (quick-sort}\  {\tt test-case))}
\end{tabbing}\end{minipage}\end{center}
The uses of {{\tt collect-garbage}\/} helps DrScheme deal with large
lists.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/quick-sort-time2.html}{\rule{3pt}{5pt}}


\section{Variations on a Theme}

As we have seen in the previous two sections, the design of an algorithm
usually starts with an informal description of a mechanism. The kernel of
this description is about how to create a problem that is more easily
solvable than the given one and whose solution contributes to the solution
of the given problem. Coming up with such ideas requires studying many
different examples.  This section presents several illustrative examples of
the design recipe for generative recursion. Some are directly drawn from
mathematics, which is the source of many ideas for general problem-solving
processes; others come from computational contexts. The important point is
to understand the generative ideas behind the algorithms so that they
can be applied in other contexts.

The first example is a graphical illustration of our principle: the
Sierpinski triangle. The second one concerns ``parsing,'' that is, the
process of dissecting sequences of symbols. The third one explains the
divide-and-conquer principle with a simple mathematical example: finding
the root of a function. Many mathematical processes exploit this idea and
is important to understand it for applied mathematics.  In the fourth
section, we discuss yet another way of finding a root, this time based on
Newton's method. The last section is an extended exercise; it introduces
Gaussian elimination, the first step in solving a system of equations.

\subsection{Fractals}

``[G]eometry can be extended to account for objects with a fractional
dimension. Such objects, known as {\sl fractals\/}, come very close 
to capturing the richness and variety of forms found in nature. Fractals 
possess structural self-similarity on multiple \ldots\ scales, meaning that
a piece of a fractal will often look like the whole'' (Flake, {\it The Computational Beauty of Nature\/}, The MIT Press 1998.) 

Here is an example of a fractal, known as the Sierpinski triangle:\label{pic:siepic}

  \begin{rawhtml}  <table  cellspacing=20 bgcolor=beige>  <tr> <td align=left><img src=../icons/sie1.gif></td>       <td align=center><img src=../icons/sie2.gif></td>       <td align=right><img src=../icons/sie3.gif></td>  </tr>  </table>  \end{rawhtml}

\noindent The basic shape is an (equilateral) triangle, as shown in the
leftmost picture.  In the rightmost example we see that the triangle is
repated many times and in many different sizes inside of the outermost
triangle. The picture in the middle is a snapshot from the middle of the
drawing process. 

The middle picture also suggests what the generative step might look like.
Given the three endpoints of a triangle, we draw the triangle and then
compute the midpoint of each side. If we were to connect these midpoints to
each other, we would divide the given triangle into four triangles. The
middle picture illustrates this idea. The Sierpinski triangle is the result
of repeating the process for the three outer triangles and leaving the inner
one alone.

A function that draws this nest of triangles must mirror this process. Its
input data must represent the triangle that we start with. The process
stops when the input data specifies a triangle that is too small to be
drawn. Since all of our drawing functions produce {{\sf true}\/} when they are
done, we agree that our Sierpinski function should also produce
{{\sf true}\/}.

If the given triangle is still large enough, the function must draw the
triangle and possibly some nested ones. The trick is to translate the
partitioning of the triangle into Scheme. Let us summarize our discussion
with a skeletal Scheme definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sierpinski}\ {\tt :}\ {\tt posn}\ {\tt posn}\ {\tt posn}\ {}{\tt ->}{}\ {\sf true}\/}}\\
\hbox{;; to draw a Sierpinski triangle down at {{\tt a}\/}, {{\tt b}\/}, and {{\tt c}\/}, }
\\
\hbox{;; assuming it is large enough}
\\
{\tt (d}\={\tt efine}\  {\tt (sierpinski}\  {\tt a}\  {\tt b}\  {\tt c)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (too-small\char'077}\  {\tt a}\  {\tt b}\  {\tt c)}\  {\sf true}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (draw-triangle}\  {\tt a}\  {\tt b}\  {\tt c)}\  {\tt ...}\  {\tt \char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The function consumes three {{\tt posn}\/} structures and returns {{\sf true}\/}
when it is done. The {{\bf cond}-exp\-res\-sion} reflects the general outline
of an algorithm. It is our task to define {{\tt too-small\char'077}\/}, the function
that determines whether the problem is trivially solvable, and
{{\tt draw-triangle}\/}. In addition, we must still add a Scheme expression
that formulates the partitioning of the triangle. 

The partitioning step requires the function to determine the three mid-points
between the three end-points. Let us call these new mid-points
{{\tt a-b}\/}, {{\tt b-c}\/}, and {{\tt c-a}\/}. Together with the given
endpoints, {{\tt a}\/}, {{\tt b}\/}, and {{\tt c}\/}, they determine four
triangles:
{{\tt a\char'054}\ {\tt a-b\char'054}\ {\tt c-a}\/};
{{\tt b\char'054}\ {\tt a-b\char'054}\ {\tt b-c}\/};
{{\tt c\char'054}\ {\tt c-a\char'054}\ {\tt b-c}\/};
{{\tt a-b\char'054}\ {\tt b-c\char'054}\ {\tt c-a}\/}.
Thus, if we wanted to create the Sierpinski triangle for, say, the first
listed triangle, we would use {{\tt (sierpinski}\ {\tt a}\ {\tt a-b}\ {\tt c-a)}\/}. 

Since each midpoint is used twice, we use a {{\bf local}-exp\-res\-sion} to
translate the generative step into Scheme. The {{\bf local}-exp\-res\-sion}
introduces the three new midpoints. Its body contains three recursive
applications of {{\tt sierpinski}\/} and the {{\tt draw-triangle}\/}
application mentioned earlier. To combine the solutions of the three
problems, we use an {{\bf and}-exp\-res\-sion}, which ensures that all three
recursions must succeed. Figure~\ref{fig:sierpinski-code} collects all the
relevant definitions, including two small functions based on domain
knowledge from geometry. 
		
\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sierpinski}\ {\tt :}\ {\tt posn}\ {\tt posn}\ {\tt posn}\ {}{\tt ->}{}\ {\sf true}\/}}\\
\hbox{;; to draw a Sierpinski triangle down at {{\tt a}\/}, {{\tt b}\/}, and {{\tt c}\/},}
\\
\hbox{;; assuming it is large enough}
\\
{\tt (d}\={\tt efine}\  {\tt (sierpinski}\  {\tt a}\  {\tt b}\  {\tt c)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (too-small\char'077}\  {\tt a}\  {\tt b}\  {\tt c)}\  {\sf true}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}\  
\\
\>\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt a-b}\  {\tt (mid-point}\  {\tt a}\  {\tt b))}
\\
\>\>\>\>\>{\tt (define}\  {\tt b-c}\  {\tt (mid-point}\  {\tt b}\  {\tt c))}
\\
\>\>\>\>\>{\tt (define}\  {\tt c-a}\  {\tt (mid-point}\  {\tt a}\  {\tt c)))}
\\
\>\>\>\>{\tt (a}\={\tt nd}
\\
\>\>\>\>\>{\tt (draw-triangle}\  {\tt a}\  {\tt b}\  {\tt c)}\  \  \  \  \  
\\
\>\>\>\>\>{\tt (sierpinski}\  {\tt a}\  {\tt a-b}\  {\tt c-a)}
\\
\>\>\>\>\>{\tt (sierpinski}\  {\tt b}\  {\tt a-b}\  {\tt b-c)}
\\
\>\>\>\>\>{\tt (sierpinski}\  {\tt c}\  {\tt c-a}\  {\tt b-c)))\char'135}{\tt ))}
\\
\hbox{;; {{\tt mid-point}\ {\tt :}\ {\tt posn}\ {\tt posn}\ {}{\tt ->}{}\ {\tt posn}\/}}
\\
\hbox{;; to compute the mid-point between {{\tt a-posn}\/} and {{\tt b-posn}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (mid-point}\  {\tt a-posn}\  {\tt b-posn)}
\\
\>{\tt (m}\={\tt ake-posn}
\\
\>\>{\tt (mid}\  {\tt (posn-x}\  {\tt a-posn)}\  {\tt (posn-x}\  {\tt b-posn))}
\\
\>\>{\tt (mid}\  {\tt (posn-y}\  {\tt a-posn)}\  {\tt (posn-y}\  {\tt b-posn))))}
\\
\hbox{;; {{\tt mid}\ {\tt :}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to compute the average of {{\tt x}\/} and {{\tt y}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (mid}\  {\tt x}\  {\tt y)}
\\
\>{\tt (/}\  {\tt (+}\  {\tt x}\  {\tt y)}\  {\tt 2))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The Sierpinski algorithm} \label{fig:sierpinski-code}
\hrule

Since {{\tt sierpinski}\/} is based on generative recursion, collecting the
code and testing it is not the last step. We must also consider why the
algorithm terminates for any given legal input. The inputs of
{{\tt sierpinski}\/} are three positions.  The algorithm terminates if the
corresponding triangle is too small. But, each recursive step subdivides
the triangle so that the sum of its sides is only half of the given
triangle. Hence, the size of the triangles indeed decrease and
{{\tt sierpinski}\/} is bound to produce {{\sf true}\/}. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 27.1.1}

 \label{ex:sierpinski-work}
Develop the functions 
\begin{enumerate}
\item ;; {{\tt draw-triangle}\ {\tt :}\ {\tt posn}\ {\tt posn}\ {\tt posn}\ {}{\tt ->}{}\ {\sf true}\/}

\item ;; {{\tt too-small\char'077}\ {\tt :}\ {\tt posn}\ {\tt posn}\ {\tt posn}\ {}{\tt ->}{}\ {\tt bool}\/}
\end{enumerate}
to complete the definitions in figure~\ref{fig:sierpinski-code}. 

Use the teachpack {\tt\bf draw.ss} to test the code. 
For a first test of the complete function, use the following
definitions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt A}\  {\tt (make-posn}\  {\tt 200}\  {\tt 0))}\\
{\tt (define}\  {\tt B}\  {\tt (make-posn}\  {\tt 27}\  {\tt 300))}
\\
{\tt (define}\  {\tt C}\  {\tt (make-posn}\  {\tt 373}\  {\tt 300)}
\end{tabbing}\end{minipage}\end{center}
Create a canvas with {{\tt (start}\ {\tt 400}\ {\tt 400)}\/}.  Experiment with other end
points and canvas dimensions.  \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sierpinski-work.html}{\rule{3pt}{5pt}}



{\bf Exercise 27.1.2}

 \label{ex:sierpinski-start} 
\htmladdimg{../icons/teacher.gif}~{\em domain knowledge -- geometry -- esp some knowledge about {{\tt sin}\/} and {{\tt cos}\/} are helpful -- provide if necessary} 
\relax
The process of drawing a Sierpinski triangle usually starts from an
equilateral shape.  To compute the endpoints of an equilateral Sierpinski
triangle, we can pick a large circle and three points on the circle that
are 120 degrees apart. For example, they could be at 0, 120, 240:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt CENTER}\  {\tt (make-posn}\  {\tt 200}\  {\tt 200))}\\
{\tt (define}\  {\tt RADIUS}\  {\tt 200)}
\\
\hbox{;; {{\tt cicrcl-pt}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt posn}\/}}
\\
\hbox{;; to compute a position on the circle with {{\tt CENTER}\/}}
\\
\hbox{;; and {{\tt RADIUS}\/} as defined above }
\\
{\tt (define}\  {\tt (circle-pt}\  {\tt factor)}\  {\tt ...)}
\\
{\tt (define}\  {\tt A}\  {\tt (circle-pt}\  {\tt 120/360))}
\\
{\tt (define}\  {\tt B}\  {\tt (circle-pt}\  {\tt 240/360))}
\\
{\tt (define}\  {\tt C}\  {\tt (circle-pt}\  {\tt 360/360))}
\end{tabbing}\end{minipage}\end{center}
Develop the function {{\tt circle-pt}\/}. 

\noindent{\bf Hints:}  Recall that DrScheme's {{\tt sin}\/} and {{\tt cos}\/} compute the
sine and cosine in terms of radians, not degrees. Also keep in mind that
on-screen positions grow downwards not upwards.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sierpinski-start.html}{\rule{3pt}{5pt}}


{\bf Exercise 27.1.3}

 \label{ex:sierpinski-struct}
Rewrite the function in figure~\ref{fig:sierpinski-code} to use structures
for the representation of triangles. Then apply the new function to a list 
of triangles and observe the effect.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sierpinski-struct.html}{\rule{3pt}{5pt}}


{\bf Exercise 27.1.4}

 \label{ex:tree-generation}
\htmladdimg{../icons/teacher.gif}~{\em domain knowledge -- geometry -- esp some knowledge about {{\tt sin}\/} and {{\tt cos}\/} are helpful -- provide if necessary} 
\relax
Take a look at the following two pictures: 

  \begin{rawhtml}  <table  cellspacing=20 bgcolor=beige>  <tr> <td align=left><img src=../icons/tree1.gif></td>       <td align=right><img src=../icons/tree2.gif></td>  </tr>  </table>  \end{rawhtml}

\noindent The left one is the basic step for the generation of the
``Savannah'' tree on the right. It is analogous to the middle picture on
page~\pageref{pic:siepic}. Develop a function that draws trees like the one
in the right picture.  

\noindent{\bf Hint:}  Think of the problem as drawing a straight line, given its starting
point and an angle in, say, radians. Then, the generative step divides a
single straight line into three pieces and uses the two intermediate points
as new starting points for straight lines. The angle changes at each
step in a regular manner.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tree-generation.html}{\rule{3pt}{5pt}}


{\bf Exercise 27.1.5}

 \label{ex:bezier}
In mathematics and computer graphics, people must often connect some given
points with a smooth curve. One popular method for this purpose is due to
Bezier.\footnote{Ms.\ Geraldine Morin suggested this exercise.} Here is a
sequence of pictures that illustrate the idea:

  \begin{rawhtml}  <table  cellspacing=20 bgcolor=beige>  <tr> <td align=left><img src=../icons/bezier1.gif></td>       <td align=center><img src=../icons/bezier2.gif></td>       <td align=right><img src=../icons/bezier3.gif></td>  </tr>  </table>  \end{rawhtml}

\noindent For simplicity, we start with three points: {{\tt p1}\/},
{{\tt p2}\/}, and {{\tt p3}\/}. They form the outermost triangle in all three
pictures, with {{\tt p1}\/} being the leftmost and {{\tt p3}\/} the
rightmost point. 

If the triangle is small enough, draw it. It appears as a large point. If
not, generate two smaller triangles as illustrated in the center
picture. The outermost points, {{\tt p1}\/} and {{\tt p3}\/}, remain the end
points. The new center points, {{\tt r2}\/} and {{\tt q2}\/}, are the
mid-points between {{\tt p1}\/} and {{\tt p2}\/} and between {{\tt p3}\/} and
{{\tt p2}\/}, respectively. The new leftmost and rightmost endpoints,
respectively, is the midpoint between {{\tt r2}\/} and {{\tt q2}\/}.

To test the function, use the teachpack {\tt\bf draw.ss}. Here is some
good test data: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt p1}\  {\tt (make-posn}\  {\tt 50}\  {\tt 50))}\\
{\tt (define}\  {\tt p2}\  {\tt (make-posn}\  {\tt 150}\  {\tt 150))}
\\
{\tt (define}\  {\tt p3}\  {\tt (make-posn}\  {\tt 250}\  {\tt 100))}
\end{tabbing}\end{minipage}\end{center} 
Use {{\tt (start}\ {\tt 300}\ {\tt 200)}\/} to create the canvas. Experiment with other
positions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/bezier.html}{\rule{3pt}{5pt}}





\subsection{From Files to Lines, From Lists to Lists of Lists}

In section~\ref{sec:files}, we discussed the organization of computer
files, which is one way to equip a computer with permanent memory. We did
not discuss the nature of files {\em per se\/}. Roughly put, we can think
of a {\sl file\/} as a list of symbols.

  \begin{quote} A {\sl {file}} is either     \begin{enumerate}     \item {{\tt empty}\/}, or     \item {{\tt (cons}\ {\tt s}\ {\tt f)}\/}          where {{\tt s}\/} is a symbol and {{\tt f}\/} is a file.      \end{enumerate}    \end{quote}

\noindent A fully faithful representation of files should only include
symbols that correspond to characters, but for our purposes we may ignore
this distinction.

Following a tradition that predates computers\footnote{The tradition of breaking a file into lines dates back to the 1890 census and makes very little sense for file storage in modern computing. Lines are meaningful only in a context where the user interacts with a program concerning a document. But this concern should be separated from the storage of information in a file. Unfortunately, such non-technical factors often affect the development of computing and software technology in a negative manner.}, one symbol is almost always treated differently:
{{\tt \char'047}{\tt NL}\/}. The symbol stands for {\sl newline\/} and separates two lines
from each other. That is, {{\tt \char'047}{\tt NL}\/} indicates the end of one line and
the beginning of another. In most cases, it is therefore better to think of
files as data with more structure. In particular, a file could be
represented as a list of lines, where each line is a list of symbols.

For example, the file 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  \={\tt \char'047}{\tt how}\  {\tt \char'047}{\tt are}\  {\tt \char'047}{\tt you}\  {\tt \char'047}{\tt NL}\\
\>{\tt \char'047}{\tt doing}\  {\tt \char'047}{\tt \char'077}\  {\tt \char'047}{\tt NL}
\\
\>{\tt \char'047}{\tt any}\  {\tt \char'047}{\tt progress}\  {\tt \char'047}{\tt \char'077}{\tt )}
\end{tabbing}\end{minipage}\end{center}
should be processed as a list of three lines: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  \={\tt (list}\  {\tt \char'047}{\tt how}\  {\tt \char'047}{\tt are}\  {\tt \char'047}{\tt you)}\\
\>{\tt (list}\  {\tt \char'047}{\tt doing}\  {\tt \char'047}{\tt \char'077}{\tt )}
\\
\>{\tt (list}\  {\tt \char'047}{\tt any}\  {\tt \char'047}{\tt progress}\  {\tt \char'047}{\tt \char'077}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Similarly, the file
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  \={\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c}\  {\tt \char'047}{\tt NL}\\
\>{\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e}\  {\tt \char'047}{\tt NL}
\\
\>{\tt \char'047}{\tt f}\  {\tt \char'047}{\tt g}\  {\tt \char'047}{\tt h}\  {\tt \char'047}{\tt NL)}
\end{tabbing}\end{minipage}\end{center}
is also represented as a list of three lines 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  \={\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)}\\
\>{\tt (list}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt f}\  {\tt \char'047}{\tt g}\  {\tt \char'047}{\tt h))}
\end{tabbing}\end{minipage}\end{center}
because, by convention, an empty line at the end is ignored. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 
{\bf Exercise 27.2.1}

 \label{ex:test-file-lines}
Determine what the list-of-lines representation for {{\tt empty}\/},
{{\tt (list}\ {\tt \char'047}{\tt NL)}\/}, and {{\tt (list}\ {\tt \char'047}{\tt NL}\ {\tt \char'047}{\tt NL)}\/} should be. Why are these
examples important test cases?

\noindent{\bf Hint:} \ Keep in mind, that an empty line at the end is
ignored.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:test-file-lines.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt} 


Here are the contract, purpose statement, and header: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\ {\tt :}\ {\tt file}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt (listof}\ {\tt symbols))}\/}}\\
\hbox{;; to convert a file into a list of lines }
\\
{\tt (define}\  {\tt (}\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\  {\tt afile)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Describing the process of separating a file into a list of lines is
easy. The problem is trivially solvable if the file is {{\tt empty}\/}; in
that case, the file doesn't contain a line. Otherwise, the file contains at
least one symbol and thus at least one line.  This line must be separated
from the rest of the file, and then the rest of the file must be translated
into a list of lines.

Let us sketch this process description in Scheme: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (}\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\  {\tt afile)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt afile)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt ...}\  {\tt (first-line}\  {\tt afile)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (}\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\  {\tt (remove-first-line}\  {\tt afile))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Because the separation of the first line from the rest of the file requires
a scan of an arbitrarily long list of symbols, we add two auxiliary
functions to our wish list: {{\tt first-line}\/}, which collects all symbols
up to, but excluding, the first occurrence of {{\tt \char'047}{\tt NL}\/} or the end of
the list; and {{\tt remove-first-line}\/}, which removes all those symbols
and produces the remainder of {{\tt afile}\/}. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\ {\tt :}\ {\tt file}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt (listof}\ {\tt symbol))}\/}}\\
\hbox{;; to convert a file into a list of lines }
\\
{\tt (d}\={\tt efine}\  {\tt (}\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\  {\tt afile)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt afile)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (cons}\  \={\tt (first-line}\  {\tt afile)}
\\
\>\>\>\>{\tt (}\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\  {\tt (remove-first-line}\  {\tt afile)))\char'135}{\tt ))}
\\
\hbox{;; {{\tt first-line}\ {\tt :}\ {\tt file}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt symbol)}\/}}
\\
\hbox{;; to compute the prefix of {{\tt afile}\/} up to the first occurrence of {{\tt NEWLINE}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (first-line}\  {\tt afile)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt afile)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt (first}\  {\tt afile)}\  {\tt NEWLINE)}\  {\tt empty\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt (first}\  {\tt afile)}\  {\tt (first-line}\  {\tt (rest}\  {\tt afile)))\char'135}{\tt )\char'135}{\tt ))}
\\
\hbox{;; {{\tt remove-first-line}\ {\tt :}\ {\tt file}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt symbol)}\/}}
\\
\hbox{;; to compute the suffix of {{\tt afile}\/} behind the first occurrence of {{\tt NEWLINE}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (remove-first-line}\  {\tt afile)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt afile)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt (first}\  {\tt afile)}\  {\tt NEWLINE)}\  {\tt (rest}\  {\tt afile)\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (remove-first-line}\  {\tt (rest}\  {\tt afile))\char'135}{\tt )\char'135}{\tt ))}
\\
{\tt (define}\  {\tt NEWLINE}\  {\tt \char'047}{\tt NL)}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Translating a file into a list of lines}\label{fig:file2list}
\hrule

From here, we can fill the gaps easily. In {\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\/},
the answer in the first clause must be {{\tt empty}\/} because an empty file
does not contain any lines. The answer in the second clause must
{{\tt cons}\/} the value of {{\tt (first-line}\ {\tt afile)}\/} onto the value
{{\tt (}\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\ {\tt (remove-first-line}\ {\tt afile))}\/}, because the first
expression computes the first line and the second one computes the rest of
the lines. Finally, the auxiliary functions process their inputs in a
structurally recursive manner; their development is a straightforward
exercise. Figure~\ref{fig:file2list} collects the three function
definitions and a variable definition for {{\tt NEWLINE}\/}.

Let us take a look at the process of turning the first file from above into a list
of lines: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (}\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c}\  {\tt \char'047}{\tt NL}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e}\  {\tt \char'047}{\tt NL}\  {\tt \char'047}{\tt f}\  {\tt \char'047}{\tt g}\  {\tt \char'047}{\tt h}\  {\tt \char'047}{\tt NL))}\\
{\tt =}\  {\tt (cons}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)}\  {\tt (}\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\  {\tt (list}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e}\  {\tt \char'047}{\tt NL}\  {\tt \char'047}{\tt f}\  {\tt \char'047}{\tt g}\  {\tt \char'047}{\tt h}\  {\tt \char'047}{\tt NL)))}
\\
{\tt =}\  {\tt (cons}\  \={\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)}
\\
\>{\tt (cons}\  \={\tt (list}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e)}
\\
\>\>{\tt (}\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\  {\tt (list}\  {\tt \char'047}{\tt f}\  {\tt \char'047}{\tt g}\  {\tt \char'047}{\tt h}\  {\tt \char'047}{\tt NL))))}
\\
{\tt =}\  {\tt (cons}\  \={\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)}
\\
\>{\tt (cons}\  \={\tt (list}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e)}
\\
\>\>{\tt (cons}\  \={\tt (list}\  {\tt \char'047}{\tt f}\  {\tt \char'047}{\tt g}\  {\tt \char'047}{\tt h)}
\\
\>\>\>{\tt (}\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\  {\tt empty))))}
\\
{\tt =}\  {\tt (cons}\  \={\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)}
\\
\>{\tt (cons}\  \={\tt (list}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e)}
\\
\>\>{\tt (cons}\  \={\tt (list}\  {\tt \char'047}{\tt f}\  {\tt \char'047}{\tt g}\  {\tt \char'047}{\tt h)}
\\
\>\>\>{\tt empty)))}
\\
{\tt =}\  {\tt (list}\  \={\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt d}\  {\tt \char'047}{\tt e)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt f}\  {\tt \char'047}{\tt g}\  {\tt \char'047}{\tt h))}
\end{tabbing}\end{minipage}\end{center}
From this evaluation we can easily tell that the argument of the recursive
application of {\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\/} is almost never the rest of the
given file. That is, it is basically never an immediate component of the
given file but always a proper suffix. The only exception occurs when
{{\tt \char'047}{\tt NL}\/} occurs twice in a row.  

Finally, the evaluation and the definition of {\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-lines}}\/}
show that its generative recursion is simple. Every recursive application
consumes a list that is shorter than the given one. Hence, the recursive
process eventually stops because the function consumes {{\tt empty}\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 27.2.2}

 \label{ex:abstract-file}
Organize the program in figure~\ref{fig:file2list} using {{\tt local}\/}. 

Abstract the functions {{\tt first-line}\/} and {{\tt remove-first-line}\/}.
Then organize the resulting program using a {{\tt local}\/}
again.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/abstract-file.html}{\rule{3pt}{5pt}} 


{\bf Exercise 27.2.3}

 \label{ex:restaurant}
Design {\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-checks}}\/}.  The function consumes a file of
numbers and the symbol {{\tt \char'047}{\tt table}\/} and outputs a list of restaurant
records.

  \begin{quote} A {\sl {file of numbers}} is either     \begin{enumerate}       \item {{\tt empty}\/}, or       \item {{\tt (cons}\ {\tt N}\ {\tt F)}\/}          where {{\tt N}\/} is a number and {{\tt F}\/} is a       file, or       \item {{\tt (cons}\ {\tt \char'047}{\tt NL}\ {\tt F)}\/}, where {{\tt F}\/} is a file.      \end{enumerate} \end{quote}


The output of {\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-checks}}\/} is a list of restaurant structures:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt rr}\  {\tt (table}\  {\tt costs))}\end{tabbing}\end{minipage}\end{center}
The first field represents a table number, the second one a list of amounts
charged to that table.

\noindent Example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (}\mbox{{\it file\/}}{{}{\tt ->}{}}\mbox{{\it list-of-checks}}\=\\
\>{\tt (list}\  \  \={\tt 1}\  {\tt 2.30}\  {\tt 4.00}\  {\tt 12.50}\  {\tt 13.50}\  {\tt \char'047}{\tt NL}
\\
\>\>{\tt 2}\  {\tt 4.00}\  {\tt 18.00}\  {\tt \char'047}{\tt NL}
\\
\>\>{\tt 4}\  {\tt 2.30}\  {\tt 12.50))}
\\
{\tt =}\  {\tt (list}\  \={\tt (make-rr}\  {\tt 1}\  {\tt (list}\  {\tt 2.30}\  {\tt 4.00}\  {\tt 12.50}\  {\tt 13.50))}
\\
\>{\tt (make-rr}\  {\tt 2}\  {\tt (list}\  {\tt 4.00}\  {\tt 18.00))}
\\
\>{\tt (make-rr}\  {\tt 4}\  {\tt (list}\  {\tt 2.30}\  {\tt 12.50)))}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:restaurant.html}{\rule{3pt}{5pt}}


{\bf Exercise 27.2.4}

 \label{ex:matrix-gen}
Develop the function {{\tt create-matrix}\/}. It consumes a number
{{\tt n}\/} and a list of $n^2$ numbers. It produces a list of {{\tt n}\/}
lists of {{\tt n}\/} numbers. 

\noindent Example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (create-matrix}\  {\tt 2}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3}\  {\tt 4))}\\
{\tt =}\  {\tt (list}\  \={\tt (list}\  {\tt 1}\  {\tt 2)}
\\
\>{\tt (list}\  {\tt 3}\  {\tt 4))}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/matrix-gen.html}{\rule{3pt}{5pt}}


\subsection{Binary Search} \label{sec:binary-search}

Applied mathematicians model the real-world with non-linear equations and
are then confronted with the task of solving them. Here is a simplistic
example:
\begin{quote}
Given a perfect cube that encloses 27m${}^3$. What area do its six
walls cover? 
\end{quote}
We know from geometry that if the length of a cube's side is $x$, the
enclosed space is $x^3$. Hence, we need to know the possible values of {\it x\/} such that
$$ x^3 = 27 \ . $$
Once we solved the equation, the covered area is $6 \cdot x^2$.

In general, we are given a function {\it f\/} from numbers to numbers, and
want to know some number $r$ such that 
$$ f(r) = 0 \ . $$
The value {{\tt r}\/} is called the {\sl root\/} of {\it f\/}.  In our above
example, $f(x) = x^3 - 27$, and the value {\it r\/} is the length of the
side of the cube.\footnote{If the equation is originally presented as $g(x) = h(x)$, we set $f(x) = g(x) - h(x)$ to transform the equation into the standard form.}  

For the past few centuries, mathematicians have developed many methods for
finding the root of different types of functions. In this section, we study
a solution that is based on the {\bf Mean Value Theorem\/}, an early result
of mathematical analysis. The resulting algorithm is a primary example of
generative recursion based on a deep mathematical theorem. It has been
adapted to other uses and has become known as the ``binary search
algorithm'' in computer science.

\hrule
\[ \epsfbox{icons/rootf.eps} \]
\center{Figure: A numeric function {\it f\/} with root in interval $[a,b]$ (stage~1)}
\label{fig:root}

\hrule

The Mean Value Theorem says that a continuous function $f$ has a root in an
 interval $[a,b]$ if the signs of $f(a)$ and $f(b)$ differ. By {\sl  continuous\/} we mean a function that doesn't ``jump'', that doesn't have
 gaps, and that always continues in a ``smooth'' fashion. The theorem is best
 illustrated with the graph of a function. The function $f$ in
 figure~\ref{fig:root} is below the $x$-axis at $a$ and above the $x$-axis at
 $b$. It is a continuous function, which we can tell from the uninterrupted,
 smooth line.  And indeed, the function intersects the $x$-axis somewhere
 between $a$ and $b$.

Now take a look at the midpoint between $a$ and $b$:
$$m = \frac{a+b}2 \ . $$ 
It partitions the interval $[a,b]$ into two smaller, equally large
intervals. We can now compute the value of $f$ at $m$ and see whether it is
below or above 0. Here $f(m) > 0$, so according to the Mean Value Theorem,
the root is in the right interval: $[m,b]$. Our picture confirms this
because the root is in the right half of the interval, labeled ``range 2''
in figure~\ref{fig:root}.

The abstract description of the Mean Value Theorem and the illustrative
example describe a process for finding a root. Specifically, we use the
halving step as many times as necessary to determine a tolerably small
range in which $f$ must have a root. Let us now translate this description
into a Scheme algorithm, which we call {{\tt find-root}\/}. 

To begin with, we must agree on the exact task of {{\tt find-root}\/}. It
consumes a function, let's call it {{\tt f}\/}, for which we need to find a
root. In addition, it must consume the boundaries of the interval in which we
expect to find a root. For simplicity, let's say that {{\tt find-root}\/}
consumes two numbers: {{\tt left}\/} and {{\tt right}\/}.  But these parameters
can't just be any two numbers. For our algorithm to work we must assume that
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (or}\  \={\tt (<=}\  {\tt (f}\  {\tt left)}\  {\tt 0}\  {\tt (f}\  {\tt right))}\\
\>{\tt (<=}\  {\tt (f}\  {\tt right)}\  {\tt 0}\  {\tt (f}\  {\tt left)))}
\end{tabbing}\end{minipage}\end{center}
holds. This assumption expresses in Scheme the condition of the Mean Value
Theorem that the function must have different signs for {{\tt left}\/} and
{{\tt right}\/}. 

According to the informal process description, the task of
{{\tt find-root}\/} is to find an interval that contains a root and that is
tolerably small. The size of the given interval is {{\tt (-}\ {\tt right}{ }{\tt left)}\/}. For the moment, we assume that the tolerance is defined as a top-level
variable {{\tt TOLERANCE}\/}. Given that, {{\tt find-root}\/} can produce one
of the two boundaries of the interval because we know what its size is;
let's pick the left one. 

Here is a translation of our discussion into a contract, a purpose
statement, and a header, including the assumption on the parameters: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt find-root}\ {\tt :}\ {\tt (number}\ {}{\tt ->}{}\ {\tt number)}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to determine {{\tt R}\/} such that {{\tt f}\/} has a root in [{{\tt R}\/},{{\tt (+}\ {\tt R}\ {\tt TOLERANCE)}\/}]}
\\
\hbox{;; }
\\
\hbox{;; {\sc Assumption}: {{\tt (or}\ {\tt (<=}\ {\tt (f}\ {\tt left)}\ {\tt 0}\ {\tt (f}\ {\tt right))}\ {\tt (<=}\ {\tt (f}\ {\tt right)}\ {\tt 0}\ {\tt (f}\ {\tt left)))}\/}}
\\
{\tt (define}\  {\tt (find-root}\  {\tt f}\  {\tt left}\  {\tt right)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
At this stage, we should develop an example of how the function works. We
have already seen one; the following exercise develops a second one. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 27.3.1}

 \label{ex:find-root-example}
Consider the following function definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt poly}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (}\={\tt define}\  {\tt (poly}\  {\tt x)}
\\
\>{\tt (*}\  {\tt (-}\  {\tt x}\  {\tt 2)}\  {\tt (-}\  {\tt x}\  {\tt 4)))}
\end{tabbing}\end{minipage}\end{center}
It defines a binomial for which we can determine its roots by hand---they
are {{\tt 2}\/} and {{\tt 4}\/}. But it is also a non-trivial input for
{{\tt find-root}\/}, so that it makes sense to use it as an example.

Mimic the root-finding process based on the Mean Value Theorem for
{{\tt poly}\/}, starting with the interval {{\tt 3}\/} and
{{\tt 6}\/}. Tabulate the information as follows: 
\begin{center}
\begin{tabular}{c|r|r|r|r|r|r}
\mbox{\#step} & {\it left\/} & $(f \mbox{\it left\/})$ & {\it right\/} &
$(f \mbox{\it right\/})$ & {\it mid\/} & $(f \mbox{\it mid\/})$ \\
\hline
      $n = 1$ &     3        &    -1                   & 6.00 & 8.00 & 4.5  & 1.25\\
      $n = 2$ &     3        &    -1                   & 4.25 & 1.25 & ? & ? \\
\end{tabular}
\end{center}
Find an interval of size .5 (or less) in which {{\tt poly}\/} contains a
root.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-root-example.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt} 


Next we turn our attention to the definition of {{\tt find-root}\/}. We start
from {{\tt generative-recursive-fun}\/} and ask the four relevant questions: 
\begin{enumerate}
\item We need a condition that describes when the problem is solved and a
 matching answer. This is straightforward. The problem is solved if the
 distance from {{\tt left}\/} to {{\tt right}\/} is smaller than or equal to
 {{\tt TOLERANCE}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (<=}\  {\tt (-}\  {\tt right}\  {\tt left)}\  {\tt TOLERANCE)}\end{tabbing}\end{minipage}\end{center}
The matching result is {{\tt left}\/}. 

\item We must formulate an expression that generates new problems for
 {{\tt find-root}\/}. According to our informal process description, this step
 requires determining the mid-point and choosing the next interval. The
 midpoint is used several times, so we use a {{\bf local}-exp\-res\-sion} to
 introduce it:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (local}\  {\tt ((define}\  {\tt mid}\  {\tt (/}\  {\tt (+}\  {\tt left}\  {\tt right)}\  {\tt 2)))}\  {\tt ...)}\end{tabbing}\end{minipage}\end{center}
 Choosing an interval is more complicated than that. 

Consider the Mean Value Theorem again. It says that a given interval is an
 interesting candidate if the function values at the boundaries have different
 signs. For the function's purpose statement, we expressed this constraint
 using 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (or}\  {\tt (<=}\  {\tt (f}\  {\tt left)}\  {\tt 0}\  {\tt (f}\  {\tt right))}\  {\tt (<=}\  {\tt (f}\  {\tt right)}\  {\tt 0}\  {\tt (f}\  {\tt left)))}\end{tabbing}\end{minipage}\end{center}
 Accordingly, the interval between {{\tt left}\/} and {{\tt mid}\/} is the next
 candidate if 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (or}\  {\tt (<=}\  {\tt (f}\  {\tt left)}\  {\tt 0}\  {\tt (f}\  {\tt mid))}\  {\tt (<=}\  {\tt (f}\  {\tt mid)}\  {\tt 0}\  {\tt (f}\  {\tt left)))}\end{tabbing}\end{minipage}\end{center}
 And, the interval between {{\tt mid}\/} and {{\tt right}\/} is it, if 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (or}\  {\tt (<=}\  {\tt (f}\  {\tt mid)}\  {\tt 0}\  {\tt (f}\  {\tt right))}\  {\tt (<=}\  {\tt (f}\  {\tt right)}\  {\tt 0}\  {\tt (f}\  {\tt mid)))}\end{tabbing}\end{minipage}\end{center}
In short, the body of the {{\bf local}-exp\-res\-sion} must be a conditional: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt mid}\  {\tt (/}\  {\tt (+}\  {\tt left}\  {\tt right)}\  {\tt 2)))}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (or}\  {\tt (<=}\  {\tt (f}\  {\tt left)}\  {\tt 0}\  {\tt (f}\  {\tt mid))}\  {\tt (<=}\  {\tt (f}\  {\tt mid)}\  {\tt 0}\  {\tt (f}\  {\tt left)))}
\\
\>\>\>{\tt (find-root}\  {\tt left}\  {\tt mid)\char'135}
\\
\>\>{\tt \char'133}\={\tt (or}\  {\tt (<=}\  {\tt (f}\  {\tt mid)}\  {\tt 0}\  {\tt (f}\  {\tt right))}\  {\tt (<=}\  {\tt (f}\  {\tt right)}\  {\tt 0}\  {\tt (f}\  {\tt mid)))}
\\
\>\>\>{\tt (find-root}\  {\tt mid}\  {\tt right)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
 In both clauses, we use {{\tt find-root}\/} to continue the search.
\end{enumerate}
The completed function is displayed in figure~\ref{fig:root-of-f}. The
following exercises suggest some tests and a termination argument. 

\hrule

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt find-root}\ {\tt :}\ {\tt (number}\ {}{\tt ->}{}\ {\tt number)}\ {\tt number}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to determine a number $R$ such that {{\tt f}\/} has a }
\\
\hbox{;; root between R and {{\tt (+}\ {\tt R}\ {\tt TOLERANCE)}\/} }
\\
\hbox{;; }
\\
\hbox{;; {\sc Assumption}: {\it f\/} is continuous and monotonic}
\\
{\tt (d}\={\tt efine}\  {\tt (find-root}\  {\tt f}\  {\tt left}\  {\tt right)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (<=}\  {\tt (-}\  {\tt right}\  {\tt left)}\  {\tt TOLERANCE)}\  {\tt left\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}\  
\\
\>\>\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt mid}\  {\tt (/}\  {\tt (+}\  {\tt left}\  {\tt right)}\  {\tt 2)))}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (<=}\  {\tt (f}\  {\tt mid)}\  {\tt 0}\  {\tt (f}\  {\tt right))}\  
\\
\>\>\>\>\>\>{\tt (find-root}\  {\tt mid}\  {\tt right)\char'135}
\\
\>\>\>\>\>{\tt \char'133}\={\tt else}\  
\\
\>\>\>\>\>\>{\tt (find-root}\  {\tt left}\  {\tt mid)\char'135}{\tt ))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The root-finding algorithm {\it find-root\/}}
\label{fig:root-of-f}
\hrule


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 27.3.2}

 \label{ex:find-root-test}
Use {{\tt poly}\/} from \ref{ex:find-root-example} to test
{{\tt find-root}\/}.  Experiment with different values for
{{\tt TOLERANCE}\/}. Use the strategy of section~\ref{sec:equal-test} to
formulate the tests as boolean-valued
expressions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-root-test.html}{\rule{3pt}{5pt}}



{\bf Exercise 27.3.3}

 \label{ex:find-root-termination}
Suppose the original arguments of {{\tt find-root}\/} describe an interval
of size {{\tt S1}\/}. How large is the distance between {{\tt left}\/} and
{{\tt right}\/} for the first recursive call to {{\tt find-root}\/}? The
second one? And the third?  After how many evaluation steps is the distance
between {{\tt left}\/} and {{\tt right}\/} smaller than or equal to
{{\tt TOLERANCE}\/}? How does the answer to this question show that
{{\tt find-root}\/} produces an answer for all inputs that satisfy the
assumption?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-root-termination.html}{\rule{3pt}{5pt}}


{\bf Exercise 27.3.4}

 \label{ex:find-root-recomp}
For every mid-point {{\tt m}\/}, except for the last one, the
function {{\tt find-root}\/} needs to determine the value of
{{\tt (f}\ {\tt m)}\/} twice. Validate this claim for one example with a
hand-evaluation. 

Since the evaluation of {{\tt (f}\ {\tt m)}\/} may be time-consuming, programmers
often implement a variant of {{\tt find-root}\/} that avoids this
recomputation.  Modify {{\tt find-root}\/} in figure~\ref{fig:root-of-f} so
that it does not need to re-compute the value of {{\tt (f}\ {\tt mid)}\/}. 

\noindent{\bf Hint:}  Define a help function {{\tt find-root-aux}\/} that takes two extra
arguments: the values {{\tt (f}\ {\tt left)}\/} and {{\tt (f}{ }{\tt right)}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-root-recomp.html}{\rule{3pt}{5pt}}


{\bf Exercise 27.3.5}

 \label{ex:find-root-discrete}
A {\sl table\/} is a function that consumes natural numbers between
{{\tt 0}\/} and {{\tt VL}\/} (exclusive) and produces numbers:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt g}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt num}\/}}\\
\hbox{;; {\sc Assumption}: {{\tt i}\/} is between 0 and {{\tt VL}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (g}\  {\tt i)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt i}\  {\tt 0)}\  {\tt -10\char'135}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt i}\  {\tt 1)}\  {\tt ...\char'135}
\\
\>\>{\tt ...}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt i}\  {\tt (-}\  {\tt VL}\  {\tt 1))}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt g}\  {\tt ``is}\  {\tt defined}\  {\tt only}\  {\tt between}\  {\tt 0}\  {\tt and}\  {\tt VL}\  {\tt (exclusive)'')\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The number {{\tt VL}\/} is called the {\sl table's length\/}. The {\sl root of a table} is the number in the table that is closest to {{\tt 0}\/}. Even
if we can't read the definition of a table, we can find its root with a
search function.

Develop the function {{\tt find-root-linear}\/}, which consumes a table, the
table's length, and finds the root of the table. Use structural induction
on natural numbers. This kind of root-finding process is often called a
{\sl linear search\/}.

A table {{\tt t}\/} is sorted in ascending order if {{\tt (t}\ {\tt 0)}\/} is less
then {{\tt (t}\ {\tt 1)}\/}, {{\tt (t}\ {\tt 1)}\/} is less than {{\tt (t}\ {\tt 2)}\/}, and so
on. If a table is monotonic, we can determine the root using binary
search. Specifically, we can use binary search to find an interval of size
{{\tt 1}\/} such that either the left or the right boundary is the root's
index.  Develop {{\tt find-root-discrete}\/}, which consumes a table and its
length, and finds the table's root.

\noindent{\bf Hints:}  (1) The interval boundary arguments for {{\tt find-root-discrete}\/}
must always be natural numbers. Consider how this affects the mid-point
computation. (2) Also contemplate how the first hint affects the discovery
of trivially solvable problem instances. (3) Does the termination argument
from exercise~\ref{ex:find-root-termination} apply?

If the tabulating function is defined on all natural numbers between
{{\tt 0}\/} and {{\tt 1024}\/}, and if its root is at {{\tt 0}\/}, how many
recursive applications are needed with {{\tt find-root-discrete}\/} and
{{\tt find-root-lin}\/} to determine a root
interval?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-root-discrete.html}{\rule{3pt}{5pt}}


{\bf Exercise 27.3.6}

 \label{ex:adaptive-integral}
As mentioned in section~\ref{sec:integrate1}, mathematicians are not only
interested in the roots of functions, but also in the area that a function
encloses between two points. Mathematically put, we are interested in {\sl integrating\/} functions over some interval. Take another look at the
following graph: 

\ \ \ {\htmladdimg{../icons/integrate.jpg}}\\

\noindent Here the area of interest is that enclosed by the bold vertical
lines at $a$ and $b$, the {\it x\/}-axis, and the graph of the
function. 

In section~\ref{sec:integrate1}, we learned to approximate the area by
computing and adding up the area of rectangles like the two above.  Using
the divide-and-conquer strategy, we can also design a function that
computes the area based on generative recursion. Roughly speaking, we split
the interval into two pieces, compute the area of each piece, and add the
two areas together.

\bigskip 

\noindent {\bf Step 1}: Develop the algorithm {{\tt integrate-dc}\/}, which
integrates a function {{\tt f}\/} between the boundaries {{\tt left}\/} and
{{\tt right}\/} via the divide-and-conquer strategy employed in
{{\tt find-root}\/}. Use rectangle approximations when an interval has
become small enough.

Although the area of a rectangle is easy to compute, a rectangle is often
a bad approximation of the area under a function graph. A better geometric
shape is the trapezoid limited by $a$, {{\tt (f}\ {\tt a)}\/}, $b$, and
{{\tt (f}\ {\tt b)}\/}. Its area is: 
$$ 
   (\mbox{\it right} - \mbox{\it left}) 
   \cdot 
   \frac{f (\mbox{\it right}) + f (\mbox{\it left})}
        {2} \ . 
$$

\bigskip 

\noindent {\bf Step 2}: Modify {{\tt integrate-dc}\/} so that it uses
trapezoids instead of rectangles.

The plain divide-and-conquer approach is wasteful. Consider a function
graph is level in one part and rapidly changes in another. For the level
part it is pointless to keep splitting the interval. We could just compute
the trapezoid over $a$ and $b$ instead of the two halves. 

To discover when {\it f\/} is level, we can change the algorithm as
follows. Instead of just testing how large the interval is, the new
algorithm computes the area of three trapezoids: the given one, and the two
halves. Suppose the difference between the two is less than
$$ \mbox{\it TOLERANCE\/} \cdot (\mbox{\it right\/} - \mbox{\it left\/}) \ . $$
This area represents a small rectangle, of height {{\tt TOLERANCE}\/}, and
represents the error margin of our computation. In other words, the
algorithm determines whether {\it f\/} changes enough to affect the error
margin, and if not, it stops. Otherwise, it continues with the
divide-and-conquer approach. 

\bigskip 

\noindent {\bf Step 3}: Develop {{\tt integrate-adaptive}\/}, which
integrates a function {\it f\/} between {\it left\/} and {\it right\/}
according to the suggested method. Do not discuss the termination
of {{\tt integrate-adaptive}\/}.

\noindent {\bf Note}: The algorithm is called ``adaptive integration''
because it automatically adapts its strategy. For those parts of {\it f\/}
that are level, it performs just a few calculations; for the other parts,
it inspects very small intervals so that the error margin is also decreased
accordingly.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/adaptive-integral.html}{\rule{3pt}{5pt}}


\subsection{Newton's Method}

Newton invented another method for finding the root of a function.
Newton's method exploits the idea of an approximation. To search a root of
some function {{\tt f}\/}, we start with a guess, say {{\tt r1}\/}. Then we
study the tangent of {{\tt f}\/} at {{\tt r1}\/}, that is, the line that
goes through the Cartesian point ({{\tt r1}\/}, {{\tt f(r1)}\/}) and has the
same slope as {{\tt f}\/}. This tangent is a linear approximation of
{{\tt f}\/} and it has a root that is in many cases closer to the root of
{{\tt f}\/} than our original guess. Hence, by repeating this process
sufficiently often, we can find an {{\tt r}\/} for which {{\tt (f}\ {\tt r)}\/} is
close to {{\tt 0}\/}.

To translate this process description into Scheme, we follow the familiar
process. The function---let's call it {{\tt newton}\/} in honor of its
inventor---consumes a function {{\tt f}\/} and a number {{\tt r0}\/}, the
current guess. If {{\tt (f}\ {\tt r0)}\/} is close to {{\tt 0}\/}, the problem is
solved. Of course, close to {{\tt 0}\/} could be mean {{\tt (f}\ {\tt r0)}\/} is a
small positive number or a small negative number. Hence, we translate this
idea into 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (<=}\  {\tt (abs}\  {\tt (f}\  {\tt r0))}\  {\tt TOLERANCE)}\end{tabbing}\end{minipage}\end{center}
That is, we determine whether the absolute value is small. The answer in
this case is {{\tt r0}\/}.

The generative step of the algorithm consists of finding the root of the
tangent of {{\tt f}\/} at {{\tt r0}\/}. It generates a new guess. By applying
{{\tt newton}\/} to this new guess, we resume the process with a hopefully
better guess:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt newton}\ {\tt :}\ {\tt (number}\ {}{\tt ->}{}\ {\tt number)}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to find a number {{\tt r}\/} such that {{\tt (<}\ {\tt (abs}\ {\tt (f}\ {\tt r))}\ {\tt TOLERANCE)}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (newton}\  {\tt f}\  {\tt r0)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (<=}\  {\tt (abs}\  {\tt (f}\  {\tt r0))}\  {\tt TOLERANCE)}\  {\tt r0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (newton}\  {\tt f}\  {\tt (find-root-tangent}\  {\tt f}\  {\tt r0))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Since finding the root of a tangent is domain knowledge, we define a
separate function for this purpose:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt find-root-tangent}\ {\tt :}\ {\tt (number}\ {}{\tt ->}{}\ {\tt number)}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to find the root of the tagent of {{\tt f}\/} at {{\tt r0}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (find-root-tangent}\  {\tt f}\  {\tt r0)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt (fprime}\  {\tt (d/dx}\  {\tt f))))}
\\
\>\>{\tt (-}\  \={\tt r0}
\\
\>\>\>{\tt (/}\  \={\tt (f}\  {\tt r0)}
\\
\>\>\>\>{\tt (fprime}\  {\tt r0)))))}
\end{tabbing}\end{minipage}\end{center}
The function first computes {{\tt (d/dx}\ {\tt f)}\/}, that is, the derivative of
{{\tt f}\/} at {{\tt r0}\/} (see~section~\ref{sec:diff1}) at {{\tt r0}\/}. The
body of the {{\bf local}-exp\-res\-sion} computes the root from the current
guess, {{\tt (f}\ {\tt r0)}\/}, and the slope of {{\tt f}\/} at
{{\tt r0}\/}.\footnote{ The tangent of a function $f$ at $r_i$ is the linear function $$\begin{array}{lcl} t(x) & = & f'(r0) \cdot (x - r0) + f(r0)\\
      & = & f'(r0) \cdot x + [f(r0) - f'(r0) \cdot r0] \\
 \end{array}$$ The function $f'$ is the derivative of $f$, and $f'(r0)$ is the slope of $f$ at $r0$. Furthermore, the root of a linear function is the intersection of a straight line with the $x$-axis. In general, if the line's equation is $$y(x) = a \cdot x + b$$ then its root is $-b/a$. In our case, the root of {\it f\/}'s tangent is $$ r - \frac{f(r0)}{f'(r0)} \ . $$ }

The most interesting aspect of {{\tt newton}\/} is that, unlike all other
functions we have discussed, it does {\bf not} always terminate. Consider
the following function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt f}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt x)}
\\
\>{\tt (-}\  {\tt (*}\  {\tt x}\  {\tt x)}\  {\tt x}\  {\tt 1.8))}
\end{tabbing}\end{minipage}\end{center}
A simple hand-calculation shows that its derivative is 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt fprime}\ {\tt :}\ {\tt number}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (fprime}\  {\tt x)}
\\
\>{\tt (-}\  {\tt (*}\  {\tt 2}\  {\tt x)}\  {\tt 1))}
\end{tabbing}\end{minipage}\end{center}
If we were to use {{\tt 1/2}\/} as the initial guess, we would have to find
the root of a tangent with slope {{\tt 0}\/}, that is, a tangent that is
parallel to the {\it x\/}-axis. Of course, such a tangent doesn't have a
root. As a result, {{\tt find-root-of-tangent}\/} cannot find a tangent
and {{\tt newton}\/} won't find a root.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 27.4.1}

 \label{ex:newton-test}
Test {{\tt newton}\/} with {{\tt f}\/}. Use the initial guesses {{\tt 1}\/},
{{\tt 2}\/}, and {{\tt 3}\/}. Also use {{\tt find-root}\/} from the preceding
section to find a root. 

Use a hand-evaluation to determine how quickly {{\tt newton}\/} finds a
value close to the root (if it finds one). Compare {{\tt newton}\/}'s
behavior with {{\tt find-root}\/}'s behavior. 

Employ the strategy of section~\ref{sec:equal-test} to formulate the tests
as boolean-valued expressions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/newton-test.html}{\rule{3pt}{5pt}}
 

\subsection{Extended Exercise: Gaussian Elimination}

Mathematicians not only search for solutions of equations in one variable,
they also study whole systems of linear equations. Here is a sample system
of equations in three variables, $x$, $y$, and $z$:
$$\begin{array}{lclcrcrr}
2 \cdot x & + & 2 \cdot y & + &  3 \cdot z & = & 10 \\
2 \cdot x & + & 5 \cdot y & + & 12 \cdot z & = & 31 & \qquad (\dagger)\\
4 \cdot x & + & 1 \cdot y & - &  2 \cdot z & = &  1
\end{array}$$
A solution to a system of equations is a series of numbers, one per
variable, such that if we replace the variable by its corresponding number, 
the two sides of each equation evaluate to the same number. In our running
example, the solution is $x = 1$, $y = 1$, and $z = 2$ as we can easily check: 
$$\begin{array}{lclcrcrr}
2 \cdot 1 & + & 2 \cdot 1 & + &  3 \cdot 2 & = & 10 \\
2 \cdot 1 & + & 5 \cdot 1 & + & 12 \cdot 2 & = & 31 & \\
 4 \cdot 1 & + & 1 \cdot 1 & - &  2 \cdot 2 & = &  1
\end{array}$$
The first equation now reads as $10 = 10$, the second one as $31 = 31$, and the
last one as $1 =1$. 

One of the most famous methods for finding a solution is called Gaussian
elimination. It consists of two steps. The first step is to transform the
system of equations into a system of different shape but with the
same solution. The second step is to find solutions to one equation
at a time. Here we focus on the first step because it is another interesting
instance of generative recursion.

The first step of the Gaussiam elminiation algorithm is called ``triangulation''
because the result is a system of equations in the shape of a triangle. In
contrast, the original system is typically a rectangle. To understand this
terminology, take a look at this representation of the original system: 
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list} & 2 & 2 &  3 & 10) \\
            & ({\sf list} & 2 & 5 & 12 & 31)  \\
            & ({\sf list} & 4 & 1 & -2 &  1) &) \\
\end{array}$$
This representation captures the essence of the system, namely the numeric
coefficients of the variables and the right hand sides. The names of the variables
don't play any role. 

The generative step in the triangulation phase is to subtract the first row
(list) of numbers from all the other rows. Subtracting one row from another
means subtracting the corresponding items in the two rows. With our running
example, this step would yield
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list} & 2 & 2 &  3 & 10) \\
            & ({\sf list} & 0 & 3 &  9 & 21)  \\
            & ({\sf list} & 4 & 1 & -2 &  1) &) \\
\end{array}$$
when we subtract the first row from the second. 

The goal of these subtractions is to put a 0 into the first column of all but the
first row. To achieve this for the last row, we subtract the first row twice from 
the second one:
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list} & 2 & 2 &  3 & 10) \\
            & ({\sf list} & 0 & 3 &  9 & 21)  \\
            & ({\sf list} & 0 &-3 & -8 &-19) &) \\
\end{array}$$
Put differently, we first multiply each item in the first row with {{\tt 2}\/} and
then subtract the result from the last row. It is easy to check that the solution
for the original system of equations and this new one are identical.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 27.5.1}

 \label{ex:gauss-equiv}
Check that the following system of equations 
$$\begin{array}{lclcrcrr}
2 \cdot x & + & 2 \cdot y & + &  3 \cdot z & = &  10 \\
          &   & 3 \cdot y & + &  9 \cdot z & = &  21 & \qquad (\ddagger)\\
          & - & 3 \cdot y & - &  8 \cdot z & = & -19
\end{array}$$
has the same solution as the one labeled with $(\dagger)$.
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/gauss-equiv.html}{\rule{3pt}{5pt}}


{\bf Exercise 27.5.2}

 \label{ex:gauss-subtract}
Develop {{\tt subtract}\/}.  The function consumes two lists of numbers of
equal length. It subtracts the first from the second, item by item, enough
times so that the result contains {{\tt 0}\/} in the first position. The
result is the {{\tt rest}\/} of this list.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/gauss-subtract.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Following mathematical convention, we drop the leading {{\tt 0}\/}'s from the
representation of the last two equations: 
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list} & 2 & 2 &  3 &   10) \\
            & ({\sf list} &   & 3 &  9 &   21)  \\
            & ({\sf list} &   & -3 &-8 &  -19) & ) \\
\end{array}$$
If, in addition, we use the same process for the remainder of the system to
generate shorter rows, the final representation has a triangular shape.

Let us study this idea with our running example. For the moment we ignore the
first row and focus on the rest of the equations: 
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list} & 3 &  9 &   21)  \\
            & ({\sf list} & -3 &-8 &  -19) & ) \\
\end{array}$$
By subtracting the first row now {-1} times from the second one, we get 
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list} & 3 &  9 &   21)  \\
            & ({\sf list} &   &  1 &    2) & ) \\
\end{array}$$
after dropping the leading {{\tt 0}\/}. The remainder of this system is a single
equation, which cannot be simplified any further. 

Here is the result of putting together this last system with the first
equation:\label{pg:star-equation}
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list} & 2 & 2 &  3 &   10) \\
            & ({\sf list} &   & 3 &  9 &   21) & \qquad (*)\\
            & ({\sf list} &   &   &  1 &    2) & ) \\
\end{array}$$
As promised, the shape of this system of equations is (roughly) a triangle, and 
as we can easily check, it has the same solution as the original system. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 27.5.3}

 \label{ex:gauss-equiv2}
Check that the following system of equations 
$$\begin{array}{lclcrcrr}
2 \cdot x & + & 2 \cdot y & + &  3 \cdot z & = &  10 \\
          &   & 3 \cdot y & + &  9 \cdot z & = &  21 & \qquad (*)\\
          &   &           &   &  1 \cdot z & = &   2
\end{array}$$
has the same solution as the one labeled with $(\dagger)$.
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/gauss-equiv2.html}{\rule{3pt}{5pt}}


{\bf Exercise 27.5.4}

 \label{ex:triangulate1}
Develop the algorithm {{\tt triangulate}\/}, which consumes a rectangular
representation of a system of equations and produces a triangular version
according the Gaussian algorithm. \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/triangulate1.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Unfortunately, the current version of the triangulation algorithm
occasionally fails to produce the solution. Consider the following
(representation of a) system of equations:  
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list} & 2 &  3 &  3 &   8) \\
            & ({\sf list} & 2 &  3 & -2 &   3)  \\
            & ({\sf list} & 4 & -2 &  2 &   4) & ) \\
\end{array}$$
Its solution is $x = 2$, $y = 1$, and $z = 1$. 

The first step is to subtract the first row from the second and to subtract
it twice from the last one, which yields the following matrix:
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list} & 2 &  3 &  3 &   8) \\
            & ({\sf list} &   &  0 & -5 &   -5)  \\
            & ({\sf list} &   & -8 & -4 &   -12) & ) \\
\end{array}$$
Next our algorithm would focus on the rest of the matrix: 
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list}  &  0 & -5 &  -5)  \\
            & ({\sf list}  & -8 & -4 & -12) 
	& ) \\
\end{array}$$
but the first item of this matrix is {{\tt 0}\/}. Since we cannot divide by
{{\tt 0}\/}, we are stuck.

To overcome this problem, we need to use another piece of knowledge from
our problem domain, namely, that we can switch equations around without
changing the solution. Of course, as we switch rows, we must make sure that
the first item of the row to be moved is not {{\tt 0}\/}. Here we can simply
swap the two rows:
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list}  & -8 & -4 & -12) \\
            & ({\sf list}  &  0 & -5 &  -5) 
	& ) \\
\end{array}$$
From here we may continue as before, subtracting the first equation from the
remaining ones a sufficient number of times. The final triangular matrix is: 
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list} & 2 &  3 &  3 &   8) \\
            & ({\sf list} &   & -8 & -4 &   -12) \\
            & ({\sf list} &   &    & -5 &   -5)  
	& ) \\
\end{array}$$
It is easy to check that this system of equations still has the solution 
$x = 2$, $y = 1$, and $z = 1$. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 27.5.5}

 \label{ex:triangulate2}
Revise the algorithm {{\tt triangulate}\/} from exercise~\ref{ex:triangulate1} so
that it switches rows when the first item of the matrix is {{\tt 0}\/}.

\noindent{\bf Hint:}  DrScheme provides the function {{\tt remove}\/}. It
consumes an item {{\tt I}\/} and a list {{\tt L}\/} and produces a list like
{{\tt L}\/} but with the first occurrence of {{\tt I}\/} removed. For
example,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (remove}\  {\tt (list}\  {\tt 0}\  {\tt 1)}\  {\tt (list}\  {\tt (list}\  {\tt 2}\  {\tt 1)}\  {\tt (list}\  {\tt 0}\  {\tt 1)))}\  \\
{\tt =}\  {\tt (list}\  {\tt (list}\  {\tt 2}\  {\tt 1))}
\end{tabbing}\end{minipage}\end{center}
\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/triangulate2.html}{\rule{3pt}{5pt}}


{\bf Exercise 27.5.6}

 \label{ex:triangulate3}
Some systems of equations don't have a solution. Consider the following system as
an example: 
$$\begin{array}{lclcrcrr}
2 \cdot x & + & 2 \cdot y & + &  2 \cdot z & = & 6 \\
2 \cdot x & + & 2 \cdot y & + &  4 \cdot z & = & 8 \\
2 \cdot x & + & 2 \cdot y & + &  z \cdot z & = & 2
\end{array}$$
Try to produce a triangular system by hand and with
{{\tt triangulate}\/}. What happens?  Modify the function so that it signals
an error if it encounters this situation.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/triangulate3.html}{\rule{3pt}{5pt}}


{\bf Exercise 27.5.7}

 \label{ex:gauss-solve}
After we obtain a triangular system of equations such as $(*)$ on
page~\pageref{pg:star-equation} (or exercise~\ref{ex:gauss-equiv2}), we can
solve the equations. In our specific example, the last equation says that
{\it z\/} is 2. Equipped with this knowledge, we can eliminate {\it z\/}
from the second equation through a substitution: 
$$ 3 \cdot y  +   9 \cdot 2  =   21 \ . $$

Determine the value for {\it y\/}. Then repeat the substitution step for {\it y\/}
and {\it z\/} in the first equation and find the value for {\it x\/}.

Develop the function {{\tt solve}\/}, which consumes triangular systems of
equations and produces a solution. A triangular system of equations has
the shape 
$$\begin{array}{llrrrrr}
({\sf list} & ({\sf list} & a_{11} & \ldots &  \ldots &  b_1) \\
            & ({\sf list} &        & a_{21} &  \ldots &  b_2)  \\
	    & ({\sf list} &        & \vdots &  \vdots &  \vdots)  \\
            & ({\sf list} &        &        &  a_{nn} &  b_n) & ) \\
\end{array}$$
where $a_{ij}$ and $b_i$ are numbers. That is, it is a list of lists and each of
the lists is one item shorter than the preceding one. A solution is a list of
numbers. The last number on the list is
$$\frac{b_n}{a_{nn}} \ . $$

\noindent{\bf Hint:}  Developing {{\tt solve}\/} requires a solution for the following
problem. Suppose we are give a row:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  {\tt 3}\  {\tt 9}\  {\tt 21)}\end{tabbing}\end{minipage}\end{center}
and a list of numbers that solve the remainder of the system:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  {\tt 2).}\end{tabbing}\end{minipage}\end{center}
In the world of equations, these two pieces of data represent the following
knowledge: 
$$ 3 \cdot x + 9 \cdot y = 21 $$
and 
$$ y = 2 \ ,$$
which in turn means we must solve the following equation: 
$$ 3 \cdot x + 9 \cdot 2 = 21 \ .$$

Develop the function {{\tt evaluate}\/}, which evaluates the rest of the
left-hand side of an equation and subtracts the right-hand side from this
sum.  Equivalently, {{\tt evaluate}\/} consumes {{\tt (list}\ {\tt 9}\ {\tt 21)}\/} and
{{\tt (list}\ {\tt 2)}\/} and produces {{\tt -3}\/}, that is, $9 \cdot 2 - 21$. Now
use {{\tt evaluate}\/} for the intermediate step in
{{\tt solve}\/}.~{\rule{3pt}{5pt}}\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/solve.html}




\section{Algorithms that Backtrack} \label{sec:backtrack}

Solving problems does not always proceed on a direct route to the
goal. Sometimes we make progress by pursuing one approach only to discover
that we are stuck because we took a wrong turn. In those cases, we
backtrack in our exploration and take a different turn at some branch, in
the hope that it leads us to a solution. Algorithms can proceed like
that. In the first subsection, we deal with an algorithm that can help us
traverse a graph, which is of course the situation we just discussed. The
second subsection is an extended exercise that uses backtracking in the
context of chess.

\subsection{Traversing Graphs} \label{sec:traverse-graph1}

On occasion, we need to navigate through a maze of one-way streets. Or, we
may wish to draw a graph of whom we consider a friend, whom they consider a
friend, and so on. Or, we need to plan a route through a network of
pipelines. Or, we ask the Internet to find some way to send a message from
one place to another.

All these situations share a common element: a {\sl directed graph\/}.
Specifically, there is always some collection of {\sl nodes\/} and a
collection of {\sl edges\/}. The edges represent one-way connections
between the nodes.  Consider figure~\ref{fig:graph}. The black bullets are
the nodes; the arrows between them are the one-way connections. The sample
graph consists of seven nodes and nine edges.

Now suppose we wish to plan routes in the graph of figure~\ref{fig:graph}. 
For example, if we plan to go from C to D, the route is simple: it consists
of the origination node C and the destination node D. In contrast, if we
wish to travel from E to D, we have two choices: 
\begin{enumerate}
 \item We either travel from E to F and then to D. 
 \item Or, we travel from E to C and then to D. 
\end{enumerate}
For some nodes, however, it is impossible to connect them. In particular,
it is impossible in our sample graph to move from C to G by following the
arrows. 


\begin{htmlonly}
\begin{rawhtml}
<center><img src=../icons/graph.gif align=center></center>
\end{rawhtml}
\end{htmlonly}

\hrule
\relax
\center{Figure: A directed graph} \label{fig:graph}
\hrule

In the real world, graphs have more than just seven nodes and many more
edges. Hence it is natural to develop functions that plan routes in
graphs. Following the general design recipe, we start with a data
analysis. Here is a compact representation of the graph in
figure~\ref{fig:graph} using lists:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt Graph}\  \\
\>{\tt \char'047}{\tt (}\={\tt (A}\  {\tt (B}\  {\tt E))}
\\
\>\>{\tt (B}\  {\tt (E}\  {\tt F))}
\\
\>\>{\tt (C}\  {\tt (D))}
\\
\>\>{\tt (D}\  {\tt ())}
\\
\>\>{\tt (E}\  {\tt (C}\  {\tt F))}
\\
\>\>{\tt (F}\  {\tt (D}\  {\tt G))}
\\
\>\>{\tt (G}\  {\tt ())))}
\end{tabbing}\end{minipage}\end{center}
The list contains one list per node. Each of these lists starts with the
name of a node followed by the list of its {\sl neighbors\/}. For example,
the second list represents node B with its two outgoing edges to E and F.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 28.1.1}

 \label{ex:represent-graph}
Translate the above definition into proper list form using {{\tt list}\/}
and proper symbols. 

The data definition for node is straightforward: 
\begin{quote} A {\sl {node}} is a symbol. \end{quote}

\noindent Formulate a data definition for graphs with arbitrarily many
nodes and edges. The data definition must specify a class of data that
contains {{\tt Graph}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/represent-graph.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Based on the data definitions for {{\tt node}\/} and {{\tt graph}\/}, we can
now produce the first draft of a contract for {{\tt find-route}\/}, the
function that searches a route in a graph:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt find-route}\ {\tt :}\ {\tt node}\ {\tt node}\ {\tt graph}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt node)}\/}}\\
\hbox{;; to create a path from {{\tt origination}\/} to {{\tt destination}\/} in {{\tt G}\/}}
\\
{\tt (define}\  {\tt (find-route}\  {\tt origination}\  {\tt destination}\  {\tt G)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
What this header leaves open is the exact shape of the result. It implies
that the result is a list of nodes, but it does not say exactly which nodes
the list contains. To understand this aspect, we must study some examples.

Consider the first problem mentioned above. Here is an expression that
formulates the problem in Scheme: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (find-route}\  {\tt \char'047}{\tt C}\  {\tt \char'047}{\tt D}\  {\tt Graph)}\end{tabbing}\end{minipage}\end{center}
A route from {{\tt \char'047}{\tt C}\/} to {{\tt \char'047}{\tt D}\/} consists of just two nodes: the
origination and the destination node. Hence, we should expect the answer
{{\tt (list}\ {\tt \char'047}{\tt C}\ {\tt \char'047}{\tt D)}\/}. Of course, one might argue that since both the
origination node and the destination node are known, the result should be
{{\tt empty}\/}. Here we choose the first alternative since it is more
natural, but it requires only a minor change of the final function
definition to produce the latter.

Now consider our second problem, going from {{\tt \char'047}{\tt E}\/} to {{\tt \char'047}{\tt D}\/},
which is more representative of the kinds of problems we might encounter.
One natural idea is to inspect all of the neighbors of {{\tt \char'047}{\tt E}\/} and to
find a route from one of them to {{\tt \char'047}{\tt D}\/}. In our sample graph,
{{\tt \char'047}{\tt E}\/} has two neighbors: {{\tt \char'047}{\tt C}\/} and {{\tt \char'047}{\tt F}\/}.  Suppose for a
moment that we didn't know the route yet. In that case, we could again
inspect all of the neighbors of {{\tt \char'047}{\tt C}\/} and find a route from those to
our goal. Of course, {{\tt \char'047}{\tt C}\/} has a single neighbor and it is
{{\tt \char'047}{\tt D}\/}.  Putting together the results of all stages shows that the
final result is {{\tt (list}\ {\tt \char'047}{\tt E}\ {\tt \char'047}{\tt C}\ {\tt \char'047}{\tt D)}\/}.

Our final example poses a new problem. Suppose {{\tt find-route}\/} is
given the arguments {{\tt \char'047}{\tt C}\/}, {{\tt \char'047}{\tt G}\/}, and {{\tt Graph}\/}. In this
case, we know from inspecting figure~\ref{fig:graph} that there is no
connecting route. To signal the lack of a route, {{\tt find-route}\/}
should produce a value that cannot be mistaken for a route. One good
choice is {{\sf false}\/}, a value that isn't a list and naturally denotes the
failure of a function to compute a proper result. 

This new agreement requires another change in our contract: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt find-route}\ {\tt :}\ {\tt node}\ {\tt node}\ {\tt graph}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt node)}\ {\tt or}\ {\sf false}\/}}\\
\hbox{;; to create a path from {{\tt origination}\/} to {{\tt destination}\/} in {{\tt G}\/}}
\\
\hbox{;; if there is no path, the function produces {{\sf false}\/}}
\\
{\tt (define}\  {\tt (find-route}\  {\tt origination}\  {\tt destination}\  {\tt G)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

Our next step is to understand the four essential pieces of the function:
the ``trivial problem'' condition, a matching solution, the generation of a
new problem, and the combination step. The discussion of the three examples
suggest answers. First, if the {{\tt origination}\/} argument of
{{\tt find-route}\/} is equal to its {{\tt destination}\/}, the problem is
trivial; the matching answer is {{\tt (list}\ {\tt destination)}\/}. Second, if the
arguments are different, we must inspect all neighbors of
{{\tt origination}\/} in {{\tt graph}\/} and determine whether there is a
route from one of those to {{\tt destination}\/}. 

Since a node can have an arbitrary number of neighbors, this task is too
complex for a single primitive. We need an auxiliary function.  The task of
the auxiliary function is to consume a list of nodes and to determine for
each one of them whether there is a route to the destination node in the
given graph.  Put differently, the function is a list-oriented version of
{{\tt find-route}\/}. Let us call this function {{\tt find-route/list}\/}.
Here is a translation of this informal description into a
contract, header, and purpose statement:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt find-route/list}\ {\tt :}\ {\tt (listof}\ {\tt node)}\ {\tt node}\ {\tt graph}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt node)}\ {\tt or}\ {\sf false}\/}}\\
\hbox{;; to create a path from some node on {{\tt lo-originations}\/} to {{\tt destination}\/}}
\\
\hbox{;; if there is no path, the function produces {{\sf false}\/}}
\\
{\tt (define}\  {\tt (find-route/list}\  {\tt lo-originations}\  {\tt destination}\  {\tt G)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

Now we can write a first draft of {{\tt find-route}\/} as follows:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (find-route}\  {\tt origination}\  {\tt destination}\  {\tt G)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt origination}\  {\tt destination)}\  {\tt (list}\  {\tt destination)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (find-route/list}\  {\tt (neighbors}\  {\tt origination}\  {\tt G)}\  {\tt destination}\  {\tt G)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The function {{\tt neighbors}\/} generates a whole list of problems: the
problems of finding routes from the neighbors of {{\tt origination}\/} to
{{\tt destination}\/}. Its definition is a straightforward exercise in
structural processing.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}
{\bf Exercise 28.1.2}

 \label{ex:find-route1}
Develop the function {{\tt neighbors}\/}. It consumes a node {{\tt n}\/} and a
graph {{\tt g}\/} (see exercise~\ref{ex:represent-graph}) and produces the
list of neighbors of {{\tt n}\/} in {{\tt g}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-route1.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}
 

Next we need to consider what {{\tt find-route/list}\/} produces. If it
finds a route from any of the neighbors, it produces a route from that
neighbor to the final destination. But, if none of the neighbors is
connected to the destination, the function produces {{\sf false}\/}. Clearly,
{{\tt find-route}\/}'s answer depends on what {{\tt find-route/list}\/}
produces. Hence, we should distinguish the answers with a
{{\bf cond}-exp\-res\-sion}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (find-route}\  {\tt origination}\  {\tt destination}\  {\tt G)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt origination}\  {\tt destination)}\  {\tt (list}\  {\tt destination)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt possible-route}\  
\\
\>\>\>\>{\tt (find-route/list}\  \={\tt (neighbors}\  {\tt origination}\  {\tt G)}\  
\\
\>\>\>\>\>{\tt destination}\  {\tt G)))}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (boolean?}\  {\tt route)}\  {\tt ...\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}\  \hbox{; {{\tt (cons?}\ {\tt route)}\/} }
\\
\>\>\>\>\>{\tt ...\char'135}{\tt ))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The two cases reflect the two kinds of answers we might receive: a boolean
or a list. If {{\tt find-route/list}\/} produces {{\sf false}\/}, it failed to
find a route from {{\tt origination}\/}'s neighbors, and it is therefore
impossible to reach {{\tt destination}\/} at all. The answer in this case
must therefore be {{\sf false}\/}. In contrast, if {{\tt find-route/list}\/}
produces a list, the answer must be route from {{\tt origination}\/} to
{{\tt destination}\/}. Since {{\tt possible-route}\/} starts with one of
{{\tt origination}\/}'s neighbors, it suffices to add {{\tt origination}\/}
to the front of {{\tt possible-route}\/}.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt find-route}\ {\tt :}\ {\tt node}\ {\tt node}\ {\tt graph}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt node)}\ {\tt or}\ {\sf false}\/}}\\
\hbox{;; to create a path from {{\tt origination}\/} to {{\tt destination}\/} in {{\tt G}\/}}
\\
\hbox{;; if there is no path, the function produces {{\sf false}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (find-route}\  {\tt origination}\  {\tt destination}\  {\tt G)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt origination}\  {\tt destination)}\  {\tt (list}\  {\tt destination)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt possible-route}\  
\\
\>\>\>\>{\tt (find-route/list}\  {\tt (neighbors}\  {\tt origination}\  {\tt G)}\  {\tt destination}\  {\tt G)))}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (boolean?}\  {\tt possible-route)}\  {\sf false}{\tt \char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt origination}\  {\tt possible-route)\char'135}{\tt ))\char'135}{\tt ))}
\\
\hbox{;; {{\tt find-route/list}\ {\tt :}\ {\tt (listof}\ {\tt node)}\ {\tt node}\ {\tt graph}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt node)}\ {\tt or}\ {\sf false}\/}}
\\
\hbox{;; to create a path from some node on {{\tt lo-originations}\/} to {{\tt destination}\/}}
\\
\hbox{;; if there is no path, the function produces {{\sf false}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (find-route/list}\  {\tt lo-Os}\  {\tt D}\  {\tt G)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt lo-Os)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt possible-route}\  {\tt (find-route}\  {\tt (first}\  {\tt lo-Os)}\  {\tt D}\  {\tt G)))}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (boolean?}\  {\tt possible-route)}\  {\tt (find-route/list}\  {\tt (rest}\  {\tt lo-Os)}\  {\tt D}\  {\tt G)\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt possible-route\char'135}{\tt ))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Finding a route in a graph}\label{fig:find-route-code}
\hrule

Figure~\ref{fig:find-route-code} contains the complete definition of
{{\tt find-route}\/}. It also contains a definition of
{{\tt find-route/list}\/}, which processes its first argument via structural
recursion. For each node in the list, {{\tt find-route/list}\/} uses
{{\tt find-route}\/} to check for a route. If {{\tt find-route}\/} indeed
produces a route, that route is the answer. Otherwise, if
{{\tt find-route}\/} fails and produces {{\sf false}\/}, the function recurs. In
other words, it backtracks its current choice of a starting position,
{{\tt (first}\ {\tt lo-Os)}\/}, and instead tries the next one in the list. For
that reason, {{\tt find-route}\/} is often called a {\sc {backtracking algorithm.}}

\medskip\noindent{\bf Backtracking in the Structural World}:\  Intermezzo~3 briefly discusses
backtracking in the structural world. A particularly good example is
exercise \ref{ex:local-interm3}, which concerns a backtracking function for
family trees. The function first searches one branch of a family tree for a
blue-eyed ancestor and, if this search produces {{\tt false}\/}, it searches
the other half of the tree. Since graphs generalize trees, comparing the
two functions is an instructive exercise.~{\rule{3pt}{5pt}}

Last, but not least, we need to understand whether the function produces an
answer in all situations.  The second one, {{\tt find-route/list}\/}, is
structurally recursive and therefore always produces some value, assuming
{{\tt find-route}\/} always does. For {{\tt find-route}\/} the answer is far
from obvious. For example, when given the graph in figure~\ref{fig:graph}
and two nodes in the graph, {{\tt find-route}\/} always produces some
answer. For other graphs, however, it does not always terminate.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 28.1.3}

 \label{ex:find-route2}
Test {{\tt find-route}\/}. Use it to find a route from A to G in the graph
of figure~\ref{fig:graph}. Ensure that it produces {{\sf false}\/} when asked 
to find a route from C to G~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-route2.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 28.1.4}

 \label{ex:find-route3}
Develop the function {{\tt test-on-all-nodes}\/}, which consumes a graph
{{\tt g}\/} and tests {{\tt find-route}\/} on all pairs of nodes in
{{\tt g}\/}. Test the function on {{\tt Graph}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-route3.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}
 



\begin{htmlonly}
\begin{rawhtml}
<center><img src=../icons/graph-cycle.gif align=center></center>
\end{rawhtml}
\end{htmlonly}

\hrule
\relax

\center{Figure: A directed graph with cycle} \label{fig:cyclic-graph}
\hrule

Consider the graph in figure~\ref{fig:cyclic-graph}.  It radically differs
from the graph in figure~\ref{fig:graph} in that it is possible to start a
route in a node and to return to the same node. Specifically, it is
possible to move from B to E to C and back to B. And indeed, if applied
{{\tt find-route}\/} to {{\tt \char'047}{\tt B}\/}, {{\tt \char'047}{\tt D}\/}, and a representation of
the graph, it fails to stop. Here is the hand-evaluation:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (find-route}\  {\tt \char'047}{\tt B}\  {\tt \char'047}{\tt D}\  {\tt Cyclic-graph)}\\
{\tt =}\  {\tt ...}\  {\tt (find-route}\  {\tt \char'047}{\tt B}\  {\tt \char'047}{\tt D}\  {\tt Cyclic-graph)}\  {\tt ...}
\\
{\tt =}\  {\tt ...}\  {\tt (find-route}\  {\tt (list}\  {\tt \char'047}{\tt E}\  {\tt \char'047}{\tt F)}\  {\tt \char'047}{\tt D}\  {\tt Cyclic-graph)}\  {\tt ...}
\\
{\tt =}\  {\tt ...}\  {\tt (find-route}\  {\tt \char'047}{\tt E}\  {\tt \char'047}{\tt D}\  {\tt Cyclic-graph)}\  {\tt ...}
\\
{\tt =}\  {\tt ...}\  {\tt (find-route}\  {\tt (list}\  {\tt \char'047}{\tt C}\  {\tt \char'047}{\tt F)}\  {\tt \char'047}{\tt D}\  {\tt Cyclic-graph)}\  {\tt ...}
\\
{\tt =}\  {\tt ...}\  {\tt (find-route}\  {\tt \char'047}{\tt C}\  {\tt \char'047}{\tt D}\  {\tt Cyclic-graph)}\  {\tt ...}
\\
{\tt =}\  {\tt ...}\  {\tt (find-route}\  {\tt (list}\  {\tt \char'047}{\tt B}\  {\tt \char'047}{\tt D)}\  {\tt \char'047}{\tt D}\  {\tt Cyclic-graph)}\  {\tt ...}
\\
{\tt =}\  {\tt ...}\  {\tt (find-route}\  {\tt \char'047}{\tt B}\  {\tt \char'047}{\tt D}\  {\tt Cyclic-graph)}\  {\tt ...}
\\
{\tt =}\  {\tt ...}\  
\end{tabbing}\end{minipage}\end{center}
where {{\tt Cyclic-Graph}\/} stands for a Scheme representation of the graph
in figure~\ref{fig:cyclic-graph}. The hand-evaluation shows that after
seven applications of {{\tt find-route}\/} and {{\tt find-route/list}\/} the
computer must evaluate the exact same expression from which we
started. Since the same input produces the same output and the same
behavior for functions, we know that the function loops forever and does not
produce a value.

In summary, if some given graph is cycle-free, {{\tt find-route}\/} produces
some output for any given inputs. After all, every route can only contain a
finite number of nodes, and the number of routes is finite, too. The
function therefore either exhaustively inspects all solutions starting from
some given node or finds a route from the origination to the destination
node. If, however, a graph contains a cycle, that is, a route from some
node back to itself, {{\tt find-route}\/} may not produce a result for some
inputs.  In the next part, we will study a programming technique that helps
us finds routes even in the presence of cycles in a graph.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 

{\bf Exercise 28.1.5}

 \label{ex:find-route4}
Test {{\tt find-route}\/} on {{\tt \char'047}{\tt B}\/}, {{\tt \char'047}{\tt C}\/}, and the graph in
figure~\ref{fig:cyclic-graph}. Use the strategy of
section~\ref{sec:equal-test} to formulate the tests as boolean-valued
expression.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-route4.html}{\rule{3pt}{5pt}}
 

{\bf Exercise 28.1.6}

 \label{ex:find-route5}
Reorganize the {{\tt find-route}\/} program into a single function
definition. Then remove parameters from the locally defined functions
following the hints in intermezzo~C on the pragmatics of
{{\tt local}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-route5.html}{\rule{3pt}{5pt}}
 

\subsection{Extended Exercise: Checking (on) Queens} \label{sec:queens}

A famous problem in the game of chess concerns the placements of queens on a
board.  For our purposes, a chess board is a ``square'' of, say,
eight-by-eight or three-by-three tiles. The queen is a game piece that can
move in a horizontal, vertical, or diagonal direction arbitrarily far. We say
that a queen {\sl threatens \/} a tile if it is on the tile or can move to
it. Figure~\ref{fig:queens} shows an example.  The solid disk represents a
queen in the second column and sixth row. The solid lines radiating from the
disk go through all those tiles that are threatened by the queen.

\hrule
\begin{center}
\begin{picture}(200,200)
\multiput(0,0)(25,0){9}{\line(0,1){200}}
\multiput(0,0)(0,25){9}{\line(1,0){200}}
\put(37,62){\circle*{10}}
\multiput(00,61)(0,.5){6}{\line(1,0){200}}
\multiput(00,24)(.5,.5){6}{\line(1,1){173}}
\multiput(99.8,00)(.1,.1){6}{\line(-1,1){100}}
\multiput(36,00)(.5,0){6}{\line(0,1){200}}
\end{picture}
\end{center}
\center{Figure: A chess board with a single queen} \label{fig:queens}
\hrule

The queen-placement problem is to place eight queens on a chess board of
eight-by-eight tiles such that the queens on the board don't threaten each
other.  In computing, we generalize the problem of course and ask whether we
can place {{\tt n}\/} queens on some board of arbitrary size {{\tt m}\/} by {{\tt m}\/}.

Even a cursory glance at the problem suggests that we need a data
representation of boards and some basic functions on boards before we can
even think of designing a program that solves the problem. Let's start with
some basic data and function definitions. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 28.2.1}

 \label{ex:queen0}
Develop a data definition for chess boards. 

\noindent{\bf Hint:}  Use lists. Represent tiles with {{\sf true}\/} and {{\tt false}\/}. A value
of {{\tt true}\/} should indicate that a position is available for the
placement of a queen; {{\sf false}\/} should indicate that a position is occupied
by, or threatened by, a queen.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/queen0.html}{\rule{3pt}{5pt}}


Next we need a function for creating a board and another one for checking on
a specific tile. Following the examples of lists, let's define
{{\tt build-board}\/} and {{\tt board-ref}\/}. 

{\bf Exercise 28.2.2}

 \label{ex:queen1}
Develop the following two functions on chess boards:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt build-board}\ {\tt :}\ {\bf N}\ {\tt (}{\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\tt boolean)}\ {}{\tt ->}{}\ {\tt board}\/}}\\
\hbox{;; to create a board of size {{\tt n}\/} x {{\tt n}\/}, }
\\
\hbox{;; fill each position with indices {{\tt i}\/} and {{\tt j}\/} with {{\tt (f}\ {\tt i}\ {\tt j)}\/}}
\\
{\tt (define}\  {\tt (build-board}\  {\tt n}\  {\tt f)}\  {\tt ...)}
\\
\hbox{;; {{\tt board-ref}\ {\tt :}\ {\tt board}\ {\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\tt boolean}\/}}
\\
\hbox{;; to access a position with indices {{\tt i}\/}, {{\tt j}\/} on a-board}
\\
{\tt (define}\  {\tt (board-ref}\  {\tt a-board}\  {\tt i}\  {\tt j)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Test them rigorously! Use the strategy of section~\ref{sec:equal-test} to
formulate the tests as boolean-valued expressions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/queen1.html}{\rule{3pt}{5pt}}


In addition to these generic functions on a chess board representation, we
also need at least one function that captures the concept of a ``threat'' as
mentioned in the problem statement. 

{\bf Exercise 28.2.3}

 \label{ex:queen2}
Develop the function {{\tt threatened\char'077}\/}, which computes whether a queen
can reach a position on the board from some given position. That is, the
function consumes two positions, given as {{\tt posn}\/} structures, and
produces {{\tt true}\/} if a queen on the first position can threaten the
second position. 

\noindent{\bf Hint:} \ The exercise translate the chess problem of ``threatening queens''
into the mathematical problem of determining whether in some given grid,
two positions are on the same vertical, horizontal, or diagonal line. Keep
in mind that each position belongs to two diagonals and that the slope of a
diagonal is either {{\tt +1}\/} or {{\tt -1}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/queen2.html}{\rule{3pt}{5pt}}


Once we have data definitions and functions for the ``language of chess
boards,'' we can turn our attention to the main task: the algorithm for
placing a number of queens on some given board. 

{\bf Exercise 28.2.4}

 \label{ex:queen}
Develop {{\tt placement}\/}.  The function consumes a natural number and a
board and tries to place that many queens on the board. If the queens can
be placed, the function produces an appropriate board. If not, it produces
{{\tt false}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/queen.html}{\rule{3pt}{5pt}}




%% BEGIN INLINED FILE: curriculumD.dl.tex


 \part{{Intermezzo 5: The Cost of Computing and Vectors}}  \section{{Intermezzo 5: The Cost of Computing and Vectors}}   \label{sec:int-cost}

In section~\ref{sec:choice} we discussed the differences between a
structurally recursive program and an equivalent, generative version. The
comparison revealed that the generative one is much faster than the
structural version. We used both informal arguments, using the number of
recursive calls, and measurements, using {{\tt time}\/} expressions
(exercises~\ref{ex:gcd-struct-time} and~\ref{ex:gcd-gen-time}), to support
our conclusion.

While timing the application of a program to specific arguments can help us
understand a program's behavior in one situation, it is not a fully
convincing argument. After all, applying the same program to some other
inputs may require a radically different amount of time. In short, timing
programs for specific inputs has the same status as testing programs for
specific examples. Just as testing may reveal bugs, timing may reveal
anomalies concerning the execution behavior for specific inputs. It does
not provide a firm foundation for general statements about the behavior of
a program.

This intermezzo introduces a tool for making general statements about the
time that programs take to compute a result. The first subsection motivates
the tool and illustrates it with several examples, though on an informal
basis. The second one provides a rigorous definition. The last one uses the
tool to motivate an additional class of Scheme data and some of its basic
operations.

 \subsection{Concrete Time, Abstract Time}

Let's study the behavior of {{\tt how-many}\/}, a function that we understand
really well:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (how-many}\  {\tt a-list)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list)}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (how-many}\  {\tt (rest}\  {\tt a-list))}\  {\tt 1)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
It consumes a list and computes how many items the list contains. 

Here is a sample evaluation:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (how-many}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c))}\end{tabbing}\end{minipage}\end{center}\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (+}\  {\tt (how-many}\  {\tt (list}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c))}\  {\tt 1)}\end{tabbing}\end{minipage}\end{center}\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (+}\  {\tt (+}\  {\tt (how-many}\  {\tt (list}\  {\tt \char'047}{\tt c))}\  {\tt 1)}\  {\tt 1)}\end{tabbing}\end{minipage}\end{center}\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (+}\  {\tt (+}\  {\tt (+}\  {\tt (how-many}\  {\tt empty)}\  {\tt 1)}\  {\tt 1)}\  {\tt 1)}\end{tabbing}\end{minipage}\end{center}\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt 3}\end{tabbing}\end{minipage}\end{center}
It consists of only those steps that are natural recursions. The steps in
between are always the same. For example, to get from the original
application to the first natural recursion, we go through the following
steps:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (how-many}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c))}\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (empty?}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c))}\  {\tt 0\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (how-many}\  {\tt (rest}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)))}\  {\tt 1)\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt false}\  {\tt 0\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (how-many}\  {\tt (rest}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)))}\  {\tt 1)\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (how-many}\  {\tt (rest}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)))}\  {\tt 1)\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (+}\  {\tt (how-many}\  {\tt (rest}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c)))}\  {\tt 1)}\end{tabbing}\end{minipage}\end{center}
The steps between the remaing natural recursions differ only as far as the
substitution for {{\tt a-list}\/} is concerned. 

If we apply {{\tt how-many}\/} to a shorter list, we need fewer natural
recursion steps: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (how-many}\  {\tt (list}\  {\tt \char'047}{\tt e))}\\
{\tt =}\  {\tt (+}\  {\tt (how-many}\  {\tt empty)}\  {\tt 1)}
\\
{\tt =}\  {\tt 1}
\end{tabbing}\end{minipage}\end{center}
If we apply {{\tt how-many}\/} to a longer list, we need more natural
recursion steps. The number of steps between natural recursions remains the
same. 

The example suggests that, not surprisingly, the number of evaluation steps
depends on the size of the input. More importantly, though, it also implies
that the number of natural recrusions is a good measure of the size of an
evaluation sequence. After all, we can reconstruct the actual number of
steps from this measure and the function definition. For this reason,
programmers have come to express the {\sc {abstract running time}} of a
program as a relationship between the size of the input and the number of
natural recursion steps in an evaluation.\footnote{We speak of an abstract running time because the measure ignores the details of how much time primitive steps take and how much time the overall evaluation takes.}

In our first example, the size of the input is simply the size of the
list. More specifically, if the list contains one item, the evaluation
requires one natural recursion. For two items, we recur twice. For a list
with {\it N\/} items, the evaluation requires {\it N\/} steps. 

Not all functions have such a uniform measure for their abstract running time. 
Take a look at our first recursive function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (contains-doll\char'077}\  {\tt a-list-of-symbols)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-list-of-symbols)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt (first}\  {\tt a-list-of-symbols)}\  {\tt \char'047}{\tt doll)}\  {\sf true}{\tt \char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (contains-doll\char'077}\  {\tt (rest}\  {\tt a-list-of-symbols))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
If we evaluate 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (contains-doll\char'077}\  {\tt (list}\  {\tt \char'047}{\tt doll}\  {\tt \char'047}{\tt robot}\  {\tt \char'047}{\tt ball}\  {\tt \char'047}{\tt game-boy}\  {\tt \char'047}{\tt pokemon))}\end{tabbing}\end{minipage}\end{center}
the application requires no natural recursion step. In contrast, for the
expression
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (contains-doll\char'077}\  {\tt (list}\  {\tt \char'047}{\tt robot}\  {\tt \char'047}{\tt ball}\  {\tt \char'047}{\tt game-boy}\  {\tt \char'047}{\tt pokemon}\  {\tt \char'047}{\tt doll))}\end{tabbing}\end{minipage}\end{center}
the evaluation requires as many natural recursion steps as there are items
in the list.  Put differently, in the best case, the function can find the
answer immediately; in the worst case, the function must search the entire
input list.

Programmers cannot assume that inputs are always of the best posisble
shape; and they must hope that the inputs are not of the worst possible
shape. Instead, they must analyze how much time their functions take {on the average}.  For exanple, {{\tt contains-doll\char'077}\/} may---on the
average---find {{\tt \char'047}{\tt doll}\/} somewhere in the middle of the list. Thus, we
could say that if the input contains {\it N\/} items, the abstract running time
of {{\tt contains-doll\char'077}\/} is (roughly)
$$ \frac{N}{2} $$
that is, it naturally recurs half as often as the number of items on the
input. Because we already measure the running time of a function in an
abstract manner, we can ignore the division by 2. More precisely, we assume
that each basic step takes $K$ units of time. If, instead, we use $K/2$ as
the constant, we can calculate
$$ K \cdot \frac{N}{2} = \frac{K}{2} \cdot N $$
which shows that we can ignore other constant factors.  To indicate that we
are hiding such constants we say that {{\tt contains-doll\char'077}\/} takes ``on
the order of {\it N\/} steps'' to find {{\tt \char'047}{\tt doll}\/} in a list of {\it N\/} items.

Now consider our standard sorting function from figure~\ref{fig:sort}. 
Here is a hand-evaluation for a small input:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (sort}\  {\tt (list}\  {\tt 3}\  {\tt 1}\  {\tt 2))}\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (insert}\  {\tt 3}\  {\tt (sort}\  {\tt (list}\  {\tt 1}\  {\tt 2)))}\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (insert}\  {\tt 3}\  {\tt (insert}\  {\tt 1}\  {\tt (sort}\  {\tt (list}\  {\tt 2))))}\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (insert}\  {\tt 3}\  {\tt (insert}\  {\tt 1}\  {\tt (insert}\  {\tt 2}\  {\tt (sort}\  {\tt empty))))}\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (insert}\  {\tt 3}\  {\tt (insert}\  {\tt 1}\  {\tt (insert}\  {\tt 2}\  {\tt empty)))}\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (insert}\  {\tt 3}\  {\tt (insert}\  {\tt 1}\  {\tt (list}\  {\tt 2)))}\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (insert}\  {\tt 3}\  {\tt (cons}\  {\tt 2}\  {\tt (insert}\  {\tt 1}\  {\tt empty)))}\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (insert}\  {\tt 3}\  {\tt (list}\  {\tt 2}\  {\tt 1))}\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (insert}\  {\tt 3}\  {\tt (list}\  {\tt 2}\  {\tt 1))}\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (list}\  {\tt 3}\  {\tt 2}\  {\tt 1)}\end{tabbing}\end{minipage}\end{center}
The evaluation is more complicated than those for {{\tt how-many}\/} or
{{\tt contains-doll\char'077}\/}. It also consists of two phases. During the first
one, the natural recursions for {{\tt sort}\/} set up as many applications
of {{\tt insert}\/} as there are items in the list. During the second phase,
each application of {{\tt insert}\/} traverses a list of 1, 2, 3, \ldots\ up
to the number of items in the original list (minus one). 

Inserting an item is similar to finding an item, so it is not surprising
that {{\tt insert}\/} behaves like {{\tt contains-doll\char'077}\/}. More
specifically, the applications of {{\tt insert}\/} to a list of {\it N\/}
items may trigger {\it N\/} natural recursions or none. On the average, we
assume it requires $N/2$, which means on the order of {\it N\/}. Because
there are {\it N\/} applications of {{\tt insert}\/}, we have an average of
on the order of $N^2$ natural recursions of {{\tt insert}\/}.

In summary, if {{\tt l}\/} contains {\it N\/} items, evaluating
{{\tt (sort}\ {\tt l)}\/} always requires {\it N} natural recursions of
{{\tt sort}\/} and on the order of $N^2$ natural recursions of
{{\tt insert}\/}. Taken together, we get 
$$ N^2 + N $$
steps, but we will see in exercise~\ref{ex:big-o1} that this is equivalent
to saying that insertion sort requires on the order of $N^2$ steps.

Our final example is the function {{\tt max}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt max}\ {\tt :}\ {\tt ne-list-of-numbers}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to determine the maximum of a non-empty list of numbers }
\\
{\tt (d}\={\tt efine}\  {\tt (max}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt (rest}\  {\tt alon))}\  {\tt (first}\  {\tt alon)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (>}\  {\tt (max}\  {\tt (rest}\  {\tt alon))}\  {\tt (first}\  {\tt alon))}\  {\tt (max}\  {\tt (rest}\  {\tt alon))\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (first}\  {\tt alon)\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
In exercise~\ref{ex:local-interm2}, we investigated its behavior and the
behavior of an observationally equivalent function that uses
{{\tt local}\/}. Here we study its abstract running time rather than just
observe some concrete running time. 

Let's start with a small example: {{\tt (max}\ {\tt (list}\ {\tt 0}\ {\tt 1}\ {\tt 2}\ {\tt 3))}\/}.  We know
that the result is {{\tt 3}\/}. Here is the first important step of a
hand-evaluation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (max}\  {\tt (list}\  {\tt 0}\  {\tt 1}\  {\tt 2}\  {\tt 3))}\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (>}\  \hbox{$\underline{\mbox{{{\tt (max}\ {\tt (list}\ {\tt 1}\ {\tt 2}\ {\tt 3))}\/}}}$}\  {\tt 0)}\  \hbox{$\underline{\mbox{{{\tt (max}\ {\tt (list}\ {\tt 1}\ {\tt 2}\ {\tt 3))}\/}}}$}{\tt \char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt 0\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
From here, we must evaluate the left of the two underlined natural
recursions. Because the result is {{\tt 3}\/} and the condition is thus
{{\tt true}\/}, we must evaluate the second underlined natural recursion as
well. 

Focusing on just the natural recursion we see that its hand-evaluation
begins with similar steps: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (max}\  {\tt (list}\  {\tt 1}\  {\tt 2}\  {\tt 3))}\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (>}\  {\tt (max}\  {\tt (list}\  {\tt 2}\  {\tt 3))}\  {\tt 1)}\  {\tt (max}\  {\tt (list}\  {\tt 2}\  {\tt 3))\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt 1\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
Again, {{\tt (max}\ {\tt (list}\ {\tt 2}\ {\tt 3))}\/} is evaluated twice because it produces the
maximum. Finally, even determining the maximum of {{\tt (max}\ {\tt (list}\ {\tt 2}\ {\tt 3))}\/}
requires two natural recursions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (max}\  {\tt (list}\  {\tt 2}\  {\tt 3))}\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (>}\  {\tt (max}\  {\tt (list}\  {\tt 3))}\  {\tt 2)}\  {\tt (max}\  {\tt (list}\  {\tt 3))\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt 2\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}

To summarize, {{\tt max}\/} requires two natural recursion for each natural
recursion. The following table counts the instances for our example:
\begin{center}
\begin{tabular}{l|l}
\mbox{original expression}    & \mbox{requires 2 evaluations of} \\
 \hline
{{\tt (max}\ {\tt (list}\ {\tt 0}\ {\tt 1}\ {\tt 2}\ {\tt 3))}\/} & {{\tt (max}\ {\tt (list}\ {\tt 1}\ {\tt 2}\ {\tt 3))}\/}  \\
{{\tt (max}\ {\tt (list}\ {\tt 1}\ {\tt 2}\ {\tt 3))}\/}   & {{\tt (max}\ {\tt (list}\ {\tt 2}\ {\tt 3))}\/}  \\
{{\tt (max}\ {\tt (list}\ {\tt 2}\ {\tt 3))}\/}     & {{\tt (max}\ {\tt (list}\ {\tt 3))}\/}  
\end{tabular}
\end{center}
Altogether the hand-evaluation requires eight natural recursions for a list
of four items. If we add {{\tt 4}\/} (or a larger number) at the end of the
list, we need to double the number of natural recursions. Thus, in general
we need on the order of $$2^N$$ recursions for a list of {\it N\/} numbers
when the last number is the maximum.\footnote{More precisely, the evaluation consists of $2^{N-1}$ steps, but $$2^{N-1} = \frac{1}{2} \cdot 2^N \, , $$ which shows that we ignore a (small) constant when we say on the order of $2^N$.}

While the scenario we considered is the worst possible case, the analysis
of {{\tt max}\/}'s abstract running time explains the phenomenon we studied
in exercise~\ref{ex:local-interm2}. It also explains why a version of
{{\tt max}\/} that uses a {{\bf local}-exp\-res\-sion} to name the result of
the natural recursion is faster: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt max2}\ {\tt :}\ {\tt ne-list-of-numbers}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to determine the maximum of a list of numbers }
\\
{\tt (d}\={\tt efine}\  {\tt (max2}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt (rest}\  {\tt alon))}\  {\tt (first}\  {\tt alon)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt max-of-rest}\  {\tt (max2}\  {\tt (rest}\  {\tt alon))))}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (>}\  {\tt max-of-rest}\  {\tt (first}\  {\tt alon))}\  {\tt max-of-rest\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (first}\  {\tt alon)\char'135}{\tt )\char'135}{\tt )))}
\end{tabbing}\end{minipage}\end{center}
Instead of recomputing the maximum of the rest of the list, this version
just refers to the variable twice when the variable stands for the
maximum of the rest of the list. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 29.1.1}

 \label{ex:order-of-2}
A number tree is either a number or a pair of number trees. Develop the
function {{\tt sum-tree}\/}, which determines the sum of the numbers in a
tree. How should we measure the size of a tree? What is its abstract
running time?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/order-of-2.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.1.2}

 \label{ex:order-of-1}
Hand-evaluate {{\tt (max2}\ {\tt (list}\ {\tt 0}\ {\tt 1}\ {\tt 2}\ {\tt 3))}\/} in a manner similar to our
evaluation of {{\tt (max}\ {\tt (list}\ {\tt 0}\ {\tt 1}\ {\tt 2}\ {\tt 3))}\/}.  What is the abstract running
time of {{\tt max2}\/}?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/order-of-1.html}{\rule{3pt}{5pt}}


 \subsection{The Definition of ``on the Order of''}

It is time to introduce a rigorous description of the phrase ``on the order
of'' and to explain why it is acceptable to ignore some constants. Any
serious programmer must be thoroughly familiar with this notion. It is the
most fundamental method for analyzing and comparing the behavior of
programs. This intermezzo provides a first glimpse at the idea; a second
course on computing usually provides some more in-depth considerations. 

\hrule
\begin{center}
\setlength{\unitlength}{.5pt}
\begin{picture}(300,250)(0,0)
\put(-15,-15){\mbox{0}}
\put(0,0){\vector(0,1){200}} 
\put(-25,180){\mbox{{\it y\/}}}
\put(0,0){\vector(1,0){300}} 
\put(290,-30){\mbox{{\it x\/}}}
\put(0,0){\line(5,3){300}}   \qbezier[500](0,0)(200,0)(300,250) 
\put(250,-10){\line(0,1){250}}
\put(232,-30){\mbox{\small 1000}}
\end{picture}
\end{center}

\center{Figure: A comparions of two running time expressions} \label{fig:big-oh}
\hrule

Let's consider a sample ``order of'' claim with concrete examples before we
agree on a definition. Recall that a function {{\tt F}\/} may require on the
order of {\it N\/} steps and a function {{\tt G}\/} $N^2$ steps, even though
both compute the same results for the same inputs. Now suppose the basic
time constants are {{\tt 1000}\/} for {{\tt F}\/} and 1 for {{\tt G}\/}. One
way to compare the two claims is to tabulate the abstract running time: 
\begin{center}
\begin{tabular}{l|c|c|c|c|c|c}
                 {\it N\/} & 1 & 10 & 50 & 100 & 500  & 1000 \\
 \hline 
{{\tt F}\/} ($1000 \cdot N$) & 1000 & 10000 & 50000 & 100000 & 500000 & 1000000 \\
 \hline 
{{\tt G}\/} ($N \cdot N$) & 1 & 100 & 2500 & 10000 & 250000 & 1000000 
\end{tabular}
\end{center}
At first glance the table seems to say that {{\tt G}\/}'s performance is
better than {{\tt F}\/}'s, because for inputs of the same size ({\it N\/}),
{{\tt G}\/}'s running time is always smaller than {{\tt F}\/}'s.  But a
closer look reveals that as the inputs get larger, {{\tt G}\/}'s advantage
decreases. Indeed, for an input of size 1000, the two functions need the
same number of steps, and thereafter {{\tt G}\/} is always slower than
{{\tt F}\/}. Figure~\ref{fig:big-oh} compares the graphs of the two
expressions. It shows that the linear graph for $1000 \cdot N$ dominates
the curve of $N \cdot N$ for some finite number of points but thereafter it
is below the curve. 

The concrete example recalls two important facts about our informal
discussion of abstract running time. First, our abstract description of
running time is always a claim about the relationship between two
quantities: the size of the input and the number of natural recursions
evaluated. More precisely, the relationship is a (mathematical) function
that maps an abstract size measure of the input to an abstract measure of
the running time. Second, when we compare ``on the order of'' properties of
functions, such as
$$N, \, \, N^2, \, \, \mbox{or} \, \, 2^N \, ,$$
we really mean to compare the corresponding functions that consume {\it N\/} and produce the above results. In short, a statement concerning the
order of things compares two functions on natural numbers
({{\bf N}\/}).

The comparison of functions on {{\bf N}\/} is difficult because
they are infinite. If a function {\it f\/} produces larger outputs than
some other function {\it g\/} for all natural numbers, then {\it f\/} is
clearly larger than {\it g\/}. But what if this comparison fails for just a
few inputs? Or for a 1,000 such as the one illustrated in
figure~\ref{fig:big-oh}? Because we would still like to make approximate
judgements, programmers and scientists adapt the mathematical notion of
comparing functions up to some factor and some finite number of exceptions.

\begin{quote}
{\sc {Order-of (Big-O)}}: Given a function $g$ on the natural numbers,
$O(g)$ (pronounced: ``on the order of $g$'' or ``big-O of $g$'') is a class
of functions on natural numbers.  A function {\it f\/} is in $O(g)$ if
there exist numbers {\it c\/} and {\it bigEnough\/} such that for all $n
\geq \mbox{\it bigEnough\/}$, it is true that $f(n) \leq c \cdot g(n).$
\end{quote}

Recall the performance of {\it F\/} and {\it G\/} from above. For the
first, we assumed that it consumed time according to the following function 
$$f(N) = 1000 \cdot N \, ;$$ 
the performance of second one obeyed the function {\it g\/}: 
$$g(N) = N^2 \, ;$$ 
Using the definition of big-O, we can say that {\it f\/} is $O(g)$, because
for all $n \geq 1000$, 
$$ f(n) \leq 1 \cdot g(n)$$
which means ${\it bigEnough\/} = 1000$ and $c = 1$. 

More importantly, the definition of big-O provides us with a shorthand for
stating claims about a functions running time. For example, from now on, we
say {{\tt how-many}\/}'s running time is $O(N)$. Keep in mind that {\it N\/}
is the standard abbreviation of the (mathematical) function $g(N) =
N$. Similarly, we can say that, in the worst case, {{\tt sort}\/}'s running
time is $O(N^2)$ and {{\tt max}\/}'s is $O(2^N)$.  

Finally, the definition of big-O explains why we don't have to pay
attention to specific constants in our comparsions of abstract running
time. Consider {{\tt max}\/} and {{\tt max2}\/}. We know that {{\tt max}\/}'s
worst-case running time is in $O(2^N)$, {{\tt max2}\/}'s is in $O(N)$. Say,
we need the maximum of a list with 10 numbers. Assuming {{\tt max}\/} and
{{\tt max2}\/} roughly consume the same amount of time per basic step,
{{\tt max}\/} will need $2^{10} = 1024$ steps and {{\tt max2}\/} will need 10
steps, which means {{\tt max2}\/} will be faster. Now even if
{{\tt max2}\/}'s basic step requires twice as much time as {{\tt max}\/}'s
basic step, {{\tt max2}\/} is still around 50 times faster. Futhermore, if
we double the size of the input list, {{\tt max}\/}'s apparent disadvantage
totally disappears. In general, the larger the input is, the less relevant
are the specific constants. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 29.2.1}

 \label{ex:big-o1}
In the first subsection, we stated that the function $f(n) = n^2 + n$
belongs to the class $O(n^2)$. Determine the pair of numbers $c$ and {\it bigEnough\/} that verify this claim.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/big-o1.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.2.2}

 \label{ex:big-o2}
Consider the functions $f(n) = 2^n$ and $g(n) = 1000 \cdot n$. Show that
{\it g\/} belongs to $O(f)$, which means that {\it f\/} is abstractly
speaking more (or at least equally) expensive than {\it g\/}. If the input
size is guaranteed to be between 3 and 12, which function is
better?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/big-o2.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.2.3}

 \label{ex:big-o3}
Compare $f(n) = n \log n$ and $g(n) = n^2$. Does $f$ belong to $O(g)$
and/or $g$ to $O(f)$?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/big-o3.html}{\rule{3pt}{5pt}}


 \subsection{A First Look at Vectors} \label{sec:vector1}

Until now we have paid little attention to how much time it takes to
retrieve data from structures or lists.  Now that we have a tool for
stating general judgements, let's take a close look at this basic
computation step.  Recall the last problem of the preceding part: finding a
route in a graph.  The program {{\tt find-route}\/} requires two auxiliary:
{{\tt find-route/list}\/} and {{\tt neighbors}\/}. We paid a lot of attention
to {{\tt find-route/list}\/} and none to {{\tt neighbors}\/}. Indeed,
developing {{\tt neighbors}\/} was just an exercise
(see~\ref{ex:find-route1}), because looking up a value in a list is by now
a routine programming task.

Here is a possible definition for {{\tt neighbors}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt neighbors}\ {\tt :}\ {\tt node}\ {\tt graph}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt node)}\/}}\\
\hbox{;; to lookup the {{\tt node}\/} in {{\tt graph}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (neighbors}\  {\tt node}\  {\tt graph)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt graph)}\  {\tt (error}\  {\tt \char'047}{\tt neighbors}\  {\tt ``can\char'047}{\tt t}\  {\tt happen'')\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt (first}\  {\tt (first}\  {\tt graph))}\  {\tt node)}\  {\tt (second}\  {\tt (first}\  {\tt graph))\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (neighbors}\  {\tt node}\  {\tt (rest}\  {\tt graph))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The function is similar to {{\tt contains-doll\char'077}\/} and has roughly the same
behavior. More concretely, {{\tt neighbors}\/} is $O(N)$ when we assume
that {{\tt graph}\/} is a list of {\it N\/} nodes. 

Considering that {{\tt neighbors}\/} is used at every stage of the
evaluation of {{\tt find-route}\/}, {{\tt neighbors}\/} is possibly a
bottleneck. As a matter of fact, if the route we are looking for involves
{{\tt N}\/} nodes (the maximum), {{\tt neighbors}\/} is applied {\it N\/}
times, so the algorithm requires $O(N^2)$ steps in {{\tt neighbors}\/}.

In contrast to lists, structures deal with value extractions as a constant
time operation. At first glance this observation seems to suggest that we
use structures as representations of graphs. A closer look, however, shows
that this idea doesn't work easily. The graph algorithm works best if we
are able to work with the names of nodes and access a node's neighbors
based on the name. A name could be a symbol or the node's number in the
graph. In general, what we really wish to have in a programming language is
\begin{quote}
a class of compound values size with constant lookup time, \\
 based on ``keys.''
\end{quote}
Because the problem is so common, Scheme and most other languages offer at
least one built-in solution.

Here we study the class of {\sl {vector}}s. A vector is a well-defined
mathematical class of data with specific basic operations. For our
purposes, it suffices to know how to construct them, how to extract values,
and how to recognize them: 
\begin{enumerate}
\item The operation {{\tt vector}\/} is like {{\tt list}\/}. It consumes an
arbitrary number of values and creates a compound value from them: a
vector. For example, {{\tt (vector}\ {\tt V-0}\ {\tt ...}\ {\tt V-n)}\/} creates a vector from
{{\tt V-0}\/} through {{\tt V-n}\/}. 

\item DrScheme also provides a vector analogue to {{\tt build-list}\/}. It
 is called {{\tt build-vector}\/}. Here is how it works:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (build-vector}\  {\tt N}\  {\tt f)}\  {\tt =}\  {\tt (vector}\  {\tt (f}\  {\tt 0)}\  {\tt ...}\  {\tt (f}\  {\tt (-}\  {\tt N}\  {\tt 1)))}\end{tabbing}\end{minipage}\end{center}
That is, {{\tt build-vector}\/} consumes a natural number {{\tt N}\/} and a function
{{\tt f}\/} on natural numbers. It then builds a vector of {{\tt N}\/} items by applying
{{\tt f}\/} to {{\tt 0}\/}, \ldots, {{\tt N-1}\/}.

\item The operation {{\tt vector-ref}\/} extracts a value from a vector in
constant time. That is, it is $O(1)$.  The characteristic equations have
the following shape:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (vector-ref}\  {\tt (vector}\  {\tt V-0}\  {\tt ...}\  {\tt V-n)}\  {\tt i)}\  {\tt =}\  {\tt V-i}\end{tabbing}\end{minipage}\end{center}
where {{\tt i}\/} is between {{\tt 0}\/} and {{\tt n}\/} (inclusive). 

If {{\tt vector-ref}\/} is applied to a vector and a natural number that is
smaller than {{\tt 0}\/} or larger than {{\tt n}\/}, {{\tt vector-ref}\/}
signals an error. 

\item The operation {{\tt vector-length}\/} produces the number of items
in a vector: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (vector-length}\  {\tt (vector}\  {\tt V-0}\  {\tt ...}\  {\tt V-n))}\  {\tt =}\  {\tt (+}\  {\tt n}\  {\tt 1)}\end{tabbing}\end{minipage}\end{center}

\item The operation {{\tt vector?}\/} is the vector-predicate. It produces
{{\tt true}\/} if its argument is a vector and {{\tt false}\/} otherwise: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (vector?}\  {\tt (vector}\  {\tt V-0}\  {\tt ...}\  {\tt V-n))}\  {\tt =}\  {\tt true}\\
{\tt (vector?}\  {\tt U)}\  {\tt =}\  {\tt false}
\end{tabbing}\end{minipage}\end{center}
if {{\tt U}\/} is a value that isn't created with {{\tt vector}\/}.
\end{enumerate}

We can think of {vector}s as functions on a small, finite range of natural
numbers. Their range is the full class of Scheme values. We can also think
of them as tables that associate a small, finite range of natural numbers
with Scheme values. Using vectors we can represent graphs like those in
figures~\ref{fig:graph} and~\ref{fig:cyclic-graph} with {vector}s if we use
numbers as names. For example:
\begin{center}
\begin{tabular}{c|c|c|c|c|c|c}
A & B & C & D & E & F & G \\
 \hline
0 & 1 & 2 & 3 & 4 & 5 & 6 
\end{tabular}
\end{center}
Using this translation, we can also produce a {vector}-based representation of
the graph in figure~\ref{fig:graph}:
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt Graph-as-list}\\
\>{\tt \char'047}{\tt (}\={\tt (A}\  {\tt (B}\  {\tt E))}
\\
\>\>{\tt (B}\  {\tt (E}\  {\tt F))}
\\
\>\>{\tt (C}\  {\tt (D))}
\\
\>\>{\tt (D}\  {\tt ())}
\\
\>\>{\tt (E}\  {\tt (C}\  {\tt F))}
\\
\>\>{\tt (F}\  {\tt (D}\  {\tt G))}
\\
\>\>{\tt (G}\  {\tt ())))}
\end{tabbing}\end{minipage}
\qquad\qquad\qquad
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt Graph-as-vector}\\
\>{\tt (vector}\  \={\tt (list}\  {\tt 1}\  {\tt 4)}
\\
\>\>{\tt (list}\  {\tt 4}\  {\tt 5)}
\\
\>\>{\tt (list}\  {\tt 3)}
\\
\>\>{\tt empty}
\\
\>\>{\tt (list}\  {\tt 2}\  {\tt 5)}
\\
\>\>{\tt (list}\  {\tt 3}\  {\tt 6)}
\\
\>\>{\tt empty))}
\end{tabbing}\end{minipage}
\end{center}
The definition on the left is the original list-based representation; the
one on the right is a vector representation. The vector's {{\tt i}\/}-th
field contains the list of neighbors of the {{\tt i}\/}-th node.

The data definitions for {{\tt node}\/} and {{\tt graph}\/} change in the
expected manner. Let's assume that $N$ is the number of nodes in the given
graph: 

\begin{quote} A {\sl {node}} is an natural number between $0$ and $N-1$. \end{quote}

\begin{quote} A {\sl {graph}} is a vector of {{\tt node}\/}s: {{\tt (vectorof}{ }{\tt (listof}\ {\tt node))}\/}. \end{quote}

\noindent The notation {{\tt (vectorof}\ {\tt X)}\/} is similar to {{\tt (listof}{ }{\tt X)}\/}. It denotes a vector that contains items from some undetermined class
of data {{\tt X}\/}. 

Now we can re-define {{\tt neighbors}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt neighbors}\ {\tt :}\ {\tt node}\ {\tt graph}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt node)}\/}}\\
\hbox{;; to lookup the {{\tt node}\/} in {{\tt graph}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (neighbors}\  {\tt node}\  {\tt graph)}
\\
\>{\tt (vector-ref}\  {\tt graph}\  {\tt node))}
\end{tabbing}\end{minipage}\end{center}
As a result, looking up the neighbors of a node just became a constant-time
operation, and we can truly ignore it when we study the abstract running
time of {{\tt find-route}\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 29.3.1}

 \label{ex:vector-neighbors}
Test the new {{\tt neighbors}\/} function.  Use the strategy of
section~\ref{sec:equal-test} to formulate the tests as boolean
expressions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector-neighbors.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.3.2}

 \label{ex:vector0}
Adapt the rest of the {{\tt find-route}\/} program to the new vector
representation. Adapt the tests from exercises~\ref{ex:find-route2}
through~\ref{ex:find-route4} to check the new program.

Measure how much time the two {{\tt find-route}\/} programs consume to
compute a route from node A to node E in the graph of
figure~\ref{fig:graph}. Recall that {{\tt (time}\ {\tt expr)}\/} measures how long
it takes to evaluate {{\tt expr}\/}. It is good practice to evaluate
{{\tt expr}\/}, say, 1000 times when we measure time. This produces more
accurate measurements.  \htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector0.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.3.3}

 \label{ex:vector-cycle}
Translate the cyclic graph from figure~\ref{fig:cyclic-graph} into our
vector representation of graphs.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector-cycle.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Before we can truly program with vectors, we must understand the data
definition. The situation is comparable to that when we first encountered
lists. We know that {{\tt vector}\/}, like {{\tt cons}\/}, is provided by
Scheme, but we don't have a data definition that directs our program
development efforts.

So, let us take a look at vectors. Roughly speaking, {{\tt vector}\/} is
like {{\tt cons}\/}. The {{\tt cons}\/} primitive constructs lists, the
{{\tt vector}\/} primitive creates vectors.  Since programming with lists
usually means programming with the selectors {{\tt first}\/} and
{{\tt rest}\/}, programming with vectors must mean programming with
{{\tt vector-ref}\/}. Unlike {{\tt first}\/} and {{\tt rest}\/}, however,
{{\tt vector-ref}\/} requires manipulating the vector and an index into a
vector. This suggests that programming with vectors really means thinking
about indices, which are natural numbers. 

Let's look at some simple examples to confirm this abstract judgement. Here
is the first one: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt vector-sum-for-3}\ {\tt :}\ {\tt (vector}\ {\tt number}\ {\tt number}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (vector-sum-for-3}\  {\tt v)}
\\
\>{\tt (+}\  \={\tt (vector-ref}\  {\tt v}\  {\tt 0)}
\\
\>\>{\tt (vector-ref}\  {\tt v}\  {\tt 1)}
\\
\>\>{\tt (vector-ref}\  {\tt v}\  {\tt 2)))}
\end{tabbing}\end{minipage}\end{center}
The function {{\tt vector-sum-for-3}\/} consumes vectors of three numbers
and produces their sum. It uses {{\tt vector-ref}\/} to extract the three
numbers and adds them up. What varies in the three selector expressions is
the index; the vector remains the same. 

Consider a second, more interesting example: {{\tt vector-sum}\/}, a
generalization of {{\tt vector-sum-for-3}\/}. It consumes an arbitrarily
large vector of numbers and produces the sum of the numbers:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt vector-sum}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to sum up the numbers in {{\tt v}\/}}
\\
{\tt (define}\  {\tt (vector-sum}\  {\tt v)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Here are some examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (vector-sum}\  {\tt (vector}\  {\tt -1}\  {\tt 3/4}\  {\tt 1/4))}\\
{\tt =}\  \={\tt 0}
\\
\>{\tt (vector-sum}\  {\tt (vector}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1))}
\\
{\tt =}\  \={\tt 1}
\\
\>{\tt (vector-sum}\  {\tt (vector))}
\\
{\tt =}\  {\tt 0}
\end{tabbing}\end{minipage}\end{center}
The last example suggests that we want a reasonable answer even if the
vector is empty. As with {{\tt empty}\/}, we use {{\tt 0}\/} as the answer in
this case. 

The problem is that the one natural number associated with {{\tt v}\/}, its
length, is not an argument of {{\tt vector-sum}\/}. The length of {{\tt v}\/}
is of course just an indication of how many items in {{\tt v}\/} are to be
processed, which in turn refers to legal indices of {{\tt v}\/}. This
reasoning forces us to develop an auxiliary function that consumes the
vector and a natural number: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt vector-sum-aux}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\bf N}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to sum up the numbers in {{\tt v}\/} relative to {{\tt i}\/} }
\\
{\tt (define}\  {\tt (vector-sum-aux}\  {\tt v}\  {\tt i)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
The natural choice for the initial value of {{\tt i}\/} is the length of
{{\tt v}\/}, which suggests the following completion of {{\tt vector-sum}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (vector-sum}\  {\tt v)}\  \\
\>{\tt (vector-sum-aux}\  {\tt v}\  {\tt (vector-length}\  {\tt v)))}
\end{tabbing}\end{minipage}\end{center}

Based on this definition, we can also adapt the examples for
{{\tt vector-sum}\/} to {{\tt vector-sum-aux}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (vector-sum-aux}\  {\tt (vector}\  {\tt -1}\  {\tt 3/4}\  {\tt 1/4)}\  {\tt 3)}\\
{\tt =}\  \={\tt 0}
\\
\>{\tt (vector-sum-aux}\  {\tt (vector}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1}\  {\tt .1)}\  {\tt 10)}
\\
{\tt =}\  \={\tt 1}
\\
\>{\tt (vector-sum-aux}\  {\tt (vector)}\  {\tt 0)}
\\
{\tt =}\  {\tt 0}
\end{tabbing}\end{minipage}\end{center}
Unfortunately, this doesn't clarify the role of the second argument. To do
that, we need to proceed to the next stage of the design process: the
template development. 

When we develop templates for functions of two arguments, we must first
decide which of the arguments must be processed, that is, which of the two
will vary in the course of a computation. The {{\tt vector-sum-for-3}\/}
example suggests that it is the second argument in this case. Because this
argument belongs to the class of natural numbers, we follow the design recipe
for those: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (vector-sum-aux}\  {\tt v}\  {\tt i)}\  \\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt i)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (vector-sum-aux}\  {\tt v}\  {\tt (sub1}\  {\tt i))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Although we considered {{\tt i}\/} to be the length of the vector initially,
the template suggests that we should consider it the number of items of
{{\tt v}\/} that {{\tt vector-sum-aux}\/} must consider and thus as an index
into {{\tt v}\/}. 

The elaboration of {{\tt i}\/}'s use naturally leads to a better purpose
statement for {{\tt vector-sum-aux}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt vector-sum-aux}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\bf N}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to sum up the numbers in {{\tt v}\/} with index in [{{\tt 0}\/}, {{\tt i}\/})}
\\
{\tt (d}\={\tt efine}\  {\tt (vector-sum-aux}\  {\tt v}\  {\tt i)}\  
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt i)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (vector-sum-aux}\  {\tt v}\  {\tt (sub1}\  {\tt i))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Excluding {{\tt i}\/} is natural because it is initially
{{\tt (vector-length}\ {\tt v)}\/} and thus not an index. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt vector-sum}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the sum of the numbers in {{\tt v}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (vector-sum}\  {\tt v)}\  
\\
\>{\tt (vector-sum-aux}\  {\tt (vector-length}\  {\tt v)}\  {\tt v))}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt vector-sum-aux}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\bf N}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to sum the numbers in {{\tt v}\/} with index in [{{\tt 0}\/}, {{\tt i}\/})}
\\
{\tt (d}\={\tt efine}\  {\tt (vector-sum-aux}\  {\tt v}\  {\tt i)}\  
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt i)}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  \={\tt (vector-ref}\  {\tt v}\  {\tt (sub1}\  {\tt i))}\  
\\
\>\>\>{\tt (vector-sum-aux}\  {\tt v}\  {\tt (sub1}\  {\tt i)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Summing up the numbers in a vector (version 1)} \label{fig:vector-sum}

\hrulefill 

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt lr-vector-sum}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to sum up the numbers in {{\tt v}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (lr-vector-sum}\  {\tt v)}
\\
\>{\tt (vector-sum-aux}\  {\tt v}\  {\tt 0))}
\\
\hbox{;; {{\tt vector-sum}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to sum up the numbers in {{\tt v}\/} with index in [{{\tt i}\/}, {{\tt (vector-length}\ {\tt v)}\/})}
\\
{\tt (d}\={\tt efine}\  {\tt (vector-sum-aux}\  {\tt v}\  {\tt i)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt i}\  {\tt (vector-length}\  {\tt v))}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (vector-ref}\  {\tt v}\  {\tt i)}\  {\tt (vector-sum-aux}\  {\tt v}\  {\tt (add1}\  {\tt i)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Summing up the numbers in a vector (version 2)} \label{fig:vector-sum-2}
\hrule

To transform the template into a complete function definition, we consider
each clause of the {{\tt cond}\/}:
\begin{enumerate}
\item If {{\tt i}\/} is {{\tt 0}\/}, there are no further items to be
 considered because there are no vector fields between {{\tt 0}\/} and
 {{\tt i}\/} with {{\tt i}\/} excluded. Therefore the result is {{\tt 0}\/}. 

\item Otherwise, {{\tt (vector-sum-aux}\ {\tt v}\ {\tt (sub1}\ {\tt i))}\/} computes the sum of
 the numbers in {{\tt v}\/} between {{\tt 0}\/} and {{\tt (sub1}\ {\tt i)}\/}
 [exclusive]. This leaves out the vector field with index {{\tt (sub1}{ }\ {\tt i)}\/}, which according to the purpose statement must be included.  By adding
 {{\tt (vector-ref}\ {\tt v}\ {\tt (sub1}\ {\tt i))}\/}, we get the desired result: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (+}\  {\tt (vector-ref}\  {\tt v}\  {\tt (sub1}\  {\tt i))}\  {\tt (vector-sum-aux}\  {\tt v}\  {\tt (sub1}\  {\tt i)))}\end{tabbing}\end{minipage}\end{center}
\end{enumerate}
See figure~\ref{fig:vector-sum} for the complete program. 

If we were to evaluate one of the examples for {{\tt vector-sum-aux}\/} by
hand, we would see that it extracts the numbers from the vector in a right
to left order as {{\tt i}\/} decreases to {{\tt 0}\/}. A natural question is
whether we can invert this order. In other words: is there a function that
extract the numbers in a left to right order?

The answer is to develop a function that processes the class of natural
numbers below {{\tt (vector-length}\ {\tt v)}\/} and to start at the first feasible
index: {{\tt 0}\/}. Developing this function is just another instance of the
design recipe for variants of natural numbers from
section~\ref{sec:nat-num:alt}.  The new function definition is shown in
figure~\ref{fig:vector-sum-2}. The new auxiliary function now consumes
{{\tt 0}\/} and counts up to {{\tt (vector-length}\ {\tt v)}\/}. A hand-evaluation
of
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (lr-vector-sum}\  {\tt (vector}\  {\tt 0}\  {\tt 1}\  {\tt 2}\  {\tt 3))}\end{tabbing}\end{minipage}\end{center}
shows that {{\tt vector-sum-aux}\/} indeed extracts the items from
{{\tt v}\/} from left to right. 

The definition of {{\tt lr-vector-sum}\/} shows why we need to study
alternative definitions of classes of natural numbers. Sometimes it is
necessary to count down to {{\tt 0}\/}. But at other times it is equally
useful, and natural, to count from {{\tt 0}\/} up to some other number.

The two functions also show how important it is to reason about
intervals. The auxiliary vector-processing functions process intervals of
the given vector. A good purpose statement specifies the exact interval
that the function works on. Indeed, once we understand the exact interval
specification, formulating the full function is relatively
straightforward. We will see the importance of this point when we return to
the study of vector-processing functions in the last section. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 29.3.4}

 \label{ex:vector-sum} Evaluate {{\tt (vector-sum-aux}\ {\tt (vector}\ {\tt -1}\ {\tt 3/4}\ {\tt 1/4)}\ {\tt 3)}\/} by hand. Show the
major steps only. Check the evaluation with DrScheme's stepper. In what
order does the function add up the numbers of the vector? 

Use a {{\bf local}-exp\-res\-sion} to define a single function
{{\tt vector-sum}\/}. Then remove the vector argument from the inner
function definition. Why can we do that?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector-sum.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.3.5}

 \label{ex:lr-vector-sum} Evaluate {{\tt (lr-vector-sum}\ {\tt (vector}\ {\tt -1}\ {\tt 3/4}\ {\tt 1/4))}\/} by hand. Show the
major steps only. Check the evaluation with DrScheme's stepper. In what
order does the function add up the numbers of the vector?

Use a {{\bf local}-exp\-res\-sion} to define a single function
{{\tt lr-vector-sum}\/}. Then remove those arguments from the inner function
definition that remain the same during an evaluation. Also introduce
definitions for those expressions that always evaluate to the same
value during the evaluation. Why is this useful?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/lr-vector-sum.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.3.6}

 \label{ex:vector-list} The list-based analogue of {{\tt vector-sum}\/} is {{\tt list-sum}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt list-sum}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/} }\\
\hbox{;; to compute the sum of the numbers on {{\tt alon}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (list-sum}\  {\tt alon)}
\\
\>{\tt (list-sum-aux}\  {\tt alon}\  {\tt (length}\  {\tt alon)))}
\\
\hbox{;; {{\tt list-sum-aux}\ {\tt :}\ {\bf N}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/} }
\\
\hbox{;; to compute the sum of the first {{\tt L}\/} numbers on {{\tt alon}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (list-sum-aux}\  {\tt L}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt L)}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (list-ref}\  {\tt alon}\  {\tt (sub1}\  {\tt L))}\  {\tt (list-sum-aux}\  {\tt (sub1}\  {\tt L)}\  {\tt alon))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Instead of using the structural definition of the list, the developer of
this program used the size of the list---a natural number---as the guiding
element in the design process.  

The resulting definition uses Scheme's {{\tt list-ref}\/} function to access
each item on the list.  Looking up an item in a list with {{\tt list-ref}\/}
is an $O(N)$ operation for lists of {\it N\/} items. Determine the abstract
running time of {{\tt sum}\/} (from section~\ref{sec:lists:more}),
{{\tt vector-sum-aux}\/} and {{\tt list-sum-aux}\/}. What does this suggest
about program development?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector-list.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.3.7}

 \label{ex:vector-norm} Develop the function {{\tt norm}\/}, which consumes a vector of numbers and
produces the square root of the square of its sum. Another name for
{{\tt norm}\/} is {{\tt distance-to-0}\/}, because the result is the distance
of a vector to the origin, when we interpret the vector as a
point.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector-norm.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.3.8}

 \label{ex:vector-find} Develop the function {{\tt vector-contains-doll\char'077}\/}. It consumes a vector
of symbols and determines whether the vector contains the symbol
{{\tt \char'047}{\tt doll}\/}. If so, it produces the index of {{\tt \char'047}{\tt doll}\/}'s field;
otherwise, it produces {{\tt false}\/}.

Determine the abstract running time of {{\tt vector-contains-doll\char'077}\/} and
compare with that of {{\tt contains-doll\char'077}\/}, which we discussed in the
preceding subsection. 

Now discuss the following problem. Suppose we are to represent a collection
of symbols. The only interesting problem concerning the collection is to
determine whether it contains some given symbol. Which data representation is
preferable for the collection: lists or vectors? Why?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector-find.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.3.9}

 \label{ex:vector-binary-find} 
Develop the function {{\tt binary-contains\char'077}\/}. It consumes a sorted vector
of numbers and a key, which is also a number. The goal is to determine the
index of the key, if it occurs in the vector, or {{\tt false}\/}. Use the
binary-search algorithm from section~\ref{sec:binary-search}. 

Determine the abstract running time of {{\tt binary-contains\char'077}\/} and
compare with that of {{\tt contains\char'077}\/}, the function that searches for a
key in a vector in the linear fashion of {{\tt vector-contains-doll\char'077}\/}. 

Suppose we are to represent a collection of numbers. The only interesting
problem concerning the collection is to determine whether it contains some
given number. Which data representation is preferable for the collection:
lists or vectors?  Why?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector-binary-find.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.3.10}

 \label{ex:vector-count} Develop the function {{\tt vector-count}\/}. It consumes a vector {{\tt v}\/}
of symbols and a symbol {{\tt s}\/}. Its result is the number of {{\tt s}\/}
that occur in {{\tt v}\/}. 

Determine the abstract running time of {{\tt vector-count}\/} and compare
with that of {{\tt count}\/}, which counts how many times {{\tt s}\/} occurs
in a list of symbols. 

Suppose we are to represent a collection of symbols. The only interesting
problem concerning the collection is to determine how many times it
contains some given symbol. Which data representation is preferable for the
collection: lists or vectors?  Why? What do exercises~\ref{ex:vector-find},
\ref{ex:vector-binary-find}, and this exercise
suggest?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector-count.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


While accessing the items of a vector is one kind of programming problem,
constructing vectors is an entirely different problem. When we know the
number of items in a vector, we can construct it using
{{\tt vector}\/}. When we we wish to write programs, however, that work on a
large class of vectors independent of their size, we need
{{\tt build-vector}\/}.

Consider the following simple example. Suppose we represent the velocity of
an object with a vector. For example, {{\tt (vector}\ {\tt 1}\ {\tt 2)}\/} represents the
veloicity of an object on a plane that moves {{\tt 1}\/} unit to the right
and {{\tt 2}\/} down in each time unit. For comparison, {{\tt (vector}\ {\tt -1}\ {\tt 2}{ }{\tt 1)}\/} is the veloicity of an object in space; it moves 
{{\tt -6}\/} units in the $x$-direction in 6 time units,
{{\tt 12}\/} units in the $y$-direction in 6 time units, and
{{\tt 6}\/} units in the $z$-direction in 6 time units. We call {{\tt (vector}\ {\tt -6}{ }{\tt 12}\ {\tt 6)}\/} the {\sl displacement} of the object in 6 time units. 

Let's develop a function that computes the displacement for an object with
some velocity {{\tt v}\/} in {{\tt t}\/} time units:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt displacement}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\tt number}\ {}{\tt ->}{}\ {\tt (vectorof}\ {\tt number)}\/}}\\
\hbox{;; to compute the displacement of {{\tt v}\/} and {{\tt t}\/}}
\\
{\tt (define}\  {\tt (displacement}\  {\tt v}\  {\tt t)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Computing the displacement is straightforward for some examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (displacement}\  {\tt (vector}\  {\tt 1}\  {\tt 2)}\  {\tt 3)}\  \\
{\tt =}\  \={\tt (vector}\  {\tt 3}\  {\tt 6)}
\\
\>{\tt (displacement}\  {\tt (vector}\  {\tt -1}\  {\tt 2}\  {\tt 1)}\  {\tt 6)}\  
\\
{\tt =}\  \={\tt (vector}\  {\tt -6}\  {\tt 12}\  {\tt 6)}
\\
\>{\tt (displacement}\  {\tt (vector}\  {\tt -1}\  {\tt -2)}\  {\tt 2)}\  
\\
{\tt =}\  {\tt (vector}\  {\tt -2}\  {\tt -4)}
\end{tabbing}\end{minipage}\end{center}
We just multiply each component of the object with the number, which yields
a new vector. 

The examples' meaning for our programming problem is that
{{\tt displacement}\/} must construct a vector of the same how-many as
{{\tt v}\/} and must use the items in {{\tt v}\/} to compute those of the new
vectors. Here is how we build a vector of the same how-many as some given
vector {{\tt v}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (build-vector}\  {\tt (vector-length}\  {\tt v)}\  {\tt ...)}\end{tabbing}\end{minipage}\end{center}
Now we need to replace \ldots\ with a function that computes the
{{\tt 0}\/}-th, {{\tt 1}\/}-st, and so on items of the new vector: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt new-item}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the contents of the new vector at the {{\tt i}\/}-th position}
\\
{\tt (define}\  {\tt (new-item}\  {\tt index)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Following our discussion, we multiply {{\tt (vector-ref}\ {\tt v}\ {\tt i)}\/} with
{{\tt t}\/} and that's all.

Take a look at the complete definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt displacement}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\tt number}\ {}{\tt ->}{}\ {\tt (vectorof}\ {\tt number)}\/}}\\
\hbox{;; to compute the displacement of {{\tt v}\/} and {{\tt t}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (displacement}\  {\tt v}\  {\tt t)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt (new-item}\  {\tt i)}\  {\tt (*}\  {\tt (vector-ref}\  {\tt v}\  {\tt i)}\  {\tt t)))}
\\
\>\>{\tt (build-vector}\  {\tt (vector-length}\  {\tt v)}\  {\tt new-item)))}
\end{tabbing}\end{minipage}\end{center}
The locally defined function is not recursive. We can thus replace it with
a plain {{\bf lambda}-exp\-res\-sion}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt displacement}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\tt number}\ {}{\tt ->}{}\ {\tt (vectorof}\ {\tt number)}\/}}\\
\hbox{;; to compute the displacement of {{\tt v}\/} and {{\tt t}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (displacement}\  {\tt v}\  {\tt t)}
\\
\>{\tt (build-vector}\  {\tt (vector-length}\  {\tt v)}\  {\tt (lambda}\  {\tt (i)}\  {\tt (*}\  {\tt (vector-ref}\  {\tt v}\  {\tt i)}\  {\tt t))))}
\end{tabbing}\end{minipage}\end{center}
Mathematicians call this function {\sl scalar product}. They have also
studied many other operations on vectors, and in Scheme we can develop
those in a natural manner. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 29.3.11}

 \label{ex:vector1} 
Develop the function {{\tt id-vector}\/}, which consumes a natural number
and produces a vector of that many {{\tt 1}\/}'s.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector1.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.3.12}

 \label{ex:vector-delta} 
Develop the functions {{\tt vector+}\/} and {{\it vector\/}$-$\/}, which compute
the pointwise sum and differences of two vectors. That is, each consumes
two vectors and produces a vector by manipulating corresponding
programs. Assume the given vectors are of the same how-many. Also develop the
functions {{\tt checked-vector+}\/} and {{\tt checked-vector-}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector-delta.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.3.13}

 \label{ex:vector-distance} 
Develop the function {{\tt distance}\/}, which consumes two vectors and
computes their distance. Think of the distance of two vectors as the length
of the line between them.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector-distance.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.3.14}

 \label{ex:vector-chess}
Develop a vector representation for chess boards of size $n \times n$ for
$n$ in {{\bf N}\/}. Then develop the following two functions on chess boards:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt build-board}\ {\tt :}\ {\bf N}\ {\tt (}{\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\tt boolean)}\ {}{\tt ->}{}\ {\tt board}\/}}\\
\hbox{;; to create a board of size {{\tt n}\/} x {{\tt n}\/}, }
\\
\hbox{;; fill each position with indices {{\tt i}\/} and {{\tt j}\/} with {{\tt (f}\ {\tt i}\ {\tt j)}\/}}
\\
{\tt (define}\  {\tt (build-board}\  {\tt n}\  {\tt f)}\  {\tt ...)}
\\
\hbox{;; {{\tt board-ref}\ {\tt :}\ {\tt board}\ {\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\tt boolean}\/}}
\\
\hbox{;; to access a position with indices {{\tt i}\/}, {{\tt j}\/} on {{\tt a-board}\/}}
\\
{\tt (define}\  {\tt (board-ref}\  {\tt a-board}\  {\tt i}\  {\tt j)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Can we now run the program of section~\ref{sec:queens} using vectors
instead of lists? Inspect the solution of exercises~\ref{ex:queen2}
and~\ref{ex:queen}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector-chess.html}{\rule{3pt}{5pt}}


{\bf Exercise 29.3.15}

 \label{ex:vector-matrix} 
A matrix is a chess board of numbers. Use the chess-board representation 
of exercise~\ref{ex:vector-chess} to represent the matrix
$$\begin{array}{|rrr|}
1 & 0 & -1 \\
2 & 0 & 9 \\
1 & 1 & 1 
\end{array}$$

Using {{\tt build-board}\/}, develop the function {{\tt transpose}\/}, which
creates a mirror image of the matrix along its diagonal from the upper-left
corner to the lower-right one. For example, the given matrix turns into 
$$\begin{array}{|rrr|}
1 & 2 & 1 \\
0 & 0 & 1 \\
-1 & 9 & 1 
\end{array}$$
More generally, the item at $(i,j)$ becomes the item at $(j,i)$.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/vector-matrix.html}{\rule{3pt}{5pt}}

 

%% BEGIN INLINED FILE: curriculum5.dl.tex





\newpage\part{Accumulating Knowledge}\thispagestyle{empty}\newpage \label{part:loops}

\section{The Loss of Knowledge} \label{sec:loss}

When we design recursive functions, we don't think about the context of
their use. Whether they are applied for the first time or whether they are
called for the 100th time in a recursive manner doesn't matter. They are to
work according to their purpose statement, and that's all we need to know
as we design the bodies of the functions.

Altough this principle of context-independence greatly facilitates the
development of functions, it also causes occasional problems. In this
section, we illustrate the most important problem with two kinds of
examples. Both concern the loss of knowledge that occurs during a recursive
evaluation. The first subsection shows how this loss make a structurally
recursive function more complicated and less efficient than necessary; the
second one shows how the loss of knowledge causes a fatal flaw in an algorithm.

\subsection{A Problem with Structural Processing} \label{sec:loss:structural}

Suppose we are given the relative distances between a series of points,
starting at the origin, and suppose we are to compute the absolute
distances from the origin. For example, we might be given a line such as
this one:
\begin{center}
\begin{picture}(300,20)
\put(000,0){if {{\tt current-color}\/} is 50 and we evaluate {{\tt (next)}\/}, then {{\tt current-color}\/} is 20}
\put(050,0){if {{\tt current-color}\/} is 40 and we evaluate {{\tt (next)}\/}, then {{\tt current-color}\/} is 15}
\put(090,0){if {{\tt current-color}\/} is 70 and we evaluate {{\tt (next)}\/}, then {{\tt current-color}\/} is 30}
\put(160,0){if {{\tt current-color}\/} is 30 and we evaluate {{\tt (next)}\/}, then {{\tt current-color}\/} is 10}
\put(190,0){if {{\tt current-color}\/} is 30 and we evaluate {{\tt (next)}\/}, then {{\tt current-color}\/} is 10}
\end{picture}
\end{center}
Each number specifies the distance between two dots. What we need is the
following picture, where each dot is annotated with the distance to the 
left-most dot: 
\begin{center}
\begin{picture}(300,20)
\put(0,3){\mbox{0}}
\put(000,0){ \begin{picture}(50,10) 
\put(50,3){\mbox{\hspace{-7pt}50}} 
\put(0,0){\circle*{3}} 
\put(0,0){\line(1,0){50}} 
\put(50,0){\circle*{3}} \end{picture}}
\put(050,0){ \begin{picture}(40,10) 
\put(40,3){\mbox{\hspace{-7pt}90}} 
\put(0,0){\circle*{3}} 
\put(0,0){\line(1,0){40}} 
\put(40,0){\circle*{3}} \end{picture}}
\put(090,0){ \begin{picture}(70,10) 
\put(70,3){\mbox{\hspace{-7pt}160}} 
\put(0,0){\circle*{3}} 
\put(0,0){\line(1,0){70}} 
\put(70,0){\circle*{3}} \end{picture}}
\put(160,0){ \begin{picture}(30,10) 
\put(30,3){\mbox{\hspace{-7pt}190}} 
\put(0,0){\circle*{3}} 
\put(0,0){\line(1,0){30}} 
\put(30,0){\circle*{3}} \end{picture}}
\put(190,0){ \begin{picture}(30,10) 
\put(30,3){\mbox{\hspace{-7pt}220}} 
\put(0,0){\circle*{3}} 
\put(0,0){\line(1,0){30}} 
\put(30,0){\circle*{3}} \end{picture}}
\end{picture}
\end{center}

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt relative-2-absolute}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt number)}\/}}\\
\hbox{;; to convert a list of relative distances to a list of absolute distances}
\\
\hbox{;; the first item on the list represents the distance to the origin}
\\
{\tt (d}\={\tt efine}\  {\tt (relative-2-absolute}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  \={\tt (first}\  {\tt alon)}
\\
\>\>\>{\tt (add-to-each}\  {\tt (first}\  {\tt alon)}\  {\tt (relative-2-absolute}\  {\tt (rest}\  {\tt alon))))\char'135}{\tt ))}
\\
\hbox{;; {{\tt add-to-each}\ {\tt :}\ {\tt number}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt number)}\/}}
\\
\hbox{;; to add {{\tt n}\/} to each number on {{\tt alon}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (add-to-each}\  {\tt n}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt (+}\  {\tt (first}\  {\tt alon)}\  {\tt n)}\  {\tt (add-to-each}\  {\tt n}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Converting relative distances to absolute distances}
\label{fig:rel-abs-dist}
\hrule

Developing a program that performs this calculation is at this point an
exercise in structural function design. Figure~\ref{fig:rel-abs-dist}
contains the complete Scheme program. When the given list is not
{{\tt empty}\/}, the natural recursion computes the absolute distance of the
remainder of the dots to the first item on {{\tt (rest}\ {\tt alon)}\/}. Because
the first item is not the actual origin and has a distance of
{{\tt (first}\ {\tt alon)}\/} to the origin, we must add {{\tt (first}\ {\tt alon)}\/} to
each and every item on the result of the recursive application. This second
step, adding a number to each item on a list of numbers, requires an
auxiliary function.

While the development of the program is straightforward, using it on larger
and larger lists reveals a problem. Consider the evaluation of the
following definition:\footnote{The most convenient way to construct this list is to evaluate {{\tt (build-list}\ {\tt (add1}\ {\tt N)}\ {\tt identity)}\/}.}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt (relative-2-absolute}\  {\tt (list}\  {\tt 0}\  {\tt ...}\  {\tt N)))}\end{tabbing}\end{minipage}\end{center}
As we increase {{\tt N}\/}, the time needed grows even
faster:\footnote{The time of evaluation will differ from computer to computer. These measurements were conducted on a Pentium 166Mhz running Linux. Measuring timings is also difficult. At a minimum, each evaluation should be repeated several times, and the time reported should be the average of those measurements.} 
\begin{center}
\begin{tabular}{c|r}
\mbox{{{\tt N}\/}} & \mbox{time of evaluation} \\
 \hline 
 100 &    220 \\
 200 &    880 \\
 300 &   2050 \\
 400 &   5090 \\
 500 &   7410 \\
 600 &  10420 \\
 700 &  14070 \\
 800 &  18530 
\end{tabular}
\end{center}
Instead of doubling as we go from {100} to {200} items, the time
quadruples. This is also the approximate relationship for going from {200}
to {400}, {300} to {600}, and so on.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 30.1.1}

 \label{ex:add-to-each=map}
Reformulate {{\tt add-to-each}\/} using {{\tt map}\/} and
{{\tt lambda}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/add-to-each=map.html}{\rule{3pt}{5pt}}


{\bf Exercise 30.1.2}

 \label{ex:rel-abs-O}
Determine the abstract running time of {{\tt relative-2-absolute}\/}.

\noindent{\bf Hint:}  Evaluate the expression 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (relative-2-absolute}\  {\tt (list}\  {\tt 0}\  {\tt ...}\  {\tt N))}\end{tabbing}\end{minipage}\end{center}
by hand. Start by replacing {{\tt N}\/} with 1, 2, and 3. How many natural
recursions of {{\tt relative-2-absolute}\/} and {{\tt add-to-each}\/} are
required each time?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/rel-abs-O.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Considering the simplicity of the problem, the amount of ``work'' that the
two functions perform is surprising. If we were to convert the same list by
hand, we would tally up the total distance and just add it to the relative
distances as we take another step along the line.

Let's attempt to design a second version of the function that is closer to
our hand method. The new function is still a list-processing function, so we
start from the appropriate template:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (rel-2-abs}\  {\tt alon)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt alon)}\  {\tt ...}\  {\tt (rel-2-abs}\  {\tt (rest}\  {\tt alon))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Now imagine an ``evaluation'' of {{\tt (rel-2-abs}\ {\tt (list}\ {\tt 3}\ {\tt 2}\ {\tt 7))}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (rel-2-abs}\  {\tt (list}\  {\tt 3}\  {\tt 2}\  {\tt 7))}\\
{\tt =}\  {\tt (c}\={\tt ons}\  {\tt ...}\  {\tt 3}\  {\tt ...}
\\
\>{\tt (convert}\  {\tt (list}\  {\tt 2}\  {\tt 7)))}
\\
{\tt =}\  {\tt (c}\={\tt ons}\  {\tt ...}\  {\tt 3}\  {\tt ...}
\\
\>{\tt (c}\={\tt ons}\  {\tt ...}\  {\tt 2}\  {\tt ...}
\\
\>\>{\tt (convert}\  {\tt (list}\  {\tt 7))))}
\\
{\tt =}\  {\tt (c}\={\tt ons}\  {\tt ...}\  {\tt 3}\  {\tt ...}
\\
\>{\tt (c}\={\tt ons}\  {\tt ...}\  {\tt 2}\  {\tt ...}
\\
\>\>{\tt (c}\={\tt ons}\  {\tt ...}\  {\tt 7}\  {\tt ...}
\\
\>\>\>{\tt (convert}\  {\tt empty))))}
\end{tabbing}\end{minipage}\end{center}
The first item of the result list should obviously be {{\tt 3}\/}, and it is
easy to construct this list. But, the second one should be {{\tt (+}\ {\tt 3}{ }{\tt 2)}\/}, yet the second instance of {{\tt rel-2-abs}\/} has no way of
``knowing'' that the first item of the {original\/} list is
{{\tt 3}\/}. The ``knowledge'' is lost.

Put differently, the problem is that recursive functions are independent of
their context. A function processes the list {{\tt L}\/} in {{\tt (cons}\ {\tt N}{ }{\tt L)}\/} in the exact same manner as {{\tt L}\/} in {{\tt (cons}\ {\tt K}\ {\tt L)}\/}. Indeed,
it would also process {{\tt L}\/} in that manner if it were given {{\tt L}\/}
by itself. While this property makes structurally recursive functions easy
to design, it also means that solutions are, on occasion, more complicated
than necessary, and this complication may affect the performance of the
function.

To make up for the loss of ``knowledge'', we equip the function with an
additional parameter: {{\tt accu-dist}\/}. The new parameter represents
the accumulated distance, which is the tally that we keep when we convert a
list of relative distances to a list of absolute distances. Its initial
value must be {{\tt 0}\/}. As the function processes the numbers on the list,
it must add them to the tally.

Here is the revised definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (rel-2-abs}\  {\tt alon}\  {\tt accu-dist)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  \={\tt (+}\  {\tt (first}\  {\tt alon)}\  {\tt accu-dist)}
\\
\>\>\>{\tt (rel-2-abs}\  {\tt (rest}\  {\tt alon)}\  {\tt (+}\  {\tt (first}\  {\tt alon)}\  {\tt accu-dist)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center} 
The recursive application consumes the rest of the list and the new
absolute distance of the current point to the origin. Although this means
that two arguments are changing simultaneously, the change in the second
one strictly depends on the first argument. The function is still a plain
list-processing procedure. 

Evaluating our running example with {{\tt rel-2-abs}\/} shows how much the
use of an accumulator simplifies the conversion process:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (rel-2-abs}\  {\tt (list}\  {\tt 3}\  {\tt 2}\  {\tt 7)}\  {\tt 0)}\\
{\tt =}\  {\tt (cons}\  {\tt 3}\  {\tt (rel-2-abs}\  {\tt (list}\  {\tt 2}\  {\tt 7)}\  {\tt 3))}
\\
{\tt =}\  {\tt (cons}\  {\tt 3}\  {\tt (cons}\  {\tt 5}\  {\tt (rel-2-abs}\  {\tt (list}\  {\tt 7)}\  {\tt 5)))}
\\
{\tt =}\  {\tt (cons}\  {\tt 3}\  {\tt (cons}\  {\tt 5}\  {\tt (cons}\  {\tt 12}\  {\tt (rel-2-abs}\  {\tt empty}\  {\tt 12))))}
\\
{\tt =}\  {\tt (cons}\  {\tt 3}\  {\tt (cons}\  {\tt 5}\  {\tt (cons}\  {\tt 12}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
Each item in the list is processed once. When {{\tt rel-2-abs}\/}
reaches the end of the argument list, the result is completely determined
and no further work is needed. In general, the function performs on the
order of {\it N\/} natural recursion steps for a list with {\it N\/}
items.

One minor problem with the new definition is that the function consumes two
arguments and is thus not equivalent to {{\tt relative-2-absolute}\/}, a
function of one argument. Worse, someone might accidentally misuse
{{\tt rel-2-abs}\/} by applying it to a list of numbers and a number that
isn't {{\tt 0}\/}. We can solve both problems with a function definition
that contains {{\tt rel-2-abs}\/} in a {{\tt local}\/} definition: see
figure~\ref{fig:rel-abs-human}. Now, {{\tt relative-2-absolute}\/} and
{{\tt relative-2-absolute2}\/} are indistinguishable.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt relative-2-absolute2}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt number)}\/}}\\
\hbox{;; to convert a list of relative distances to a list of absolute distances}
\\
\hbox{;; the first item on the list represents the distance to the origin}
\\
{\tt (d}\={\tt efine}\  {\tt (relative-2-absolute2}\  {\tt alon)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (rel-2-abs}\  {\tt alon}\  {\tt accu-dist)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt empty\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  \={\tt (+}\  {\tt (first}\  {\tt alon)}\  {\tt accu-dist)}
\\
\>\>\>\>\>{\tt (rel-2-abs}\  {\tt (rest}\  {\tt alon)}\  {\tt (+}\  {\tt (first}\  {\tt alon)}\  {\tt accu-dist)))\char'135}{\tt )))}
\\
\>\>{\tt (rel-2-abs}\  {\tt alon}\  {\tt 0)))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Converting relative distances with an accumulator}
\label{fig:rel-abs-human}
\hrule


\subsection{A Problem with Generative Recursion} \label{sec:loss:generative}

\htmladdimg{../icons/teacher.gif}~{\em Even though this section makes several references to the backtracking algorithm of section~\ref{sec:backtrack}, no knowledge of backtracking nor of the route finding algorithm is assumed. The section is self-contained with respect to generative recursion.}

Let us revisit the problem of finding a path in a graph from
section~\ref{sec:backtrack}. Recall that we are given a collection of nodes
and connections between nodes, and that we need to determine whether there
is a route from a node labeled {{\tt orig}\/} to one called
{{\tt dest}\/}. Here we study the slightly simpler version of the problem of
{\sc {simple graphs}} where each node has exactly one (one-directional)
connection to another node.

Consider the example in figure~\ref{fig:sim-graph}. There are six nodes:
{\sf A} through {\sf F}, and six connections. To get from {\sf A} to {\sf E}, we go through {\sf B}, {\sf C}, and {\sf E}. It is impossible, though,
to reach {\sf F} from {\sf A} or from any other node (besides {\sf F}
itself).


\begin{htmlonly}
\end{htmlonly}

\hrule
\bigskip\bigskip
\centerline{\hbox{\begin{rawhtml}<img src=../icons/sga.gif></img>\end{rawhtml}~\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt SimpleG}\  \\
\>{\tt \char'047}{\tt (}\={\tt (A}\  {\tt B)} \\
\>\>{\tt (B}\  {\tt C)} \\
\>\>{\tt (C}\  {\tt E)} \\
\>\>{\tt (D}\  {\tt E)} \\
\>\>{\tt (E}\  {\tt B)} \\
\>\>{\tt (F}\  {\tt F)))}\  \  \  \   \end{tabbing}\end{minipage}}}

\center{Figure: A simple graph}\label{fig:sim-graph}
\hrule

The right part of figure~\ref{fig:sim-graph} contains a Scheme definition
that represents the graph. Each node is represented by a list of two
symbols. The first symbol is the label of the node; the second one is the
reachable node. Here are the relevant data definitions: 

     \begin{quote} A {\sl {node}} is a symbol. \end{quote}

     \begin{quote} A {\sl {pair}} is a list of two {{\tt node}\/}s: \\
          \centerline{ {{\tt (cons}\ {\tt S}\ {\tt (cons}\ {\tt T}\ {\tt empty))}\/} }          where {{\tt S}\/}, {{\tt T}\/} are symbols. \end{quote}

     \begin{quote} A {\sl {simple-graph}} is a list of {{\tt pair}\/}s: \\
          \centerline{{{\tt (listof}\ {\tt pair)}\/}.} \end{quote}

\noindent They are straightforward translations of our informal
descriptions. 

Finding a route in a graph is a problem of generative recursion. We have
data definitions, we have (informal) examples, and the header material is 
standard: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt route-exists\char'077}\ {\tt :}\ {\tt node}\ {\tt node}\ {\tt simple-graph}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether there is a route from {{\tt orig}\/} to {{\tt dest}\/} in {{\tt sg}\/}}
\\
{\tt (define}\  {\tt (route-exists\char'077}\  {\tt orig}\  {\tt dest}\  {\tt sg)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
What we need are answers to the four basic questions of the recipe for
generative recursion: 
\begin{description}
\item[What is a trivially solvable problem?] The problem is trivial if
the nodes {{\tt orig}\/} and {{\tt dest}\/} are the same. 

\item[What is a corresponding solution?] Easy: {{\sf true}\/}. 

\item[How do we generate new problems?] If {{\tt orig}\/} is not the same as
{{\tt dest}\/}, there is only one thing we can do, namely, go to the node to
which {{\tt orig}\/} is connected and determine whether a route exists
between it and {{\tt dest}\/}.

\item[How do we relate the solutions?] There is no need to do anything
after we find the solution to the new problem. If {{\tt orig}\/}'s neighbor
is connected to {{\tt dest}\/}, then so is {{\tt orig}\/}. 
\end{description}
From here we just need to express these answers in Scheme, and we get an
algorithm. Figure~\ref{fig:route-exists1} contains the complete
function, including a function for looking up the neighbor of a node in a
simple graph. 

\hrule 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt route-exists\char'077}\ {\tt :}\ {\tt node}\ {\tt node}\ {\tt simple-graph}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether there is a route from {{\tt orig}\/} to {{\tt dest}\/} in {{\tt sg}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (route-exists\char'077}\  {\tt orig}\  {\tt dest}\  {\tt sg)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt orig}\  {\tt dest)}\  {\sf true}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (route-exists\char'077}\  {\tt (neighbor}\  {\tt orig}\  {\tt sg)}\  {\tt dest}\  {\tt sg)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt neighbor}\ {\tt :}\ {\tt node}\ {\tt simple-graph}\ {}{\tt ->}{}\ {\tt node}\/}}\\
\hbox{;; to determine the node that is connected to {{\tt a-node}\/} in {{\tt sg}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (neighbor}\  {\tt a-node}\  {\tt sg)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt sg)}\  {\tt (error}\  {\tt ``neighbor:}\  {\tt impossible'')\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (first}\  {\tt (first}\  {\tt sg))}\  {\tt a-node)}
\\
\>\>\>\>{\tt (second}\  {\tt (first}\  {\tt sg))\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (neighbor}\  {\tt a-node}\  {\tt (rest}\  {\tt ag))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Finding a route in a simple graph (version 1)} 
\label{fig:route-exists1}
\hrule 

Even a casual look at the function suggests that we have a
problem. Although the function is supposed to produce {{\sf false}\/} if there
is no route from {{\tt orig}\/} to {{\tt dest}\/}, the function definition
doesn't contain {{\sf false}\/} anywhere. Conversely, we need to ask what the
function actually does when there is no route between two nodes.

Take another look at figure~\ref{fig:sim-graph}. In this simple graph there
is no route from {\sf C} to {\sf D}. The connection that leaves {\sf C}
passes right by {\sf D} and instead goes to {\sf E}. So let's look at how
{{\tt route-exists\char'077}\/} deals with the inputs {{\tt \char'047}{\tt C}\/} and {{\tt \char'047}{\tt D}\/}
for {{\tt SimpleG}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (route-exists\char'077}\  {\tt \char'047}{\tt C}\  {\tt \char'047}{\tt D}\  {\tt \char'047}{\tt ((A}\  {\tt B)}\  {\tt (B}\  {\tt C)}\  {\tt (C}\  {\tt E)}\  {\tt (D}\  {\tt E)}\  {\tt (E}\  {\tt B)}\  {\tt (F}\  {\tt F)))}\\
{\tt =}\  {\tt (route-exists\char'077}\  {\tt \char'047}{\tt E}\  {\tt \char'047}{\tt D}\  {\tt \char'047}{\tt ((A}\  {\tt B)}\  {\tt (B}\  {\tt C)}\  {\tt (C}\  {\tt E)}\  {\tt (D}\  {\tt E)}\  {\tt (E}\  {\tt B)}\  {\tt (F}\  {\tt F)))}
\\
{\tt =}\  {\tt (route-exists\char'077}\  {\tt \char'047}{\tt B}\  {\tt \char'047}{\tt D}\  {\tt \char'047}{\tt ((A}\  {\tt B)}\  {\tt (B}\  {\tt C)}\  {\tt (C}\  {\tt E)}\  {\tt (D}\  {\tt E)}\  {\tt (E}\  {\tt B)}\  {\tt (F}\  {\tt F)))}
\\
{\tt =}\  {\tt (route-exists\char'077}\  {\tt \char'047}{\tt C}\  {\tt \char'047}{\tt D}\  {\tt \char'047}{\tt ((A}\  {\tt B)}\  {\tt (B}\  {\tt C)}\  {\tt (C}\  {\tt E)}\  {\tt (D}\  {\tt E)}\  {\tt (E}\  {\tt B)}\  {\tt (F}\  {\tt F)))}
\\
{\tt =}\  {\tt ...}\  
\end{tabbing}\end{minipage}\end{center}
The hand-evaluation confirms that as the function recurs, it calls itself
{with the exact same arguments again and again.\/} In other words, the
evaluation never stops. 

Our problem with {{\tt route-exists\char'077}\/} is again a loss of ``knowledge,''
similar to that of {{\tt relative-2-absolute}\/} in the preceding section.
Like {{\tt relative-2-absolute}\/}, {{\tt route-exists\char'077}\/}  was developed
according to the recipe and is independent of its context. That is, it
doesn't ``know'' whether some application is the first one or the 100th of
a long recursive chain. In the case of {{\tt route-exists\char'077}\/}  this means,
in particular, that the function doesn't ``know'' whether a previous
application in the current chain of recursions received the exact same
arguments.

The solution follows the pattern of the preceding section. We add a
parameter, which we call {{\tt accu-seen}\/} and which represents the
accumulated list of origination nodes that the function has encountered,
starting with the original application. Its initial value must be
{{\tt empty}\/}. As the function checks on a specific {{\tt orig}\/} and
moves to its neighbors, {{\tt orig}\/} is added to {{\tt accu-seen}\/}.

Here is a first revision of {{\tt route-exists\char'077}\/}, dubbed
{{\tt route-exists-accu\char'077}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt route-exists-accu\char'077}\ {\tt :}\ {\tt node}\ {\tt node}\ {\tt simple-graph}\ {\tt (listof}\ {\tt node)}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether there is a route from {{\tt orig}\/} to {{\tt dest}\/} in {{\tt sg}\/}, }
\\
\hbox{;; assuming the nodes in {{\tt accu-seen}\/}}
\\
\hbox{;; have already been inspected and failed to deliver a solution }
\\
{\tt (d}\={\tt efine}\  {\tt (route-exists-accu\char'077}\  {\tt orig}\  {\tt dest}\  {\tt sg}\  {\tt accu-seen)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt orig}\  {\tt dest)}\  {\sf true}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (route-exists-accu\char'077}\  \={\tt (neighbor}\  {\tt orig}\  {\tt sg)}\  {\tt dest}\  {\tt sg}
\\
\>\>\>{\tt (cons}\  {\tt orig}\  {\tt accu-seen))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The addition of the new parameter alone does not solve our problem, but, as
the following hand-evaluation shows, provides the foundation for one: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (route-exists-accu\char'077}\  {\tt \char'047}{\tt C}\  {\tt \char'047}{\tt D}\  {\tt \char'047}{\tt ((A}\  {\tt B)}\  {\tt (B}\  {\tt C)}\  {\tt (C}\  {\tt E)}\  {\tt (D}\  {\tt E)}\  {\tt (E}\  {\tt B)}\  {\tt (F}\  {\tt F))}\  {\tt empty)}\\
{\tt =}\  {\tt (route-exists-accu\char'077}\  {\tt \char'047}{\tt E}\  {\tt \char'047}{\tt D}\  {\tt \char'047}{\tt ((A}\  {\tt B)}\  {\tt (B}\  {\tt C)}\  {\tt (C}\  {\tt E)}\  {\tt (D}\  {\tt E)}\  {\tt (E}\  {\tt B)}\  {\tt (F}\  {\tt F))}\  {\tt \char'047}{\tt (C))}
\\
{\tt =}\  {\tt (route-exists-accu\char'077}\  {\tt \char'047}{\tt B}\  {\tt \char'047}{\tt D}\  {\tt \char'047}{\tt ((A}\  {\tt B)}\  {\tt (B}\  {\tt C)}\  {\tt (C}\  {\tt E)}\  {\tt (D}\  {\tt E)}\  {\tt (E}\  {\tt B)}\  {\tt (F}\  {\tt F))}\  {\tt \char'047}{\tt (E}\  {\tt C))}
\\
{\tt =}\  {\tt (route-exists-accu\char'077}\  \={\tt \char'047}{\tt C}\  {\tt \char'047}{\tt D}\  {\tt \char'047}{\tt ((A}\  {\tt B)}\  {\tt (B}\  {\tt C)}\  {\tt (C}\  {\tt E)}\  {\tt (D}\  {\tt E)}\  {\tt (E}\  {\tt B)}\  {\tt (F}\  {\tt F))}
\\
\>{\tt \char'047}{\tt (B}\  {\tt E}\  {\tt C))}
\end{tabbing}\end{minipage}\end{center}
In contrast to the original function, the revised function no longer calls
itself with the exact same arguments. While the three arguments proper are
again the same for the third recursive application, the accumulator
argument is different from that of the first application. Instead of
{{\tt empty}\/}, it is now {{\tt \char'047}{\tt (B}\ {\tt E}\ {\tt C)}\/}. The new value represents the
fact that during the search of a route from {{\tt \char'047}{\tt C}\/} to {{\tt \char'047}{\tt D}\/}, the
function has inspected {{\tt \char'047}{\tt B}\/}, {{\tt \char'047}{\tt E}\/}, and {{\tt \char'047}{\tt C}\/} as starting
points.

All we need to do at this point, is exploit the accumulated knowledge in
the function definition. Specifically, we determine whether the given
{{\tt orig}\/} is already an item on {{\tt accu-seen}\/}. If so, the problem
is trivially solvable with {{\sf false}\/}. Figure~\ref{fig:route-exists2}
contains the definition of {{\tt route-exists2\char'077}\/}, which is the revision
of {{\tt route-exists\char'077}\/}. The definition refers to {{\tt contains}\/}, our
first recursive function (see part~\ref{part:structural}), which determines
whether a specific symbol is on a list of symbols.

\hrule 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt route-exists2\char'077}\ {\tt :}\ {\tt node}\ {\tt node}\ {\tt simple-graph}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether there is a route from {{\tt orig}\/} to {{\tt dest}\/} in {{\tt sg}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (route-exists2\char'077}\  {\tt orig}\  {\tt dest}\  {\tt sg)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (re-accu\char'077}\  {\tt orig}\  {\tt dest}\  {\tt sg}\  {\tt accu-seen)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt orig}\  {\tt dest)}\  {\sf true}{\tt \char'135}
\\
\>\>\>\>{\tt \char'133}{\tt (contains}\  {\tt orig}\  {\tt accu-seen)}\  {\sf false}{\tt \char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (re-accu\char'077}\  {\tt (neighbor}\  {\tt orig}\  {\tt sg)}\  {\tt dest}\  {\tt sg}\  {\tt (cons}\  {\tt orig}\  {\tt accu-seen))\char'135}{\tt )))}\  
\\
\>\>{\tt (re-accu\char'077}\  {\tt orig}\  {\tt dest}\  {\tt sg}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Finding a route in a simple graph (version~2)} 
\label{fig:route-exists2}
\hrule 

The definition of {{\tt route-exists2\char'077}\/} also eliminates the two minor
problems with the first revision. By {{\tt local}\/}izing the definition of
the accumulating function, we can ensure that the first call to
{{\tt re-accu\char'077}\/}  always uses {{\tt empty}\/} as the initial value for
{{\tt accu-seen}\/}. And, {{\tt route-exists2\char'077}\/} satisfies the exact same
contract and purpose statement as {{\tt route-exists\char'077}\/}.

Still, there is a significant difference between {{\tt route-exists2\char'077}\/}
and {{\tt relative-to-absolute2}\/}. Whereas the latter was equivalent to
the original function, {{\tt route-exists2\char'077}\/} is an improvement over the
{{\tt route-exists\char'077}\/} function. After all, it corrects a fundamental flaw
in {{\tt route-exists\char'077}\/}, which completely failed to find an answer for
some inputs.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 30.2.1}

 \label{ex:route-exists}
Complete the definition in figure~\ref{fig:route-exists2} and test it with
the running example. Use the strategy of section~\ref{sec:equal-test} to 
formulate the tests as boolean-valued expressions.

Check with a hand-evaluation that this function computes the proper result
for {{\tt \char'047}{\tt A}\/}, {{\tt \char'047}{\tt C}\/}, and
{{\tt SimpleG}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/route-exists.html}{\rule{3pt}{5pt}}


{\bf Exercise 30.2.2}

 \label{ex:find-route-edit}
Edit the function in figure~\ref{fig:route-exists2} so that the locally
defined function consumes only those arguments that change during an
evaluation.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-route-edit.html}{\rule{3pt}{5pt}}


{\bf Exercise 30.2.3}

 \label{ex:find-route-vector}
Develop a vector-based representation of simple graphs.  Adapt the function
in figure~\ref{fig:route-exists2} so that it works on a vector-based
representation of simple graphs.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-route-vector.html}{\rule{3pt}{5pt}}


{\bf Exercise 30.2.4}

 \label{ex:find-route-comp}
Modify the definitions of {{\tt find-route}\/} and {{\tt find-route/list}\/}
in figure~\ref{fig:find-route-code} so that they produce {{\sf false}\/}, even
if they encounter the same starting point
twice.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/find-route-comp.html}{\rule{3pt}{5pt}}


\section{Designing Accumulator-Style Functions}

Section~\ref{sec:loss} illustrated with two examples the need for
accumulating extra knowledge. In some cases, the accumulation makes it
easier to understand a function, in others it is necessary for the function
to work properly. In both cases, however, we first chose one of the
available design recipes, inspected the function, and then revised or fixed
it. Put more generally, adding an {\sc {accumulator}}, that is, a
parameter that accumulates knowledge, is something that we add to a function
after we have designed a function, not before.

The keys to the development of an accumulator-style function are: 
\begin{enumerate}
\item to recognize that the function benefits from, or needs, an accumulator; 

\item to understand what the accumulator represents.
\end{enumerate}
The first two subsections address these two questions. Because the second
one is a difficult topic, the third subsection illustrates how to formulate
precise claims about accumulators. More concretely, in this section, we
transform several standard recursive functions into functions that use
auxiliary functions with accumulators. 

\subsection{Recognizing the Need for Accumulator}

Recognizing the need for accumulators is not an easy task. We have seen two
reasons, and they are the most prevalent reasons for adding accumulator
parameters. In either case, it is critical that we first built a complete
function {\em based on a design recipe\/}. Then we study the function and
look for one of the following characteristics: 
\begin{enumerate}
\item If the function is structurally recursive and if the result of a
recursive application is processed by an auxiliary, recursive function, 
then we should consider the use of an accumulator parameter. 

Take the function {{\tt reverse}\/} for an example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reverse}\ {\tt :}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/}}\\
\hbox{;; to construct the reverse of {{\tt alox}\/}}
\\
\hbox{;; structural recursion }
\\
{\tt (d}\={\tt efine}\  {\tt (reverse}\  {\tt alox)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alox)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (make-last-item}\  {\tt (reverse}\  {\tt (rest}\  {\tt alox))}\  {\tt (first}\  {\tt alox))\char'135}{\tt ))}
\\
\hbox{;; {{\tt make-last-item}\ {\tt :}\ {\tt X}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/}}
\\
\hbox{;; to add {{\tt an-x}\/} to the end of {{\tt alox}\/}}
\\
\hbox{;; structural recursion }
\\
{\tt (d}\={\tt efine}\  {\tt (make-last-item}\  {\tt an-x}\  {\tt alox)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alox)}\  {\tt (list}\  {\tt an-x)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt (first}\  {\tt alox)}\  {\tt (make-last-item}\  {\tt an-x}\  {\tt (rest}\  {\tt alox)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The result of the recursive application produces the reverse of the rest of
the list. It is processed by {{\tt make-last-item}\/}, which adds the first
item to the reverse of the rest and thus creates the reverse of the entire
list.  This second, auxiliary function is also recursive. We have thus
identified a potential candidate. It is now time to study some
hand-evaluations, as we did in section~\ref{sec:loss:structural}, to see
whether an accumulator helps.

\item If we are dealing with a function based on generative recursion, we
are faced with a much more difficult task. Our goal must be to understand
whether the algorithm can fail to produce a result for inputs for which we
expect a result. If so, adding a parameter that accumulates knowledge may
help. Because these situations are complex, we postpone the discussion of
an example until section~\ref{sec:more-accu-mc}. 
\end{enumerate}
These two situations are by no means the only ones; they are just the most
common ones. To sharpen our perception, we will discuss an additional array
of possibilities in the following section. 

\subsection{Accumulator-Style Functions}

When we have decided that an accumulator-style function is necessary, we
introduce it in two steps: 
\begin{description}
\item[Setting-up an accumulator:] First, we must understand what knowledge
the accumulator needs to remember about the parameters proper and then how
it is to remember it. For example, for the conversion of relative distances
to absolute distances, it suffices to accumulate the total distance
encountered so far. For the routing problem, we needed the accumulator to
remember every node inspected so far. Thus the first accumulator was just a
number, the second one a list of nodes.

The best way to discuss the accumulation process is to introduce a template
of the accumulator-style function via a {{\tt local}\/} definition and to
name the parameters of the function proper differently from those of the 
auxiliary function. 

Let's take a look at the {{\tt reverse}\/} example:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reverse}\ {\tt :}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/}}\\
\hbox{;; to construct the reverse of {{\tt alox}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (reverse}\  {\tt alox0)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} \ldots}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (rev}\  {\tt alox}\  {\tt accumulator)}\  \  \  \  \  \  \  \  
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alox)}\  {\tt ...\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}\  
\\
\>\>\>\>\>\>{\tt ...}\  {\tt (rev}\  {\tt (rest}\  {\tt alox)}\  \fbox{\ldots ({\sf first} {\it alox\/}) \ldots {\it accumulator\/}}{\tt )}
\\
\>\>\>\>\>\>{\tt ...}\  {\tt \char'135}{\tt )))}
\\
\>\>{\tt (rev}\  {\tt alox0}\  {\tt ...)))}
\end{tabbing}\end{minipage}\end{center}
Here we have a definition of {{\tt reverse}\/} with an auxiliary function
{{\tt rev}\/} in template form. This auxiliary template has one parameter in
addition to those of {{\tt reverse}\/}: the accumulating parameter. The box
in the recursive application indicates that we need an expression that
maintains the accumulation process and that this process depends on the
current value of {{\tt accumulator}\/} and {{\tt (first}\ {\tt alox)}\/}, the value
{{\tt rev}\/} is about to forget.

Clearly, {{\tt reverse}\/} cannot forget anything, because it only reverses
the order of items on the list. Hence, we might just wish to accumulate all
items that {{\tt rev}\/} encounters. This means 
\begin{enumerate}
\item that {{\tt accumulator}\/} stands for a list, and 
\item that it stands for all those items in {{\tt alox0}\/} that precede the
{{\tt alox}\/} argument of {{\tt rev}\/}. 
\end{enumerate}
For the second part of the analysis, it is critical that we can distinguish 
the original argument, {{\tt alox0}\/}, from the current one, {{\tt alox}\/}. 

Now that we know the rough purpose of the accumulator, let's consider what
the first value should be and what we should do for the recursion. When we
apply {{\tt rev}\/} in the body of the {{\bf local}-exp\-res\-sion}, it
receives {{\tt alox0}\/}, which means that it hasn't encountered any of its
items. The initial value for {{\tt accumulator}\/} is {{\tt empty}\/}.
When {{\tt rev}\/} recurs, it has just encountered one extra item:
{{\tt (first}\ {\tt alox)}\/}. To remember it, we can {{\tt cons}\/} it onto the
current value of accumulator.

Here is the enhanced definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reverse}\ {\tt :}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/}}\\
\hbox{;; to construct the reverse of {{\tt alox}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (reverse}\  {\tt alox0)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} is the reversed list of all those items}
\\
\>\>\>\hbox{;; on {{\tt alox0}\/} that precede {{\tt alox}\/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (rev}\  {\tt alox}\  {\tt accumulator)}\  \  \  \  \  \  \  \  
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alox)}\  {\tt ...\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>\>\>{\tt ...}\  {\tt (rev}\  {\tt (rest}\  {\tt alox)}\  {\tt (cons}\  {\tt (first}\  {\tt alox)}\  {\tt accumulator))}
\\
\>\>\>\>\>\>{\tt ...\char'135}{\tt )))}
\\
\>\>{\tt (rev}\  {\tt alox0}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
A close inspection reveals that {{\tt accumulator}\/} is not just the items
on {{\tt alox0}\/} that precede but a list of these items in reverse order. 

\item[Exploiting an accumulator:] Once we have decided on what knowledge the
accumulator maintains and how it is maintained, we can move to the question
of how to exploit this knowledge for our function. 

In the case of {{\tt reverse}\/}, the answer is almost obvious. If
{{\tt accumulator}\/} is the list of all items on {{\tt alox0}\/} that
precede {{\tt alox}\/} in reverse order, then, if {{\tt alox}\/} is empty,
{{\tt accumulator}\/} stands for the reverse of {{\tt alox0}\/}. Put
differently: if {{\tt alox}\/} is {{\tt empty}\/}, {{\tt rev}\/}'s answer is
{{\tt accumulator}\/}, and that is the answer we want in both cases:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reverse}\ {\tt :}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt X)}\/}}\\
\hbox{;; to construct the reverse of {{\tt alox}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (reverse}\  {\tt alox0)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} is the reversed list of all those items}
\\
\>\>\>\hbox{;; on {{\tt alox0}\/} that precede {{\tt alox}\/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (rev}\  {\tt alox}\  {\tt accumulator)}\  \  \  \  \  \  \  \  
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alox)}\  {\tt accumulator\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>\>\>{\tt (rev}\  {\tt (rest}\  {\tt alox)}\  {\tt (cons}\  {\tt (first}\  {\tt alox)}\  {\tt accumulator))\char'135}{\tt )))}
\\
\>\>{\tt (rev}\  {\tt alox0}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}

\end{description}

The key step of this development was the precise description of the role of
{{\tt accumulator}\/}. In general, an {\sc {accumulator invariant}}
describes a relationship between the argument proper of the function, the
current argument of the auxiliary function, and the accumulator that must
always hold when an accumulator-style function is used.

\subsection{Transforming Functions into Accumulator-Style}

The most complex part of the design recipe is the requirement to formulate
an accumulator invariant.  Without that we cannot produce functioning
accumulator-style functions. Because formulating invariants is clearly an
art that deserves a lot of practice, we practice it in this section with
three small, well-understood structural functions that do not need an
accumulator. The section concludes with a group of exercises concerning
this step. 

For the first example, consider the function {{\tt sum}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sum}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the sum of the numbers on {{\tt alon}\/}}
\\
\hbox{;; structural recursion }
\\
{\tt (d}\={\tt efine}\  {\tt (sum}\  {\tt alon)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (first}\  {\tt alon)}\  {\tt (sum}\  {\tt (rest}\  {\tt alon)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Here is the first step toward an accumulator version: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sum}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the sum of the numbers on {{\tt alon0}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (sum}\  {\tt alon0)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} \ldots }
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (sum-a}\  {\tt alon}\  {\tt accumulator)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt ...\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>\>\>{\tt ...}\  {\tt (sum-a}\  {\tt (rest}\  {\tt alon)}\  \fbox{\ldots ({\sf first} {\it alon\/}) \ldots {\it accumulator\/}}{\tt )}
\\
\>\>\>\>\>\>{\tt ...}\  {\tt \char'135}{\tt )))}
\\
\>\>{\tt (sum-a}\  {\tt alon0}\  {\tt ...)))}
\end{tabbing}\end{minipage}\end{center}
As suggested by our first step, we have put the template for {{\tt sum-a}\/}
into a {{\tt local}\/} definition, added an accumulator parameter, and
renamed {{\tt sum}\/}'s parameter.

Our goal is to develop an accumulator invariant for {{\tt sum}\/}. To do so,
we must consider how {{\tt sum}\/} proceeds and what the goal of the process
is. Like {{\tt rev}\/}, {{\tt sum-a}\/} processes the numbers on the list
one by one. The goal is to add up these numbers. This suggests that 
{{\tt accumulator}\/} represent the sum of the numbers {seen so far\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt ..}\={\tt .}\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} is the sum of the numbers that preceded}
\\
\>\>\>\hbox{;; those in {{\tt alon}\/} on {{\tt alon0}\/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (sum-a}\  {\tt alon}\  {\tt accumulator)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt ...\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>\>\>{\tt ...}\  {\tt (sum-a}\  {\tt (rest}\  {\tt alon)}\  {\tt (+}\  {\tt (first}\  {\tt alon)}\  {\tt accumulator))}
\\
\>\>\>\>\>\>{\tt ...}\  {\tt \char'135}{\tt )))}
\\
\>\>{\tt (sum-a}\  {\tt alon0}\  {\tt 0)))}
\end{tabbing}\end{minipage}\end{center}
When we apply {{\tt sum-a}\/} we must use {{\tt 0}\/} as the value of
{{\tt accumulator}\/}, because it hasn't processed any of the numbers on
{{\tt alon}\/} yet. For the second clause, we must add {{\tt (first}\ {\tt alon)}\/}
to {{\tt accumulator}\/} so that the invariant holds again for the function
application. 

Given a precise invariant, the rest is straightforward again.  If
{{\tt alon}\/} is {{\tt empty}\/}, {{\tt sum-a}\/} returns
{{\tt accumulator}\/} because it represents the sum of all numbers on
{{\tt alon}\/} now. Figure~\ref{fig:accumulator-style} contains the final
definition of the accumulator-style version of {{\tt sum}\/}. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sum}\ {\tt :}\ {\tt (listof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to compute the sum of the numbers on {{\tt alon0}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (sum}\  {\tt alon0)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} is the sum of the numbers that preceded}
\\
\>\>\>\hbox{;; those in {{\tt alon}\/} on {{\tt alon0}\/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (sum-a}\  {\tt alon}\  {\tt accumulator)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt alon)}\  {\tt accumulator\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (sum-a}\  {\tt (rest}\  {\tt alon)}\  {\tt (+}\  {\tt (first}\  {\tt alon)}\  {\tt accumulator))\char'135}{\tt )))}
\\
\>\>{\tt (sum-a}\  {\tt alon0}\  {\tt 0)))}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt !}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to compute $n \cdot (n-1) \cdot \ldots \cdot 2 \cdot 1$}
\\
{\tt (d}\={\tt efine}\  {\tt (!}\  {\tt n0)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} is the product of all natural numbers in [{{\tt n0}\/}, {{\tt n}\/})}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (!-a}\  {\tt n}\  {\tt accumulator)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt accumulator\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (!-a}\  {\tt (sub1}\  {\tt n)}\  {\tt (*}\  {\tt n}\  {\tt accumulator))\char'135}{\tt )))}
\\
\>\>{\tt (!-a}\  {\tt n0}\  {\tt 1)))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Some simple accumulator-style functions} \label{fig:accumulator-style}
\hrule


Let's compare how the original definition of {{\tt sum}\/} and the
accumulator-style definition produce an answer for the same input: 
\begin{center}\small
\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (sum}\  {\tt (list}\  {\tt 10.23}\  {\tt 4.50}\  {\tt 5.27))}\\
{\tt =}\  {\tt (+}\  {\tt 10.23}\  {\tt (sum}\  {\tt (list}\  {\tt 4.50}\  {\tt 5.27)))}
\\
{\tt =}\  {\tt (+}\  {\tt 10.23}\  {\tt (+}\  {\tt 4.50}\  {\tt (sum}\  {\tt (list}\  {\tt 5.27))))}
\\
{\tt =}\  {\tt (+}\  {\tt 10.23}\  {\tt (+}\  {\tt 4.50}\  {\tt (+}\  {\tt 5.27}\  {\tt (sum}\  {\tt empty))))}
\\
{\tt =}\  {\tt (+}\  {\tt 10.23}\  {\tt (+}\  {\tt 4.50}\  {\tt (+}\  {\tt 5.27}\  {\tt 0)))}
\\
{\tt =}\  {\tt (+}\  {\tt 10.23}\  {\tt (+}\  {\tt 4.50}\  {\tt 5.27))}
\\
{\tt =}\  {\tt (+}\  {\tt 10.23}\  {\tt 9.77)}
\\
{\tt =}\  {\tt 20.0}
\end{tabbing}\end{minipage} \qquad \begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (sum}\  {\tt (list}\  {\tt 10.23}\  {\tt 4.50}\  {\tt 5.27))}\\
{\tt =}\  {\tt (sum-a}\  {\tt (list}\  {\tt 10.23}\  {\tt 4.50}\  {\tt 5.27)}\  {\tt 0)}
\\
{\tt =}\  {\tt (sum-a}\  {\tt (list}\  {\tt 4.50}\  {\tt 5.27)}\  {\tt 10.23)}
\\
{\tt =}\  {\tt (sum-a}\  {\tt (list}\  {\tt 5.27)}\  {\tt 14.73)}
\\
{\tt =}\  {\tt (sum-a}\  {\tt empty}\  {\tt 20.0)}
\\
{\tt =}\  {\tt 20.0}
\\
\relax
\\
\relax
\end{tabbing}\end{minipage}
\end{center}
On the left side, we see how the plain recursive function descends the list
of numbers all the way to the end and sets up addition operations on the
way. On the right side, we see how the accumulator-style version adds up
the numbers as it goes. Furthermore, we see that for each application of
{{\tt sum-a}\/} the invariant holds with respect to the application of
{{\tt sum}\/}. When {{\tt sum-a}\/} is finally applied to {{\tt empty}\/}, the
accumulator is the final result, and {{\tt sum-a}\/} returns it.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 31.3.1}

 \label{ex:sum-accu-inex}
A second difference between the two functions concerns the order of
addition. While the original version of {{\tt sum}\/} adds up the numbers
from left to right, the accumulator-style version adds them up from right
to left. For exact numbers, this difference has no effect on the final
result. For inexact numbers, the difference is significant.

Consider the following definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (g-series}\  {\tt n)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (cons}\  {\tt (expt}\  {\tt -0.99}\  {\tt n)}\  {\tt (g-series}\  {\tt (sub1}\  {\tt n)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Applying {{\tt g-series}\/} to a natural number produces the beginning of a
decreasing geometric series (see section~\ref{sec:seqs-func}). 

Depending on which function we use to sum up the items of this list, we
get vastly different results. Evaluate the expression
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (sum}\  {\tt (g-series}\  {\tt 1000))}\end{tabbing}\end{minipage}\end{center}
with both the original version of {{\tt sum}\/} as well as its
accumulator-style version. Then evaluate 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (*}\  {\tt 10e15}\  {\tt (sum}\  {\tt (g-series}\  {\tt 1000)))}\end{tabbing}\end{minipage}\end{center}
which proves that, depending on the context, the difference can be
arbitrarily large.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sum-accu-inex.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


For the second example, we return to the factorial function from
part~\ref{part:structural}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt !}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to compute $n \cdot (n-1) \cdot \ldots \cdot 2 \cdot 1$}
\\
\hbox{;; structural recursion }
\\
{\tt (d}\={\tt efine}\  {\tt (!}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt 1\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (*}\  {\tt n}\  {\tt (!}\  {\tt (sub1}\  {\tt n)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
While {{\tt relative-2-absolute}\/} and {{\tt reverse}\/} processed lists,
the factorial function works on natural numbers. Its template is that for
{{\bf N}\/}-processing functions.

We proceed as before by creating a {{\tt local}\/} definition of {{\tt !}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt !}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to compute $n \cdot (n-1) \cdot \ldots \cdot 2 \cdot 1$}
\\
{\tt (d}\={\tt efine}\  {\tt (!}\  {\tt n0)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} \ldots }
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (!-a}\  {\tt n}\  {\tt accumulator)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt ...\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}\  
\\
\>\>\>\>\>\>{\tt ...}\  {\tt (!-a}\  {\tt (sub1}\  {\tt n)}\  \fbox{\ldots n \ldots {\it accumulator\/}}{\tt )}\  {\tt ...\char'135}{\tt )))}
\\
\>\>{\tt (!-a}\  {\tt n0}\  {\tt ...)))}
\end{tabbing}\end{minipage}\end{center}
This sketch suggests that if {{\tt !}\/} is applied to the natural number
$n$, {{\tt !-a}\/} processes $n$, then $n-1$, $n-2$, and so on until it
reaches {{\tt 0}\/}. Since the goal is to multiply these numbers, the
accumulator should be the product of all those numbers that {{\tt !-a}\/}
has encountered: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt ..}\={\tt .}\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} is the product of all natural numbers between}
\\
\>\>\>\hbox{;; {{\tt n0}\/} (inclusive) and {{\tt n}\/} (exclusive)}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (!-a}\  {\tt n}\  {\tt accumulator)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt ...\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}\  
\\
\>\>\>\>\>\>{\tt ...}\  {\tt (!-a}\  {\tt (sub1}\  {\tt n)}\  {\tt (*}\  {\tt n}\  {\tt accumulator))}\  {\tt ...\char'135}{\tt )))}
\\
\>\>{\tt (!-a}\  {\tt n0}\  {\tt 1)))}
\end{tabbing}\end{minipage}\end{center}
To make the invariant true at the beginning, we must use {{\tt 1}\/} for the
accumulator. When {{\tt !-a}\/} recurs, we must multiply the current value
of the accumulator with {{\tt n}\/} to re-establish the invariant.

From the purpose statement for the accumulator of {{\tt !-a}\/}, we can see
that if {{\tt n}\/} is {{\tt 0}\/}, the accumulator is the product of
{{\tt n}\/}, \ldots, {{\tt 1}\/}. That is, it is the desired result. So, 
like {{\tt sum}\/}, {{\tt !-a}\/} returns {{\tt accumulator}\/} in the first
case and simply recurs in the second
one. Figure~\ref{fig:accumulator-style} contains the complete definition. 

It is instructive to compare hand-evaluations for the two versions of
{{\tt !}\/}: 
\begin{center}\small
\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (!}\  {\tt 3)}\\
{\tt =}\  {\tt (*}\  {\tt 3}\  {\tt (!}\  {\tt 2))}
\\
{\tt =}\  {\tt (*}\  {\tt 3}\  {\tt (*}\  {\tt 2}\  {\tt (!}\  {\tt 1)))}
\\
{\tt =}\  {\tt (*}\  {\tt 3}\  {\tt (*}\  {\tt 2}\  {\tt (*}\  {\tt 1}\  {\tt (!}\  {\tt 0))))}
\\
{\tt =}\  {\tt (*}\  {\tt 3}\  {\tt (*}\  {\tt 2}\  {\tt (*}\  {\tt 1}\  {\tt 1)))}
\\
{\tt =}\  {\tt (*}\  {\tt 3}\  {\tt (*}\  {\tt 2}\  {\tt 1))}
\\
{\tt =}\  {\tt (*}\  {\tt 3}\  {\tt 2)}
\\
{\tt =}\  {\tt 6}
\end{tabbing}\end{minipage} \qquad\qquad \begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (!}\  {\tt 3)}\\
{\tt =}\  {\tt (!-a}\  {\tt 3}\  {\tt 1)}
\\
{\tt =}\  {\tt (!-a}\  {\tt 2}\  {\tt 3)}
\\
{\tt =}\  {\tt (!-a}\  {\tt 1}\  {\tt 6)}
\\
{\tt =}\  {\tt (!-a}\  {\tt 0}\  {\tt 6)}
\\
{\tt =}\  {\tt 6}
\\
\relax
\\
\relax
\end{tabbing}\end{minipage}
\end{center}
The left column shows how the original version works, the right one how the
accumulator-style function proceeds. Both traverse the natural number until
they reach {{\tt 0}\/}, but while the original version only schedules
multiplications, the new one multiplies the numbers as they are processed.
In addition, the right column illustrates how the new factorial function
maintains the accumulator invariant. For each application, the accumulator
is the product of {{\tt 3}\/} to {{\tt n}\/} where {{\tt n}\/} is the first
argument to {{\tt !-a}\/}. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 31.3.2}

 \label{ex:time-factorial}
Like {{\tt sum}\/}, {{\tt !}\/} performs the primitive computation steps
(multiplication) in reverse order. Surprisingly, this affects the
performance of the function in a negative manner. 

Use DrScheme's {{\tt time}\/}-facility to determine how long the two
variants need to evaluate {{\tt (!}\ {\tt 20)}\/} 1000 times.

\noindent{\bf Hint:} \ (1) Develop the function
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt many}\ {\tt :}\ {\bf N}\ {\tt (}{\bf N}\ {}{\tt ->}{}\ {\bf N}{\tt )}\ {}{\tt ->}{}\ {\tt true}\/}}\\
\hbox{;; to evaluate {{\tt (f}\ {\tt 20)}\/} {{\tt n}\/} times }
\\
{\tt (define}\  {\tt (many}\  {\tt n}\  {\tt f)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

\noindent (2) Evaluating {{\tt (time}\ {\tt an-expression)}\/} determines how much
time the evaluation of {{\tt an-expression}\/} takes.\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/time-factorial.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


For the last example, we study a function on simplified binary trees. The
example illustrates that accumulator-style programming is not just for data
definitions with a single self-reference. Indeed, it is as common for
complicated data definitions as it is for lists and natural numbers.

Here is the structure definition for a stripped-down version of binary trees:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt node}\  {\tt (left}\  {\tt right))}\end{tabbing}\end{minipage}\end{center}
and its corresponding data definition: 

   \begin{quote} A {\sl {binary tree}} (short: tree) is either  	\begin{enumerate}         \item {{\tt empty}\/}         \item {{\tt (make-node}\ {\tt tl}\ {\tt tr)}\/} where {{\tt tl}\/}, {{\tt tr}\/} are {{\tt trees}\/}. 	\end{enumerate} \end{quote}

\noindent These trees contain no information, and all of them end in
{{\tt empty}\/}. Still, there are many different trees as
figure~\ref{fig:sim-bt-ex} shows. The table indicates how to think of each
tree as a graphical element, that is, of {{\tt empty}\/} as a plain dot and
{{\tt make-node}\/} as a dot that combines two trees.

\hrule
\begin{center}
\def\btnode(#1,#2)#3{
\put(#1,#2){\circle*{3}}
\put(#1,#2){\line#3{25}}}
\begin{tabular}{|l|c|}\hline
{{\tt empty}\/} &
\begin{picture}(100,10)
\put(0,0){\circle*{3}}
\end{picture}
\\
 \hline
{{\tt (make-node}\ {\tt empty}\ {\tt empty)}\/} &
\begin{picture}(100,30)
\btnode(00,25){(+1,-1)}
\btnode(50,25){(-1,-1)}
\put(25,00){\circle*{3}}
\end{picture}
\\
 \hline
\parbox[t]{2.5in}{ \begin{minipage}[t]{8in}\begin{tabbing}{\tt (m}\={\tt ake-node}\  \\
\>{\tt (m}\={\tt ake-node}\   \\
\>\>{\tt empty}\   \\
\>\>{\tt (make-node}\  {\tt empty}\  {\tt empty))}\   \\
\>{\tt empty)}\   \end{tabbing}\end{minipage}}
&
\begin{picture}(100,80)
\btnode(25,75){(+1,-1)}
\btnode(75,75){(-1,-1)}
\btnode(00,50){(+1,-1)}
\btnode(50,50){(-1,-1)}
\btnode(25,25){(+1,-1)}
\btnode(75,25){(-1,-1)}
\put(50,00){\circle*{3}}
\end{picture}
\\
 \hline
\end{tabular}
\end{center}
\center{Figure: Some stripped-down binary trees} \label{fig:sim-bt-ex}
\hrule

Using the graphical representation of binary trees we can easily determine
properties of trees. For example, we can count how many nodes it contains,
how many {{\tt empty}\/}s there are, or how high it is. Let's look at the
function {{\tt height}\/}, which consumes a tree and determines how high it
is: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt height}\ {\tt :}\ {\tt tree}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to measure the height of {{\tt abt0}\/}}
\\
\hbox{;; structural recursion }
\\
{\tt (d}\={\tt efine}\  {\tt (height}\  {\tt abt)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt abt)}\  {\tt 0\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (max}\  \={\tt (height}\  {\tt (node-left}\  {\tt abt))}
\\
\>\>\>{\tt (height}\  {\tt (node-right}\  {\tt abt)))}\  {\tt 1)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Like the data definition, this function definition has two
self-references.

To transform this function into an accumulator-style function, we follow the
standard path. We begin with putting an appropriate template into a
{{\tt local}\/} definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt height}\ {\tt :}\ {\tt tree}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to measure the height of {{\tt abt0}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (height}\  {\tt abt0)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} \ldots }
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (height-a}\  {\tt abt}\  {\tt accumulator)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt abt)}\  {\tt ...\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>\>\>{\tt ...}\  {\tt (height-a}\  \={\tt (node-left}\  {\tt abt)}\  
\\
\>\>\>\>\>\>\>\fbox{\ldots ({\sf node-right} {\it abt\/}) \ldots {\it accumulator\/}}{\tt )}\  {\tt ...}
\\
\>\>\>\>\>\>{\tt ...}\  {\tt (height-a}\  \={\tt (node-right}\  {\tt abt)}\  
\\
\>\>\>\>\>\>\>\fbox{\ldots ({\sf node-left} {\it abt\/}) \ldots {\it accumulator\/}}{\tt )}\  {\tt ...}\  {\tt \char'135}{\tt )))}
\\
\>\>{\tt (height}\  {\tt abt0}\  {\tt ...)))}
\end{tabbing}\end{minipage}\end{center}
The problem, as always, is to determine what knowledge the accumulator
should represent. 

An obvious choice is that {{\tt accumulator}\/} should be a number. More
specifically, {{\tt accumulator}\/} should represent the number of
{{\tt node}\/}s that {{\tt height-a}\/} has processed so far. Initially, it
has seen {{\tt 0}\/} nodes; as it descends the tree, it must increase the
accumulator as it processes a {{\tt node}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt ..}\={\tt .}\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} represents how many nodes {{\tt height-a}\/} }
\\
\>\>\>\hbox{;; has encountered on its way to {{\tt abt}\/} from {{\tt abt0}\/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (height-a}\  {\tt abt}\  {\tt accumulator)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt abt)}\  {\tt ...\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>\>\>{\tt ...}\  {\tt (height-a}\  {\tt (node-left}\  {\tt abt)}\  \  {\tt (+}\  {\tt accumulator}\  {\tt 1))}\  {\tt ...}
\\
\>\>\>\>\>\>{\tt ...}\  {\tt (height-a}\  {\tt (node-right}\  {\tt abt)}\  {\tt (+}\  {\tt accumulator}\  {\tt 1))}\  {\tt ...\char'135}{\tt )))}
\\
\>\>{\tt (height}\  {\tt abt0}\  {\tt 0))}
\end{tabbing}\end{minipage}\end{center}
That is, the accumulator invariant is that {{\tt accumulator}\/} counts how
many steps {{\tt height-a}\/} has taken on a particular path into the tree
{{\tt abt}\/}.

The result in the base case is {{\tt accumulator}\/} again; after all it
represents the height or length of the particular path. But, in contrast to
the first two examples, it is not the final result. In the second
{{\tt cond}\/}-clause, the new function has two heights to deal with. Given
that we are interested in the larger one, we use Scheme's {{\tt max}\/}
operation to select it. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt height}\ {\tt :}\ {\tt tree}\ {}{\tt ->}{}\ {\tt number}\/}}\\
\hbox{;; to measure the height of {{\tt abt0}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (height}\  {\tt abt0)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} represents how many nodes {{\tt height-a}\/} }
\\
\>\>\>\hbox{;; has encountered on its way to {{\tt abt}\/} from {{\tt abt0}\/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (height-a}\  {\tt abt}\  {\tt accumulator)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt abt)}\  {\tt accumulator\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (max}\  \={\tt (height-a}\  {\tt (node-left}\  {\tt abt)}\  \  {\tt (+}\  {\tt accumulator}\  {\tt 1))}
\\
\>\>\>\>\>\>{\tt (height-a}\  {\tt (node-right}\  {\tt abt)}\  {\tt (+}\  {\tt accumulator}\  {\tt 1)))\char'135}{\tt )))}
\\
\>\>{\tt (height-a}\  {\tt abt0}\  {\tt 0)))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The accumulator-style version of {\it height}}
\label{fig:height-accu}
\hrule 

Figure~\ref{fig:height-accu} contains the complete definition for
{{\tt height}\/}. Our final step is to check out a hand-evaluation of  the
new function. We use the most complex example from the above table: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (height}\  {\tt (m}\={\tt ake-node}\\
\>{\tt (make-node}\  \={\tt empty}\  
\\
\>\>{\tt (make-node}\  {\tt empty}\  {\tt empty))}\  
\\
\>{\tt empty))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (height-a}\  \={\tt (m}\={\tt ake-node}\\
\>\>{\tt (make-node}\  \={\tt empty}\  
\\
\>\>\>{\tt (make-node}\  {\tt empty}\  {\tt empty))}\  
\\
\>\>{\tt empty)}
\\
\>{\tt 0)}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (max}\  \={\tt (h}\={\tt eight-a}\\
\>\>{\tt (make-node}\  \={\tt empty}\  
\\
\>\>\>{\tt (make-node}\  {\tt empty}\  {\tt empty))}
\\
\>\>{\tt 1)}
\\
\>{\tt (height-a}\  {\tt empty}\  {\tt 1))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (max}\  \={\tt (m}\={\tt ax}\  \\
\>\>{\tt (height-a}\  {\tt empty}\  {\tt 2)}
\\
\>\>{\tt (height-a}\  {\tt (make-node}\  {\tt empty}\  {\tt empty)}\  {\tt 2))}
\\
\>{\tt (height-a}\  {\tt empty}\  {\tt 1))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (max}\  \={\tt (m}\={\tt ax}\  \\
\>\>{\tt (height-a}\  {\tt empty}\  {\tt 2)}
\\
\>\>{\tt (max}\  {\tt (height-a}\  {\tt empty}\  {\tt 3)}\  {\tt (height-a}\  {\tt empty}\  {\tt 3)))}
\\
\>{\tt (height-a}\  {\tt empty}\  {\tt 1))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (max}\  \={\tt (m}\={\tt ax}\  \\
\>\>{\tt 2}
\\
\>\>{\tt (max}\  {\tt 3}\  {\tt 3))}
\\
\>{\tt 1)}
\\
{\tt =}\  {\tt 3}
\end{tabbing}\end{minipage}\end{center}
It shows how {{\tt height-a}\/} increments the accumulator at each step and
that the accumulator at the top of a path represents the number of lines
traversed. The hand-evaluation also shows that the results of the various
branches are combined at each branching point. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 31.3.3}

 \label{ex:pi-accu}
Develop an accumulator-style version of {{\tt product}\/}, the function that
computes the product of a list of numbers. Show the stage that explains
what the accumulator represents.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/pi-accu.html}{\rule{3pt}{5pt}}


{\bf Exercise 31.3.4}

 \label{ex:length-accu}
Develop an accumulator-style version of {{\tt how-many}\/}, the function
that determines the number of items on a list. Show the stage that explains
what the accumulator represents.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/length-accu.html}{\rule{3pt}{5pt}}


{\bf Exercise 31.3.5}

 \label{ex:add-pi-accu}
Develop an accumulator-style version of {{\tt add-to-pi}\/}, the function
that adds a natural number to {{\tt pi}\/} without using {{\tt +}\/} (see
section~\ref{sec:nat-num:nature}). Show the stage that explains what the
accumulator represents.

Generalize the function so that it adds two numbers, the first one a natural
number, without using {{\tt +}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/add-pi-accu.html}{\rule{3pt}{5pt}}


{\bf Exercise 31.3.6}

 \label{ex:palindrome}
Develop the function {{\tt make-palindrome}\/}, which accepts a non-empty
list and constructs a palindrome by mirroring the list around the last
item.  Thus, if we were to represent the word ``abc'' and apply
{{\tt make-palindrome}\/}, we would get back the representation of
``abcba''.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/palindrome.html}{\rule{3pt}{5pt}}


{\bf Exercise 31.3.7}

 \label{ex:to10}
Develop {{\tt to10}\/}. It consumes a list of digits and produces the
corresponding number. The first item on the list is the {most significant\/} digit.

\noindent Examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (to10}\  {\tt (list}\  {\tt 1}\  {\tt 0}\  {\tt 2))}\  \\
{\tt =}\  {\tt 102}
\\
\  \  {\tt (to10}\  {\tt (list}\  {\tt 2}\  {\tt 1))}
\\
{\tt =}\  {\tt 21}
\end{tabbing}\end{minipage}\end{center}

Now generalize the function so that it consumes a base $b$ and a list of
$b$-digits. The conversion produces the decimal (10-based) value of the
list. The base is between {{\tt 2}\/} and {{\tt 10}\/}. A $b$-digit is a
number between {0} and $b-1$. 

\noindent Examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (to10-general}\  {\tt 10}\  {\tt (list}\  {\tt 1}\  {\tt 0}\  {\tt 2))}\  \\
{\tt =}\  {\tt 102}
\\
\  \  {\tt (to10-general}\  {\tt 08}\  {\tt (list}\  {\tt 1}\  {\tt 0}\  {\tt 2))}\  
\\
{\tt =}\  {\tt 66}
\end{tabbing}\end{minipage}\end{center}

\noindent{\bf Hint:} \ In the first example, the result is determine by
$$ 1 \cdot 10^2 + 0 \cdot 10^1 + 2 \cdot 10^0 
 = ((1 \cdot 10 + 0) \cdot 10) + 2 \ ;$$
the second one is 
$$ 1 \cdot 8^2 + 0 \cdot 8^1 + 2 \cdot 8^0 
 = ((1 \cdot 8) + 0) \cdot 8) + 2 \ .$$
That is, the exponent represents the number of digits that
follow.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/to10.html}{\rule{3pt}{5pt}}


{\bf Exercise 31.3.8}

 \label{ex:is-prime}
Develop the function {{\tt is-prime\char'077}\/}, which consumes a natural number and
returns {{\sf true}\/} if it is prime and {{\sf false}\/} otherwise. A number $n$
is prime if it not divisible by any number between $2$ and $n-1$. 

\noindent{\bf Hints:} \ (1) The design recipe for {{\bf N}{\tt \char'133}{\tt >=1\char'135}\/} suggests
the following template: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt is-prime\char'077}\ {\tt :}\ {\bf N}{\tt \char'133}{\tt >=1\char'135}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt n}\/} is a prime number}
\\
{\tt (d}\={\tt efine}\  {\tt (is-prime\char'077}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt 1)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (is-prime\char'077}\  {\tt (sub1}\  {\tt n))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
From this outline, we can immediately conclude that the function
forgets {{\tt n}\/}, its initial argument as it recurs. Since {{\tt n}\/}
is clearly needed to determine whether {{\tt n}\/} is divisible by $2$ ... $n-1$,
this suggests that we design an accumulator-style local function that
remembers {{\tt n}\/} as it recurs.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/is-prime.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


\medskip\noindent{\bf Pitfalls}:\ 
\htmladdimg{../icons/teacher.gif}~{\em PLAN: need to dig out the bad exam solution and work it in \\
    quick-sort or merge-sort misunderstood -- and we get a bad version of    insertion sort back}
People who encounter accumulator-style programming for the first time often
get the impression that they are always faster or easier to understand
(design) than their recursive counterparts. Both parts are plain
wrong. While it is impossible to deal with the full scope of the mistake,
let us take a look at a small counterexample. 

Consider the following table:
\begin{center}
\begin{tabular}{r|r}
\mbox{plain factorial} & \mbox{accumulator-style factorial} \\
 \hline
5.760	& 5.970 \\
5.780 	& 5.940 \\
5.800 	& 5.980 \\
5.820 	& 5.970 \\
5.870	& 6.690 \\
 \hline
5.806   & 6.110
\end{tabular}
\end{center}
It represents the results for
exercise~\ref{ex:time-factorial}. Specifically, the left column shows the
number of seconds for 1000 evaluations of {{\tt (!}\ {\tt 20)}\/} with the plain
factorial function; the right column shows what the same experiment yields
when we use the factorial function with an accumulator parameter. The last
row shows the averages for the two columns. The table shows that the
performance of the accumulator-style version of factorial is always worse
than that of the original factorial function.~{\rule{3pt}{5pt}}

\section{More Uses of Accumulation} \label{sec:more-accu}

This section presents three extended exercises that require the whole range
of skills: design by recipe, including generative recursion, and the
addition of accumulators for various purposes. 

\subsection{Extended Exercise: Accumulators on Trees} \label{sec:more-accu-trees}

\htmladdimg{../icons/teacher.gif}~{\em This subsection requires a thorough understanding of tree-processing, not just list-processing. It is possible to skip this subsection and to  work through one of the games in the following two subsection.}

Figure~\ref{fig:blue-eyes-recall} recalls the structure and data
definitions of family trees from section~\ref{sec:struct-in-struct} where
we developed the function {{\tt blue-eyed-ancestor\char'077}\/}, which determined
whether an ancestor family tree contained a blue-eyed family member. In
contrast, {{\tt all-blue-eyed-ancestors}\/}, the function in
figure~\ref{fig:blue-eyes-recall}, collects the names of all blue-eyed
family in a given family tree.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt child}\  {\tt (father}\  {\tt mother}\  {\tt name}\  {\tt date}\  {\tt eyes))}\end{tabbing}\end{minipage}\end{center}

\begin{quote}   A {\sl {node in a family tree}} (short: {{\tt ftn}\/}) is either    \begin{enumerate}   \item {{\tt empty}\/}, or   \item {{\tt (make-child}\ {\tt f}\ {\tt m}\ {\tt na}\ {\tt da}\ {\tt ec)}\/}            where {{\tt f}\/} and {{\tt m}\/} are {{\tt ftn}\/}s, {{\tt na}\/}          and {{\tt ec}\/} are symbols, and {{\tt da}\/} is a number.    \end{enumerate} \end{quote}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt all-blue-eyed-ancestors}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt symbol)}\/}}\\
\hbox{;; to construct a list of all blue-eyed ancestors in {{\tt a-ftree}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (all-blue-eyed-ancestors}\  {\tt a-ftree)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-ftree)}\  {\tt empty\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt in-parents}\  
\\
\>\>\>\>{\tt (append}\  \={\tt (all-blue-eyed-ancestors}\  {\tt (child-father}\  {\tt a-ftree))}
\\
\>\>\>\>\>{\tt (all-blue-eyed-ancestors}\  {\tt (child-mother}\  {\tt a-ftree)))))}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (child-eyes}\  {\tt a-ftree)}\  {\tt \char'047}{\tt blue)}\  
\\
\>\>\>\>\>{\tt (cons}\  {\tt (child-name}\  {\tt a-ftree)}\  {\tt in-parents)\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt in-parents\char'135}{\tt ))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Collecting family trees with {\it blue-eyed-ancestor?\/}}
\label{fig:blue-eyes-recall}
\hrule

The function's structure is the template that of a tree-processing function.
It has two cases: one for the empty tree and another one for a
{{\tt child}\/} node. The latter clause contains two self-references: one
per parent. These recursive applications collect the names of all blue-eyed
ancestors from the father's and the mother's family tree; {{\tt append}\/}
combines the two lists into one. 

The {{\tt append}\/} function is a structurally recursive function. Here it
processes the two lists that the natural recursions of
{{\tt all-blue-eyed-ancestors}\/} produce. According to
section~\ref{sec:two-inputs:case1}, this observation suggests that the
function is a natural candidate for a transformation into
accumulator-style. 

Let's get started:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt all-blue-eyed-ancestors}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt symbol)}\/}}\\
\hbox{;; to construct a list of all blue-eyed ancestors in {{\tt a-ftree}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (all-blue-eyed-ancestors}\  {\tt a-ftree0)}\  
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} \ldots}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (all-a}\  {\tt a-ftree}\  {\tt accumulator)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-ftree)}\  {\tt ...\char'135}
\\
\>\>\>\>\>{\tt \char'133}\={\tt else}\  
\\
\>\>\>\>\>\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt in-parents}\  
\\
\>\>\>\>\>\>\>\>{\tt (all-a}\  \={\tt ...}\  {\tt (child-father}\  {\tt a-ftree)}\  {\tt ...}\  
\\
\>\>\>\>\>\>\>\>\>{\tt ...}\  {\tt accumulator}\  {\tt ...)}
\\
\>\>\>\>\>\>\>\>{\tt (all-a}\  \={\tt ...}\  {\tt (child-mother}\  {\tt a-ftree)}\  {\tt ...}
\\
\>\>\>\>\>\>\>\>\>{\tt ...}\  {\tt accumulator}\  {\tt ...)))}
\\
\>\>\>\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (child-eyes}\  {\tt a-ftree)}\  {\tt \char'047}{\tt blue)}\  
\\
\>\>\>\>\>\>\>\>\>{\tt (cons}\  {\tt (child-name}\  {\tt a-ftree)}\  {\tt in-parents)\char'135}
\\
\>\>\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt in-parents\char'135}{\tt ))\char'135}{\tt )))}
\\
\>\>{\tt (all-a}\  {\tt a-ftree0}\  {\tt ...)))}
\end{tabbing}\end{minipage}\end{center}

Our next goal is the formulation of an accumulator invariant. The general
purpose of the accumulator is to remember knowledge about {{\tt a-ftree0}\/}
that {{\tt all-a}\/} loses as it traverses the tree.  A look at the
definition in figure~\ref{fig:blue-eyes-recall} shows two recursive
applications. The first one processes {{\tt (child-father}\ {\tt a-ftree)}\/},
which means that this application {{\tt all-blue-eyed-ancestors}\/} loses
knowledge about the mother of {{\tt a-ftree}\/}. Conversely, the second
recursive application has no knowledge of the father of {{\tt a-ftree}\/} as
it processes the mother's family tree.

At this point, we have two choices: 
\begin{enumerate}
\item The accumulator could represent all blue-eyed ancestors encountered
so far, including those in the mother's family tree, as it descends into
the father's tree. 

\item The alternative is to have the accumulator stand for the lost items
in the tree. That is, as {{\tt all-a}\/} processes the father's family tree,
it remembers the mother's tree in the accumulator (and everything else it
hasn't seen before). 
\end{enumerate}
Let's explore both possibilities, starting with the first.

Initially, {{\tt all-a}\/} has not seen any of the nodes in the family tree,
so {{\tt accumulator}\/} is {{\tt empty}\/}. As {{\tt all-a}\/} is about to
traverse the father's family tree, we must create a list that represents
all blue-eyed ancestors in the tree that we are about to forget, which is
the mother's tree. This suggests the following accumulator invariant
formulation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt accumulator}\/} is the list of blue-eyed ancestors}\\
\hbox{;; encountered on the mother-side trees of the path in}
\\
\hbox{;; {{\tt a-ftree0}\/} to {{\tt a-ftree}\/}}
\end{tabbing}\end{minipage}\end{center}

To maintain the invariant for the natural recursion, we must collect the
ancestors on the mother's side of the tree. Since the purpose of
{{\tt all-a}\/} is to collect those ancestors, we use the expression 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (all-a}\  {\tt (child-mother}\  {\tt a-ftree)}\  {\tt accumulator)}\end{tabbing}\end{minipage}\end{center}
to compute the new accumulator for the application of {{\tt all-a}\/} to
{{\tt (child-father}\ {\tt a-ftree)}\/}. Putting everything together for the second
{{\tt cond}\/}-clause yields this:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt in-parents}\  \\
\>\>{\tt (a}\={\tt ll-a}\  {\tt (child-father}\  {\tt a-ftree)}
\\
\>\>\>{\tt (a}\={\tt ll-a}\  {\tt (child-mother}\  {\tt a-ftree)}
\\
\>\>\>\>{\tt accumulator))))}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (child-eyes}\  {\tt a-ftree)}\  {\tt \char'047}{\tt blue)}\  
\\
\>\>\>{\tt (cons}\  {\tt (child-name}\  {\tt a-ftree)}\  {\tt in-parents)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt in-parents\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
This leaves us with the answer in the first {{\tt cond}\/}-clause.  Since
{{\tt accumulator}\/} represents all blue-eyed ancestors encountered so far,
it is the result. Figure~\ref{fig:blue-eyes-accu-1} contains the complete
definition.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt all-blue-eyed-ancestors}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt symbol)}\/}}\\
\hbox{;; to construct a list of all blue-eyed ancestors in {{\tt a-ftree}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (all-blue-eyed-ancestors}\  {\tt a-ftree)}\  
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} is the list of blue-eyed ancestors}
\\
\>\>\>\hbox{;; encountered on the mother-side trees of the path in}
\\
\>\>\>\hbox{;; {{\tt a-ftree0}\/} to {{\tt a-ftree}\/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (all-a}\  {\tt a-ftree}\  {\tt accumulator)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt a-ftree)}\  {\tt accumulator\char'135}
\\
\>\>\>\>\>{\tt \char'133}\={\tt else}\  
\\
\>\>\>\>\>\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt in-parents}\  
\\
\>\>\>\>\>\>\>\>{\tt (all-a}\  \={\tt (child-father}\  {\tt a-ftree)}\  
\\
\>\>\>\>\>\>\>\>\>{\tt (all-a}\  {\tt (child-mother}\  {\tt a-ftree)}\  {\tt accumulator))))}
\\
\>\>\>\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (child-eyes}\  {\tt a-ftree)}\  {\tt \char'047}{\tt blue)}\  
\\
\>\>\>\>\>\>\>\>\>{\tt (cons}\  {\tt (child-name}\  {\tt a-ftree)}\  {\tt in-parents)\char'135}
\\
\>\>\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt in-parents\char'135}{\tt ))\char'135}{\tt )))}
\\
\>\>{\tt (all-a}\  {\tt a-ftree}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Collecting blue-eyed ancestors, accumulator-style (version 1)}
\label{fig:blue-eyes-accu-1}
\hrule

For the second version, we want the accumulator to represent a list of all
of the family trees that haven't been processed yet. Because of the
different intention, let us call the accumulator parameter {{\tt todo}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt todo}\/} is the list of family trees }\\
\hbox{;; encountered but not processed }
\end{tabbing}\end{minipage}\end{center}
Like the accumulator-style {{\tt reverse}\/}, {{\tt all-a}\/} initializes
{{\tt todo}\/} to {{\tt empty}\/}. It updates it by extending the list for
the natural recursion:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt in-parents}\  \\
\>\>{\tt (a}\={\tt ll-a}\  {\tt (child-father}\  {\tt a-ftree)}\  
\\
\>\>\>{\tt (cons}\  {\tt (child-mother}\  {\tt a-ftree)}\  {\tt todo))))}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (child-eyes}\  {\tt a-ftree)}\  {\tt \char'047}{\tt blue)}\  
\\
\>\>\>{\tt (cons}\  {\tt (child-name}\  {\tt a-ftree)}\  {\tt in-parents)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt in-parents\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

The problem now is that when {{\tt all-a}\/} is applied to the
{{\tt empty}\/} tree, {{\tt todo}\/} does not represent the result but what
is left to do for {{\tt all-a}\/}. To visit all those family trees,
{{\tt all-a}\/} must be applied to them, one at a time. Of course, if
{{\tt todo}\/} is {{\tt empty}\/}, there is nothing left to do; the result is
{{\tt empty}\/}. If {{\tt todo}\/} is a list, we pick the first tree on the
list as the next one to be processed: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (empty?}\  {\tt todo)}\  {\tt empty\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (all-a}\  {\tt (first}\  {\tt todo)}\  {\tt (rest}\  {\tt todo))\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
The rest of the list is what is now left to do. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt all-blue-eyed-ancestors}\ {\tt :}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt (listof}\ {\tt symbol)}\/}}\\
\hbox{;; to construct a list of all blue-eyed ancestors in {{\tt a-ftree}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (all-blue-eyed-ancestors}\  {\tt a-ftree0)}\  
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt todo}\/} is the list of family trees encountered but not processed }
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (all-a}\  {\tt a-ftree}\  {\tt todo)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (empty?}\  {\tt a-ftree)}\  
\\
\>\>\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt todo)}\  {\tt empty\char'135}
\\
\>\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (all-a}\  {\tt (first}\  {\tt todo)}\  {\tt (rest}\  {\tt todo))\char'135}{\tt )\char'135}
\\
\>\>\>\>\>{\tt \char'133}\={\tt else}\  
\\
\>\>\>\>\>\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt in-parents}\  
\\
\>\>\>\>\>\>\>\>{\tt (all-a}\  \={\tt (child-father}\  {\tt a-ftree)}\  
\\
\>\>\>\>\>\>\>\>\>{\tt (cons}\  {\tt (child-mother}\  {\tt a-ftree)}\  {\tt todo))))}
\\
\>\>\>\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (child-eyes}\  {\tt a-ftree)}\  {\tt \char'047}{\tt blue)}\  
\\
\>\>\>\>\>\>\>\>\>{\tt (cons}\  {\tt (child-name}\  {\tt a-ftree)}\  {\tt in-parents)\char'135}
\\
\>\>\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt in-parents\char'135}{\tt ))\char'135}{\tt )))}
\\
\>\>{\tt (all-a}\  {\tt a-ftree0}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Collecting blue-eyed ancestors, accumulator-style (version 2)}
\label{fig:blue-eyes-accu}
\hrule

Figure~\ref{fig:blue-eyes-accu} contains the complete definition for this
second accumulator-style variant of {{\tt all-blue-eyed-ancestors}\/}. The
auxiliary definition is the most unusual recursive function definition we
have seen.  It contains a recursive application of {{\tt all-a}\/} in both
the first and the second {{\tt cond}\/}-clause. That is, the function
definition does not match the data definition for family trees, the primary
inputs.  While a function like that can be the result of a careful chain of
development steps, starting from a working function developed with a design
recipe, it should never be a starting point. 

The use of accumulators is also fairly common in programs that process
representations of programs. We encountered these forms of data in
section~\ref{sec:interpreter}, and like family trees, they have complicated
data definitions. In intermezzo~3, we also discussed some concepts
concerning variables and their mutual association, though without
processing these concepts. The following exercises introduce simple
functions that work with the scope of parameters, binding occurrences of
variables, and other notions. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 32.1.1}

 \label{ex:free-bound-dd}
Develop a data representation for the following tiny subset of 
Scheme expressions: 
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{exp}>}} 
  &=& \mbox{{\tt <{var}>}} 
      $\,\,\vert\,\,$ ({\bf lambda} (\mbox{{\tt <{var}>}}) \mbox{{\tt <{exp}>}})
      $\,\,\vert\,\,$ (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}})
\end{tabular}
\end{center}
The subset contains only three kinds of expressions: variables, functions
of one argument, and function applications. 

\noindent Examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \  \  \\
\hbox{$1.$}\  {\tt (lamb}\={\tt da}\  {\tt (x)}\  {\tt y)}
\\
\>
\\
\hbox{$2.$}\  {\tt ((lam}\={\tt bda}\  {\tt (x)}\  {\tt x)}\  
\\
\>{\tt (lambda}\  {\tt (x)}\  {\tt x))}
\\
\hbox{$3.$}\  {\tt (((la}\={\tt m}\={\tt bd}\={\tt a}\  {\tt (y)}
\\
\>\>\>{\tt (l}\={\tt ambda}\  {\tt (x)}
\\
\>\>\>\>{\tt y))}
\\
\>\>{\tt (lambda}\  {\tt (z)}\  {\tt z))}
\\
\>{\tt (lambda}\  {\tt (w)}\  {\tt w))}
\end{tabbing}\end{minipage}\end{center}        
Represent variables as symbols. Call the class of data {\sl {Lam}}.

Recall that {{\bf lambda}-exp\-res\-sion}s are functions without names. Thus, they bind their
parameter in the body. In other words, the scope of a {\bf lambda}-exp\-res\-sion's parameter
is the body. Explain the scope of each binding occurrence in the above
examples. Draw arrows from all bound occurrences to the binding
occurrences.

If a variable occurs in an expression but has no corresponding binding
occurrence, the occurrence is said to be free.  Make up an expression in
which {{\tt x}\/} occurs both free and bound.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/free-bound-dd.html}{\rule{3pt}{5pt}}


{\bf Exercise 32.1.2}

 \label{ex:free-bound0}
Develop the function 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt free-or-bound}\ {\tt :}\ {\tt Lam}\ {}{\tt ->}{}\ {\tt Lam}\/} }\\
\hbox{;; to replace each non-binding occurrence of a variable in {{\tt a-lam}\/} }
\\
\hbox{;; with {{\tt \char'047}{\tt free}\/} or {{\tt \char'047}{\tt bound}\/}, depending on whether the }
\\
\hbox{;; occurrence is bound or not.}
\\
{\tt (define}\  {\tt (free-or-bound}\  {\tt a-lam)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
where {{\tt Lam}\/} is the class of expression representations from
exercise~\ref{ex:free-bound-dd}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/free-bound0.html}{\rule{3pt}{5pt}}


{\bf Exercise 32.1.3}

 \label{ex:free-bound1}
Develop the function 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt unique-binding}\ {\tt :}\ {\tt Lam}\ {}{\tt ->}{}\ {\tt Lam}\/} }\\
\hbox{;; to replace variables names of binding occurrences and their bound}
\\
\hbox{;; counterparts so that no name is used twice in a binding occurrence}
\\
{\tt (define}\  {\tt (unique-binding}\  {\tt a-lam)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
where {{\tt Lam}\/} is the class of expression representations from
exercise~\ref{ex:free-bound-dd}.

\noindent{\bf Hint:} \ The function {{\tt gensym}\/} creates a new and unique symbol from a
given symbol. The result is guaranteed to be distinct from all other
symbols in the program, including those previously generated with
gensym.

Use the technique of this section to improve the function. \noindent{\bf Hint:} \ The
accumulator relates old parameter names to new parameter
names.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/free-bound1.html}{\rule{3pt}{5pt}}



\subsection{Extended Exercise: Missionaries and Cannibals}
\label{sec:more-accu-mc}

\htmladdimg{../icons/teacher.gif}~{\em The solution to this extended exercise requires many ``domain knowledge'' functions, structurally recursive functions on lists, algorithms, and accumulators.  Ask students to annotate each function with the appropriate design recipe: domain knowledge, structural, and generative. If a function maintains an  accumulator invariant, the function must contain an appropriate comment.   The structurally recursive functions are instances of {{\tt map}\/} and {{\tt filter}\/}; {{\tt build-list}\/}, {{\tt foldl}\/} and {{\tt append}\/} can also be used. There are also plenty of other opportunities for abstraction. Make sure students ``edit'' functions; if they don't, quiz them on how many changes they have to make for some possible modification of the problem.   {Warning} The function is computationally intensive. It can take about a  minute to run on a SparcStation.}

\def\mc(#1,#2)(#3,#4)#5{\fbox{\begin{picture}(50,25)(-5,-8) \multiput(0,12)(0,-6){#1}{\circle*{3}} \multiput(6,12)(0,-6){#2}{\circle{3}} \multiput(42,12)(0,-6){#3}{\circle*{3}} \multiput(36,12)(0,-6){#4}{\circle{3}} 
\put(12,-5){\line(0,1){20}} 
\put(30,-5){\line(0,1){20}} \multiput(#5,07)(0,1){3}{\line(1,0){3}} \end{picture}}}


\begin{htmlonly}
\end{htmlonly}

On occasion, accumulators are a part of a piece of compound data because a
function manages many pieces of data (in the same class) at the same time.
The following story poses just such a problem:
\begin{quote}
``Once upon a time, three cannibals were guiding three missionaries through
a jungle. They were on their way to the nearest mission station. After some
time, they arrived at a wide river, filled with deadly snakes and
fish. There was no way to cross the river without a boat. Fortunately, they
found a row boat with two oars after a short search. Unfortunately, the
boat was too small to carry all of them. It could barely carry two people
at a time.  Worse, because of the river's width there was no way to bring
the boat back other than to row it back.

``Since the missionaries could not trust the cannibals they had to figure
out a plan to get all six of them safely across the river. The problem was
that these cannibals would kill and eat missionaries as soon as there were
more cannibals than missionaries at some place. Thus, our
missionary-programmer had to devise a plan that guaranteed that there were
never any missionaries in the minority at either side of the river.  The
cannibals, however, can be trusted to cooperate otherwise. Specifically,
they won't abandon any potential food, just as the missionaries won't
abandon any potential converts.

Luckily one of the missionaries had taken a Scheme course and knew how to
solve this problem.''
\end{quote}
While we can solve the problem by hand, solving it with a Scheme function is
more fun and more general. If the same story comes up again with different
numbers of cannibals and missionaries or different boat sizes, we can use
the same function to solve the problem again.

As with every problem, we begin by laying out how to represent the problem
in our data language and then study how to represent certain actions in the
real world in our programming language. Here are two basic constants
concerning the data representation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt MC}\  {\tt 3)}\\
{\tt (define}\  {\tt BOAT-CAPACITY}\  {\tt 2)}
\end{tabbing}\end{minipage}\end{center}
Formulate the function in terms of these constants. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 32.2.1}

 \label{ex:mc-state} 
Provide a data representation for the states of a river crossing.  A
state should record the number of missionaries and cannibals on each side
of the river and the location of the boat.  Here is a graphical
representation of the states: \\
 \begin{rawhtml} <center><img src=../icons/mc-a.gif></img></center> \end{rawhtml}\\
[.3cm]
The two lines represent the river, the black and white dots the
missionaries and cannibals, the black rectangle the boat. Determine the
initial and final state of the game.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mc-state.html}{\rule{3pt}{5pt}}


{\bf Exercise 32.2.2}

 \label{ex:mc-boatload} 
Develop a data representation for boat loads. Define {{\tt BOAT-LOADS}\/},
the list of all possible boat loads. 

Develop the function {{\tt make-BOAT-LOADS}\/}, which consumes boat's maximal
capacity and constructs the list of possible boat loads.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mc-boatload.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


One way to deal with search problems in a systematic manner is to generate
all possible successor states for the states we have reach so far, to
filter out the interesting ones, and to start the search over from those.
A successor state is reached by using a feasible transition, for example,
an enabled move in a game, a boat trip, etc. 

Here is a graphical illustration of the situation for our problem:
 \begin{rawhtml} <center><img src=../icons/mc-b.gif></img></center> \end{rawhtml}

\noindent The initial state in the top row has five possible successor states, one
per feasible boat load. They are shown in the second row. Two of these
successor states are illegal, because one side contains more cannibals than
missionaries. One of the legal ones is the state in which one missionary
and cannibal reached the right side of the river; it has three successor
states in turn. The following exercises deal with generating the successor
states and filtering out the interesting ones. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

\medskip\noindent{\bf Tests}:\  Formulate all tests as boolean-valued expressions that
produce {{\tt true}\/} if the expected value is the computed one, and 
{{\tt false}\/} if not.~{\rule{3pt}{5pt}}

{\bf Exercise 32.2.3}

 \label{ex:mc-succ} 
Develop a function that consumes a state and returns a list of all possible
successor states, that is, all those states that are reachable with one
boat trip from one side of the river to the other. 

Develop a generalized version that consumes a list of states, and returns a
list of states reachable with one crossing.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mc-succ.html}{\rule{3pt}{5pt}}


{\bf Exercise 32.2.4}

 \label{ex:mc-legal} 
Develop a function that determines whether a give state is legal. A state
is legal if it contains the proper number of missionaries and cannibals
and if the missionaries on each side of the river are safe.

Develop a generalized function that consumes a list of states and returns
the sublist of legal states.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mc-legal.html}{\rule{3pt}{5pt}}


{\bf Exercise 32.2.5}

 \label{ex:mc-final} 
Develop a function that consumes a state and determines if it is final.

Develop a generalized version that consumes a list of states and returns
the sublist of final states.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mc-final.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


The functions we have developed can generate the successor states of a list
of states and can detect whether any of the states reached so far are
legal.  Now we can develop a function that determines whether we can
transport the missionaries and cannibals across the river.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 32.2.6}

 \label{ex:mc-together1} 
Develop {{\tt mc-solvable\char'077}\/}, which consumes a list of states and
generates the list of all successor states until it has found a final
state. The function should simply produce {{\sf true}\/} when it finds a final
state.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mc-together1.html}{\rule{3pt}{5pt}}


{\bf Exercise 32.2.7}

 \label{ex:mc-together2} 
Develop {{\tt mc-solution}\/}. The function is an adaptation of
{{\tt mc-solvable\char'077}\/} that not only produces {{\sf true}\/} when it finds a
solution but a list of river crossings if a given missionary-and-cannibal
problem is solvable.

\noindent{\bf Hint:} \ Modify the state representations so that they accumulate the list of
 crossings that got the group to this particular state. For the initial
 state, this is just the empty list; for a final state, it is the desired
 solution.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mc-together2.html}{\rule{3pt}{5pt}}


{\bf Exercise 32.2.8}

 \label{ex:mc-together3} 
A series of boat trips may bring the group of missionaries and cannibals
 back to the initial state (or some other previous state). The series may
 include two, four, or more boat trips. In short, the ``game'' contains
 cycles.  Make up an example.

The function {{\tt mc-solution}\/} generates all those states reachable with,
 say, seven boat trips before it generates all those states reachable with
 eight crossings. Therefore we do not have to worry about cycles in
 solution attempts. Why?  

Modify the solution so that a state reached via a cycle is also illegal. 

\medskip\noindent{\bf Note}:\  This shows how the accumulator inside the state representation
 has two uses.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mc-together3.html}{\rule{3pt}{5pt}}


\subsection{Extended Exercise: Board Solitaire}
\label{sec:more-accu-soli}

\htmladdimg{../icons/teacher.gif}~{\em {\bf Warning}: This exercise is complex and requires serious work. At Rice, we assign it as a project after eight weeks of work and give students two weeks to think about it. Then they turn in a design for solitaire (based on generative recursion template, plus backtracking) and headers plus purpose statements for all help functions mentioned in the design.  For the final product, ask that they specify for each function whether it is a function based on structural processing (including which argument's structure is used), generative recursion, or domain knowledge. Also, if a function uses an accumulator, request a comment on what the accumulator invariant is and how it is maintained during evaluation.}


Peg Solitaire is a board game for individuals.  The board comes in various
shapes. Here is the simplest one:
\begin{center}
\begin{picture}(40,40) \multiput(00,00)(10,0){4}{\circle{6}} \multiput(05,10)(10,0){3}{\circle{6}} \multiput(10,20)(10,0){2}{\circle{6}} \multiput(15,30)(10,0){1}{\circle{6}} 
\put(15,10){\circle*{3}} \end{picture}
\end{center}
The circle with a black dot represents an unoccupied hole, the others holes
with little pegs.

The goal of the game is to eliminate the pegs one by one, until only one
peg is left. A player can eliminate a peg if one of the neighboring holes
is unoccupied and if there is a peg in the hole in the opposite
direction. In that case, the second peg can jump over the first one and the
first one is eliminated. Consider the following configuration: 
\begin{center}
\begin{picture}(40,40) \multiput(00,00)(10,0){4}{\circle{6}} \multiput(05,10)(10,0){3}{\circle{6}} \multiput(10,20)(10,0){2}{\circle{6}} \multiput(15,30)(10,0){1}{\circle{6}}  
\put(25,10){\circle*{3}} 
\put(-18,06){\mbox{1}}
\put(-10,10){\vector(1,0){10}} 
\put(-08,26){\mbox{2}}
\put(-00,30){\vector(1,0){10}} \end{picture}
\end{center}
Here the pegs labeled~1 and~2 could jump. If the player decides to move the
peg labeled~2, the next configuration is
\begin{center}
\begin{picture}(40,40) \multiput(00,00)(10,0){4}{\circle{6}} \multiput(05,10)(10,0){3}{\circle{6}} \multiput(10,20)(10,0){2}{\circle{6}} \multiput(15,30)(10,0){1}{\circle{6}}  
\put(15,30){\circle*{3}} 
\put(20,20){\circle*{3}} \end{picture}
\end{center}

Some configurations are dead-ends. For a simple example, consider the first
board configuration. Its hole is in the middle of the board, Hence, not one
peg can jump, because there are no two pegs in a row, column, or diagonal
such that one can jump over the other into the hole. A player who discovers
a dead-end configuration must stop or backtrack by undoing moves and trying
alternatives. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 32.3.1}

 \label{ex:peg-sol-data} Develop a representation for
triangular Solitaire boards. 

Develop a data representation for peg moves. Pegs can move along a row, a
column, and a diagonal. 

\noindent{\bf Hints:} \ (1) There are at least four rows, because it is impossible to play
the game with three or fewer. Still, develop the data definition
independently of such constraints. (2) Translate our examples from above
into the chosen data representations.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/peg-sol-data.html}{\rule{3pt}{5pt}}


{\bf Exercise 32.3.2}

 \label{ex:peg-sol-abs}
Develop a function that, given a board and the board position of a peg,
determines whether or not the peg can jump. We call such a peg {\sl enabled\/}. 

Develop a function that, given a board and the board position of an enabled
peg, creates a board that represents the next
configuration.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/peg-sol-abs.html}{\rule{3pt}{5pt}}



{\bf Exercise 32.3.3}

 \label{ex:peg-sol}
Develop the function {{\tt solitaire}\/}, which solves a Solitaire problem
 for different sizes of the equilateral triangle. The function should
 consume a the board. It produces {{\sf false}\/}, if the given problem is not
 solvable. Otherwise, it should produce a list of moves that specifies in
 what order the pegs must be moved to solve the given Solitaire
 problem.

Formulate the tests for all functions as boolean-valued
expressions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/peg-sol.html}{\rule{3pt}{5pt}}


%% BEGIN INLINED FILE: curriculumE.dl.tex



 \part{{Intermezzo 6: The Nature of Inexact Numbers}}  \section{{Intermezzo 6: The Nature of Inexact Numbers}}   \label{sec:inexact}

Computers represent and process information in chunks of a fixed size.
Because computers were first used for numerical calculations, early
computer engineers developed a representation for numbers in terms of
fixed-size chunks. Programming languages must mediate the gap between these
fixed-size representations and the true mathematics. Because using the
hardware representation for numbers makes a program's calculations as
efficient as possible, the designers and implementors of programming
languages adopted the hardware-based choice.

This intermezzo explains the fixed-size representation for numbers and its
consequences in some detail. The first subsection introduces a concrete
fixed-size representation for numbers, discusses what it implies for the
representation of numbers, and shows how to calculate with such
numbers. The second and third section illustrate the two most fundamental
problems of fixed-size number arithmetic: overflow and underflow,
respectively.

 \subsection{Fixed-size Number Arithmetic}

Suppose we can use four digits to represent numbers. If we represent
natural numbers, the representable range is 0 \ldots 9999. Alternatively we
could represent 10,000 fractions between 0 and 1 with that many digits. In
either case, this is a rather small range of numbers and not useful for
most scientific or business computations.  

We can represent a larger range of numbers if we use a different notation
for numbers instead. In science, for example, we encounter so-called
scientific notation, which represents numbers as two parts:
\begin{enumerate}
\item a {\sc {mantissa}}, which is a base number and
\item an {\sc {exponent}}, which is used to determine a 10-based factor.
\end{enumerate}
For pure scientific notation, the base is between 0 and 9. We relax this
constraint and just write numbers as 
$$ m \cdot 10^e$$
where {\it m\/} is the mantissa and {\it e\/} the exponent. For example,
one representation of 1200 with this scheme is
$$ 120 \cdot 10^1 \, ;$$
another one is 
$$ 12 \cdot 10^2 \, .$$
In general, a number has several equivalents in mantissa-exponent
representation. 

We can also use negative exponents, which adds fractions at the cost of one
extra piece of data: the sign of the exponent. For example, 
$$ 120 \cdot 10^{-1}$$
stands for 
$$ \frac{1}{1200} \, .$$
As before, the fraction has several different representations in the new
notation. 

To use a form of mantissa-exponent notation for our problem, we must decide
how many digits we wish to use for the representation of the mantissa and
how many for the exponent. Here we use two for each and a sign for the
exponent; other choices are possible. Given this decision, we can still
represent 0 as
$$ 0 \cdot 10^0 \, ,$$
The maximal number we can represent is 
$$ 99 \cdot 10^{99} \, ,$$
which is 99 followed by 99 0's. If we use negative exponents in addition to
positive ones, we can also represent 
$$ 01  \cdot 10^{-99} \, ,$$
which is a small number close to {0}.  Thus, we can now represent a vastly
larger range of numbers with four digits and a sign than before. But, this
improvement comes with its own problems.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt create-inex}\ {\tt :}\ {\bf N}\ {\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\tt inex}\/}}\\
\hbox{;; to make an instance of {{\tt inex}\/} after checking the appropriateness}
\\
\hbox{;; of the arguments}
\\
{\tt (d}\={\tt efine}\  {\tt (create-inex}\  {\tt m}\  {\tt s}\  {\tt e)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (and}\  {\tt (<=}\  {\tt 0}\  {\tt m}\  {\tt 99)}\  {\tt (<=}\  {\tt 0}\  {\tt e}\  {\tt 99)}\  {\tt (or}\  {\tt (=}\  {\tt s}\  {\tt +1)}\  {\tt (=}\  {\tt s}\  {\tt -1)))}
\\
\>\>\>{\tt (make-inex}\  {\tt m}\  {\tt s}\  {\tt e)\char'135}
\\
\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>{\tt (error}\  {\tt \char'047}{\tt make-inex}\  {\tt ``(}$<${\tt \char'075}\  {\tt 0}\  {\tt m}\  {\tt 99)\char'054}\  {\tt \char'053}{\tt 1}\  {\tt or}\  {\tt -1\char'054}\  {\tt (}$<${\tt \char'075}\  {\tt 0}\  {\tt e}\  {\tt 99)}\  {\tt expected'')\char'135}{\tt ))}
\\
\hbox{;; {\mbox{{\it inex}}{{}{\tt ->}{}}\mbox{{\it number}}\ {\tt :}\ {\tt inex}\ {}{\tt ->}{}\ {\tt number}\/}}
\\
\hbox{;; to convert an {{\tt inex}\/} into its numeric equivalent }
\\
{\tt (d}\={\tt efine}\  {\tt (}\mbox{{\it inex}}{{}{\tt ->}{}}\mbox{{\it number}}\  {\tt an-inex)}
\\
\>{\tt (*}\  \={\tt (inex-mantissa}\  {\tt an-inex)}\  
\\
\>\>{\tt (expt}\  {\tt 10}\  {\tt (*}\  {\tt (inex-sign}\  {\tt an-inex)}\  {\tt (inex-exponent}\  {\tt an-inex)))))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Functions for inexact representations} \label{fig:inexact-construct}
\hrule

To understand the problems, it is best to agree on a fixed representation
schema and to experiment with the number representations. Let's represent a
fixed-size number with a structure that has three fields:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt inex}\  {\tt (mantissa}\  {\tt sign}\  {\tt exponent))}\end{tabbing}\end{minipage}\end{center}
The first and last field contain the mantissa and exponent of the
number, the {{\tt sign}\/} field is {{\tt +1}\/} or {{\tt -1}\/} and
represents the sign of the exponent. This sign field enables us to
represent numbers between {{\tt 0}\/} and {{\tt 1}\/}.

Here is the data definition: 

\begin{quote} An {{\tt inex}\/} is a structure: \\
        \centerline{{{\tt (make-inex}\ {\tt m}\ {\tt s}\ {\tt e)}\/}}    where {{\tt m}\/}  and {{\tt e}\/} are natural numbers between {{\tt 0}\/}    and {{\tt 99}\/} and {{\tt s}\/} is {{\tt +1}\/} or {{\tt -1}\/}. \end{quote}

\noindent Because the conditions on the fields of an {{\tt inex}\/}
structure are so stringent, we use the function {{\tt create-inex}\/} to
create these structures. Figure~\ref{fig:inexact-construct} contains the
function definition for {{\tt create-inex}\/}. The figure also defines the
function {\mbox{{\it inex}}{{}{\tt ->}{}}\mbox{{\it number}}\/}, which turns {{\tt inex}\/}s into numbers
according to the principles of our new notation.

Let's translate the above example, {{\tt 1200}\/}, into our Scheme representation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (create-inex}\  {\tt 12}\  {\tt +1}\  {\tt 2)}\end{tabbing}\end{minipage}\end{center}
The alternative representation, $120 \cdot 10^1$, is illegal in our Scheme
world, however. If we evaluate 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (create-inex}\  {\tt 120}\  {\tt +1}\  {\tt 1)}\end{tabbing}\end{minipage}\end{center}
we get an error message because the arguments don't satisfy the stated data
contract. For other numbers, though, we can find two {{\tt inex}\/}
equivalents. One example is {{\tt 0.0000000000000000005}\/}, which we can
express as 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (create-inex}\  {\tt 50}\  {\tt -1}\  {\tt 20)}\\
\hbox{;and}
\\
{\tt (create-inex}\  {\tt 5}\  {\tt -1}\  {\tt 19)}
\end{tabbing}\end{minipage}\end{center}
Confirm the equivalence of these two representations with {\mbox{{\it inex}}{{}{\tt ->}{}}\mbox{{\it number}}\/}. 

The range of {{\tt inex}\/} numbers is vast:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt MAX-POSITIVE}\  {\tt (make-inex}\  {\tt 99}\  {\tt +1}\  {\tt 99))}\\
{\tt (define}\  {\tt MIN-POSITIVE}\  {\tt (make-inex}\  {\tt 1}\  {\tt -1}\  {\tt 99))}
\end{tabbing}\end{minipage}\end{center}
That is, we can represent large numbers that consist of up to 101 digits in
the standard decimal notation; we can also represent small positive
fractions smaller than {{\tt 1}\/} down to the fraction 1 over $10\ldots0$
with 99 zeros. The appearances, however, are deceiving.  Not all real
numbers in the range between 0 and {{\tt MAX-POSITIVE}\/} can be translated
into an {{\tt inex}\/} structure. In particular, any positive number less
than
$$ 10^{-99} $$ 
has no equivalent {{\tt inex}\/} structure. Similarly, the {{\tt inex}\/}
representation has gaps in the middle. For example, the successor of 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (create-inex}\  {\tt 12}\  {\tt +1}\  {\tt 2)}\end{tabbing}\end{minipage}\end{center}
is
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (create-inex}\  {\tt 13}\  {\tt +1}\  {\tt 2)}\end{tabbing}\end{minipage}\end{center}
The first {{\tt inex}\/} structure corresponds to {{\tt 1200}\/}, the second
one to {{\tt 1300}\/}.  Numbers in the middle, such as {{\tt 1240}\/} or
{{\tt 1260}\/}, can only be represented as one or the other. The standard
choice is to round the number and to the closest representable equivalent.
In short, we must approximate such mathematical numbers as we translate
into a chosen representation. 

Finally, we must also consider arithmetic operations on {{\tt inex}\/}
structures. Adding two {{\tt inex}\/} representations with the same exponent
means adding the two mantissas: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (inex+}\  \={\tt (create-inex}\  {\tt 1}\  {\tt +1}\  {\tt 0)}\\
\>{\tt (create-inex}\  {\tt 2}\  {\tt +1}\  {\tt 0))}\  
\\
{\tt =}\  {\tt (create-inex}\  {\tt 3}\  {\tt +1}\  {\tt 0)}
\end{tabbing}\end{minipage}\end{center}
Translated into mathematical notation, we have
$$\begin{array}{cl}
  & 1 \cdot 10^0 \\
+ & 2 \cdot 10^0 \\
 \hline
  & 3 \cdot 10^0 
\end{array}$$
When the addition of two mantissas yields too many digits, we may have to
find a suitable representation. Consider the example of adding
$$ 55\cdot 10^0 $$ 
to itself. Mathematically we get 
$$ 110\cdot 10^0 \, ,$$ 
but we can't just translate this number naively into our chosen
representation because $110 > 99$. The proper corrective action is to
represent the result as  
$$ 11\cdot 10^1 \, .$$ 
Or, translated into Scheme, we must ensure that {{\tt inex+}\/} computes as
follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (inex+}\  \={\tt (create-inex}\  {\tt 55}\  {\tt +1}\  {\tt 0)}\\
\>{\tt (create-inex}\  {\tt 55}\  {\tt +1}\  {\tt 0))}\  
\\
{\tt =}\  {\tt (create-inex}\  {\tt 11}\  {\tt +1}\  {\tt 1)}
\end{tabbing}\end{minipage}\end{center}
More generally, if the mantissa of the result is too large, we must divide
it by {{\tt 10}\/} and increase the exponent by one. 

Sometimes the result contains more mantissa digits than we can
represent. In those cases, {{\tt inex+}\/} must round to the closest
equivalent in the {{\tt inex}\/} world. For example:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (inex+}\  \={\tt (create-inex}\  {\tt 56}\  {\tt +1}\  {\tt 0)}\\
\>{\tt (create-inex}\  {\tt 56}\  {\tt +1}\  {\tt 0))}\  
\\
{\tt =}\  {\tt (create-inex}\  {\tt 11}\  {\tt +1}\  {\tt 1)}
\end{tabbing}\end{minipage}\end{center}
This correspons to the precise calculation: 
$$  56\cdot 10^{0} + 56\cdot 10^{0} = (56 + 56) \cdot 10^0 = 112 \cdot 10^0 $$
Because the result has too many mantissa digits, the integer division of
the result mantissa by {{\tt 10}\/} produces an approximate result:
$$ 11\cdot 10^{1} \, .$$
This is an example of the many approximations that make {\sc {inexact arithmetic}} inexact.

We can also multiply numbers represented as {{\tt inex}\/} structures. Recall that
$$\begin{array}{cl}
   & (a \cdot 10^n)  \cdot  (b \cdot 10^m) \\
 = & (a  \cdot  b)  \cdot  10^n  \cdot 10^m \\
 
 = & (a \cdot b) \cdot 10^{(n+m)}
\end{array}$$
Thus, we get: 
$$ 2\cdot 10^{+4} * 8\cdot 10^{+10} = 16\cdot 10^{+14} $$
or, in Scheme notation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (inex*}\  \={\tt (create-inex}\  {\tt 2}\  {\tt +1}\  {\tt 4)}\\
\>{\tt (create-inex}\  {\tt 8}\  {\tt +1}\  {\tt 10))}\  
\\
{\tt =}\  {\tt (make-inex}\  {\tt 16}\  {\tt +1}\  {\tt 14)}
\end{tabbing}\end{minipage}\end{center}
As with addition, things are not always straightforward. When the result
has too many significant digits in the mantissa, {{\tt inex*}\/} has to
increase the exponent:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (inex*}\  \={\tt (create-inex}\  {\tt 20}\  {\tt -1}\  {\tt 1)}\\
\>{\tt (create-inex}\  \  {\tt 5}\  {\tt +1}\  {\tt 4))}\  
\\
{\tt =}\  {\tt (create-inex}\  {\tt 10}\  {\tt +1}\  {\tt 4)}
\end{tabbing}\end{minipage}\end{center}
In the process, {{\tt inex*}\/} will introduce an approximation if the true
mantissa doesn't have an exact equivalent in the class of {{\tt inex}\/}
structures: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (inex*}\  \={\tt (create-inex}\  {\tt 27}\  {\tt -1}\  {\tt 1)}\\
\>{\tt (create-inex}\  \  {\tt 7}\  {\tt +1}\  {\tt 4))}\  
\\
{\tt =}\  {\tt (create-inex}\  {\tt 19}\  {\tt +1}\  {\tt 4)}
\end{tabbing}\end{minipage}\end{center}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 33.1.1}

 \label{ex:inex-arith-plus}
Develop the function {{\tt inex+}\/}, which adds {{\tt inex}\/}
representations that have the same exponent. The function must be able to
deal with examples that increase the exponent. Furthermore, it must signal
its own error if the result is out of range for {{\tt inex}\/}
representations. 

\medskip\noindent{\bf Challenge}:\  Extend {{\tt inex+}\/} so that it can deal with inputs
whose exponents differ by {{\tt 1}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (inex+}\  {\tt (create-inex}\  {\tt 1}\  {\tt +1}\  {\tt 0)}\  {\tt (create-inex}\  {\tt 1}\  {\tt -1}\  {\tt 1))}\\
{\tt =}\  {\tt (create-inex}\  {\tt 11}\  {\tt -1}\  {\tt 1)}
\end{tabbing}\end{minipage}\end{center}
Do not attempt to deal with larger classes of inputs than that without
reading the following subsection.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/inex-arith-plus.html}{\rule{3pt}{5pt}}


{\bf Exercise 33.1.2}

 \label{ex:inex-arith-times}
Develop the function {{\tt inex*}\/}, which multiplies {{\tt inex}\/}
representations.  The function must be able to deal with examples that
increase the exponent. Furthermore, it must signal its own error if the
result is out of range for {{\tt inex}\/}
representations.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/inex-arith-times.html}{\rule{3pt}{5pt}}  


{\bf Exercise 33.1.3}

 \label{ex:inex-arith-round}
The section illustrated how an inexact representation system for real
numbers has gaps. For example, 1240 was represented as {{\tt (create-inex}{ }{\tt 12}\ {\tt +1}\ {\tt 2)}\/} by rounding off the last significant digit of the mantissa. 
The problem is, round-off errors can accumulate. 

Develop the function {{\tt add}\/}, which adds up {{\tt n}\/} copies of
{{\tt \char'043}{\tt i1/185}\/}. What is the result for {{\tt (add}\ {\tt 185)}\/}? What should it
be? What happens if we multiply the result of the second expression with a
large number?

Develop the function {{\tt sub}\/}, which counts how often {{\tt 1/185}\/}
can be subtracted from the argument until the argument is {{\tt 0}\/}. How
often should the evaluation recur for {{\tt (sub}\ {\tt 1)}\/} and {{\tt (sub}{ }{\tt \char'043}{\tt i1.)}\/} is evaluated?  What happens in the second case?
Why?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/inex-arith-round.html}{\rule{3pt}{5pt}}



 \subsection{Overflow}

While the use of scientific notation expands the range of numbers we can
represent with fixed-size chunks of data, it still doesn't cover
arbitrarily large numbers. Some numbers are just too big to fit into a
fixed-size number representation. For example, $$99 \cdot 10^{500}$$ can't
be represented, because the exponent 500 won't fit into two digits, and the
mantissa is as large as it can be.

Numbers that are too large for our representation schema can arise during a
computation. For example, two numbers that we can represent can add up to a
number that we cannot represent: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (inex+}\  \={\tt (create-inex}\  {\tt 50}\  {\tt +1}\  {\tt 99)}\\
\>{\tt (create-inex}\  {\tt 50}\  {\tt +1}\  {\tt 99))}
\\
{\tt =}\  {\tt (create-inex}\  {\tt 100}\  {\tt +1}\  {\tt 99)}
\end{tabbing}\end{minipage}\end{center}
which violates the data contract, or
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (inex+}\  \={\tt (create-inex}\  {\tt 50}\  {\tt +1}\  {\tt 99)}\\
\>{\tt (create-inex}\  {\tt 50}\  {\tt +1}\  {\tt 99))}
\\
{\tt =}\  {\tt (create-inex}\  {\tt 10}\  {\tt +1}\  {\tt 100)}
\end{tabbing}\end{minipage}\end{center}
which also breaks the contract for {{\tt inex}\/} structures. When the
result of {{\tt inex}\/} arithmetic produces numbers that are too large to
be represented, we say (arithmetic) {\sc {overflow}} occurred.

When overflow occurs, some language implementations signal an error and
stop the computation.  Others designate some symbol, called infinity, for
all numbers that are too large. Arithmetic operations are aware of infinity
and propagate it. 

\medskip\noindent{\bf Negative Numbers}:\  If our {{\tt inex}\/} structures had a sign field
for the mantissa, then two negative numbers can add up to one that is so
negative that it can't be represented either. This is also called overflow,
though to emphasize the distinction people sometimes say overflow in the
negative direction.~{\rule{3pt}{5pt}}

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 33.2.1}

 \label{ex:flow-large}
DrScheme's inexact number system uses an infinity value to deal with
overflow.  Determine the integer {{\tt n}\/} such that {{\tt (expt}{ }{\tt \char'043}{\tt i10.}\ {\tt n)}\/} is still an inexact Scheme number and {{\tt (expt}\ {\tt \char'043}{\tt i10.}\ {\tt (+}\ {\tt n}{ }{\tt 1))}\/} is approximated with infinity. \noindent{\bf Hint:}  Use a function to compute
{{\tt n}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/flow-large.html}{\rule{3pt}{5pt}}


 \subsection{Underflow}

At the opposite end of the spectrum, we have already seen small numbers
that cannot be represented with {{\tt inex}\/} structures. For example,
$$10^{-500}$$ is not 0, but it's smaller than the smallest non-zero number we
can represent. An arithemtic {\sc {underflow}} arises when we multiply
two small numbers and the result is too small to fit into our class of
{{\tt inex}\/} structures:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (inex*}\  \={\tt (create-inex}\  {\tt 1}\  {\tt -1}\  {\tt 10)}\\
\>{\tt (create-inex}\  {\tt 1}\  {\tt -1}\  {\tt 99))}
\\
{\tt =}\  {\tt (create-inex}\  {\tt 1}\  {\tt -1}\  {\tt 109)}
\end{tabbing}\end{minipage}\end{center}
which causes an error. 

When underflow occurs, some language implementations signal an error;
others use 0 to approximate the result. An apporximation with 0 for
underflow is qualitatively different from our ealier kinds of
approximations. In approximating 1250 with {{\tt (create-inex}\ {\tt 12}\ {\tt +1}\ {\tt 2)}\/},
we approximated by dropping significant digits from the mantissa, but we
were left with a non-zero mantissa. The result is within 10\% of the number
we wanted to represent. Appromixating on underflow, however, means dropping
the entire mantissa. The result is not within a predictable precentage
range of the true result.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 33.3.1}

 \label{ex:flow-small}
DrScheme's inexact number system uses {{\tt \char'043}{\tt i0}\/} to approximate underflow.
Determine the smallest integer {{\tt n}\/} such that {{\tt (expt}\ {\tt \char'043}{\tt i10.}\ {\tt n)}\/}
is still an inexact Scheme number and {{\tt (expt}\ {\tt \char'043}{\tt i10.}\ {\tt (-}\ {\tt n}\ {\tt 1))}\/} is
approximated with {{\tt 0}\/}. \noindent{\bf Hint:}  Use a function to compute
{{\tt n}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/flow-small.html}{\rule{3pt}{5pt}}


 \subsection{DrScheme's Numbers}

Most programming languages support only inexact number representations (and
arithmetic) for both integers and reals. Scheme, in contrast, supports both
exact and inexact numbers and arithmetic. Of course, the base of the
representation is 2, not 10, because Scheme uses the underlying computer's
on-off machinery. 

As the note on page~\pageref{anote:numbers} explained, DrScheme's teaching
levels interpret all numbers in our programs as exact rationals, unless
they are prefixed with {{\tt \char'043}{\tt i}\/}. Some numeric operations, though,
produce inexact numbers. Plain Scheme, which is called Full Scheme in
DrScheme, interprets all numbers with a dot as inexact numbers;\footnote{We can force Full Scheme to interpret numbers with a dot as exact by prefixing the numbers with e.} it also prints inexact reals with just a dot, implying
that all such numbers are inexact and possibly distant from the actual
result. 


Scheme programmers can thus choose to use exact arithmetic or inexact
arithmetic as necessary. For example, numbers in financial statements
should always be interpreted as exact numbers; arithmetical operations on
such numbers should be as precise as possible. For some problems, however,
we may not wish to spend the extra time to produce exact
results. Scientific computations are a primary example. In such cases, we
may wish switch to inexact numbers and arithmetic. It is then important to
ask a numerical analyst to study our program so that we understand how much
the program's results can differ from the true results. Over the past few
decades, this advanced topic, called numerical analysis, has become a
subject of its own right in applied mathematics. Numerical analysts study
how badly the result of a computation can differ from the mathematically
correct result, and thus whether it is acceptable to trade speed of
computation for lack of accuracy. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

\htmladdimg{../icons/teacher.gif}~{\em The three exercises clarify why people use inexact arithmetic on computers and what risk they run into doing so. They should impress  both aspects of the coin on students.}

{\bf Exercise 33.4.1}

 \label{ex:dr-number}
Evaluate 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (expt}\  {\tt 1.001}\  {\tt 1e-12)}\end{tabbing}\end{minipage}\end{center}
in {\tt Full} Scheme (any variant) and in {\tt Intermediate Student}
Scheme.  Explain the observations.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/dr-number.html}{\rule{3pt}{5pt}} 

{\bf Exercise 33.4.2}

 \label{ex:inex-time}
Develop the function {{\tt my-expt}\/}, which raises one number to the power
of some integer. Using this function, conduct the following experiment. Add 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt inex}\  {\tt (+}\  {\tt 1}\  {\tt \char'043}{\tt i1e-12))}\\
{\tt (define}\  {\tt exac}\  {\tt (+}\  {\tt 1}\  {\tt 1e-12))}
\end{tabbing}\end{minipage}\end{center}
to the {\tt Definitions} window. What is {{\tt (my-expt}\ {\tt inex}\ {\tt 30)}\/}?
How about {{\tt (my-expt}\ {\tt exac}\ {\tt 30)}\/}? Which answer is more
useful?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/inex-time.html}{\rule{3pt}{5pt}}  

{\bf Exercise 33.4.3}

 \label{ex:janus}
When we add two inexact numbers of vastly different orders of magnitude, we
may get the larger one back as the result. For example, if we are using
only 15 significant digits, then we run into problems when adding numbers
which vary by more than a factor of $10^{16}$: 
$$ 1.0 \cdot 10^{16} + 1 = 1.00000000000000001 \cdot 10^{16}$$
but if the number system supports only 15 digits, the closest answer is
$10^{16}$. At first glance, this doesn't look too bad. After all, being
wrong by one part in $10^16$ (ten million billion) is close enough to the
accurate result. Unfortunately, this kind of problem can add up to huge
problems. 

Consider the following list of inexact numbers: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt JANUS}\\
\>{\tt (list}\  \={\tt \char'043}{\tt i31}
\\
\>\>{\tt \char'043}{\tt i2e+34}
\\
\>\>{\tt \char'043}{\tt i-1.2345678901235e+80}
\\
\>\>{\tt \char'043}{\tt i2749}
\\
\>\>{\tt \char'043}{\tt i-2939234}
\\
\>\>{\tt \char'043}{\tt i-2e+33}
\\
\>\>{\tt \char'043}{\tt i3.2e+270}
\\
\>\>{\tt \char'043}{\tt i17}
\\
\>\>{\tt \char'043}{\tt i-2.4e+270}
\\
\>\>{\tt \char'043}{\tt i4.2344294738446e+170}
\\
\>\>{\tt \char'043}{\tt i1}
\\
\>\>{\tt \char'043}{\tt i-8e+269}
\\
\>\>{\tt \char'043}{\tt i0}
\\
\>\>{\tt \char'043}{\tt i99))}
\end{tabbing}\end{minipage}\end{center}
Determine the values {{\tt (sum}\ {\tt JANUS)}\/} and {{\tt (sum}\ {\tt (reverse}\ {\tt JANUS))}\/}. 
Explain the difference. Can we trust computers?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/janus.html}{\rule{3pt}{5pt}}












%% BEGIN INLINED FILE: curriculum6.dl.tex


\newpage\part{Changing the State of Variables}\thispagestyle{empty}\newpage \label{part:state}

\section{Memory for Functions} \label{sec:memory}

No matter how often we use a function with one and the same argument, we
always get the same result.  Even an accumulator-style function produces
the same result every time we apply it to the same argument, as long as the
accumulator argument is also the same. Functions simply do not have any
memory about their past uses.

Many programs, though, must remember something about their past
uses. Recall that a program typically consists of several functions. In the
past we have always assumed that there is one main function and all others
are auxiliary and invisible to the user. In some cases, however, a user may
expect more than one service from a program, and each service is best
implemented as a function. When a program provides more than one function
as a service to the user, it is common that, for sheer convenince or
possibly because we add a graphical user interface, the functions must have
memory. 

Because this point is difficult to grasp in the abstract, we study some
examples. The first one concerns a program for managing telephone numbers
in an address book.  The standard address book software provides at east
two services: 
\begin{enumerate}
\item a service for looking up the phone number of some person; and

\item a service for adding a name and a phone number to the address book. 
\end{enumerate}
Based on our guidelines, the program provides two functions to the
user. The user can apply those functions in DrScheme's
{\tt Interactions} window to appropriate data. Or, we can develop a
graphical user interface with text fields and buttons so that the user
doesn't need to know anything about programming. Figure~\ref{fig:pb-gui}
displays such an interface. 

\hrule 
  \begin{rawhtml}  <center><img src=../icons/phonebook.gif></td></center>  \end{rawhtml}
\center{Figure: A phonebook GUI}\label{fig:pb-gui}
\hrule

The two services roughly correspond to two functions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt lookup}\ {\tt :}\ {\tt list-of-symbol-number-pairs}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt number}\ {\tt or}\ {\tt false}\/}}\\
\hbox{;; to lookup the number associated with {{\tt name}\/} in {{\tt pb}\/}}
\\
\hbox{;; if it doesn't find {{\tt name}\/}, the function produces {{\tt false}\/}}
\\
{\tt (define}\  {\tt (lookup}\  {\tt pb}\  {\tt name)}\  {\tt ...)}
\\
\hbox{;; {{\tt add-to-address-book}\ {\tt :}\ {\tt symbol}\ {\tt number}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\hbox{;; to add {{\tt name}\/} and {{\tt number}\/} to {{\tt address-book}\/}}
\\
{\tt (define}\  {\tt (add-to-address-book}\  {\tt name}\  {\tt number)}\  {\tt ...)}
\\
{\tt (d}\={\tt efine}\  {\tt ADDRESS-BOOK}\  
\\
\>{\tt (list}\  \={\tt (list}\  {\tt \char'047}{\tt Adam}\  {\tt 1)}
\\
\>\>{\tt (list}\  {\tt \char'047}{\tt Eve}\  {\tt 2)))}
\end{tabbing}\end{minipage}\end{center}
We also introduce a variable definition for maintaing a list of name-number
associations.

The first function is a variant of our very first recursive function. A
user applies it to a list of name-number associations, such as
{{\tt ADDRESS-BOOK}\/}, and a name. It produces a number, if the name is on
the list, or {{\tt false}\/} otherwise. The second function is radically
different from what we have seen.  The user would apply it to a name and a
number and, any future lookup of that name would then produce that number.

Let's imagine an interaction in DrScheme: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt (lookup}\  {\tt ADDRESS-BOOK}\  {\tt \char'047}{\tt Adam)}\\
{\tt 1}\  
\\
{\tt >}\  {\tt (lookup}\  {\tt ADDRESS-BOOK}\  {\tt \char'047}{\tt Dawn)}
\\
{\tt false}
\\
{\tt >}\  {\tt (add-to-address-book}\  {\tt \char'047}{\tt Dawn}\  {\tt 4)}
\\
{\tt >}\  {\tt (lookup}\  {\tt ADDRESS-BOOK}\  {\tt \char'047}{\tt Dawn)}
\\
{\tt 4}
\end{tabbing}\end{minipage}\end{center}
The first two confirm that {{\tt \char'047}{\tt Adam}\/} has the phone number {{\tt 1}\/}
and that we don't have phone number for {{\tt \char'047}{\tt Dawn}\/}. The third one adds
the phone number {{\tt 4}\/} for {{\tt \char'047}{\tt Dawn}\/} to
{{\tt ADDRESS-BOOK}\/}. And the last interaction shows that the very same
use of {{\tt lookup}\/} as before now produces the expected phone number. 

In the past, the only way we could have achieved this same effect is by
editing the definition of {{\tt ADDRESS-BOOK}\/}. But, we do not wish users
to edit our programs. Indeed, they shouldn't even have access to our
programs. We are therefore forced to provide an interface with a function
that permits such changes. We could go even further and implement the
graphical interface of figure~\ref{fig:pb-gui}. A dialog equivalent to the
above interaction would proceed as follows: 
\begin{enumerate}
\item Type {\tt Adam} into the text field, click the {\tt Lookup}
button, and ``1'' appears in the lower text field. 

\item Enter {\tt Dawn} into the text field, click the {\tt Lookup}
button, and some message concerning a missing number appears in the lower
text field.

\item Replace the message with ``4'' and click the {\tt Add} button. 

\item Erase the ``4'' from the lower text field, click the {\tt Lookup}
and the ``4'' shows up again. 
\end{enumerate}
In short, providing a convenient interface to a user forces us to develop a
program whose functions know about each other's usage history. 


\hrule 
  \begin{rawhtml}  <table  cellspacing=20 bgcolor=beige>  <tr> <td valign=top align=left><img src=../icons/next.gif></td>       <td align=left><img src=../icons/green.gif></td>       <td align=center><img src=../icons/yellow.gif></td>       <td align=right><img src=../icons/red.gif></td>  </tr>  </table>  \end{rawhtml}
\center{Figure: The three stages of a traffic light canvas and its GUI}\label{fig:traffic-light}
\hrule

The second example, a traffic light simulation, illustrates how a single
 function may need to have some memory.  Recall the function {{\tt next}\/}
 from exercise~\ref{ex:traffic-draw5}. It consumes the current color of a
 traffic light and, with the help of {{\tt clear-bulb}\/} and
 {{\tt draw-bulb}\/}, switches the state of the traffic light on a canvas to
 the next traffic color. The result is the next color. 

A user who wishes to switch the traffic light four times in a row, must
enter 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (next}\  {\tt (next}\  {\tt (next}\  {\tt (next}\  {\tt \char'047}{\tt red))))}\end{tabbing}\end{minipage}\end{center}
 into the {\tt Interactions} window. An even more convenient user
 interface, however, would provide a button that the user can click. 

Providing a button means to provide a call-back function that somehow knows
 about the current state of the traffic light and changes it. Let's call
 this function {{\tt next}\/}, too, but let's assume that it consumes no
 arguments. Here is an imaginary interaction using this function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt (next)}\\
{\tt >}\  {\tt (next)}
\\
{\tt >}\  {\tt (next)}
\end{tabbing}\end{minipage}\end{center}
Every time we apply {{\tt next}\/} to no arguments, it produces
 the invisible value and simulates the switch of state in the traffic light on
 the canvas. In other words, the canvas cycles through the three states
 depicted in figure~\ref{fig:traffic-light}. Equivalently, we can have a
 user click the ``NEXT'' button three times, which would apply the next
 function and have the same visual effect. To accomplish this effect, 
 the use of {{\tt next}\/} must affect its own future uses.


\hrule
  \begin{rawhtml}  <table cellspacing=20 bgcolor=beige>  <tr> <td align=left>  <img src=../icons/hang0.gif></td>       <td align=center><img src=../icons/hang1.gif></td>       <td align=right> <img src=../icons/hang2.gif></td>  </tr>  </table>  \end{rawhtml}
\hrulefill
\smallskip
  \begin{rawhtml}  <center><img src=../icons/hang-gui.gif></td></center>  \end{rawhtml}
\center{Figure: Three stages in the hangman game and its GUI}\label{fig:hangman-again}
\hrule

The final example concerns the {{\tt hangman}\/} game, which is also the
subject of section~\ref{sec:hangman}. The game program requires us to
develop three functions: {{\tt make-word}\/}, {{\tt reveal}\/}, and
{{\tt draw-next-part}\/}. We start the game by evaluating
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (hangman}\  {\tt make-word}\  {\tt reveal}\  {\tt draw-next-part)}\end{tabbing}\end{minipage}\end{center}
which picks a word, creates the graphical user interface of the lower half
of figure~\ref{fig:hangman-again}, and draws the leftmost picture in the
sequence of the upper half of the figure. The player can then choose a
letter from the choice menu in the GUI and click on the ``Check'' button to
determine whether the letter occurs in the word. If so, the
{{\tt hangman}\/} function reveals where the letter occurs; if not, it uses
our {{\tt draw-next-part}\/} function to draw the next stage in the hangman
picture. The more bad guesses the player makes, the more of the stick
figure appears in the picture (see top-half of
figure~\ref{fig:hangman-again}).

Our description suggests that the {{\tt hangman}\/} function in the
teachpack employs a callback function for the ``Check'' button. Let's call
this function {{\tt check}\/}. It consumes the letter and produces
{{\tt true}\/} if the check reveals new knowledge: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt (check}\  {\tt \char'047}{\tt b)}\\
{\tt true}
\end{tabbing}\end{minipage}\end{center}
If not, because the letter has already been guessed, the function produces
{{\tt false}\/} to indicate that the player didn't gain new knowledge: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt (check}\  {\tt \char'047}{\tt b)}\\
{\tt false}
\end{tabbing}\end{minipage}\end{center}
In this case, {{\tt check}\/} also employs {{\tt draw-next-part}\/} to draw
another part of the hangman figure. Of course, to accomplish this,
{{\tt hangman}\/} and {{\tt check}\/} must have some memory about how often
the ``Check'' button was used and how often it was used with a negative
result. 

With our current knowledge of Scheme, we cannot formulate functions such as
{{\tt add-to-address-book}\/}, {{\tt next}\/}, or {{\tt check}\/}. To fill
this gap in our knowledge, the next section introduces
{{\tt set!}\/}\footnote{This keyword is pronounced set-bang.}
expressions. This new form of expression permits functions to change the
value that a {{\tt define}\/}d variable represents. Using this new
construct, we can formulate Scheme functions that have memory. That is, we
can define functions that know something about their history and the
history of other functions.

\section{Assignment to Variables} \label{sec:set-bang}

A {{\bf set!}-exp\-res\-sion}, also known as an {\sc {assignment}}, has the
following shape:  
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set!}\  {\tt var}\  {\tt exp)}\end{tabbing}\end{minipage}\end{center}
It consists of a variable, the {\sl left-hand side}, and an expression,
called {\sl right-hand side}. The left-hand side of a {{\bf set!}-exp\-res\-sion} is a fixed
variable. In this book, we only use variables that are {{\tt define}\/}d,
either at the top-level or in a {\bf local}-exp\-res\-sion. A {{\bf set!}-exp\-res\-sion} may occur wherever
an expression is legal.

The value of a {{\bf set!}-exp\-res\-sion} is always the same and moreover
invisible. It is therefore irrelevant. What matters about a {{\tt set!}\/}
expression instead, is the {effect} of its evaluation. Specifically, for
the first step of the evaluation of a {{\bf set!}-exp\-res\-sion}, we
determine the value of {{\tt exp}\/}. Let's say this value is
{{\tt V}\/}. For the second step, we change the definition of {{\tt var}\/}
to 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt var}\  {\tt V)}\end{tabbing}\end{minipage}\end{center}
The {\sc {effect}} of this second step is that from this point on,
all references to {{\tt var}\/} in an evaluation replace {{\tt var}\/} by
{{\tt V}\/}.\footnote{We have already encountered several kinds of effects: drawing to a canvas, changing the text field in a GUI, the creating of files by teachpacks, and so on. These effects aren't as complex as those of {\bf set!} because they don't affect the program proper.} Its former value is lost. 

Understanding the true nature of assignments is difficult. We
therefore first consider a simple though useless example.

\subsection{Simple Assignments at Work} \label{sec:simple-set}

Consider the following definition and expression: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt z}\  {\tt (set!}\  {\tt x}\  {\tt (+}\  {\tt x}\  {\tt 2))))}
\\
\>{\tt x)}
\end{tabbing}\end{minipage}\end{center}
The definition says that {{\tt x}\/} stands for {{\tt 3}\/}. The
{{\bf local}-exp\-res\-sion} introduces a definition for {{\tt z}\/}. Its body
is {{\tt x}\/} so, in the past, the value of this {{\bf local}-exp\-res\-sion}
would have been {{\tt 3}\/} (if anything). Now, with {{\tt set!}\/} in the
language, this is no longer true. To understand what happens, we must
rewrite the program step by step until we have a final answer.

The first step in the evaluation lifts the {{\tt local}\/} definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (define}\  {\tt z}\  {\tt (set!}\  {\tt x}\  {\tt (+}\  {\tt x}\  {\tt 2)))}
\\
{\tt x}
\end{tabbing}\end{minipage}\end{center}
Next we must determine the value of {{\tt (set!}\ {\tt x}\ {\tt (+}\ {\tt x}\ {\tt 2))}\/}. According to
the general explanation of {{\tt set!}\/}, this requires the evaluation of
the right-hand side of the assignment: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (define}\  {\tt z}\  {\tt (set!}\  {\tt x}\  {\tt 5))}
\\
{\tt x}
\end{tabbing}\end{minipage}\end{center}
That value is {{\tt 5}\/} because the current value of {{\tt x}\/} is
{{\tt 3}\/}. 

Finally, the general explanation says that the effect of the {{\tt set!}\/}
expression is to change the value that the left-hand side variable
represents. In our example this means that from now on, {{\tt x}\/} is no
longer {{\tt 3}\/} but {{\tt 5}\/}. The best way to express this change is to
modify the definition of {{\tt x}\/} for the next step:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 5)}\\
{\tt (define}\  {\tt z}\  {\tt (}{\sf void}{\tt ))}
\\
{\tt x}
\end{tabbing}\end{minipage}\end{center}
The value of {{\tt set!}\/} is {{\tt (}{\sf void}{\tt )}\/}, the invisible value. By
replacing the {{\bf set!}-exp\-res\-sion} with the invisible value, we indicate that its
evaluation is finished.

At this point, it is easy to see that the result is {{\tt 5}\/}. The first
definition says that {{\tt x}\/} currently represents {{\tt 5}\/}, and the
last expression is {{\tt x}\/}. Hence, the value of the function evaluation
is {{\tt 5}\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 35.1.1}

 \label{ex:set-syntax}
Consider the following: 
\begin{enumerate}
\item 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set!}\  {\tt x}\  {\tt 5)}\end{tabbing}\end{minipage}\end{center}

\item 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (set!}\  {\tt (+}\  {\tt x}\  {\tt 1)}\  {\tt 5)}
\end{tabbing}\end{minipage}\end{center}

\item 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (define}\  {\tt y}\  {\tt 7)}
\\
{\tt (define}\  {\tt z}\  {\tt false)}
\\
{\tt (set!}\  {\tt (if}\  {\tt z}\  {\tt x}\  {\tt y)}\  {\tt 5)}
\end{tabbing}\end{minipage}\end{center}
\end{enumerate}
Which ones are syntactically legal programs? Which ones are
illegal?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:set-syntax.html}{\rule{3pt}{5pt}}


{\bf Exercise 35.1.2}

 \label{ex:set-simple0}
Evaluate the following program: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 1)}\\
{\tt (define}\  {\tt y}\  {\tt 1)}
\\
{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt u}\  {\tt (set!}\  {\tt x}\  {\tt (+}\  {\tt x}\  {\tt 1)))}
\\
\>\>{\tt (define}\  {\tt v}\  {\tt (set!}\  {\tt y}\  {\tt (-}\  {\tt y}\  {\tt 1))))}
\\
\>{\tt (*}\  {\tt x}\  {\tt y))}
\end{tabbing}\end{minipage}\end{center}
If {{\tt set!}\/} were not a part of the language, what could we say about
the result of the {{\bf local}-exp\-res\-sion}? That is, consider the skeleton 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 1)}\\
{\tt (define}\  {\tt y}\  {\tt 1)}
\\
{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt u}\  {\tt ...)}
\\
\>\>{\tt (define}\  {\tt v}\  {\tt ...))}
\\
\>{\tt (*}\  {\tt x}\  {\tt y))}
\end{tabbing}\end{minipage}\end{center}
where the right-hand sides of the definitions have been removed. What would
this expression have produced before the introduction of
{{\bf set!}-exp\-res\-sion}s?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:set-simple0.html}{\rule{3pt}{5pt}} 


\subsection{Sequencing Expression Evaluations} \label{sec:seq-time}

The hand-evaluation shows that the {{\tt local}\/} definition for {{\tt z}\/}
serves to evaluate a {{\bf set!}-exp\-res\-sion} and ``to throw away'' its
value. After all, a {{\tt set!}\/}'s true purpose is to change a definition
and not to generate a value. Because this situation is quite common, Scheme
also provides the {{\bf begin}-exp\-res\-sion}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (begin}\  \={\tt exp-1}\\
\>{\tt ...}
\\
\>{\tt exp-n}
\\
\>{\tt exp)}
\end{tabbing}\end{minipage}\end{center}
A {{\bf begin}-exp\-res\-sion} consists of the keyword {{\tt begin}\/} followed
by a sequence of $n+1$ expressions. The evaluation determines the values of
all expressions, in order, and then throws away the first $n$. The value of
the last expression is the value of the entire {{\bf begin}-exp\-res\-sion}. 
In general, the first $n$ subexpressions in a {{\bf begin}-exp\-res\-sion}
change some definitions; only the last one has an interesting value.

We can now rewrite our first sample program with {{\tt set!}\/} into
a short expression: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (begin}\  \={\tt (set!}\  {\tt x}\  {\tt (+}\  {\tt x}\  {\tt 2))}
\\
\>{\tt x)}
\end{tabbing}\end{minipage}\end{center}
The use of {{\tt begin}\/} not only simplifies the program, it also suggests
a straight-line ordering of the evaluation. 

The hand-evaluation also shows that the evaluation of {{\tt set!}\/}
expressions introduce additional timing constraints. More concretely, the
above evaluation consists of two parts: the one before and the one after
the assignment exerted its effect on the state of the definitions. Before
we introduced assignments, we could replace a variable by its value or a
function application by the function's body whenever we wished. Now, we
must wait until we truly need the value of a variable before we perform the
substitution. After all, definitions may change.

While some partial ordering is always a part of computation, the timing
constraints of {{\tt set!}\/} are new. By altering a definition, an
assignment ``destroys'' the current value. Unless the programmer carefully
plans the arrangement of assignments, such an action may be fatal. The
exercises illustrate the problem in more detail.

\htmladdimg{../icons/teacher.gif}~{\em  Finally, an evaluation is no longer a manipulation of a single expression. Instead, we must rewrite the {\em entire\/} program, that is, the definitions and the expression, when we wish to evaluate a program. }

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 35.2.1}

 \label{ex:set-simple1}
Evaluate the following program by hand: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 1)}\\
{\tt (define}\  {\tt y}\  {\tt 1)}
\\
{\tt (begin}\  \={\tt (set!}\  {\tt x}\  {\tt (+}\  {\tt x}\  {\tt 1))}
\\
\>{\tt (set!}\  {\tt y}\  {\tt (-}\  {\tt y}\  {\tt 1))}
\\
\>{\tt (*}\  {\tt x}\  {\tt y))}\  \  \  \  \  \  \  
\end{tabbing}\end{minipage}\end{center}
How many time periods can we distinguish in this hand-evaluation?

Compare this with the evaluation of 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt a}\  {\tt 5)}\\
{\tt (*}\  {\tt (+}\  {\tt a}\  {\tt 1)}\  {\tt (-}\  {\tt a}\  {\tt 1)))}\  \  \  \  \  \  \  
\end{tabbing}\end{minipage}\end{center}
Does the nesting imply an ordering among our calculations? Does the order
of addition and subtraction matter?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:set-simple1.html}{\rule{3pt}{5pt}}


{\bf Exercise 35.2.2}

 \label{ex:set-simple2}
Evaluate the following program by hand: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (define}\  {\tt y}\  {\tt 5)}
\\
{\tt (begin}\  \={\tt (set!}\  {\tt x}\  {\tt y)}
\\
\>{\tt (set!}\  {\tt y}\  {\tt x)}
\\
\>{\tt (list}\  {\tt x}\  {\tt y))}\  
\end{tabbing}\end{minipage}\end{center}
How many time periods can we distinguish in this hand-evaluation?

Now evaluate the following: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (define}\  {\tt y}\  {\tt 5)}
\\
{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt z}\  {\tt x))}
\\
\>{\tt (begin}\  \={\tt (set!}\  {\tt x}\  {\tt y)}
\\
\>\>{\tt (set!}\  {\tt y}\  {\tt z)}
\\
\>\>{\tt (list}\  {\tt x}\  {\tt y)))}
\end{tabbing}\end{minipage}\end{center}
Is it true that the definition of {{\tt x}\/} contains the initial value of
{{\tt y}\/} and {{\tt y}\/} contains the initial value of {{\tt x}\/} after
the two {{\bf set!}-exp\-res\-sion}s are evaluated, no matter what the initial
values are?

Discuss what the two examples teach us about time and ``destruction of
values'' in definitions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:set-simple2.html}{\rule{3pt}{5pt}}


{\bf Exercise 35.2.3}

 \label{ex:set-simple3}
Evaluate the following program by hand: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (define}\  {\tt y}\  {\tt 5)}
\\
{\tt (b}\={\tt egin}\  
\\
\>{\tt (set!}\  {\tt x}\  {\tt y)}
\\
\>{\tt (set!}\  {\tt y}\  {\tt (+}\  {\tt y}\  {\tt 2))}
\\
\>{\tt (set!}\  {\tt x}\  {\tt 3)}
\\
\>{\tt (list}\  {\tt x}\  {\tt y))}
\end{tabbing}\end{minipage}\end{center}
How many time intervals must we distinguish in this hand-evaluation?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:set-simple3.html}{\rule{3pt}{5pt}}


\subsection{Assignments and Functions} \label{sec:assign-func}

An assignment can also occur in a function body:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (define}\  {\tt y}\  {\tt 5)}
\\
{\tt (d}\={\tt efine}\  {\tt (swap-x-y}\  {\tt x0}\  {\tt y0)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt x}\  {\tt y0)}
\\
\>\>{\tt (set!}\  {\tt y}\  {\tt x0)))}
\\
{\tt (swap-x-y}\  {\tt x}\  {\tt y)}
\end{tabbing}\end{minipage}\end{center}
Here the function {{\tt swap-x-y}\/} consumes two values and performs two
{{\tt set!}\/}s. 

Let us see how the evaluation works. Because {{\tt (swap-x-y}\ {\tt x}\ {\tt y)}\/} is a
function application, we need to evaluate the arguments, which are plain
variables here. So we replace the variables with their (current) values:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (define}\  {\tt y}\  {\tt 5)}
\\
{\tt (d}\={\tt efine}\  {\tt (swap-x-y}\  {\tt x0}\  {\tt y0)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt x}\  {\tt y0)}
\\
\>\>{\tt (set!}\  {\tt y}\  {\tt x0)))}
\\
{\tt (swap-x-y}\  {\tt 3}\  {\tt 5)}
\end{tabbing}\end{minipage}\end{center}
From here we proceed with the usual substitution rule for application: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (define}\  {\tt y}\  {\tt 5)}
\\
{\tt (d}\={\tt efine}\  {\tt (swap-x-y}\  {\tt x0}\  {\tt y0)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt x}\  {\tt y0)}
\\
\>\>{\tt (set!}\  {\tt y}\  {\tt x0)))}
\\
{\tt (b}\={\tt egin}
\\
\>{\tt (set!}\  {\tt x}\  {\tt 5)}
\\
\>{\tt (set!}\  {\tt y}\  {\tt 3))}
\end{tabbing}\end{minipage}\end{center}
That is, the application is now replaced by an assignment of {{\tt x}\/} to
the current value of {{\tt y}\/} and of {{\tt y}\/} to the current value of
{{\tt x}\/}. 

The next two steps are also the last ones and they thus accomplish what the
name of the function suggests:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 5)}\\
{\tt (define}\  {\tt y}\  {\tt 3)}
\\
{\tt (d}\={\tt efine}\  {\tt (swap-x-y}\  {\tt x0}\  {\tt y0)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt x}\  {\tt y0)}
\\
\>\>{\tt (set!}\  {\tt y}\  {\tt x0)))}
\\
{\tt (}{\sf void}{\tt )}
\end{tabbing}\end{minipage}\end{center}
The value of the application is invisible because the last expression
evaluated was a {{\bf set!}-exp\-res\-sion}. 

In summary, functions with {{\tt set!}\/} have results and effects. The
result may be invisible. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 35.3.1}

 \label{ex:set-syntax2}
Consider the following function definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt x}\  {\tt y)}\\
\>{\tt (b}\={\tt egin}\  
\\
\>\>{\tt (set!}\  {\tt x}\  {\tt y)}
\\
\>\>{\tt y))}
\end{tabbing}\end{minipage}\end{center}
Is it syntactically legal or illegal?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:set-syntax2.html}{\rule{3pt}{5pt}}


{\bf Exercise 35.3.2}

 \label{ex:set-simple4}
Evaluate the following program by hand: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 3)}\\
{\tt (d}\={\tt efine}\  {\tt (increase-x)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt x}\  {\tt (+}\  {\tt x}\  {\tt 1))}
\\
\>\>{\tt x))}
\\
{\tt (increase-x)}
\\
{\tt (increase-x)}
\\
{\tt (increase-x)}
\end{tabbing}\end{minipage}\end{center}
What is the result? What is {{\tt increase-x}\/}'s effect?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:set-simple4.html}{\rule{3pt}{5pt}}


{\bf Exercise 35.3.3}

 \label{ex:set-simple5}
Evaluate the following program by hand: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 0)}\\
{\tt (d}\={\tt efine}\  {\tt (switch-x)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt x}\  {\tt (-}\  {\tt x}\  {\tt 1))}
\\
\>\>{\tt x))}
\\
{\tt (switch-x)}
\\
{\tt (switch-x)}
\\
{\tt (switch-x)}
\end{tabbing}\end{minipage}\end{center}
What is the result? What is {{\tt switch-x}\/}'s effect?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:set-simple5.html}{\rule{3pt}{5pt}}


{\bf Exercise 35.3.4}

 \label{ex:set-simple6}
Evaluate the following program by hand: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt x}\  {\tt 0)}\\
{\tt (define}\  {\tt y}\  {\tt 1)}
\\
{\tt (d}\={\tt efine}\  {\tt (change-to-3}\  {\tt z)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt y}\  {\tt 3)}
\\
\>\>{\tt z))}
\\
{\tt (change-to-3}\  {\tt x)}
\end{tabbing}\end{minipage}\end{center}
What is the effect of {{\tt change-to-3}\/}? What is its
result?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ex:set-simple6.html}{\rule{3pt}{5pt}} 


\subsection{A First Useful Example} \label{sec:state-example}

Let's take a look at the definitions in figure~\ref{fig:ab}. The function
{{\tt add-to-address-book}\/} consumes a symbol and a number. The former
represents a name, the latter a phone number.  Its body contains a
{{\bf set!}-exp\-res\-sion} for {{\tt address-book}\/}, a variable {{\tt define}\/}d at
top-level variable. The function {{\tt lookup}\/} consumes an address book
and a name; its result is the matching phone number or {{\tt false}\/}, if
the name is not in {{\tt address-book}\/}.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (define}\  {\tt address-book}\  {\tt empty)}
\\
\hbox{;; {{\tt add-to-address-book}\ {\tt :}\ {\tt symbol}\ {\tt number}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (add-to-address-book}\  {\tt name}\  {\tt phone)}
\\
\>{\tt (set!}\  {\tt address-book}\  {\tt (cons}\  {\tt (list}\  {\tt name}\  {\tt phone)}\  {\tt address-book)))}
\end{tabbing}\end{minipage}\end{center} 

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt lookup}\ {\tt :}\ {\tt symbol}\ {\tt (listof}\ {\tt (list}\ {\tt symbol}\ {\tt number))}\ {}{\tt ->}{}\ {\tt number}\ {\tt or}\ {\tt false}\/}}\\
\hbox{;; to lookup the phone number for {{\tt name}\/} in {{\tt ab}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (lookup}\  {\tt name}\  {\tt ab)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt ab)}\  {\sf false}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (first}\  {\tt (first}\  {\tt ab))}\  {\tt name)}
\\
\>\>\>\>{\tt (second}\  {\tt (first}\  {\tt ab))\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (lookup}\  {\tt name}\  {\tt (rest}\  {\tt ab))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The basic address-book program} \label{fig:ab}
\hrule

Using {{\tt lookup}\/}, we can study the effect of the {{\tt set!}\/}
expression in {{\tt add-to-address-book}\/}. Supposed we evaluate
{{\tt (lookup}\ {\tt \char'047}{\tt Adam}\ {\tt address-book)}\/} with the given definitions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (lookup}\  {\tt \char'047}{\tt Adam}\  {\tt address-book)}\\
{\tt =}\  {\tt (lookup}\  {\tt \char'047}{\tt Adam}\  {\tt empty)}
\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (empty?}\  {\tt empty)}\  {\sf false}{\tt \char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt ...\char'135}{\tt )}
\\
{\tt =}\  {\sf false}
\end{tabbing}\end{minipage}\end{center}
Because {{\tt address-book}\/} is {{\tt empty}\/}, we get {{\tt false}\/}, and
the calculation is straightforward. 

Now let's evaluate the following in the {\tt Interactions} window:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (begin}\  \={\tt (add-to-address-book}\  {\tt \char'047}{\tt Adam}\  {\tt 1)}
\\
\>{\tt (add-to-address-book}\  {\tt \char'047}{\tt Eve}\  {\tt 2)}
\\
\>{\tt (add-to-address-book}\  {\tt \char'047}{\tt Chris}\  {\tt 6145384))}
\end{tabbing}\end{minipage}\end{center} 
The first subexpression is a plain function application. So, the first step
relies on the usual law of substitution:\footnote{Because the calculation does not affect the function definitions, we do not include them in the calculation here. This convention saves space and time, but it should be used carefully.}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (define}\  {\tt address-book}\  {\tt empty)}
\\
{\tt (begin}\  \={\tt (set!}\  {\tt address-book}\  {\tt (cons}\  {\tt (list}\  {\tt \char'047}{\tt Adam}\  {\tt 1)}\  {\tt address-book))}
\\
\>{\tt (add-to-address-book}\  {\tt \char'047}{\tt Eve}\  {\tt 2)}
\\
\>{\tt (add-to-address-book}\  {\tt \char'047}{\tt Chris}\  {\tt 6145384))}
\end{tabbing}\end{minipage}\end{center}
The next expression to be evaluated is the {{\bf set!}-exp\-res\-sion} that is
nested in the {{\bf begin}-exp\-res\-sion}s, in particular its right-hand
side. The first argument to {{\tt cons}\/} is a value, but the second one is
still a variable whose  current value is {{\tt empty}\/}. With this, we
can see what happens next:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (define}\  {\tt address-book}\  {\tt empty)}
\\
{\tt (begin}\  \={\tt (set!}\  {\tt address-book}\  {\tt (cons}\  {\tt (list}\  {\tt \char'047}{\tt Adam}\  {\tt 1)}\  {\tt empty))}
\\
\>{\tt (add-to-address-book}\  {\tt \char'047}{\tt Eve}\  {\tt 2)}
\\
\>{\tt (add-to-address-book}\  {\tt \char'047}{\tt Chris}\  {\tt 6145384))}
\end{tabbing}\end{minipage}\end{center}

At this point we are ready to evaluate the {{\bf set!}-exp\-res\-sion}.
Specifically, we change the definition of {{\tt address-book}\/} so that the
variable now stands for {{\tt (cons}\ {\tt (list}\ {\tt \char'047}{\tt Adam}\ {\tt 1)}\ {\tt empty)}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (d}\={\tt efine}\  {\tt address-book}
\\
\>{\tt (c}\={\tt ons}\  {\tt (list}\  {\tt \char'047}{\tt Adam}\  {\tt 1)}
\\
\>\>{\tt empty))}
\\
{\tt (begin}\  \={\tt (}{\sf void}{\tt )}
\\
\>{\tt (add-to-address-book}\  {\tt \char'047}{\tt Eve}\  {\tt 2)}
\\
\>{\tt (add-to-address-book}\  {\tt \char'047}{\tt Chris}\  {\tt 6145384))}
\end{tabbing}\end{minipage}\end{center}
The {{\bf begin}-exp\-res\-sion} throws away the invisible value.

Evaluating the remaining applications of {{\tt add-to-address-book}\/}
yields 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (d}\={\tt efine}\  {\tt address-book}
\\
\>{\tt (list}\  \={\tt (list}\  {\tt \char'047}{\tt Chris}\  {\tt 6145384)}
\\
\>\>{\tt (list}\  {\tt \char'047}{\tt Eve}\  {\tt 2)}
\\
\>\>{\tt (list}\  {\tt \char'047}{\tt Adam}\  {\tt 1)))}
\\
{\tt (}{\sf void}{\tt )}
\end{tabbing}\end{minipage}\end{center}
In short, the three applications turn {{\tt address-book}\/} into a list of
three pairs. 

If we now evaluate {{\tt (lookup}\ {\tt \char'047}{\tt Adam}\ {\tt address-book)}\/} in the
{\tt Interactions} window again, we get {{\tt 1}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (lookup}\  {\tt \char'047}{\tt Adam}\  {\tt address-book)}\\
{\tt =}\  {\tt (lookup}\  {\tt \char'047}{\tt Adam}\  {\tt (list}\  \={\tt (list}\  {\tt \char'047}{\tt Chris}\  {\tt 6145384)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt Eve}\  {\tt 2)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt Adam}\  {\tt 1))}
\\
{\tt =}\  {\tt ...}
\\
{\tt =}\  {\tt 1}
\end{tabbing}\end{minipage}\end{center}
The comparison of this evaluation and the one at the beginning of the
section shows how {{\tt set!}\/} changes the meaning of
{{\tt address-book}\/} over time and how the two functions,
{{\tt add-to-address-book}\/} and {{\tt lookup}\/}, implement the services
that we discussed in section~\ref{sec:memory}. The exercises show useful
this collaboration of two functions is in the context of a graphical user
interface. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 35.4.1}

 \label{ex:set-pb-remove}
The software for managing address books permits users to remove
entries. Develop the function  
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt remove}\ {\tt :}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt void}\/}}\\
{\tt (define}\  {\tt (remove}\  {\tt name)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
which changes {{\tt address-book}\/} so that all future {{\tt lookup}\/}s for
{{\tt name}\/} yield {{\sf false}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/set-pb-remove.html}{\rule{3pt}{5pt}}


{\bf Exercise 35.4.2}

 \label{ex:set-pb-gui}
The teachpack {\tt\bf phone-book.ss} implements a graphical user interface
based on the model-view pattern discussed in
section~\ref{sec:gui1}. Figure~\ref{fig:pb-gui} shows what the graphical
user interface offers:
\begin{enumerate}
\item a text-field for entering a name; 
\item a text-field for displaying the search result and for entering a phone number; 
\item a button for looking up the phone number for a name; 
\item a button for adding a name and a phone number; and 
\item a button for removing the phone number for a name. 
\end{enumerate}

Use the teachpack's {{\tt connect}\/} function to create a GUI for the
functions in this section and in exercise~\ref{ex:set-pb-remove}. 
The function has the following contract, purpose, and header: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt model-T}\ {\tt =}\ {\tt (button\char'045}\ {\tt control-event\char'045}\ {}{\tt ->}{}\ {\sf true}{\tt )}\/}}\\
\hbox{;; {{\tt connect}\ {\tt :}\ {\tt model-T}\ {\tt model-T}\ {\tt model-T}\ {}{\tt ->}{}\ {\sf true}\/}}
\\
{\tt (define}\  {\tt (connect}\  {\tt lookup-cb}\  {\tt change-cb}\  {\tt remove-cb)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
That is, it consumes three model functions and wires them up with the
GUI. The names of the parameters specify which call-back function goes with
which button. 

A model function may obtain the contents of the name field with
{{\tt (name-control)}\/} and the contents of the number field with
{{\tt (number-field)}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/set-pb-gui.html}{\rule{3pt}{5pt}}


\section{Designing Functions with Memory} \label{sec:design-memory}

Section~\ref{sec:memory} motivated the idea of functions with memory;
section~\ref{sec:set-bang} explained how variable definitions and
{{\tt set!}\/} can together achieve the effect of memory. It is now time
to discuss the design of programs with memory. 

Designing functions with memory requires three important steps: 
\begin{enumerate}
\item We must determine that a program requires memory. 

\item We must identify the data that goes into the memory. 

\item We must understand which of the services are supposed to modify the
memory and which are to use the memory. 
\end{enumerate}
The need for the first step is obvious. Once we have know that a program
requires memory, we must conduct a data analysis for the program's
memory. That is, we must figure out what kind of data the programs puts
into memory and retrieves from there. Finally, we must carefully design
those functions for the program that change the memory. The others are
those that use the variables (without modification); they are typically
designed with one of the receipes we have already discussed.

\subsection{The Need for Memory} \label{sec:state-need}

Programs need memory because we want them to work with users who know
little or nothing about programming. Even if we wanted users to employ
DrScheme's {\tt Interactions} window, we would organize our programs so
that each service corresponds to a function and the function collaborate
through memory. With graphical user interfaces, we are almost forced to
think of programs as a collection of collaborating functions attached to
various widgets in a window. Finally, even programs that work in physical
devices such as elevators or VCRs are forced to interact with the device in
some fixed way, and that often includes keeping around information about
the history of device-program interactions. In short, the interface between
the program and the rest of the world dictates whether a program needs
memory and what kind of memory it needs. 

Fortunately it is relatively easy to recognize when programs truly need
memory. As discussed already, there are two situations. The first involves
programs that provide more than one service to users. Each service
corresponds to a function. A user may apply these functions in DrScheme's
{\tt Interaction} window, or they may be applied in response to some
user action in a graphical user interface. The second involves a program
that provides a single service and is implemented with a single user-level
function. But the program may have to produce different answers when it is
applied to the same arguments.


Let us take a look at some concrete examples for each situation. Software
for managing an address book is a classical example of the first kind. In
sections~\ref{sec:memory} and~\ref{sec:set-bang}, we saw how one function
adds entries to the address book, and another one looks them up. Clearly,
the use of the ``addition service'' affects future uses of the ``lookup
service'' and therefore requires memory. Indeed, the memory in this case
corresponds to a natural physical object: the address book that people used
to keep in place of electronic notebooks. 

Next, consider a warehouse with a clerk that registers the items that
people deliver and pick up. Every time someone delivers an item, the clerk
enters it into a ledger; an inquiry for a specific item triggers a search
in the ledger; when someone picks up an item, the clerk removes it from the
ledger. If we were to provide a function for managing the ledger, the
program would have to offer three services: one for entering items, one for
searching in the ledger, and one for removing entries from the ledger. Of
course, we can't remove something that isn't in the warehouse, so the
program must ensure that the two services interact properly. The memory in
this program will be similar to the ledgers that warehouse clerks use (or
used), that is, a physical object.

The second class of memory need also has its classical examples. The
traffic light simulation mentioned in section~\ref{sec:memory} is one of
them. Recall that the description of the program {{\tt next}\/} says that
every time it is applied, it redraws the picture on a canvas according to
the common traffic rules. Because two evaluations of {{\tt (next)}\/} in a
row produce two different effects, this program needs memory.

For another example, take a look at the Scheme function {{\tt random}\/}.
 It consumes a natural number $n$ and produces a number between $0$ and
 $n-1$.  If we evaluate {{\tt (random}\ {\tt 10)}\/} twice in a row, we may or may
 not obtain the same digit. Again, to achieve this effect, the implementor
 of {{\tt random}\/} needed to equip the function with some memory.

\hrule



\noindent\begin{picture}(140,140)(+0,0)
\put(010,095){\mbox{number}}
\put(010,118){\mbox{name}}
\put(010,105){\vector(1,0){40}}
\put(010,115){\vector(1,0){40}}
\put(050,100){\begin{picture}(100,20) 
\put(000,000){\line(1,0){100}} 
\put(100,000){\line(0,1){20}} 
\put(100,020){\line(-1,0){100}} 
\put(000,020){\line(0,-1){20}} 
\put(20,5){\mbox{add entry}} \end{picture}} 
\put(099,100){\vector(0,-1){36}}    
\put(104,064){\vector(0,+1){36}}    
\put(100,060){ \begin{picture}(20,20) 
\put(00,00){\circle{10}} 
\put(10,-2){\mbox{phone book}} \end{picture}} 
\put(100,056){\vector(0,-1){36}}    
\put(050,000){\begin{picture}(100,20) 
\put(000,000){\line(1,0){100}} 
\put(100,000){\line(0,1){20}} 
\put(100,020){\line(-1,0){100}} 
\put(000,020){\line(0,-1){20}} 
\put(20,5){\mbox{lookup number}} \end{picture}}
\put(010,013){\mbox{name}}
\put(010,010){\vector(1,0){40}}
\end{picture}\qquad\qquad\qquad\rule{1pt}{120pt}\quad\begin{picture}(140,140)(+20,0)
\put(050,100){\begin{picture}(100,20) 
\put(000,000){\line(1,0){100}} 
\put(100,000){\line(0,1){20}} 
\put(100,020){\line(-1,0){100}} 
\put(000,020){\line(0,-1){20}} 
\put(20,5){\mbox{next}} \end{picture}} 
\put(099,100){\vector(0,-1){36}}    
\put(102,064){\vector(0,+1){36}}    
\put(100,060){ \begin{picture}(20,20) 
\put(00,00){\circle{10}} 
\put(10,-2){\mbox{current color}} \end{picture}} \end{picture}

\center{Figure: Organizational charts for programs with memory}\label{fig:set-chart}
\hrule

In general, as we analyze a problem statement, we should draw organization
charts. Figure~\ref{fig:set-chart} contains sample charts for the
phone-book and the traffic-light programs. They represent each service that
the program is to support with a rectangular box.  Arrows going into the
box indicate what kind of data a service consumes; outgoing arrows specify
the output. Memory is represented with circles. An arrow from a circle to a
box means that the service uses the memory as an input; an arrow to a
circle means that the service changes the memory. The two charts show that
services commonly use memory and change it.

\subsection{Memory and State Variables} \label{sec:state-variables}

Memory is implemented with variable definitions. The memory-using programs
we have seen used a single variable to represent the memory of a
function. In principle, a single variable is enough to implement all memory
needs, but this is usually inconvenient. Typically, the memory analysis
suggests how many variables we need and which services need which
variables. When memory changes, the corresponding variables assume a new
value or, put differently, the state of the variable declaration changes
and reflects the memory change over time. We therefore refer to variables
that implement memory as {\sc {state variables}}.

Every service in a program corresponds to a function that may employ
auxiliary functions. A service that changes the memory of a program is
implemented with a function that uses {{\tt set!}\/} on some of the state
variables. To understand how a function should change a state variable, we
need to know what kind of values the variable may represent and what its
purpose is.  In other words, we must develop a contract and a purpose
statement for state variables in the same manner in which we develop
contracts and purpose statements for function definitions.

Let us take a look at the address-book and the traffic-light examples. The
first one has one state variable: {{\tt address-book}\/}.  It is intended to
represent a list of entries, where each entry is a list of two items: a
name and a number. To document that {{\tt address-book}\/} may only
represent such lists, we add a contract as follows:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt address-book}\ {\tt :}\ {\tt (listof}\ {\tt (list}\ {\tt symbol}\ {\tt number))}\/}}\\
\hbox{;; to keep track of pairs of names and phone numbers}
\\
{\tt (define}\  {\tt address-book}\  {\tt empty)}
\end{tabbing}\end{minipage}\end{center}
Recall that {{\tt (listof}\ {\tt X)}\/} and {{\tt (list}\ {\tt Y}\ {\tt Z)}\/} are short-hands for
data definitions. By the definition of {{\tt (listof}\ {\tt X)}\/}, it is
permissible to use {{\tt empty}\/} as the initial value of
{{\tt address-book}\/}.

From the contract for the state variable, we can conclude that the following
assignment is nonsensical: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set!}\  {\tt address-book}\  {\tt 5)}\end{tabbing}\end{minipage}\end{center}
It sets {{\tt address-book}\/} to {{\tt 5}\/}, which is not a list. The
expression therefore violates the state variable's contract. But
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set!}\  {\tt address-book}\  {\tt empty)}\end{tabbing}\end{minipage}\end{center}
is proper, because it sets {{\tt address-book}\/} back to its initial
value. Here is a third assignment:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set!}\  {\tt address-book}\  {\tt (cons}\  {\tt (list}\  {\tt \char'047}{\tt Adam}\  {\tt 1)}\  {\tt address-book))}\end{tabbing}\end{minipage}\end{center}
It helps us gain some understanding how functions can change the value of
{{\tt address-book}\/} in a useful manner.  Because {{\tt address-book}\/}
stands for a list of lists, {{\tt (cons}\ {\tt (list}\ {\tt \char'047}{\tt Adam}\ {\tt 1)}\ {\tt address-book)}\/}
constructs a longer list of the right kind. Hence, the {{\tt set!}\/}
expression just changes the state variable to stand for a different value in
the class of {{\tt (listof}\ {\tt (list}\ {\tt symbol}\ {\tt number))}\/}.

A program that controls a traffic light should contain a variable
definition for the current color of the traffic light. This variable should
assume one of three values: {{\tt \char'047}{\tt red}\/}, {{\tt \char'047}{\tt green}\/}, or
{{\tt \char'047}{\tt yellow}\/}, which suggests a data definition: 

  \begin{quote} A {\sl {TL-color}} is either        {{\tt \char'047}{\tt green}\/}, {{\tt \char'047}{\tt yellow}\/}, or {{\tt \char'047}{\tt red}\/}. \end{quote} 

\noindent Here is the variable definition with matching contract
and purpose statement:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt current-color}\ {\tt :}\ {\tt TL-color}\/}}\\
\hbox{;; to keep track of the current color of the traffic light}
\\
{\tt (define}\  {\tt current-color}\  {\tt \char'047}{\tt red)}
\end{tabbing}\end{minipage}\end{center}
As before, the expression 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set!}\  {\tt current-color}\  {\tt 5)}\end{tabbing}\end{minipage}\end{center}
is nonsensical because {{\tt 5}\/} is not one of the three legitimate
symbols mentioned in the contract. In contrast,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt green)}\end{tabbing}\end{minipage}\end{center}
is perfectly okay. 

The right-hand side of an assignment does not have to consist of a value or
an expression that almost instantaneously produces a value. In many cases
it makes sense to use a function to compute the new value. Here is a
function that computes the next color for our traffic light:\label{pg:next-color}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt next-color}\ {\tt :}\ {\tt TL-color}\ {}{\tt ->}{}\ {\tt TL-color}\/}}\\
\hbox{;; to compute the next color for a traffic light }
\\
{\tt (d}\={\tt efine}\  {\tt (next-color}\  {\tt c)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt red}\  {\tt c)}\  {\tt \char'047}{\tt green\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt green}\  {\tt c)}\  {\tt \char'047}{\tt yellow\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt yellow}\  {\tt c)}\  {\tt \char'047}{\tt red\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Using this function, we can now write an assignment that switches the state
of {{\tt current-color}\/} appropriately:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set!}\  {\tt current-color}\  {\tt (next-color}\  {\tt current-color))}\end{tabbing}\end{minipage}\end{center}
Because {{\tt current-color}\/} is one of the three legitimate symbols, 
we can apply {{\tt next-color}\/} to the value of {{\tt current-color}\/}. 
The function also produces one of these three symbols, so that the next
state of {{\tt current-color}\/} is again proper. 

\subsection{Functions that Initialize Memory} \label{sec:state-init}

After we have developed contracts and purpose statements for the state
variables of a program, we immediately define a function that sets the state
variables to proper values. We call this function an {\sc {initialization function}} or an {\sc {initializer}}. A program's {{\tt initializer}\/} is
the first function that is used during an execution; a program may also
provide other means to invoke the initializer. 

For our current examples, the initializers are straightforward. Here is one
for the address-book example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt init-address-book}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/} }\\
{\tt (d}\={\tt efine}\  {\tt (init-address-book)}
\\
\>{\tt (set!}\  {\tt address-book}\  {\tt empty))}
\end{tabbing}\end{minipage}\end{center}
The one for traffic-light is equally trivial:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt init-traffic-light}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/} }\\
{\tt (d}\={\tt efine}\  {\tt (init-traffic-light)}
\\
\>{\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt red))}
\end{tabbing}\end{minipage}\end{center}
In setting {{\tt current-color}\/} to {{\tt \char'047}{\tt red}\/}, we follow a
conventional rule of engineering to put devices into their least harmful
state when starting it up.\footnote{A device should also go into the least harmful state when it detects an internal failure. Unfortunately, many  software engineers don't follow this rule.}

At first glance, these initializers don't seem to add much to our
programs. Both set the respective state variables to the values that are
their defined values. For both cases, however, it is easy to see that 
the initializer could do some additional useful work. The first one, for
example, could create and display the graphical user interface for an
address book; the second one could create and display a canvas that
displays the current state of the traffic light. 


\subsection{Functions that Change Memory} \label{sec:design-change}

Once we have the state variables and their initializers in place, we turn
our attention to the design of functions that modify a program's memory.
Unlike the functions in the preceding parts of the book, the
memory-changing functions not only consume and produce data, they also
affect the definitions of the state variables. We therefore speak of the
{\sc {effect}} that functions have on the state variables.

\hrule

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline Data Def.}: A {\sl {TL-color}} is either {{\tt \char'047}{\tt green}\/}, {{\tt \char'047}{\tt yellow}\/}, or {{\tt \char'047}{\tt red}\/}.}\\
\hbox{;; {\underline State Variable}: }
\\
\hbox{;; {{\tt current-color}\ {\tt :}\ {\tt TL-color}\/}}
\\
\hbox{;; to keep track of the current color of the traffic light}
\\
{\tt (define}\  {\tt current-color}\  {\tt \char'047}{\tt red)}
\\
\hbox{;; {\underline Contract}: {{\tt next}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\hbox{;; {\underline Purpose}: the function always produces {{\tt (}{\sf void}{\tt )}\/}}
\\
\hbox{;; {\underline Effect}: to change {{\tt current-color}\/} from {{\tt \char'047}{\tt green}\/} to {{\tt \char'047}{\tt yellow}\/}, }
\\
\hbox{;; {{\tt \char'047}{\tt yellow}\/} to {{\tt \char'047}{\tt red}\/}, and {{\tt \char'047}{\tt red}\/} to {{\tt \char'047}{\tt green}\/}}
\\
\hbox{;; {\underline Header}: omitted for this particular example}
\\
\hbox{;; {\underline Examples}: }
\\
\hbox{;; if {{\tt current-color}\/} is {{\tt \char'047}{\tt green}\/} and we evaluate {{\tt (next)}\/}, then {{\tt current-color}\/} is {{\tt \char'047}{\tt yellow}\/}}
\\
\hbox{;; if {{\tt current-color}\/} is {{\tt \char'047}{\tt yellow}\/} and we evaluate {{\tt (next)}\/}, then {{\tt current-color}\/} is {{\tt \char'047}{\tt red}\/}}
\\
\hbox{;; if {{\tt current-color}\/} is {{\tt \char'047}{\tt red}\/} and we evaluate {{\tt (next)}\/}, then {{\tt current-color}\/} is {{\tt \char'047}{\tt green}\/}}
\\
\hbox{;; {\underline Template}: data-directed on state-variable that is to be mutated}
\\
\hbox{;; {{\tt (define}\ {\tt (f)}\/}}
\\
\hbox{;; ~~{\ {\tt (cond}\/}}
\\
\hbox{;; \quad{\ {\tt \char'133}{\tt (symbol=?}\ {\tt \char'047}{\tt green}\ {\tt current-color)}\ {\tt (set!}\ {\tt current-color}\ {\tt ...)\char'135}\/}}
\\
\hbox{;; \quad{\ {\tt \char'133}{\tt (symbol=?}\ {\tt \char'047}{\tt yellow}\ {\tt current-color)}\ {\tt (set!}\ {\tt current-color}\ {\tt ...)\char'135}\/}}
\\
\hbox{;; \quad{\ {\tt \char'133}{\tt (symbol=?}\ {\tt \char'047}{\tt red}\ {\tt current-color)}\ {\tt (set!}\ {\tt current-color}\ {\tt ...)\char'135}{\tt ))}\/}}
\\
\hbox{;; {\underline Definition}:}
\\
{\tt (d}\={\tt efine}\  {\tt (next)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt green}\  {\tt current-color)}\  {\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt yellow)\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt yellow}\  {\tt current-color)}\  {\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt red)\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt red}\  {\tt current-color)}\  {\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt green)\char'135}{\tt ))}
\\
\>
\\
\hbox{;; {\underline Tests}:}
\\
{\tt (begin}\  {\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt green)}\  {\tt (next)}\  {\tt (symbol=?}\  {\tt current-color}\  {\tt \char'047}{\tt yellow))}
\\
{\tt (begin}\  {\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt yellow)}\  {\tt (next)}\  {\tt (symbol=?}\  {\tt current-color}\  {\tt \char'047}{\tt red))}
\\
{\tt (begin}\  {\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt red)}\  {\tt (next)}\  {\tt (symbol=?}\  {\tt current-color}\  {\tt \char'047}{\tt green))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The design recipe for state variables: a complete example} \label{fig:sv-recipe1-example}
\hrule

Let us now take a look at the stages of our most basic design recipe and
how we can accommodate effects on state variables: 
\begin{description}
\item[Data Analysis:] Even functions that affect the state of variables
consume and (possibly) produce data. Thus we still need to analyze how to
represent information and, if necessary, introduce structure and data
definitions. 

For example, the traffic-light example benefits from the data definition
for {{\tt TL-color}\/} (see above).

\item[Contract, Purpose, and Effect:] The first major change concerns the
second step. In addition to specifying what a function consumes and
produces, we must also write down which variables it affects and how it
affects those state variables. The effect of a function on state variables
must be consistent with the purpose statement of a variable. 

Consider the traffic-light example again. It requires a function that
switches the color of the traffic light in accordance with the traffic
laws. The function checks the variable {{\tt current-color}\/} and affects
its state. Here is how we should specify this function: 

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt next}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to change {{\tt current-color}\/} from {{\tt \char'047}{\tt green}\/} to {{\tt \char'047}{\tt yellow}\/}, }
\\
\hbox{;; {{\tt \char'047}{\tt yellow}\/} to {{\tt \char'047}{\tt red}\/}, and {{\tt \char'047}{\tt red}\/} to {{\tt \char'047}{\tt green}\/}}
\\
{\tt (define}\  {\tt (next)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

The function consumes no data and always produces the invisible value; in
Scheme this value is called {{\tt void}\/}. Because the function has no
purpose in the traditional sense, it is accompanied by an effect statement
only.

Here is the specification for {{\tt add-to-address-book}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt add-to-address-book}\ {\tt :}\ {\tt symbol}\ {\tt number}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to add {{\tt (list}\ {\tt name}\ {\tt phone)}\/} to the front of {{\tt address-book}\/}}
\\
{\tt (define}\  {\tt (add-to-address-book}\  {\tt name}\  {\tt phone)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
We can tell from the effect statement that the definition of
{{\tt address-book}\/} is modified in a fashion that's coherent with its
purpose statement and contract. 

\item[Program Examples:] Examples are as important as ever, but formulating
them has become more difficult. As before, we must develop examples that
illustrate the relationship between inputs and outputs, but, because
functions now have effects, we also need examples that illustrate those. 

Let us return to our first running example, the {{\tt next}\/} function for
traffic lights. It affects one state-variable: {{\tt current-color}\/}.
Because this variable can stand for one of three symbols, we can actually
characterize all of its possible effects with examples: 


\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; if {{\tt current-color}\/} is {{\tt \char'047}{\tt green}\/} and we evaluate {{\tt (next)}\/}, }\\
\hbox{;; then {{\tt current-color}\/} is {{\tt \char'047}{\tt yellow}\/} afterwards}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; if {{\tt current-color}\/} is {{\tt \char'047}{\tt yellow}\/} and we evaluate {{\tt (next)}\/}, }\\
\hbox{;; then {{\tt current-color}\/} is {{\tt \char'047}{\tt red}\/} afterwards}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; if {{\tt current-color}\/} is {{\tt \char'047}{\tt red}\/} and we evaluate {{\tt (next)}\/}, }\\
\hbox{;; then {{\tt current-color}\/} is {{\tt \char'047}{\tt green}\/} afterwards}
\end{tabbing}\end{minipage}\end{center}


In contrast, the state variable {{\tt address-book}\/} can stand for an
infinite number of values, so it is impossible to make up a comprehensive 
series of examples. But it is still important to state a few, because 
examples make it easier to develop the function body later: 


\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; if {{\tt address-book}\/} is {{\tt empty}\/} and }\\
\hbox{;; we evaluate {{\tt (add-to-address-book}\ {\tt \char'047}{\tt Adam}\ {\tt 1)}\/}, }
\\
\hbox{;; then {{\tt address-book}\/} is {{{\tt (list}\ {\tt (list}\ {\tt \char'047}{\tt Adam}\ {\tt 1))}\/}} afterwards. }
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; if {{\tt address-book}\/} is {{{\tt (list}\ {\tt (list}\ {\tt \char'047}{\tt Eve}\ {\tt 2))}\/}} and }\\
\hbox{;; we evaluate {{\tt (add-to-address-book}\ {\tt \char'047}{\tt Adam}\ {\tt 1)}\/}, }
\\
\hbox{;; then {{\tt address-book}\/} is {{{\tt (list}\ {\tt (list}\ {\tt \char'047}{\tt Adam}\ {\tt 1)}\ {\tt (list}\ {\tt \char'047}{\tt Eve}\ {\tt 2))}\/}} afterwards. }
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; if {{\tt address-book}\/} is {{{\tt (list}\ {\tt E-1}\ {\tt ...}\ {\tt E-2)}\/}} and }\\
\hbox{;; we evaluate {{\tt (add-to-address-book}\ {\tt \char'047}{\tt Adam}\ {\tt 1)}\/}, }
\\
\hbox{;; then {{\tt address-book}\/} is {{{\tt (list}\ {\tt (list}\ {\tt \char'047}{\tt Adam}\ {\tt 1)}\ {\tt E-1}\ {\tt ...}\ {\tt E-2)}\/}} afterwards. }
\end{tabbing}\end{minipage}\end{center}


Not surprisingly, the language of examples involves words of temporal
nature. After all, assignmentss emphasize the notion of time in
programming.

\medskip\noindent{\bf Warning}:\  The state variable is never a parameter of a function.~{\rule{3pt}{5pt}}

\item[The Template:] The template for state-changing functions is like that
of an ordinary function, but the body should also contain {{\tt set!}\/}
expressions to specify the state variables that are to be modified:

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (fun-for-state-change}\  {\tt x}\  {\tt y}\  {\tt z)}\\
\>{\tt (set!}\  {\tt a-state-variable}\  {\tt ...))}
\end{tabbing}\end{minipage}\end{center}

The computation of the next value for {{\tt a-state-variable}\/} can be left
to an auxiliary function, which consumes {{\tt x}\/}, {{\tt y}\/}, and
{{\tt z}\/}. Our two examples fit this pattern. 

On occasion, we should add selector and {{\bf cond}-exp\-res\-sion}s, based on
the data definitions for the function's inputs. Consider {{\tt next}\/}
again. The data definition for its input suggests a
{{\bf cond}-exp\-res\-sion}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (next)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt green}\  {\tt current-color)}\  {\tt (set!}\  {\tt current-color}\  {\tt ...)\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt yellow}\  {\tt current-color)}\  {\tt (set!}\  {\tt current-color}\  {\tt ...)\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt red}\  {\tt current-color)}\  {\tt (set!}\  {\tt current-color}\  {\tt ...)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
In this simple case, we can indeed go with either alternative and design a
proper program. 

\item[The Body:] As always, the development of the full function requires a
solid understanding of the examples, of how they are computed, and of the
template.  For functions with effects, the completion of the {{\tt set!}\/}
expression is the most demanding step. In some cases, the right-hand side
involves nothing but primitive operations, the function's parameters, and
the state variable (or several of them). In others, it is best to develop
an auxiliary function (without effect) that consumes the current value of
the state variable and the function's parameters and that produces the new
value of the state variable.

The function {{\tt add-to-address-book}\/} is an example of the first
kind. The right-hand side of the {\bf set!}-exp\-res\-sion\ consists of
{{\tt address-book}\/}, {{\tt cons}\/}, {{\tt list}\/}, and nothing else. 

The traffic-light example, in contrast, is a good basis to demonstrate
both choices. Here is a definition that is based on the template: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (next)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt green}\  {\tt current-color)}\  {\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt yellow)\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt yellow}\  {\tt current-color)}\  {\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt red)\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt red}\  {\tt current-color)}\  {\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt green)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Writing one based on an auxiliary function is also straightforward: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (next)}\\
\>{\tt (set!}\  {\tt current-color}\  {\tt (next-color}\  {\tt current-color)))}
\end{tabbing}\end{minipage}\end{center}
For the definition of {{\tt next-color}\/}, see
page~\pageref{pg:next-color}. 

\item[Testing:] In the past, we have tested functions by translating the
examples into boolean-valued expressions and by adding them to the bottom
of the {\tt Definitions} window. For functions with effects, we use a
similar approach, but to verify that functions have the desired effect on
state variables is a complex task.

There are two ways to test functions with efffects. First, we can set the
state variable into a desired state, apply the function, and then check
whether the functions has the desired result and effect. The {{\tt next}\/}
function is a particular good one for this approach. We characterized its
complete behavior with three examples. All three can be translated into 
{{\bf begin}-exp\-res\-sion}s that test as suggested. Here is one example:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (begin}\  \={\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt green)}\\
\>{\tt (next)}
\\
\>{\tt (symbol=?}\  {\tt current-color}\  {\tt \char'047}{\tt yellow))}
\end{tabbing}\end{minipage}\end{center}
Each line sets the state variable {{\tt current-color}\/} to the desired
color, evaluates {{\tt (next)}\/}, and then checks whether the effect is
appropriate. We can also do this for the {{\tt add-to-address-book}\/}
function, though the tests are less comprehensive than those for
{{\tt next}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (begin}\  \={\tt (set!}\  {\tt address-book}\  {\tt empty)}\\
\>{\tt (add-to-address-book}\  {\tt \char'047}{\tt Adam}\  {\tt 1)}
\\
\>{\tt (equal?}\  {\tt \char'047}{\tt ((Adam}\  {\tt 1))}\  {\tt address-book))}
\end{tabbing}\end{minipage}\end{center}
In this one test, we only check that {{\tt Adam}\/} and {{\tt 1}\/} were
properly included into the {{\tt empty}\/} list. 

Second, we can capture the value of a state variable before it is tested,
apply the memory-changing function, and then conduct appropriate tests. 
Consider the following expression: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt (\char'133}{\tt define}\  {\tt current-value-of}\  {\tt address-book\char'135}{\tt )}\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (add-to-address-book}\  {\tt \char'047}{\tt Adam}\  {\tt 1)}
\\
\>\>{\tt (equal?}\  {\tt (cons}\  {\tt (list}\  {\tt \char'047}{\tt Adam}\  {\tt 1)}\  {\tt current-value-of)}\  {\tt address-book)))}
\end{tabbing}\end{minipage}\end{center}
It defines {{\tt current-value-of}\/} to be the value of
{{\tt address-book}\/} at the beginning of the evaluation, and at the end
checks that the appropriate entry was added at the front and that nothing 
changed for the rest of the value. 

To conduct tests for functions with effects, especially tests of the second
kind, it is useful to abstract the test
expression into a function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt test-for-address-book}\ {\tt :}\ {\tt symbol}\ {\tt number}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether {{\tt add-to-address-book}\/} has the appropriate}
\\
\hbox{;; effect on {{\tt address-book}\/} and no more than that}
\\
\hbox{;; effect: same as {{\tt (add-to-address-book}\ {\tt name}\ {\tt number)}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (test-for-address-book}\  {\tt name}\  {\tt number)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (\char'133}{\tt define}\  {\tt current-value-of}\  {\tt address-book\char'135}{\tt )}
\\
\>\>{\tt (b}\={\tt egin}
\\
\>\>\>{\tt (add-to-address-book}\  {\tt name}\  {\tt number)}
\\
\>\>\>{\tt (equal?}\  \={\tt (cons}\  {\tt (list}\  {\tt name}\  {\tt number)}\  {\tt current-value-of)}\  
\\
\>\>\>\>{\tt address-book))))}
\end{tabbing}\end{minipage}\end{center}
Using this function, we can now easily test {{\tt add-to-address-book}\/}
several times and ensure for each test that its effects are appropriate: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (and}\  \={\tt (test-for-address-book}\  {\tt \char'047}{\tt Adam}\  {\tt 1)}
\\
\>{\tt (test-for-address-book}\  {\tt \char'047}{\tt Eve}\  {\tt 2)}
\\
\>{\tt (test-for-address-book}\  {\tt \char'047}{\tt Chris}\  {\tt 6145384))}
\end{tabbing}\end{minipage}\end{center} 
The {{\bf and}-exp\-res\-sion} guarantees that the test expressions are
evaluated in order and that all of them produce {{\tt true}\/}. 


\item[Future Reuse:] Once we have a complete, tested program, we should
remember its existence, {\em what\/} it computes, and what its {\em effects\/} are. We do not, however, need to remember {\em how\/} it
computes.  If we encounter a situation that calls for the same computation
and the same effects, we can reuse the program as if it were a primitive
operation. {\bf Warning}: In the presence of effects, it is much more
difficult to reuse a function than in the world of algebraic programs. 
\end{description}
Figures~\ref{fig:sv-recipe1-example} and~\ref{fig:sv-recipe2-example}
summarize our two running examples; the header in the first one is omitted 
because it is useless for the purpose and effect statements in this
particular case. 

\hrule

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline Data Def.}: lists of arbitrary length: {{\tt (listof}\ {\tt X)}\/}, lists of two items: {{\tt (list}\ {\tt Y}\ {\tt Z)}\/}}\\
\hbox{;; {\underline State Variable}: }
\\
\hbox{;; {{\tt address-book}\ {\tt :}\ {\tt (listof}\ {\tt (list}\ {\tt symbol}\ {\tt number))}\/}}
\\
\hbox{;; to keep track of pairs of names and phone numbers}
\\
{\tt (define}\  {\tt address-book}\  {\tt empty)}
\\
\hbox{;; {\underline Contract}: {{\tt add-to-address-book}\ {\tt :}\ {\tt symbol}\ {\tt number}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\hbox{;; {\underline Purpose}: the function always produces {{\tt (}{\sf void}{\tt )}\/}}
\\
\hbox{;; {\underline Effect}: to add {{\tt (list}\ {\tt name}\ {\tt phone)}\/} to the front of {{\tt address-book}\/}}
\\
\hbox{;; {\underline Header}: }
\\
\hbox{;; {{\tt (define}\ {\tt (add-to-address-book}\ {\tt name}\ {\tt phone)}\ {\tt ...)}\/}}
\\
\hbox{;; {\underline Examples}: }
\\
\hbox{;; if {{\tt address-book}\/} is {{{\tt empty}\/}} and we evaluate}
\\
\hbox{;; {{\tt (add-to-address-book}\ {\tt \char'047}{\tt Adam}\ {\tt 1)}\/}, {{\tt address-book}\/} is {{{\tt (list}\ {\tt (list}\ {\tt \char'047}{\tt Adam}\ {\tt 1))}\/}}.}
\\
\hbox{;; if {{\tt address-book}\/} is {{{\tt (list}\ {\tt (list}\ {\tt \char'047}{\tt Eve}\ {\tt 2))}\/}} and we evaluate}
\\
\hbox{;; {{\tt (add-to-address-book}\ {\tt \char'047}{\tt Adam}\ {\tt 1)}\/}, {{\tt address-book}\/} is {{{\tt (list}\ {\tt (list}\ {\tt \char'047}{\tt Adam}\ {\tt 1)}\ {\tt (list}\ {\tt \char'047}{\tt Eve}\ {\tt 2))}\/}}.}
\\
\hbox{;; if {{\tt address-book}\/} is {{{\tt (list}\ {\tt E-1}\ {\tt ...}\ {\tt E-2)}\/}} and we evaluate}
\\
\hbox{;; {{\tt (add-to-address-book}\ {\tt \char'047}{\tt Adam}\ {\tt 1)}\/}, {{\tt address-book}\/} is {{{\tt (list}\ {\tt (list}\ {\tt \char'047}{\tt Adam}\ {\tt 1)}\ {\tt E-1}\ {\tt ...}\ {\tt E-2)}\/}}.}
\\
\hbox{;; {\underline Template}: omitted}
\\
\hbox{;; {\underline Definition}:}
\\
{\tt (d}\={\tt efine}\  {\tt (add-to-address-book}\  {\tt name}\  {\tt phone)}
\\
\>{\tt (set!}\  {\tt address-book}\  {\tt (cons}\  {\tt (list}\  {\tt name}\  {\tt phone)}\  {\tt address-book)))}
\\
\>
\\
\hbox{;; {\underline Tests}:}
\\
{\tt (begin}\  \={\tt (set!}\  {\tt address-book}\  {\tt empty)}
\\
\>{\tt (add-to-address-book}\  {\tt \char'047}{\tt Adam}\  {\tt 1)}
\\
\>{\tt (equal?}\  {\tt \char'047}{\tt ((Adam}\  {\tt 1))}\  {\tt address-book))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: The design recipe for state variables: a second example} \label{fig:sv-recipe2-example}
\hrule

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 36.4.1}

 \label{ex:traffic-state}
Modify the traffic light program in figure~\ref{fig:sv-recipe1-example} to
draw the current state of the traffic light into a canvas. Start by adding
the initializer. Use the solutions for
section~\ref{sec:traffic-draw}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/traffic-state.html}{\rule{3pt}{5pt}}


{\bf Exercise 36.4.2}

 \label{ex:pb-state}
Modify the phone book program in figure~\ref{fig:sv-recipe2-example} so
that it offers a graphical user interface. Start by adding the
initializer. Use the solution of exercise~\ref{ex:set-pb-gui}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/pb-state.html}{\rule{3pt}{5pt}}


\section{Examples of Memory Usage} \label{sec:state-many-examples}

Designing programs with memory requires experience and practice, which, in
turn, come from studying examples. In this section we study three more
examples of programs that use memory. The first one illustrates the
importance of initializers; the second one demonstrates how to design
programs whose effects depend on conditions; and the last one shows how
effects can be useful in recursive functions.  The last two subsections
provide opportunities for practicing what we learned.

\subsection{Initializing State} \label{sec:color-state}

Recall the color-guessing game from exercise~\ref{ex:check-guess}.  One
player picks two colors for two squares; we call those ``targets''. The
other one tries to guess which color is assigned to which square; they are
guesses.  The first player's response to a guess is to compare the colors
and to produce one of the following answers:
\begin{enumerate}
\item {{\tt \char'047}{\tt perfect!}\/}, if the first target is equal to the first
guess and the second target is equal to the second guess; 

\item {{\tt \char'047}{\tt OneColorAtCorrectPosition}\/}, if the first guess is equal to
the first target or the second guess is equal to the second target; 

\item {{\tt \char'047}{\tt OneColorOccurs}\/}, if either of the guesses is one of the two
targets; 

\item and {{\tt \char'047}{\tt NothingCorrect}\/}, otherwise. 
\end{enumerate}
These four answers are the only answers that the first player gives. The
second player is to guess the two chosen target colors with as few guesses
as possible. 

To simplify the game, the choice of colors is limited: see the top of
figure~\ref{fig:master}. Our goal is to develop a program that plays the
role of the master player. That is, we want a program that picks the colors
and checks the guesses of the second player.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline Constants}:}\\
\hbox{;; the legitimate colors }
\\
{\tt (d}\={\tt efine}\  {\tt COLORS}
\\
\>{\tt (list}\  {\tt \char'047}{\tt black}\  {\tt \char'047}{\tt white}\  {\tt \char'047}{\tt red}\  {\tt \char'047}{\tt blue}\  {\tt \char'047}{\tt green}\  {\tt \char'047}{\tt gold}\  {\tt \char'047}{\tt pink}\  {\tt \char'047}{\tt orange}\  {\tt \char'047}{\tt purple}\  {\tt \char'047}{\tt navy))}
\\
\hbox{;; the number of colors}
\\
{\tt (define}\  {\tt COL\char'043}\  {\tt (length}\  {\tt COLORS))}
\\
\hbox{;; {\underline Data Definition}:}
\\
\hbox{;; A {\sl {color}} is a symbol on COLORS. }
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Guessing colors} \label{fig:master}
\hrule

The game description suggests that the program must offer two services: one
for setting up two target colors and another one for checking the
guesses. Naturally, each service corresponds to a function. Let's call the
first {{\tt master}\/} and the second one {{\tt master-check}\/}. 
Here is a possible dialogue, based on the two functions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt (master)}\\
{\tt >}\  {\tt (master-check}\  {\tt \char'047}{\tt red}\  {\tt \char'047}{\tt red)}
\\
{\tt \char'047}{\tt NothingCorrect}
\\
{\tt >}\  {\tt (master-check}\  {\tt \char'047}{\tt black}\  {\tt \char'047}{\tt pink)}
\\
{\tt \char'047}{\tt OneColorOccurs}
\\
{\tt ...}\  
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt (master)}\\
{\tt >}\  {\tt (master-check}\  {\tt \char'047}{\tt red}\  {\tt \char'047}{\tt red)}
\\
{\tt \char'047}{\tt perfect!}
\end{tabbing}\end{minipage}\end{center}
The {{\tt master}\/} function consumes nothing and produces the invisible
value; its effect is to initialize the two targets. Depending on what the
chosen colors are, checking the two same guesses may produce
{{\tt \char'047}{\tt perfect!}\/} or {{\tt \char'047}{\tt NothingCorrect}\/}. In other words,
{{\tt master}\/} sets up some memory that {{\tt master-check}\/} uses.  

Let us now study how the design recipe applies to the development of the
program. The first step is to define the state variables and to specify the
purpose of each variable. Our analysis suggests that we need two state
variables, one per target:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt target1\char'054}\ {\tt target2}\ {\tt :}\ {\tt color}\/}}\\
\hbox{;; the variables represent the two colors that the first player chooses}
\\
{\tt (define}\  {\tt target1}\  {\tt (first}\  {\tt COLORS))}
\\
{\tt (define}\  {\tt target2}\  {\tt (first}\  {\tt COLORS))}
\end{tabbing}\end{minipage}\end{center}
Both variables are set to the first item from {{\tt COLORS}\/}, just so that
they stand for some color.

The second step is to develop an initializer for the two state variables. A
single initializer is enough because the two variables go together. Indeed,
the initializer is the desired {{\tt master}\/} function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt master}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: set {{\tt target1}\/} and {{\tt target2}\/} to randomly chosen items in {{\tt COLORS}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (master)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt target1}\  {\tt (list-ref}\  {\tt COLORS}\  {\tt (random}\  {\tt COL\char'043}{\tt )))}
\\
\>\>{\tt (set!}\  {\tt target2}\  {\tt (list-ref}\  {\tt COLORS}\  {\tt (random}\  {\tt COL\char'043}{\tt )))))}
\end{tabbing}\end{minipage}\end{center}
The effect comment explains how {{\tt master}\/} changes the two state
variables by picking an item from {{\tt COLORS}\/} based on a random number
between 0 and the length of {{\tt COLORS}\/}. 

Finally, we can turn to the functions that modify and utilize the program's
memory. As it turns out, the memory isn't modified after the two target
variables are initialized; it is only used to compare to the two guesses of
the player. The only other service we need is {{\tt master-check}\/}. It
uses {{\tt check-guess}\/}, the solution of exercise~\ref{ex:check-guess},
to conduct the comparison. For a summary, see figure~\ref{fig:master2},
which contains the variable and function definitions that we just
discussed. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt target1\char'054}\ {\tt target2}\ {\tt :}\ {\tt color}\ \/}}\\
\hbox{;; the two variables represent the two colors that the first player chose}
\\
{\tt (define}\  {\tt target1}\  {\tt (first}\  {\tt COLORS))}
\\
{\tt (define}\  {\tt target2}\  {\tt (first}\  {\tt COLORS))}
\\
\hbox{;; {{\tt master}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\hbox{;; effect: set {{\tt target1}\/} and {{\tt target2}\/} to two randomly chosen items from {{\tt COLORS}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (master)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt target1}\  {\tt (list-ref}\  {\tt COLORS}\  {\tt (random}\  {\tt COL\char'043}{\tt )))}
\\
\>\>{\tt (set!}\  {\tt target2}\  {\tt (list-ref}\  {\tt COLORS}\  {\tt (random}\  {\tt COL\char'043}{\tt )))))}
\\
\hbox{;; {{\tt master-check}\ {\tt :}\ {\tt color}\ {\tt color}\ {}{\tt ->}{}\ {\tt symbol}\/}}
\\
\hbox{;; to determine how many colors at how many positions are guessed correctly}
\\
\hbox{;; The function defers to {{\tt check-guess}\/}, the solution of exercise~\ref{ex:check-guess}.}
\\
{\tt (d}\={\tt efine}\  {\tt (master-check}\  {\tt guess1}\  {\tt guess2)}
\\
\>{\tt (check-guess}\  {\tt guess1}\  {\tt guess2}\  {\tt target1}\  {\tt target2))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Guessing colors (Part 2)} \label{fig:master2}
\hrule

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 

{\bf Exercise 37.1.1}

 \label{ex:master-diagram}
Draw a diagram that shows how {{\tt master}\/} and {{\tt master-check}\/}
interact with memory.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/master-diagram.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.1.2}

 \label{ex:master-pick}
Abstract the repeated expressions in {{\tt master}\/} into the function
{{\tt random-pick}\/}. It consumes a list and chooses a random item from
that list. Then use the function to eliminate the repeated expressions in
{{\tt master}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/master-pick.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.1.3}

 \label{ex:master-count}
Modify the color guessing program so that its final answer isn't just
{{\tt \char'047}{\tt perfect!}\/} but a list of two items: the symbol {{\tt perfect!}\/}
and the number of guesses that the second player made. Start by modifying
the diagram of exercise~\ref{ex:master-diagram}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/master-count.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.1.4}

 \label{ex:master-reset}
Modify the color guessing program so that it automatically re-starts the
game when a player has guessed the correct target colors.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/master-reset.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.1.5}

 \label{ex:master-gui}
Develop a graphical user interface, similar to that of the teachpack
{\tt\bf master.ss}. Instead of colored buttons, use buttons labeled with the
color. Show the current selection in message fields.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/master-gui.html}{\rule{3pt}{5pt}}


\subsection{State Changes from User Interactions} \label{sec:count-state}

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline Data Analysis and Definitions}:}\\
\hbox{;; A {\sl {letter}} is a symbol in: {{\tt \char'047}{\tt a}\ {\tt ...}\ {\tt \char'047}{\tt z}\/} plus {{\tt \char'047}{\tt \char'137}\/}}
\\
\hbox{;; A {\sl {word}} is a (listof letter).}
\\
\hbox{;; A {\sl {body-part}} is one of the following symbols:}
\\
{\tt (define}\  {\tt PARTS}\  {\tt \char'047}{\tt (head}\  {\tt body}\  {\tt right-arm}\  {\tt left-arm}\  {\tt right-leg}\  {\tt left-leg))}
\\
\hbox{;; {\underline Constants}:}
\\
\hbox{;; some guessing words: }
\\
{\tt (d}\={\tt efine}\  {\tt WORDS}\  
\\
\>{\tt \char'047}{\tt (}\={\tt (h}\  {\tt e}\  {\tt l}\  {\tt l}\  {\tt o)}
\\
\>\>{\tt (w}\  {\tt o}\  {\tt r}\  {\tt l}\  {\tt d)}
\\
\>\>{\tt (i}\  {\tt s)}
\\
\>\>{\tt (a)}
\\
\>\>{\tt (s}\  {\tt t}\  {\tt u}\  {\tt p}\  {\tt i}\  {\tt d)}
\\
\>\>{\tt (p}\  {\tt r}\  {\tt o}\  {\tt g}\  {\tt r}\  {\tt a}\  {\tt m)}
\\
\>\>{\tt ...}
\\
\>\>{\tt ))}
\\
\hbox{;; the number of words we can choose from }
\\
{\tt (define}\  {\tt WORDS\char'043}\  {\tt (length}\  {\tt WORDS))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Hangman Basics} \label{fig:hangman-state}
\hrule

\noindent 
Recall the hangman game from \ref{sec:hangman}. The goal of the game is to
 test a person's active vocabulary. One player thinks of a word and draws
 the noose of a gallows; the other player tries to guess the word, one
 letter at a time. For every wrong guess, the first player adds another
 part to the drawing (see figure~\ref{fig:hangman}): first the head, then
 the body, the arms, and the legs. If, however, the player's guess reveals
 new knowledge about the chosen word, the first player indicates where the
 the letter occurs in the word. The game is over when the second player
 guesses the complete word or when the first player has completed the stick
 figure.

Figure~\ref{fig:hangman-state} contains the data definitions for letters,
words, and body-parts. In particular, {{\tt PARTS}\/} not only specifies the
body parts that are drawn, but also the order in which they are drawn. The
figure also defines an incomplete list of words so that the hangman program
can randomly pick a word for us to guess.

The random picking of words occurs at the beginning of the game, which
suggests a random initialization function, similar to that of the
color-guessing program in the preceding section.  In contrast to the
latter, the hangman program must also remember the number of guesses that a
player made, because there is only a limited number of them.  After
{{\tt \char'047}{\tt left-leg}\/} is drawn, the game is over. Counting down the number of
body parts also implies that as the program checks each guess, it must
inform the player not only what the guess revealed but also which body
part, if any, was lost.

Let us capture this thought in a data definition that specifies the legitimate
class of responses: 

  \begin{quote} A {\sl {response}} is either       \begin{enumerate}        \item {{\tt ``You}\ {\tt won''}\/}        \item {{\tt (list}\ {\tt ``The}\ {\tt End''}\ {\tt word)}\/}        \item {{\tt (list}\ {\tt ``Good}\ {\tt guess!''}\ {\tt word)}\/}        \item {{\tt (list}\ {\tt ``Sorry''}\ {\tt body-part}\ {\tt word)}\/}      \end{enumerate} \end{quote}

\noindent Three of the responses are lists so that the program can provide
several pieces of information at once. Specifically, the first response
says that filling in the guess turns the status word into the chosen word
and that the player survived the game. The second response indicates the
opposite; the list of available body parts is exhausted and the game is
over because the player did not guess all the letters in the word. In the
third case, the player's guess was successful and the second item in the
list shows how much the player knows about the word. Finally, the fourth
response represents a bad guess, in which case the response is a list of
three items: a greeting, the lost body part, and a reminder of what the
player has found about the word.

We can now imagine the role of the two services in the {hangman}
program. The first, called {{\tt hangman}\/}, picks a new word; the second,
called {{\tt hangman-guess}\/}, consumes a letter and produces one of the four
possible responses. Here is a feasible dialog:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt (hangman)}\\
{\tt >}\  {\tt (hangman-guess}\  {\tt \char'047}{\tt a)}
\\
{\tt (list}\  {\tt ``Sorry''}\  {\tt \char'047}{\tt head}\  {\tt (list}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}{\tt ))}
\\
{\tt >}\  {\tt (hangman-guess}\  {\tt \char'047}{\tt i)}
\\
{\tt (list}\  {\tt ``Good}\  {\tt guess!''}\  {\tt (list}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt i}\  {\tt \char'047}{\tt \char'137}{\tt ))}
\\
{\tt >}\  {\tt (hangman-guess}\  {\tt \char'047}{\tt s)}
\\
{\tt (list}\  {\tt ``Good}\  {\tt guess!''}\  {\tt (list}\  {\tt \char'047}{\tt s}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt i}\  {\tt \char'047}{\tt \char'137}{\tt ))}
\\
{\tt >}\  {\tt (hangman-guess}\  {\tt \char'047}{\tt i)}
\\
{\tt (list}\  {\tt ``Sorry''}\  {\tt \char'047}{\tt body}\  {\tt (list}\  {\tt \char'047}{\tt s}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt i}\  {\tt \char'047}{\tt \char'137}{\tt ))}
\\
{\tt ...}
\\
{\tt >}\  {\tt (hangman)}
\\
{\tt >}\  {\tt (hangman-guess}\  {\tt \char'047}{\tt a)}
\\
{\tt ``You}\  {\tt won''}
\end{tabbing}\end{minipage}\end{center}
The dialog consists of two rounds of hangman. They show that the results of
{{\tt hangman-guess}\/} depend on the prior use of
{{\tt hangman}\/}. Furthermore, the first round illustrates how
{{\tt hangman-guess}\/} applied to the same guess twice produces two different
answers. This again means that {{\tt hangman-guess}\/} modifies and uses
memory, specifically, it counts down the body parts as the player makes
useless guesses.

In addition, the dialog shows that the player loses a body part whenever a
guess doesn't contribute any new knowledge. Consider the second guess:
{{\tt \char'047}{\tt i}\/}. It occurs in the penultimate position of the word and the
response of {{\tt hangman-guess}\/} says so. When the player enters
{{\tt \char'047}{\tt i}\/} again as the fourth guess, {{\tt hangman-guess}\/} detects no
progress because the positions of {{\tt \char'047}{\tt i}\/} have already been
revealed. In the informal description of the game, this aspect had been
left open. By putting together an example, we become aware of this
ambiguity and can make a decision. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt chosen-word}\ {\tt :}\ {\tt word}\/}}\\
\hbox{;; the word that the player is to guess}
\\
{\tt (define}\  {\tt chosen-word}\  {\tt (first}\  {\tt WORDS))}
\\
\hbox{;; {{\tt status-word}\ {\tt :}\ {\tt word}\/}}
\\
\hbox{;; represents which letters the player has and hasn't guessed}
\\
{\tt (define}\  {\tt status-word}\  {\tt (first}\  {\tt WORDS))}
\\
\hbox{;; {{\tt body-parts-left}\ {\tt :}\ {\tt (listof}\ {\tt body-part)}\/}}
\\
\hbox{;; represents the list of body parts that are still "available"}
\\
{\tt (define}\  {\tt body-parts-left}\  {\tt PARTS)}
\\
\hbox{;; {{\tt hangman}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\hbox{;; effect: initialize {{\tt chosen-word}\/}, {{\tt status-word}\/}, and {{\tt body-parts-left}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (hangman)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt chosen-word}\  {\tt (list-ref}\  {\tt WORDS}\  {\tt (random}\  {\tt (length}\  {\tt WORDS))))}
\\
\>\>{\tt (set!}\  {\tt status-word}\  {\tt ...)}
\\
\>\>{\tt (set!}\  {\tt body-parts-left}\  {\tt PARTS)))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Hangman Basics (Part 2)} \label{fig:hangman-state2}
\hrule

Thus far, our reasoning has revealed the need for two services and three
state variables: 
\begin{enumerate}
\item[] {{\tt chosen-word}\/}, which is the word to be guessed; 
\item[] {{\tt status-word}\/}, which records how much of the word has been
guessed; 
\item[] and {{\tt body-parts-left}\/}, which remembers how many, and which,
imaginary body parts the player can still lose. 
\end{enumerate}
The first two variables always stand for {{\tt word}\/}s, as their name
says. A natural value for the last one is a list of body parts; indeed, the
list should always be a suffix of {{\tt PARTS}\/}. 

Figure~\ref{fig:hangman-state2} contains the definitions of the state variables
and their purpose statements. The first two, {{\tt chosen-word}\/} and
{{\tt status-word}\/}, are set to the first items if WORDS, so that they
represent some word. The third one is set to {{\tt PARTS}\/} because this
list represents the entire collection of available body parts. 

Next we must develop an initializer for the state variables. As in the
preceding section, a single initializer suffices. It is the
{{\tt hangman}\/} function, and its purpose is to set up the program's
memory. Specifically, it picks a word for {{\tt chosen-word}\/}, and it sets
{{\tt status-word}\/} and {{\tt body-parts-left}\/} to values that reflect
that the game has just begun. For the last one, this is easy because 
{{\tt PARTS}\/} is the appropriate list. The initial value for
{{\tt status-word}\/} requires a short analysis. First, the value must be a
word. Second, it must consist of as many letters as
{{\tt chosen-word}\/}. Finally, each of the letters is unknown, because the
player hasn't made any guesses yet. Thus, the matching action is a build a
word as long as {{\tt chosen-word}\/} but to use {{\tt \char'047}{\tt \char'137}\/} as the letters.  
 
  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 
{\bf Exercise 37.2.1}

 \label{ex:status-plain}
Develop the function {{\tt make-status-word}\/}, which consumes a word and
produces an equally long word consisting of just {{\tt \char'047}{\tt \char'137}\/}. Use the
function to complete the definition of {{\tt hangman}\/} in
figure~\ref{fig:hangman-state2}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/status-plain.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.2.2}

 \label{ex:status-build-list}
Use {{\tt build-list}\/} to create the status word in a single
expression. Complete the definition of {{\tt hangman}\/} in
figure~\ref{fig:hangman-state2}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/status-build-list.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}



Now we are ready to deal with the most difficult part: the design of
{{\tt hangman-guess}\/}, a function that uses and modifies the memory.  It
consumes a letter and produces an answer, specifically a {{\tt response}\/},
which depends on how the current value of {{\tt status-word}\/},
{{\tt chosen-word}\/}, and {{\tt guess}\/} compare.  At the same time, the
function must affect the state variable {{\tt status-word}\/} if the
player's guess added new knowledge. If not, the function must shorten
{{\tt body-parts-left}\/}, the list of available body parts. The matching
contract, purpose and effect statements are as follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt hangman-guess}\ {\tt :}\ {\tt letter}\ {}{\tt ->}{}\ {\tt response}\/}}\\
\hbox{;; to determine whether the player has won, lost, or may continue to}
\\
\hbox{;; play and, if no progress was made, which body part was lost}
\\
\hbox{;; effect:}
\\
\hbox{;; (1) if the guess represents progress, update {{\tt status-word}\/}}
\\
\hbox{;; (2) if not, shorten the {{\tt body-parts-left}\/} by one }
\end{tabbing}\end{minipage}\end{center}

We have already considered a sample dialog that illustrates the working of
{{\tt hangman-guess}\/}. By dissecting this dialog, we can develop specific
examples for {{\tt hangman-guess}\/}.

The sample dialog and the purpose/effect statements imply that
the result of {{\tt hangman-guess}\/} depends on whether or not the guess
constitutes progress and, if not, whether or not the guess was the last
one. Let's use these distinctions for the development of examples:
\begin{enumerate}
\item 
 If {{\tt status-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt \char'137}{\tt )}\/} and
 {{\tt chosen-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/}, then evaluating 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  {\tt (hangman-guess}\  {\tt \char'047}{\tt l)}\end{tabbing}\end{minipage}\end{center}
 produces {{\tt (list}\ {\tt ``Good}\ {\tt guess!''}\ {\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l))}\/} and
 {{\tt status-word}\/} becomes {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/}.

\item
 If {{\tt status-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/} and
 {{\tt chosen-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/}, then evaluating 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  {\tt (hangman-guess}\  {\tt \char'047}{\tt a)}\end{tabbing}\end{minipage}\end{center}
 produces {{\tt ``You}\ {\tt won''}\/}. The evaluation has no effect in this case. 

\item 
 If {{\tt status-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/},
 {{\tt chosen-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/}, and 
 {{\tt body-parts-left}\/} is {{\tt (list}\ {\tt \char'047}{\tt right-leg}\ {\tt \char'047}{\tt left-leg)}\/}, 
 then evaluating 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  {\tt (hangman-guess}\  {\tt \char'047}{\tt l)}\end{tabbing}\end{minipage}\end{center}
 produces {{\tt (list}\ {\tt ``Sorry''}\ {\tt \char'047}{\tt right-leg}\ {\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l))}\/} and
 {{\tt body-parts-left}\/} becomes {{\tt (list}\ {\tt \char'047}{\tt left-leg)}\/}.

\item 
 Finally, if {{\tt status-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/},
 {{\tt chosen-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/}, and 
 {{\tt body-parts-left}\/} is {{\tt (list}\ {\tt \char'047}{\tt left-leg)}\/}, 
 then evaluating 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  {\tt (hangman-guess}\  {\tt \char'047}{\tt l)}\end{tabbing}\end{minipage}\end{center}
 produces {{\tt (list}\ {\tt ``The}\ {\tt End''}\ {\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l))}\/} and
 {{\tt body-parts-left}\/} becomes {{\tt empty}\/}.
\end{enumerate}
The first two examples illustrate what happens when the player enters a
guess that reveals new information; the last two focus on those cases where
the guess contributes nothing. 

The case split naturally suggests a basic template based on a distinction
among the possible situations:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (hangman-guess}\  {\tt guess)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt .}\={\tt ..}\  \hbox{;; {{\tt guess}\/} did reveal new information: }
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}\={\tt ...}\  \hbox{;; guess completed the search for the word}
\\
\>\>\>\>\>{\tt ...\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt .}\={\tt ..}\  \hbox{;; guess did {\bf not} complete the search for the word}
\\
\>\>\>\>\>{\tt (b}\={\tt egin}\  
\\
\>\>\>\>\>\>{\tt (set!}\  {\tt status-word}\  {\tt ...)}
\\
\>\>\>\>\>\>{\tt ...)\char'135}{\tt )\char'135}
\\
\>\>{\tt \char'133}\={\tt ...}\  \hbox{;; {{\tt guess}\/} did {\bf not} reveal any new information: }
\\
\>\>\>{\tt (b}\={\tt egin}\  
\\
\>\>\>\>{\tt (set!}\  {\tt body-parts-left}\  {\tt ...)}
\\
\>\>\>\>{\tt ...}\  {\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The location of the {{\bf set!}-exp\-res\-sion}s in the template's nested
 {{\tt cond}\/}s specify exactly under which conditions effects happen.
 First, the outermost conditional distinguishes whether or not
 {{\tt guess}\/} produces new knowledge about the hidden word; if it
 doesn't, the function must modify {{\tt body-parts-left}\/}. Second, if
 {{\tt guess}\/} reveals new knowledge, the function updates the
 {{\tt status-word}\/} variable unless the player has just finished the
 entire word. 

Because we haven't considered yet how to express these tests, we use
comments to indicate what the conditions are. Let us turn to this problem
first, so that we can start the function-definition step with a
full-fledged template. The first missing condition concerns the question
whether {{\tt guess}\/} reveals new information. To this end, we must
compare {{\tt guess}\/} with the letters in {{\tt chosen-word}\/}. This
comparison should produce the new status word. Here is the specification
for the auxiliary function that conducts this computation:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reveal-list}\ {\tt :}\ {\tt word}\ {\tt word}\ {\tt letter}\ {}{\tt ->}{}\ {\tt word}\/}}\\
\hbox{;; to compute the new status word from {{\tt chosen-word}\/},}
\\
\hbox{;; {{\tt status-word}\/}, and {{\tt guess}\/}}
\\
{\tt (define}\  {\tt (reveal-list}\  {\tt chosen-word}\  {\tt status-word}\  {\tt guess)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Fortunately, we have discussed this auxiliary function twice before (see
sections~\ref{sec:hangman} and exercise~\ref{ex:hangman-list}) and know how to
define it; figure~\ref{fig:hangman-state3} contains a suitable
definition. Using {{\tt reveal-list}\/}, we can now formulate a condition that
determines whether {{\tt guess}\/} reveals new knowledge:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (equal?}\  {\tt status-word}\  {\tt (reveal-list}\  {\tt status-word}\  {\tt chosen-word}\  {\tt guess))}\end{tabbing}\end{minipage}\end{center}
The condition uses {{\tt equal?}\/} to compare the current value of
{{\tt status-word}\/} with its new value, as computed by
{{\tt reveal-list}\/}. If the two lists are equal, {{\tt guess}\/} doesn't
produce new knowledge; otherwise it does. 

The second missing condition concerns the question whether the given {{\tt guess}\/}
completes the search for the word. If {{\tt guess}\/} is equal to all missing
letters in {{\tt status-word}\/}, then the player has found the complete
word. Here is the corresponding condition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (equal?}\  {\tt chosen-word}\  {\tt (reveal-list}\  {\tt status-word}\  {\tt chosen-word}\  {\tt guess))}\end{tabbing}\end{minipage}\end{center}
That is, the game is over if {{\tt chosen-word}\/} is equal to the result of
{{\tt reveal-list}\/}. 

Let's put everything together in a single template: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (hangman-guess}\  {\tt guess)}\\
\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt new-status}\  {\tt (reveal-list}\  {\tt status-word}\  {\tt chosen-word}\  {\tt guess)))}
\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (equal?}\  {\tt new-status}\  {\tt status-word)}
\\
\>\>\>\>{\tt (b}\={\tt egin}\  
\\
\>\>\>\>\>{\tt (set!}\  {\tt body-parts-left}\  {\tt ...)}
\\
\>\>\>\>\>{\tt ...}\  {\tt )\char'135}
\\
\>\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (equal?}\  {\tt new-status}\  {\tt chosen-word)}
\\
\>\>\>\>\>\>{\tt ...\char'135}
\\
\>\>\>\>\>{\tt \char'133}\={\tt else}\  
\\
\>\>\>\>\>\>{\tt (b}\={\tt egin}\  
\\
\>\>\>\>\>\>\>{\tt (set!}\  {\tt status-word}\  {\tt ...)}
\\
\>\>\>\>\>\>\>{\tt ...)\char'135}{\tt )\char'135}{\tt )))}
\end{tabbing}\end{minipage}\end{center}
The template uses a {{\bf local}-exp\-res\-sion} because the result of
{{\tt reveal-list}\/} is used in two conditions. Also, in this template the two
outer {{\tt cond}\/}-clauses are swapped, because it is more natural to write 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (equal?}\  {\tt new-status}\  {\tt status-word)}\end{tabbing}\end{minipage}\end{center}
than its negation. We can now turn to the function-design step. 

The template is conditional. So we develop the effect and the answer for
each clause separately:
\begin{enumerate}
\item Assume that {{\tt (equal?}\ {\tt new-status}\ {\tt status-word)}\/} evaluates to
  {{\tt true}\/}, that is, the player made no progress. This implies that the
  player loses an imaginary body part. To capture this effect, the
  {\bf set!}-exp\-res\-sion\ must change the value of
  {{\tt body-parts-left}\/}. Specifically, it must set the state variable to the
  rest of its current value: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set!}\  {\tt body-parts-left}\  {\tt (rest}\  {\tt body-parts-left))}\end{tabbing}\end{minipage}\end{center}

 The answer depends on the new value of {{\tt body-parts-left}\/}. If it is
 {{\tt empty}\/}, the game is over; the appropriate response is
 {{\tt (list}\ {\tt ``The}\ {\tt End''}\ {\tt chosen-word)}\/} so that the player finds out what
 the chosen word was.  If {{\tt body-parts-left}\/} is not empty, the
 response is {{\tt (list}\ {\tt ``Sorry''}\ {\tt \char'077}{\tt \char'077}{\tt \char'077}\ {\tt status-word)}\/}.  The response says
 that {{\tt guess}\/} is useless. Its last part is the current value of
 {{\tt status-word}\/} so that the player sees what he has discovered.  The
 {{\tt \char'077}{\tt \char'077}{\tt \char'077}\/} indicates a problem. To understand the problem, take a look
 at what we have: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (b}\={\tt egin}\  \\
\>{\tt (set!}\  {\tt body-parts-left}\  {\tt (rest}\  {\tt body-parts-left))}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt body-parts-left)}\  {\tt (list}\  {\tt ``The}\  {\tt End''}\  {\tt chosen-word)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (list}\  {\tt ``Sorry''}\  {\tt \char'077}{\tt \char'077}{\tt \char'077}\  {\tt status-word)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
 In principle, the question marks should be the body part that the player
 just lost to the gallows. But, because {{\tt set!}\/} modifies
 {{\tt body-parts-left}\/}, we can no longer just say {{\tt (first}{ }\ {\tt body-parts-left)}\/}. As mentioned in section~\ref{sec:seq-time}, when
 programming with {{\tt set!}\/} timing matters. We can solve the problem
 with a {{\bf local}-exp\-res\-sion}s that names the first item on
 {{\tt body-parts-left}\/} before the state variable is modified.

\item
  The second case is much simpler than the first. We distinguish two subcases: 

  \begin{enumerate}
  \item 
    If {{\tt new-status}\/} is equal to {{\tt chosen-word}\/}, the player has
    won. The response is {{\tt ``You}\ {\tt won''}\/}; there is no effect. 

  \item
    If the two are not equal, the player made some progress and must be
    told. Furthermore, the function must keep track of the progress; a
    {{\tt (set!}\ {\tt status-word}\ {\tt new-status)}\/} accomplishes this effect. 
    The response consists of an encouragement and the new status. 
  \end{enumerate}
\end{enumerate}
Figure~\ref{fig:hangman-state3} contains the complete definition of
{{\tt hangman-guess}\/}. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt hangman-guess}\ {\tt :}\ {\tt letter}\ {}{\tt ->}{}\ {\tt response}\/}}\\
\hbox{;; to determine whether the player has won, lost, or may continue to play}
\\
\hbox{;; and, if so, which body part was lost, if no progress was made}
\\
\hbox{;; effects: (1) if the guess represents progress, update {{\tt status-word}\/}}
\\
\hbox{;; (2) if not, shorten the {{\tt body-parts-left}\/} by one }
\\
{\tt (d}\={\tt efine}\  {\tt (hangman-guess}\  {\tt guess)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt new-status}\  {\tt (reveal-list}\  {\tt chosen-word}\  {\tt status-word}\  {\tt guess)))}
\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (equal?}\  {\tt new-status}\  {\tt status-word)}
\\
\>\>\>\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt next-part}\  {\tt (first}\  {\tt body-parts-left)))}
\\
\>\>\>\>\>{\tt (b}\={\tt egin}\  
\\
\>\>\>\>\>\>{\tt (set!}\  {\tt body-parts-left}\  {\tt (rest}\  {\tt body-parts-left))}
\\
\>\>\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt body-parts-left)}\  {\tt (list}\  {\tt ``The}\  {\tt End''}\  {\tt chosen-word)\char'135}
\\
\>\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (list}\  {\tt ``Sorry''}\  {\tt next-part}\  {\tt status-word)\char'135}{\tt )))\char'135}
\\
\>\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (equal?}\  {\tt new-status}\  {\tt chosen-word)}\  {\tt ``You}\  {\tt won''\char'135}
\\
\>\>\>\>\>{\tt \char'133}\={\tt else}\  
\\
\>\>\>\>\>\>{\tt (b}\={\tt egin}\  
\\
\>\>\>\>\>\>\>{\tt (set!}\  {\tt status-word}\  {\tt new-status)}
\\
\>\>\>\>\>\>\>{\tt (list}\  {\tt ``Good}\  {\tt guess!''}\  {\tt status-word))\char'135}{\tt )\char'135}{\tt )))}
\\
\hbox{;; {{\tt reveal-list}\ {\tt :}\ {\tt word}\ {\tt word}\ {\tt letter}\ {}{\tt ->}{}\ {\tt word}\/}}
\\
\hbox{;; to compute the new status word}
\\
{\tt (d}\={\tt efine}\  {\tt (reveal-list}\  {\tt chosen-word}\  {\tt status-word}\  {\tt guess)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (reveal-one}\  {\tt chosen-letter}\  {\tt status-letter)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt chosen-letter}\  {\tt guess)}\  {\tt guess\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt status-letter\char'135}{\tt )))}
\\
\>\>{\tt (map}\  {\tt reveal-one}\  {\tt chosen-word}\  {\tt status-word)))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Hangman Basics (Part 3)} \label{fig:hangman-state3}
\hrule

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 

{\bf Exercise 37.2.3}

 \label{ex:hangman-diagram}
Draw a diagram that shows how {{\tt hangman}\/} and {{\tt hangman-guess}\/}
interact with the state variables.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hangman-diagram.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.2.4}

 \label{ex:hangman-test}
Formulate the four examples for {{\tt hangman-guess}\/} as boolean-valued
expressions that produce {{\tt true}\/} if {{\tt hangman-guess}\/} is
correct. Develop an additional example for each case; turn these new
examples into additional tests.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hangman-test.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.2.5}

 \label{ex:hangman-gui}
Develop a graphical user interface, similar to that of the teachpack
{\tt\bf hangman.ss}. Connect the functions in this section as
call-backs.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hangman-gui.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.2.6}

 \label{ex:hangman-progress}
Modify the program so that it keeps track of all the guesses. Then, if a
player enters the same guess twice for the same round of a hangman game,
the response of {{\tt hangman-guess}\/} is {{\tt ``You}\ {\tt have}\ {\tt used}\ {\tt this}\ {\tt guess}{ }{\tt before.``}\/}~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hangman-progress.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.2.7}

 \label{ex:hangman-rev-effect}
Consider the following variant of {{\tt reveal-list!}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reveal-list!}\ {\tt :}\ {\tt letter}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to modify {{\tt status-word}\/} based on a comparison of {{\tt chosen-word}\/},}
\\
\hbox{;; the {{\tt status-word}\/}, and the player's {{\tt guess}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (reveal-list!}\  {\tt chosen-word}\  {\tt status-word}\  {\tt guess)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (reveal-one}\  {\tt chosen-letter}\  {\tt status-letter)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt chosen-letter}\  {\tt guess)}\  {\tt guess\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt status-letter\char'135}{\tt )))}
\\
\>\>{\tt (set!}\  {\tt status-word}\  {\tt (map}\  {\tt reveal-one}\  {\tt chosen-word}\  {\tt status-word))))}
\end{tabbing}\end{minipage}\end{center}
It changes the state variable {{\tt status-word}\/} to a value that is computed
from the old value of {{\tt status-word}\/}, {{\tt chosen-word}\/}, and the
guess. 

Modify {{\tt hangman-guess}\/} so that it works properly with the
{{\tt reveal-list!}\/} function.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hangman-rev-effect.html}{\rule{3pt}{5pt}}


\subsection{State Changes from Recursion} \label{sec:repeat-state}

Functions that affect the memory of a program may not only process simple forms
of data but also arbitrarily large pieces of data. To understand how this works, 
let us take a closer look at the purpose of {{\tt reveal-list}\/} from the
hangman game program. 

As we have just seen, the function compares {{\tt guess}\/} with each letter
in {{\tt chosen-word}\/}. If it is the same, {{\tt guess}\/} may uncover new
knowledge and is included at the appropriate position in the word;
otherwise, the corresponding letter from {{\tt status-word}\/} represents
what the player knows.

The {{\tt hangman-guess}\/} function then compares the result of
{{\tt reveal-list}\/} with the old value of {{\tt status-word}\/} to find out
whether the player uncovered new knowledge. Furthermore, the result is
compared with {{\tt chosen-word}\/} again if the player found new knowledge,
because {{\tt guess}\/} might have matched all remaining unknown
letters. Clearly, both of these comparisons repeat the computations of
{{\tt reveal-one}\/}. The problem is that the result of {{\tt reveal-one}\/}
is useful to {{\tt reveal-list}\/} and that the result of its individual
comparisons are useful in the conditionals of {{\tt hangman-guess}\/}. 

We can solve the first part of the problem with the use of an additional
piece of memory: a state variable that records whether {{\tt reveal-one}\/}
uncovers a letter.  The state variable, let's call it
{{\tt new-knowledge}\/}, is modified by {{\tt reveal-one}\/} if it determines
that {{\tt guess}\/} uncovers a currently hidden letter in
{{\tt chosen-word}\/}. The {{\tt hangman-guess}\/} function can use
{{\tt new-knowledge}\/} to find out what {{\tt reveal-one}\/} discovered.

Let us now translate our idea into new program definitions systematically. 
First, we need to specify the state variable and its meaning: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt new-knowledge}\ {\tt :}\ {\tt boolean}\/}}\\
\hbox{;; the variable represents whether the most recent application of}
\\
\hbox{;; {{\tt reveal-list}\/} has provided the player with new knowledge}
\\
{\tt (define}\  {\tt new-knowledge}\  {\tt false)}
\end{tabbing}\end{minipage}\end{center}

Second, we must consider what it means to initialize the new state
variable.  From what we know, the state variable is used every time
{{\tt reveal-list}\/} is applied to {{\tt guess}\/}. When the application
starts, the state variable should be {{\tt false}\/}; it should change to
{{\tt true}\/} if {{\tt guess}\/} is useful. This suggests that
{{\tt new-knowledge}\/} is to be initialized to {{\tt false}\/} every time
{{\tt reveal-list}\/} is applied. We can achieve this re-initialization by
changing {{\tt reveal-list}\/} so that it sets the state variable before it
computes anything else:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reveal-list}\ {\tt :}\ {\tt word}\ {\tt word}\ {\tt letter}\ {}{\tt ->}{}\ {\tt word}\/}}\\
\hbox{;; to compute the new status word}
\\
\hbox{;; effect: to set {{\tt new-knowledge}\/} to {{\tt false}\/} first}
\\
{\tt (d}\={\tt efine}\  {\tt (reveal-list}\  {\tt chosen-word}\  {\tt status-word}\  {\tt guess)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt (reveal-one}\  {\tt chosen-letter}\  {\tt status-letter)}\  {\tt ...))}
\\
\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\hbox{$\underline{\mbox{{{\tt (set!}\ {\tt new-knowledge}\ {\tt false)}\/}}}$}
\\
\>\>\>{\tt (map}\  {\tt reveal-one}\  {\tt chosen-word}\  {\tt status-word))))}
\end{tabbing}\end{minipage}\end{center}
The underlined expression is the essential modification. The {{\tt local}\/}
expression defines the auxiliary function {{\tt reveal-one}\/} and then
evaluates the {{\tt local}\/}'s body. The first step of the body is to
initialize {{\tt new-knowledge}\/}. 

Third, we must develop the program that modifies {{\tt new-knowledge}\/}. Here
the program already exists: {{\tt reveal-list}\/}, so our task is to modify it
in such a way that it changes the state variable appropriately. Let's describe
the idea with a modified effect statement: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reveal-list}\ {\tt :}\ {\tt word}\ {\tt word}\ {\tt letter}\ {}{\tt ->}{}\ {\tt word}\/}}\\
\hbox{;; to compute the new status word}
\\
\hbox{;; effect: }
\\
\hbox{;; (1) to set {{\tt new-knowledge}\/} to {{\tt false}\/} first}
\\
\hbox{;; (2) to set {{\tt new-knowledge}\/} to {{\tt true}\/} if {{\tt guess}\/} reveals new knowledge}
\end{tabbing}\end{minipage}\end{center}
The first part of the effect is necessary for the second one; an experienced
programmer may drop it. 

Next we should modify the examples for the function to illustrate what kind
of effects happen. The purpose of the function is to compute the new status
word by checking whether {{\tt guess}\/} occurs in the
{{\tt chosen-word}\/}. There are two basic situations depending on whether
{{\tt guess}\/} reveals new knowledge or not: 
\begin{enumerate}
\item
 If {{\tt status-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/} and
 {{\tt chosen-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/}, then evaluating 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (reveal-one}\  {\tt chosen-wor}\  {\tt status-word}\  {\tt \char'047}{\tt a)}\end{tabbing}\end{minipage}\end{center}
 produces {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/} and {{\tt new-knowledge}\/} is {{\tt true}\/}. 

\item
 If {{\tt status-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt \char'137}{\tt )}\/} and
 {{\tt chosen-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/}, then evaluating 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (reveal-one}\  {\tt chosen-wor}\  {\tt status-word}\  {\tt \char'047}{\tt x)}\end{tabbing}\end{minipage}\end{center}
 produces {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt \char'137}{\tt )}\/} and {{\tt new-knowledge}\/} is {{\tt false}\/}. 

\item
 If {{\tt status-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt \char'137}{\tt )}\/} and
 {{\tt chosen-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/}, then evaluating 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (reveal-one}\  {\tt chosen-wor}\  {\tt status-word}\  {\tt \char'047}{\tt l)}\end{tabbing}\end{minipage}\end{center}
 produces {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/} and {{\tt new-knowledge}\/} is {{\tt true}\/}. 

\item 
 Finally, if {{\tt status-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/} and 
 {{\tt chosen-word}\/} is {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt a}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/}, then evaluating 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (reveal-one}\  {\tt chosen-wor}\  {\tt status-word}\  {\tt \char'047}{\tt l)}\end{tabbing}\end{minipage}\end{center}
  produces {{\tt (list}\ {\tt \char'047}{\tt b}\ {\tt \char'047}{\tt \char'137}\ {\tt \char'047}{\tt l}\ {\tt \char'047}{\tt l)}\/} and {{\tt new-knowledge}\/} is {{\tt false}\/}. 

\end{enumerate}
The first two examples cover the basic situations; the third one shows that
if {{\tt guess}\/} reveals several new positions in the word,
{{\tt new-knowledge}\/} also becomes {{\tt true}\/}; and the last shows how
guessing a letter that has been uncovered before means no new knowledge has
been added.

Given that we already have a function, we can skip the template step and
instead focus on the question what we need to change in the existing
function. The given version of {{\tt reveal-list}\/} maps {{\tt reveal-one}\/}
over the two words, which are lists of letters. It is {{\tt reveal-one}\/}
that compares {{\tt guess}\/} with the letters in {{\tt chosen-word}\/} and
that determines whether the player has uncovered new knowledge. Hence, we
must modify the auxiliary function so that it recognizes when {{\tt guess}\/}
represents new knowledge and to set {{\tt new-knowledge}\/} to true in that
case.

As it is currently defined, {{\tt reveal-one}\/} merely compares
{{\tt guess}\/} with the letters in {{\tt chosen-word}\/}.  It does not check
whether the player discovers truly new knowledge if {{\tt guess}\/} and
{{\tt chosen-letter}\/} are the same.  The letter {{\tt guess}\/}, however,
represents new knowledge only if the matching letter in the {status} word
is still {{\tt \char'047}{\tt \char'137}\/}. This suggests the following two modifications:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reveal-list}\ {\tt :}\ {\tt word}\ {\tt word}\ {\tt letter}\ {}{\tt ->}{}\ {\tt word}\/}}\\
\hbox{;; to compute the new status word}
\\
\hbox{;; effect: to set {{\tt new-knowledge}\/} to {{\tt true}\/} if {{\tt guess}\/} reveals new knowledge}
\\
{\tt (d}\={\tt efine}\  {\tt (reveal-list}\  {\tt chosen-word}\  {\tt status-word}\  {\tt guess)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (reveal-one}\  {\tt chosen-letter}\  {\tt status-letter)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}\={\tt (and}\  \={\tt (symbol=?}\  {\tt chosen-letter}\  {\tt guess)}
\\
\>\>\>\>\>\>\hbox{$\underline{\mbox{{{\tt (symbol=?}\ {\tt status-letter}\ {\tt \char'047}{\tt \char'137}{\tt )}\/}}}$}{\tt )}
\\
\>\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>\>\hbox{$\underline{\mbox{{{\tt (set!}\ {\tt new-knowledge}\ {\tt true)}\/}}}$}
\\
\>\>\>\>\>\>{\tt guess)\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt status-letter\char'135}{\tt )))}
\\
\>\>{\tt (b}\={\tt egin}
\\
\>\>\>{\tt (set!}\  {\tt new-knowledge}\  {\tt false)}
\\
\>\>\>{\tt (map}\  {\tt reveal-one}\  {\tt chosen-word}\  {\tt status-word))))}
\end{tabbing}\end{minipage}\end{center}
That is, {{\tt reveal-one}\/} changes the value of {{\tt new-knowledge}\/}
if, and only if, both {{\tt (symbol=?}\ {\tt chosen-letter}\ {\tt guess)}\/} and {{\tt (symbol=?}{ }{\tt status-letter}\ {\tt \char'047}{\tt \char'137}{\tt )}\/} are true.

In summary, we can use state variables if we wish to communicate several
results from one computation to distant places. For such cases, the
interface of a function is under our control but we choose to design it
such that the function has both a result and an effect. The proper way to
achieve these combinations is to develop the computations separately and to
merge them later, if necessary. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 37.3.1}

 \label{ex:hangman-diagram2}
Draw a diagram that shows how {{\tt hangman}\/}, {{\tt hangman-guess}\/}, and
{{\tt reveal-list}\/} interact with the state
variables.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hangman-diagram2.html}{\rule{3pt}{5pt}} 


{\bf Exercise 37.3.2}

 \label{ex:reveal-one-O}
Turn the three examples into tests, that is, boolean-valued expressions, and test
the new version of {{\tt reveal-list}\/}. How many times does
{{\tt reveal-one}\/} modify {{\tt new-knowledge}\/} for the third test
case?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/reveal-one-O.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.3.3}

 \label{ex:hangman-new-knowledge}
Modify {{\tt hangman-guess}\/} in the hangman program to take advantage of the
additional information that {{\tt reveal-list}\/} provides through
{{\tt new-knowledge}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hangman-new-knowledge.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.3.4}

 \label{ex:hangman-uk-let}
Modify the hangman program a second time to eliminate the second
{{\tt equal?}\/} in {{\tt hangman-guess}\/}. \noindent{\bf Hint:} \ Introduce a state variable
that counts how many letters the player doesn't know yet.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hangman-uk-let.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Let us study a second example of a function that consumes an arbitrarily
large piece of data and modifies the program's memory. The example is a
natural extension of the traffic light simulator in
section~\ref{sec:design-memory}. We developed two functions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt init-traffic-light}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/} }\\
\hbox{;; effects: (1) to initialize {{\tt current-color}\/}; (2) to draw traffic light}
\end{tabbing}\end{minipage}\end{center}
and 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt next}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; {effects}: (1) to change {{\tt current-color}\/} from {{\tt \char'047}{\tt green}\/} to {{\tt \char'047}{\tt yellow}\/}, }
\\
\hbox{;; {{\tt \char'047}{\tt yellow}\/} to {{\tt \char'047}{\tt red}\/}, and {{\tt \char'047}{\tt red}\/} to {{\tt \char'047}{\tt green}\/}}
\\
\hbox{;; (2) to re-draw the traffic light appropriately }
\end{tabbing}\end{minipage}\end{center}
The first one starts the process; with the second one, we can repeatedly
switch the state of the light by evaluating {{\tt (next)}\/} in the
{\tt Interactions} window. 

Typing in {{\tt (next)}\/} over and over again is tiring, so it is natural
to wonder how to write a program that switches the state of the traffic
light a 100 or 1000 or 10000 times. In other words, we should develop a
program---let's call it {{\tt switch}\/}---that consumes a natural number
and that switches the light from one color to another that many times.

The function consumes a natural number and produces {{\tt (}{\sf void}{\tt )}\/}, after it
succeeded in switching the traffic light a sufficient number of times. By
now we can immediately write down all the basics, including the template,
for a function that consumes a natural number:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt switch}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; purpose: it computes nothing of interest}
\\
\hbox{;; effect: switch the traffic light {{\tt n}\/} times, }
\\
\hbox{;; holding each color for three seconds}
\\
{\tt (d}\={\tt efine}\  {\tt (switch}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt n)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (switch}\  {\tt (-}\  {\tt n}\  {\tt 1))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The template is that of a conventional, structurally recursive function. 

Making up an example is also straightforward. If we evaluate {{\tt (switch}{ }{\tt 4)}\/}, we wish to see a change from {{\tt \char'047}{\tt red}\/} to {{\tt \char'047}{\tt yellow}\/},
{{\tt \char'047}{\tt green}\/}, and {{\tt \char'047}{\tt red}\/} again, with each stage visible for three
seconds.

Defining the full function based on the template is straightforward. We
proceed by cases. If {{\tt n}\/} is {{\tt 0}\/}, the answer is
{{\tt (}{\sf void}{\tt )}\/}. Otherwise, we know that 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (switch}\  {\tt (-}\  {\tt n}\  {\tt 1))}\end{tabbing}\end{minipage}\end{center}
simulates all the necessary switching actions but one. To accomplish this
one additional switch, the function must use {{\tt (next)}\/} to perform all
the state changes and the change of canvas and must wait three seconds. If
we put everything together in a {{\bf begin}-exp\-res\-sion}, things happen in
the right order: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (begin}\  \={\tt (sleep-for-a-while}\  {\tt 3)}\\
\>{\tt (next)}
\\
\>{\tt (switch}\  {\tt (-}\  {\tt n}\  {\tt 1)))}
\end{tabbing}\end{minipage}\end{center}
The top of figure~\ref{fig:switching} is the complete definition for
{{\tt switch}\/}.

\hrule 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt switch}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: switch the traffic light {{\tt n}\/} times, holding each color for {{\tt 3}\/} seconds}
\\
\hbox{;; structural recursion }
\\
{\tt (d}\={\tt efine}\  {\tt (switch}\  {\tt n)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt n}\  {\tt 0)}\  {\tt (}{\sf void}{\tt )\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (begin}\  \={\tt (sleep-for-a-while}\  {\tt 3)}
\\
\>\>\>{\tt (next)}
\\
\>\>\>{\tt (switch}\  {\tt (-}\  {\tt n}\  {\tt 1)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt switch-forever}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: switch the traffic light forever, holding each color for {{\tt 3}\/} seconds}
\\
\hbox{;; generative recursion }
\\
{\tt (d}\={\tt efine}\  {\tt (switch-forever)}
\\
\>{\tt (begin}\  \={\tt (sleep-for-a-while}\  {\tt 3)}
\\
\>\>{\tt (next)}
\\
\>\>{\tt (switch-forever)))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Two ways of switching traffic lights}\label{fig:switching}
\hrule

An alternative is to switch the traffic light forever or at least until it
breaks due to some external event. In this case, the simulator does not
consume any argument and, when applied, runs forever. This is the simplest
form of generative recursion we can possibly encounter: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt switch-forever}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: switch the traffic light forever, }
\\
\hbox{;; holding each color for {{\tt 3}\/} seconds}
\\
{\tt (d}\={\tt efine}\  {\tt (switch-forever)}\  
\\
\>{\tt ...}
\\
\>{\tt (switch-forever))}
\end{tabbing}\end{minipage}\end{center}
Because the program does not terminate under any conditions, the template
contains only one recursive call. This suffices to construct an eternally
looping function.

Using this template, we can define the complete function as before. Before
recurring, the function must sleep and switch the light with {{\tt next}\/}.
We can accomplish this with a {{\bf begin}-exp\-res\-sion}, as shown in the
bottom definition of figure~\ref{fig:switching}.

In summary, when we must develop recursive functions that modify the
program's memory, we choose the design recipe that best matches our
situation and proceed accordingly. In particular, if the function has both
an interesting purpose and an effect, as for example {{\tt reveal-list}\/},
we should first develop the pure function and then add the effects later.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 

{\bf Exercise 37.3.5}

 \label{ex:route-set}
In section~\ref{sec:loss:generative}, we discussed how to search for routes
in simple graphs. The Scheme representation of a simple graph is a list of
pairs (of symbols). The pairs specify the direct connections among the
nodes in the graph. Each node is the beginning of exactly one connection,
but may be the end of several such connections or none. Given two nodes in
a simple graph, the problem is to find out whether one can go from the
first to the second. 

Recall is our first attempt at a function that determines whether the route
exists (see also figure~\ref{fig:route-exists1}): 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt route-exists\char'077}\ {\tt :}\ {\tt node}\ {\tt node}\ {\tt simple-graph}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether there is a route from {{\tt orig}\/} to {{\tt dest}\/} in {{\tt sg}\/}}
\\
\hbox{;; generative recursion }
\\
{\tt (d}\={\tt efine}\  {\tt (route-exists\char'077}\  {\tt orig}\  {\tt dest}\  {\tt sg)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt orig}\  {\tt dest)}\  {\sf true}{\tt \char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (route-exists\char'077}\  {\tt (neighbor}\  {\tt orig}\  {\tt sg)}\  {\tt dest}\  {\tt sg)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The function checks whether the origination and destination nodes are the
same. If not, it generates a new problem by looking up the neighbor of the
origination node in the graph.

On occasion, {{\tt route-exists\char'077}\/} fails to produce an answer if the graph
contains a cycle. In section~\ref{sec:loss:generative} we solved the
problem with an accumulator. It is also possible to solve it with a state
variable that keeps track of the origination nodes that
{{\tt route-exists\char'077}\/} has visited for one particular attempt. Modify the
function appropriately.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/route-set.html}{\rule{3pt}{5pt}} 




{\bf Exercise 37.3.6}

 \label{ex:files-set}
In section~\ref{sec:files-models}, we developed several simple models of
a computer's file system. Develop the function {{\tt dir-listing}\/}, which
consumes a directory and produces a list of all file names in the directory
and all of its subdirectories. The function also sets the state variable 
{{\tt how-many-directories}\/} to the number of subdirectories it encounters
during the process.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/files-set.html}{\rule{3pt}{5pt}}


\subsection{Finger Exercises on State Changes} \label{sec:finger-state}

\htmladdimg{../icons/teacher.gif}~{\em Many of these exercises require solutions from previous (extended) exercises. The students can often do the exercises here even if they didn't solve those from previous sections by using our solutions. This requires students to read programs and to understand them.}

{\bf Exercise 37.4.1}

 \label{ex:number-guess}
Modify {{\tt check-guess-for-list}\/} from exercise~\ref{ex:guess3} so that
it also counts how many times the player has clicked on the ``Check''
button of the interface. \noindent{\bf Hint:} \ The call-back function for the button uses
{{\tt check-guess-for-list}\/} once per click.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/number-guess.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.4.2}

 \label{ex:taskQ}
Develop a program that manages a task queue. The program should support at
least four services:
\begin{enumerate}
\item {{\tt enter}\/}, which adds a task to end of the queue; 
\item {{\tt next}\/}, which determines the next task in the queue, if any; 
\item {{\tt remove}\/}, which removes the first task from the queue, if any; 
\item and {{\tt count}\/}, which computes the number of items in the queue. 
\end{enumerate}
A user should start the task manager with {{\tt start-task-manager}\/}. 

After the program is developed and tested, use {\tt\bf gui.ss} to develop a
graphical user interface to the task manager. The interface should start up
with a friendly message and should always display the first task in the
queue and the number of items in the queue:

  \begin{rawhtml}  <table  cellspacing=20 bgcolor=beige>  <tr> <td align=left><img src=../icons/taskq1.gif></td>       <td align=center><img src=../icons/taskq2.gif></td>       <td align=right><img src=../icons/taskq3.gif></td>  </tr>  </table>  \end{rawhtml}

\noindent Unless the queue is empty, clicking the ``Next'' button should
remove an item from the queue and display the first item in the remainder
of the queue. If the queue is empty, clicking the ``Next'' button should
have no effect.

\noindent{\bf Hint:} \ The greeting and the year are two separate message items.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/taskQ.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.4.3}

 \label{ex:move-pic-set}
In section~\ref{sec:move-fig}, we developed a program that moves pictures
across a canvas. A picture is a list of shapes; the program consists of
functions that draws, erases, and translates pictures. The main function is
{{\tt move}\/} (exercise~\ref{ex:moving}). It consumes a picture and a
number {{\tt n}\/}. It produces a new picture, moved by {{\tt n}\/} pixels;
it also erases the original picture and draws the new one. 

Develop the program {{\tt drive}\/}. It draws a (fixed) picture on a canvas
and permits players to move the picture left or right by a player-specified
number of pixels.

Modify {{\tt drive}\/} so that it also keeps track of some given amount of
fuel. A move by one pixel should consume a fixed amount of
fuel.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/move-pic-set.html}{\rule{3pt}{5pt}}



{\bf Exercise 37.4.4}

 \label{ex:intersection}
Modify the two functions that control the state of a single traffic light
so that they control the state of the two traffic lights at an ordinary
intersection. Each light can assume one of three states: {{\tt \char'047}{\tt red}\/},
{{\tt \char'047}{\tt green}\/}, and {{\tt \char'047}{\tt yellow}\/}. When one is {{\tt \char'047}{\tt green}\/} or
{{\tt \char'047}{\tt yellow}\/}, the other one must be {{\tt \char'047}{\tt red}\/}. 

Recall the basics about the two functions for a single traffic light: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt init-traffic-light}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/} }\\
\hbox{;; effects: (1) to initialize {{\tt current-color}\/}; (2) to draw traffic light}
\end{tabbing}\end{minipage}\end{center}
and 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt next}\ {\tt :}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; {effects}: (1) to change {{\tt current-color}\/} from {{\tt \char'047}{\tt green}\/} to {{\tt \char'047}{\tt yellow}\/}, }
\\
\hbox{;; {{\tt \char'047}{\tt yellow}\/} to {{\tt \char'047}{\tt red}\/}, and {{\tt \char'047}{\tt red}\/} to {{\tt \char'047}{\tt green}\/}}
\\
\hbox{;; (2) to re-draw the traffic light appropriately }
\end{tabbing}\end{minipage}\end{center}
Modify the basics first. 

When the program is developed and tested, develop a graphical display like
the following: 

  \begin{rawhtml}   <center><img src=../icons/traffic.gif alt="[An intersection]"></center>  \end{rawhtml}

\noindent Use the functions {{\tt init-traffic-light}\/} and {{\tt next}\/}
to drive the display, but keep the functions that display information
separate from these two functions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/intersection.html}{\rule{3pt}{5pt}}

 
{\bf Exercise 37.4.5}

 \label{ex:repl}
In sections~\ref{sec:interpreter} and~\ref{sec:interpreter2} we developed
an evaluator for a portion of Scheme. A typical Scheme implementation also
provides an {\sc {interactive}} user mode. In DrScheme, the
{\tt Interactions} window plays this role.

An interactive system prompts the reader for definitions and expressions,
evaluates them appropriately, and possibly produces some result.
Definitions are added to some repository; to confirm the addition, the
interactive system may produce a value like {{\tt true}\/}. Expressions are
evaluated relative to the definition repository. The function
{{\tt interpret-with-defs}\/} from section~\ref{sec:interpreter2} performs
this role. 

Develop an interaction system around {{\tt interpret-with-defs}\/}. The
system should provide at least two services: 
\begin{enumerate}
\item {{\tt add-definition}\/}, which adds (the representation of) some
function definition to the system's repository; 
\item {{\tt evaluate}\/}, which consumes (the representation of) some
expression and evaluates it relative to the current repository. 
\end{enumerate}
If a user adds two (or more) definitions for some function {{\tt f}\/}, the
last addition is the one that matters. The previous ones can be
ignored.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/repl.html}{\rule{3pt}{5pt}}


\subsection{Extended Exercise: Exploring Places} \label{sec:explore-state}

\hrule

~\bigskip\\
  \begin{rawhtml}   <center><img src=../icons/rice-tour.gif alt="[A Rice University Tour in DrScheme]"></center>  \end{rawhtml}\\
\bigskip

\center{Figure: A tour of a university} \label{fig:rice-tour}
\hrule

Early computer games asked players to find their way through dangerous
mazes and caves. The player wanders from cave to cave, finds treasures,
encounters creatures of all kinds, fights, kisses, picks up energy, and
eventually reaches paradise. This section lays out the basics of such a
game, using our iterative approach to program design.

Our tour takes place at one of the scariest places of all: campus. A campus
consists of buildings, one more dangerous than the other. Each building has
a name and is connected to a bunch of other buildings. 

The player is always in a building. We refer to this building as the {\sl current location\/}. To find out more about the location, the player can
ask for a picture of the building and for the list of connections. The
player can also move to one of the connected buildings by issuing a
{{\tt go}\/} command. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 37.5.1}

 \label{ex:tour-data}
Provide structure and data definitions for buildings. Include a picture
field in the structure. 

A campus is a list of buildings. Define a sample campus. See
figure~\ref{fig:rice-tour} for a small example.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tour-data.html}{\rule{3pt}{5pt}}


{\bf Exercise 37.5.2}

 \label{ex:tour-showme}
Develop a program that permits a player to move through the sample campus
of exercise~\ref{ex:tour-data}. The program should support at least three
services: 
\begin{enumerate}
\item {{\tt show-me}\/}, which produces the picture of the current location:
see figure~\ref{fig:rice-tour2}; 
\item {{\tt where-2-go}\/}, which produces the list of connected buildings;
\item {{\tt go}\/}, which changes the current location of the player. 
\end{enumerate}
If the player issues the command {{\tt (go}\ {\tt s)}\/} and {{\tt s}\/} is not
connected to the current location, the function must report an error. 
Develop other functions as necessary or as
desired.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tour-showme.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


\hrule

~\bigskip\\
  \begin{rawhtml}   <center><img src=../icons/rice-tour.gif alt="[A Rice University Tour in DrScheme]"></center>  \end{rawhtml}\\
\bigskip

\center{Figure: Take the tour} \label{fig:rice-tour2}
\hrule

Players of early maze games could also gather objects and trade objects at
the various sites. Specifically, the player had a bag for carrying objects, 
and each site contained objects that the player could trade for things in 
the bag. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 37.5.3}

 \label{ex:tour-trade}
Modify the tour program so that each building contains one object.
Furthermore, the player should have a {\sl bag\/} that contains (at most)
one object. At each location, the player can pick up an object, if the bag
is empty, or swap the object in the bag for the object in the building
otherwise.

Modify the program further so that the player can carry an arbitrary number
of objects in the bag.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/tour-trade.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


The three exercises in this section illustrate how maze games work. From
here it is easy to experiment with various flavors of the game. Taking a
walk from one building to another may take some energy, and the player may
have only a finite amount of energy. Creatures may fight or kiss the
player, which consumes or replenishes the player's energy level. Use your
imagination to extend the game and have friends take the tour.


%% BEGIN INLINED FILE: curriculumF.dl.tex




 \part{{Intermezzo 7: The Final Syntax and Semantics}}  \section{{Intermezzo 7: The Final Syntax and Semantics}}  
\label{sec:synsemF}

With the introduction of {{\bf set!}-exp\-res\-sion}s and {{\bf begin}-exp\-res\-sion}s we have covered most
of Scheme's kernel language.  In DrScheme, this portion is called
{\tt Advanced Student} Scheme. Considering the complexity of {{\tt set!}\/}, this
is a good place to summarize our programming language in the spirit of
intermezzo~1. Following the organization of that intermezzo, we discuss the
vocabulary, the grammar, and the meaning of {\tt Advanced Student} Scheme.  The
last subsection explains what kind of errors we may encounter in
{\tt Advanced Student} Scheme.

 \subsection{The Vocabulary of Advanced Scheme}
\label{sec:advanced:vocab}

The foundation of any language is its vocabulary. In {\tt Beginning Student}
Scheme, we distinguish four categories of words: variables, constants,
primitive functions, and keywords. The classification ignores parentheses
but we know that every compound phrase is surrounded by a pair of
parentheses, and that every atomic phrase stands on its own. 

{\tt Advanced Student} Scheme respects this basic classification, though it
contains four important new keywords: {{\tt local}\/}, {{\tt lambda}\/},
{{\tt set!}\/}, and {{\tt begin}\/}. The first two are important for
organizing and abstracting programs; the last two are important for the
computation of effects. Still, keywords {\em per se\/} have no
meaning. They are road signs that tell us what is ahead so that we can
orient ourselves. It is the grammar and the meaning of a language that
explains the role of the keywords. 

 \subsection{The Grammar of Advanced Scheme} \label{sec:advanced:grammar}

Even though Scheme is a full-fledged language with as much power as any
other programming language, its designers have kept its grammar simple. The
grammar of {\tt Advanced Student} Scheme, which is most of Scheme, is only a few
lines longer than that of {\tt Beginning Student} Scheme.

\hrule
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{def}>}} 
  &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ ({\bf define} (\mbox{{\tt <{var}>}} \mbox{{\tt <{var}>}} \ldots \mbox{{\tt <{var}>}}) \mbox{{\tt <{exp}>}}) \\
  &&  $\,\,\vert\,\,$ ({\bf define} \mbox{{\tt <{var}>}} \mbox{{\tt <{exp}>}}) \\
  &&  $\,\,\vert\,\,$ ({\bf define-struct} $\mbox{{\tt <{var0}>}}$ ($\mbox{{\tt <{var-1}>}} \ldots \mbox{{\tt <{var-n}>}}$))\\
\ \\
\mbox{{\tt <{exp}>}} 
  &=& $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ \mbox{{\tt <{var}>}} \\
  && $\,\,\vert\,\,$ \mbox{{\tt <{con}>}} \\
  && $\,\,\vert\,\,$ \mbox{{\tt <{prm}>}} \\
     && $\,\,\vert\,\,$ (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}} \ldots \mbox{{\tt <{exp}>}})\\
  && $\,\,\vert\,\,$ ({\bf cond} (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}) \ldots (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}))\\
  && $\,\,\vert\,\,$ ({\bf cond} (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}}) \ldots ({{\tt else}\/} \mbox{{\tt <{exp}>}}))\\
  && $\,\,\vert\,\,$ ({\bf local} (\mbox{{\tt <{def}>}} \ldots \mbox{{\tt <{def}>}}) \mbox{{\tt <{exp}>}}) \\
  && $\,\,\vert\,\,$ ({\bf lambda} (\mbox{{\tt <{var}>}} \ldots \mbox{{\tt <{var}>}}) \mbox{{\tt <{exp}>}})\\
  && $\,\,\vert\,\,$ ({\bf set!} \mbox{{\tt <{var}>}} \mbox{{\tt <{exp}>}})\\
  && $\,\,\vert\,\,$ ({\bf begin} \mbox{{\tt <{exp}>}} \ldots \mbox{{\tt <{exp}>}})
\end{tabular}
\end{center}
\center{Figure: {\tt Advanced Student} Scheme: The core grammar} \label{fig:syn-advanced}
\hrule

Figure~\ref{fig:syn-advanced} contains the essential grammar of
{\tt Advanced Student} Scheme.\footnote{The grammar misses {\bf and}-exp\-res\-sion\ and {\bf or}-exp\-res\-sion, and a few other short-cuts.} The extensions of {\tt Beginning Student}
Scheme are four new forms of expressions: {{\bf local}-exp\-res\-sion}s, {{\bf lambda}-exp\-res\-sion}s,
{{\bf set!}-exp\-res\-sion}s, and {{\bf begin}-exp\-res\-sion}s. The specification of {{\bf local}-exp\-res\-sion}s refers to
the category of definitions, which refers back to the category of
expressions. A {\bf lambda}-exp\-res\-sion\ consists of a sequence of variables, enclosed in
parentheses, and an expression. Similarly, a {\bf set!}-exp\-res\-sion\ consists of a
variable and an expression. Finally, a {\bf begin}-exp\-res\-sion\ is just a sequence of
expressions prefixed with the keyword {{\tt begin}\/} to distinguish it from
an application.

Since functions are values now, the grammar of {\tt Advanced Student} Scheme is
also simpler than that of {\tt Beginning Student} Scheme in one
regard. Specifically, it merges the lines for primitive and function
application into a single line. The new line specifies applications, which
are now sequences of expressions enclosed in parentheses. Due to the
inclusion of primitive operations into the set of expressions,
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (+}\  {\tt 1}\  {\tt 2)}\end{tabbing}\end{minipage}\end{center}
is still an expression. After all, {{\tt +}\/} is now an expression, and so
are {{\tt 1}\/} and {{\tt 2}\/}. The application of {{\tt define}\/}d
functions works similarly: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (f}\  {\tt 1}\  {\tt 2)}\end{tabbing}\end{minipage}\end{center}
The first expression is a variable, the others are numbers.  The
application is thus a legal expression.

Unfortunately, a language grammar can only specify the large contours of
what is a legal sentence construction. It cannot express restrictions that
require some knowledge about the context of a phrase. {\tt Advanced Student}
Scheme requires a few such restrictions: 
\begin{enumerate}
\item In a {\bf lambda}-exp\-res\-sion\ no variable may occur twice in the parameter
sequence. 

\item In a {\bf local}-exp\-res\-sion\ no definition may introduce the same variable as any
other definition in the same sequence. 

\item A {\bf set!}-exp\-res\-sion\ must occur in the lexical scope of a {{\tt define}\/} that
introduces the {\bf set!}-exp\-res\-sion's left-hand side. 
\end{enumerate}
In addition, the old restriction applies that keywords cannot be used as
variables. 

Consider the following definition, which uses the four new constructs: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt switch}\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define-struct}\  {\tt hide}\  {\tt (it))}
\\
\>\>\>{\tt (define}\  {\tt state}\  {\tt (make-hide}\  {\tt 1)))}
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt ()}
\\
\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (set!}\  {\tt state}\  {\tt (make-hide}\  {\tt (-}\  {\tt 1}\  {\tt (hide-it}\  {\tt state))))}
\\
\>\>\>\>{\tt state))))}
\end{tabbing}\end{minipage}\end{center}
The definition introduces the variable {{\tt switch}\/}. The right-hand side
of the definition is a {\bf local}-exp\-res\-sion. It in turn defines the structure
{{\tt hide}\/} and the variable {{\tt state}\/}, which stands for an instance
of {{\tt hide}\/}. The body of the {\bf local}-exp\-res\-sion\ is a {\bf lambda}-exp\-res\-sion, whose parameter
sequence is empty. The function's body consists of a {\bf begin}-exp\-res\-sion\ with two
expressions: a {\bf set!}-exp\-res\-sion\ and an expression that consists of just the
variable {{\tt state}\/}.

All subexpressions in our program satisfy the necessary
restrictions. First, the {\bf local}-exp\-res\-sion\ introduces four distinct names:
{{\tt make-hide}\/}, {{\tt hide?}\/}, {{\tt hide-it}\/}, and
{{\tt state}\/}. Second, the parameter list of the {\bf lambda}-exp\-res\-sion\ is empty, so
there is no possible conflict.  Finally, the {\bf set!}-exp\-res\-sion's variable is the
{{\tt local}\/}ly defined variable {{\tt state}\/}, so it is legal, too.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 38.2.1}

 \label{ex:adv:syn0} 
Determine whether the following phrases are syntactically legal or illegal
programs:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$1.$}\  {\tt (defin}\={\tt e}\  {\tt (f}\  {\tt x)}\  \\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt y}\  {\tt x)}
\\
\>\>{\tt x))}
\\
\hbox{$2.$}\  {\tt (defin}\={\tt e}\  {\tt (f}\  {\tt x)}\  
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt f}\  {\tt x)}
\\
\>\>{\tt x))}
\\
\hbox{$3.$}\  {\tt (local}\=\  {\tt ((def}\={\tt ine-struct}\  {\tt hide}\  {\tt (it))}
\\
\>\>{\tt (define}\  {\tt make-hide}\  {\tt 10))}
\\
\>{\tt (hide?}\  {\tt 10))}
\\
\hbox{$4.$}\  {\tt (local}\=\  {\tt ((def}\={\tt ine-struct}\  {\tt loc}\  {\tt (con))}
\\
\>\>{\tt (define}\  {\tt loc}\  {\tt 10))}
\\
\>{\tt (loc?}\  {\tt 10))}
\\
\hbox{$5.$}\  {\tt (def}\={\tt in}\={\tt e}\  {\tt f}
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt (x}\  {\tt y}\  {\tt x)}
\\
\>\>\>{\tt (*}\  {\tt x}\  {\tt y}\  {\tt z)))}
\\
\>\relax
\\
\>{\tt (define}\  {\tt z}\  {\tt 3.14)}
\end{tabbing}\end{minipage}\end{center}
Explain why a phrase is legal or illegal.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/adv:syn0.html}{\rule{3pt}{5pt}}


 \subsection{The Meaning of Advanced Scheme} \label{sec:advanced:sem} 

When we first extended our language from {\tt Intermediate Student}
Scheme to {\tt Advanced Student} Scheme, we did so because we wanted to
deal with functions as ordinary values. The evaluation rules barely
changed. We just agreed to allow expressions in the first position of an
application and to deal with the names of functions as values.

The extension of the language with {{\bf set!}-exp\-res\-sion}s required another change to
our rules. Now definitions that associate variables and values can change
over the course of an evaluation. The informal rules we used so far deal
with changes to the definition of state variables, because they matter the
most. But the rules are informal and imprecise, so a precise description of
how the addition of {{\tt set!}\/} changes the meaning of the language must
be our primary concern.

Let's recall how we determine the meaning of a program. A program consists
of two parts: a collection of definitions and an expression. The goal is to
evaluate the expression, which means to determine the expression's
value.\footnote{We also evaluate the right-hand side of definitions if they are not values, but we can safely ignore this minor issue here.} In
{\tt Beginning Student} Scheme, the collection of values consists of all the
constants plus lists. Only one list has a concise representation: the empty
one. All other lists are written down as a series of {{\tt cons}\/}es.

The evaluation of an expression consists of a series of steps. At each step
we use the laws of arithmetic and algebra to simplify a subexpression. This
yields another expression. We also say that we {\sc {rewrite}} the first
expression into the second. If the second expression is a value, we are
finished. 

The introduction of {{\bf set!}-exp\-res\-sion}s into our programming language requires a
few small adjustments and extensions to this process: 
\begin{enumerate}
\item Instead of rewriting just an expression, we must now rewrite
  definitions and expressions. More precisely, each step changes the
  expression and possibly the definition of a state variable. To make these
  effects as obvious as possible, each stage in an evaluation displays the
  definitions of state variables and the current expression.

\item Furthermore, it is no longer possible to apply the laws of arithmetic
  and algebra whenever or wherever we want. Instead, we must determine the
  subexpression that we must evaluate if we wish to make progress. This
  rule still leaves us with choices. For example, to rewrite the expression 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (+}\  {\tt (*}\  {\tt 3}\  {\tt 3)}\  {\tt (*}\  {\tt 4}\  {\tt 4))}\end{tabbing}\end{minipage}\end{center}
  we may choose to evaluate {{\tt (*}\ {\tt 3}\ {\tt 3)}\/} and then {{\tt (*}\ {\tt 4}\ {\tt 4)}\/} or
  \emph{vice versa}. Fortunately, for such simple expressions, the choice
  doesn't affect the final outcome, so we don't have to supply a complete
  unambigous rule. In general, though, we rewrite subexpressions in a
  left-to-right and top-to-bottom order. At each stage in the evaluation,
  we best start by underlining the subexpression that must be evaluated
  next.

\item Suppose the underlined subexpression is a {\bf set!}-exp\-res\-sion. By the
  restrictions on {{\bf set!}-exp\-res\-sion}s, we know that there is a {{\tt define}\/} for
  the left-hand side of the subexpression. That is, we are facing the
  following situation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (define}\  {\tt x}\  {\tt aValue)}\\
\>{\tt ...}
\\
\>{\tt ...}\  \hbox{$\underline{\mbox{{{\tt (set!}\ {\tt x}\ {\tt anotherValue)}\/}}}$}\  {\tt ...}\  
\\
{\tt =}\  \={\tt (define}\  {\tt x}\  {\tt anotherValue)}
\\
\>{\tt ...}
\\
\>{\tt ...}\  {\tt (}{\sf void}{\tt )}\  {\tt ...}
\end{tabbing}\end{minipage}\end{center}
  The equation indicates that the program changes in two ways. First, the
  variable definition is modified. Second, the underlined {\bf set!}-exp\-res\-sion\ is
  replaced by {{\tt (}{\sf void}{\tt )}\/}, the invisible value. 

\item The next change concerns the replacement of variables in expressions
  with the value in their definition. Until now, we could replace a
  variable by its value wherever we thought it was convenient or
  necessary. Indeed, we just thought of the variable as a shorthand for the
  value. With {{\bf set!}-exp\-res\-sion}s in the language, this is no longer
  possible. After all, the evaluation of a {\bf set!}-exp\-res\-sion\ modifies the
  definition of a state variable, and if we replace a variable by its value
  at the wrong time, we get the wrong value. 

  Thus, suppoe that the underlined expression is a (state) variable. Then
  we know that we can't make any progress in our evaluation until we have
  replaced the variable by the current value in its definition. This
  suggests the following revised law for variable evaluation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (define}\  {\tt x}\  {\tt aValue)}\\
\>{\tt ...}
\\
\>{\tt ...}\  \hbox{$\underline{{{\tt x}\/}}$}\  {\tt ...}\  
\\
{\tt =}\  \={\tt (define}\  {\tt x}\  {\tt aValue)}
\\
\>{\tt ...}
\\
\>{\tt ...}\  {\tt aValue}\  {\tt ...}\  
\end{tabbing}\end{minipage}\end{center}
  In short, substitute the value in a state variable definition for the
  state variable only when the value is needed for this particular
  occurrence of the state variable. 

\item Last, but not least, we also need a rule for {{\bf begin}-exp\-res\-sion}s. The
  simplest one says to drop the first subexpression if it is a value: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (begin}\  {\tt v}\  {\tt exp-1}\  {\tt ...}\  {\tt exp-n)}\\
{\tt =}\  {\tt (begin}\  {\tt exp-1}\  {\tt ...}\  {\tt exp-n)}
\end{tabbing}\end{minipage}\end{center} 
  That means, we also need a rule for dropping {{\tt begin}\/} completely: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (begin}\  {\tt exp)}\\
{\tt =}\  {\tt exp}
\end{tabbing}\end{minipage}\end{center} 
In addition, we use a rule for dropping several values at once: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (begin}\  {\tt v-1}\  {\tt ...}\  {\tt v-m}\  {\tt exp-1}\  {\tt ...}\  {\tt exp-n)}\\
{\tt =}\  {\tt (begin}\  {\tt exp-1}\  {\tt ...}\  {\tt exp-n)}
\end{tabbing}\end{minipage}\end{center} 
 But this is only a convenience, nothing else. 
\end{enumerate}
Although the laws are more complicated than those of {\tt Beginning Student}
Scheme, they are still manageable. 

\htmladdimg{../icons/teacher.gif}~{\em The programming language is rich enough to describe all kinds of computations, including those of object-oriented languages. Considering this power, the rules are actually simple.}

Let's consider some examples. The first one demonstrates how the order of
evaluation of subexpressions makes a difference: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (define}\  {\tt x}\  {\tt 5)}\\
\>{\tt (+}\  {\tt (begin}\  \hbox{$\underline{\mbox{{{\tt (set!}\ {\tt x}\ {\tt 11)}\/}}}$}\  {\tt x)}\  {\tt x)}
\\
{\tt =}\  \={\tt (define}\  {\tt x}\  {\tt 11)}
\\
\>{\tt (+}\  {\tt (begin}\  {\tt (}{\sf void}{\tt )}\  \hbox{$\underline{{{\tt x}\/}}$}{\tt )}\  {\tt x)}
\\
{\tt =}\  {\tt ...}
\\
{\tt =}\  \={\tt (define}\  {\tt x}\  {\tt 11)}
\\
\>{\tt (+}\  {\tt 11}\  \hbox{$\underline{{{\tt x}\/}}$}{\tt )}
\\
{\tt =}\  \={\tt (define}\  {\tt x}\  {\tt 11)}
\\
\>\hbox{$\underline{\mbox{{{\tt (+}\ {\tt 11}\ {\tt 11)}\/}}}$}
\end{tabbing}\end{minipage}\end{center}
The program consists of one definition and one addition, which is to be
evaluated. One of the addition's arguments is a {\bf set!}-exp\-res\-sion\ that mutates
{{\tt x}\/}; the other is just {{\tt x}\/}. By evaluating the subexpressions
of the addition from left to right, the mutation takes place before we
replace the second subexpression with its value. As a result, the outcome
is {{\tt 22}\/}. If we had evaluated the addition from right to left, the
result would have been {{\tt 16}\/}. To avoid such problems, we use the
fixed ordering but give ourselves more freedom when no state variables are
involved.

The second example illustrates how a {\bf set!}-exp\-res\-sion\ that occurred in a {\bf local}-exp\-res\-sion\
actually affects a top-level definition:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (d}\={\tt efine}\  {\tt (make-counter}\  {\tt x0)}\\
\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt counter}\  {\tt x0)}
\\
\>\>\>\>{\tt (d}\={\tt efine}\  {\tt (increment)}
\\
\>\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>\>{\tt (set!}\  {\tt counter}\  {\tt (+}\  {\tt counter}\  {\tt 1))}
\\
\>\>\>\>\>\>{\tt counter)))}
\\
\>\>\>{\tt increment))}
\\
\>{\tt (}\hbox{$\underline{\mbox{{{\tt (make-counter}\ {\tt 0)}\/}}}$}{\tt )}
\end{tabbing}\end{minipage}\end{center}
The program again consists of a single definition and an expression that is
to be evaluated. The latter, however, is an application nested in an
application. The inner application is underlined, because we must evaluate
it to make progress. Here are the first few steps: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt ...}\\
{\tt =}\  \={\tt (d}\={\tt efine}\  {\tt (make-counter}\  {\tt x0)}
\\
\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt counter}\  {\tt x0)}
\\
\>\>\>\>{\tt (d}\={\tt efine}\  {\tt (increment)}
\\
\>\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>\>{\tt (set!}\  {\tt counter}\  {\tt (+}\  {\tt counter}\  {\tt 1))}
\\
\>\>\>\>\>\>{\tt counter)))}
\\
\>\>\>{\tt increment))}
\\
\>{\tt ((l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt counter}\  {\tt 0)}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (increment)}
\\
\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>{\tt (set!}\  {\tt counter}\  {\tt (+}\  {\tt counter}\  {\tt 1))}
\\
\>\>\>\>\>{\tt counter)))}
\\
\>\>{\tt increment))}
\\
{\tt =}\  \={\tt (d}\={\tt efine}\  {\tt (make-counter}\  {\tt x0)}
\\
\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt counter}\  {\tt x0)}
\\
\>\>\>\>{\tt (d}\={\tt efine}\  {\tt (increment)}
\\
\>\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>\>{\tt (set!}\  {\tt counter}\  {\tt (+}\  {\tt counter}\  {\tt 1))}
\\
\>\>\>\>\>\>{\tt counter)))}
\\
\>\>\>{\tt increment))}
\\
\>{\tt (define}\  {\tt counter1}\  {\tt 0)}
\\
\>{\tt (d}\={\tt efine}\  {\tt (increment1)}
\\
\>\>{\tt (b}\={\tt egin}
\\
\>\>\>{\tt (set!}\  {\tt counter1}\  {\tt (+}\  {\tt counter1}\  {\tt 1))}
\\
\>\>\>{\tt counter1))}
\\
\>{\tt (increment1)}
\end{tabbing}\end{minipage}\end{center}
The evaluation of the {\bf local}-exp\-res\-sion\ created additional top-level
expressions. One of them introduces a state variable; the others define
functions.

The second part of the evaluation determines what {{\tt (increment1)}\/}
accomplishes: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (define}\  {\tt counter1}\  {\tt 0)}\\
\>\hbox{$\underline{\mbox{{{\tt (increment1)}\/}}}$}
\\
{\tt =}\  \={\tt (define}\  {\tt counter1}\  {\tt 0)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt counter1}\  {\tt (+}\  \hbox{$\underline{\mbox{{{\tt counter1}\/}}}$}\  {\tt 1))}
\\
\>\>{\tt counter1)}
\\
{\tt =}\  \={\tt (define}\  {\tt counter1}\  {\tt 0)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt counter1}\  \hbox{$\underline{\mbox{{{\tt (+}\ {\tt 0}\ {\tt 1)}\/}}}$}{\tt )}
\\
\>\>{\tt counter1)}
\\
{\tt =}\  \={\tt (define}\  {\tt counter1}\  {\tt 0)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>\hbox{$\underline{\mbox{{{\tt (set!}\ {\tt counter1}\ {\tt 1)}\/}}}$}
\\
\>\>{\tt counter1)}
\\
{\tt =}\  \={\tt (define}\  {\tt counter1}\  {\tt 1)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (}{\sf void}{\tt )}
\\
\>\>\hbox{$\underline{\mbox{{{\tt counter1}\/}}}$}{\tt )}
\\
{\tt =}\  \={\tt (define}\  {\tt counter1}\  {\tt 1)}
\\
\>{\tt 1}
\end{tabbing}\end{minipage}\end{center}
During the evaluation, we replace {{\tt counter1}\/} with its value
twice. First, the second step replaces {{\tt counter1}\/} with {{\tt 0}\/},
its value at that point. Second, we substitute {{\tt 1}\/} for
{{\tt counter1}\/} during the last step, which is its new
value. 


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 38.3.1}

 \label{ex:adv:sem0} 
Underline the subexpression that must be evaluated next in the following
expressions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$1.$}\  {\tt (def}\={\tt ine}\  {\tt x}\  {\tt 11)}\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt x}\  {\tt (*}\  {\tt x}\  {\tt x))}
\\
\>\>{\tt x)}
\\
\hbox{$2.$}\  {\tt (def}\={\tt ine}\  {\tt x}\  {\tt 11)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (s}\={\tt et!}\  {\tt x}\  
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt 0)}\  {\tt 22\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (/}\  {\tt 1}\  {\tt x)\char'135}{\tt ))}
\\
\>\>{\tt \char'047}{\tt done)}
\\
\hbox{$3.$}\  {\tt (def}\={\tt in}\={\tt e}\  {\tt (run}\  {\tt x)}
\\
\>\>{\tt (run}\  {\tt x))}
\\
\>{\tt (run}\  {\tt 10)}
\\
\hbox{$4.$}\  {\tt (def}\={\tt ine}\  {\tt (f}\  {\tt x)}\  {\tt (*}\  {\tt pi}\  {\tt x}\  {\tt x))}
\\
\>{\tt (define}\  {\tt a1}\  {\tt (f}\  {\tt 10))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt a1}\  {\tt (-}\  {\tt a1}\  {\tt (f}\  {\tt 5)))}
\\
\>\>{\tt \char'047}{\tt done)}
\\
\hbox{$5.$}\  {\tt (def}\={\tt in}\={\tt e}\  {\tt (f)}
\\
\>\>{\tt (set!}\  {\tt state}\  {\tt (-}\  {\tt 1}\  {\tt state)))}
\\
\>{\tt (define}\  {\tt state}\  {\tt 1)}
\\
\>{\tt (f}\  {\tt (f}\  {\tt (f)))}
\end{tabbing}\end{minipage}\end{center}
Explain why the expression must be evaluated.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/adv:sem0.html}{\rule{3pt}{5pt}}


{\bf Exercise 38.3.2}

 \label{ex:adv:sem1} 
Confirm that the underlined expressions must be evaluated next: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$1.$}\  {\tt (def}\={\tt ine}\  {\tt x}\  {\tt 0)}\\
\>{\tt (define}\  {\tt y}\  {\tt 1)}
\\
\>{\tt (b}\={\tt egin}\  
\\
\>\>\hbox{$\underline{\mbox{{{\tt (set!}\ {\tt x}\ {\tt 3)}\/}}}$}
\\
\>\>{\tt (set!}\  {\tt y}\  {\tt 4)}
\\
\>\>{\tt (+}\  {\tt (*}\  {\tt x}\  {\tt x)}\  {\tt (*}\  {\tt y}\  {\tt y)))}
\\
\hbox{$2.$}\  {\tt (def}\={\tt ine}\  {\tt x}\  {\tt 0)}
\\
\>{\tt (s}\={\tt et!}\  {\tt x}\  
\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (zero?}\  \hbox{$\underline{\mbox{{{\tt x}\/}}}$}{\tt )}\  {\tt 1\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt 0\char'135}{\tt ))}
\\
\hbox{$3.$}\  {\tt (def}\={\tt in}\={\tt e}\  {\tt (f}\  {\tt x)}
\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt x)}\  {\tt 1\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt 0\char'135}{\tt ))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>\hbox{$\underline{\mbox{{{\tt (set!}\ {\tt f}\ {\tt 11)}\/}}}$}
\\
\>\>{\tt f)}
\end{tabbing}\end{minipage}\end{center}
Rewrite the three programs to show the next state.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/adv:sem1.html}{\rule{3pt}{5pt}}


{\bf Exercise 38.3.3}

 \label{ex:adv:semN} 
Evaluate the following programs: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$1.$}\  {\tt (def}\={\tt ine}\  {\tt x}\  {\tt 0)}\\
\>{\tt (d}\={\tt efine}\  {\tt (bump}\  {\tt delta)}
\\
\>\>{\tt (b}\={\tt egin}
\\
\>\>\>{\tt (set!}\  {\tt x}\  {\tt (+}\  {\tt x}\  {\tt delta))}
\\
\>\>\>{\tt x))}
\\
\>{\tt (+}\  {\tt (bump}\  {\tt 2)}\  {\tt (bump}\  {\tt 3))}
\\
\hbox{$2.$}\  {\tt (def}\={\tt ine}\  {\tt x}\  {\tt 10)}
\\
\>{\tt (set!}\  {\tt x}\  {\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zeor\char'077}\  {\tt x)}\  {\tt 13\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (/}\  {\tt 1}\  {\tt x)\char'135}{\tt ))}
\\
\hbox{$3.$}\  {\tt (def}\={\tt in}\={\tt e}\  {\tt (make-box}\  {\tt x)}
\\
\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt contents}\  {\tt x)}
\\
\>\>\>\>{\tt (d}\={\tt efine}\  {\tt (new}\  {\tt y)}
\\
\>\>\>\>\>{\tt (set!}\  {\tt x}\  {\tt y))}
\\
\>\>\>\>{\tt (d}\={\tt efine}\  {\tt (peek)}
\\
\>\>\>\>\>{\tt x))}
\\
\>\>\>{\tt (list}\  {\tt new}\  {\tt peek))}
\\
\>\relax
\\
\>{\tt (define}\  {\tt B}\  {\tt (make-box}\  {\tt 55))}
\\
\>{\tt (define}\  {\tt C}\  {\tt (make-box}\  {\tt \char'047}{\tt a))}
\\
\>\relax
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt ((first}\  {\tt B)}\  {\tt 33)}
\\
\>\>{\tt ((second}\  {\tt C)))}
\end{tabbing}\end{minipage}\end{center}
Underline for each step the subexpression that must be evaluated next. 
Only show those steps that involve a {\bf local}-exp\-res\-sion\ or a
{\bf set!}-exp\-res\-sion.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/adv:semN.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt} 


In principle, we could work with the rules we just discussed. They cover
the common cases, and they explain the behavior of the programs we have
encountered. They do not explain, however, how an assignment works when the
left-hand side refers to a {{\tt define}\/}d function. Consider the
following example, for which the rules still work:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (define}\  {\tt (f}\  {\tt x)}\  {\tt x)}\\
\>\relax
\\
\>{\tt (b}\={\tt egin}\  
\\
\>\>\hbox{$\underline{\mbox{{{\tt (set!}\ {\tt f}\ {\tt 10)}\/}}}$}
\\
\>\>{\tt f)}
\\
{\tt =}\  \={\tt (define}\  {\tt f}\  {\tt 10)}
\\
\>\relax
\\
\>{\tt (b}\={\tt egin}\  
\\
\>\>{\tt (}{\sf void}{\tt )}
\\
\>\>{\tt f)}
\end{tabbing}\end{minipage}\end{center}
Here {{\tt f}\/} is a state variable. The {\bf set!}-exp\-res\-sion\ changes the definition
so {{\tt f}\/} stands for a number. The next step in an evaluation
substitutes {{\tt 10}\/} for the occurrence of {{\tt f}\/}.

Under ordinary circumstances, an assignment would replace a function
definition with a different function definition. Take a look at this
program: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (define}\  {\tt (f}\  {\tt x)}\  {\tt x)}\\
\>{\tt (define}\  {\tt g}\  {\tt f)}
\\
\>{\tt (+}\  {\tt (begin}\  \hbox{$\underline{\mbox{{{\tt (set!}\ {\tt f}\ {\tt (lambda}\ {\tt (x)}\ {\tt 22))}\/}}}$}\  {\tt 5)}\  {\tt (g}\  {\tt 1))}
\end{tabbing}\end{minipage}\end{center}
The purpose of the underlined {\bf set!}-exp\-res\-sion\ is to modify the definition of
{{\tt f}\/} so that it becomes a function that always produces {{\tt 22}\/}.
But {{\tt g}\/} stands for {{\tt f}\/} initially. Since {{\tt f}\/} is a the
name of a function, we can think of {{\tt (define}\ {\tt g}\ {\tt f)}\/} as a value
definition. The problem is that our current rules change the definition of
{{\tt f}\/} and, by implication, the definition of {{\tt g}\/}, because it
stands for {{\tt f}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  \={\tt (define}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt 22))}\\
\>{\tt (define}\  {\tt g}\  {\tt f)}
\\
\>{\tt (+}\  \hbox{$\underline{\mbox{{{\tt (begin}\ {\tt (}{\sf void}{\tt )}\ {\tt 5)}\/}}}$}\  {\tt (g}\  {\tt 1))}
\\
{\tt =}\  \={\tt (define}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt 22))}
\\
\>{\tt (define}\  {\tt g}\  {\tt f)}
\\
\>{\tt (+}\  {\tt 5}\  \hbox{$\underline{\mbox{{{\tt (g}\ {\tt 1)}\/}}}$}{\tt )}
\\
{\tt =}\  \={\tt (define}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt 22))}
\\
\>{\tt (define}\  {\tt g}\  {\tt f)}
\\
\>{\tt (+}\  {\tt 5}\  {\tt 22)}
\end{tabbing}\end{minipage}\end{center}
Scheme, however, does not behave this way. A {\bf set!}-exp\-res\-sion\ can modify only one
definition at a time. Here it modifies two: {{\tt f}\/}'s, which is
intended, and {{\tt g}\/}'s, which happens through the indirection from
{{\tt g}\/} to {{\tt f}\/}. In short, our rules do not explain the behavior
of all programs with {{\bf set!}-exp\-res\-sion}s; we need better rules if we wish to
understand Scheme fully. 

\hrule
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{vdf}>}} 
  & = & $\newdimen\p\setbox4=\hbox{$\,\,\vert\,\,$}\p=\wd4\kern\p$ ({\bf define} $\mbox{{\tt <{var}>}}$ $\mbox{{\tt <{val}>}}$) \\
  &   & $\,\,\vert\,\,$ ({\bf define-struct} $\mbox{{\tt <{var}>}}$ ($\mbox{{\tt <{var}>}}$ \ldots $\mbox{{\tt <{var}>}}$)) \\
 \\
          
\mbox{{\tt <{val}>}} & = & 
 \mbox{{\tt <{con}>}} $\,\,\vert\,\,$ \mbox{{\tt <{lst}>}} $\,\,\vert\,\,$ \mbox{{\tt <{prm}>}} $\,\,\vert\,\,$ \mbox{{\tt <{fun}>}} $\,\,\vert\,\,$ \mbox{{\tt <{void}>}} \\
 \\
\mbox{{\tt <{lst}>}} & = & {\sf empty} $\,\,\vert\,\,$ ({\sf cons} \mbox{{\tt <{val}>}} \mbox{{\tt <{lst}>}}) \\
 \\
\mbox{{\tt <{fun}>}} & = & ({\bf lambda} (\mbox{{\tt <{var}>}} \ldots \mbox{{\tt <{var}>}}) \mbox{{\tt <{exp}>}}) 
\end{tabular}
\end{center}
\center{Figure:  {\tt Advanced Student} Scheme: The values} \label{fig:adv:values}
\hrule

The problem concerns the definitions of functions, which suggests that we
take a second look at the representation of functions and function
definitions. So far, we used the names of functions as values. As we have
just seen, this choice may cause trouble in case the state variable is a
function. The solution is to use a concrete representation of
functions. Fortunately, we already have one in Scheme:
{{\bf lambda}-exp\-res\-sion}s. Furthermore, we rewrite function definitions so that they turn
into variable definitions with a {\bf lambda}-exp\-res\-sion\ on the right-hand side:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (define}\  {\tt (f}\  {\tt x)}\  {\tt x)}\\
{\tt =}\  {\tt (define}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt x))}
\end{tabbing}\end{minipage}\end{center}
Even recursive definitions are evaluated in this manner: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (d}\={\tt efine}\  {\tt (g}\  {\tt x)}\  \\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt x)}\  {\tt 1\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (g}\  {\tt (sub1}\  {\tt x))\char'135}{\tt ))}
\\
{\tt =}\  {\tt (d}\={\tt efine}\  {\tt g}
\\
\>{\tt (l}\={\tt ambda}\  {\tt (x)}
\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt x)}\  {\tt 1\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (g}\  {\tt (sub1}\  {\tt x))\char'135}{\tt )))}
\end{tabbing}\end{minipage}\end{center}
All other rules, especially, the rule for replacing variables with their
values remain the same. 

Figure~\ref{fig:adv:values} specifies the set of values,\footnote{It misses a specification of structural values, but they play no role in this discussion.}  as a subset of the set of expressions, and the set of value
definitions, as a subset of the definitions. Using these definitions and
the modified rules, we can take a second look at at the above example:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \=\hbox{$\underline{\mbox{{{\tt (define}\ {\tt (f}\ {\tt x)}\ {\tt x)}\/}}}$}\\
\>{\tt (define}\  {\tt g}\  {\tt f)}
\\
\>{\tt (+}\  {\tt (begin}\  {\tt (set!}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt 22))}\  {\tt 5)}\  {\tt (g}\  {\tt 1))}
\\
{\tt =}\  \={\tt (define}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt x))}
\\
\>{\tt (define}\  {\tt g}\  \hbox{$\underline{\mbox{{{\tt f}\/}}}$}{\tt )}
\\
\>{\tt (+}\  {\tt (begin}\  {\tt (set!}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt 22))}\  {\tt 5)}\  {\tt (g}\  {\tt 1))}
\\
{\tt =}\  \={\tt (define}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt x))}
\\
\>{\tt (define}\  {\tt g}\  {\tt (lambda}\  {\tt (x)}\  {\tt x))}
\\
\>{\tt (+}\  {\tt (begin}\  \hbox{$\underline{\mbox{{{\tt (set!}\ {\tt f}\ {\tt (lambda}\ {\tt (x)}\ {\tt 22))}\/}}}$}\  {\tt 5)}\  {\tt (g}\  {\tt 1))}
\\
{\tt =}\  \={\tt (define}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt 22))}
\\
\>{\tt (define}\  {\tt g}\  {\tt (lambda}\  {\tt (x)}\  {\tt x))}
\\
\>{\tt (+}\  \hbox{$\underline{\mbox{{{\tt (begin}\ {\tt (}{\sf void}{\tt )}\ {\tt 5)}\/}}}$}\  {\tt (g}\  {\tt 1))}
\\
{\tt =}\  \={\tt (define}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt 22))}
\\
\>{\tt (define}\  {\tt g}\  {\tt (lambda}\  {\tt (x)}\  {\tt x))}
\\
\>{\tt (+}\  {\tt 5}\  \hbox{$\underline{\mbox{{{\tt (g}\ {\tt 1)}\/}}}$}{\tt )}
\\
{\tt =}\  \={\tt (define}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt 22))}
\\
\>{\tt (define}\  {\tt g}\  {\tt (lambda}\  {\tt (x)}\  {\tt x))}
\\
\>{\tt (+}\  {\tt 5}\  {\tt 1)}
\end{tabbing}\end{minipage}\end{center}
The key difference is that the definition of {{\tt g}\/} directly associates
the variable with a function representation, not just a name for a
function. 

The following program shows the effects of {{\bf set!}-exp\-res\-sion}s on functions with an extreme
example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (f}\  {\tt x)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt x)}\  {\tt \char'047}{\tt done\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (f}\  {\tt (sub1}\  {\tt x))\char'135}{\tt ))}
\\
\relax
\\
{\tt (define}\  {\tt g}\  {\tt f)}
\\
\relax
\\
{\tt (b}\={\tt egin}\  
\\
\>{\tt (set!}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt \char'047}{\tt ouch))}
\\
\>{\tt (symbol=?}\  {\tt (g}\  {\tt 1)}\  {\tt \char'047}{\tt ouch))}
\end{tabbing}\end{minipage}\end{center}
The function {{\tt f}\/} is recursive on natural numbers and always produces
{{\tt \char'047}{\tt done}\/}. Initially, {{\tt g}\/} is defined to be {{\tt f}\/}. The
final {\bf begin}-exp\-res\-sion\ first modifies {{\tt f}\/} and then uses {{\tt g}\/}.

At first, we must rewrite the function definitions according to our
modified rules: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt ...}\\
{\tt =}\  \={\tt (d}\={\tt efine}\  {\tt f}
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt (x)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt x)}\  {\tt \char'047}{\tt done\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (f}\  {\tt (sub1}\  {\tt x))\char'135}{\tt )))}
\\
\>\relax
\\
\>{\tt (define}\  {\tt g}\  \hbox{$\underline{{{\tt f}\/}}$}{\tt )}
\\
\>\relax
\\
\>{\tt (b}\={\tt egin}\  
\\
\>\>{\tt (set!}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt \char'047}{\tt ouch))}
\\
\>\>{\tt (symbol=?}\  {\tt (g}\  {\tt 1)}\  {\tt \char'047}{\tt ouch))}
\\
{\tt =}\  \={\tt (d}\={\tt efine}\  {\tt f}
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt (x)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt x)}\  {\tt \char'047}{\tt done\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (f}\  {\tt (sub1}\  {\tt x))\char'135}{\tt )))}
\\
\>\relax
\\
\>{\tt (d}\={\tt efine}\  {\tt g}\  
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt (x)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt x)}\  {\tt \char'047}{\tt done\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (f}\  {\tt (sub1}\  {\tt x))\char'135}{\tt )))}
\\
\>\relax
\\
\>{\tt (b}\={\tt egin}\  
\\
\>\>\hbox{$\underline{\mbox{{{\tt (set!}\ {\tt f}\ {\tt (lambda}\ {\tt (x)}\ {\tt \char'047}{\tt ouch))}\/}}}$}
\\
\>\>{\tt (set!}\  {\tt f}\  {\tt (lambda}\  {\tt (x)}\  {\tt \char'047}{\tt ouch))}
\\
\>\>{\tt (symbol=?}\  {\tt (g}\  {\tt 1)}\  {\tt \char'047}{\tt ouch))}
\end{tabbing}\end{minipage}\end{center}
Rewriting the definition of {{\tt f}\/} is straightforward. The major change
concerns the definition of {{\tt g}\/}. Instead of {{\tt f}\/} it now
contains a copy of the value for which {{\tt f}\/} currently stands. This
value contains a reference to {{\tt f}\/}, but that is not unusual. 

Next, the {\bf set!}-exp\-res\-sion\ modifies the definition of {{\tt f}\/}, but nothing
else: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt ...}\\
{\tt =}\  \={\tt (d}\={\tt efine}\  {\tt f}
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt (x)}\  
\\
\>\>\>{\tt \char'047}{\tt ouch))}
\\
\>\relax
\\
\>{\tt (d}\={\tt efine}\  {\tt g}\  
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt (x)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt x)}\  {\tt \char'047}{\tt done\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (f}\  {\tt (sub1}\  {\tt x))\char'135}{\tt )))}
\\
\>\relax
\\
\>{\tt (b}\={\tt egin}\  
\\
\>\>{\tt (}{\sf void}{\tt )}
\\
\>\>{\tt (symbol=?}\  \hbox{$\underline{\mbox{{{\tt (g}\ {\tt 1)}\/}}}$}\  {\tt \char'047}{\tt ouch))}
\end{tabbing}\end{minipage}\end{center}
In particular, the definition of {{\tt g}\/} remains the same, though the
{{\tt f}\/} inside of {{\tt g}\/}'s value now refers to a new value. But we
have seen this phenomenon before. The next two steps follow the basic rules
of intermezzo~1 and yield this program: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  \={\tt (d}\={\tt efine}\  {\tt f}\\
\>\>{\tt (l}\={\tt ambda}\  {\tt (x)}\  
\\
\>\>\>{\tt \char'047}{\tt ouch))}
\\
\>\relax
\\
\>{\tt (d}\={\tt efine}\  {\tt g}\  
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt (x)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt x)}\  {\tt \char'047}{\tt done\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (f}\  {\tt (sub1}\  {\tt x))\char'135}{\tt )))}
\\
\>\relax
\\
\>{\tt (b}\={\tt egin}\  
\\
\>\>{\tt (}{\sf void}{\tt )}
\\
\>\>{\tt (symbol=?}\  {\tt (f}\  {\tt 0)}\  {\tt \char'047}{\tt ouch))}
\\
{\tt =}\  \={\tt (d}\={\tt efine}\  {\tt f}
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt (x)}\  
\\
\>\>\>{\tt \char'047}{\tt ouch))}
\\
\>\relax
\\
\>{\tt (d}\={\tt efine}\  {\tt g}\  
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt (x)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt x)}\  {\tt \char'047}{\tt done\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (f}\  {\tt (sub1}\  {\tt x))\char'135}{\tt )))}
\\
\>\relax
\\
\>{\tt (b}\={\tt egin}\  
\\
\>\>{\tt (}{\sf void}{\tt )}
\\
\>\>{\tt (symbol=?}\  {\tt \char'047}{\tt ouch}\  {\tt \char'047}{\tt ouch))}
\end{tabbing}\end{minipage}\end{center}
That is, the application of {{\tt g}\/} eventually applies {{\tt f}\/} to
{{\tt 0}\/}, which yields {{\tt \char'047}{\tt ouch}\/}. Hence the final result is
{{\tt true}\/}. 


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 38.3.4}

 \label{ex:adv:sem13} 
Validate that the following program evaluates to {{\tt true}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (make-box}\  {\tt x)}\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt contents}\  {\tt x)}
\\
\>\>\>{\tt (define}\  {\tt (new}\  {\tt y)}\  {\tt (set!}\  {\tt x}\  {\tt y))}
\\
\>\>\>{\tt (define}\  {\tt (peek)}\  {\tt x))}
\\
\>\>{\tt (list}\  {\tt new}\  {\tt peek)))}
\\
{\tt (define}\  {\tt B}\  {\tt (make-box}\  {\tt 55))}
\\
\relax
\\
{\tt (define}\  {\tt C}\  {\tt B)}
\\
{\tt (a}\={\tt nd}\  
\\
\>{\tt (b}\={\tt egin}\  
\\
\>\>{\tt ((first}\  {\tt B)}\  {\tt 33)}
\\
\>\>{\tt true)}
\\
\>{\tt (=}\  {\tt (second}\  {\tt C)}\  {\tt 33)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt B}\  {\tt (make-box}\  {\tt 44))}
\\
\>\>{\tt (=}\  {\tt (second}\  {\tt C)}\  {\tt 33)))}\  
\end{tabbing}\end{minipage}\end{center}
Underline for each step the subexpression that must be evaluated next. 
Only show those steps that involve a {\bf local}-exp\-res\-sion\ or a
{\bf set!}-exp\-res\-sion.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/adv:sem13.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt} 


While we decided to rewrite function definitions so that they contain
{{\bf lambda}-exp\-res\-sion}s, we stuck with a function application rule that assumes
function definitions in the style of {\tt Beginning Student} Scheme. More
concretely, if the definition context contains a definition such as
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt f}\  {\tt (lambda}\  {\tt (x}\  {\tt y)}\  {\tt (+}\  {\tt x}\  {\tt y)))}\end{tabbing}\end{minipage}\end{center}
and the expression is
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (*}\  {\tt (f}\  {\tt 1}\  {\tt 2)}\  {\tt 5)}\end{tabbing}\end{minipage}\end{center}
then the next step in the evaluation is
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (*}\  {\tt (+}\  {\tt 1}\  {\tt 2)}\  {\tt 5)}\end{tabbing}\end{minipage}\end{center}
For other occasions, however, we just replace variables with the values in
the respective definitions. If we followed that rule, we would rewrite
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (*}\  {\tt (f}\  {\tt 1}\  {\tt 2)}\  {\tt 5)}\end{tabbing}\end{minipage}\end{center}
to
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (*}\  \={\tt (}\={\tt (lambda}\  {\tt (x}\  {\tt y)}\  {\tt (+}\  {\tt x}\  {\tt y))}\\
\>\>{\tt 1}\  {\tt 2)}
\\
\>{\tt 5)}
\end{tabbing}\end{minipage}\end{center}
At first glance, this exploration route ends here, because there are no
laws for this application. 

We can reconcile the two ideas with a new law, suggested by the last
expression:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (}\={\tt (lambda}\  {\tt (x-1}\  {\tt ...}\  {\tt x-n)}\  {\tt exp)}\\
\>{\tt v-1}\  {\tt ...}\  {\tt v-n)}
\\
{\tt =}\  {\tt exp}\  \hbox{$\mbox{ with all }$}\  {\tt x-1}\  {\tt ...}\  {\tt x-n}\  \hbox{$\mbox{ replaced by }$}\  {\tt v-1}\  {\tt ...}\  {\tt v-n}
\end{tabbing}\end{minipage}\end{center}
The law serves as a replacement of the law of application from algebra in
the study of the foundations of computing. By convention, this law is
called the ``beta value'' rule.

\medskip\noindent{\bf Beta and the Lambda Calculus}:\  The $\beta$ (pronounced ``beta'') law
was formulated by Alonzo Church in the last 1920s, during a period when
logicians\footnote{Logic is to computing what mathematics is to physics.}
explored the principles of computation, what computation could achieve, and
what it couldn't. Church and his PhD students confirmed that the law and a
small sublanguage of Scheme, namely,
\begin{center}
\begin{tabular}{lcl}
\mbox{{\tt <{exp}>}} 
  &=& \mbox{{\tt <{var}>}} 
      $\,\,\vert\,\,$ ({\bf lambda} (\mbox{{\tt <{var}>}}) \mbox{{\tt <{exp}>}})
      $\,\,\vert\,\,$ (\mbox{{\tt <{exp}>}} \mbox{{\tt <{exp}>}})
\end{tabular}
\end{center}
is enough to define all computable functions on a (simulation of) the
natural numbers. Functions that cannot be formulated in this language 
are not computable. 

The language and the $\beta$ law became known as the $\lambda$ (pronounced:
lambda) calculus. Guy Steele and Gerry Sussman later derived Scheme from
the $\lambda$ calculus. In the mid-1970s, Plotkin suggested the $\beta_v$
law as a better method for understanding function applications in
programming languages such as Scheme.~{\rule{3pt}{5pt}}

 \subsection{Errors in Advanced Scheme} \label{sec:advanced:errors} 

The extension of our language with functions as values not only introduces
new powers for the programmer but also new possibilities for errors. Recall
that there are three kinds of errors: syntax errors, run-time (or semantics) 
errors, and logical errors. {\tt Advanced Student} Scheme turns a class of
syntactic errors of {\tt Beginning Student} Scheme into run-time errors. It also
introduces a new form of logical error. 

Consider the following program: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt how-many-in-list}\ {\tt :}\ {\tt (listof}\ {\tt X)}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to count how many items {{\tt alist}\/} contains }
\\
{\tt (d}\={\tt efine}\  {\tt (how-many-in-list}\  {\tt alist)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt empty?}\  {\tt (alist)\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (+}\  {\tt (how-many-in-list}\  {\tt (rest}\  {\tt alist))}\  {\tt 1)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
In {\tt Beginning Student} or {\tt Intermediate Student} Scheme,
DrScheme would have signaled a syntax error because {{\tt alist}\/} is the
parameter to a function but is also used as a function. Because functions
are values in {\tt Advanced Student} Scheme, DrScheme must now accept this
function definition as syntactially correct. When the function is applied
to {{\tt empty}\/} or any other list value, however, DrScheme soon applies
{{\tt empty}\/} to no arguments, which is a run-time error. After all, lists
are not functions.  DrScheme signals any attempt to apply a non-function
immediately with an error message and stops the evaluation.

The second form of error is logical. That is, a program that suffers from
this form of error doesn't produce a syntax or a run-time error
message. Instead, it produces wrong answers. Take a look at the following
two definitions: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt flip1}\\
\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt state}\  {\tt 1))}
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt ()}
\\
\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (set!}\  {\tt state}\  {\tt (-}\  {\tt 1}\  {\tt state))}
\\
\>\>\>\>{\tt state))))}
\end{tabbing}\end{minipage}\qquad\qquad\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt flip2}\\
\>{\tt (l}\={\tt ambda}\  {\tt ()}
\\
\>\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt state}\  {\tt 1))}
\\
\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (set!}\  {\tt state}\  {\tt (-}\  {\tt 1}\  {\tt state))}
\\
\>\>\>\>{\tt state))))}
\end{tabbing}\end{minipage}
\end{center}
They differ in the order of two lines. One introduces a {{\tt local}\/}
definition whose body evaluates to a function. The other defines a function
whose body contains a {\bf local}-exp\-res\-sion.  According to our rules, the definition on
the left rewrites to
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt state1}\  {\tt 1)}\\
\relax
\\
{\tt (d}\={\tt efine}\  {\tt flip1}
\\
\>{\tt (l}\={\tt ambda}\  {\tt ()}
\\
\>\>{\tt (b}\={\tt egin}
\\
\>\>\>{\tt (set!}\  {\tt state1}\  {\tt (-}\  {\tt 1}\  {\tt state1))}
\\
\>\>\>{\tt state1)))}
\end{tabbing}\end{minipage}\qquad\qquad\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt flip2}\\
\>{\tt (l}\={\tt ambda}\  {\tt ()}
\\
\>\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt state}\  {\tt 1))}
\\
\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (set!}\  {\tt state}\  {\tt (-}\  {\tt 1}\  {\tt state))}
\\
\>\>\>\>{\tt state))))}
\\
\relax
\end{tabbing}\end{minipage}
\end{center}
The one on the right already associates a name with a function.

Let us now see how the two functions have radically different behaviors. To
do so, we evaluate the expressions
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  \={\tt (=}\  {\tt (flip1)}\  {\tt 0)}\\
\>{\tt (=}\  {\tt (flip1)}\  {\tt 1)}
\\
\>{\tt (=}\  {\tt (flip1)}\  {\tt 0))}
\end{tabbing}\end{minipage}\qquad\qquad\begin{minipage}[t]{8in}\begin{tabbing}{\tt (and}\  \={\tt (=}\  {\tt (flip2)}\  {\tt 0)}\\
\>{\tt (=}\  {\tt (flip2)}\  {\tt 1)}
\\
\>{\tt (=}\  {\tt (flip2)}\  {\tt 0))}
\end{tabbing}\end{minipage}
\end{center}
in the context of the respective definitions.

Here are the first four steps of the evaluation for the left-hand
expression: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (define}\  {\tt state1}\  {\tt 1)}\\
\>{\tt (and}\  \={\tt (=}\  {\tt (flip1)}\  {\tt 0)}
\\
\>\>{\tt (=}\  {\tt (flip1)}\  {\tt 1)}
\\
\>\>{\tt (=}\  {\tt (flip1)}\  {\tt 0))}
\\
{\tt =}\  \={\tt (define}\  {\tt state1}\  {\tt 1)}
\\
\>{\tt (and}\  \={\tt (=}\  \={\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (set!}\  {\tt state1}\  {\tt (-}\  {\tt 1}\  {\tt state1))}
\\
\>\>\>\>{\tt state1)}
\\
\>\>\>{\tt 0)}
\\
\>\>{\tt (=}\  {\tt (flip1)}\  {\tt 1)}
\\
\>\>{\tt (=}\  {\tt (flip1)}\  {\tt 0))}
\\
{\tt =}\  \={\tt (define}\  {\tt state1}\  {\tt 1)}
\\
\>{\tt (and}\  \={\tt (=}\  \={\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (set!}\  {\tt state1}\  {\tt 0)}
\\
\>\>\>\>{\tt state1)}
\\
\>\>\>{\tt 0)}
\\
\>\>{\tt (=}\  {\tt (flip1)}\  {\tt 1)}
\\
\>\>{\tt (=}\  {\tt (flip1)}\  {\tt 0))}
\\
{\tt =}\  \={\tt (define}\  {\tt state1}\  {\tt 0)}
\\
\>{\tt (and}\  \={\tt (=}\  \={\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (}{\sf void}{\tt )}
\\
\>\>\>\>{\tt state1)}
\\
\>\>\>{\tt 0)}
\\
\>\>{\tt (=}\  {\tt (flip1)}\  {\tt 1)}
\\
\>\>{\tt (=}\  {\tt (flip1)}\  {\tt 0))}
\\
{\tt =}\  \={\tt (define}\  {\tt state1}\  {\tt 0)}
\\
\>{\tt (and}\  \={\tt (=}\  {\tt 0}\  {\tt 0)}
\\
\>\>{\tt (=}\  {\tt (flip1)}\  {\tt 1)}
\\
\>\>{\tt (=}\  {\tt (flip1)}\  {\tt 0))}
\end{tabbing}\end{minipage}\end{center}
The relevant definition context is the definition of {{\tt state1}\/}, which
we see changing from {{\tt 1}\/} to {{\tt 0}\/} during the third step. From
this point, it is not difficult to validate that the expression produces
{{\tt true}\/} and that {{\tt state1}\/} ends up being {{\tt 0}\/}.

Compare this with the first three steps in the evaluation of the right-hand
expression: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (and}\  \={\tt (=}\  {\tt (flip2)}\  {\tt 0)}\\
\>{\tt (=}\  {\tt (flip2)}\  {\tt 1)}
\\
\>{\tt (=}\  {\tt (flip2)}\  {\tt 0))}
\\
{\tt =}\  {\tt (and}\  \={\tt (=}\  \={\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt state}\  {\tt 1))}
\\
\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (set!}\  {\tt state}\  {\tt (-}\  {\tt 1}\  {\tt state))}
\\
\>\>\>\>{\tt state))}
\\
\>\>{\tt 0)}
\\
\>{\tt (=}\  {\tt (flip2)}\  {\tt 1)}
\\
\>{\tt (=}\  {\tt (flip2)}\  {\tt 0))}
\\
{\tt =}\  \={\tt (define}\  {\tt state1}\  {\tt 1)}
\\
\>{\tt (and}\  \={\tt (=}\  \={\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (set!}\  {\tt state1}\  {\tt (-}\  {\tt 1}\  {\tt state1))}
\\
\>\>\>\>{\tt state1)}
\\
\>\>\>{\tt 0)}
\\
\>\>{\tt (=}\  {\tt (flip2)}\  {\tt 1)}
\\
\>\>{\tt (=}\  {\tt (flip2)}\  {\tt 0))}
\\
{\tt =}\  \={\tt (define}\  {\tt state1}\  {\tt 0)}
\\
\>{\tt (and}\  \={\tt (=}\  {\tt 0}\  {\tt 0)}
\\
\>\>{\tt (=}\  {\tt (flip2)}\  {\tt 1)}
\\
\>\>{\tt (=}\  {\tt (flip2)}\  {\tt 0))}
\end{tabbing}\end{minipage}\end{center}
The only definition that matters here is the one for {{\tt flip2}\/}.
Superficially, the two evaluations are alike. But a closer look shows 
that the second one differs from the first in crucial way. It creates the
definition for {{\tt state1}\/} while the first evaluation started with such
a definition.

Here is the continuation of the second evaluation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt ...}\  \\
{\tt =}\  \={\tt (define}\  {\tt state1}\  {\tt 0)}
\\
\>{\tt (and}\  \={\tt true}
\\
\>\>{\tt (=}\  \={\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt state}\  {\tt 1))}
\\
\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>{\tt (set!}\  {\tt state}\  {\tt (-}\  {\tt 1}\  {\tt state))}
\\
\>\>\>\>\>{\tt state))}
\\
\>\>\>{\tt 1)}
\\
\>\>{\tt (=}\  {\tt (flip2)}\  {\tt 0))}
\\
{\tt =}\  \={\tt (define}\  {\tt state1}\  {\tt 0)}
\\
\>{\tt (define}\  {\tt state2}\  {\tt 1)}
\\
\>{\tt (and}\  \={\tt true}
\\
\>\>{\tt (=}\  \={\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (set!}\  {\tt state2}\  {\tt (-}\  {\tt 1}\  {\tt state2))}
\\
\>\>\>\>{\tt state2)}
\\
\>\>\>{\tt 1)}
\\
\>\>{\tt (=}\  {\tt (flip2)}\  {\tt 0))}
\\
{\tt =}\  \={\tt (define}\  {\tt state1}\  {\tt 0)}
\\
\>{\tt (define}\  {\tt state2}\  {\tt 0)}
\\
\>{\tt (and}\  \={\tt true}
\\
\>\>{\tt (=}\  \={\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (}{\sf void}{\tt )}
\\
\>\>\>\>{\tt state2)}
\\
\>\>\>{\tt 1)}
\\
\>\>{\tt (=}\  {\tt (flip2)}\  {\tt 0))}
\\
{\tt =}\  \={\tt (define}\  {\tt state1}\  {\tt 0)}
\\
\>{\tt (define}\  {\tt state2}\  {\tt 0)}
\\
\>{\tt (and}\  \={\tt true}
\\
\>\>{\tt (=}\  {\tt 0}\  {\tt 1)}
\\
\>\>{\tt (=}\  {\tt (flip2)}\  {\tt 0))}
\end{tabbing}\end{minipage}\end{center}
It shows that {{\tt flip2}\/} creates a new definition every time it is
applied and that it always produces {{\tt 0}\/}. Contrary to its name, it
does not flip the value of {{\tt state}\/} upon every application. As a
result, the evaluation ends now with two new top-level definitions and the
value {{\tt false}\/}. 

The general moral is that a function defined in a {\bf local}-exp\-res\-sion\ is different
from a function whose body contains a {\bf local}-exp\-res\-sion. The first ensures that some
definitions are only accessible to a function. The definitions exists once
and only once for this function. In contrast, the second creates new
top-level the evaluation of the function body. In the next part of the
book, we exploit both ideas to create new kinds of programs.


%% BEGIN INLINED FILE: curriculum7.dl.tex




\newpage\part{Changing Compound Values}\thispagestyle{empty}\newpage \label{part:structmod}

\section{Encapsulation} \label{sec:encapsulate}

When we design a program to control a traffic light, we probably don't want
to control just one traffic light, but several. Similarly, when we design a
program to manage names and phone numbers, we might wish to manage several
address books, not just one. Of course, we could copy the code for a
traffic light controller (or an address book manager) and rename the state
variables, but copying code is bad.  Furthermore, we might wish to create
so many traffic lights that copying code is plain impractical.

The proper solution is to use the power of abstraction. Here we abstract
over several instances of the address book program and the traffic light
program and so on. This differs from the notion of abstraction in
part~\ref{part:abstract} because it involves state variables, but the idea
and even the technique is the same. We encapsulate the state variables and
the functions in a {{\bf local}-exp\-res\-sion} and thus give ourselves the power to create
as many different versions as necessary.  We learn how to encapsulate state
variables in the first subsection, and practice it in the second one.

\subsection{Abstracting with State Variables} \label{sec:abstract}

Suppose we wish to turn the program in figure~\ref{fig:sv-recipe1-example}
(page~\pageref{fig:sv-recipe1-example}) into a program for managing
(simulated) traffic lights. A traffic light comes with a single state
variable, {{\tt current-color}\/}, and a single service, {{\tt next}\/},
which switches the traffic light to the next color according to the traffic
laws. For a graphical simulation, the latter would also re-draw the traffic
light so that users can view the current color.

In a simulation, each traffic light has a unique location on the canvas:\\
[.2cm]
  \begin{rawhtml}   <center><img src=../icons/multiple-traffic.gif alt="[Multiple Traffic Lights]"></center>  \end{rawhtml}

\noindent A user should be able to control each traffic light independently
of the others. Indeed, the operator should be able to add or shut down
traffic lights while the rest of the system remains unchanged and running.

The sample program of figure~\ref{fig:sv-recipe1-example} deals with a
 single traffic light, though without the drawing operation.  The problem
 is now to turn this into a program that can create as many traffic lights
 as needed, each with its own state variables and switching functions and
 at its own location. If we were to copy the definitions in
 figure~\ref{fig:sv-recipe1-example} and add definitions for dealing with
 the canvas, the various instances would differ in only one aspect: the
 data concerning the location of the traffic light. This thought experiment
 suggests that we should develop an abstract function for creates and
 manages traffic lights at various locations. 

Because the original program consists of several top-level definitions, we use
 the recipe of section~\ref{sec:design-abs-1st}, which suggests to wrap the
 definitions in a {{\bf local}-exp\-res\-sion} inside a function. When local definitions
 include state variables, as in this example, we prefer to say that we
 {\sc {encapsulate}} definitions. This terminology emphasizes that the
 abstract function hides the state variables from other parts of the
 program,\footnote{By encapsulating a state variable in a {{\bf local}-exp\-res\-sion} we  guarantee that it can only change according to the managed services, not  by arbitrary assignments.} and deemphasizes the act of abstraction. Still,
 the definition encapsulates and abstracts at the same time, and a
 programmer must keep this in mind.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {\underline View:}}\\
\hbox{;; {{\tt draw-light}\ {\tt :}\ {\tt TL-color}\ {\tt number}\ {}{\tt ->}{}\ {\tt true}\/}}
\\
\hbox{;; to (re)draw the traffic light on the canvas }
\\
{\tt (define}\  {\tt (draw-light}\  {\tt current-color}\  {\tt x-posn)}\  {\tt ...))}
\\
\hbox{;; {\underline Model:}}
\\
\hbox{;; {{\tt make-traffic-light}\ {\tt :}\ {\tt symbol}\ {\tt number}\ {}{\tt ->}{}\ {\tt (}{}{\tt ->}{}\ {\tt true)}\/}}
\\
\hbox{;; to create a red light with {{\tt (make-posn}\ {\tt x-posn}\ {\tt 0)}\/} as the upper-left corner}
\\
\hbox{;; effect: draw the traffic light on the canvas}
\\
{\tt (d}\={\tt efine}\  {\tt (make-traffic-light}\  {\tt street}\  {\tt x-posn)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt current-color}\ {\tt :}\ {\tt TL-color}\/}}
\\
\>\>\>\hbox{;; to keep track of the current color of the traffic light}
\\
\>\>\>{\tt (define}\  {\tt current-color}\  {\tt \char'047}{\tt red)}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt init-traffic-light}\ {\tt :}\ {}{\tt ->}{}\ {\tt true}\/}}
\\
\>\>\>\hbox{;; to (re)set {{\tt current-color}\/} to red and to (re)create the view }
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (init-traffic-light)}
\\
\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>{\tt (set!}\  {\tt current-color}\  {\tt \char'047}{\tt red)}
\\
\>\>\>\>\>{\tt (draw-light}\  {\tt current-color}\  {\tt x-posn)))}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt next}\ {\tt :}\ {}{\tt ->}{}\ {\tt true}\/}}
\\
\>\>\>\hbox{;; effect: to change {{\tt current-color}\/} from {{\tt \char'047}{\tt green}\/} to {{\tt \char'047}{\tt yellow}\/}, }
\\
\>\>\>\hbox{;; {{\tt \char'047}{\tt yellow}\/} to {{\tt \char'047}{\tt red}\/}, and {{\tt \char'047}{\tt red}\/} to {{\tt \char'047}{\tt green}\/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (next)}
\\
\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>{\tt (set!}\  {\tt current-color}\  {\tt (next-color}\  {\tt current-color))}
\\
\>\>\>\>\>{\tt (draw-light}\  {\tt current-color}\  {\tt x-posn)))}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt next-color}\ {\tt :}\ {\tt TL-color}\ {}{\tt ->}{}\ {\tt TL-color}\/}}
\\
\>\>\>\hbox{;; to compute the successor of {{\tt current-color}\/} based on the traffic laws}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (next-color}\  {\tt current-color)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt green}\  {\tt current-color)}\  {\tt \char'047}{\tt yellow\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt yellow}\  {\tt current-color)}\  {\tt \char'047}{\tt red\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt red}\  {\tt current-color)}\  {\tt \char'047}{\tt green\char'135}{\tt ))}
\\
\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\hbox{;; Initialize and produce {{\tt next}\/}}
\\
\>\>\>{\tt (init-traffic-light)}
\\
\>\>\>{\tt next)))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Managing multiple traffic lights} \label{fig:multiple-tl}
\hrule

The next step is to consider what this function should do, that is, what it
should consume, what it should produce, and what effects it should have, if
any. Let's start with the name. We call the new function
{{\tt make-traffic-light}\/}; after all, making a simulated traffic light is
the purpose of the abstracted program. Furthermore, according to our
abstraction recipes, an abstraction must consume values that represent the
unique aspects of an instance.  The unique aspect of a traffic light is its
position on the canvas; for clarity, let's add a physical address, too. 

Every use of {{\tt make-traffic-light}\/} should create a traffic light and
enable the operator to switch it from one state to the next. The first part
suggests an effect. Specifically, the function should initialize the state
variable and draw the initial state of the traffic light at the designated
position on the canvas. The second part of the statement suggests a result:
a function for switching the state of the traffic light. 

Figure~\ref{fig:multiple-tl} contains the outline of the traffic simulator,
including the complete definition of {{\tt make-traffic-light}\/}. The
simulator consists of a model and a view. The model is
{{\tt make-traffic-light}\/}. The view is called {{\tt draw-light}\/} and is
only sketched; the full definition of the view is left as an exercise.

The definition of {{\tt make-traffic-light}\/} is an ordinary function
definition. It uses a {{\tt local}\/} definition to set up the single state
variable, the initializer, and the state-changing function. The body of the
{{\bf local}-exp\-res\-sion} uses the initializer and then produces {{\tt next}\/} as the
function's value. 

Using {{\tt make-traffic-light}\/} we can create svereal individual traffic
lights or entire collections of them. We could also add lights as time goes
by. First, we create a sufficient large canvas:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; create the canvas first }\\
{\tt (start}\  {\tt 300}\  {\tt 160)}
\end{tabbing}\end{minipage}\end{center}
Second, we apply {{\tt make-traffic-light}\/} as often as needed:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt lights}\ {\tt :}\ {\tt (listof}\ {\tt traffic-light)}\/}}\\
\hbox{;; to manage the lights along Sunrise }
\\
{\tt (d}\={\tt efine}\  {\tt lights}
\\
\>{\tt (list}\  \={\tt (make-traffic-light}\  {\tt \char'047}{\tt sunrise\char'100}{\tt rice}\  {\tt 50)}
\\
\>\>{\tt (make-traffic-light}\  {\tt \char'047}{\tt sunrise\char'100}{\tt cmu}\  {\tt 150)))}
\end{tabbing}\end{minipage}\end{center}
Here we define {{\tt lights}\/} to be a list of two traffic lights. Each
traffic light is a function, so {{\tt lights}\/} stands for a list of two
functions.

After creating the traffic lights, we can change their states as desired. 
To do so, we must keep in mind that each traffic light is represented by a
function that consumes nothing and produces {{\tt true}\/}. Its effect is to
change the hidden state variable and the drawing on the canvas. In our
running example, we could use the {\tt Interactions} window as follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt ((second}\  {\tt lights))}\  \\
{\tt true}
\\
{\tt >}\  {\tt (andmap}\  {\tt (lambda}\  {\tt (a-light)}\  {\tt (a-light))}\  {\tt lights)}
\\
{\tt true}
\end{tabbing}\end{minipage}\end{center}
The first interaction extracts the second item from {{\tt lights}\/} and
applies it. This sets the light at {{\tt \char'047}{\tt sunrise\char'100}{\tt cmu}\/} to red.  The
second one switches the state of all items on {{\tt lights}\/}.

Each application of {{\tt make-traffic-light}\/} turns variants of the
{{\tt local}\/} definitions into top-level definitions, after renaming
them. Because the above {{\tt define}\/} contains two applications of
{{\tt make-traffic-light}\/}, it creates two copies of each {{\tt local}\/}ly
defined function and state variable during an evaluation:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; definitions for {{\tt \char'047}{\tt sunrise\char'100}{\tt rice}\/}}\\
{\tt (define}\  {\tt current-color\char'100}{\tt rice}\  {\tt \char'047}{\tt red)}
\\
{\tt (d}\={\tt efine}\  {\tt (init-traffic-light\char'100}{\tt rice)}\  
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt current-color\char'100}{\tt rice}\  {\tt \char'047}{\tt red)}
\\
\>\>{\tt (draw-light}\  {\tt current-color\char'100}{\tt rice}\  {\tt 50)))}
\\
{\tt (define}\  {\tt (next\char'100}{\tt rice)}\  {\tt ...)}
\\
{\tt (define}\  {\tt (next-color\char'100}{\tt rice}\  {\tt current-color)}\  {\tt ...)}
\\
\hbox{;; definitions for {{\tt \char'047}{\tt sunrise\char'100}{\tt cmu}\/}}
\\
{\tt (define}\  {\tt current-color\char'100}{\tt cmu}\  {\tt \char'047}{\tt red)}
\\
{\tt (d}\={\tt efine}\  {\tt (init-traffic-light\char'100}{\tt cmu)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt current-color\char'100}{\tt cmu}\  {\tt \char'047}{\tt red)}
\\
\>\>{\tt (draw-light}\  {\tt current-color\char'100}{\tt cmu}\  {\tt 150)))}
\\
{\tt (define}\  {\tt (next\char'100}{\tt cmu)}\  {\tt ...)}
\\
{\tt (define}\  {\tt (next-color\char'100}{\tt cmu}\  {\tt current-color)}\  {\tt ...)}
\\
{\tt (d}\={\tt efine}\  {\tt lights}
\\
\>{\tt (list}\  \={\tt next\char'100}{\tt rice}
\\
\>\>{\tt next\char'100}{\tt cmu))}
\end{tabbing}\end{minipage}\end{center}
The new top-level definitions of {{\tt init-traffic-light}\/} show how the
renaming ensures that one of them takes care of {{\tt \char'047}{\tt sunrise\char'100}{\tt rice}\/} and
the other one of {{\tt \char'047}{\tt sunrise\char'100}{\tt cmu}\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}



{\bf Exercise 39.1.1}

 \label{ex:multipl-tl-dd}
What is the concrete effect of the second interaction
above?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/multipl-tl-dd.html}{\rule{3pt}{5pt}}


{\bf Exercise 39.1.2}

 \label{ex:multipl-tl-eval}
Fill in the bodies of {{\tt next\char'100}{\tt rice}\/} and {{\tt next\char'100}{\tt cmu}\/} in the
hand-evaluated program. Then evaluate {{\tt ((second}\ {\tt lights))}\/} in the
context of these definitions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/multipl-tl-eval.html}{\rule{3pt}{5pt}}


{\bf Exercise 39.1.3}

 \label{ex:multipl-tl-draw}
Develop the function {{\tt draw-light}\/}. It realizes the view part of the
traffic light simulation in figure~\ref{fig:multiple-tl}. Each traffic
light should be as tall as the canvas, delineated by solid lines on the
left and right. The suggested dimensions of a single light are
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt WIDTH}\  {\tt 50)}\\
{\tt (define}\  {\tt RADIUS}\  {\tt 20)}
\\
{\tt (define}\  {\tt DISTANCE-BETWEEN-BULBS}\  {\tt 10)}
\\
\hbox{;; the minimum canvas height}
\\
{\tt (d}\={\tt efine}\  {\tt HEIGHT}\  
\\
\>{\tt (+}\  \={\tt DISTANCE-BETWEEN-BULBS}
\\
\>\>{\tt (*}\  {\tt 2}\  {\tt RADIUS)}
\\
\>\>{\tt DISTANCE-BETWEEN-BULBS}
\\
\>\>{\tt (*}\  {\tt 2}\  {\tt RADIUS)}
\\
\>\>{\tt DISTANCE-BETWEEN-BULBS}
\\
\>\>{\tt (*}\  {\tt 2}\  {\tt RADIUS)}
\\
\>\>{\tt DISTANCE-BETWEEN-BULBS))}
\end{tabbing}\end{minipage}\end{center}
Develop the necessary definitons separately from the rest of the traffic
light program, then create a single function definition using
{{\tt local}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/multipl-tl-draw.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Now suppose we wish to provide the additional service of resetting an
individual traffic light. That is, in addition to switching from the
current color to the next, an operator should be able to set a traffic
light to red. The function for doing so already exists:
{{\tt init-traffic-light}\/}. It sets {{\tt current-color}\/} to
{{\tt \char'047}{\tt red}\/} and re-draws the image on the canvas. But,
{{\tt init-traffic-light}\/} is inaccessible because it is defined within
the {{\bf local}-exp\-res\-sion} of {{\tt make-traffic-light}\/}.  If we wish the function to
be visible, it must be the result of {{\tt make-traffic-light}\/} just like
{{\tt next}\/}.

To make both {{\tt next}\/} and {{\tt init-traffic-light}\/} a result of
{{\tt make-traffic-light}\/} requires some way of combining the two
functions into a single value. Since functions are values in Scheme, we
could combine the two functions in a list, a structure, or even a vector.
Another possibility is to combine the two functions in a third function. 
Here we discuss this third possibility because it is an important technique
in the context of managing state variables and services. 

We call the new kind of function {{\tt service-manager}\/}, because it hides
and manages functions that implement services. The function accepts two 
symbols: 
\begin{enumerate}
\item {{\tt \char'047}{\tt next}\/}, which indicates that {{\tt (next)}\/} should be
evaluated 

\item {{\tt \char'047}{\tt reset}\/}, which indicates that {{\tt (reset)}\/} should be
evaluated. 
\end{enumerate}
Furthermore, the function is the result of the revised version of
{{\tt make-traffic-light}\/}. 

Figure~\ref{fig:multiple-tl-reset} contains the modified definition of
{{\tt make-traffic-light}\/}.  Since an operator may mistakenly apply
functions to inappropriate arguments, {{\tt service-manager}\/} is a checked
function in the sense of section~\ref{sec:input-errors}. It signals an
error if the input is a symbol other than {{\tt \char'047}{\tt next}\/} or
{{\tt \char'047}{\tt reset}\/}.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt make-traffic-light}\ {\tt :}\ {\tt symbol}\ {\tt number}\ {}{\tt ->}{}\ {\tt (symbol}\ {}{\tt ->}{}\ {\tt true)}\/}}\\
\hbox{;; to create a red light with {{\tt (make-posn}\ {\tt x-posn}\ {\tt 0)}\/} as the upper-left corner}
\\
\hbox{;; effect: draw the traffic light on the canvas}
\\
{\tt (d}\={\tt efine}\  {\tt (make-traffic-light}\  {\tt street}\  {\tt x-posn)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {\underline Model:}          }
\\
\>\>\>\hbox{;; {{\tt current-color}\ {\tt :}\ {\tt TL-color}\/}}
\\
\>\>\>\hbox{;; to keep track of the current color of the traffic light}
\\
\>\>\>{\tt (define}\  {\tt current-color}\  {\tt \char'047}{\tt red)}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt init-traffic-light}\ {\tt :}\ {}{\tt ->}{}\ {\tt true}\/}}
\\
\>\>\>\hbox{;; to (re)set {{\tt current-color}\/} to red and to (re)create the view }
\\
\>\>\>{\tt (define}\  {\tt (init-traffic-light)}\  {\tt ...)}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt next}\ {\tt :}\ {}{\tt ->}{}\ {\tt true}\/}}
\\
\>\>\>\hbox{;; effect: to change {{\tt current-color}\/} from {{\tt \char'047}{\tt green}\/} to {{\tt \char'047}{\tt yellow}\/}, }
\\
\>\>\>\hbox{;; {{\tt \char'047}{\tt yellow}\/} to {{\tt \char'047}{\tt red}\/}, and {{\tt \char'047}{\tt red}\/} to {{\tt \char'047}{\tt green}\/}}
\\
\>\>\>{\tt (define}\  {\tt (next)}\  {\tt ...)}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt next-color}\ {\tt :}\ {\tt TL-color}\ {}{\tt ->}{}\ {\tt TL-color}\/}}
\\
\>\>\>\hbox{;; to compute the successor of {{\tt current-color}\/} based on the traffic laws}
\\
\>\>\>{\tt (define}\  {\tt (next-color}\  {\tt current-color)}\  {\tt ...)}
\\
\>\>\>\hbox{;; {{\tt service-manager}\ {\tt :}\ {\tt (symbol}\ {}{\tt ->}{}\ {\tt true)}\/}}
\\
\>\>\>\hbox{;; to apply either {{\tt next}\/} or {{\tt init-traffic-light}\/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (service-manager}\  {\tt msg)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt next)}\  {\tt (next)\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt reset)}\  {\tt (init-traffic-light)\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt traffic-light}\  {\tt ``message}\  {\tt not}\  {\tt understood'')\char'135}{\tt )))}
\\
\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\hbox{;; Initialize and produce {{\tt service-manager}\/}}
\\
\>\>\>{\tt (init-traffic-light)}
\\
\>\>\>{\tt service-manager)))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Managing multiple traffic lights with a reset service}
\label{fig:multiple-tl-reset} 
\hrule

We use the new {{\tt make-traffic-light}\/} function exactly like the old one:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; create the canvas first }\\
{\tt (start}\  {\tt 300}\  {\tt 160)}
\\
\hbox{;; {{\tt lights}\ {\tt :}\ {\tt (listof}\ {\tt traffic-light)}\/}}
\\
\hbox{;; to manage the lights along Sunrise }
\\
{\tt (d}\={\tt efine}\  {\tt lights}
\\
\>{\tt (list}\  \={\tt (make-traffic-light}\  {\tt \char'047}{\tt sunrise\char'100}{\tt rice}\  {\tt 50)}
\\
\>\>{\tt (make-traffic-light}\  {\tt \char'047}{\tt sunrise\char'100}{\tt cmu}\  {\tt 150)))}
\end{tabbing}\end{minipage}\end{center}
The result is, however, that now every traffic light is represented as a
function on symbols: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt ((second}\  {\tt lights)}\  {\tt \char'047}{\tt next)}\  \\
{\tt true}
\\
{\tt >}\  {\tt (andmap}\  {\tt (lambda}\  {\tt (a-light)}\  {\tt (a-light}\  {\tt \char'047}{\tt reset))}\  {\tt lights)}
\\
{\tt true}
\end{tabbing}\end{minipage}\end{center}
The first interaction switches the initially red light labeld
{{\tt \char'047}{\tt sunrise\char'100}{\tt cmu}\/} to {{\tt \char'047}{\tt green}\/}. The second one changes the state
of every light back to {{\tt \char'047}{\tt red}\/} skipping the {{\tt \char'047}{\tt yellow}\/} stage
for the light at {{\tt \char'047}{\tt sunrise\char'100}{\tt cmu}\/}.


  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 

{\bf Exercise 39.1.4}

 \label{ex:multipl-tl-repl}
Complete the definition of the program in
figure~\ref{fig:multiple-tl-reset}, using the function from
exercise~\ref{ex:multipl-tl-draw}. Then use DrScheme's
{\tt Interactions} window to switch and reset traffic
lights.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/multipl-tl-repl.html}{\rule{3pt}{5pt}}


{\bf Exercise 39.1.5}

 \label{ex:multipl-tl-eval2}
Evaluate the above program by hand and confirm that the light labeled
{{\tt \char'047}{\tt sunrise\char'100}{\tt rice}\/} switches from {{\tt \char'047}{\tt green}\/} directly back to
{{\tt \char'047}{\tt red}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/multipl-tl-eva2.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


For the address-book example from part~\ref{part:state}, the need for
managing two services is even more apparent. After all, the motivating idea
behind the example is that users can access one state variable with two
different services: {{\tt add-to-address-book}\/} for adding new entries and
{{\tt lookup}\/} for looking up the phone number for a given name. Following
our encapsulation receipe, we must
\begin{enumerate}
\item define a function {{\tt make-address-book}\/} whose body is a
{{\bf local}-exp\-res\-sion}; 

\item place the definitions in this {{\bf local}-exp\-res\-sion}; and

\item introduce a function called {{\tt service-manager}\/} to manage the
two services.  
\end{enumerate}
By now, we have the first two steps firmly under control; the last one,
however, is complex here, because unlike in the previous case, the two
functions that implement the services consume different numbers of
arguments and produce different kinds of results.

Let's first agree on the inputs for {{\tt service-manager}\/}. Two good
mnemonic symbols are {{\tt \char'047}{\tt add}\/} for adding phone numbers and
{{\tt \char'047}{\tt search}\/} for looking up the number for some given name. This
suggests the following template:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (service-manager}\  {\tt msg)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt add)}\  {\tt ...}\  {\tt A}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt search)}\  {\tt ...}\  {\tt B}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt address-book}\  {\tt ``message}\  {\tt not}\  {\tt understood'')\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The problem is that it is not clear how to replace {{\tt A}\/} and
{{\tt B}\/} with valid Scheme expressions that compute the appropriate value
and effect. For {{\tt A}\/}, we not only need {{\tt msg}\/} but also a name
and a phone number. For {{\tt B}\/}, we need the name.

One solution is to produce functions that consume the additional arguments
and then perform the appropriate computation. In other words,
{{\tt service-manager}\/} is a now function that produces a function for two
symbols. Since we have not encountered this kind of result before, we
introduce a new form of data definition: 

\begin{quote} An {\sl {address-book}} is an interface:   \begin{enumerate}   \item {{\tt \char'047}{\tt add}\/} :: {{\tt symbol}\ {\tt number}\ {}{\tt ->}{}\ {\tt void}\/}   \item {{\tt \char'047}{\tt search}\/} :: {{\tt symbol}\ {}{\tt ->}{}\ {\tt number}\/}  \end{enumerate}  \end{quote}

\noindent The data definition refers to the concept of {\sc {interface}},
which is a function that consumes a finite number of symbols and produces 
functions with different types in return. Because this kind of function is
radically different from what we have seen before, we use a different
name. 

Now it is possible to write a contract and a purpose statement:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt service-manager}\ {\tt :}\ {\tt address-book}\/}}\\
\hbox{;; to manage addition to, and searches in, the address book }
\\
{\tt (define}\  {\tt (service-manager}\  {\tt msg)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
To define the function, we distinguish the two cases. In the case of
{{\tt \char'047}{\tt add}\/}, it is obvious what we produce:
{{\tt add-to-address-book}\/}. In the case of {{\tt \char'047}{\tt search}\/}, we need a
function that consumes a symbol and then applies {{\tt lookup}\/} to this
symbol and the {{\tt local}\/}ly defined {{\tt address-book}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ambda}\  {\tt (name)}\\
\>{\tt (lookup}\  {\tt name}\  {\tt address-book))}
\end{tabbing}\end{minipage}\end{center}
The use of {{\tt lambda}\/} is appropriate here, because the function
doesn't need a name. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt make-address-book}\ {\tt :}\ {\tt string}\ {}{\tt ->}{}\ {\tt address-book}\/}}\\
\hbox{;; to create a function that manages all the services for a hidden address book}
\\
{\tt (d}\={\tt efine}\  {\tt (make-address-book}\  {\tt title)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define-struct}\  {\tt entry}\  {\tt (name}\  {\tt number))}
\\
\>\>\>\hbox{;; {{\tt address-book}\ {\tt :}\ {\tt (listof}\ {\tt (list}\ {\tt name}\ {\tt number))}\/}}
\\
\>\>\>\hbox{;; to maintain a list of name-phone number associations}
\\
\>\>\>{\tt (define}\  {\tt address-book}\  {\tt empty)}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt add-to-address-book}\ {\tt :}\ {\tt symbol}\ {\tt number}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\>\>\>\hbox{;; effect: to add a name-phone number association to {{\tt address-book}\/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (add-to-address-book}\  {\tt name}\  {\tt phone)}
\\
\>\>\>\>{\tt (set!}\  {\tt address-book}\  {\tt (cons}\  {\tt (make-entry}\  {\tt name}\  {\tt phone)}\  {\tt address-book)))}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt lookup}\ {\tt :}\ {\tt symbol}\ {\tt (listof}\ {\tt (list}\ {\tt symbol}\ {\tt number))}\ {}{\tt ->}{}\ {\tt number}\ {\tt or}\ {\tt false}\/}}
\\
\>\>\>\hbox{;; to lookup the phone number for {{\tt name}\/} in {{\tt address-book}\/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (lookup}\  {\tt name}\  {\tt ab)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt ab)}\  {\sf false}{\tt \char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (entry-name}\  {\tt (first}\  {\tt ab))}\  {\tt name)}
\\
\>\>\>\>\>\>\>{\tt (entry-number}\  {\tt (first}\  {\tt ab))\char'135}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (lookup}\  {\tt name}\  {\tt (rest}\  {\tt ab))\char'135}{\tt )\char'135}{\tt ))}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt service-manager}\ {\tt :}\ {\tt address-book}\ {\tt object}\/}}
\\
\>\>\>\hbox{;; to manage addition to, and searches in, the address book }
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (service-manager}\  {\tt msg)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt add)}
\\
\>\>\>\>\>\>{\tt add-to-address-book\char'135}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt search)}
\\
\>\>\>\>\>\>{\tt (l}\={\tt ambda}\  {\tt (name)}
\\
\>\>\>\>\>\>\>{\tt (lookup}\  {\tt name}\  {\tt address-book))\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt address-book}\  {\tt ``message}\  {\tt not}\  {\tt understood'')\char'135}{\tt )))}
\\
\>\>{\tt service-manager))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Managing multiple address books} \label{fig:multiple-ab}
\hrule

Figure~\ref{fig:multiple-ab} shows the complete definition of
{{\tt make-address-book}\/}. The definition is standard by now. It consists
of a {{\bf local}-exp\-res\-sion}, which in turn produces the {{\tt local}\/}ly defined
{{\tt service-manager}\/} as the result. There is no need for an initializer
because the only state variable is immediately initialized and there is no
graphical view. 

To use the address books, we first create one with {{\tt make-address-book}\/},
using an appropriate title:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt friends}\ {\tt :}\ {\tt an}\ {\tt address}\ {\tt book}\/}}\\
\hbox{;; to maintain an address book for friends }
\\
{\tt (d}\={\tt efine}\  {\tt friends}
\\
\>{\tt (make-address-book}\  {\tt ``Friends}\  {\tt of}\  {\tt Charles''))}
\\
\hbox{;; {{\tt business}\ {\tt :}\ {\tt an}\ {\tt address}\ {\tt book}\/}}
\\
\hbox{;; to maintain an address book for business colleagues}
\\
{\tt (d}\={\tt efine}\  {\tt business}
\\
\>{\tt (make-address-book}\  {\tt ``Colleagues}\  {\tt \char'100}\  {\tt Rice\char'054}\  {\tt Inc.''))}
\end{tabbing}\end{minipage}\end{center}
Then we can add names and phone numbers to the address book, or we can
retrieve numbers as desired: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt ((friends}\  {\tt \char'047}{\tt add)}\  {\tt \char'047}{\tt Bill}\  {\tt 2)}\\
{\tt >}\  {\tt ((friends}\  {\tt \char'047}{\tt add)}\  {\tt \char'047}{\tt Sally}\  {\tt 3)}
\\
{\tt >}\  {\tt ((friends}\  {\tt \char'047}{\tt add)}\  {\tt \char'047}{\tt Dave}\  {\tt 4)}
\\
{\tt >}\  {\tt ((business}\  {\tt \char'047}{\tt add)}\  {\tt \char'047}{\tt Emil}\  {\tt 5)}
\\
{\tt >}\  {\tt ((business}\  {\tt \char'047}{\tt add)}\  {\tt \char'047}{\tt Faye}\  {\tt 18)}
\end{tabbing}\end{minipage}\end{center}
In this case, we added three entries to the address book named
{{\tt friends}\/} and two to the one called {{\tt business}\/}. 

An addition to, say, {{\tt friends}\/} works in two steps. The first step is
to apply {{\tt friends}\/} to {{\tt \char'047}{\tt add}\/}. This yields the (hidden) function 
{{\tt add-to-address-book}\/}. The second step is to apply this resulting
function to a name and a number. In a similar vein, looking up a phone number
also works in two steps. The application of, say, {{\tt friends}\/} to
{{\tt \char'047}{\tt search}\/} yields a function that consumes a name. This function is then
applied to a symbol: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt ((friends}\  {\tt \char'047}{\tt search)}\  {\tt \char'047}{\tt Bill)}\\
{\tt 2}
\\
{\tt >}\  {\tt ((business}\  {\tt \char'047}{\tt search)}\  {\tt \char'047}{\tt Bill)}
\\
{\tt false}
\end{tabbing}\end{minipage}\end{center}
The two applications show that the number for {{\tt \char'047}{\tt Bill}\/} in
{{\tt friends}\/} is {{\tt 2}\/} and that there is no number for {{\tt \char'047}{\tt Bill}\/}
in colleagues. According to the above additions, that's exactly what we should
expect. Of course, we could also co-mingle the two actions in the
{\tt Interactions} window, adding and searching for phone numbers at will.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 

{\bf Exercise 39.1.6}

 \label{ex:multipl-ab-eval-13}
Develop an interface definition for the results of the revised version of
{{\tt make-traffic-light}\/} (see
figure~\ref{fig:multiple-tl-reset}).~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/multipl-ab-eval-13.html}{\rule{3pt}{5pt}}


{\bf Exercise 39.1.7}

 \label{ex:multipl-ab-eval}
Show the top-level definitions that the evaluation of {{\tt friends}\/} and
{{\tt colleagues}\/} creates.

What is the state of these definitions after the five {{\tt \char'047}{\tt add}\/}
expressions have been evaluated?  Evaluate {{\tt ((friends}\ {\tt \char'047}{\tt search)}\ {\tt \char'047}{\tt Bill)}\/}
in this context.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/multipl-ab-eval.html}{\rule{3pt}{5pt}}


{\bf Exercise 39.1.8}

 \label{ex:multipl-ab-gui}
Design {{\tt gui-for-address-boook}\/}. The function consumes a list
of strings and creates a new address book for each one of them. It also
creates and displays a graphical user interface for an address book with a
choice menu that lets users choose to which address book they want to add an
entry and in which address book the program should search for a
number.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/multipl-ab-gui.html}{\rule{3pt}{5pt}}


\subsection{Practice with Encapsulation} \label{sec:ex-encap-state}

{\bf Exercise 39.2.1}

 \label{ex:local-traffic}
Develop the program {{\tt make-city}\/}. It manages a collection of traffic
lights. The program should provide four services:
\begin{enumerate}
\item adding a traffic light with a label (string); 
\item removing a traffic light by label; 
\item switching the state of a traffic light with some given label; and 
\item resetting a traffic light to red with some given label. 
\end{enumerate}
\noindent{\bf Hint:}  The first two services are provided directly; the last two are
implemented by the simulated traffic lights. 

After the development of the program is completed, develop a graphical user
interface.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/local-traffic.html}{\rule{3pt}{5pt}}


{\bf Exercise 39.2.2}

 \label{ex:local-master}
Develop {{\tt make-master}\/}. The program consumes nothing, creates an
instance of the color-guessing game of section~\ref{sec:color-state}, and
produces the {{\tt master-check}\/} function as the only result. After the
player has guessed the answer, the function should simply respond with
``game over.'' A typical dialog would proceed as follows:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt (define}\  {\tt master1}\  {\tt (make-master))}\\
{\tt >}\  {\tt (master-check}\  {\tt \char'047}{\tt red}\  {\tt \char'047}{\tt red)}
\\
{\tt \char'047}{\tt NothingCorrect}
\\
{\tt >}\  {\tt (master-check}\  {\tt \char'047}{\tt black}\  {\tt \char'047}{\tt pink)}
\\
{\tt \char'047}{\tt OneColorOccurs}
\\
{\tt ...}\  
\end{tabbing}\end{minipage}\end{center}
Compare this with the first dialog in section~\ref{sec:color-state}.

Add a service to {{\tt make-master}\/} that reveals the hidden colors. That
way a player who is tired of playing the game can find out the
answer.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/local-master.html}{\rule{3pt}{5pt}}


{\bf Exercise 39.2.3}

 \label{ex:local-hangman}
Develop {{\tt make-hangman}\/}. The program consumes a list of words,
creates a hangman game using the list, and produces the
{{\tt hangman-guess}\/} function as a result. A player would use the dialog
as follows:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt >}\  {\tt (define}\  {\tt hangman-easy}\  {\tt (make-hangman}\  {\tt (list}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt an}\  {\tt \char'047}{\tt and}\  {\tt \char'047}{\tt able}\  {\tt \char'047}{\tt adler)))}\\
{\tt >}\  {\tt (define}\  {\tt hangman-difficult}\  {\tt (make-hangman}\  {\tt (list}\  {\tt \char'047}{\tt ardvark}\  {\tt ...)))}
\\
{\tt >}\  {\tt (hangman-easy}\  {\tt \char'047}{\tt a)}
\\
{\tt ``You}\  {\tt won''}
\\
{\tt >}\  {\tt (hangman-difficult}\  {\tt \char'047}{\tt a)}
\\
{\tt (list}\  {\tt \char'047}{\tt head}\  {\tt (list}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}\  {\tt \char'047}{\tt \char'137}{\tt ))}
\\
{\tt >}\  {\tt ...}\  
\end{tabbing}\end{minipage}\end{center}
Compare this with the first dialog in section~\ref{sec:count-state}. 

Add a service to {{\tt make-master}\/} that reveals the chosen word.

An optional extension is to equip the program with a graphical user
interface and a graphical view of the stick figure. Reuse existing 
solutions as much as possible.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/local-hangman.html}{\rule{3pt}{5pt}}


{\bf Exercise 39.2.4}

 \label{ex:local-campus-tours}
Develop {{\tt make-player}\/}. The program abstracts over the functions of
section~\ref{sec:explore-state}. Using the program, we can create several
players that wander through the campus: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt player1}\  {\tt (make-player}\  {\tt \char'047}{\tt BioEngineering))}\\
{\tt (define}\  {\tt player2}\  {\tt (make-player}\  {\tt \char'047}{\tt MuddBuilding))}
\\
{\tt ...}
\end{tabbing}\end{minipage}\end{center}
The argument to {{\tt make-player}\/} specifies the initial position of the
player. 

Each instance should be able to produce
\begin{enumerate}
\item a picture of the current surroundings; 
\item a list of the available building connections; and 
\item a move from one place to another through an available connection. 
\end{enumerate}

\medskip\noindent{\bf Extension}:\  Two players may be in the same building at the same time,
but they cannot interact. Extend the game so that two players in the same
building can interact in some form.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/local-campus-tours.html}{\rule{3pt}{5pt}}


{\bf Exercise 39.2.5}

 \label{ex:localmoving}
Develop the program {{\tt moving-pictures}\/}. It consumes a position and a
picture, that is, a list of shapes as defined in
sections~\ref{sec:move-circle}, and~\ref{sec:move-shape},
and~\ref{sec:move-fig}. (Also see~\ref{sec:abs-moving} for functions on
moving pictures.) It supports two services. First, it can place the shape
at a specific position. Second, it can reset the picture to the initially
given position.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/localmoving.html}{\rule{3pt}{5pt}}


\section{Mutable Structures} \label{sec:mutable}

Encapsulating and managing state variables is similar to forming and
managing structures.  When we first apply a function that abstracts over
state variables we provide initial values for some of the variables. The
service manager serves the (current) value of these variables, which is
similar to extracting the values of fields in structures. Not surprisingly
then, the technique can simulate the constructors and selectors of a
{{\tt define-struct}\/} definition. This simulation naturally suggests the
introduction of functions that modify the value in a structure's field. The
following subsections spell out the details behind this idea; the last
subsection generalizes it to vectors.


\subsection{Structures from Functions} \label{sec:struct-func}

\hrule
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt posn}\  {\tt (x}\  {\tt y))}\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\\
\relax
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (f-make-posn}\  {\tt x0}\  {\tt y0)}\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt x}\  {\tt y0)}
\\
\>\>\>{\tt (define}\  {\tt y}\  {\tt y0)}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (service-manager}\  {\tt msg)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt x)}\  {\tt x\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt y)}\  {\tt y\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt posn}\  {\tt ``...'')\char'135}{\tt )))}
\\
\>\>{\tt service-manager))}
\\
\relax
\\
{\tt (d}\={\tt efine}\  {\tt (f-posn-x}\  {\tt p)}
\\
\>{\tt (p}\  {\tt \char'047}{\tt x))}
\\
\relax
\\
{\tt (d}\={\tt efine}\  {\tt (f-posn-y}\  {\tt p)}
\\
\>{\tt (p}\  {\tt \char'047}{\tt y))}
\end{tabbing}\end{minipage}
\end{center}
\center{Figure: A functional analog of {\it posn\/}} \label{fig:functional-posn}
\hrule

\noindent Take a look at figure~\ref{fig:functional-posn}. The left-hand
side is the one-line definition of a {{\tt posn}\/} structure. The
right-hand side is a functional definition that provides almost all the
same services. In particular, the definition provides a constructor that
consumes two values and constructs a compound value, and two selectors for
extracting the values that went into the construction of a compound value.

To understand why {{\tt f-make-posn}\/} is a constructor and why
{{\tt f-posn-x}\/} and {{\tt f-posn-y}\/} are selectors, we can discuss how they
work, and we can confirm that they validate the expected equations. Here we do
both, because the definitions are unusual.

The definition of {{\tt f-make-posn}\/} encapsulates two variable definitions
and one function definition. The two variables stand for the arguments of
{{\tt f-make-posn}\/} and the function is a service manager; it produces the
value of {{\tt x}\/} when given {{\tt \char'047}{\tt x}\/} and the value of {{\tt y}\/} when
given {{\tt \char'047}{\tt y}\/}. In the preceding section, we might have written something
like
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt a-posn}\  {\tt (f-make-posn}\  {\tt 3}\  {\tt 4))}\\
{\tt (+}\  {\tt (a-posn}\  {\tt \char'047}{\tt x)}\  {\tt (a-posn}\  {\tt \char'047}{\tt y))}
\end{tabbing}\end{minipage}\end{center}
to define and to compute with {{\tt f-make-posn}\/}. Since selecting values
is such a frequent operation, figure~\ref{fig:functional-posn} introduces 
the functions {{\tt f-posn-x}\/} and {{\tt f-posn-y}\/}, which perform these
computations. 

When we first introduced structures rigorously in intermezzo~1, we said
that the selectors and constructors can be described with equations. For a
definition such as that for {{\tt posn}\/}, the two relevant equations are:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (posn-x}\  {\tt (make-posn}\  {\tt V-1}\  {\tt V-2))}\  \\
{\tt =}\  \={\tt V-1}
\\
\hbox{;and}
\\
\>{\tt (posn-y}\  {\tt (make-posn}\  {\tt V-1}\  {\tt V-2))}\  
\\
{\tt =}\  {\tt V-2}
\end{tabbing}\end{minipage}\end{center}
where {{\tt V-1}\/} and {{\tt V-2}\/} are arbitrary values. 

To confirm that {{\tt f-posn-x}\/} and {{\tt f-make-posn}\/} are in the same
relationship as {{\tt posn-x}\/} and {{\tt make-posn}\/}, we can validate that
they satisfy the first equation:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (f-posn-x}\  {\tt (f-make-posn}\  {\tt 3}\  {\tt 4))}\\
{\tt =}\  {\tt (f-posn-x}\  \={\tt (local}\  {\tt (}\={\tt (define}\  {\tt x}\  {\tt 3)}
\\
\>\>{\tt (define}\  {\tt y}\  {\tt 4)}
\\
\>\>{\tt (d}\={\tt efine}\  {\tt (service-manager}\  {\tt msg)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt x)}\  {\tt x\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt y)}\  {\tt y\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt posn}\  {\tt ``...'')\char'135}{\tt )))}
\\
\>{\tt service-manager))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  \={\tt (f-posn-x}\  {\tt service-manager)}\\
\>\hbox{;; add to top-level definitions:}
\\
\>{\tt (define}\  {\tt x}\  {\tt 3)}
\\
\>{\tt (define}\  {\tt y}\  {\tt 4)}
\\
\>{\tt (d}\={\tt efine}\  {\tt (service-manager}\  {\tt msg)}
\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt x)}\  {\tt x\char'135}
\\
\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt y)}\  {\tt y\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt posn}\  {\tt ``...'')\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt =}\  {\tt (service-manager}\  {\tt \char'047}{\tt x)}\\
{\tt =}\  {\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt x}\  {\tt \char'047}{\tt x)}\  {\tt x\char'135}
\\
\>{\tt \char'133}{\tt (symbol=?}\  {\tt \char'047}{\tt x}\  {\tt \char'047}{\tt y)}\  {\tt y\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt posn}\  {\tt ``...'')\char'135}{\tt )}
\\
{\tt =}\  {\tt x}
\\
{\tt =}\  {\tt 3}
\end{tabbing}\end{minipage}\end{center}
It is an exercise to show that {{\tt f-posn-y}\/} and {{\tt f-make-posn}\/}
satisfy the analogous equation. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 40.1.1}

 \label{ex:func-struct0}
Which function does the simulation of structures not provide? Why
not?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/func-struct0.html}{\rule{3pt}{5pt}} 


{\bf Exercise 40.1.2}

 \label{ex:func-struct1}
Here is yet another implementation of {{\tt posn}\/} structures:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (ff-make-posn}\  {\tt x}\  {\tt y)}\\
\>{\tt (l}\={\tt ambda}\  {\tt (select)}
\\
\>\>{\tt (select}\  {\tt x}\  {\tt y)))}
\\
{\tt (d}\={\tt efine}\  {\tt (ff-posn-x}\  {\tt a-ff-posn)}
\\
\>{\tt (a-ff-posn}\  {\tt (lambda}\  {\tt (x}\  {\tt y)}\  {\tt x)))}
\\
{\tt (d}\={\tt efine}\  {\tt (ff-posn-y}\  {\tt a-ff-posn)}
\\
\>{\tt (a-ff-posn}\  {\tt (lambda}\  {\tt (x}\  {\tt y)}\  {\tt y)))}
\end{tabbing}\end{minipage}\end{center}
Evaluate {{\tt (ff-posn-x}\ {\tt (ff-make-posn}\ {\tt V-1}\ {\tt V2))}\/} in this context. What
does the calculation demonstrate?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/func-struct1.html}{\rule{3pt}{5pt}}


{\bf Exercise 40.1.3}

 \label{ex:func-struct}
Provide functional implementations for these structure definitions:
\begin{enumerate}
\item {{\tt (define-struct}\ {\tt movie}\ {\tt (title}\ {\tt producer))}\/}
\item {{\tt (define-struct}\ {\tt boyfriend}\ {\tt (name}\ {\tt hair}\ {\tt eyes}\ {\tt phone))}\/}
\item {{\tt (define-struct}\ {\tt cheerleader}\ {\tt (name}\ {\tt number))}\/}
\item {{\tt (define-struct}\ {\tt CD}\ {\tt (artist}\ {\tt title}\ {\tt price))}\/}
\item {{\tt (define-struct}\ {\tt sweater}\ {\tt (material}\ {\tt size}\ {\tt producer))}\/}~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/func-struct.html}{\rule{3pt}{5pt}}
\end{enumerate}


\subsection{Mutable Functional Structures} \label{sec:imp-struct-func}

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (fm-make-posn}\  {\tt x0}\  {\tt y0)}\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt x}\  {\tt y0)}
\\
\>\>\>{\tt (define}\  {\tt y}\  {\tt y0)}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (service-manager}\  {\tt msg)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt x)}\  {\tt x\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt y)}\  {\tt y\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt set-x)}\  {\tt (lambda}\  {\tt (x-new)}\  {\tt (set!}\  {\tt x}\  {\tt x-new))\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt set-y)}\  {\tt (lambda}\  {\tt (y-new)}\  {\tt (set!}\  {\tt y}\  {\tt y-new))\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt posn}\  {\tt ``...'')\char'135}{\tt )))}
\\
\>\>{\tt service-manager))}
\\
{\tt (d}\={\tt efine}\  {\tt (fm-posn-x}\  {\tt p)}
\\
\>{\tt (p}\  {\tt \char'047}{\tt x))}
\\
{\tt (d}\={\tt efine}\  {\tt (fm-posn-y}\  {\tt p)}
\\
\>{\tt (p}\  {\tt \char'047}{\tt y))}
\\
{\tt (d}\={\tt efine}\  {\tt (fm-set-posn-x!}\  {\tt p}\  {\tt new-value)}
\\
\>{\tt ((p}\  {\tt \char'047}{\tt set-x)}\  {\tt new-value))}
\\
{\tt (d}\={\tt efine}\  {\tt (fm-set-posn-y!}\  {\tt p}\  {\tt new-value)}
\\
\>{\tt ((p}\  {\tt \char'047}{\tt set-y)}\  {\tt new-value))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: An implementation of {\it posn\/}s with mutators} \label{fig:imperative-posn}
\hrule

\noindent Together, sections~\ref{sec:encapsulate}
and~\ref{sec:struct-func} suggest that structures are mutable. That is, we
should be able to change the values of some field in a structure. After
all, we introduced the service managers in section~\ref{sec:encapsulate} to
hide state variables, not just ordinary variable definitions.

Figure~\ref{fig:imperative-posn} shows how a small change to the
definitions of figure~\ref{fig:functional-posn} turns the {{\tt local}\/}ly
hidden variables into state variables. The modified service manager offers
two services per state variable: one for looking up the current value and
one for changing it. 

Consider the following definition and expression: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt a-posn}\  {\tt (fm-make-posn}\  {\tt 3}\  {\tt 4))}\\
{\tt (b}\={\tt egin}
\\
\>{\tt (fm-set-posn-x!}\  {\tt a-posn}\  {\tt 5)}
\\
\>{\tt (+}\  {\tt (posn-x}\  {\tt a-posn)}\  {\tt 8))}
\end{tabbing}\end{minipage}\end{center}
Evaluating them by hand shows how structures change. Here is the first
step:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt ...}\  \\
{\tt =}\  \=
\\
\>{\tt (define}\  {\tt x-for-a-posn}\  {\tt 3)}
\\
\>{\tt (define}\  {\tt y-for-a-posn}\  {\tt 4)}
\\
\>{\tt (d}\={\tt efine}\  {\tt (service-manager-for-a-posn}\  {\tt msg)}
\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt x)}\  {\tt x-for-a-posn\char'135}
\\
\>\>\>{\tt \char'133}{\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt y)}\  {\tt y-for-a-posn\char'135}
\\
\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt set-x)}
\\
\>\>\>\>{\tt (lambda}\  {\tt (x-new)}\  {\tt (set!}\  {\tt x-for-a-posn}\  {\tt x-new))\char'135}
\\
\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt set-y)}
\\
\>\>\>\>{\tt (lambda}\  {\tt (y-new)}\  {\tt (set!}\  {\tt y-for-a-posn}\  {\tt y-new))\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt posn}\  {\tt ``...'')\char'135}{\tt ))}
\\
\>{\tt (define}\  {\tt a-posn}\  {\tt service-manager-for-a-posn)}
\\
\  \  {\tt (b}\={\tt egin}
\\
\>{\tt (fm-set-posn-x!}\  {\tt a-posn}\  {\tt 5)}
\\
\>{\tt (+}\  {\tt (posn-x}\  {\tt a-posn)}\  {\tt 8))}
\end{tabbing}\end{minipage}\end{center}
It renames and lifts the local definitions from inside of
{{\tt fm-make-posn}\/}. Because the function definition doesn't change for
the rest of the evaluation, we focus on just the variable definitions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (define}\  {\tt x-for-a-posn}\  {\tt 3)}\\
\>{\tt (define}\  {\tt y-for-a-posn}\  {\tt 4)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (fm-set-posn-x!}\  {\tt a-posn}\  {\tt 5)}
\\
\>\>{\tt (+}\  {\tt (posn-x}\  {\tt a-posn)}\  {\tt 8))}
\\
{\tt =}\  \={\tt (define}\  {\tt x-for-a-posn}\  {\tt 3)}
\\
\>{\tt (define}\  {\tt y-for-a-posn}\  {\tt 4)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (fm-set-posn-x!}\  {\tt service-manager-for-a-posn}\  {\tt 5)}
\\
\>\>{\tt (+}\  {\tt (posn-x}\  {\tt a-posn)}\  {\tt 8))}
\\
{\tt =}\  \={\tt (define}\  {\tt x-for-a-posn}\  {\tt 3)}
\\
\>{\tt (define}\  {\tt y-for-a-posn}\  {\tt 4)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt ((service-manager-for-a-posn}\  {\tt \char'047}{\tt set-x)}\  {\tt 5)}
\\
\>\>{\tt (+}\  {\tt (posn-x}\  {\tt a-posn)}\  {\tt 8))}
\\
{\tt =}\  \={\tt (define}\  {\tt x-for-a-posn}\  {\tt 3)}
\\
\>{\tt (define}\  {\tt y-for-a-posn}\  {\tt 4)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt x-for-a-posn}\  {\tt 5)}
\\
\>\>{\tt (+}\  {\tt (posn-x}\  {\tt a-posn)}\  {\tt 8))}
\\
{\tt =}\  \={\tt (define}\  {\tt x-for-a-posn}\  {\tt 5)}
\\
\>{\tt (define}\  {\tt y-for-a-posn}\  {\tt 4)}
\\
\>{\tt (+}\  {\tt (posn-x}\  {\tt a-posn)}\  {\tt 8)}\  
\end{tabbing}\end{minipage}\end{center}
At this point, the definition of {{\tt x-for-a-posn}\/} has been modified in
the expected manner. From now on every reference to this state variable,
which represents the (simulated) {{\tt x}\/} field {{\tt a-posn}\/}, stands
for {{\tt 5}\/}. Every further reference to {{\tt x-for-a-posn}\/} produces
{{\tt 5}\/}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 40.2.1}

 \label{ex:imp-struct2}
Develop a functional representation for the following structure definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt boyfriend}\  {\tt (name}\  {\tt hair}\  {\tt eyes}\  {\tt phone))}\end{tabbing}\end{minipage}\end{center}
such that the fields of the simulated structure can be changed.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/imp-struct2.html}{\rule{3pt}{5pt}}


{\bf Exercise 40.2.2}

 \label{ex:imp-struct3}
Here is a modification of the function-based implementation of
{{\tt posn}\/} structures in exercise~\ref{ex:func-struct1}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (ffm-make-posn}\  {\tt x0}\  {\tt y0)}\\
\>{\tt (local}\  {\tt (}\={\tt (define}\  {\tt x}\  {\tt x0)}
\\
\>\>{\tt (define}\  {\tt (set-x}\  {\tt new-x)}\  {\tt (set!}\  {\tt x}\  {\tt new-x))}
\\
\>\>{\tt (define}\  {\tt y}\  {\tt y0)}
\\
\>\>{\tt (define}\  {\tt (set-y}\  {\tt new-y)}\  {\tt (set!}\  {\tt y}\  {\tt new-y)))}
\\
\>{\tt (l}\={\tt ambda}\  {\tt (select)}
\\
\>\>{\tt (select}\  {\tt x}\  {\tt y}\  {\tt set-x}\  {\tt set-y))))}
\\
{\tt (d}\={\tt efine}\  {\tt (ffm-posn-x}\  {\tt a-ffm-posn)}
\\
\>{\tt (a-ffm-posn}\  {\tt (lambda}\  {\tt (x}\  {\tt y}\  {\tt sx}\  {\tt sy)}\  {\tt x)))}
\\
{\tt (d}\={\tt efine}\  {\tt (ffm-posn-y}\  {\tt a-ffm-posn)}
\\
\>{\tt (a-ffm-posn}\  {\tt (lambda}\  {\tt (x}\  {\tt y}\  {\tt sx}\  {\tt sy)}\  {\tt y)))}
\\
{\tt (d}\={\tt efine}\  {\tt (ffm-set-posn-x!}\  {\tt a-ffm-posn}\  {\tt new-value)}
\\
\>{\tt (a-ffm-posn}\  {\tt (lambda}\  {\tt (x}\  {\tt y}\  {\tt sx}\  {\tt sy)}\  {\tt (sx}\  {\tt new-value))))}
\\
{\tt (d}\={\tt efine}\  {\tt (ffm-set-posn-y!}\  {\tt a-ffm-posn}\  {\tt new-value)}
\\
\>{\tt (a-ffm-posn}\  {\tt (lambda}\  {\tt (x}\  {\tt y}\  {\tt sx}\  {\tt sy)}\  {\tt (sy}\  {\tt new-value))))}
\end{tabbing}\end{minipage}\end{center}
Demonstrate how to modify a structure like {{\tt (ffm-make-posn}\ {\tt 3}\ {\tt 4)}\/} so
that its {{\tt y}\/} field contains {{\tt 5}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/imp-struct3.html}{\rule{3pt}{5pt}}


\subsection{Mutable Structures} \label{sec:mutable-struct}

Scheme structures are mutable. In {\tt Advanced Student} Scheme, a structure
definition such as 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-structure}\  {\tt posn}\  {\tt (x}\  {\tt y))}\end{tabbing}\end{minipage}\end{center}
introduces six primitives, not just four: 
\begin{enumerate}
\item {{\tt make-posn}\/}, the constructor; 
\item {{\tt posn-x}\/} and {{\tt posn-y}\/}, the selectors; 
\item {{\tt posn?}\/}, the predicate; and 
\item {{\tt set-posn-x!}\/} and {{\tt set-posn-y!}\/}, the {\sc {mutators}}. 
\end{enumerate}
The mutators are operations that change the contents of a structure. 


Recall that we think of a structure as a box with compartments. For example,
the structure 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-posn}\  {\tt 3}\  {\tt 4)}\end{tabbing}\end{minipage}\end{center}
should be visualized as a box with two compartments: 
\begin{center}
 \begin{picture}(130,30) 
\put(00,00){\line(+1,0){130}} 
\put(130,00){\line(0,+1){30}} 
\put(130,30){\line(-1,0){130}} 
\put(00,30){\line(0,-1){30}} 
\put(03,22){\mbox{\footnotesize{\emph{x:}}}} 
\put(60,00){\line(0,+1){30}} 
\put(63,22){\mbox{\footnotesize{\emph{y:}}}} 
\put(10,05){\mbox{\small{{\tt 3}\/}}} 
\put(70,05){\mbox{\small{{\tt 4}\/}}} \end{picture}
\end{center}
A constructor creates a box; a selector extracts the value from a particular
compartment; the predicate recognizes it; and the mutator changes the content
of a compartment. That is, a mutator has an effect on its arguments; its
result is the invisible value. Pictorially, we should imagine an
evaluation step for an expression such as
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt p}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}\\
{\tt (set-posn-x!}\  {\tt p}\  {\tt 5)}
\end{tabbing}\end{minipage}\end{center}
as a box with the old {{\tt x}\/} value deleted and a new one inserted into
the same box: 
\begin{center}
\begin{picture}(130,30)
\put(0,0){ \begin{picture}(130,30) 
\put(00,00){\line(+1,0){130}} 
\put(130,00){\line(0,+1){30}} 
\put(130,30){\line(-1,0){130}} 
\put(00,30){\line(0,-1){30}} 
\put(03,22){\mbox{\footnotesize{\emph{x:}}}} 
\put(60,00){\line(0,+1){30}} 
\put(63,22){\mbox{\footnotesize{\emph{y:}}}} 
\put(10,05){\mbox{\small{{\tt 3}\/}}} 
\put(70,05){\mbox{\small{{\tt 4}\/}}} \end{picture}}
\put(30,05){\mbox{\small5}}
\put(16,08){\line(+1,+1){11}}
\put(16,08){\line(+1,-1){11}}
\put(16,08){\line(-1,+1){11}}
\put(16,08){\line(-1,-1){11}}
\end{picture}
\end{center}

Consider the following definitions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}\\
{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt drums))}
\end{tabbing}\end{minipage}\end{center}
Let's consider the effect and computation of the following expression: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (b}\={\tt egin}\\
\>{\tt (set-star-instrument!}\  {\tt p}\  {\tt \char'047}{\tt vocals)}
\\
\>{\tt (list}\  {\tt (star-instrument}\  {\tt p)))}
\end{tabbing}\end{minipage}\end{center}
According to our explanation, the first subexpression modifies the
{{\tt instrument}\/} field of the {{\tt star}\/} structure named {{\tt p}\/};
the second one produces a list of one item, the current value the
{{\tt instrument}\/} field in the structure name {{\tt p}\/}.  By analogy to
section~\ref{sec:imp-struct-func}, the evaluation proceeds as follows:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt drums))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set-star-instrument!}\  {\tt p}\  {\tt \char'047}{\tt vocals)}
\\
\>\>{\tt (list}\  {\tt (star-instrument}\  {\tt p)))}
\\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt vocals))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (}{\sf void}{\tt )}
\\
\>\>{\tt (list}\  {\tt (star-instrument}\  {\tt p)))}
\\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt vocals))}
\\
\>{\tt (list}\  {\tt \char'047}{\tt vocals)}
\end{tabbing}\end{minipage}\end{center}
The first step changes one part of the value in the definition of
{{\tt p}\/}, but not the entire value. The second one extracts the current
value of the {{\tt instrument}\/} field and places it in a list.

The introduction of mutators for structures requires two changes to our
system of evaluation:
rules:
\begin{enumerate}
\item Every constructor expressions adds a definition with a new, unique
name to the top level, unless it already occurs in a
definition.\footnote{For simplicity, we use this simple approximation. When a program also uses {\bf set!}-exp\-res\-sion, we must rely on the refinement of intermezzo~8.3 to understand its behavior completely. Also see the note on this topic on page~\pageref{pg:note-set-vs-set}.}

\item A name that stands for a structure is a value. 
\end{enumerate}
We can understand these changes if we think of each structure as a function
that manages services such as looking up the current value of a field and
modifying the field. After all, {{\tt local}\/} function definitions also
create top-level definitions with unique names. And, the names of functions
are values, too.

Using these two new rules we can study the unusual behavior of mutators in
more depth. Here is a first example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}\\
{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt drums))}
\\
{\tt (define}\  {\tt q}\  {\tt p)}
\\
{\tt (b}\={\tt egin}
\\
\>{\tt (set-star-instrument!}\  {\tt p}\  {\tt \char'047}{\tt vocals)}
\\
\>{\tt (list}\  {\tt (star-instrument}\  {\tt q)))}
\end{tabbing}\end{minipage}\end{center}
It differs from the first in two ways. First, it defines {{\tt q}\/} to be
{{\tt p}\/}. Second, the second subexpression of the
{{\bf begin}-exp\-res\-sion} refers to {{\tt q}\/} not {{\tt p}\/}. Let's
check our understanding of the evaluation process:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt drums))}
\\
\>{\tt (define}\  {\tt q}\  {\tt p)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set-star-instrument!}\  {\tt p}\  {\tt \char'047}{\tt vocals)}
\\
\>\>{\tt (list}\  {\tt (star-instrument}\  {\tt q)))}
\\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt vocals))}
\\
\>{\tt (define}\  {\tt q}\  {\tt p)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (}{\sf void}{\tt )}
\\
\>\>{\tt (list}\  {\tt (star-instrument}\  {\tt q)))}
\end{tabbing}\end{minipage}\end{center}
As before, the first step changes one part of the definition of
{{\tt p}\/}. The second step is to look up {{\tt q}\/}'s current value: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \  {\tt ...}\\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt vocals))}
\\
\>{\tt (define}\  {\tt q}\  {\tt p)}
\\
\>{\tt (list}\  {\tt (star-instrument}\  {\tt p))}
\\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt vocals))}
\\
\>{\tt (define}\  {\tt q}\  {\tt p)}
\\
\>{\tt (list}\  {\tt \char'047}{\tt vocals)}
\end{tabbing}\end{minipage}\end{center}
Because {{\tt q}\/} is {{\tt p}\/} and the current value of the
{{\tt instrument}\/} field of {{\tt p}\/} instrument is {{\tt \char'047}{\tt vocals}\/}, the
result is again {{\tt (list}\ {\tt \char'047}{\tt vocals)}\/}.

What we have just seen is the effect of {\sc {sharing}} (the effects of
mutators), which means that a modification of a struture affects the
program in more than one place. Sharing is also visible inside of lists as
our second example shows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}\\
{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt drums)))}
\\
{\tt (b}\={\tt egin}
\\
\>{\tt (set-star-instrument!}\  {\tt (first}\  {\tt q)}\  {\tt \char'047}{\tt vocals)}
\\
\>{\tt (list}\  {\tt (star-instrument}\  {\tt (first}\  {\tt q))))}
\end{tabbing}\end{minipage}\end{center}
Here, the right-hand side of the definition of {{\tt q}\/} is an
expression whose only subexpression isn't a value. More
precisely, it is a structure expression that must be evaluated:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt ...}\  \\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt drums))}
\\
\>{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt p))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set-star-instrument!}\  {\tt (first}\  {\tt q)}\  {\tt \char'047}{\tt vocals)}
\\
\>\>{\tt (list}\  {\tt (star-instrument}\  {\tt (first}\  {\tt q))))}
\\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt drums))}
\\
\>{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt p))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set-star-instrument!}\  {\tt p}\  {\tt \char'047}{\tt vocals)}
\\
\>\>{\tt (list}\  {\tt (star-instrument}\  {\tt (first}\  {\tt q))))}
\end{tabbing}\end{minipage}\end{center}
Thus the first step is to introduce a new definition, for which we chose
{{\tt p}\/} as the name. The second step replaces {{\tt (first}\ {\tt q)}\/} by
{{\tt p}\/}, because {{\tt q}\/} is  a list of one item: {{\tt p}\/}. The rest 
proceeds almost as above: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt ...}\  \\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt vocals))}
\\
\>{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt p))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (}{\sf void}{\tt )}
\\
\>\>{\tt (list}\  {\tt (star-instrument}\  {\tt (first}\  {\tt q))))}
\\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt vocals))}
\\
\>{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt p))}
\\
\>{\tt (list}\  {\tt (star-instrument}\  {\tt p))}
\\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt vocals))}
\\
\>{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt p))}
\\
\>{\tt (list}\  {\tt \char'047}{\tt vocals)}
\end{tabbing}\end{minipage}\end{center}

Finally, effects can be shared among items in different lists. Take a look
at this third variant of our program: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}\\
{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt drums)))}
\\
{\tt (define}\  {\tt r}\  {\tt (list}\  {\tt (first}\  {\tt q)}\  {\tt (star-instrument}\  {\tt (first}\  {\tt q))))}
\\
{\tt (b}\={\tt egin}
\\
\>{\tt (set-star-instrument!}\  {\tt (first}\  {\tt q)}\  {\tt \char'047}{\tt vocals)}
\\
\>{\tt (list}\  {\tt (star-instrument}\  {\tt (first}\  {\tt r))))}
\end{tabbing}\end{minipage}\end{center}
The new definition introduces the variable {{\tt r}\/}, which stands for a
list that contains two items. Let's use our new rules to determine the
values and the effects of this program: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt ...}\  \\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt drums))}
\\
\>{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt p))}
\\
\>{\tt (define}\  {\tt r}\  {\tt (list}\  {\tt (first}\  {\tt q)}\  {\tt (star-instrument}\  {\tt (first}\  {\tt q))))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set-star-instrument!}\  {\tt (first}\  {\tt q)}\  {\tt \char'047}{\tt vocals)}
\\
\>\>{\tt (list}\  {\tt (star-instrument}\  {\tt (first}\  {\tt r))))}
\\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt drums))}
\\
\>{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt p))}
\\
\>{\tt (define}\  {\tt r}\  {\tt (list}\  {\tt p}\  {\tt (star-instrument}\  {\tt p)))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set-star-instrument!}\  {\tt (first}\  {\tt q)}\  {\tt \char'047}{\tt vocals)}
\\
\>\>{\tt (list}\  {\tt (star-instrument}\  {\tt (first}\  {\tt r))))}
\\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt drums))}
\\
\>{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt p))}
\\
\>{\tt (define}\  {\tt r}\  {\tt (list}\  {\tt p}\  {\tt \char'047}{\tt drums))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set-star-instrument!}\  {\tt (first}\  {\tt q)}\  {\tt \char'047}{\tt vocals)}
\\
\>\>{\tt (list}\  {\tt (star-instrument}\  {\tt (first}\  {\tt r))))}
\end{tabbing}\end{minipage}\end{center}
As above, the first step introduces a definition for the new {{\tt star}\/}
structure. The second and third step create the list named {{\tt r}\/},
which contains {{\tt p}\/}, the newly created structure, and
{{\tt \char'047}{\tt vocals}\/}, its current {{\tt instrument}\/} value. 

The next step selects the first item from {{\tt q}\/} and modifies its
{{\tt instrument}\/} field: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt ...}\  \\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt vocals))}
\\
\>{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt p))}
\\
\>{\tt (define}\  {\tt r}\  {\tt (list}\  {\tt p}\  {\tt \char'047}{\tt drums))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (}{\sf void}{\tt )}
\\
\>\>{\tt (list}\  {\tt (star-instrument}\  {\tt (first}\  {\tt r))))}
\\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt vocals))}
\\
\>{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt p))}
\\
\>{\tt (define}\  {\tt r}\  {\tt (list}\  {\tt p}\  {\tt \char'047}{\tt drums))}
\\
\>{\tt (list}\  {\tt (star-instrument}\  {\tt p))}
\\
{\tt =}\  \={\tt (define-struct}\  {\tt star}\  {\tt (name}\  {\tt instrument))}
\\
\>{\tt (define}\  {\tt p}\  {\tt (make-star}\  {\tt \char'047}{\tt PhilCollins}\  {\tt \char'047}{\tt vocals))}
\\
\>{\tt (define}\  {\tt q}\  {\tt (list}\  {\tt p))}
\\
\>{\tt (define}\  {\tt r}\  {\tt (list}\  {\tt p}\  {\tt \char'047}{\tt drums))}
\\
\>{\tt (list}\  {\tt \char'047}{\tt vocals)}
\end{tabbing}\end{minipage}\end{center}
Because {{\tt r}\/} contains {{\tt p}\/} as the first item and because the
instrument field of {{\tt p}\/} is still {{\tt \char'047}{\tt vocals}\/}, the result is 
{{\tt (list}\ {\tt \char'047}{\tt vocals)}\/} here, too. But, this program still has some
knowledge of {{\tt \char'047}{\tt drums}\/}, the original value of the {{\tt star}\/}
structure. 

In summary, mutators give us more power than constructors and
selectors. Instead of just creating new structures and revealing their
contents, we can now change their contents, while the structures remain the
same. Next we must contemplate what this means for the design of programs. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 40.3.1}

 \label{ex:mutate-syn}
Name the mutators that the following structure definitions introduce: 
\begin{enumerate}
\item {{\tt (define-struct}\ {\tt movie}\ {\tt (title}\ {\tt producer))}\/}
\item {{\tt (define-struct}\ {\tt boyfriend}\ {\tt (name}\ {\tt hair}\ {\tt eyes}\ {\tt phone))}\/}
\item {{\tt (define-struct}\ {\tt cheerleader}\ {\tt (name}\ {\tt number))}\/}
\item {{\tt (define-struct}\ {\tt CD}\ {\tt (artist}\ {\tt title}\ {\tt price))}\/}
\item {{\tt (define-struct}\ {\tt sweater}\ {\tt (material}\ {\tt size}\ {\tt producer))}\/}~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mutate-syn.html}{\rule{3pt}{5pt}}
\end{enumerate}


{\bf Exercise 40.3.2}

 \label{ex:mutate-swap}
Develop the function {{\tt swap-posn}\/}, which consumes a {{\tt posn}\/}
structure and swaps the values in the two fields. Its result is
{{\tt (}{\sf void}{\tt )}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mutate-swap.html}{\rule{3pt}{5pt}}


{\bf Exercise 40.3.3}

 \label{ex:mutate-incr}
Develop the function {{\tt one-more-date}\/}, which consumes a
{{\tt girlfriends}\/} structure and increases the contents of the
{{\tt number-past-dates}\/} field by {{\tt 1}\/}. The structure definition is
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt girlfriends}\  {\tt (name}\  {\tt hair}\  {\tt eyes}\  {\tt number-past-dates))}\end{tabbing}\end{minipage}\end{center}
The result of {{\tt one-more-date}\/} is {{\tt (}{\sf void}{\tt )}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mutate-incr.html}{\rule{3pt}{5pt}}


{\bf Exercise 40.3.4}

 \label{ex:mutate-eval1}
Evaluate the following program, step by step: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt cheerleader}\  {\tt (name}\  {\tt number))}\\
{\tt (define}\  {\tt A}\  {\tt (make-cheerleader}\  {\tt \char'047}{\tt JoAnn}\  {\tt 2))}
\\
{\tt (define}\  {\tt B}\  {\tt (make-cheerleader}\  {\tt \char'047}{\tt Belle}\  {\tt 1))}
\\
{\tt (define}\  {\tt C}\  {\tt (make-cheerleader}\  {\tt \char'047}{\tt Krissy}\  {\tt 1))}\  
\\
{\tt (define}\  {\tt all}\  {\tt (list}\  {\tt A}\  {\tt B}\  {\tt C))}
\\
{\tt (l}\={\tt ist}
\\
\>{\tt (cheerleader-number}\  {\tt (second}\  {\tt all))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set-cheerleader-number!}\  {\tt (second}\  {\tt all)}\  {\tt 17)}
\\
\>\>{\tt (cheerleader-number}\  {\tt (second}\  {\tt all))))}
\end{tabbing}\end{minipage}\end{center}
Underline in the last program where definitions differ from the initial
program. 
~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mutate-eval1.html}{\rule{3pt}{5pt}}


{\bf Exercise 40.3.5}

 \label{ex:mutate-eval2}
Evaluate the following program: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt CD}\  {\tt (artist}\  {\tt title}\  {\tt price))}\\
{\tt (d}\={\tt efine}\  {\tt in-stock}
\\
\>{\tt (l}\={\tt ist}
\\
\>\>{\tt (}\={\tt (make-CD}\  {\tt \char'047}{\tt R.E.M}\  {\tt ``New}\  {\tt Adventures}\  {\tt in}\  {\tt Hi-fi''}\  {\tt 0)}
\\
\>\>\>{\tt (make-CD}\  {\tt \char'047}{\tt France}\  {\tt ``simple}\  {\tt je''}\  {\tt 0)}
\\
\>\>\>{\tt (make-CD}\  {\tt \char'047}{\tt Cranberries}\  {\tt ``no}\  {\tt need}\  {\tt to}\  {\tt argue''}\  {\tt 0))))}
\\
{\tt (b}\={\tt egin}
\\
\>{\tt (set-CD-price!}\  {\tt (first}\  {\tt in-stock)}\  {\tt 12)}
\\
\>{\tt (set-CD-price!}\  {\tt (second}\  {\tt in-stock)}\  {\tt 19)}
\\
\>{\tt (set-CD-price!}\  {\tt (third}\  {\tt in-stock)}\  {\tt 11)}
\\
\>{\tt (+}\  \={\tt (CD-price}\  {\tt (first}\  {\tt in-stock))}
\\
\>\>{\tt (CD-price}\  {\tt (second}\  {\tt in-stock))}
\\
\>\>{\tt (CD-price}\  {\tt (third}\  {\tt in-stock))))}
\end{tabbing}\end{minipage}\end{center}
Show every step.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mutate-eval2.html}{\rule{3pt}{5pt}}


\subsection{Mutable Vectors} \label{sec:mutable-vector}

Recall from intermezzo~\ref{sec:int-cost} that vectors, like structures,
are compound values. To extract a value from a structure, programs use
selector operations. To extract a value from a vector, programs use natural
numbers as indices. Hence, functions that process vectors defer to
auxiliary functions that process vectors and natural numbers.

Not surprisingly, vectors, like structures, are mutable compound
values. The only mutator for vectors is {{\tt vector-set!}\/}, a function
that consumes a vector, an index, and a value. Thus, for example, the
following program evaluates to {{\tt \char'047}{\tt blank}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt X}\  {\tt (vector}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b}\  {\tt \char'047}{\tt c}\  {\tt \char'047}{\tt d))}\\
{\tt (b}\={\tt egin}
\\
\>{\tt (vector-set!}\  {\tt X}\  {\tt 0}\  {\tt \char'047}{\tt blank)}
\\
\>{\tt (vector-set!}\  {\tt X}\  {\tt 1}\  {\tt \char'047}{\tt blank)}
\\
\>{\tt (vector-set!}\  {\tt X}\  {\tt 2}\  {\tt \char'047}{\tt blank)}
\\
\>{\tt (vector-set!}\  {\tt X}\  {\tt 3}\  {\tt \char'047}{\tt blank)}
\\
\>{\tt (vector-ref}\  {\tt X}\  {\tt 2))}
\end{tabbing}\end{minipage}\end{center}
The four {{\tt vector-set!}\/} expressions change {{\tt X}\/} so that all of
its four fields contain {{\tt \char'047}{\tt blank}\/}. The last expression extracts the
value of one of the fields.

In general, an evaluation concerning mutable vectors proceeds just like an
evaluation for mutable structures. In particular, a {{\tt vector}\/}
expression introduces a new definition:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (list}\  {\tt (vector}\  {\tt 1}\  {\tt 2}\  {\tt 3))}\\
{\tt =}\  \={\tt (list}\  {\tt v)}
\\
\>\hbox{;; add to top-level definitions: }
\\
\>{\tt (define}\  {\tt v}\  {\tt (vector}\  {\tt 1}\  {\tt 2}\  {\tt 3))}
\end{tabbing}\end{minipage}\end{center}
The variable name {{\tt v}\/} is new and unique. Similarly, a
{{\tt vector-set!}\/}  expression modifies a part of a vector definition:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (set-vector!}\  {\tt (vector}\  {\tt 1}\  {\tt 2}\  {\tt 3)}\  {\tt 0}\  {\tt \char'047}{\tt a)}\\
{\tt =}\  \={\tt (define}\  {\tt v}\  {\tt (vector}\  {\tt 1}\  {\tt 2}\  {\tt 3))}
\\
\>{\tt (set-vector!}\  {\tt v}\  {\tt 0}\  {\tt \char'047}{\tt a)}
\\
{\tt =}\  \={\tt (define}\  {\tt v}\  {\tt (vector}\  {\tt \char'047}{\tt a}\  {\tt 2}\  {\tt 3))}
\\
\>{\tt (}{\sf void}{\tt )}
\end{tabbing}\end{minipage}\end{center}
Finally, effects to vectors are shared just like effects to structures.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}


{\bf Exercise 40.4.1}

 \label{ex:mut-vec-eval1}
Evaluate the following program: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt X}\  {\tt (vector}\  {\tt 0}\  {\tt 0}\  {\tt 0}\  {\tt 0))}\\
{\tt (define}\  {\tt Y}\  {\tt X)}
\\
{\tt (b}\={\tt egin}
\\
\>{\tt (vector-set!}\  {\tt X}\  {\tt 0}\  {\tt 2)}
\\
\>{\tt (vector-set!}\  {\tt Y}\  {\tt 1}\  {\tt (+}\  {\tt (vector-ref}\  {\tt Y}\  {\tt 0)}\  {\tt (vector-ref}\  {\tt Y}\  {\tt 1)))}
\\
\>{\tt (vector-ref}\  {\tt Y}\  {\tt 1))}
\end{tabbing}\end{minipage}\end{center}
Show all steps.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mut-vec-eval1.html}{\rule{3pt}{5pt}}


{\bf Exercise 40.4.2}

 \label{ex:mut-vec-0}
Develop the function {{\tt clear}\/}, which consumes a vector with three
slots and sets them to {{\tt 0}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mut-vec-0.html}{\rule{3pt}{5pt}}


{\bf Exercise 40.4.3}

 \label{ex:mut-vec-1}
Develop the function {{\tt swap}\/}, which consumes a vector with two
slots and swaps the values in these slots.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mut-vec-1.html}{\rule{3pt}{5pt}}


{\bf Exercise 40.4.4}

 \label{ex:mut-vec-2}
Extend the board representation of exercise~\ref{ex:vector-chess} with the
function 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt board-flip!}\ {\tt :}\ {\tt board}\ {\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to negate the board position with indices {{\tt i}\/}, {{\tt j}\/} on {{\tt a-board}\/}}
\\
{\tt (define}\  {\tt (board-set!}\  {\tt a-board}\  {\tt i}\  {\tt j)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Don't forget to develop examples and tests for the function.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mut-vec-2.html}{\rule{3pt}{5pt}}



\subsection{Changing Variables, Changing Structures}

Structure mutators and {{\bf set!}-exp\-res\-sion}s are related. Indeed, in
section~\ref{sec:imp-struct-func} we explained the effects of the first
with the second. Still, there are also important differences that a
programmer must understand. Let's start with the syntax: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set!}\  \hbox{$\mbox{{\tt <{variable}>}}$}\  \hbox{$\mbox{{\tt <{expression}>}}$}{\tt )}\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$\mbox{\sf set}$}{\tt -}\hbox{$\mbox{{\tt <{structure-tag}>}}$}{\tt -}\hbox{$\mbox{{\tt <{field}>}}$}\hbox{${\sf!}$}\end{tabbing}\end{minipage}
\end{center}
A {{\bf set!}-exp\-res\-sion} is an expression that consists of two pieces: a variable and
an expression. The variable is fixed; it is never evaluated. The expression
is evaluated. In contrast, a structure mutator is a function. As such, it
is a value that the program can apply (to two arguments), pass to other
functions, store inside of structures, and so on. Structure mutators are
created in response to structure definitions, just as structure
constructors and selectors.

Next we must consider lexical scope issues (see section~\ref{sec:scope}).
A {\bf set!}-exp\-res\-sion\ contains a variable. For the {\bf set!}-exp\-res\-sion\ to be valid, this
variable must be bound. The connection between a {\bf set!}-exp\-res\-sion's variable and
its binding occurrence is static and can never be changed.

The scope of a mutator is that of its corresponding {{\tt define-struct}\/}. Thus, 
in the following program 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt aaa}\  {\tt (xx}\  {\tt yy))}\\
{\tt (d}\={\tt efine}\  {\tt UNIQUE}\  
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\hbox{$\underline{\mbox{{{\tt (define-struct}\ {\tt aaa}\ {\tt (xx}\ {\tt yy))}\/}}}$}{\tt )}
\\
\>\>{\tt (make-aaa}\  {\tt \char'047}{\tt my}\  {\tt \char'047}{\tt world)))}
\\
{\tt ...}
\end{tabbing}\end{minipage}\end{center}
the underlined occurrence of {{\tt define-struct}\/} has a limited lexical
scope, and its scope is a hole in the scope of the top-level
{{\tt define-struct}\/}. A result of this scoping is that the mutator for
the top-level {{\tt define-struct}\/} cannot mutate the structure called
{{\tt UNIQUE}\/}. The two mutators are unrelated functions that
coincidentally have the same name; the rules for the evaluation of
{\bf local}-exp\-res\-sion\ dictate that we rename one consistently. 

To highlight the differences in syntax and lexical scope, take a look at
the following two, apparently similar programs: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt the-point}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}\\
\relax
\\
{\tt (set!}\  {\tt x}\  {\tt 17)}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt the-point}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}\\
\relax
\\
{\tt (set-posn-x!}\  {\tt the-point}\  {\tt 17)}
\end{tabbing}\end{minipage}
\end{center}
The one on the left is illegal, because the {{\tt x}\/} in the {\bf set!}-exp\-res\-sion\ is
an unbound variable. The program on the right is perfectly legal; it refers
to the field {{\tt x}\/} of a {{\tt posn}\/} structure. 

The largest difference between {{\bf set!}-exp\-res\-sion}s and mutators concerns their
semantics. Let's study two examples to understand the differences once and
for all. The first illustrates how similar looking expressions evaluate in
a radically different manner:
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt the-point}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}\\
\relax
\\
{\tt (set!}\  {\tt the-point}\  {\tt 17)}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt the-point}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}\\
\relax
\\
{\tt (set-posn-x!}\  {\tt the-point}\  {\tt 17)}
\end{tabbing}\end{minipage}
\end{center}
The program on the left consists of a definition for {{\tt the-point}\/} and
an assignment to {{\tt the-point}\/}; the one on the right starts with the
same definition for {{\tt the-point}\/} followed by an application of the
mutator. The evaluation of both affect the variable definition but in
different ways: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt the-point}\  {\tt 17)}\\
\relax
\\
{\tt (}{\sf void}{\tt )}
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt the-point}\  {\tt (make-posn}\  {\tt 17}\  {\tt 4))}\\
\relax
\\
{\tt (}{\sf void}{\tt )}
\end{tabbing}\end{minipage}
\end{center}
On the left, {{\tt the-point}\/} now stands for a number; on the right, it
is still a {{\tt posn}\/} structure but with a new value in the
{{\tt x}\/}-field. More generally, a {\bf set!}-exp\-res\-sion\ changes the value on the
right-hand side of a definition, and the application of a mutator changes
the value of just one field in a structure that occurs on the right-hand
side of a definition. 

The second example shows how an application of mutator evaluates the
arguments, which is not the case for {{\bf set!}-exp\-res\-sion}s: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt the-point}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}\\
{\tt (define}\  {\tt an-other}\  {\tt (make-posn}\  {\tt 12}\  {\tt 5))}
\\
\relax
\\
{\tt (set!}\  \={\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (zero?}\  {\tt (point-x}\  {\tt the-point))}
\\
\>\>\>{\tt the-point\char'135}
\\
\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>{\tt an-other\char'135}{\tt )}
\\
\>{\tt 1)}
\\
\relax
\end{tabbing}\end{minipage}\hfill\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt the-point}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}\\
{\tt (define}\  {\tt an-other}\  {\tt (make-posn}\  {\tt 12}\  {\tt 5))}
\\
\relax
\\
{\tt (s}\={\tt et-posn-x!}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (zero?}\  {\tt (point-x}\  {\tt the-point))}
\\
\>\>\>{\tt the-point\char'135}
\\
\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>{\tt an-other\char'135}{\tt )}
\\
\>{\tt 1)}
\end{tabbing}\end{minipage}
\end{center}
Whereas the program on the left is illegal, because a {\bf set!}-exp\-res\-sion\ must
contain a fixed variable in the second position, the one on the right is
legitimate. The evaluation of the program on the right changes the
{{\tt x}\/}-field in {{\tt an-other}\/} to {{\tt 1}\/}.

Finally, {{\tt mutators}\/} are values, which means a function can consume a
mutator and apply it: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt set-to-2}\ {\tt :}\ {\tt S-mutator}\ {\tt S-structure}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; to change a field in {{\tt s}\/} to {{\tt 2}\/} via {{\tt mutator}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (set-to-2}\  {\tt mutator}\  {\tt s)}
\\
\>{\tt (mutator}\  {\tt s}\  {\tt 2))}
\\
{\tt (define-struct}\  {\tt bbb}\  {\tt (zz}\  {\tt ww))}
\\
{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt s}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}
\\
\>\>{\tt (define}\  {\tt t}\  {\tt (make-bbb}\  {\tt \char'047}{\tt a}\  {\tt \char'047}{\tt b)))}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set-to-2}\  {\tt set-posn-x!}\  {\tt s)}
\\
\>\>{\tt (set-to-2}\  {\tt set-bbb-ww!}\  {\tt t)))}
\end{tabbing}\end{minipage}\end{center}
The function {{\tt set-to-2}\/} consumes a mutator and a structure that the
mutator can modify. The program uses it to change the {{\tt x}\/}-field in a
{{\tt posn}\/} structure and the {{\tt ww}\/}-field in a {{\tt bbb}\/}
structure. In contrast, if we were to apply a function to a {\bf set!}-exp\-res\-sion, it
would receive {{\tt (}{\sf void}{\tt )}\/} and nothing else. 

\medskip\noindent{\bf Mixing set! and Structure Mutators}:\  \label{pg:note-set-vs-set} When
a program uses both {{\bf set!}-exp\-res\-sion}s and structure mutators, our evaluation
rules fail for some cases. Specifically, they don't explain sharing
properly. Consider this program fragment:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt the-point}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}\\
{\tt (define}\  {\tt another-point}\  {\tt the-point)}
\\
{\tt (b}\={\tt egin}
\\
\>{\tt (set!}\  {\tt the-point}\  {\tt 17)}
\\
\>{\tt (=}\  {\tt (posn-x}\  {\tt another-point)}\  {\tt 3))}
\end{tabbing}\end{minipage}\end{center}
According to our rules, the two definitions refer to the same
structure. The second one does so by indirection. The {\bf set!}-exp\-res\-sion changes what
{{\tt the-point}\/} stands for, but it shouldn't affect the second
definition. In particular, the program should produce {{\tt true}\/}. 
If we were to use our rules in a naive manner, we would not be able to
validate this point. 

A proper explanation of structures must introduce a new definition for
every application of a structure constructor, including those on the
right-hand side of definitions in the original program. We will place the
new definitions at the beginning of the sequence of
definitions. Furthermore, the variable in the new definition must be unique
so that it cannot occur in a {\bf set!}-exp\-res\-sion. We will use variables such as
{{\tt struct-1}\/}, {{\tt struct-2}\/}, and so on and agree to use them for
this purpose only. These names, and only these names, are values.

Using the minor changes to our rules, we can evaluate the program fragment
properly: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  \=\hbox{;; evaluate from here: }\\
\>{\tt (define}\  {\tt the-point}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}
\\
\>{\tt (define}\  {\tt another-point}\  {\tt the-point)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt the-point}\  {\tt 17)}
\\
\>\>{\tt (=}\  {\tt (posn-x}\  {\tt another-point)}\  {\tt 3))}
\\
{\tt =}\  \={\tt (define}\  {\tt struct-1}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}
\\
\>{\tt (define}\  {\tt the-point}\  {\tt struct-1)}
\\
\>\hbox{;; evaluate from here: }
\\
\>{\tt (define}\  {\tt another-point}\  {\tt the-point)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt the-point}\  {\tt 17)}
\\
\>\>{\tt (=}\  {\tt (posn-x}\  {\tt another-point)}\  {\tt 3))}
\\
{\tt =}\  \={\tt (define}\  {\tt struct-1}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}
\\
\>{\tt (define}\  {\tt the-point}\  {\tt struct-1)}
\\
\>{\tt (define}\  {\tt another-point}\  {\tt struct-1)}
\\
\>\hbox{;; evaluate from here: }
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set!}\  {\tt the-point}\  {\tt 17)}
\\
\>\>{\tt (=}\  {\tt (posn-x}\  {\tt another-point)}\  {\tt 3))}
\end{tabbing}\end{minipage}\end{center}
At this point, the structure is created, and both of the original variables
refer to the new structure. The rest of the evaluation changes the
definition of {{\tt the-point}\/} but not {{\tt another-point}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt ...}\\
{\tt =}\  \={\tt (define}\  {\tt struct-1}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}
\\
\>{\tt (define}\  {\tt the-point}\  {\tt 17)}
\\
\>{\tt (define}\  {\tt another-point}\  {\tt struct-1)}
\\
\>\hbox{;; evaluate from here: }
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (}{\sf void}{\tt )}
\\
\>\>{\tt (=}\  {\tt (posn-x}\  {\tt another-point)}\  {\tt 3))}
\\
{\tt =}\  \={\tt (define}\  {\tt struct-1}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}
\\
\>{\tt (define}\  {\tt the-point}\  {\tt 17)}
\\
\>{\tt (define}\  {\tt another-point}\  {\tt struct-1)}
\\
\>\hbox{;; evaluate from here: }
\\
\>{\tt (=}\  {\tt (posn-x}\  {\tt another-point)}\  {\tt 3)}
\\
{\tt =}\  \={\tt (define}\  {\tt struct-1}\  {\tt (make-posn}\  {\tt 3}\  {\tt 4))}
\\
\>{\tt (define}\  {\tt the-point}\  {\tt 17)}
\\
\>{\tt (define}\  {\tt another-point}\  {\tt struct-1)}
\\
\>\hbox{;; evaluate from here: }
\\
\>{\tt (=}\  {\tt 3}\  {\tt 3)}
\end{tabbing}\end{minipage}\end{center}
The final result is {{\tt true}\/}, as expected. 

The modified evaluation rules are a bit more cumbersome than the old
ones. But, they fully explain the difference between the effects of
{{\bf set!}-exp\-res\-sion}s and those of structure mutation, which for programming in
modern languages, is an essential concept.~{\rule{3pt}{5pt}}

\section{Designing Functions that Change Structures} \label{sec:design-mut}

Sections~\ref{sec:encapsulate} and~\ref{sec:mutable} gradually introduced
the idea of mutable structures. In the first of the two sections we studied
the idea of changing a {{\tt local}\/}ly defined variable through a
function. In the second one, we discussed how structures could be modified,
too. 

Now we need to learn when and how to use this new power.  The first
subsection concerns the question why a program should modify a structure.
The second reviews how the existing design recipes apply when we wish to
use mutators. The third one discusses some difficult cases. The last one is
dedicated to the differences between {{\tt set!}\/} and structure mutators.

\subsection{Why Mutate Structures} \label{sec:design-mut-why}

Whenever we apply a structure constructor, we create a new structure. On
some occasions, this is truly what we want. Consider a function that
consumes a list of personnel records and produces a list of phone book
entries. The personnel records may contain information about a person's
address, including the phone number, date of birth, marital status, closest
relatives, and salary level. The entry for the phone book should contain
the name and the phone number and nothing else. This kind of program should
definitely generate a new structure from each structure in the given list. 

On other occasions, though, creating a new structure doesn't correspond to
our intuition. Suppose we wish to give someone a raise. The only way of
accomplishing this at the moment is to create a new personnel record that
contains all the old information and the new salary information. Or,
suppose someone has moved and received a new phone number, and we wish to
update the phone book on our PDA. Just like the program that changes a
person's salary level, the program that updates the phone book would create
a new phone book entry. In reality, however, we would not create a new
personnel record or a new entry in the phone book. We would instead correct
the existing personnel record and the existing entry in our phone book. A
program should be able to perform the same kind of corrective action, and
with mutators, we can indeed develop such programs.

Roughly speaking, the examples suggest two distinct cases. First, if a
structure corresponds to a physical object and the computation corresponds
to a corrective action, then the program may mutate the structure. Second,
if a structure does not correspond to a physical object or if the
computation creates a new kind of value from existing information, then the
program should create a new structure. These two rules are not
clear-cut. We will often encounter situations where both solutions are
feasible. In that case, we should consider an ease-of-programming
argument. If one of the two solutions is easier to program, choose it. If
we discover later that it is a performance bottleneck, we may have to
change our mind. 

\subsection{Structural Design Recipes and Mutation, Part 1} \label{sec:design-mut-ex1}

Surprisingly programming with mutators does not require any new design
recipes---as long as the mutated fields always contain atomic values. Our
receipes work perfectly fine.  While the design of non-mutating programs
requires the combination of values, programming with mutators requires the
combination of effects. Hence, the key is to add a well-formulated effect
statement to a function's contract and to make up examples that illustrate
the effects. We practiced both of these activities for {{\tt set!}\/}
expressions already in section~\ref{sec:design-memory}. In this section we
learn to adapt the design recipes and effect statements to structure and
vector mutations. To do that, we consider a short series of examples.  Each
illustrates how an applicable design recipe helps with the design of
structure-modifying or vector-modifying functions.

The first example concerns the mutation of plain structures. Suppose we are
given a structure and a data definition for personnel records:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt personnel}\  {\tt (name}\  {\tt address}\  {\tt salary))}\\
\hbox{;; A personnel record ({\sl {PR}}) is a structure: }
\\
\hbox{;; {{\tt (make-personnel}\ {\tt n}\ {\tt a}\ {\tt s)}\/}}
\\
\hbox{;; where {{\tt n}\/} is a symbol, {{\tt a}\/} is a string, and {{\tt s}\/} is a number. }
\end{tabbing}\end{minipage}\end{center}
A function that consumes such a record is based on the following template: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (fun-for-personnel}\  {\tt pr)}\\
\>{\tt ...}\  {\tt (personnel-name}\  {\tt pr)}\  {\tt ...}
\\
\>{\tt ...}\  {\tt (personnel-address}\  {\tt pr)}\  {\tt ...}
\\
\>{\tt ...}\  {\tt (personnel-salary}\  {\tt pr)}\  {\tt ...)}\  \  
\end{tabbing}\end{minipage}\end{center}

Consider a function for increasing the salary field:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt increase-salary}\ {\tt :}\ {\tt PR}\ {\tt number}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to modify the salary field of {{\tt a-pr}\/} by adding {{\tt a-raise}\/}}
\\
{\tt (define}\  {\tt (increase-salary}\  {\tt a-pr}\  {\tt a-raise)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
The contract specifies that the function consumes a {{\tt PR}\/} and a
number. The purpose statement is an effect statement, which explains how
the argument of {{\tt increase-salary}\/} is modified. 

Developing examples for {{\tt increase-salary}\/} requires the techniques of
section~\ref{sec:design-memory}. Specifcially, we must be able to compare
the before and after state of some {{\tt PR}\/} structure: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt pr1}\  {\tt (make-personnel}\  {\tt \char'047}{\tt Bob}\  {\tt \char'047}{\tt Pittsburgh}\  {\tt 70000)))}\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (increase-salary}\  {\tt pr1}\  {\tt 10000)}
\\
\>\>{\tt (=}\  {\tt (personnel-salary}\  {\tt pr1)}\  {\tt 80000)))}
\end{tabbing}\end{minipage}\end{center}
The result of the expression is {{\tt true}\/} if, and only if,
{{\tt increase-salary}\/} works properly for this example. 

We can now use the template and the example to define the function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt increase-salary}\ {\tt :}\ {\tt PR}\ {\tt number}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to modify the salary field of {{\tt a-pr}\/} by adding in {{\tt a-raise}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (increase-salary}\  {\tt a-pr}\  {\tt a-raise)}
\\
\>{\tt (set-personnel-salary!}\  {\tt a-pr}\  {\tt (+}\  {\tt (personnel-salary}\  {\tt a-pr)}\  {\tt a-raise)))}
\end{tabbing}\end{minipage}\end{center}
As usual, the full definition uses only one of several subexpressions from
the template, but the template reminds us of what information we can use:
the arguments and their pieces, and what parts we can modify: the fields
for which we have selectors. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 41.2.1}

 \label{ex:mut-struct0}
Make up examples for {{\tt increase-salary}\/} and test the
function. Formulate the tests as boolean-valued expressions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mut-struct0.html}{\rule{3pt}{5pt}} 


{\bf Exercise 41.2.2}

 \label{ex:mut-struct1}
Adapt {{\tt increase-salary}\/} such that it accepts only values for
{{\tt a-raise}\/} between 3\% and 7\% of the salary. It calls {{\tt error}\/}
otherwise.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mut-struct1.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.3}

 \label{ex:mut-struct2}
Develop {{\tt increase-percentage}\/}. The function consumes a {{\tt PR}\/}
and a percentage between 3\% and 7\%. It increases the value in the salary
field of the {{\tt PR}\/} by the lesser of the percentage increase or
{{\tt 7000}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mut-struct2.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.4}

 \label{ex:mut-struct3}
Develop the function {{\tt new-date}\/}. It consumes a {{\tt cheerleader}\/}
record and adds a date to the beginning of a list. Here are the relevant
definitions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt cheerleader}\  {\tt (name}\  {\tt dates))}\\
\hbox{;; A {\sl {cheerleader}} is a structure: }
\\
\hbox{;; {{\tt (make-cheerleader}\ {\tt n}\ {\tt d)}\/}}
\\
\hbox{;; where {{\tt n}\/} is a symbol and {{\tt d}\/} is a list of symbols. }
\end{tabbing}\end{minipage}\end{center}
For example, {{\tt (make-cheerleader}\ {\tt \char'047}{\tt JoAnn}\ {\tt \char'047}{\tt (Carl}\ {\tt Bob}\ {\tt Dude}\ {\tt Adam}\ {\tt Emil))}\/}
is a valid {{\tt cheerleader}\/} record. Develop an example that shows what 
it means to add {{\tt \char'047}{\tt Frank}\/} as a date.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mut-struct3.html}{\rule{3pt}{5pt}}



{\bf Exercise 41.2.5}

 \label{ex:mut-struct4}
Recall the structure definitions for {{\it square\/}\/}s: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\it square\/}\  {\tt (nw}\  {\it length\/}{\tt ))}\end{tabbing}\end{minipage}\end{center}
The matching data definition specifies that the {{\tt nw}\/} field is always
a {{\tt posn}\/} structure and that {{\it length\/}\/} is a number: 

  \begin{quote} A {\sl {square}} is a structure: \\
          \centerline{{{\tt (make-square}\ {\tt p}\ {\tt s)}\/}}       where {{\tt p}\/} is a {{\tt posn}\/} and {{\tt s}\/} is a number. \end{quote}

\noindent We started working with {{\it square\/}\/}s in
part~\ref{part:basic}. 

Develop the function {{\tt move-square!}\/}. It consume a square, called
{{\tt sq}\/}, and a number, called {{\tt delta}\/}. It modifies {{\tt sq}\/}
by adding {{\tt delta}\/} to its $x$-coordinate.

Look up the structure and data definition for circles and develop the
function {{\tt move-circle}\/}, which is analogous to
{{\tt move-square}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mut-struct4.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


The second example recalls the design recipe for functions that work on
unions of classes. One of our first examples of this kind concerned the
class of geometric shapes. Here is a data definition that merges squares
and circles: 

  \begin{quote} A {\sl {shape}} is either       \begin{enumerate}         \item a {{\tt circle}\/}, or          \item a {{\it square\/}\/}.       \end{enumerate} \end{quote}

\noindent See exercise~\ref{ex:mut-struct4} or part~\ref{part:basic} for
the definitions of {{\tt circle}\/} and {{\it square\/}\/}. 

Following our recipe, a template for {{\tt shape}\/}-processing functions
consists of a two-clause {{\bf cond}-exp\-res\-sion}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (fun-for-shape}\  {\tt a-shape)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (circle?}\  {\tt a-shape)}\  {\tt ...}\  {\tt (fun-for-circle}\  {\tt a-shape)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt (square?}\  {\tt a-shape)}\  {\tt ...}\  {\tt (fun-for-square}\  {\tt a-shape)}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
Each {{\tt cond}\/}-clause refers to a function with the same purpose for
the matching kind of shape. 

So, suppose we wish to move a {{\tt shape}\/} in the {\it x\/}-direction by
a fixed number of pixels. In part~\ref{part:basic}, we created a new
structure for this puropose. Now we can use the mutators for
{{\tt circle}\/} and {{\it square\/}\/} structures instead, that is, the
function can have an effect:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt move-shape!}\ {\tt :}\ {\tt shape}\ {\tt number}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to move {{\tt a-shape}\/} in the {\it x\/} direction by {{\tt delta}\/} pixels}
\\
{\tt (d}\={\tt efine}\  {\tt (move-shape!}\  {\tt a-shape)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (circle?}\  {\tt a-shape)}\  {\tt (move-circle}\  {\tt a-shape}\  {\tt delta)\char'135}
\\
\>\>{\tt \char'133}{\tt (square?}\  {\tt a-shape)}\  {\tt (move-square}\  {\tt a-shape}\  {\tt delta)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The functions {{\tt move-circle}\/} and {{\tt move-square}\/} are the subject
of execise~\ref{ex:mut-struct4} because they consume and affect plain
structures.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 41.2.6}

 \label{ex:mut-rec0}
Make up examples for {{\tt move-shape!}\/} and test the function. Formulate
the tests as boolean-valued expressions!~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mut-rec0.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.7}

 \label{ex:mut-rec2}
The following structure definitions are to represent items that a music
store sells: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt cd}\  {\tt (price}\  {\tt title}\  {\tt artist))}\\
{\tt (define-struct}\  {\tt record}\  {\tt (price}\  {\tt antique}\  {\tt title}\  {\tt artist))}
\\
{\tt (define-struct}\  {\tt dvd}\  {\tt (price}\  {\tt title}\  {\tt artist}\  {\tt to-appear))}
\\
{\tt (define-struct}\  {\tt tape}\  {\tt (price}\  {\tt title}\  {\tt artist))}
\end{tabbing}\end{minipage}\end{center}

Provide a data definition for the class of {\sl {music item}}s, which
comprises {{\tt cd}\/}s, {{\tt record}\/}s, {{\tt dvd}\/}s, and
{{\tt tape}\/}s. The price must be a number in each case.

Develop the program {{\tt inflate!}\/}, which consumes a {{\tt music-item}\/}
and a percentage. Its effect is to increase the price in the given
structure according to the percentage.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mut-rec2.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.8}

 \label{ex:mut-rec1}
Develop a program that keeps track of the feeding of zoo animals.  Our zoo
has three kinds of animals: elephants, monkeys, and spiders.  Each animal
has a name and two feeding times per day: morning and evening. Initially a
structure that represents an animal (structure) contains {{\tt false}\/} in
the fields for feeding times. The program {{\tt feed-animal}\/} should
consume a structure that represents an animal and the name of a feeding
time. It should switch the corresponding field in the animal structure to
{{\tt true}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mut-rec1.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


The next two examples are about mutations when the underlying data
definitions involve self-references. Self-references are needed if we wish
to deal with data that has no size limit. Lists were the first class of
such data we encountered and natural numbers the second one.

Let's first take a look at mutation of lists of structures, using the
running data example of this part: the address book. An address book is a
list of entries; for completeness, here are the structure and data
definitions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt entry}\  {\tt (name}\  {\tt number))}\end{tabbing}\end{minipage}\end{center}

\begin{quote} An {\sl {entry}} is a structure: \\
   \centerline{{{\tt (make-entry}\ {\tt n}\ {\tt p)}\/}}  where {{\tt n}\/} is a symbol and {{\tt p}\/} is a number. \end{quote}

\begin{quote} An {\sl {address book}} is      \begin{enumerate}       \item the empty list, {{\tt empty}\/}, or       \item {{\tt (cons}\ {\tt an-e}\ {\tt an-ab)}\/} where {{\tt an-e}\/} is an entry and          {{\tt an-ab}\/} is an address book.        \end{enumerate} \end{quote}

\noindent Only the second one is self-referential, so we focus on the
template for it:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt fun-for-ab}\ {\tt :}\ {\tt address-book}\ {}{\tt ->}{}\ {\tt XYZ}\/}}\\
{\tt (d}\={\tt efine}\  {\tt (fun-for-ab}\  {\tt ab)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt ab)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (fun-for-entry}\  {\tt (first}\  {\tt ab))}\  {\tt ...}\  {\tt (fun-for-ab}\  {\tt (rest}\  {\tt ab))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
If we needed an auxiliary function for processing an {{\tt entry}\/}, we
might also wish to write out the template for structure-processing
functions. 


So suppose we want a function that updates an existing entry. The function
consumes an {{\tt address-book}\/}, a name, and a phone number. The first
{{\tt entry}\/} that contains the name is modified to contain the new phone
number: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt change-number!}\ {\tt :}\ {\tt symbol}\ {\tt number}\ {\tt address-book}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to modify the first {{\tt entry}\/} for {{\tt name}\/} in {{\tt ab}\/} so that its}
\\
\hbox{;; number field is {{\tt phone}\/}}
\\
{\tt (define}\  {\tt (change-number!}\  {\tt name}\  {\tt phone}\  {\tt ab)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
It is justfied to develop this function with mutators because just as in
reality, most of the address book stays the same while one entry is
changed. 

Here is an example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt ab}\\
\>{\tt (l}\={\tt ist}
\\
\>\>{\tt (make-entry}\  {\tt \char'047}{\tt Adam}\  {\tt 1)}
\\
\>\>{\tt (make-entry}\  {\tt \char'047}{\tt Chris}\  {\tt 3)}
\\
\>\>{\tt (make-entry}\  {\tt \char'047}{\tt Eve}\  {\tt 2)))}
\\
{\tt (b}\={\tt egin}
\\
\>{\tt (change-number!}\  {\tt \char'047}{\tt Chris}\  {\tt 17}\  {\tt ab)}
\\
\>{\tt (=}\  {\tt (entry-number}\  {\tt (second}\  {\tt ab))}\  {\tt 17))}
\end{tabbing}\end{minipage}\end{center}
The definition introduces {{\tt ab}\/}, an {{\tt address-book}\/} with three
items.  The {{\bf begin}-exp\-res\-sion} first changes {{\tt ab}\/} by
associating {{\tt \char'047}{\tt Chris}\/} with {{\tt 17}\/}; then it compares the phone
number of the second item on {{\tt ab}\/} with {{\tt 17}\/}. If
{{\tt change-number!}\/} functions properly, the result of the
{{\bf begin}-exp\-res\-sion} is {{\tt true}\/}. An even better test would ensure that nothing
else in {{\tt ab}\/} changes. 

The next step is to develop the function definition, using the template and
the examples. Let's consider each case separately:
\begin{enumerate}
\item If {{\tt ab}\/} is empty, {{\tt name}\/} doesn't occur in it. Unfortunately, 
the purpose statement doesn't specify what the function should compute in this 
case, and there is indeed nothing sensible the function can do. To be safe,
we use {{\tt error}\/} to signal that no matching entry was found. 

\item If {{\tt ab}\/} contains a first {{\tt entry}\/}, it might or might not
contain {{\tt name}\/}. To find out, the function must distinguish the two 
cases with a {{\bf cond}-exp\-res\-sion}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (symbol=?}\  {\tt (entry-name}\  {\tt (first}\  {\tt ab))}\  {\tt name)}\  {\tt ...\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt ...\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
In the first subcase, the function must modify the structure. In the
second, {{\tt name}\/} can occur only in {{\tt (rest}\ {\tt ab)}\/}, which means the
function must mutate some {{\tt entry}\/} in the rest of the
list. Fortunately, the natural recursion accomplishes just that. 
\end{enumerate}

Putting everything together, we get the following definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (change-number!}\  {\tt name}\  {\tt phone}\  {\tt ab)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt ab)}\  {\tt (error}\  {\tt \char'047}{\tt change-number!}\  {\tt ``name}\  {\tt not}\  {\tt in}\  {\tt list'')\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt (entry-name}\  {\tt (first}\  {\tt ab))}\  {\tt name)}
\\
\>\>\>\>{\tt (set-entry-number!}\  {\tt (first}\  {\tt ab)}\  {\tt phone)\char'135}
\\
\>\>\>{\tt \char'133}\={\tt else}\  
\\
\>\>\>\>{\tt (change-number!}\  {\tt name}\  {\tt phone}\  {\tt (rest}\  {\tt ab))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The only unique aspect of this function is that it uses a structure mutator
in one of the cases. Otherwise it has the familiar recursive shape: a
{{\tt cond}\/} with two clauses and a natural recursion. It is especially
instructive to compare the function with {{\tt contains-doll\char'077}\/} from
section~\ref{sec:lists:process} and {{\tt contains\char'077}\/} from
exercise~\ref{ex:containsp}.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 41.2.9}

 \label{ex:rec0}
Define {{\tt test-change-number}\/}.  The function consumes a name, a phone
number, and an address book. It uses {{\tt change-number!}\/} to update the
address book, and then ensures that it was changed properly. If so, it produces
{{\tt true}\/}; if not, it produces an error message. Use this new function to
test {{\tt change-number!}\/} with at least three different
examples.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/rec0.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.10}

 \label{ex:rec1}
Develop {{\tt move-squares}\/}. It consumes a list of {{\it square\/}\/}s, as
defined above, and a number {{\tt delta}\/}. The function modifies each on
the list by adding {{\tt delta}\/} to the {\it x\/}-component of its
position.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/rec1.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.11}

 \label{ex:rec3}
Develop the function {{\tt all-fed}\/}. It consumes a list of
{{\tt animal}\/}s, as defined in exercise~\ref{ex:mut-rec1}, and modifies
them so that their field for morning feedings is switched to
{{\tt true}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/rec3.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.12}

 \label{ex:abs-list}
Develop the function {{\tt for-all}\/}, which abstracts
{{\tt move-squares}\/} and {{\tt all-fed}\/} from exercises~\ref{ex:rec1}
and~\ref{ex:rec3}.  It consumes two values: a function that consumes
structures and produces {{\tt (}{\sf void}{\tt )}\/}; and a list of structures. Its
result is {{\tt (}{\sf void}{\tt )}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/abs-list.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.13}

 \label{ex:rec4}
Develop the function {{\tt ft-descendants}\/}. It consumes a descendant
family tree (see section~\ref{sec:mut-ref-dd}) based on the following
structure definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt parent}\  {\tt (children}\  {\tt name}\  {\tt date}\  {\tt eyes}\  {\tt no-descendants))}\end{tabbing}\end{minipage}\end{center}
The last field in a {{\tt parent}\/} structure is originally {{\tt 0}\/}. 
The function {{\tt ft-descendants}\/} traverses the tree and modifies these
slots so that they contain the total number of descendants of the
corresponding family member. Its result is the number of total descendants
of the given tree.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/rec4.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Natural numbers make up another class of values that requires a
self-referential descriptions. Recursion on natural numbers {\em per se\/}
isn't useful in conjunction with mutation, but recursion on natural numbers
as indices into vectors is useful when a problem's data representation
involves vectors.

Let's start with a snipet of an elevator control program. An elevator
control program must know at which floor people have pressed the call
buttons. We assume that the elevator hardware can mutate some status vector
of booleans. That is, we assume that the program contains a vector, call it
{{\tt call-status}\/}, and that a field in {{\tt call-status}\/} is
{{\tt true}\/} if someone has pushed the call button at the corresponding
floor.

One important elevator operation is to {{\tt reset}\/} all the buttons. For
example, an operator may have to restart the elevator after it has been out
of service for a while. We start the development of {{\tt reset}\/} by
re-stating the known facts in a Scheme outline:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt call-status}\ {\tt :}\ {\tt (vectorof}\ {\tt boolean)}\/}}\\
\hbox{;; to keep track of the floors from which calls have been issued }
\\
{\tt (define}\  {\tt call-status}\  {\tt (vector}\  {\sf true}\  {\sf true}\  {\sf true}\  {\sf false}\  {\sf true}\  {\sf true}\  {\sf true}\  {\sf false}{\tt ))}
\\
\hbox{;; {{\tt reset}\ {\tt :}\ {}{\tt ->}{}\ {\tt true}\/}}
\\
\hbox{;; effect: to set all fields in {{\tt call-status}\/} to {{\tt false}\/}}
\\
{\tt (define}\  {\tt (reset)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
The first definition specifies {{\tt call-status}\/} as a state variable but
of course we use each slot in the vector as a state value not the entire
variable.  The second part consists of three pieces: a contract, an effect
statement, and a header for the function {{\tt reset}\/}, which implements
the informally specified service. 

While it is possible to implement the service as
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (reset)}\\
\>{\tt (s}\={\tt et!}\  {\tt call-status}
\\
\>\>{\tt (build-vector}\  {\tt (vector-length}\  {\tt call-status)}\  {\tt (lambda}\  {\tt (i)}\  {\tt false))))}
\end{tabbing}\end{minipage}\end{center} 
this trivial solution is clearly not what we want. Instead, we want a
function that modifies each field of the vector. Following the suggestions
of intermezzo~5, we develop an auxiliary function with the following
template:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reset-aux}\ {\tt :}\ {\tt (vectorof}\ {\tt boolean)}\ {\bf N}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to set the fields of {{\tt v}\/} with index in [{{\tt 0}\/}, {{\tt i}\/}) to {{\tt false}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (reset-aux}\  {\tt v}\  {\tt i)}\  
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt i)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (reset-aux}\  {\tt v}\  {\tt (sub1}\  {\tt i))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
That is, the auxiliary function not only consumes the vector but also an
interval bound. The shape of the template is based on the data definition
of the latter. 

The effect statement suggests the following examples:
\begin{enumerate}
\item {{\tt (reset-aux}\ {\tt call-status}\ {\tt 0)}\/} leaves {{\tt call-status}\/}
unchanged, because the purpose statement says to change all indices in
[{{\tt 0}\/},{{\tt 0}\/}) and there are none;

\item {{\tt (reset-aux}\ {\tt 1)}\/} changes {{\tt call-status}\/} so that
{{\tt (vector-ref}\ {\tt call-status}\ {\tt 0)}\/} is {{\tt false}\/}, because {{\tt 0}\/}
is the only natural number in [{{\tt 0}\/}, {{\tt 1}\/}); 

\item {{\tt (reset-aux}\ {\tt call-status}\ {\tt (vector-length}\ {\tt call-status))}\/} sets all
fields of {{\tt call-status}\/} to {{\tt false}\/}.
\end{enumerate}
The last example implies that we can define {{\tt reset}\/} with
{{\tt (reset-aux}\ {\tt call-status}\ {\tt (vector-length}\ {\tt call-status))}\/}. 

Equipped with examples, we can turn our attention to the definition.  The
key is to remember that the additional argument must be interpreted as an
index into the vector. Keeping the example and the guideline in mind, let's
look at each of the two cases separately: 
\begin{enumerate}
\item If {{\tt (zero?}\ {\tt i)}\/} holds, the function has no effect and produces
 {{\tt (}{\sf void}{\tt )}\/}

\item Otherwise {{\tt i}\/} is positive. In that case, the natural recursion
 sets all fields in {{\tt call-status}\/} with an index in [0,{{\tt (sub1}{ }\ {\tt i)}\/}) to {{\tt false}\/}. Furthermore, to complete the task, the function
 must set the vector field with index {{\tt (sub1}\ {\tt i)}\/} to {{\tt false}\/}.
 The combination of the two effects is achieved with a
 {{\bf begin}-exp\-res\-sion} that sequences the natural recursion and the
 additional {{\tt vector-set!}\/}.
\end{enumerate}
Figure~\ref{fig:reset} puts everything together. The second clause in the
definition of {{\tt reset-aux}\/} first changes the vector at index
{{\tt (sub1}\ {\tt i)}\/} and then uses the natural recursion. The result of the
natural recursion is the result of the {{\bf begin}-exp\-res\-sion}.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt call-status}\ {\tt :}\ {\tt (vectorof}\ {\tt boolean)}\/}}\\
\hbox{;; to keep track of the floors from which calls have been issued }
\\
{\tt (define}\  {\tt call-status}\  {\tt (vector}\  {\sf true}\  {\sf true}\  {\sf true}\  {\sf false}\  {\sf true}\  {\sf true}\  {\sf true}\  {\sf false}{\tt ))}
\\
\hbox{;; {{\tt reset}\ {\tt :}\ {}{\tt ->}{}\ {\tt true}\/}}
\\
\hbox{;; effect: to set all fields in {{\tt call-status}\/} to {{\tt false}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (reset)}
\\
\>{\tt (reset-aux}\  {\tt call-status}\  {\tt (vector-length}\  {\tt call-status)))}
\\
\hbox{;; {{\tt reset-aux}\ {\tt :}\ {\tt (vectorof}\ {\tt boolean)}\ {\bf N}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\hbox{;; effect: to set the fields of {{\tt v}\/} with index in [{{\tt 0}\/}, {{\tt i}\/}) to {{\tt false}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (reset-aux}\  {\tt v}\  {\tt i)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt i)}\  {\tt (}{\sf void}{\tt )\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (b}\={\tt egin}
\\
\>\>\>{\tt (vector-set!}\  {\tt v}\  {\tt (sub1}\  {\tt i)}\  {\tt false)}
\\
\>\>\>{\tt (reset-aux}\  {\tt v}\  {\tt (sub1}\  {\tt i)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Resetting call-buttons for an elevator} \label{fig:reset}
\hrule

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 41.2.14}

 \label{ex:served0}
Use the examples to develop tests for {{\tt reset-aux}\/}. Formulate the
tests as boolean-valued expressions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/served0.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.15}

 \label{ex:served1}
Develop the following variant of {{\tt reset}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt reset-interval}\ {\tt :}\ {\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\tt (}{\sf void}{\tt )}\/}}\\
\hbox{;; effect: to set all fields in [{{\tt from}\/}, {{\tt to}\/}] to {{\tt false}\/}}
\\
\hbox{;; assume: {{\tt (<=}\ {\tt from}\ {\tt to)}\/} holds }
\\
{\tt (define}\  {\tt (reset-interval}\  {\tt from}\  {\tt to)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Use {{\tt reset-interval}\/} to define {{\tt reset}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/served1.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.16}

 \label{ex:pos0}
Suppose we represent the position of an object with a vector and the
velocity of an object with a second vector.  Develop the function
{{\tt move!}\/}, which consumes a position vector and an equally long
velocity vector. It modifies the position vector by adding in the numbers
of the speed vector, field by field:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt move!}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\tt (vectorof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to add the fields of {{\tt v}\/} to the corresponding fields of {{\tt pos}\/} }
\\
{\tt (define}\  {\tt (move!}\  {\tt pos}\  {\tt v)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Justify why the use of a vector-modifying function is appropriate to
model the movement of an object.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/pos0.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.17}

 \label{ex:abs-vec}
Develop the function {{\tt vec-for-all}\/}, which abstracts
{{\tt reset-aux}\/} and the auxiliary vector-processing function for
{{\tt move!}\/} from exercise~\ref{ex:pos0}.  It consumes two values: a
function {{\tt f}\/} and a vector {{\tt vec}\/}. The function {{\tt f}\/}
consumes indices ({{\bf N}\/}) and vector items. The result of
{{\tt vec-for-all}\/} is {{\tt (}{\sf void}{\tt )}\/}; its effect is to apply
{{\tt f}\/} to each index and corresponding value in {{\tt vec}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt vec-for-all}\ {\tt :}\ {\tt (}{\bf N}\ {\tt X}\ {}{\tt ->}{}\ {\tt void)}\ {\tt (vectorof}\ {\tt X)}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to apply {{\tt f}\/} to all indices and values in {{\tt vec}\/}}
\\
\hbox{;; equation: }
\\
\hbox{;; {{\tt (vec-for-all}\ {\tt f}\ {\tt (vector}\ {\tt v-0}\ {\tt ...}\ {\tt v-N))}\/} }
\\
\hbox{;; = }
\\
\hbox{;; {{\tt (begin}\ {\tt (f}\ {\tt N}\ {\tt v-N)}\ {\tt ...}\ {\tt (f}\ {\tt 0}\ {\tt v-0)}\ {\tt (}{\sf void}{\tt ))}\/}}
\\
{\tt (define}\  {\tt (vec-for-all}\  {\tt f}\  {\tt vec)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Use {{\tt vec-for-all}\/} to define {{\tt vector*!}\/}, which consumes a
number {{\tt s}\/} and a vector of numbers and modifies the vector by
multiplying each field's value with~{{\tt s}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/abs-vec.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


The last example covers the common situation when we wish to compute
several numeric values at once and place them in a vector.  In
section~\ref{sec:state-many-examples} we saw that the use of effects is on
occasion useful to communicate several results. In the same manner, it is
sometimes best to create a vector and to modify it within the same
function. Consider the problem of counting how many times each vowel occurs
in a list of letters:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt count-vowels}\ {\tt :}\ {\tt (listof}\ {\tt letter)}\ \/}}\\
\hbox{;; \qquad {\ {}{\tt ->}{}\ {\tt (vector}\ {\tt number}\ {\tt number}\ {\tt number}\ {\tt number}\ {\tt number)}\/}}
\\
\hbox{;; where a {\sl {letter}} is a symbol in {{\tt \char'047}{\tt a}\ {\tt ...}\ {\tt \char'047}{\tt z}\/}}
\\
\hbox{;; to determine how many times the five vowels occur in {{\tt chars}\/}}
\\
\hbox{;; the resulting vector lists the counts in the lexicographic order}
\\
{\tt (define}\  {\tt (count-vowels}\  {\tt chars)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
The choice of vector as a result is appropriate because the function must
combine five values into one and each of the values is equally interesting.

Using the purpose statement, we can also come up with examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (count-vowels}\  {\tt \char'047}{\tt (a}\  {\tt b}\  {\tt c}\  {\tt d}\  {\tt e}\  {\tt f}\  {\tt g}\  {\tt h}\  {\tt i))}\\
{\tt =}\  {\tt (vector}\  {\tt 1}\  {\tt 1}\  {\tt 1}\  {\tt 0}\  {\tt 0)}
\\
\  \  {\tt (count-vowels}\  {\tt \char'047}{\tt (a}\  {\tt a}\  {\tt i}\  {\tt u}\  {\tt u))}
\\
{\tt =}\  {\tt (vector}\  {\tt 2}\  {\tt 0}\  {\tt 1}\  {\tt 0}\  {\tt 2)}
\end{tabbing}\end{minipage}\end{center}
Given that the input is a list, the natural choice for the template is that
for a list-processing function:\label{pg:count-vowels}
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (count-vowels}\  {\tt chars)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt chars)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt ...}\  {\tt (first}\  {\tt chars)}\  {\tt ...}\  {\tt (count-vowels}\  {\tt (rest}\  {\tt chars))}\  {\tt ...}\  {\tt \char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\hrule

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt count-vowels}\ {\tt :}\ {\tt (listof}\ {\tt letter)}\ \/}}\\
\hbox{;; \qquad {\ {}{\tt ->}{}\ {\tt (vector}\ {\tt number}\ {\tt number}\ {\tt number}\ {\tt number}\ {\tt number)}\/}}
\\
\hbox{;; where a {\sl {letter}} is a symbol in {{\tt \char'047}{\tt a}\ {\tt ...}\ {\tt \char'047}{\tt z}\/}}
\\
\hbox{;; to determine how many times the five vowels occur in {{\tt chars}\/}}
\\
\hbox{;; the resulting vector lists the counts in the lexicographic order}
\\
{\tt (d}\={\tt efine}\  {\tt (count-vowels}\  {\tt chars)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (empty?}\  {\tt chars)}\  {\tt (vector}\  {\tt 0}\  {\tt 0}\  {\tt 0}\  {\tt 0}\  {\tt 0)\char'135}
\\
\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt count-rest}\  {\tt (count-vowels}\  {\tt (rest}\  {\tt chars))))}
\\
\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>{\tt (count-a-vowel}\  {\tt (first}\  {\tt chars)}\  {\tt count-rest)}
\\
\>\>\>\>\>{\tt count-rest))\char'135}{\tt ))}
\\
\hbox{;; {{\tt count-a-vowel}\ {\tt :}\ {\tt letter}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\hbox{;; effect: to modify {{\tt counts}\/} at the appropriate place if {{\tt l}\/} is a vowel, }
\\
\hbox{;; none otherwise}
\\
{\tt (d}\={\tt efine}\  {\tt (count-a-vowel}\  {\tt counts}\  {\tt l)}\  
\\
\>{\tt ...)}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Counting vowels} \label{fig:count-vowels}
\hrule

To fill the gaps in the template, we consider each of the two clauses
separately:
\begin{enumerate}
\item If {{\tt (empty?}\ {\tt chars)}\/} is {{\tt true}\/}, the result is a vector
of five {{\tt 0}\/}'s. After all, there are no vowels in an empty list. 

\item If {{\tt chars}\/} isn't {{\tt empty}\/}, the natural recursion counts
how many vowels and which ones occur in {{\tt (rest}\ {\tt chars)}\/}. To get the
correct result, we also have to check whether {{\tt (first}\ {\tt chars)}\/} is a
vowel, and depending on the outcome, increase one of the vector
fields. Since this kind of task is a separate, repeated task, we leave it
to an auxiliary function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt count-a-vowel}\ {\tt :}\ {\tt letter}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to modify {{\tt counts}\/} at the appropriate place if {{\tt c}\/} is a vowel, }
\\
\hbox{;; none otherwise}
\\
{\tt (define}\  {\tt (count-a-vowel}\  {\tt counts}\  {\tt c)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}

In other words, the second clause first counts the vowels in the rest of
the list. This computation is guaranteed to yield a vector according to the
purpose statement. Let's call this vector {{\tt counts}\/}. Then, it uses
{{\tt count-a-vowel}\/} to increase the appropriate field in
{{\tt counts}\/}, if any. The result is {{\tt counts}\/}, after the first
letter on the list has been counted. 
\end{enumerate}
Figure~\ref{fig:count-vowels} contains the complete definition of the main
function. Defining the auxiliary function follows the recipe for
non-recursive structure mutations. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 41.2.18}

 \label{ex:count-vowel0}
Develop the function {{\tt count-a-vowel}\/}. Then test the complete
{{\tt count-vowels}\/} program.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/count-vowel0.html}{\rule{3pt}{5pt}}




{\bf Exercise 41.2.19}

 \label{ex:count-vowel5}
At the end of intermezzo~5, we could have defined {{\tt count-vowels}\/} as
shown in figure~\ref{fig:another-vowel}. This version does not use
{{\tt vector-set!}\/}, but constructs the vector directly using
{{\tt build-vector}\/}.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (count-vowels-bv}\  {\tt chars)}\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (count-vowel}\  {\tt x}\  {\tt chars)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt chars)}\  {\tt 0\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt x}\  {\tt (first}\  {\tt chars))}
\\
\>\>\>\>\>\>{\tt (+}\  {\tt (count-vowel}\  {\tt x}\  {\tt (rest}\  {\tt chars))}\  {\tt 1)\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (count-vowel}\  {\tt x}\  {\tt (rest}\  {\tt chars))\char'135}{\tt )\char'135}{\tt )))}
\\
\>\>{\tt (build-vector}\  {\tt 5}\  {\tt (l}\={\tt ambda}\  {\tt (i)}\  
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (=}\  {\tt i}\  {\tt 0)}\  {\tt (count-vowel}\  {\tt \char'047}{\tt a}\  {\tt chars)\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt (=}\  {\tt i}\  {\tt 1)}\  {\tt (count-vowel}\  {\tt \char'047}{\tt e}\  {\tt chars)\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt (=}\  {\tt i}\  {\tt 2)}\  {\tt (count-vowel}\  {\tt \char'047}{\tt i}\  {\tt chars)\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt (=}\  {\tt i}\  {\tt 3)}\  {\tt (count-vowel}\  {\tt \char'047}{\tt o}\  {\tt chars)\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt (=}\  {\tt i}\  {\tt 4)}\  {\tt (count-vowel}\  {\tt \char'047}{\tt u}\  {\tt chars)\char'135}{\tt )))))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: Another way of counting vowels} \label{fig:another-vowel}
\hrule

Measure the performance difference between {{\tt count-vowels-bv}\/} and
{{\tt count-vowels}\/}. \noindent{\bf Hint:} \ Define a function that generates a large list
of random letters (with, say, 5,000 or 10,000 items).

Explain the performance difference between {{\tt count-vowels-bv}\/} and
{{\tt count-vowels}\/}. Does the explanation reflect the measured
difference? What does this suggest concerning the {{\tt vector-set!}\/}
operation?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/count-vowel5.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.20}

 \label{ex:count-vowel2}
Develop {{\tt histogram}\/}. The function consumes a list of grades between
0 and 100; it produces a vector of size 101 where each slot contains the
number of grades at that level.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/count-vowel2.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.2.21}

 \label{ex:count-vowel3}
Develop {{\tt count-children}\/}. The function consumes an descendant family
tree, which is a family tree that leads from a family member to the
descendants. It produces a vector with six fields. The first five slots
contain the number of family members that have that many children; the
sixth field contains the number of family members that have five or more
children.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/count-vowel3.html}{\rule{3pt}{5pt}}


\subsection{Structural Design Recipes and Mutation, Part 2} \label{sec:design-mut-ex2}

In the preceding sections, we studied structure mutation for fields that
contain atomic data. We know, however, that structures can contain
structures. Starting in section~\ref{sec:struct-in-struct}, we even
encountered self-referential data definitions involving structures in
structures. On occasion, processing such classes of data may also require
mutations of structure fields that contain structures. In this section, we
work through one such an example.


\hrule

\bigskip  \begin{rawhtml}   <center><img src=../icons/cards.gif alt="[cards in DrScheme]"></center>  \end{rawhtml}

\center{Figure: Playing with cards} \label{fig:cards}
\hrule

Suppose we wish to simulate a card game as a program. Each card has two
important characteristics: its {\sl suit} and its {\sl rank}.  A player's
collection of cards is called a {\sl hand}. For now we also assume that
every player has at least one card, that is, a hand is never empty. 

Figure~\ref{fig:cards} contains a screen shot of DrScheme with structure
and data definitions for manipulating cards and hands. The program fragment
does not introduce separate classes of cards and hands, but a single
structure and a single data definitions for hands. A hand consists of a
{{\tt hand}\/} structure, which contains a {{\tt rank}\/}, a {{\tt suit}\/},
and a {{\tt next}\/} field. The data definition shows that the next field
may contain two kinds of values: {{\tt empty}\/}, which means that there are
no other cards, and a {{\tt hand}\/} structure, which contains the remainder
of the cards. From a global perspective, a {{\tt hand}\/} forms a chain of
cards; only the last one contains {{\tt empty}\/} in the {{\tt next}\/}
field.\footnote{Scheme proper provides list mutators, and a Scheme programmer would use them to represent a hand as a list of cards.}

At first, a player has no cards. Picking up the first card creates a
hand. Others cards are then inserted into the existing hand as needed.
This calls for two functions: one for creating a hand and another one for
inserting a card into a hand. Because a hand exists only once and
corresponds to a physical object, it is natural to think of the second
functions as one that modifies an existing value rather than building a new
one. For now, we accept this premise and explore its consequences. 

Creating a hand is a simple act and easy to implement as a function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt create-hand}\ {\tt :}\ {\tt rank}\ {\tt suit}\ {}{\tt ->}{}\ {\tt hand}\/}}\\
\hbox{;; to create a single-card hand from {{\tt r}\/} and {{\tt s}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (create-hand}\  {\tt r}\  {\tt s)}
\\
\>{\tt (make-hand}\  {\tt r}\  {\tt s}\  {\tt empty))}
\end{tabbing}\end{minipage}\end{center}
The function consumes the properties of a card; it creates a hand with one
card and no others. 

\hrule




\begin{htmlonly}
\end{htmlonly}

 \begin{rawhtml} <table noborder cellspacing=20 align=center> <tr align=left><td> (define hand0 (create-hand 13 spades)) </td><td> <table> <!-- (define hand0 (create-hand 13 spades)) --> <tr align=left> <table border bgcolor=beige> <tr>    <td><font size=-2>rank</font></td>    <td><font size=-2>suit</font></td>    <td><font size=-2>next</font></td> </tr> <tr>   <td>13</td>    <td><img src=../icons/spades.gif alt="spades"></td>    <td>empty</td> </tr> </table> </tr> <tr align=left><td> (add-at-end! diamonds 1 hand0) </td><td> <table> <!-- (add-at-end! diamonds 1 hand0) --> <tr align=left> <table border bgcolor=tan> <tr>    <td><font size=-2>rank</font></td>    <td><font size=-2>suit</font></td>    <td><font size=-2>next</font></td> </tr> <tr>   <td>13</td>    <td><img src=../icons/spades.gif alt="spades"></td>    <td>      <table>      <!-- (make-hand 1 diamond empty)) -->      <tr align=left>      <table border bgcolor=beige>      <tr>         <td><font size=-2>rank</font></td>         <td><font size=-2>suit</font></td>         <td><font size=-2>next</font></td>      </tr>      <tr>        <td>13</td>         <td><img src=../icons/diamonds.gif alt="diamonds"></td>         <td>empty</td>      </tr>      </table>   </td> </tr> </table> </td></tr> </table> \end{rawhtml}

\center{Figure: Building a hand} \label{fig:build-a-hand}
\hrule

Adding a card to the end of a hand is a more difficult task than that. To
simplify our life a bit, let's say that a player always adds new cards to
the end of the hand. In this case we must process an arbitrarily large
value, which means we need a recursive function. Here is contract, effect
statement, and header:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt add-at-end!}\ {\tt :}\ {\tt rank}\ {\tt suit}\ {\tt hand}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect : to add a card with {{\tt r}\/} as rank and {{\tt s}\/} as suit at the end }
\\
{\tt (define}\  {\tt (add-at-end!}\  {\tt rank}\  {\tt suit}\  {\tt a-hand)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
These specifications say that the function has the invisible value as the
result and that it communicates with the rest of the program exclusively
through its effects. 

Let's start with examples: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt hand0}\  {\tt (create-hand}\  {\tt 13}\  {\tt SPADES))}\end{tabbing}\end{minipage}\end{center}
If we were to evaluate the following expression 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (add-at-end!}\  {\tt 1}\  {\tt DIAMONDS}\  {\tt hand0)}\end{tabbing}\end{minipage}\end{center}
in the context of this definition, {{\tt hand0}\/} becomes a card with two
hands: a spades-13 followed by a diamonds-1. Figure~\ref{fig:build-a-hand}
depicts the change of hand0; the top half displays the initial state of
{{\tt hand0}\/}, the lower half displays the state after
{{\tt add-at-end!}\/} has added a card. If we furthermore evaluate 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (add-at-end!}\  {\tt 2}\  {\tt CLUBS}\  {\tt hand0))}\end{tabbing}\end{minipage}\end{center}
in this context, {{\tt hand0}\/} become a hand with three cards. The last
one is a clubs-2. In terms of an evaluation, the definition of
{{\tt hand0}\/} should change to
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt hand0}\  \\
\>{\tt (m}\={\tt ake-hand}\  {\tt 13}\  {\tt SPADES}
\\
\>\>{\tt (m}\={\tt ake-hand}\  {\tt 1}\  {\tt DIAMONDS}
\\
\>\>\>{\tt (make-hand}\  {\tt 2}\  {\tt CLUBS}\  {\tt empty))))}
\end{tabbing}\end{minipage}\end{center}
after both additions have been carried out. 

Given that the {{\tt rank}\/} and {{\tt suit}\/} argument to
{{\tt add-at-end!}\/} are atomic values, the template must be based on the
data definition for {{\tt hand}\/}s: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (add-at-end!}\  {\tt rank}\  {\tt suit}\  {\tt a-hand)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (empty?}\  {\tt (hand-next}\  {\tt a-hand))}\  
\\
\>\>\>{\tt ...}\  {\tt (hand-rank}\  {\tt a-hand)}\  {\tt ...}\  {\tt (hand-suit}\  {\tt a-hand)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}\={\tt else}\  {\tt ...}\  {\tt (hand-rank}\  {\tt a-hand)}\  {\tt ...}\  {\tt (hand-suit}\  {\tt a-hand)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (add-at-end!}\  {\tt rank}\  {\tt suit}\  {\tt (hand-next}\  {\tt a-hand))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The template consists of two clauses, which check the content of the
{{\tt next}\/} field of {{\tt a-hand}\/}. It is recursive in the second
clause, because the data definition for {{\tt hand}\/}s is self-referential
in that clause. In short, the template is completely conventional. 

The next step is to consider how the function should affect {{\tt a-hand}\/}
in each clause: 
\begin{enumerate}
\item In the first case, {{\tt a-hand}\/}'s {{\tt next}\/} field is
{{\tt empty}\/}. In that case, we can modify the {{\tt next}\/} field so that
it contains the new card: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set-next-hand!}\  {\tt a-hand}\  {\tt (make-hand}\  {\tt rank}\  {\tt suit}\  {\tt empty))}\end{tabbing}\end{minipage}\end{center}
The newly created {{\tt hand}\/} structure is now the one that contains
{{\tt empty}\/} in its next field, that is, it is the new end of the
{{\tt a-hand}\/} value. 

\item In the second case, the natural recursion adds a new card to the end
of {{\tt a-hand}\/}. Indeed, because the given {{\tt a-hand}\/} isn't the
last one in the chain, the natural recursion does everything that needs to
be done.
\end{enumerate}

Here is the complete definition of {{\tt add-at-end!}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt add-at-end!}\ {\tt :}\ {\tt rank}\ {\tt suit}\ {\tt hand}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to add a card with v as rank and s as suit at the end of {{\tt a-hand}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (add-at-end!}\  {\tt rank}\  {\tt suit}\  {\tt a-hand)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (empty?}\  {\tt (hand-next}\  {\tt a-hand))}
\\
\>\>\>{\tt (set-hand-next!}\  {\tt a-hand}\  \  {\tt (make-hand}\  {\tt rank}\  {\tt suit}\  {\tt empty))\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (add-at-end!}\  {\tt rank}\  {\tt suit}\  {\tt (hand-next}\  {\tt a-hand))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
It closely resembles the list-processing functions we designed in
part~\ref{part:structural}. This should come as no surprise, because
{{\tt add-at-end!}\/} processes values from a class that closely resembles
the data definition of lists and the design recipes are formulated in a
general manner. 
 
  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 
{\bf Exercise 41.3.1}

 \label{ex:cards0}
Evaluate the following program by hand: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt hand0}\  {\tt (create-hand}\  {\tt 13}\  {\tt SPADES))}\\
{\tt (b}\={\tt egin}
\\
\>{\tt (add-at-end!}\  {\tt 1}\  {\tt DIAMONDS}\  {\tt hand0)}
\\
\>{\tt (add-at-end!}\  {\tt 2}\  {\tt CLUBS}\  {\tt hand0)}
\\
\>{\tt hand0)}
\end{tabbing}\end{minipage}\end{center}
Test the function with this example. 

Make up two other examples. Recall that each example consists of an initial
hand, cards to be added, and a prediction of what the result should
be. Then test the function with the additional examples. Formulate the
tests as boolean-valued expressions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/cards0.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.3.2}

 \label{ex:cards2}
Develop the function {{\tt last-card}\/}. It consumes a {{\tt hand}\/} and
produces a list with the last card's rank and suit. How can we use this
function to test the {{\tt add-at-end!}\/} function?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/cards2.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.3.3}

 \label{ex:ft-ss1}
Suppose a family tree consists of structures that record the name, social
security number, and parents of a person. Describing such a tree requires a
structure definition:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt child}\  {\tt (name}\  {\tt social}\  {\tt father}\  {\tt mother))}\end{tabbing}\end{minipage}\end{center}
and a data definition: 

  \begin{quote} A {\sl {family tree node}} (short: {{\tt ftn}\/}) is either      \begin{enumerate}     \item {{\tt false}\/}, or     \item {{\tt (make-child}\ {\tt name}\ {\tt socsec}\ {\tt f}\ {\tt m)}\/}            where {{\tt name}\/} is a symbol, {{\tt socsec}\/} is a number, and                {{\tt f}\/} and {{\tt m}\/} are {\em ftn\/}s.      \end{enumerate} \end{quote}

\noindent For here, we assume that everyone has a social security number
and that social security numbers are unique. 

Following our convention from part~\ref{part:advanced}, {{\tt false}\/}
represents a lack of knowledge about someone's father or mother. As we find
out more information, though, we can add nodes to our family tree. 

Develop the function {{\tt add-ftn!}\/}. It consumes a family tree
{{\tt a-ft}\/}, a social security number {{\tt ssc}\/}, a symbol
{{\tt anc}\/}, and a {{\tt child}\/} structure. Its effect is to modify that
structure in {{\tt a-ft}\/} whose social security number is {{\tt ssc}\/}. If
{{\tt anc}\/} is {{\tt \char'047}{\tt father}\/}, it modifies the {{\tt father}\/} field to
contain the given {{\tt child}\/} structure; otherwise, {{\tt anc}\/} was the
symbol {{\tt \char'047}{\tt mother}\/} and {{\tt add-ftn!}\/} mutates the {{\tt mother}\/}
field. If the respective fields already contain a {{\tt child}\/} structure,
{{\tt add-ftn!}\/} signals an error. 

\medskip\noindent{\bf Using Functions as Arguments}:\  Instead of accepting {{\tt \char'047}{\tt father}\/}
and {{\tt \char'047}{\tt mother}\/} for {{\tt anc}\/}, the function could also accept one
of the two structure mutators: {{\tt set-child-father!}\/} or
{{\tt set-child-mother!}\/}. Modify {{\tt add-ftn!}\/}
accordingly.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ft-ss1.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.3.4}

 \label{ex:cards1}
Develop an implementation of a hand with {{\tt create-hand}\/} and
{{\tt add-at-end!}\/} services using encapsulated state variables and
function definitions. Use {{\tt set!}\/} expression but no structure
mutators.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/cards1.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Not all mutator functions are as easily designed as the
{{\tt add-at-end!}\/} function. Indeed, in some cases things don't even work
out at all. Let's consider two additional services. The first one removes
the last card in a hand. Its contract and effect statement are variations
on those for {{\tt add-at-end!}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt remove-last!}\ {\tt :}\ {\tt hand}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect : to remove the last card in {{\tt a-hand}\/}, unless it is the only one}
\\
{\tt (define}\  {\tt (remove-last!}\  {\tt a-hand)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
The effect is restricted because a hand must always contain one card. 

We can also adapt the example for {{\tt add-at-end!}\/} without difficulty: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt hand0}\  {\tt (create-hand}\  {\tt 13}\  {\tt SPADES))}\\
{\tt (b}\={\tt egin}
\\
\>{\tt (add-at-end!}\  {\tt 1}\  {\tt DIAMONDS}\  {\tt hand0)}
\\
\>{\tt (add-at-end!}\  {\tt 2}\  {\tt CLUBS}\  {\tt hand0)}
\\
\>{\tt (remove-last!}\  {\tt hand0)}
\\
\>{\tt (remove-last!}\  {\tt hand0))}\  \  
\end{tabbing}\end{minipage}\end{center}
The resulting value is {{\tt void}\/}. The effect of the computation is to 
return {{\tt hand0}\/} in its initial state. 

The template for {{\tt remove-last!}\/} is the same as that for
{{\tt add-at-end!}\/} because both functions process the same class of
values. So the next step is to analyze what effects the function must
compute for each case in the template: 
\begin{enumerate}
\item Recall that the first clause represents the case when {{\tt a-hand}\/}'s 
{{\tt next}\/} field is {{\tt empty}\/}. In contrast to the situation with
{{\tt add-at-end!}\/}, it is not clear what we need to do now. According to the
effect statement, we must do one of two things: 
\begin{enumerate}
\item If {{\tt a-hand}\/} is the last item in a chain that consists of more
than one {{\tt hand}\/} structure, it must be removed.  

\item If {{\tt a-hand}\/} is the only structure that {{\tt remove-last!}\/}
consumed, the function should have no effect.
\end{enumerate}
But we can't know whether or not {{\tt a-hand}\/} is the last item in a long
chain of {{\tt hand}\/}s or the only one. We have lost knowledge that was
available at the beginning of the evaluation!
\end{enumerate}
The analysis of the first clause suggests the use of an accumulator. We
tried the natural route and discovered that knowledge is lost during an
evaluation, which is the criterium for considering a switch to an
accumulator-based design recipe. 


Once we have recognized the need for an accumulator-style function, we
encapsulate the template in a {{\bf local}-exp\-res\-sion} and add an
accumulator argument to its definition and applications: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (remove-last!}\  {\tt a-hand0)}\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt accumulator}\/} \ldots }
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (rem!}\  {\tt a-hand}\  {\tt accumulator)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (empty?}\  {\tt (hand-next}\  {\tt a-hand))}\  
\\
\>\>\>\>\>\>{\tt ...}\  {\tt (hand-rank}\  {\tt a-hand)}\  {\tt ...}\  {\tt (hand-suit}\  {\tt a-hand)}\  {\tt ...\char'135}
\\
\>\>\>\>\>{\tt \char'133}\={\tt else}\  {\tt ...}\  {\tt (hand-rank}\  {\tt a-hand)}\  {\tt ...}\  {\tt (hand-suit}\  {\tt a-hand)}\  {\tt ...}
\\
\>\>\>\>\>\>{\tt ...}\  {\tt (rem!}\  {\tt (hand-next}\  {\tt a-hand)}\  {\tt ...}\  \fbox{{\it accumulator\/}}\  {\tt ...)}\  {\tt ...\char'135}{\tt )))}
\\
\>\>{\tt ...}\  {\tt (rem!}\  {\tt a-hand0}\  {\tt ...)}\  {\tt ...))}
\end{tabbing}\end{minipage}\end{center}
The questions to ask now are what the accumulator represents and what its
first value is. 

The best way to understand the nature of accumulators is to study why the
plain structural design of {{\tt remove-last!}\/} failed. Hence, we return
to the analysis of our first clause in the template. When {{\tt rem!}\/}
reaches that clause, two things should have been accomplished. First,
{{\tt rem!}\/} should know that {{\tt a-hand}\/} is not the only
{{\tt hand}\/} structure in {{\tt a-hand0}\/}. Second, {{\tt rem!}\/}  must be
enabled to remove {{\tt a-hand}\/} from {{\tt a-hand0}\/}.  For the first
goal, {{\tt rem!}\/}'s first application should be in a context where we
know that {{\tt a-hand0}\/} contains more than one card. This argument
suggests a {{\bf cond}-exp\-res\-sion} for the body of the {{\bf local}-exp\-res\-sion}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (c}\={\tt ond}
\\
\>{\tt \char'133}{\tt (empty?}\  {\tt (hand-next}\  {\tt a-hand))}\  {\tt (}{\sf void}{\tt )\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt (rem!}\  {\tt a-hand0}\  {\tt ...)\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
For the second goal, {{\tt rem!}\/}'s accumulator argument should always
represent the {{\tt hand}\/} structure that preceeds {{\tt a-hand}\/} in
{{\tt a-hand0}\/}. Then {{\tt rem!}\/} can remove {{\tt a-hand}\/} by
modifying the predecessor's {{\tt next}\/} field:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
{\tt (set-hand-next!}\  {\tt accumulator}\  {\tt empty)}
\end{tabbing}\end{minipage}\end{center}

Now the pieces of the design puzzle fall into place. The complete
definition of the function is in figure~\ref{fig:def-remove-last}.  The
{{\tt accumulator}\/} parameter is renamed to {{\tt predecessor-of:a-hand}\/}
to emphasize the relationship to the parameter proper.  The first
application of {{\tt rem!}\/} in the body of the {\bf local}-exp\-res\-sion\ hands it the
second {{\tt hand}\/} structure in {{\tt a-hand0}\/}. The second argument is
{{\tt a-hand0}\/}, which establishes the desired relationship.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt remove-last!}\ {\tt :}\ {\tt hand}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect : to remove the last card in {{\tt a-hand0}\/}, unless it is the only one}
\\
{\tt (d}\={\tt efine}\  {\tt (remove-last!}\  {\tt a-hand0)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt predecessor-of:a-hand}\/} represents the predecessor of}
\\
\>\>\>\hbox{;; {{\tt a-hand}\/} in the {{\tt a-hand0}\/} chain }
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (rem!}\  {\tt a-hand}\  {\tt predecessor-of:a-hand)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (empty?}\  {\tt (hand-next}\  {\tt a-hand))}
\\
\>\>\>\>\>\>{\tt (set-hand-next!}\  {\tt predecessor-of:a-hand}\  {\tt empty)\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (rem!}\  {\tt (hand-next}\  {\tt a-hand)}\  {\tt a-hand)\char'135}{\tt )))}
\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (empty?}\  {\tt (hand-next}\  {\tt a-hand0))}\  {\tt (}{\sf void}{\tt )\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (rem!}\  {\tt (hand-next}\  {\tt a-hand0)}\  {\tt a-hand0)\char'135}{\tt )))}
\end{tabbing}\end{minipage}\end{center}
Both applications of {{\tt rem!}\/} have the shape
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (rem!}\  {\tt (hand-next}\  {\tt a-hand)}\  {\tt a-hand)}\end{tabbing}\end{minipage}\end{center}
\center{Figure: Removing the last card} \label{fig:def-remove-last}
\hrule

It is now time to revisit the basic assumption about the card game that the
 cards are added to the end of a hand.  When human players pick up cards,
 they hardly ever just add them to the end. Instead, many use some special
 arrangement and maintain it over the course of a game. Some arrange hands
 according to suits, others according to rank, and yet others according to
 both criteria.

Let's consider an operation for inserting a card into a {{\tt hand}\/} based
on its rank:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sorted-insert!}\ {\tt :}\ {\tt rank}\ {\tt suit}\ {\tt hand}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; assume: {{\tt a-hand}\/} is sorted by rank, in descending order}
\\
\hbox{;; effect: to add a card with {{\tt r}\/} as rank and {{\tt s}\/} as suit at the proper place}
\\
{\tt (define}\  {\tt (sorted-insert!}\  {\tt r}\  {\tt s}\  {\tt a-hand)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
The function assumes that the given {{\tt hand}\/} is already sorted. The
assumption naturally holds if we always use {{\tt create-hand}\/} to create
a hand and {{\tt sorted-insert!}\/} to insert cards.

Suppose we start with the same hand as above for {{\tt add-at-end!}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt hand0}\  {\tt (create-hand}\  {\tt 13}\  {\tt SPADES))}\end{tabbing}\end{minipage}\end{center}
If we evaluate {{\tt (sorted-insert!}\ {\tt 1}\ {\tt DIAMONDS}\ {\tt hand0)}\/} in this context,
{{\tt hands0}\/} becomes
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (m}\={\tt ake-hand}\  {\tt 13}\  {\tt SPADES}\\
\>{\tt (make-hand}\  {\tt 1}\  {\tt DIAMONDS}\  {\tt empty))}
\end{tabbing}\end{minipage}\end{center}
If we now evaluate {{\tt (sorted-insert!}\ {\tt 1}\ {\tt CLUBS}\ {\tt hand0)}\/} in addition, 
we get
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (m}\={\tt ake-hand}\  {\tt 13}\  {\tt SPADES}\\
\>{\tt (m}\={\tt ake-hand}\  {\tt 2}\  {\tt CLUBS}
\\
\>\>{\tt (make-hand}\  {\tt 1}\  {\tt DIAMONDS}\  {\tt empty)))}
\end{tabbing}\end{minipage}\end{center}
for {{\tt hand0}\/}. This value shows what it means for a chain to be
sorted in descending order. As we traverse the chain, the ranks get smaller
and smaller independent of what the suits are. 

Our next step is to analyze the template. Here is the template, adapted to
our present purpose: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (sorted-insert!}\  {\tt r}\  {\tt s}\  {\tt a-hand)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (empty?}\  {\tt (hand-next}\  {\tt a-hand))}\  
\\
\>\>\>{\tt ...}\  {\tt (hand-rank}\  {\tt a-hand)}\  {\tt ...}\  {\tt (hand-suit}\  {\tt a-hand)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}\  {\tt ...}\  {\tt (hand-rank}\  {\tt a-hand)}\  {\tt ...}\  {\tt (hand-suit}\  {\tt a-hand)}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (sorted-insert!}\  {\tt r}\  {\tt s}\  {\tt (hand-next}\  {\tt a-hand))}\  {\tt ...\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The key step of the function is to insert the new card between two cards
such that first card's rank is larger than, or equal to, {{\tt r}\/} and
{{\tt r}\/} is larger than, or equal to, the rank of the second. Because 
we only have two cards in the second clause, we start by formulating the 
answer for the second clause. The condition we just specified implies that
we need a nested {{\bf cond}-exp\-res\-sion}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (>=}\  {\tt (hand-rank}\  {\tt a-hand)}\  {\tt r}\  {\tt (hand-rank}\  {\tt (hand-next}\  {\tt a-hand)))}\  {\tt ...\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt ...\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
The first condition expresses in Scheme what we just discussed. In
particular, {{\tt (hand-rank}\ {\tt a-hand)}\/} picks the rank of the first card in
{{\tt a-hand}\/} and {{\tt (hand-rank}\ {\tt (hand-next}\ {\tt a-hand))}\/} picks the rank
of the second one. The comparison determines whether the three ranks are
properly ordered. 

Each case of this new {{\bf cond}-exp\-res\-sion} deserves its own analysis: 
\begin{enumerate}
\item If {{\tt (>=}\ {\tt (hand-rank}\ {\tt a-hand)}\ {\tt r}\ {\tt (hand-rank}\ {\tt (hand-next}\ {\tt a-hand)))}\/}
is true, then the new card must go between the two cards that are currently
linked. That is, the {{\tt next}\/} field of {{\tt a-hand}\/} must be changed
to contain a new {{\tt hand}\/} structure. The new structure consists of
{{\tt r}\/}, {{\tt s}\/}, and the original value of {{\tt a-hand}\/}'s
{{\tt next}\/} field. This yields the following elaboration of the
{{\bf cond}-exp\-res\-sion}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}\={\tt (>=}\  {\tt (hand-rank}\  {\tt a-hand)}\  {\tt r}\  {\tt (hand-rank}\  {\tt (hand-next}\  {\tt a-hand)))}
\\
\>\>{\tt (set-hand-next!}\  {\tt a-hand}\  {\tt (make-hand}\  {\tt r}\  {\tt s}\  {\tt (hand-next}\  {\tt a-hand)))\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt ...\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}

\item If {{\tt (>=}\ {\tt (hand-rank}\ {\tt a-hand)}\ {\tt r}\ {\tt (hand-rank}\ {\tt (hand-next}\ {\tt a-hand)))}\/}
is false, the new card must be inserted at some place in the rest of the
chain. Of course, the natural recursion accomplishes just that, which
finishes the analysis of the second clause of {{\tt sorted-insert!}\/}.
\end{enumerate}
Putting all the pieces together yields a partial function definition: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (sorted-insert!}\  {\tt r}\  {\tt s}\  {\tt a-hand)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}\={\tt (empty?}\  {\tt (hand-next}\  {\tt a-hand))}\  
\\
\>\>\>{\tt ...}\  {\tt (hand-rank}\  {\tt a-hand)}\  {\tt ...}\  {\tt (hand-suit}\  {\tt a-hand)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}\  
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}\={\tt (>=}\  {\tt (hand-rank}\  {\tt a-hand)}\  {\tt r}\  {\tt (hand-rank}\  {\tt (hand-next}\  {\tt a-hand)))}
\\
\>\>\>\>\>{\tt (set-hand-next!}\  {\tt a-hand}\  {\tt (make-hand}\  {\tt r}\  {\tt s}\  {\tt (hand-next}\  {\tt a-hand)))\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (sorted-insert!}\  {\tt r}\  {\tt s}\  {\tt (hand-next}\  {\tt a-hand))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The only remaining gaps are now in the first clause. 

The difference between the first and the second {{\tt cond}\/}-clause is
that there is no second {{\tt hand}\/} structure in the first clause so that
we cannot compare ranks. Still, we can compare {{\tt r}\/} and
{{\tt (hand-rank}\ {\tt a-hand)}\/} and compute something based on the outcome of
this comparison:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (>=}\  {\tt (hand-rank}\  {\tt a-hand)}\  {\tt r)}\  {\tt ...\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt ...\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
Clearly, if the comparison expression evaluates to {{\tt true}\/}, the
function must mutate the {{\tt next}\/} field of {{\tt a-hand}\/} and add a
new {{\tt hand}\/} structure: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}\={\tt (>=}\  {\tt (hand-rank}\  {\tt a-hand)}\  {\tt r)}\  
\\
\>\>{\tt (set-hand-next!}\  {\tt a-hand}\  {\tt (make-hand}\  {\tt r}\  {\tt s}\  {\tt empty))\char'135}
\\
\>{\tt \char'133}{\tt else}\  {\tt ...\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
The problem is that we have nothing to mutate in the second clause. If
{{\tt r}\/} is larger than the rank of {{\tt a-hand}\/}, the new card should
be inserted between the predecessor of {{\tt a-hand}\/} and {{\tt a-hand}\/}.
But that kind of situation would have been discovered by the second
clause. The seeming contradiction suggests that the dots in the second
clause are a response to a singular case:
\begin{quote}
The dots are only evaluated if {{\tt sorted-insert!}\/} consumes a rank
{{\tt r}\/} that is larger than all the values in the {{\tt rank}\/} fields
of {{\tt a-hand}\/}.
\end{quote}
In that singular case, {{\tt a-hand}\/} shouldn't change at all. After all,
there is no way to create a descending chain of cards by mutating
{{\tt a-hand}\/} or any of its embedded {{\tt hand}\/} structures. 

At first glance, we can overcome the problem with a {{\tt set!}\/}
expression that changes the definition of {{\tt hand0}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set!}\  {\tt hand0}\  {\tt (make-hand}\  {\tt r}\  {\tt s}\  {\tt hand0))}\end{tabbing}\end{minipage}\end{center}
This fix doesn't work in general though, because we can't assume that we
know which variable definition must be modified. Since expressions can be
abstracted over values but not variables, there is also no way to abstract
over {{\tt hand0}\/} in this {\bf set!}-exp\-res\-sion.

\hrule
\begin{quote} A {\sl {hand}} is an interface:   \begin{enumerate}   \item {{\tt \char'047}{\tt insert}\/} :: {{\tt rank}\ {\tt suit}\ {}{\tt ->}{}\ {\tt void}\/}  \end{enumerate} \end{quote}

\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt create-hand}\ {\tt :}\ {\tt rank}\ {\tt suit}\ {}{\tt ->}{}\ {\tt hand}\/}}\\
\hbox{;; to create a {{\tt hand}\/} from the {{\tt rank}\/} and {{\tt suit}\/} of a single card}
\\
{\tt (d}\={\tt efine}\  {\tt (create-hand}\  {\tt rank}\  {\tt suit)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define-struct}\  {\tt hand}\  {\tt (rank}\  {\tt suit}\  {\tt next))}
\\
\>\>\>\relax
\\
\>\>\>{\tt (define}\  {\tt the-hand}\  {\tt (make-hand}\  {\tt rank}\  {\tt suit}\  {\tt empty))}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt insert-aux!}\ {\tt :}\ {\tt rank}\ {\tt suit}\ {\tt hand}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\>\>\>\hbox{;; assume: hand is sorted by rank in descending order}
\\
\>\>\>\hbox{;; effect: to add a card with {{\tt r}\/} as rank and {{\tt s}\/} as suit}
\\
\>\>\>\hbox{;; at the proper place}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (insert-aux!}\  {\tt r}\  {\tt s}\  {\tt a-hand)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (empty?}\  {\tt (hand-next}\  {\tt a-hand))}\  
\\
\>\>\>\>\>\>{\tt (set-hand-next!}\  {\tt a-hand}\  {\tt (make-hand}\  {\tt r}\  {\tt s}\  {\tt empty))\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>{\tt \char'133}\={\tt (>=}\  \={\tt (hand-rank}\  {\tt a-hand)}
\\
\>\>\>\>\>\>\>\>{\tt r}
\\
\>\>\>\>\>\>\>\>{\tt (hand-rank}\  {\tt (hand-next}\  {\tt a-hand)))}
\\
\>\>\>\>\>\>\>{\tt (s}\={\tt et-hand-next!}\  {\tt a-hand}
\\
\>\>\>\>\>\>\>\>{\tt (make-hand}\  {\tt r}\  {\tt s}\  {\tt (hand-next}\  {\tt a-hand)))\char'135}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (insert-aux!}\  {\tt r}\  {\tt s}\  {\tt (hand-next}\  {\tt a-hand))\char'135}{\tt )\char'135}{\tt ))}
\\
\>\>\>\relax
\\
\>\>\>{\tt ...}
\\
\>\>\>\relax
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (service-manager}\  {\tt msg)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}\={\tt (symbol=?}\  {\tt msg}\  {\tt \char'047}{\tt insert!)}\  
\\
\>\>\>\>\>\>{\tt (l}\={\tt ambda}\  {\tt (r}\  {\tt s)}
\\
\>\>\>\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>\>\>{\tt \char'133}\={\tt (>}\  {\tt r}\  {\tt (hand-rank}\  {\tt the-hand))}
\\
\>\>\>\>\>\>\>\>\>{\tt (set!}\  {\tt the-hand}\  {\tt (make-hand}\  {\tt r}\  {\tt s}\  {\tt the-hand))\char'135}
\\
\>\>\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (insert-aux!}\  {\tt rank}\  {\tt suit}\  {\tt the-hand)\char'135}{\tt ))\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (error}\  {\tt \char'047}{\tt managed-hand}\  {\tt ``message}\  {\tt not}\  {\tt understood'')\char'135}{\tt )))}
\\
\>\>{\tt manage))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Encapsulation and structure mutation for hands of cards} 
\label{fig:hand-enc}
\hrule

We're stuck. It is impossible to define {{\tt sorted-insert!}\/}, at least
as specified above. The analysis suggests a remedy, however. If we
introduce a single variable that always stands for the current
{{\tt hand}\/} structure, we can use a combindation of assignments and
structure mutators to insert a new card. The trick is not to let any other
part of the program refer to this variable or even change it. Otherwise a
simple {{\tt set!}\/} won't work, as argued before. In other words, we need
a state variable for each {{\tt hand}\/} structure, and we need to
encapsulate it in a {{\bf local}-exp\-res\-sion}.

Figure~\ref{fig:hand-enc} displays the complete function definition. It 
follows the pattern of section~\ref{sec:encapsulate}. The function itself
corresponds to {{\tt create-hand}\/}, though instead of producing a
structure the new {{\tt create-hand}\/} function produces a manager
function. At this point, the manager can only deal with one message:
{{\tt \char'047}{\tt insert}\/}; all other messages are rejected. An {{\tt \char'047}{\tt insert}\/}
message first checks whether the new rank is larger than the first one in
{{\tt the-hand}\/}, the hidden state variable. If so, the manager just
changes {{\tt the-hand}\/}; if not, it uses {{\tt insert-aux!}\/}, which may
now assume that the new card belongs into the middle of the chain. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises} 

{\bf Exercise 41.3.5}

 \label{ex:hand-remove} 
Extend the definition in figure~\ref{fig:hand-enc} with a service for
removing the first card of a given rank, even if it is the only
card.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hand-remove.html}{\rule{3pt}{5pt}} 


{\bf Exercise 41.3.6}

 \label{ex:hand-project} 
Extend the definition in figure~\ref{fig:hand-enc} with a service for
determing the suits of those cards in {{\tt the-hand}\/} that have a given
rank. The function should produce a list of suits.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hand-project.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.3.7}

 \label{ex:hand-mostly-functional} 
Reformulate {{\tt create-hand}\/} in figure~\ref{fig:hand-enc} such that the
manager uses a single {{\bf set!}-exp\-res\-sion} and {{\tt sorted-insert}\/} does not use
any structure mutation.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/hand-mostly-functional.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.3.8}

 \label{ex:bst-insert} 
Recall the definition of a binary tree from section~\ref{sec:bst}

  \begin{quote} A {\sl {binary tree}} ({{\tt BT}\/}) is either       \begin{enumerate}         \item {{\sf false}\/} or          \item {{\tt (make-node}\ {\tt soc}\ {\tt pn}\ {\tt lft}\ {\tt rgt)}\/} \\
           where {{\tt soc}\/} is a number, {{\tt pn}\/} is a symbol,           and {{\tt lft}\/} and {{\tt rgt}\/} are {{\tt BT}\/}s.        \end{enumerate} \end{quote}

\noindent where the required structure definition is 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt node}\  {\tt (ssn}\  {\tt name}\  {\tt left}\  {\tt right))}\end{tabbing}\end{minipage}\end{center}
A binary tree is a {\sl {binary search tree}} if every {{\tt node}\/}
structure contains a social security numbers that is larger than all those
in the left subtree and smaller than all those in the right subtree. 

Develop the function {{\tt insert-bst!}\/}. The function consumes a name
{{\tt n}\/}, a social security number {{\tt s}\/}, and a bst. It modifies the
bst so that it contains a new node with {{\tt n}\/} and {{\tt s}\/} while
maintaining it as a search tree.

Also develop the function {{\tt remove-bst!}\/}, which removes a node with a
given social security number. It combines the two subtrees of the removed
node by inserting all the nodes from the right tree into the left
one.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/bst-insert.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


The discussion in this subsection and the exercises suggest that adding or
removing items from linked structures is a messy task.  Dealing with an
item in the middle of the linked structures is best done with
accumulator-style functions. Dealing with the first structure requires
encapsulation and management functions. In contrast, as
exercise~\ref{ex:hand-mostly-functional} shows, a solution without mutators
is much easier to produce than a solution based on structure mutation. And
in the case of cards and hands, which deals with at most 52 structures, is
equally efficient. To decide which of the two approaches to use requires a
better understanding of algorithmic analysis (see intermezzo~5) and of the
language mechanisms and program design recipes for encapsulating state
variables.

\subsection{Extended Exercise: Moving Pictures, a Last Time} \label{sec:move-final}

In sections~\ref{sec:move-circle}, \ref{sec:move-shape},
\ref{sec:move-fig}, and~\ref{sec:abs-moving} we studied how to move
pictures across a canvas. A picture is a list of shapes; a shape is one of
several basic geometric shapes: circles, rectangles, etc. Following our
most basic design principle---one function per concept---we first defined
functions for moving basic geometric shapes, then for mixed classes of
shapes, and finally for lists of shapes. Eventually we abstracted over
related functions.

The functions for moving basic shapes create a new shape from an existing
shape. For example, the function for moving a circle consumes a
{{\tt circle}\/} structure and produces a new {{\tt circle}\/} structure. If
we think of the {{\tt circle}\/} as a painting with a round frame and the
canvas as a wall, however, creating a new shape for each move is
inappropriate. Instead, we should change the shape's current position.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 41.4.1}

 \label{ex:mu-picture1} 
Adapt the functions {{\tt translate-circle}\/} and
{{\tt translate-rectangle}\/} of exercises~\ref{ex:circ-draw}
and~\ref{ex:rect-draw}, respectively, so that they mutate their inputs
instead of creating new geometric shapes.

Adapt {{\tt move-circle}\/}, defined in section~\ref{sec:move-circle}, and
{{\tt move-rectangle}\/} from exercise~\ref{ex:rect-move} so that they use
these new functions.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mu-picture1.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.4.2}

 \label{ex:mu-picture2} 
Adapt the function {{\tt move-picture}\/} from exercise~\ref{ex:moving} to 
use the structure-mutating functions from exercise~\ref{ex:mu-picture1}. 

\noindent{\bf Hint:} \ Timing effects is critical in this exercise.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mu-picture2.html}{\rule{3pt}{5pt}}


{\bf Exercise 41.4.3}

 \label{ex:mu-picture3} 
Use Scheme's {{\tt for-each}\/} function to abstract where possible in the
functions of exercise~\ref{ex:mu-picture2}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/mu-picture3.html}{\rule{3pt}{5pt}}



\section{Equality}

\htmladdimg{../icons/teacher.gif}~{\em This section is as much about the connection between philosophy and computing as it is about programming. Students often figure out the basic idea, that there are two notions of equality, on their own during the discussion of structure or vector mutation. If so, this section can be skipped.}

As we mutate structures or vectors, we use words such as ``the vector now
contains {{\tt false}\/} in its first field'' to describe what
happens. Behind those words is the idea that the vector stays the
same---even though it changes. What this observation suggests is that there
are really two notions of equality: the one we have used so far and a new
one based on effects on a structure or vector. Understanding these two
notions of equality is critically important for a programmer. We therefore
discuss them in detail in the following two subsections. 

\subsection{Extensional Equality}

Recall the class of {{\tt posn}\/} structures from part~\ref{part:basic}. 
A {{\tt posn}\/} combines two numbers; its fields are called {{\tt x}\/} and
{{\tt y}\/}. Here are two examples: 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-posn}\  {\tt 3}\  {\tt 4)}\end{tabbing}\end{minipage}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-posn}\  {\tt 8}\  {\tt 6)}\end{tabbing}\end{minipage}
\end{center}
They are obviously distinct. In contrast, the following two 
\begin{center}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-posn}\  {\tt 12}\  {\tt 1)}\end{tabbing}\end{minipage}
\begin{minipage}[t]{8in}\begin{tabbing}{\tt (make-posn}\  {\tt 12}\  {\tt 1)}\end{tabbing}\end{minipage}
\end{center}
are equal. They both contain {{\tt 12}\/} in the {{\tt x}\/}-field
and {{\tt 1}\/} in the {{\tt y}\/}-field.

More generally, we consider two structures as equal if they contain equal
components.  This assumes that we know how to compare the components, but
that's not surprising. It just reminds us that processing structures
follows the data definition that comes with the structure
definition. Philosophers refer to this notion of equality as
{\sc {extensional equality}}.

Section~\ref{sec:equal-test} introduced extensional equality and discussed
its use for building tests. As a reminder, let's consider a function for
determining the extensional equality of {{\tt posn}\/} structures:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt equal-posn\char'077}\ {\tt :}\ {\tt posn}\ {\tt posn}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether two {{\tt posn}\/}s are extensionally equal }
\\
{\tt (d}\={\tt efine}\  {\tt (equal-posn}\  {\tt p1}\  {\tt p2)}
\\
\>{\tt (and}\  \={\tt (=}\  {\tt (posn-x}\  {\tt p1)}\  {\tt (posn-x}\  {\tt p2))}
\\
\>\>{\tt (=}\  {\tt (posn-y}\  {\tt p1)}\  {\tt (posn-y}\  {\tt p2))))}
\end{tabbing}\end{minipage}\end{center}
The function consumes two {{\tt posn}\/} structures, extracts their field
values, and then compares the corresponding field values using {{\tt =}\/},
the predicate for comparing numbers. Its organization matches that of the
data definition for {{\tt posn}\/} structures; its design is standard. This
implies that for recursive classes of data, we naturally need recursive
equality functions. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 42.1.1}

 \label{ex:eq-extensional1}
Develop an extensional equality function for the class of {{\tt child}\/}
structures from exercise~\ref{ex:ft-ss1}. If {{\tt ft1}\/} and {{\tt ft2}\/}
are family tree nodes, how long is the maximal abstract running time of the
function?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/eq-extensional1.html}{\rule{3pt}{5pt}}


{\bf Exercise 42.1.2}

 \label{ex:eq-extensional2}
Use exercise~\ref{ex:eq-extensional1} to abstract {{\tt equal-posn}\/} so
that its instances can test the intensional equality of any given class of
structures.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/eq-extensional2.html}{\rule{3pt}{5pt}} 


\subsection{Intensional Equality}

Consider the following toy program: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt a}\  {\tt (make-posn}\  {\tt 12}\  {\tt 1))}\\
{\tt (define}\  {\tt b}\  {\tt (make-posn}\  {\tt 12}\  {\tt 1))}
\\
{\tt (b}\={\tt egin}
\\
\>{\tt (set-posn-x!}\  {\tt a}\  {\tt 1)}
\\
\>{\tt (equal-posn}\  {\tt a}\  {\tt b))}
\end{tabbing}\end{minipage}\end{center}
It defines two {{\tt posn}\/} structures. The two structures are initially
equal in the sense of the preceding subsection. Yet when we evaluate the
{\bf begin}-exp\-res\-sion, the result is {{\tt false}\/}.

Even though the two structures initially consist of the same values, they
are different because the structure mutation in the {\bf begin}-exp\-res\-sion\ changes the
{{\tt x}\/}-field of the first structure and leaves the second one
alone. More generally, the expression has an effect on one structure but
not the other.  Now take a look at a slightly different program:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define}\  {\tt a}\  {\tt (make-posn}\  {\tt 12}\  {\tt 1))}\\
{\tt (define}\  {\tt b}\  {\tt a)}
\\
{\tt (b}\={\tt egin}
\\
\>{\tt (set-posn-x!}\  {\tt a}\  {\tt 1)}
\\
\>{\tt (equal-posn}\  {\tt a}\  {\tt b))}
\end{tabbing}\end{minipage}\end{center}
Here {{\tt a}\/} and {{\tt b}\/} are two different names for the same
structure. Therefore, the evaluation of {{\tt (set-posn-x!}\ {\tt a}\ {\tt 1)}\/} affects
both {{\tt a}\/} and {{\tt b}\/}, which means that {{\tt (equal-posn}\ {\tt a}\ {\tt b)}\/}
is going to yield {{\tt true}\/} this time. 

The two observations have a general moral. If the evaluation of an
expression affects one structure and simultaneously some other structure,
the two structures are equal in a deeper sense than {{\tt equal-posn}\/} can
determine. Philosophers refer to this notion of equality as
{\sc {intensional equality}}. In contrast to extensional equality, this
notion of equality requires not only that two structures consist of equal
parts, but that they also simultaneously react to structure mutations. It
is a direct consequence that two intensionally equal structures are also
extensionally equal.

Designing a function for determining the intensional equality of structures
is more work than designing one for determining their extensional
equality. We start with a precise description:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt eq-posn\char'077}\ {\tt :}\ {\tt posn}\ {\tt posn}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether two {{\tt posn}\/} structures }
\\
\hbox{;; are affected by the same mutation }
\\
{\tt (define}\  {\tt (equal-posn}\  {\tt p1}\  {\tt p2)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
We already have an example, so we move on to a discussion of the template:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (eq-posn}\  {\tt p1}\  {\tt p2)}\\
\>{\tt ...}\  {\tt (posn-x}\  {\tt p1)}\  {\tt ...}\  {\tt (posn-x}\  {\tt p2)}\  {\tt ...}
\\
\>{\tt ...}\  {\tt (posn-y}\  {\tt p1)}\  {\tt ...}\  {\tt (posn-y}\  {\tt p2)}\  {\tt ...}\  {\tt )}
\end{tabbing}\end{minipage}\end{center}
The template contains of four expressions, each one reminding us of the
available information and which structure fields we can mutate.

Translating the above observations into a full definition yields the
following draft: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (equal-posn}\  {\tt p1}\  {\tt p2)}\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set-posn-x!}\  {\tt p1}\  {\tt 5)}
\\
\>\>{\tt (=}\  {\tt (posn-x}\  {\tt p2)}\  {\tt 5)))}
\end{tabbing}\end{minipage}\end{center}
This function sets {{\tt p1}\/}'s {{\tt x}\/}-field to {{\tt 5}\/} and then
checks whether {{\tt p2}\/}'s {{\tt x}\/}-field also became {{\tt 5}\/}. If
so, both structures reacted to the mutation and are, by definition,
intensionally equal. 

Unfortunately, our reasoning has a problem. Consider the following
application:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (eq-posn}\  {\tt (make-posn}\  {\tt 1}\  {\tt 2)}\  {\tt (make-posn}\  {\tt 5}\  {\tt 6))}\end{tabbing}\end{minipage}\end{center}
The two {{\tt posn}\/}'s aren't even extensionally equivalent, so they
should not be intensionally equivalent. But our first version of
{{\tt eq-posn}\/} would produce {{\tt true}\/}, and that is a problem. 

We can improve the first version with a second mutation:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (equal-posn}\  {\tt p1}\  {\tt p2)}\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (set-posn-x!}\  {\tt p1}\  {\tt 5)}
\\
\>\>{\tt (set-posn-x!}\  {\tt p2}\  {\tt 6)}
\\
\>\>{\tt (=}\  {\tt (posn-x}\  {\tt p1)}\  {\tt 6)))}
\end{tabbing}\end{minipage}\end{center}
This function changes {{\tt p1}\/} and then {{\tt p2}\/}. If the structures
are intensionally equal, then the mutation of {{\tt p2}\/} must affect
{{\tt p1}\/}. Furthermore, we know that {{\tt p1}\/}'s {{\tt x}\/}-field can't
be coincidentally contain {{\tt 6}\/}, because we first changed it
to~{{\tt 5}\/}. Thus, when {{\tt (equal-posn}\ {\tt a}\ {\tt b)}\/} produces {{\tt true}\/},
{{\tt a}\/} changes when {{\tt b}\/} changes and {\em vice versa\/}, and the
structures are intensionally equal.

The only problem left now is that {{\tt equal-posn}\/} has effects on the
two structures that it consumes but has no effect statement. Indeed, it
should not have a visible effect because its only purpose is to determine
whether two structures are intensionally equal. We can avoid this effect by
first saving the old values in {{\tt p1}\/}'s and {{\tt p2}\/}'s {{\tt x}\/}
fields, mutating the fields, and then restoring the old
values. Figure~\ref{fig:eq} contains a function definition that performs an
intensional equality check without any visible effects.

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt eq-posn\char'077}\ {\tt :}\ {\tt posn}\ {\tt posn}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether two {{\tt posn}\/} structures }
\\
\hbox{;; are affected by the same mutation }
\\
{\tt (d}\={\tt efine}\  {\tt (equal-posn}\  {\tt p1}\  {\tt p2)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; save old {{\tt x}\/} values of {{\tt p1}\/} and {{\tt p2}\/}}
\\
\>\>\>{\tt (define}\  {\tt old-x1}\  {\tt (posn-x}\  {\tt p1))}
\\
\>\>\>{\tt (define}\  {\tt old-x2}\  {\tt (posn-x}\  {\tt p2))}
\\
\>\>\>\hbox{;; modify both {{\tt x}\/} fields of {{\tt p1}\/} and {{\tt p2}\/}}
\\
\>\>\>{\tt (define}\  {\tt effect1}\  {\tt (set-posn-x!}\  {\tt p1}\  {\tt 5))}
\\
\>\>\>{\tt (define}\  {\tt effect2}\  {\tt (set-posn-x!}\  {\tt p2}\  {\tt 6))}
\\
\>\>\>\hbox{;; now compare the two fields}
\\
\>\>\>{\tt (define}\  {\tt same}\  {\tt (=}\  {\tt (posn-x}\  {\tt p1)}\  {\tt (posn-x}\  {\tt p2)))}
\\
\>\>\>\hbox{;; restore old values}
\\
\>\>\>{\tt (define}\  {\tt effect3}\  {\tt (set-posn-x!}\  {\tt p1}\  {\tt old-x1))}
\\
\>\>\>{\tt (define}\  {\tt effect4}\  {\tt (set-posn-x!}\  {\tt p2}\  {\tt old-x2)))}
\\
\>\>{\tt same))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Determining the intensional equality of two structures} \label{fig:eq}
\hrule

The existence of {{\tt eq-posn\char'077}\/} says that all structures have a unique
``finger print''. We can inspect two structures (of the same class) for
this finger print if we have access to the mutators. Scheme and many other
languages typically provide built-in functions for comparing two structural
values extensionally and intensionally. The corresponding functions in
Scheme are {{\tt equal?}\/} and {{\tt eq?}\/}. As a matter of fact, in
Scheme, both functions are applicable to all values, whether mutators and
selectors are accessible or hidden. Still, programmers should use equality 
functions that indicate what kind of values they expect to compare, such as
{{\tt symbol=?}\/}, {{\tt boolean?}\/}, or {{\tt =}\/}, because the additional
information helps readers understand the purpose of the program more
easily. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 42.2.1}

 \label{ex:eq1}
Evaluate the following expressions by hand: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{$1.$}\  {\tt (eq-posn\char'077}\  {\tt (make-posn}\  {\tt 1}\  {\tt 2)}\  {\tt (make-posn}\  {\tt 1}\  {\tt 2))}\\
\hbox{$2.$}\  {\tt (local}\  \={\tt ((define}\  {\tt p}\  {\tt (make-posn}\  {\tt 1}\  {\tt 2)))}
\\
\>{\tt (eq-posn\char'077}\  {\tt p}\  {\tt p))}
\\
\hbox{$3.$}\  {\tt (local}\  \={\tt ((def}\={\tt ine}\  {\tt p}\  {\tt (make-posn}\  {\tt 1}\  {\tt 2))}
\\
\>\>{\tt (define}\  {\tt a}\  {\tt (list}\  {\tt p)))}
\\
\>{\tt (eq-posn\char'077}\  {\tt (first}\  {\tt a)}\  {\tt p))}
\end{tabbing}\end{minipage}\end{center}
Check the answers with DrScheme.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/eq1.html}{\rule{3pt}{5pt}}


{\bf Exercise 42.2.2}

 \label{ex:eq-intensional1}
Develop an intensional equality function for the class of {{\tt child}\/}
structures from exercise~\ref{ex:ft-ss1}. If {{\tt ft1}\/} and {{\tt ft2}\/}
are family tree nodes, how long is the maximal abstract running time of the
function?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/eq-intensional1.html}{\rule{3pt}{5pt}}


{\bf Exercise 42.2.3}

 \label{ex:eq-intensional2}
Use exercise~\ref{ex:eq-intensional1} to abstract {{\tt eq-posn}\/} so that
its instances can test the intensional equality of any given class of
structures.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/eq-intensional2.html}{\rule{3pt}{5pt}}


\section{Changing Structures, Vectors, and Objects}

This section introduces several small projects on programming with mutable
structures. The ordering of the subsections roughly matches the outline of
the book, proceeding from simple classes of data to complicated ones and
from structural recursion to generative recursion with backtracking and
accumulators.

\subsection{More Practice with Vectors}

Programming with mutable vectors is hardly ever needed in the kinds of
programs that we encountered. Still, because it is far more prevelant in
conventional languages, it is an important skill and deserves more practice
than section~\ref{sec:design-mut-ex1} suggests. This section covers 
sorting with vectors, but its goal is to practice reasoning about intervals
when processing vectors. 

We encountered the idea of sorting as early as in section~\ref{sec:sortI},
where we designed the {{\tt sort}\/} function. It consumes a list of numbers
and produces a list of numbers with the same items in sorted (ascending or
descending) order. An analogous function for vectors consumes a vector and
produces a new vector. But, using vector mutation, we can also design a
function that changes the vector so that it contains the same items as
before, in a sorted order. Such a function is called an {\sl in-place sort} because it leaves all the items inside the existing vector.

An in-place-sort function exclusively relies on effects on its input vector
to accomplish its task:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt in-place-sort}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to modify {{\tt V}\/} such that it contains the same items }
\\
\hbox{;; as before but in ascending order }
\\
{\tt (define}\  {\tt (in-place-sort}\  {\tt V)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Examples must demonstrate the effect: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt v1}\  {\tt (vector}\  {\tt 7}\  {\tt 3}\  {\tt 0}\  {\tt 4}\  {\tt 1)))}\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (in-place-sort}\  {\tt v1)}
\\
\>\>{\tt (equal?}\  {\tt v1}\  {\tt (vector}\  {\tt 0}\  {\tt 1}\  {\tt 3}\  {\tt 4}\  {\tt 7))))}
\end{tabbing}\end{minipage}\end{center}
Of course, given that {{\tt in-place-sort}\/} consumes a vector, the true
problem is to design the auxiliary function that works on specific
segments of the vector. 

The standard template for a vector-processing function uses an auxiliary
function:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (in-place-sort}\  {\tt V)}\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt (sort-aux}\  {\tt V}\  {\tt i)}
\\
\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt i)}\  {\tt ...\char'135}
\\
\>\>\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>\>\>{\tt ...}\  {\tt (vector-ref}\  {\tt V}\  {\tt (sub1}\  {\tt i))}\  {\tt ...}
\\
\>\>\>\>\>{\tt ...}\  {\tt (sort-aux}\  {\tt V}\  {\tt (sub1}\  {\tt i))}\  {\tt ...\char'135}{\tt )))}
\\
\>\>{\tt (sort-aux}\  {\tt V}\  {\tt (vector-length}\  {\tt V))))}
\end{tabbing}\end{minipage}\end{center}
Following the design ideas of intermezzo~5, the auxiliary function consumes
a natural number and uses it as an index into the vector. Because the
initial argument is {{\tt (vector-length}\ {\tt V)}\/}, the accessible index is
always {{\tt (sub1}\ {\tt i)}\/}. 

Recall that the key to designing functions such as {{\tt sort-aux}\/} is to
formulate a rigorous purpose and/of effect statement. The statement must
clarify on which interval of the possible vector indices the function works
and what exactly it accomplishes. One natural effect statement follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt sort-aux}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\bf N}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to sort the interval [{{\tt 0}\/},{{\tt i}\/}) of {{\tt V}\/} in place }
\\
{\tt (define}\  {\tt (sort-aux}\  {\tt V}\  {\tt i)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
To understand this effect statement in the larger context, let's adapt our
original example:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt v1}\  {\tt (vector}\  {\tt 7}\  {\tt 3}\  {\tt 0}\  {\tt 4}\  {\tt 1)))}\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (sort-aux}\  {\tt v1}\  {\tt 5)}
\\
\>\>{\tt (equal?}\  {\tt v1}\  {\tt (vector}\  {\tt 0}\  {\tt 1}\  {\tt 3}\  {\tt 4}\  {\tt 7))))}
\end{tabbing}\end{minipage}\end{center}
If {{\tt sort-aux}\/} is applied to a vector's length, it should sort the entire
vector. This statement implies that if the first argument is less than the
vector's length only some initial segment of the vector is sorted:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt v1}\  {\tt (vector}\  {\tt 7}\  {\tt 3}\  {\tt 0}\  {\tt 4}\  {\tt 1)))}\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (sort-aux}\  {\tt v1}\  {\tt 4)}
\\
\>\>{\tt (equal?}\  {\tt v1}\  {\tt (vector}\  {\tt 0}\  {\tt 3}\  {\tt 4}\  {\tt 7}\  {\tt 1))))}
\end{tabbing}\end{minipage}\end{center}
In this particular example, the last number remains in its original
place, and only the first four vector items are sorted. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt in-place-sort}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to modify {{\tt V}\/} such that it contains the same items }
\\
\hbox{;; as before but in ascending order }
\\
{\tt (d}\={\tt efine}\  {\tt (in-place-sort}\  {\tt V)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt sort-aux}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\bf N}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\>\>\>\hbox{;; effect: to sort the interval [{{\tt 0}\/},{{\tt i}\/}) of {{\tt V}\/} in place }
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (sort-aux}\  {\tt V}\  {\tt i)}
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt i)}\  {\tt (}{\sf void}{\tt )\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (b}\={\tt egin}
\\
\>\>\>\>\>\>\hbox{;; sort the segment [{{\tt 0}\/},{{\tt (sub1}\ {\tt i)}\/}):}
\\
\>\>\>\>\>\>{\tt (sort-aux}\  {\tt V}\  {\tt (sub1}\  {\tt i))}
\\
\>\>\>\>\>\>\hbox{;; insert {{\tt (vector-ref}\ {\tt V}\ {\tt (sub1}\ {\tt i))}\/} into the segment }
\\
\>\>\>\>\>\>\hbox{;; [{{\tt 0}\/},{{\tt i}\/}) so that it becomes sorted''}
\\
\>\>\>\>\>\>{\tt (insert}\  {\tt (sub1}\  {\tt i)}\  {\tt V))\char'135}{\tt ))}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt insert}\ {\tt :}\ {\bf N}\ {\tt (vectorof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\>\>\>\hbox{;; to place the value in the {{\tt i}\/}-th into its proper place }
\\
\>\>\>\hbox{;; in the segement [{{\tt 0}\/},{{\tt i}\/}] of {{\tt V}\/}}
\\
\>\>\>\hbox{;; assume: the segment  [{{\tt 0}\/},{{\tt i}\/}) of {{\tt V}\/} is sorted}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (insert}\  {\tt i}\  {\tt V)}
\\
\>\>\>\>{\tt ...))}
\\
\>\>{\tt (sort-aux}\  {\tt V}\  {\tt (vector-length}\  {\tt V))))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: An {in-place} sort function: first part} \label{fig:in-place-sort1}
\hrule

Now we can analyze each case in the template of {{\tt sort-aux}\/}: 
\begin{enumerate}
\item If {{\tt i}\/} is {{\tt 0}\/}, the interval of the effect statement is
[{{\tt 0}\/},{{\tt 0}\/}). This means that the interval is empty and that the
function has nothing to do. 

\item The second clause in the template contains two expressions:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (vector-ref}\  {\tt V}\  {\tt (sub1}\  {\tt i))}\\
\hbox{;and}
\\
{\tt (sort-aux}\  {\tt V}\  {\tt (sub1}\  {\tt i))}
\end{tabbing}\end{minipage}\end{center}
The first reminds us that we can use the $i-1$-st field of {{\tt V}\/}; the
second one reminds us of the natural recursion. In this case, the natural
recursion sorts the interval [{{\tt 0}\/},{{\tt (sub1}\ {\tt i)}\/}). To finish the
task, we must insert the value of the $i-1$-st field into its proper place in
the interval [{{\tt 0}\/},{{\tt i}\/}).

The above examples make this case concrete. When we evaluate {{\tt (sort-aux}{ }{\tt v1}\ {\tt 4)}\/}, the number in the last field of {{\tt v1}\/} remains at its place. The
first four items in the vectors are: {{\tt 0}\/}, {{\tt 3}\/}, {{\tt 4}\/}, and
{{\tt 7}\/}. To sort the entire interval [{{\tt 0}\/},{{\tt 5}\/}), we must insert
{{\tt 1}\/}, which is {{\tt (vector-ref}\ {\tt V}\ {\tt (sub1}\ {\tt 5))}\/}, between {{\tt 0}\/} and
{{\tt 3}\/}.
\end{enumerate}
In short, the design of {{\tt in-place-sort}\/} follows the same pattern as
that of the function {{\tt sort}\/} in section~\ref{sec:sortI} up to this
point. For {{\tt sort}\/}, we also designed the main function only to find
out that we needed to design an auxiliary function for inserting one more
item into its proper place. 

Figure~\ref{fig:in-place-sort1} gathers what we have discussed about
{{\tt in-place-sort}\/} so far. It also includes a specification of
{{\tt insert}\/}, the second auxiliary function. To understand its effect
statement, we reformulate the example for the second clause of
{{\tt sort-aux}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt v1}\  {\tt (vector}\  {\tt 0}\  {\tt 3}\  {\tt 4}\  {\tt 7}\  {\tt 1)))}\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (insert}\  {\tt 4}\  {\tt v1)}
\\
\>\>{\tt (equal?}\  {\tt v1}\  {\tt (vector}\  {\tt 0}\  {\tt 1}\  {\tt 3}\  {\tt 4}\  {\tt 7))))}
\end{tabbing}\end{minipage}\end{center}
In this case, {{\tt insert}\/} moves {{\tt 1}\/} over three numbers: first
{{\tt 7}\/}, then {{\tt 4}\/}, and finally {{\tt 3}\/}. It stops when the next
number in the leftwards direction, that is, {{\tt 0}\/}, is smaller than the
number that is being inserted.

Let's look at a second example for {{\tt insert}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt v1}\  {\tt (vector}\  {\tt 7}\  {\tt 3}\  {\tt 0}\  {\tt 4}\  {\tt 1)))}\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (insert}\  {\tt 1}\  {\tt v1)}
\\
\>\>{\tt (equal?}\  {\tt v1}\  {\tt (vector}\  {\tt 3}\  {\tt 7}\  {\tt 0}\  {\tt 4}\  {\tt 1))))}
\end{tabbing}\end{minipage}\end{center}
Here the problem is to insert {{\tt 3}\/} into a segment that contains only
one number: {{\tt 7}\/}. This means that insert must swap the values in the
first two fields and must stop then, because {{\tt 3}\/} can't move any
further to the left. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (in-place-sort}\  {\tt V)}\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\=\hbox{;; {{\tt sort-aux}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\bf N}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\>\>\>\hbox{;; effect: to sort the interval [{{\tt 0}\/},{{\tt i}\/}) of {{\tt V}\/} in place }
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (sort-aux}\  {\tt V}\  {\tt i)}
\\
\>\>\>\>{\tt ...)}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt insert}\ {\tt :}\ {\bf N}\ {\tt (vectorof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\>\>\>\hbox{;; to place the value in the {{\tt i}\/}-th into its proper place }
\\
\>\>\>\hbox{;; in the [{{\tt 0}\/},{{\tt i}\/}] segement of {{\tt V}\/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (insert}\  {\tt i}\  {\tt V)}\  
\\
\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>{\tt \char'133}{\tt (zero?}\  {\tt i)}\  {\tt (}{\sf void}{\tt )\char'135}
\\
\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>{\tt \char'133}\={\tt (>}\  {\tt (vector-ref}\  {\tt V}\  {\tt (sub1}\  {\tt i))}\  {\tt (vector-ref}\  {\tt V}\  {\tt i))}\  
\\
\>\>\>\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>\>\>\>{\tt (swap}\  {\tt V}\  {\tt (-}\  {\tt i}\  {\tt 1)}\  {\tt i)}
\\
\>\>\>\>\>\>\>\>{\tt (insert}\  {\tt (sub1}\  {\tt i)}\  {\tt V))\char'135}
\\
\>\>\>\>\>\>{\tt \char'133}{\tt else}\  {\tt (}{\sf void}{\tt )\char'135}{\tt )\char'135}{\tt ))}
\\
\>\>\>\relax
\\
\>\>\>\hbox{;; {{\tt swap}\ {\tt :}\ {\tt (vectorof}\ {\tt X)}\ {\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\tt void}\ \/}}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (swap}\  {\tt V}\  {\tt i}\  {\tt j)}
\\
\>\>\>\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt temp}\  {\tt (vector-ref}\  {\tt V}\  {\tt i)))}
\\
\>\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>\>{\tt (vector-set!}\  {\tt V}\  {\tt i}\  {\tt (vector-ref}\  {\tt V}\  {\tt j))}
\\
\>\>\>\>\>\>{\tt (vector-set!}\  {\tt V}\  {\tt j}\  {\tt temp)))))}
\\
\>\>{\tt (sort-aux}\  {\tt V}\  {\tt (vector-length}\  {\tt V))))}
\end{tabbing}\end{minipage}\end{center}
\center{Figure: An {in-place} sort function: second part} \label{fig:in-place-sort2}
\hrule

Now take a look at the template for {{\tt insert}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (insert}\  {\tt i}\  {\tt V)}\  \\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt i)}\  {\tt ...\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt ...}\  {\tt (vector-ref}\  {\tt V}\  {\tt (sub1}\  {\tt i))}\  {\tt ...}
\\
\>\>\>{\tt ...}\  {\tt (insert}\  {\tt (sub1}\  {\tt i)}\  {\tt V)}\  {\tt ...}\  {\tt \char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
It is the standard template for a vector-processing auxiliary function.  As
usual we distinguish two cases:
\begin{enumerate}
\item If {{\tt i}\/} is {{\tt 0}\/}, the goal is to insert
{{\tt (vector-ref}\ {\tt V}\ {\tt 0)}\/} into the segment [{{\tt 0}\/},{{\tt 0}\/}]. Since
this interval contains only one number, {{\tt insert}\/} has accomplished
its task. 

\item If {{\tt i}\/} is positive, the template implies that we may consider
another item in {{\tt V}\/}, namely {{\tt (vector-ref}\ {\tt V}\ {\tt (sub1}\ {\tt i))}\/}, and that
we can perform a natural recursion. The immediate question is whether
{{\tt (vector-ref}\ {\tt V}\ {\tt (sub1}\ {\tt i))}\/} is smaller or larger than {{\tt (vector-ref}{ }{\tt V}\ {\tt i)}\/}, the item that is to be moved around. If so, {{\tt V}\/} is sorted on the
entire interval [{{\tt 0}\/},{{\tt i}\/}], because {{\tt V}\/} is sorted 
on  [{{\tt 0}\/},{{\tt i}\/}) by assumption. If not, the item at {{\tt i}\/} is
out of order still. 

The {\bf cond}-exp\-res\-sion\ that employs the necessary conditions is
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (c}\={\tt ond}\\
\>{\tt \char'133}{\tt (>}\  {\tt (vector-ref}\  {\tt V}\  {\tt (sub1}\  {\tt i))}\  {\tt (vector-ref}\  {\tt V}\  {\tt i))}\  {\tt ...\char'135}
\\
\>{\tt \char'133}{\tt (<=}\  {\tt (vector-ref}\  {\tt V}\  {\tt (sub1}\  {\tt i))}\  {\tt (vector-ref}\  {\tt V}\  {\tt i))}\  {\tt (}{\sf void}{\tt )\char'135}{\tt )}
\end{tabbing}\end{minipage}\end{center}
The second clause contains {{\tt (}{\sf void}{\tt )}\/} because nothing is left to do.  In
the first clause, {{\tt insert}\/} must---at a minimum---swap the values in the
two fields. That is, {{\tt insert}\/} must place {{\tt (vector-ref}\ {\tt V}\ {\tt i)}\/} into
field~{{\tt (sub1}\ {\tt i)}\/} and {{\tt (vector-ref}\ {\tt V}\ {\tt (sub1}\ {\tt i))}\/} into
field~{{\tt i}\/}. But even that may not be enough. After all, the value in the
{{\tt i}\/}-th field may have to wander over several fields as the first example
demonstarted. Fortunately, we can easily solve this problem with the natural
recursion, which inserts the {{\tt (vector-ref}\ {\tt V}\ {\tt (sub1}\ {\tt i))}\/} into its proper
place in [0,{{\tt (sub1}\ {\tt i)}\/}] after the swapping has taken place.
\end{enumerate}
Figure~\ref{fig:in-place-sort2} contains the complete definition of
{{\tt insert}\/} and {{\tt swap}\/}. This second function is responsible for
swaping the value of two fields.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 43.1.1}

 \label{ex:sort-ip1}
Test the auxiliary functions for {{\tt in-place-sort}\/} from
figures~\ref{fig:in-place-sort1} and~\ref{fig:in-place-sort2}.
Formulate the tests as boolean-valued expressions. 

Develop and test more examples for {{\tt in-place-sort}\/}. 

Integrate the pieces. Test the integrated function. Eliminate superflous
arguments from the auxiliary programs in the integrated definition, step by
step, testing the complete function after each step. Finally, change
{{\tt in-place-sort}\/} so that its result is the modified
vector.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sort-ip1.html}{\rule{3pt}{5pt}}





\begin{htmlonly}
\end{htmlonly}

\hrule

 \begin{rawhtml} <img src=../icons/sort-ip.gif alt="[in-place sort]"> \end{rawhtml}

\center{Figure: Inserting an item into a sorted vector segment} \label{fig:sort-in-place}

\hrule


{\bf Exercise 43.1.2}

 \label{ex:sort-ip2}
The {{\tt insert}\/} function of figure~\ref{fig:in-place-sort2} performs two
vector mutations for each time the function recurs. Each of these mutations
pushed {{\tt (vector-ref}\ {\tt V}\ {\tt i)}\/}, for the original value of {{\tt i}\/}, to the
left in {{\tt V}\/} until its proper place is found.

Figure~\ref{fig:sort-in-place} illustrates a slightly better solution. The
situation in the top row assumes that the values {{\tt a}\/}, {{\tt b}\/},
and {{\tt c}\/} are properly arranged, that is, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (<}\  {\tt a}\  {\tt b}\  {\tt ...}\  {\tt c)}\end{tabbing}\end{minipage}\end{center}
holds. Furthermore, {{\tt d}\/} is to be inserted and its place is between
{{\tt a}\/} and {{\tt b}\/}, that is, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (<}\  {\tt a}\  {\tt d}\  {\tt b)}\end{tabbing}\end{minipage}\end{center}
holds, too. The solution is to compare {{\tt d}\/} with all the items in $k+1$
through $i$ and to shift the items to the right if they are larger than
{{\tt d}\/}. Eventually, we find {{\tt a}\/} (or the left end of the vector) and
have a ``hole'' in the vector, where {{\tt d}\/} must be inserted.  (The hole
actually contains {{\tt b}\/}.) This situation is illustrated in the middle
row. The last one shows how {{\tt d}\/} is placed in between {{\tt a}\/} and
{{\tt b}\/}.

Develop a function {{\tt insert}\/} that implements its desired effect
according to this description. \noindent{\bf Hint:} \ The new function must consume {{\tt d}\/}
as an additional argument.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sort-ip2.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.1.3}

 \label{ex:sort-ip3}
For many other programs, we could swap the order of the subexpressions in
{{\bf begin}-exp\-res\-sion}s and still get a working program. Let's consider this idea for
{{\tt sort-aux}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \\
\hbox{;; {{\tt sort2-aux}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\bf N}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (sort2-aux}\  {\tt V}\  {\tt i)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (zero?}\  {\tt i)}\  {\tt (}{\sf void}{\tt )\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (b}\={\tt egin}
\\
\>\>\>{\tt (insert2}\  {\tt (sub1}\  {\tt i)}\  {\tt V)}
\\
\>\>\>{\tt (sort2-aux}\  {\tt V}\  {\tt (sub1}\  {\tt i)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The order implies that {{\tt sort2-aux}\/} first inserts the item from
{{\tt (sub1}\ {\tt i)}\/} into some already-sorted part of {{\tt V}\/} and then
sorts the remainder of {{\tt V}\/}. Here is a picture that illustrates the
situation graphically:





\begin{htmlonly}
\end{htmlonly}

 \begin{rawhtml} <blockquote> <blockquote>  <table> <tr>  <td colspan=4>&nbsp;</td>  <td align=center colspan=1><i>i</i>-1</td>  <td colspan=3>&nbsp;</td> </tr> <tr>  <td width=150 bgcolor=wine colspan=4>&nbsp;</td>  <td width=30 align=center bgcolor=beige colspan=1><i>a</i></td>  <td width=100 bgcolor=tan colspan=3>&nbsp;</td> </tr> <tr>  <td align=center colspan=4>left</td>  <td align=right colspan=1>&nbsp;</td>  <td align=center colspan=3>right</td> </tr> </table>  </blockquote> </blockquote> \end{rawhtml}

The depicted vector consists of three pieces: {{\tt a}\/}, the item in
field~{{\tt (sub1}\ {\tt i)}\/}, the left fragment, and the right fragment. The
questions are which of the two fragments is sorted and into which fragment
{{\tt a}\/} should be inserted. 

Considering that {{\tt sort2-aux}\/} decreases its first argument and thus
sweeps over the vector from right to left, the answers are that the right
fragment is initially empty and thus sorted in ascending order by default; 
the left fragment is still unordered; and {{\tt a}\/} must be inserted into 
its proper place in the right fragment. 

Develop a precise effect statement for {{\tt sort-aux}\/} based on these
observations. Then develop the function {{\tt insert2}\/} so that
{{\tt sort2-aux}\/} sorts vectors properly.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/sort-ip3.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


In section~\ref{sec:sortII}, we got to know {{\tt quicksort}\/}, a function
based on generative recursion. Given a list, {{\tt quicksort}\/} constructs
a sorted version in three steps: 
\begin{enumerate}
\item choose an item from the list, call it {{\tt pivot}\/}; 

\item create two sublists: one with all those items strictly smaller than
{{\tt pivot}\/}, another one with all those items strictly larger than
{{\tt pivot}\/};

\item sort each of the two sublists, using the same steps, and then append
the two lists with the pivot item in the middle. 
\end{enumerate}
It isn't difficult to see why the result is sorted, why it contains all the
items from the original list, and why the process stops. After all, at every
stage, the function removes at least one item from the list so that the two
sublists are shorter than the given one; eventually the list must be empty.


   
   



\begin{htmlonly}
\end{htmlonly}

\hrule

 \begin{rawhtml} <blockquote> <blockquote> <p>a vector fragment with pivot item <font color=red><i>p</i></font>: </p>  <table width=300> <tr> <td align=center><i>left</i></td> <td colspan=4>&nbsp;</td> <td><i>right</i></td> </tr> <tr> <td width=40 align=center bgcolor=red><i>p</i></td> <td align=center bgcolor=tan><i>sm-1</i></td> <td align=center bgcolor=beige><i>la-1</i></td> <td align=center bgcolor=tan><i>sm-2</i></td> <td align=center bgcolor=tan><i>sm-3</i></td> <td align=center bgcolor=beige><i>la-2</i></td> </tr> </table>  <br><br> <hr width=350 align=left>  <p>partitioning the vector fragment into two regions, separated by <font color=red><i>p</i></font></p>  <table width=300> <tr> <td align=center><i>left1</i></td> <td>&nbsp;</td> <td align=center><i>right1</i></td> <td>&nbsp;</td> <td align=center><i>left2</i></td> <td align=center><i>right2</i></td> </tr> <tr> <td align=center bgcolor=tan><i>sm-2</i></td> <td align=center bgcolor=tan><i>sm-1</i></td> <td align=center bgcolor=tan><i>sm-3</i></td> <td width=40 align=center  bgcolor=red><i>p</i></td> <td align=center bgcolor=beige><i>la-1</i></td> <td align=center bgcolor=beige><i>la-2</i></td> </tr> </table>  </blockquote> </blockquote>  <!-- img src=../icons/qsort-ip1.gif alt="[in-place quick sort]" --> \end{rawhtml}

\center{Figure: A partitioning step for in-place quick-sort} \label{fig:part-qsort}

\hrule



Figure~\ref{fig:part-qsort} illustrates how this idea can be adapted for an
in-place version that works on vectors. At each stage, the algorithm works
on a specific fragment of the vector. It picks the first item as the
{{\tt pivot}\/} item and rearranges the fragment so that all items smaller
than the pivot appear to the left of {{\tt pivot}\/} and all those items
larger than {{\tt pivot}\/} appear to its right. Then {{\tt quicksort}\/} is
used twice: once for the fragment between {{\tt left1}\/} and {{\tt right1}\/}
and another time for the fragment between {{\tt left2}\/} and
{{\tt right2}\/}. Because each of these two intervals is shorter than the
originally given interval, {{\tt quicksort}\/} eventually encounters the
empty interval and stops. After {{\tt quicksort}\/} has sorted each
fragment, there is nothing left to do; the partitioning arranged the vector
into fragments of ascending order. 

Here is the definition of {{\tt qsort}\/}, an in-place sorting algorithm for
vectors: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt qsort}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {}{\tt ->}{}\ {\tt (vectorof}\ {\tt number)}\/}}\\
\hbox{;; to sort {{\tt V}\/} in ascending order}
\\
\hbox{;; effect: to modify {{\tt V}\/} such that it contains the same items }
\\
\hbox{;; as before but in ascending order }
\\
{\tt (d}\={\tt efine}\  {\tt (qsort}\  {\tt V)}
\\
\>{\tt (qsort-aux}\  {\tt V}\  {\tt 0}\  {\tt (sub1}\  {\tt (vector-length}\  {\tt V))))}
\\
\hbox{;; {{\tt qsort-aux}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\tt N}\ {\tt N}\ {}{\tt ->}{}\ {\tt (vectorof}\ {\tt number)}\/}}
\\
\hbox{;; to sort {{\tt V}\/} in ascending order, between left and right}
\\
\hbox{;; effect: sort the interval [{{\tt left}\/},{{\tt right}\/}] of vector {{\tt V}\/}}
\\
\hbox{;; generative recursion}
\\
{\tt (d}\={\tt efine}\  {\tt (qsort-aux}\  {\tt V}\  {\tt left}\  {\tt right)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (>=}\  {\tt left}\  {\tt right)}\  {\tt V\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt new-pivot-position}\  {\tt (partition}\  {\tt V}\  {\tt left}\  {\tt right)))}
\\
\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (qsort-aux}\  {\tt V}\  {\tt left}\  {\tt (sub1}\  {\tt new-pivot-position))}
\\
\>\>\>\>{\tt (qsort-aux}\  {\tt V}\  {\tt (add1}\  {\tt new-pivot-position)}\  {\tt right)))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The main function's input is a vector, so it uses an auxiliary function to
do its job. As suggested above, the auxiliary function consumes the vector
and two boundaries. Each boundary is an index into the vector. Initially,
the boundaries are {{\tt 0}\/} and {{\tt (sub1}\ {\tt (vector-length}\ {\tt V))}\/}, which
means that {{\tt qsort-aux}\/} is to sort the entire vector. 

The definition of {{\tt qsort-aux}\/} closely follows the algoritm's
description. If {{\tt left}\/} and {{\tt right}\/} describe a boundary of
size~1 or less, its task is done. Otherwise, it partitions the
vector. Because the partitioning step is a separate complex process, it
requires a separate function. It must have both an effect and a result
proper, the new index for the pivot item, which is now at its proper
place. Given this index, {{\tt qsort-aux}\/} continues to sort {{\tt V}\/} on
the intervals [{{\tt left}\/},{{\tt (sub1}\ {\tt new-pivot-position)}\/}] and
[{{\tt (add1}\ {\tt new-pivot-position)}\/}, {{\tt right}\/}]. Both intervals are at
least one item shorter than the original, which is the termination argument
for {{\tt qsort-aux}\/}. 

Naturally, the key problem here is the partitioning step, which is
implemented by {{\tt partition}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt partition}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to determine the proper position {{\tt p}\/} of the pivot-item }
\\
\hbox{;; effect: rearrange the so that }
\\
\hbox{;; -- all items in {{\tt V}\/} in [{{\tt left}\/},{{\tt p}\/}) are smaller than the pivot item}
\\
\hbox{;; -- all items of {{\tt V}\/} in ({{\tt p}\/},{{\tt right}\/}] are larger than the pivot item}
\\
{\tt (define}\  {\tt (partition}\  {\tt V}\  {\tt left}\  {\tt right)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
For simplicity, we choose the leftmost item in the given interval as the
pivot item. The question is how {{\tt partition}\/} can accomplish its task, for
example, whether it is a function based on structural recursion or whether
it is based on generative recursion. Furthermore, if it is based on
generative recursion, the question is what the generative step
accomplishes. 





\begin{htmlonly}
\end{htmlonly}

\hrule

 \begin{rawhtml} <blockquote> <blockquote> <p>finding the swapping points for <i>partition</i>:  <table width=400> <tr> <td align=left colspan=5><i>left</i></td> <td><i>right</i></td> </tr> <tr> <td align=center bgcolor=red><i>p</i></td> <td align=center bgcolor=tan><i>sm-1</i></td> <td align=center bgcolor=beige><i>la-1</i></td> <td align=center bgcolor=tan><i>sm-2</i></td> <td align=center bgcolor=tan><i>sm-3</i></td> <td align=center bgcolor=beige><i>la-2</i></td> </tr> <tr> <td align=right colspan=3><i>new-left</i></td> <td align=right colspan=2><i>new-right</i></td> </tr> </table>  <br><br> <hr width=450 align=left>  <p>swapping the items and recuring on a new interval:</p>  <table width=400> <tr> <td align=right colspan=2><i>&nbsp;</i></td> <td align=center colspan=1><i>left</i></td> <td align=right colspan=1><i>&nbsp;</i></td> <td align=center colspan=1><i>right</i></td> </tr> <tr> <td align=center bgcolor=red><i>p</i></td> <td align=center bgcolor=tan><i>sm-1</i></td> <td align=center bgcolor=tan><i>sm-3</i></td> <td align=center bgcolor=tan><i>sm-2</i></td> <td align=center bgcolor=beige><i>la-1</i></td> <td align=center bgcolor=beige><i>la-2</i></td> </tr> </table>  <br><br> <hr width=450 align=left>  <p> stopping the generative recursion and clean-up:</p>  <table width=400> <tr> <td align=right colspan=2><i>&nbsp;</i></td> <td align=center colspan=1><i>left</i></td> <td align=right colspan=1><i>&nbsp;</i></td> <td align=center colspan=1><i>right</i></td> </tr> <tr> <td align=center bgcolor=red><i>p</i></td> <td align=center bgcolor=tan><i>sm-1</i></td> <td align=center bgcolor=tan><i>sm-3</i></td> <td align=center bgcolor=tan><i>sm-2</i></td> <td align=center bgcolor=beige><i>la-1</i></td> <td align=center bgcolor=beige><i>la-2</i></td> </tr> <tr> <td align=right colspan=3><i>&nbsp;</i></td> <td align=center colspan=1><i>new-right</i></td> <!-- td align=right colspan=1><i>&nbsp;</i></td --> <td align=center colspan=1><i>new-left</i></td> </tr> </table>  </blockquote> </blockquote> \end{rawhtml}

\center{Figure: A partitioning step for in-place quick-sort} \label{fig:part-swap}

\hrule


The best strategy is to consider an example and to see how the partitioning
step could be accomplished. The first example is a small vector with six
numbers: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (vector}\  {\tt 1.1}\  {\tt 0.75}\  {\tt 1.9}\  {\tt 0.35}\  {\tt 0.58}\  {\tt 2.2)}\end{tabbing}\end{minipage}\end{center}
The pivot's position is {{\tt 0}\/}; the pivot item is {{\tt 1.1}\/}. The
boundaries are {{\tt 0}\/} and {{\tt 5}\/}. One item, {{\tt 1.9}\/} is
obviously out of place. If we swap it with {{\tt 0.58}\/}, then the vector
is almost perfectly partitioned:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (vector}\  {\tt 1.1}\  {\tt 0.75}\  {\tt 0.58}\  {\tt 0.35}\  {\tt 1.9}\  {\tt 2.2)}\end{tabbing}\end{minipage}\end{center}
In this modified vector, the only item out of place is the pivot item
itself. 

Figure~\ref{fig:part-swap} illustrates the swapping process that we just
described. First, we must find two items to swap. To do that, we search
{{\tt V}\/} for the first item to the right of {{\tt left}\/} that is larger than
the pivot item. Analogously, we search {{\tt V}\/} for the first item to the
left of {{\tt right}\/} that is smaller than the pivot item. These searcges
yield two indices: {{\tt new-left}\/} and {{\tt new-right}\/}. Second, we swap
the items in fields {{\tt new-left}\/} and {{\tt new-right}\/}. The result is
that the item at {{\tt new-left}\/} is now smaller than the pivot item and the
one at {{\tt new-right}\/} is larger. Finally, we can continue the swapping
process with the new, smaller interval. When the first step yields values for
{{\tt new-left}\/} and {{\tt new-right}\/} that are out of order, as in the
bottom row of figure~\ref{fig:part-swap}, then we have a mostly partitioned
vector (fragment).

Working through this first example suggests that {{\tt partition}\/} is an
algorithm, that is, a function based on generative recursion. Following our
recipe, we must ask and answer four questions: 

\begin{enumerate}
    \item What is a trivially solvable problem? 

    \item What is a corresponding solution? 

    \item How do we generate new problems that are more easily solvable
      than the original problem? Is there one new problem that we generate
      or are there several?

    \item Is the solution of the given problem the same as the solution of (one
      of) the new problems? Or, do we need to perform an additional computation
      to combine these solutions before we have a final solution?  And, if so,
      do we need anything from the original problem data?

\end{enumerate} 
The example addressed issues~1, 3 and~4. The first step is to determine the
{{\tt new-left}\/} and {{\tt new-right}\/} indices. If {{\tt new-left}\/} is
smaller than {{\tt new-right}\/}, the generative work is to swap items in
the two fields. Then the process recurs with the two new boundaries. If
{{\tt new-left}\/} is larger than {{\tt new-right}\/}, the partitioning
process is finished with the exception of the placement of the pivot item;
placing the pivot item answers question~2. Assuming we can solve this
``trivially solvable'' problem, we also know that the overall problem is
solved 

Let's study question~2 with some examples. We stopped working on the first
example when the vector had been changed to 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (vector}\  {\tt 1.1}\  {\tt 0.75}\  {\tt 0.58}\  {\tt 0.35}\  {\tt 1.9}\  {\tt 2.2)}\end{tabbing}\end{minipage}\end{center}
and the interval had been narrowed down to [{{\tt 2}\/},{{\tt 4}\/}]. The
search for {{\tt new-left}\/} and {{\tt new-right}\/} now yields {{\tt 4}\/}
and {{\tt 3}\/}, respectively. That is, 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (<=}\  {\tt new-right}\  {\tt new-left)}\end{tabbing}\end{minipage}\end{center}
holds. Switching the item in field {{\tt new-right}\/} with the original
left-most boundary places the pivot item in the proper spot:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (vector}\  {\tt 0.35}\  {\tt 0.75}\  {\tt 0.58}\  \  {\tt 1.1}\  {\tt 1.9}\  {\tt 2.2)}\end{tabbing}\end{minipage}\end{center}
because {{\tt new-right}\/} points at the right-most item that is smaller
than the pivot item.

Before we accept this seemingly simple solution, we must check it with some
additional examples, especially examples where the pivot item is the
largest or smallest item in the vector fragment of interest. Here is one
such example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (vector}\  {\tt 1.1}\  {\tt 0.1}\  {\tt 0.5}\  {\tt 0.4)}\end{tabbing}\end{minipage}\end{center}
Assuming the initial interval is [{{\tt 0}\/},{{\tt 3}\/}], the pivot item is
{{\tt 1.1}\/}. Thus, all other items in the vector are smaller than the
pivot item, which means that it should end up in the right most position.

Our process clearly yields {{\tt 3}\/} for {{\tt new-right}\/}. After all,
{{\tt 0.4}\/} is smaller than pivot. The search for {{\tt new-left}\/},
though, works differently. Since none of the items in the vector are larger
than the pivot item, it eventually generates {{\tt 3}\/} as an index, which
is the largest legal index for this vector. At this point the search must
stop. Fortunately, {{\tt new-left}\/} and {{\tt new-right}\/} are equal at
this point, which implies that the partitioning process can stop and which
means that we can still swap the pivot item with the one in field
{{\tt new-right}\/}. If we do that, we get 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (vector}\  {\tt 0.4}\  {\tt 0.1}\  {\tt 0.5}\  {\tt 0.4}\  {\tt 1.1)}\end{tabbing}\end{minipage}\end{center}
which is a perfectly well-partitioned vector. 

The third example uses a vector whose items are all larger than the pivot
item: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (vector}\  {\tt 1.1}\  {\tt 1.2}\  {\tt 3.3}\  {\tt 2.4)}\end{tabbing}\end{minipage}\end{center}
In this case, the pivot item is already in the right spot, and the search
for {{\tt new-left}\/} and {{\tt new-right}\/} should discover this fact. On
one hand, the search for {{\tt new-left}\/} ends at field~1, which is the
first field that contains an item larger than the pivot item. On the other
hand, the search for {{\tt new-right}\/} ends with {{\tt 0}\/}, because it is
the smallest legal index and the search must stop there. As before, the
search for the swapping points suggests that the process must stop, and
{{\tt new-right}\/} once again points to that field in the vector that must
contain the pivot item for the vector (fragment) to be properly
partitioned.

In short, the examples suggest several things: 
\begin{enumerate}
\item The termination condition for {{\tt partition}\/} is {{\tt (<=}{ }{\tt new-right}\ {\tt new-left)}\/}.

\item The value of {{\tt new-right}\/} is the final position of the pivot
item, which is in the original left-most point of the interval of interest.
It is always acceptable to swap the contents of the two fields. 

\item The search for {{\tt new-right}\/} starts at the right-most boundary
and continues until it either finds an item that is smaller than the pivot
item or until it hits the left-most boundary. 

\item Dually, the search for {{\tt new-left}\/} starts at the left-most
boundary and continues until it either finds an item that is larger than
the pivot item or until it hits the right-most boundary.
\end{enumerate} 
And, the two searches are complex tasks that deserve their own function. 

We can now gradually translate our discussion into Scheme. First, the
partitioning process is a function of not just the vector and some
interval, but also of the original leftmost position of the vector and its
content. This suggests the use of {{\tt local}\/}ly defined functions and
variables: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (partition}\  {\tt V}\  {\tt left}\  {\tt right)}\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt pivot-position}\  {\tt left)}
\\
\>\>\>{\tt (define}\  {\tt the-pivot}\  {\tt (vector-ref}\  {\tt V}\  {\tt left))}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (partition-aux}\  {\tt left}\  {\tt right)}\  
\\
\>\>\>\>{\tt ...))}
\\
\>\>{\tt (partition-aux}\  {\tt left}\  {\tt right)))}
\end{tabbing}\end{minipage}\end{center}
The alternative is to use an auxiliary function that consumes the pivot's
original position in addition to the vector and the current interval. 

Second, the auxiliary function's consumes an interval's boundaries. It
immediately generates a new pair of indices from these boundaries:
{{\tt new-left}\/} and {{\tt new-right}\/}. As mentioned, the search for the
two new boundaries are complex tasks and deserve their own functions: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt find-new-right}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\tt number}\ {\bf N}\ {\bf N}\ {\tt \char'133}{\tt >=}\ {\tt left\char'135}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to determine an index {{\tt i}\/} between {{\tt right}\/} and {{\tt left}\/} (inclusive)}
\\
\hbox{;; such that {{\tt (<}\ {\tt (vector-ref}\ {\tt V}\ {\tt i)}\ {\tt the-pivot)}\/} holds}
\\
{\tt (define}\  {\tt (find-new-right}\  {\tt V}\  {\tt the-pivot}\  {\tt right}\  {\tt left)}\  {\tt ...)}
\\
\hbox{;; {{\tt find-new-left}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\tt number}\ {\bf N}\ {\bf N}\ {\tt \char'133}{\tt <=}\ {\tt right\char'135}\ {}{\tt ->}{}\ {\bf N}\/}}
\\
\hbox{;; to determine an index {{\tt i}\/} between {{\tt left}\/} and {{\tt right}\/} (inclusive)}
\\
\hbox{;; such that {{\tt (>}\ {\tt (vector-ref}\ {\tt V}\ {\tt i)}\ {\tt the-pivot)}\/} holds}
\\
{\tt (define}\  {\tt (find-new-left}\  {\tt V}\  {\tt the-pivot}\  {\tt left}\  {\tt right)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Using these two functions, {{\tt partition-aux}\/} can generate the new
boundaries: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (partition}\  {\tt V}\  {\tt left}\  {\tt right)}\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt pivot-position}\  {\tt left)}
\\
\>\>\>{\tt (define}\  {\tt the-pivot}\  {\tt (vector-ref}\  {\tt V}\  {\tt left))}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (partition-aux}\  {\tt left}\  {\tt right)}
\\
\>\>\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt new-right}\  {\tt (find-new-right}\  {\tt V}\  {\tt the-pivot}\  {\tt right}\  {\tt left))}
\\
\>\>\>\>\>\>{\tt (define}\  {\tt new-left}\  {\tt (find-new-left}\  {\tt V}\  {\tt the-pivot}\  {\tt left}\  {\tt right)))}
\\
\>\>\>\>\>{\tt ...}\  {\tt )))}
\\
\>\>{\tt (partition}\  {\tt left}\  {\tt right)))}
\end{tabbing}\end{minipage}\end{center}
Finally, from here the rest of the definition is a plain transliteration of
our discussion into Scheme. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt partition}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\bf N}\/}}\\
\hbox{;; to determine the proper position {{\tt p}\/} of the pivot-item }
\\
\hbox{;; effect: rearrange the so that }
\\
\hbox{;; -- all items in {{\tt V}\/} in [{{\tt left}\/},{{\tt p}\/}) are smaller than the pivot item}
\\
\hbox{;; -- all items of {{\tt V}\/} in ({{\tt p}\/},{{\tt right}\/}] are larger than the pivot item}
\\
\hbox{;; generative recursion}
\\
{\tt (d}\={\tt efine}\  {\tt (partition}\  {\tt V}\  {\tt left}\  {\tt right)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt pivot-position}\  {\tt left)}
\\
\>\>\>{\tt (define}\  {\tt the-pivot}\  {\tt (vector-ref}\  {\tt V}\  {\tt left))}
\\
\>\>\>{\tt (d}\={\tt efine}\  {\tt (partition-aux}\  {\tt left}\  {\tt right)}
\\
\>\>\>\>{\tt (l}\={\tt ocal}\  {\tt (}\={\tt (define}\  {\tt new-right}\  {\tt (find-new-right}\  {\tt V}\  {\tt the-pivot}\  {\tt right}\  {\tt left))}
\\
\>\>\>\>\>\>{\tt (define}\  {\tt new-left}\  {\tt (find-new-left}\  {\tt V}\  {\tt the-pivot}\  {\tt left}\  {\tt right)))}
\\
\>\>\>\>\>{\tt (c}\={\tt ond}
\\
\>\>\>\>\>\>{\tt \char'133}\={\tt (>=}\  {\tt new-left}\  {\tt new-right)}
\\
\>\>\>\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>\>\>\>{\tt (swap}\  {\tt V}\  {\tt pivot-position}\  {\tt new-right)}
\\
\>\>\>\>\>\>\>\>{\tt new-right)\char'135}
\\
\>\>\>\>\>\>{\tt \char'133}\={\tt else}\  \hbox{; {{\tt (<}\ {\tt new-left}\ {\tt new-right)}\/}}
\\
\>\>\>\>\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>\>\>\>\>{\tt (swap}\  {\tt V}\  {\tt new-left}\  {\tt new-right)}
\\
\>\>\>\>\>\>\>\>{\tt (partition-aux}\  {\tt new-left}\  {\tt new-right))\char'135}{\tt ))))}
\\
\>\>{\tt (partition}\  {\tt left}\  {\tt right)))}
\\
\hbox{;; {{\tt find-new-right}\ {\tt :}\ {\tt (vectorof}\ {\tt number)}\ {\tt number}\ {\bf N}\ {\bf N}\ {\tt \char'133}{\tt >=}\ {\tt left\char'135}\ {}{\tt ->}{}\ {\bf N}\/}}
\\
\hbox{;; to determine an index {{\tt i}\/} between {{\tt right}\/} and {{\tt left}\/} (inclusive)}
\\
\hbox{;; such that {{\tt (<}\ {\tt (vector-ref}\ {\tt V}\ {\tt i)}\ {\tt the-pivot)}\/} holds}
\\
\hbox{;; structural recursion: see text}
\\
{\tt (d}\={\tt efine}\  {\tt (find-new-right}\  {\tt V}\  {\tt the-pivot}\  {\tt left}\  {\tt right)}
\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (=}\  {\tt right}\  {\tt left)}\  {\tt right\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (<}\  {\tt (vector-ref}\  {\tt V}\  {\tt right)}\  {\tt the-pivot)}\  {\tt right\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (find-new-right}\  {\tt V}\  {\tt the-pivot}\  {\tt left}\  {\tt (sub1}\  {\tt right))\char'135}{\tt )\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Rearranging a vector fragment into two partitions} \label{fig:part-ip}




\hrule

Figure~\ref{fig:part-ip} contains the complete definition of
{{\tt partition}\/}, {{\tt partition-aux}\/}, and {{\tt find-new-right}\/}; the
function {{\tt swap}\/} is defined in figure~\ref{fig:in-place-sort2}. The
definition of the search function uses an unusual structural recursion based on
subclasses of natural numbers whose limits are parameters of the
function. Because the search functions are based on a rarely used design
recipe, it is best to design them separately. Still, they are only useful in
the context of {{\tt partition}\/}, which means that they should be integrated
into its definition when their design is completed. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 43.1.4}

 \label{ex:ip-qsort1} 
Complete the definition of {{\tt find-new-left}\/}. The two definitions have
the same structure; develop the common abstraction. 

Use the definitions of {{\tt find-new-right}\/} and {{\tt find-new-left}\/}
to provide a termination argument for {{\tt partition-aux}\/}.

Use the examples to develop tests for {{\tt partition}\/}. Recall that the
function computes the proper place for the pivot item and rearranges a
fragment of the vector. Formulate the tests as boolean-valued expressions. 

When the functions are properly tested, integrate {{\tt find-new-right}\/}
and {{\tt find-new-left}\/} into {{\tt partition}\/} and eliminate
superfluous parameters. 

Finally, test {{\tt qsort}\/} and produce a single function definition for
the in-place quick-sort algorithm.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ip-qsort1.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.1.5}

 \label{ex:ip-qsort3} 
Develop the function {{\tt vector-reverse!}\/}. It inverts the contents of a
vector; its result is the modified vector.

\noindent{\bf Hint:} \ Swap items from both ends until there are no more items to
swap.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ip-qsort3.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.1.6}

 \label{ex:ip-qsort4} 
Economists, meteorologists, and many others consistently measure various
things and obtain time series. All of them need to understand the idea of
``n-item averages'' or ``smoothing''.  Suppose we have weekly prices for
some basket of groceries: 
\begin{center}
\begin{tabular}{ccccc}
1.10 & 1.12 & 1.08 & 1.09 & 1.11 
\end{tabular}
\end{center}
Computing the corresponding three-item average time series proceeds as
follows: 
\begin{center}
\begin{picture}(150,80)(0,20)
\put(0,80){\begin{tabular}{ccccc}            1.10 & 1.12 & 1.08 & 1.09 & 1.11            \end{tabular}}
 
\put(5,75){\line(1,0){85}} 
\put(5,75){\line(0,1){5}} 
\put(90,75){\line(0,1){5}}
 
\put(47,45){\line(0,1){30}}
 
\put(40,25){\mbox{1.10}}
 
\put(35,70){\line(1,0){85}} 
\put(35,70){\line(0,1){10}} 
\put(120,70){\line(0,1){10}}
 
\put(77,45){\line(0,1){25}}
 
\put(68,25){$\frac{329}{300}$}
 
\put(65,65){\line(1,0){85}} 
\put(65,65){\line(0,1){15}} 
\put(150,65){\line(0,1){15}}
 
\put(105,45){\line(0,1){20}}
 
\put(96,25){$\frac{328}{300}$}
\end{picture}
\end{center}
There are no averages for the end points, which means a series with $k$
items turns into $k-2$ averages. 

Develop the function {{\tt list-3-average}\/}, which computes the 3-item
sliding averages of a list of numbers. That is, we represent a series of
grocesry prices with lists and {{\tt list-3-averages}\/} consumes a list
such as 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  {\tt 1.10}\  {\tt 1.10}\  {\tt 1.12}\  {\tt 1.08}\  {\tt 1.09}\  {\tt 1.11)}\end{tabbing}\end{minipage}\end{center}
and produces 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (list}\  {\tt 1.10}\  {\tt 329/300}\  {\tt 328/300)}\end{tabbing}\end{minipage}\end{center}
in return. 

Develop the function {{\it vector\/}$-${\tt 3-averages}\/}, which computes the 3-item
sliding averages of a vector of numbers. Since vectors are mutable, this gives
us the alternative of producing a new vector or of mutating the existing one.

Develop both versions of the function: one that produces a new vector and
another one that mutates the vector it is handed. 

Compare all three versions and the complexity of designing
them.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ip-qsort4.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.1.7}

 \label{ex:ip-qsort2}
All the examples in this section deal with vector fragments, that is,
intervals of natural numbers. Processing an interval requires a starting
point for an interval, an end point, and, as the definitions of
{{\tt find-new-right}\/} and {{\tt find-new-left}\/} show, a direction of
traversal. In addition, processing means applying some function to each
point in the interval.

Here is a function for processing intervals:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt for-interval}\ {\tt :}\ {\bf N}\ {\tt (}{\bf N}\ {}{\tt ->}{}\ {\bf N}{\tt )}\ {\tt (}{\bf N}\ {}{\tt ->}{}\ {\bf N}{\tt )}\ {\tt (}{\bf N}\ {}{\tt ->}{}\ {\tt X)}\ {}{\tt ->}{}\ {\tt X}\/}}\\
\hbox{;; to evaluate {{\tt (action}\ {\tt i}\ {\tt (vector-ref}\ {\tt V}\ {\tt i))}\/} for {{\tt i}\/}, {{\tt (step}\ {\tt i)}\/}, \ldots}
\\
\hbox{;; until {{\tt (end\char'077}\ {\tt i)}\/} holds (inclusive)}
\\
\hbox{;; generative recursion: {{\tt step}\/} generates new value, {{\tt end\char'077}\/} detects end}
\\
\hbox{;; termination is not guaranteed }
\\
{\tt (d}\={\tt efine}\  {\tt (for-interval}\  {\tt i}\  {\tt end\char'077}\  {\tt step}\  {\tt action)}
\\
\>{\tt (b}\={\tt egin}
\\
\>\>{\tt (action}\  {\tt i)}
\\
\>\>{\tt (c}\={\tt ond}
\\
\>\>\>{\tt \char'133}{\tt (end\char'077}\  {\tt i)}\  {\tt (action}\  {\tt i)\char'135}
\\
\>\>\>{\tt \char'133}{\tt else}\  {\tt (for-interval}\  {\tt (step}\  {\tt i)}\  {\tt end\char'077}\  {\tt step}\  {\tt action)\char'135}{\tt )))}
\end{tabbing}\end{minipage}\end{center}
It consumes a starting index, called {{\tt i}\/}, a function for determining
whether the end of the interval has been reached, a function that generates
the next index, and a function that is applied to each point in between.
Assuming {{\tt (end\char'077}\ {\tt (step}\ {\tt (step}\ {\tt ...}\ {\tt (step}\ {\tt i)}\ {\tt ...)))}\/} holds,
{{\tt for-interval}\/} satisfies the following equation:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (for-interval}\  {\tt i}\  {\tt end\char'077}\  {\tt step}\  {\tt action)}\\
{\tt =}\  {\tt (begin}\  \={\tt (action}\  {\tt i)}
\\
\>{\tt (action}\  {\tt (step}\  {\tt i))}
\\
\>{\tt ...}
\\
\>{\tt (action}\  {\tt (step}\  {\tt (step}\  {\tt ...}\  {\tt (step}\  {\tt i)}\  {\tt ...))))}
\end{tabbing}\end{minipage}\end{center}
Compare the function definition and the equation with those for
{{\tt map}\/}.

With {{\tt for-interval}\/} we can develop (some) functions on vectors without
the traditional detour through an auxiliary function. Instead, we use
{{\tt for-interval}\/} the way we used {{\tt map}\/} for processing each item on
a list. Here is a function that adds {{\tt 1}\/} to each vector field:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt increment-vec-rl}\ {\tt :}\ {\tt (vector}\ {\tt number)}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to increment each item in V by {{\tt 1}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (increment-vec-rl}\  {\tt V)}
\\
\>{\tt (for-interval}\  \={\tt (sub1}\  {\tt (vector-length}\  {\tt V))}\  {\tt zero?}\  {\tt sub1}\  
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt (i)}
\\
\>\>\>{\tt (vector-set!}\  {\tt V}\  {\tt i}\  {\tt (+}\  {\tt (vector-ref}\  {\tt V}\  {\tt i)}\  {\tt 1)))))}
\end{tabbing}\end{minipage}\end{center}
It processes the interval [{{\tt 0}\/},{{\tt (sub1}\ {\tt (vector-length}\ {\tt V))}\/}],
where the left boundary is determined by {{\tt zero?}\/}, the termination
test.  The starting point, however, is {{\tt (sub1}\ {\tt (vector-length}\ {\tt V))}\/},
which is the rightmost legal vector index. The third argument to
{{\tt for-interval}\/}, {{\tt sub1}\/}, determines the traversal direction,
which is from right to left, until the index is {{\tt 0}\/}. Finally, the
action is to mutate the contents of the {{\tt i}\/}-th field by adding
{{\tt 1}\/}. 

Here is a function with the same visible effect on vectors but a different
processing order:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt increment-vec-lr}\ {\tt :}\ {\tt (vector}\ {\tt number)}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to increment each item in V by {{\tt 1}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (increment-vec-lr}\  {\tt V)}
\\
\>{\tt (for-interval}\  \={\tt 0}\  {\tt (lambda}\  {\tt (i)}\  {\tt (=}\  {\tt (sub1}\  {\tt (vector-length}\  {\tt V))}\  {\tt i))}\  {\tt add1}\  
\\
\>\>{\tt (l}\={\tt ambda}\  {\tt (i)}
\\
\>\>\>{\tt (vector-set!}\  {\tt V}\  {\tt i}\  {\tt (+}\  {\tt (vector-ref}\  {\tt V}\  {\tt i)}\  {\tt 1)))))}
\end{tabbing}\end{minipage}\end{center}
Its starting point is {{\tt 0}\/} and the end point is the rightmost legal
index of {{\tt V}\/}. The {{\tt add1}\/} function determines that the vector
is processed from left to right. 

Develop the following functions, using {{\tt for-interval}\/}: 
\begin{enumerate}
\item {{\tt rotate-left}\/}, which moves all items in vector into the
adjacent field to the left, except for the first item, which moves to the
last field; 

\item {{\tt insert-i-j}\/}, which moves all items between two indices
{{\tt i}\/} and {{\tt j}\/} to the right, except for the rightmost one, which
gets inserted into the {{\tt i}\/}-th field
(cmp. figure~\ref{fig:sort-in-place}); 

\item {{\tt vector-reverse!}\/}, which swaps the left half of a vector with
its right half;

\item {{\tt find-new-right}\/}, that is, an alternative to the definition in
figure~\ref{fig:part-ip};

\item {{\tt vector-sum!}\/}, which computes the sum of the numbers in a vector
using {{\tt set!}\/} (\noindent{\bf Hint:} \ see section~\ref{sec:repeat-state}).

\end{enumerate}
The last two tasks show that {{\tt for-interval}\/} is useful for
computations that have no visible effects. Of course,
exercise~\ref{sec:vector1} shows that there is no need for a clumsy
formulation such as {{\tt vector-sum!}\/}. 

Which of these functions can be defined in terms of {{\tt vec-for-all}\/}
from exercise~\ref{ex:abs-vec}?

\medskip\noindent{\bf Looping Constructs}:\  Many programming languages (must) provide
functions like {{\tt for-interval}\/} as built-in constructs, and force
programmers to use them for processing vectors. As a result, many more
programs than necessary use {{\tt set!}\/} and require complex temporal
reasoning.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ip-qsort2.html}{\rule{3pt}{5pt}}


\subsection{Collections of Structures with Cycles}
\label{sec:design-mut-ex3} 

Many objects in our world are related with each other in a circular
manner. We have parents; our parents have children. A computer may connect
to another computer, which in turn may connect to the first. And we have
seen data definitions that refer to each other. 

Since data represents information about real-world objects, we will encounter
situations that call for the design of a class of structures with a circular
relationship. In the past, we have skirted the issue, or we used a trick to
represent such collections. The trick is to use an indirection. For example, in
section~\ref{sec:traverse-graph1}, we associated each structure with a symbol,
kept a table of symbols and structures around, and placed symbols into
structures. Then, when we needed to find out whether some structure refers to
another, we extracted the relevant symbol and looked in the table to find the
structure for the symbol. While this use of indirection allows us to represent
structures with mutual references or structures in a cyclic relationship, it
also leads to awkward data representations and programs. This section
demonstrates that we can simplify the representation of collections with
structure mutation.

To make this idea concrete, we discuss two examples: family trees and
simple graphs. Consider the case of family trees. Thus far, we have used
two kinds of family trees to record family relationships. The first is the
ancestor tree; it relates people to their parents, grandparents, and so
on. The second is the descendant tree; it relates people to their children,
grandchildren, and so on. In other words, we have avoided the step of
combining the two family trees into one, the way it is done in the real
world. The reason for skirting the joint representation is also
clear. Translated into our data language, a joint tree requires that a
structure for a father should contain the structures for his children, and
each of the child structures should contain the father structure. In the
past, we couldn't create such collections of structures. With structure
mutations, we can now create them. 

Here is structure definition that makes this discussion concrete: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt person}\  {\tt (name}\  {\tt social}\  {\tt father}\  {\tt mother}\  {\tt children))}\end{tabbing}\end{minipage}\end{center}
The goal is to create family trees that consist of {{\tt person}\/}
structures. A person structure has five fields. The contents of each is
specified by the following data definition:

\begin{quote} An {\sl {family tree node}} ({{\tt ftn}\/}) is either      \begin{enumerate}       \item {{\tt false}\/} or         \item a {{\tt person}\/}.      \end{enumerate}      A {\sl {person}} is a structure: \\
        \centerline{{{\tt (make-person}\ {\tt n}\ {\tt s}\ {\tt f}\ {\tt m}\ {\tt c)}\/}}         where {{\tt n}\/} is a symbol, {{\tt s}\/} is number, {{\tt f}\/} and        {{\tt m}\/} are {{\tt ftn}\/}s, and {{\tt c}\/} is a {{\tt (listof}\ {\tt person)}\/}.      \end{quote}

\noindent As usual, the {{\tt false}\/} in the definition of {{\tt family}{ }{\tt tree}\ {\tt node}\/}s represents missing information about a portion of the family
tree.\footnote{The data definition uses the abbreviation {{\tt (listof}{ }{\tt X)}\/} to denote a list of items in class {{\tt X}\/}, but we could also expand this abbreviation without changing the rest of the program design.}

Using {{\tt make-person}\/} alone, we cannot establish the mutual reference
between a family tree node for a father and his child. Suppose we follow an
ancestoral tree strategy, that is, we create the structure for the father
first. Then we can't add any child to the {{\tt children}\/} field, because, by
assumption, the corresponding structure doesn't exist yet. Conversely, if we
follow a descendant tree strategy, we first create a structure for all of a
father's children but those structures can't contain any information about the
father yet.






\begin{htmlonly}
\end{htmlonly}

\hrule

 \begin{rawhtml} <center>   <img src=../icons/ftn-circ.gif alt="bidirectional family trees"   align=center> </center> \end{rawhtml}

\center{Figure: Adding a child} \label{fig:build-a-family}
\hrule

What this suggests is that a simple constructor for this kind of data isn't
really enough. Instead, we should define a {\sc {generalized constructor}}
that not only creates a {{\tt person}\/} structure but also initializes it
properly when possible. To develop this function, it is best to follow the
real world where upon the birth of a child, we create a new entry in the
family tree, record the child's parents, and record in the existing parents'
entries that they have a newborn.  Here is the specification for just such a
function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt add-child!}\ {\tt :}\ {\tt symbol}\ {\tt number}\ {\tt person}\ {\tt person}\ {}{\tt ->}{}\ {\tt person}\/}}\\
\hbox{;; to construct a {{\tt person}\/} structure for a newborn }
\\
\hbox{;; effect: to add the new structure to the cildren of {{\tt father}\/} and {{\tt mother}\/}}
\\
{\tt (define}\  {\tt (add-child!}\  {\tt name}\  {\tt soc-sec}\  {\tt father}\  {\tt mother)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Its task is to create a new structure for a newborn child and to add the
structure to an existing family tree. The function consumes the child's
name, social security number, and the structures representing the father and
the mother.

The first step of the design of {{\tt add-child!}\/} is to create the new
structure for the child:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (add-child!}\  {\tt name}\  {\tt soc-sec}\  {\tt father}\  {\tt mother)}\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt the-child}
\\
\>\>\>{\tt (make-person}\  {\tt name}\  {\tt soc-sec}\  {\tt father}\  {\tt mother}\  {\tt empty)))}
\\
\>\>{\tt ...))}
\end{tabbing}\end{minipage}\end{center}
This covers the first part of the contract. By naming the structure in a
{\bf local}-exp\-res\-sion\ we can mutate it in the body of the expression. 

The second step of the design of {{\tt add-child!}\/} is to add a body to
the {\bf local}-exp\-res\-sion\ that performs the desired effects:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (add-child!}\  {\tt name}\  {\tt soc-sec}\  {\tt father}\  {\tt mother)}\\
\>{\tt (l}\={\tt ocal}\  {\tt ((d}\={\tt efine}\  {\tt the-child}
\\
\>\>\>{\tt (make-person}\  {\tt name}\  {\tt soc-sec}\  {\tt father}\  {\tt mother}\  {\tt empty)))}
\\
\>\>{\tt (b}\={\tt egin}
\\
\>\>\>{\tt (set-person-children!}\  \={\tt father}
\\
\>\>\>\>{\tt (cons}\  {\tt the-child}\  {\tt (person-children}\  {\tt father)))}
\\
\>\>\>{\tt (set-person-children!}\  \={\tt mother}
\\
\>\>\>\>{\tt (cons}\  {\tt the-child}\  {\tt (person-children}\  {\tt mother)))}
\\
\>\>\>{\tt the-child)))}
\end{tabbing}\end{minipage}\end{center}
Since there are two specified effects and since the purpose statement
also specifies a result, the body of the {\bf local}-exp\-res\-sion\ is a {\bf begin}-exp\-res\-sion\ with
three subexpressions. The first mutates {{\tt father}\/}, adding
{{\tt the-child}\/} to the list of children. The second mutates
{{\tt mother}\/} in an analogous manner. The last one produces the desired
result. 

Figure~\ref{fig:build-a-family} illustrates the evaluation of an
application of {{\tt add-child!}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (add-child!}\  \={\tt \char'047}{\tt Ludwig}\  {\tt 3}\\
\>{\tt (make-person}\  {\tt \char'047}{\tt Adam}\  {\tt ...}\  {\tt ...}\  {\tt ...)}\  
\\
\>{\tt (make-person}\  {\tt \char'047}{\tt Eve}\  {\tt ...}\  {\tt ...}\  {\tt ...))}
\end{tabbing}\end{minipage}\end{center}
The top-half shows the new structure for {{\tt \char'047}{\tt Ludwig}\/} and how it refers
to the {{\tt father}\/} and {{\tt mother}\/} structures. Just as in
section~\ref{sec:struct-in-struct}, the picture uses arrows to relate the
nodes of a family tree. But now this choice isn't just a convenience, it is
dictated by necessity. As the bottom half of the figure shows, the
structure mutation of {{\tt add-child!}\/} modify the {{\tt children}\/}
fields of the {{\tt father}\/} and {{\tt mother}\/} structure. They add an
additional item to the list in this field, and this new item is the
structure for {{\tt \char'047}{\tt Ludwig}\/}. Without arrows, we wouldn't be able to draw
this constellation of structures because it is impossible to draw the two
structures as nested in each other. 

With {{\tt add-child!}\/} we can create family trees, one child at a
time. What we need to learn is to design functions that process this new
class of family trees. In this case, we can almost always pick one of the
two views that we used before: the ancestor family tree or the descendant
family tree. Either view just ignores certain fields in the structures.
Once we have chosen a view, we design the desired functions following the
known recipes. Even if we decide to use the bi-directional relations in the
new family tree representation, designing a function is usually simply a
matter of formulating those auxiliary functions that correspond to the
real-world family relationships and to compose them properly. The following
few exercises demonstrate these principles.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 43.2.1}

 \label{ex:ftn-mu1} 
Modify {{\tt add-child!}\/} so that it has the following contract: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt add-child!}\ {\tt :}\ {\tt symbol}\ {\tt number}\ {\tt ftn}\ {\tt ftn}\ {}{\tt ->}{}\ {\tt person}\/}}\end{tabbing}\end{minipage}\end{center}
The function otherwise behaves just like the original version.

Once we have the modified function, there is no need for
{{\tt make-person}\/} any more. We can create all forms of {{\tt person}\/}
structures with {{\tt add-child!}\/} directly. 

Transliterate the family tree in figure~\ref{fig:familyA} into the new
representation; use the new modified {{\tt add-child!}\/} function
exclusively.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ftn-mu1.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.2.2}

 \label{ex:ftn-mu2} 
Develop the function {{\tt how-many-ancestors}\/}, which consumes a family
tree node and determines how many ancestors there are. The node itself
counts as an ancestor.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ftn-mu2.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.2.3}

 \label{ex:ftn-mu3} 
Develop {{\tt how-many-descendants}\/}, which consumes a family
tree node and determines how many descendants there are. The node itself
counts as a descendant.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ftn-mu3.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.2.4}

 \label{ex:ftn-mu} 
Develop {{\tt names-of-cousins}\/}. The function consumes a {{\tt person}\/} and
produces the names of the cousins.

\noindent{\bf Hints:} \ (1) Don't forget to use Scheme's built-in functions for processing
lists. (2) Use a sufficiently large portion of your own family tree to test
the functions. (3) For the testing step, compare the names of the results
of the auxiliary functions with the expected results. Because the
structures are mutually referential, it is difficult to compare them
automatically. Alternatively, use {{\tt eq?}\/}, Scheme's intensional
equality predicate, to compare two structures. Why does this
work?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/ftn-mu.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt} 



In sections~\ref{sec:traverse-graph1} and~\ref{sec:loss:generative}, we
encountered the problem of representing and traversing graphs.  Recall that
a graph is a collection of nodes and connections between nodes.  The graph
traversal problem is to determine whether there is a route from a node
labeled {{\tt orig}\/} to one called {{\tt dest}\/}. In a simple graph, each
node has exactly one one-way connection to another node.

Originally, we represented a graph as a list of named nodes. If one node
was connected to another, the corresponding structure for the first node
contained the name of the second node, not the node itself.
Exercise~\ref{ex:find-route-vector} introduced a vector-based
representation. Still, all of our representations used the indirection
trick, so that if we wanted to move from one node to another, we first had
to lookup the connection in a table. 

Using structure mutation, we can eliminate this indirection and create
structures for nodes that contain each other, even if the gaph contains a
cycle. To understand how this works in a concrete manner, let's discuss how
to model simple graphs such as those in figure~\ref{fig:sim-graph} and how
to design programs that find routes through such graphs. First, we need a
structure definition for nodes:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt node}\  {\tt (name}\  {\tt to))}\end{tabbing}\end{minipage}\end{center}
The {{\tt name}\/} field records the name of the node, and the {{\tt to}\/}
field specifies to which other node it is connected. Second, we need a data
definition:

\begin{quote} An {\sl {simple graph node}} ({{\tt node}\/}) is a structure:\\
        \centerline{{{\tt (make-node}\ {\tt n}\ {\tt t)}\/}}     where {{\tt n}\/} is a symbol and {{\tt t}\/} is a {{\tt node}\/}. \end{quote}

\noindent The data definition is unusual in that it is self-referential, but it
doesn't consist of several clauses. This immediately raises the question of how
we can construct a node that complies with this definition. Clearly, applying
{{\tt make-node}\/} doesn't work; instead, we need to define a generalized
constructor that immediately sets the {{\tt to}\/} field of a node. 

The generalized constructor consumes the atomic data for a {{\tt node}\/}
structure and constructs a legal node structure from there: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt create-node}\ {\tt :}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt node}\/}}\\
\hbox{;; to create a simple legal graph node with {{\tt a-name}\/} in the name field}
\\
{\tt (d}\={\tt efine}\  {\tt (create-node}\  {\tt a-name)}
\\
\>{\tt (local}\  {\tt ((define}\  {\tt the-node}\  {\tt (make-node}\  {\tt a-name}\  {\tt false)))}\  {\tt ...))}
\end{tabbing}\end{minipage}\end{center}
The natural candidate to place into the {{\tt to}\/} field is the node itself.
In other words, the generalized constructor creates a node that contains
itself:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt create-node}\ {\tt :}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt node}\/}}\\
\hbox{;; to create a simple graph node that contains {{\tt a-name}\/} and itself}
\\
{\tt (d}\={\tt efine}\  {\tt (create-node}\  {\tt a-name)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt the-node}\  {\tt (make-node}\  {\tt a-name}\  {\tt false)))}
\\
\>\>{\tt (b}\={\tt egin}
\\
\>\>\>{\tt (set-node-to!}\  {\tt the-node}\  {\tt the-node)}
\\
\>\>\>{\tt the-node)))}
\end{tabbing}\end{minipage}\end{center}
The generalized constructor makes the node using the ordinary constructor,
initializing the {{\tt name}\/} field properly and putting {{\tt false}\/} into
the {{\tt to}\/} field. Although the latter is an improper action according to
our data definition, it is acceptable because it is immediately corrected in
the {\bf local}-exp\-res\-sion's body. Hence, the result of an application of
{{\tt create-node}\/} produces a {{\tt node}\/} as promised.

With {{\tt create-node}\/} we can create the nodes in a graph, but we can't
establish the connections between them. To connect two nodes, we must
modify the {{\tt to}\/} field of one of the structures so that it contains
the other. While this suggestion is generally on target, it raises the
problem of how to identify the nodes. The family tree example suggests one
solution, namely, to introduce one variable definition per node. Another
comes from our orginal work with graphs, where we represented graphs as
lists of symbolic pairs of connections or lists of nodes or vectors of
nodes. Here we pursue the second option:

\begin{quote} An {\sl {simple graph}} is a {{\tt (listof}\ {\tt node)}\/}. \end{quote}

Assuming we have a list of all nodes, say {{\tt the-graph}\/}, and a
function for looking up the node with a given name, say {{\tt lookup-node}\/}, we
can create a connection from one node to the other with a structure
mutation: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (set-node-to!}\  \={\tt (lookup-node}\  {\tt from-name}\  {\tt the-graph)}\\
\>{\tt (lookup-node}\  {\tt to-name}\  {\tt the-graph))}
\end{tabbing}\end{minipage}\end{center}
We can make connecting two nodes more convenient than that with an
auxiliary function: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt connect-nodes}\ {\tt :}\ {\tt symbol}\ {\tt symbol}\ {\tt graph}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to mutate the {{\tt to}\/} field in the structure with}
\\
\hbox{;; {{\tt from-name}\/} in the {{\tt name}\/} field so that it contains}
\\
\hbox{;; the structure with {{\tt to-name}\/} in the {{\tt name}\/} field}
\\
{\tt (d}\={\tt efine}\  {\tt (connect-nodes}\  {\tt from-name}\  {\tt to-name}\  {\tt a-graph)}
\\
\>{\tt (set-node-to!}\  \={\tt (lookup-node}\  {\tt from-name}\  {\tt a-graph)}
\\
\>\>{\tt (lookup-node}\  {\tt to-name}\  {\tt a-graph)))}
\end{tabbing}\end{minipage}\end{center}
Defining {{\tt lookup-node}\/} is a exercise in structural function design,
though it is best done using Scheme's {{\tt assf}\/} function, which
abstracts this situation. 

\hrule
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt create-node}\ {\tt :}\ {\tt symbol}\ {}{\tt ->}{}\ {\tt node}\/}}\\
\hbox{;; to create a simple graph node that contains itself}
\\
{\tt (d}\={\tt efine}\  {\tt (create-node}\  {\tt name)}
\\
\>{\tt (l}\={\tt ocal}\  {\tt ((define}\  {\tt the-node}\  {\tt (make-node}\  {\tt name}\  {\tt false)))}
\\
\>\>{\tt (b}\={\tt egin}
\\
\>\>\>{\tt (set-node-to!}\  {\tt the-node}\  {\tt the-node)}
\\
\>\>\>{\tt the-node)))}
\\
\hbox{;; {{\tt connect-nodes}\ {\tt :}\ {\tt symbol}\ {\tt symbol}\ {\tt graph}\ {}{\tt ->}{}\ {\tt void}\/}}
\\
\hbox{;; effect: to mutate the {{\tt to}\/} field in the structure named }
\\
\hbox{;; {{\tt from-name}\/} so that it contains the structure named {{\tt to-name}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (connect-nodes}\  {\tt from-name}\  {\tt to-name}\  {\tt a-graph)}
\\
\>{\tt (set-node-to!}\  \={\tt (lookup-node}\  {\tt from-name}\  {\tt a-graph)}
\\
\>\>{\tt (lookup-node}\  {\tt to-name}\  {\tt a-graph)))}
\\
\hbox{;; {{\tt lookup-node}\ {\tt :}\ {\tt symbol}\ {\tt graph}\ {}{\tt ->}{}\ {\tt node}\ {\tt or}\ {\tt false}\/}}
\\
\hbox{;; to lookup up the node named {{\tt x}\/} in {{\tt a-graph}\/}}
\\
{\tt (define}\  {\tt (lookup-node}\  {\tt x}\  {\tt a-graph)}\  {\tt ...)}
\\
\hbox{;; {{\tt the-graph}\ {\tt :}\ {\tt graph}\/}}
\\
\hbox{;; the list of all available nodes }
\\
{\tt (d}\={\tt efine}\  {\tt the-graph}
\\
\>{\tt (list}\  \={\tt (create-node}\  {\tt \char'047}{\tt A)}
\\
\>\>{\tt (create-node}\  {\tt \char'047}{\tt B)}
\\
\>\>{\tt (create-node}\  {\tt \char'047}{\tt C)}
\\
\>\>{\tt (create-node}\  {\tt \char'047}{\tt D)}
\\
\>\>{\tt (create-node}\  {\tt \char'047}{\tt E)}
\\
\>\>{\tt (create-}\={\tt node}\  {\tt \char'047}{\tt F)))}
\\
\>\>\>
\\
{\tt (b}\={\tt egin}
\\
\>{\tt (connect-nodes}\  {\tt \char'047}{\tt A}\  {\tt \char'047}{\tt B}\  {\tt the-graph)}
\\
\>{\tt (connect-nodes}\  {\tt \char'047}{\tt B}\  {\tt \char'047}{\tt C}\  {\tt the-graph)}
\\
\>{\tt (connect-nodes}\  {\tt \char'047}{\tt C}\  {\tt \char'047}{\tt E}\  {\tt the-graph)}
\\
\>{\tt (connect-nodes}\  {\tt \char'047}{\tt D}\  {\tt \char'047}{\tt E}\  {\tt the-graph)}
\\
\>{\tt (connect-nodes}\  {\tt \char'047}{\tt E}\  {\tt \char'047}{\tt B}\  {\tt the-graph))}
\end{tabbing}\end{minipage}\end{center}

\center{Figure: Creating a simple graph via mutation} \label{fig:sg-imp} 
\hrule

Now we can transliterate simple graphs into a Scheme
representation. Suppose we start with the graph in
figure~\ref{fig:sim-graph}, which is reproduced here in a tabular format:
\begin{center} 
\begin{tabular}{l|c|c|c|c|c|c}
{\it from\/} & A & B & C & D & E & F \\
 \hline
{\it to\/}   & B & C & E & E & B & F 
\end{tabular}
\end{center}
The first step is to create a list of all the nodes and to name it. The second
step is to establish the connections according to this
table. Figure~\ref{fig:sg-imp} shows the corresponding Scheme expressions.
They are straight transliterations of the columns in the tabular representation
of the graph. There is no need to re-connect the {{\tt \char'047}{\tt F}\/} node because it is
already connected to itself.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 43.2.5}

 \label{ex:graph0} 
Draw a picture of {{\tt (create-node}\ {\tt \char'047}{\tt A)}\/}. Use the boxes-in-boxes
approach and the boxes-and-arrow approach.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/graph0.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.2.6}

 \label{ex:graph1} 
Transliterate the given simple graph without creating a list of all
nodes.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/graph1.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.2.7}

 \label{ex:graph2} 
Develop the function {{\tt symbolic-graph-to-structures}\/}. It consumes a
list of pairs and creates a {{\tt graph}\/}. 

\noindent Example: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt the-graph}\  \\
\>{\tt (symbolic-graph-to-structures}\  {\tt \char'047}{\tt ((A}\  {\tt B)}\  {\tt (B}\  {\tt C)}\  {\tt (C}\  {\tt E)}\  {\tt (D}\  {\tt E)}\  {\tt (E}\  {\tt B)}\  {\tt (F}\  {\tt F))))}
\end{tabbing}\end{minipage}\end{center}
Evaluating this definition is equivalent to evaluating the definitions in
figure~\ref{fig:sg-imp}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/graph2.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


Once we have a method for representing simple graphs, we can turn our
attention to the problem of finding a route from one node in the graph to
another. Recall the original specification from
section~\ref{sec:loss:generative}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt route-exists\char'077}\ {\tt :}\ {\tt node}\ {\tt node}\ {\tt simple-graph}\ {}{\tt ->}{}\ {\tt boolean}\/}}\\
\hbox{;; to determine whether there is a route from {{\tt orig}\/} to {{\tt dest}\/} in {{\tt sg}\/}}
\\
{\tt (define}\  {\tt (route-exists\char'077}\  {\tt orig}\  {\tt dest}\  {\tt sg)}\  {\tt ...)}
\end{tabbing}\end{minipage}\end{center}
Of course, we must re-interpret the names for our data classes in the new
context but otherwise the specification is perfectly fine. 

The development of the original function demonstrated two new ideas. First, the
function uses generative recursion. Once it is known that {{\tt orig}\/} and
{{\tt dest}\/} are distinct nodes, the search resumes from the node to which
{{\tt orig}\/} is connected. Second, the function requires an accumulator to 
remember which nodes have been visited. Without the accumulator, the
function may re-visit the same node over and over again. 

So, let's start from the template for generative recursion: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (route-exists\char'077}\  {\tt orig}\  {\tt dest}\  {\tt sg)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (eq-node\char'077}\  {\tt orig}\  {\tt dest)}\  {\tt true\char'135}
\\
\>\>{\tt \char'133}\={\tt else}
\\
\>\>\>{\tt (route-exists\char'077}\  {\tt ...}\  \hbox{$\mbox{the node to which {{\tt orig}\/} is connected}$}\  {\tt ...}\  {\tt dest}\  {\tt sg)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The function {{\tt eq-node\char'077}\/} determines whether the two nodes are the same;
this may just use {{\tt eq?}\/}, Scheme's intentional equality predicate, or
it may compare the names of the nodes, assuming they are unique.  If the
nodes are the same, a route exists. If not, we can generate a new,
potentially useful problem by moving to the node to which {{\tt orig}\/} is
connected. In the graph representation of section~\ref{sec:loss:generative},
this requires looking in {{\tt sg}\/}. In our new graph representation, the
connection is a part of the {{\tt node}\/} representation. Hence, we can use
{{\tt node-to}\/} instead of looking in {{\tt sg}\/}:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (route-exists\char'077}\  {\tt orig}\  {\tt dest}\  {\tt sg)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (eq-node\char'077}\  {\tt orig}\  {\tt dest)}\  {\tt true\char'135}
\\
\>\>{\tt \char'133}{\tt else}\  {\tt (route-exists\char'077}\  {\tt (node-to}\  {\tt orig)}\  {\tt dest}\  {\tt sg)\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
The function definition shows that, so far, {{\tt sg}\/} is useless. Because a
node in the new graph representation contains its neighbors, the neighbor
contains its neighbor, and so on, there is no need to use the table.

The termination argument for this function fails, just as for the original
one in section~\ref{sec:loss:generative}. To see why our new function may
fail to terminate, take a look at its definition. It doesn't contain
{{\tt false}\/}, and the function cannot possible produce
{{\tt false}\/}---even though we know that our sample graph, for example,
doesn't contain a path from {{\tt \char'047}{\tt F}\/} to {{\tt \char'047}{\tt A}\/} or anywhere else.
If we inspect what happens with
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (route-exists\char'077}\  {\tt (lookup-node}\  {\tt the-graph}\  {\tt \char'047}{\tt F)}\  {\tt (lookup-node}\  {\tt the-graph}\  {\tt \char'047}{\tt A))}\end{tabbing}\end{minipage}\end{center}
we see that {{\tt route-exists\char'077}\/} repeatedly visits the node {{\tt \char'047}{\tt F}\/}. In
short, it forgets what it has processed so far. 

We know that equipping {{\tt route-exists\char'077}\/} with an accumulator overcomes
this lack of knowledge, but that requires another table lookup. We can do
better than that with a structure mutation that records a visit by the
{{\tt route-exists\char'077}\/} function. To do that, the {{\tt node}\/} structures
need an addtional field; we call it {{\tt visited}\/}: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt node}\  {\tt (name}\  {\tt visited}\  {\tt to))}\end{tabbing}\end{minipage}\end{center}
Initially the field contains {{\tt false}\/}. As {{\tt route-exists\char'077}\/}
visits a node, it puts {{\tt true}\/} into the field:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (d}\={\tt efine}\  {\tt (route-exists\char'077}\  {\tt orig}\  {\tt dest}\  {\tt sg)}\\
\>{\tt (c}\={\tt ond}
\\
\>\>{\tt \char'133}{\tt (eq-node\char'077}\  {\tt orig}\  {\tt dest)}\  {\tt true\char'135}
\\
\>\>{\tt \char'133}{\tt (node-visited}\  {\tt orig)}\  {\tt false\char'135}
\\
\>\>{\tt \char'133}{\tt e}\={\tt lse}
\\
\>\>\>{\tt (b}\={\tt egin}
\\
\>\>\>\>{\tt (set-node-visited!}\  {\tt orig}\  {\tt true)}
\\
\>\>\>\>{\tt (route-exists\char'077}\  {\tt (node-to}\  {\tt orig)}\  {\tt dest}\  {\tt sg))\char'135}{\tt ))}
\end{tabbing}\end{minipage}\end{center}
To exploit this new knowledge, the function checks the new structure field
as one of the new termination condition. If {{\tt orig}\/} has been visited
before, there is no route because the function has discovered a cycle in
the graph.

The second structure mutation of this example illustrates two ideas. First,
structure mutation can replace a table-based accumulator. In general,
though, it is best to study a table-based version and to add structure
mutations based on a solid understanding of the accumulated
knowledge. Second, structure mutations can play a role in termination test
for generative recursion. After all, state change is motivated by the
desire to remember things across function applications and termination
tests must discover whether things have changed. While the combination is
rare, it is useful and appears time and again in the study of algorithms. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 43.2.8}

 \label{ex:graph3} 
The function {{\tt route-exists\char'077}\/} assumes that the {{\tt visited}\/}
fields of all the nodes are initially {{\tt false}\/}. A single use of the
function, however, sets some of the fields in a graph to
{{\tt true}\/}. This implies that the function cannot be used twice in a
row. 

Develop a revised version of {{\tt route-exists\char'077}\/}, with the same
specification, that sets all {{\tt visited}\/} fields to {{\tt false}\/}
before it searches for a route between the given nodes. 

Determine the abstract running time of the new function, assuming the graph
has $N$ nodes.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/graph3.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.2.9}

 \label{ex:graph4} 
Develop the function {{\tt reachable}\/}. It consumes a node in a simple
graph. Its effect is to place {{\tt true}\/} into the {{\tt visited}\/}
fields of all those nodes that are reachable from the given node and to
ensure that the {{\tt visited}\/} fields of all other nodes are
{{\tt false}\/}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/graph4.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.2.10}

 \label{ex:graph} 
Develop {{\tt make-simple-graph}\/}, a function that manages the state of a
{{\tt local}\/}ly defined graph. The function accepts a simple graph in the
form of lists of pairs of symbols: {{\tt (listof}\ {\tt (list}\ {\tt symbol}\ {\tt symbol))}\/}. 
It supports four services: 
\begin{enumerate}
\item adding nodes that are connected to already existing nodes (by name); 

\item changing the connection of a node (by name); 

\item determining whether a route between two nodes exists; 

\item and removing nodes that are not reachable from some given node. 
\end{enumerate}

\noindent{\bf Hint:} \ Instead of using a list, the manager should use a node sequence,
which is analogous to the {{\tt hand}\/} structure from
section~\ref{sec:design-mut-ex2}. A node sequence relies on the following
structure: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}{\tt (define-struct}\  {\tt sequence}\  {\tt (node}\  {\tt next))}\end{tabbing}\end{minipage}\end{center}
A sequence is similar to a list, but it supports structure
mutations.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/graph.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt} 


The discussion of this section confirms the usefulness of the design recipes,
even for collections of structures that refer to each other.  The most
important lesson is that such situations call for a generalized constructor, a
function that creates a structure and immediately establishes the necessary
connections. Generalized constructors correspond to the initializers of
section~\ref{sec:set-bang}; we have also seen the idea in
section~\ref{sec:design-mut-ex2} where we created a hand from a single card.
In some cases, such as the one for simple graphs, we may also want to introduce
auxiliary functions for mutating the structures a second time. Once we have
those functions, we can use the standard recipes, including those for
introducing additional structure fields.

\subsection{Backtracking with State}

Section~\ref{sec:backtrack} introduced algorithms that backtrack. An algorithm
is a recursive function that generates new problems for the recursive step
rather than using the pieces of its input data. On occasion, an algorithm may
have to make choices among several different branches on the path to a
solution.  Some of them may lead nowhere. In such cases, an algorithm can
backtrack. That is, it can restart the search for a solution with a different
branch to check if it succeeds.

When the data representation for a problem uses structures or vectors, a
backtracking algorithm can use structure mutation to test different
approaches to a solution. The key is to design a pair of functions that
change the state of the problem representation and that undo such a change
in case the attempt fails. In this section, we discuss two examples of this
kind: the Queens puzzle and the Peg Solitaire problem. 

Recall the Queens puzzle from section~\ref{sec:queens}. The goal of the
puzzle is to place {{\tt n}\/} queens on some board of arbitrary size {\it m\/}-by-{\it m} such that the queens do not threaten each other. A queen in
chess threatens all places on the row, the column, and the two diagonals
going through her own position. Figure~\ref{fig:queens} illustrates the
notion with a single queen on an 8-by-8 board. 

In section~\ref{sec:queens}, we represented chess boards with
lists. When we got to know vectors, we also developed a vector-based
representation in exercise~\ref{ex:vector-chess}, as follows: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; A {\sl {chess board}} {{\tt CB}\/} is a {{\tt (vectorof}\ {\tt (vectorof}\ {\tt boolean))}\/}}\\
\hbox{;; such that all vectors have the same size.}
\\
\hbox{;; {{\tt make-chess-board}\ {\tt :}\ {\bf N}\ {}{\tt ->}{}\ {\tt CB}\/}}
\\
{\tt (d}\={\tt efine}\  {\tt (make-chess-board}\  {\tt m)}
\\
\>{\tt (build-vector}\  {\tt m}\  {\tt (lambda}\  {\tt (i)}\  {\tt (build-vector}\  {\tt m}\  {\tt (lambda}\  {\tt (j)}\  {\tt true)))))}
\end{tabbing}\end{minipage}\end{center}
The initial value of {{\tt true}\/} indicates that it is still legitimate to
place a queen on the corresponding field. 

The queen-placement algorithm places a queen on one of the available field
on the given board and creates a new board that reflects the addition of
the queen. This step is repeated until there are no more queens to be
placed, in which case the puzzle is solved, or until there are no more
places to choose from. In the second case, the algorithm backtracks. That
is, the algorithm removes the last queen that was added and chooses some
other available field. If there are no more fields, it backtracks further. 
The algorithm signals a complete failure when it becomes impossible to
backtrack. 

On one hand, creating a new board at each stage is acceptable because the
chosen field may turn out to be the wrong one in which case the old board is
the starting point for the next step. On the other hand, a human player is more
likely to place the queen on the board and to remove it if the position turns
out to be a bad choice. Thus the Queens problem is an example of where the
ability of computer programs to create many different alternative ``worlds''
clashes with the human world, which offers extremely limited possibilities of
this kind\footnote{For example, a human puzzler could set up an entirely new board for every new stage in the algorithm. The additional work is, however, prohibitive, which is why humans shy away from such simulations. It also means that computers are better at searching for solutions in parallel, and people are not.} and thus restricts human imagination. Still, it is worth exploring
how the addition of vector mutation to our vocablulary enables us to mimic the
actions of a human player more closely than before.

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 43.3.1}

 \label{ex:iqueen0}
Placing an additional queen on a chess board means that some of the fields
on the chess board have to be set to {{\tt false}\/} because they are now
threatened and no longer available for future placements of queens. The
placement of a queen is a function of the given chess board and the indices
of the new queen:  
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt place-queen}\ {\tt :}\ {\tt CB}\ {\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to set those fields in {{\tt CB}\/} to {{\tt false}\/} that are threatened by }
\\
\hbox{;; a queen on row {{\tt i}\/}, column {{\tt j}\/}}
\\
{\tt (define}\  {\tt (place-queen}\  {\tt CB}\  {\tt i}\  {\tt j)}\  {\tt ...))}
\end{tabbing}\end{minipage}\end{center}

\noindent{\bf Hints:} \ (1) Recall {{\tt threatened\char'077}\/} from exercise~\ref{ex:queen2}.  (2)
Consider developing an abstract function for processing all items on a
board. The function is analogous to {{\tt vec-for-all}\/} from
exercise~\ref{ex:abs-vec}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/iqueen0.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.3.2}

 \label{ex:iqueen1}
Develop {{\tt unplace-queen}\/}. The function removes a queen and its threats
from a chess board:
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\hbox{;; {{\tt unplace-queen}\ {\tt :}\ {\tt CB}\ {\bf N}\ {\bf N}\ {}{\tt ->}{}\ {\tt void}\/}}\\
\hbox{;; effect: to set those fields in {{\tt CB}\/} to {{\tt false}\/} that were threatened by }
\\
\hbox{;; a queen on row {{\tt i}\/}, column {{\tt j}\/}}
\\
{\tt (define}\  {\tt (unplace-queen}\  {\tt CB}\  {\tt i}\  {\tt j)}\  {\tt ...))}
\end{tabbing}\end{minipage}\end{center}

Given any chess board {{\tt CB}\/}, the following equation holds: 
\begin{center}\begin{minipage}[t]{8in}\begin{tabbing}\  \  {\tt (b}\={\tt egin}\  \\
\>{\tt (place-queen}\  {\tt CB}\  {\tt i}\  {\tt j)}
\\
\>{\tt (unplace-queen}\  {\tt CB}\  {\tt i}\  {\tt j)}
\\
\>{\tt CB)}
\\
{\tt =}\  {\tt CB}
\end{tabbing}\end{minipage}\end{center}
for all legal positios {{\tt i}\/} and {{\tt j}\/}. Why is this not true if
we swap the first two subexpressions?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/iqueen1.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.3.3}

 \label{ex:iqueen2}
Modify the solution of the Queens problem in section~\ref{sec:queens} to
use the vector-based representation of chess boards and the functions
{{\tt place-queen}\/} and {{\tt unplace-queen}\/} from
exercises~\ref{ex:iqueen0} and~\ref{ex:iqueen1}.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/iqueen2.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.3.4}

 \label{ex:iqueen3}
Use the {\tt\bf draw.ss} teachpack to develop a view for the Queens
problem. Recall that a view is a function that illustrates certain aspects
of a problem in a graphical manner. The natural solution here is to display
the intermediate stages of the solution process according to the algorithm
of exercise~\ref{ex:iqueen2}, including the backtracking
steps.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/iqueen3.html}{\rule{3pt}{5pt}}\\
\nopagebreak\noindent\rule{\textwidth}{1pt}


In section~\ref{sec:more-accu-soli} we discussed the Peg Solitaire
problem. The goal of the game is to eliminate the pegs one by one, until
only one peg is left. A player can eliminate a peg if one of the
neighboring holes is unoccupied and if there is a peg in the hole in the
opposite direction. In that case, the second peg can jump over the first
one and the first one is eliminated.

Just as with the Queens puzzle, we can represent the problem state with
vectors and indicators for pegs and holes. In the real world, moving a peg
corresponds to physical action that changes the state of the board. When a
player backtracks, the two pegs are placed back to their original
positions. 

  \noindent\rule{\textwidth}{1pt}\\
%   \nopagebreak  \noindent{\large \bf Exercises}

{\bf Exercise 43.3.5}

 \label{ex:isolitaire0}
Design a vector representation for the triangular peg solitaire
board. Develop an auxiliary function for creating a board with 
a single hole.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/isolitaire0.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.3.6}

 \label{ex:isolitaire1}
Design a data representation for a move in the Peg Solitaire problem.  Develop
a function for making a move.  Develop a function for undoing a move. The two
functions should exclusively rely on effects. Do the functions satisfy an
equation analogous to {{\tt place-queen}\/} and {{\tt unplace-queen}\/} in
exercise~\ref{ex:iqueen1}?~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/isolitaire1.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.3.7}

 \label{ex:isolitaire2}
Develop a backtracking algorithm for solving a Peg Solitaire problem whose
hole is placed randomly.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/isolitaire2.html}{\rule{3pt}{5pt}}


{\bf Exercise 43.3.8}

 \label{ex:isolitaire3}
Use the {\tt\bf draw.ss} teachpack to develop a view for the Peg Solitaire
problem. Recall that a view is a function that illustrates certain aspects
of a problem in a graphical manner. The natural solution here is to display
the intermediate stages of the solution process according to the algorithm
of exercise~\ref{ex:isolitaire2}, including the backtracking
steps.~\htmladdimg{../icons/hand.right.gif}\htmladdnormallink{Solution}{../Solutions/isolitaire3.html}{\rule{3pt}{5pt}} 



%% BEGIN INLINED FILE: curriculum8.dl.tex

\renewcommand{\thesection}{A}

\begin{htmlonly}
\end{htmlonly}


\part{Epilogue} \begin{flushright} \begin{minipage}{3.60in} {\it ROS: I mean, what exactly do you {\em do\/}?\\
 PLAYER: We keep to our usual stuff, more or less, only inside out.  We  do on stage things that are supposed to happen off.  Which is a kind of integrity, if you look on every exit as being an extrance somewhere  else.\\
[.35cm] } \hfill---Tom Stoppard, {\em Rosencrantz and Guildenstern are Dead\/} \end{minipage} \end{flushright}

We have reached the end of this introduction to computing and program
design. While there is more to learn about both subjects, this is a good
point to stop, to summarize, and to look ahead.

\subsection{Computing} 

From elementary school to high school we learn to compute with one form of
data: numbers. Our first use of numbers is to count real things, say, three
apples, five friends, twelve bagels. Later we use numbers without any
appeal to concrete objects, but we have learned that numbers represent
information in the real world.

Computing with software is {\em algebra for all kinds of data\/}, not just
numbers. Nowadays, computer programs process representations of music,
molecules, law cases, electrical diagrams, architectures of houses, and
poems. Fortunately, we have learned to represent information with other
forms of data than just numbers. Otherwise, computing and programming would
become extremely tedious tasks.

Above all, we shouldn't forget that computing means to manipulate data
through proper basic operations.  Some operations create new values. Others
extract values from values.  Yet others modify values.  Finally, there are
also basic operations for determining to which class a piece of data
belongs. Built-in operations and functions are of course just another class
of data.  Definition is value creation, application is a form of value
extraction.\footnote{An object in a language such as Java is a function with many different bodies. Each method represents a different way of extracting data from an object.}

When we define a function, we combine basic data operations. There are two
fundamental mechanisms for combining functions: function composition and
conditional expressions. The former means that the result of one function
becomes the argument of another one. The latter represents a choice among
several possibilities. When we eventually apply a function, we trigger a
computation. 

In this book we have studied the laws of basic operations and the laws of
operation combination. Using these laws we can understand, in principle,
how any function processes its input data and how it produces its results
and effects. Because the computer is extremely fast and good at using these
laws, it can perform such evaluations for more data and for larger programs
than we can do with paper and pencil.

\subsection{Programming} 

Programs consist of definitions and expressions. Large programs consist of
hundreds and thousands of definitions and expressions. Programmers design
functions, use other programmer's functions, leave, start on the
project. Without a strong discipline we cannot hope to produce software of
high quality. The key to programming discipline is to understand the design
of programs as a means to describe computations, which, in turn, means to
manipulate data through combinations of basic operations.

For that reason, the design of every program---whether it is small and for
personal use or large and for business use--- must start with an analysis
of the surrounding world of information and a description of the classes of
data that represent the relevant information. If the classes are unusual or
new, we make up examples so we understand the structure of the class
description. After we understand the world of information surrounding our
project and its data representation, we make a plan.

A project plan identifies what data we wish to produce from the data that
the program will be given. In many cases, though, a program doesn't just
process data in one way but in many ways. For example, a program for
managing bank accounts must handle deposits, withdrawals, interest
calculations, tax form generation, and many other tasks. In other cases, a
program may have to compute complex relationships. For example, a program
for simulating a ping-pong game must compute the movement of the ball,
bounces on the table, bounces from the paddle, paddle movements, etc.  In
either case, we need to describe what the various ways of processing data
are and how they relate to each other.  Then we rank them and start with
the most important one. We develop a working product, make sure that it
meets our specifications, and refine the product by adding more functions
or taking care of more cases or both.

Designing a function requires a rigorous understanding of what it computes.
Unless we can describe its purpose and its effect with concise statements,
we can't produce the function. In almost all cases, it helps to make up
examples and work through the function's computation by hand. For
complicated functions or for functions that use generative recursion, we
should include some examples with the purpose statements. The examples
illustrate the purpose and effect statements for others who may have to
read or modify the program.

Studying examples tends to suggest the basic design recipe. In most cases,
the design of a function is structural, even if it uses an accumulator or
structure mutation. In a few others, we must use generative recursion. For
these cases, it is important to explain the method for generating new
problems and to sketch why the computation terminates. 

When the definition is complete, we must test the function. Testing
discovers mistakes, which we are bound to make due to all kinds of
reasons. The best testing process turns independently developed examples
into test suites, that is, a bunch of expressions that apply the function
to select input examples and compare its results and effects with expected
results and effects (mostly) automatically. If a mismatch is discovered,
the test suite reports a problem. The test suite should never be discarded,
only commented out. Every time we modify the function, we must use the test
suite to check that we didn't introduce mistakes. If we changed the
underlying process, we may have to adapt the test suite {\it mutatis mutandis\/}.

No matter how hard we work, a function (or program) isn't done the first
time it works for our test suite. We must consider whether the development
of the function revealed new interesting examples and turn such examples
into additional tests. And we must edit the program. In particular, we must
use abstraction properly to eliminate all related patterns wherever
possible. 

If we respect these guidelines, we will produce decent software. It will
work because we understand why and how it works. Others who must modify or
enhance this software will understand it, because we include sufficient
information on its development process. Still, to produce great software,
we must practice following these guidelines and learn a lot more about
computing and programming than a first book can teach.

\subsection{Moving On} 

The knowledge and design skills from this book are a good foundation for
learning more about programming, computing, and even practical work on
software. First, the skills are good for learning the currently fashionable
collection of object-oriented languages, especially Java. The two languages
share a philosophy of programming. In both settings, computing means
dealing with data, and programming means describing classes of values and
functions on them.  Unlike Scheme, however, Java requires programmers to
spell out the class descriptions in Java, not just in English, and to place
function definitions with class descriptions. As a result, Java requires
programmers to learn a lot of syntactic conventions and is unsuitable as a
first language.

Second, a programmer must study the fundamental ideas of computing. Thus
far, our studies have focused on the laws of computing for data-oriented
programming languages. Using the programming skills from this book, we can
design and implement a simulation of how the hardware computes. By doing so
we see the laws of computing from a radically different perspective. The
contrast points to a number of interesting questions:
\begin{enumerate}
\item The two mechanisms of computing are rather different. Can one
mechanism compute what the other one can compute and {\it vice versa\/}?

\item The laws we have used are mathematical and abstract. They do not take
into account any real-world limitations. Does this mean that we can compute
whatever we wish? 

\item The (simulated) hardware shows that computers have limitations. How
do these limitations affect what we can compute? 
\end{enumerate}
Research on these questions created the discipline of computing and still
guides the design of most computing curricula.

Finally, the design knowledge of this book is enough to build some
real-world programs in Scheme. DrScheme with its build-in Web browser and
email capabilities is such a program. Building large real-world programs,
however, requires some more knowledge about the functions that Scheme uses
to create GUIs, to connect computers on a network, to script things such as
shells, common gateway interfaces (CGI), COM objects, and so on. 

Material on all three topics is available from this book's Web site in a
form that just extends the coverage and the style of the book. The book's
Web site is\\

\verb|         http://www.htdp.org/|

\medskip

\noindent Check in with this site on a regular basis and continue to study
computing and programming. 

\end{document}
