How to Design Programs: An Introduction to Computing and Programming
Composing Functions, Revisited Again
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:
Formulate auxiliary function definitions for every dependency between quantities in the problem
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.
Designing Complex Programs
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 a function. Others who read the problem statement and the program can follow our reasoning
more easily that way. The movie-theater example in section
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 combining the values of the template's subexpressions into the final
answer. As we do so, we might encounter several situations:
If the formulation of an answer requires a case analysis of the available values, use a
If a computation requires knowledge of a particular domain of application, for example, drawing on (computer)
canvases, accounting, music, or science, use an auxiliary function.
If a computation must process a list, a natural number, or some other piece of data of arbitrary size, use an
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
The last two criteria are situations that we haven't discussed yet. The following two subsections illustrate them with
..How%20to%20Design%20Programs/curriculum-Z-H-16.html (1 of 13) [2/5/2008 4:46:22 PM]