Design Recipe for Structural Data
- What kinds of data are involved? Create data definitions. If they look
complex, construct examples according to the data definitions, just to
make sure they work.
- What kinds of data does the function consume? Which kind does it
produce? And what is its purpose? (in one line)
- Can you make up examples of inputs? What should the function produce
for these inputs?
- Let's construct the template:
- Does the data definition (of the main argument) mention clauses? If
so, use a
cond with as many cases as there are clauses in
the data definition.
- How can you distinguish these kinds of data with conditions involving
the main parameter?
structs involved? If so, write down all the selector
expressions. (Do so on a per-clause basis.)
- Does the data definition involve any self-references (or
cross-references)? If so, use recursion in the template to express these
- Let's code:
- Can you deal with the simple
cond cases? Your examples
should cover those cases directly.
- What do the expressions in the recursive cases compute? Use the purpose
statement of the function to figure out what the recursive function
- How can you combine the results of these expressions so that the
function returns the desired value? Use the examples to tabulate the
values of the intermediate expressions and the desired output for the
given input. This will usually suggest an expression for combining the
intermediate results, though admittedly this is the one and only "deep"
step of design and you won't get away without some real thinking here.
- Did you turn the examples into tests? You may want to do this as you
develop functional examples.