Design recipe for Scheme programs, version 3
(or programming in most other languages, for that matter!)
Are you creating a new function or a new data type?
Functions are analogous to verbs in human languages: they represent
actions that happen at a particular time.
For example, the built-in
function takes in two or more numbers, adds them up, and returns the
resulting number; the (user-defined)
cube function takes in
one number, computes its third power, and returns that. Functions
generally accept parameters when they are called and return results.
Data types are more like improper nouns in human languages: they represent
a kind of thing. In English, "computer", "student", "dog",
"banana", and "program" are kinds of things. In most computer programming
languages, there are built-in types like "number", "boolean", "symbol",
and "string", while you can define others like "circle", "rectangle",
"shape", etc. A data type is not "called" or "invoked" at any particular
time on any particular parameters, and it doesn't return a result.
Many programming tasks require inventing both a new data type and
a new function (or even several of each). Just deal with them one at a
|Creating a function||Creating a data type|
Analysis & Design (i.e. "decide what you want to do")
Understand the assignment informally
- Write a function contract,
specifying what kind of information goes in and what kind comes out.
Note that some of this information may be of new data types, in which case
you'll also need to create a data type (see right-hand column).
- Write examples of how your
function will be used, with correct answers
Implementation ("do it")
- Write the function's Skeleton
- Choose and use an appropriate Template
- Fill in the Function Body
Correctness Checking ("check that you did it right")
- Proofread your code,
fixing any mistakes you spot.
- Check the syntax of your
code, fixing any mistakes the computer detects.
- Test your program to see whether it works
correctly on all your test cases.
Describe the new data type in English. At this point, most of the new
data type definitions you've seen are "by parts": an object of the new type
"contains one of these, and one of those,
and one of the others.
For example, a
posn consists of two
numbers, the x coordinate and the y coordinate, and every
posn must have both.
We'll see another way to define data types, "by choices", shortly.
- Identify the parts,
specifying the name and type of each one.
- Write a
Syntax Rule 5.
- Write function
for the functions that
define-struct gives you.
- Write examples of your
new data type. (Note that unlike the "examples of function calls",
there are no "correct answers" attached to these, because an instance
of a data type doesn't produce an answer, it just is.)
- Write two function templates: one for
functions that take in
the new data type as a parameter, and one for
functions that return the
new data type as a result.
For the next elaboration of this, see version 4.
Mon Sep 15 13:40:34 EDT 2003
Stephen Bloch / firstname.lastname@example.org