Design recipe for Scheme programs, version 4

(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 time.

Creating a functionCreating a data type

    Analysis & Design (i.e. "decide what you want to do")

  1. Understand the assignment informally
  2. 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).
  3. Write examples of how your function will be used, with correct answers
  4. Implementation ("do it")

  5. Write the function's Skeleton
  6. Choose and use an appropriate Template
  7. Fill in the Function Body
  8. Correctness Checking ("check that you did it right")

  9. Proofread your code, fixing any mistakes you spot.
  10. Check the syntax of your code, fixing any mistakes the computer detects.
  11. Test your program to see whether it works correctly on all your test cases.

Describe the new data type in English. In particular, decide whether the new data type is defined by parts or by choices. That is, does an object of the new type "contain one of these, and one of those, and one of the others", or is it "either one of these, or one of those, or one of the others"?

For example, a posn consists of two numbers, the x coordinate and the y coordinate. Every posn must have both, so it's defined by parts. On the other hand, a shape might be defined as either a circle or a rectangle; no one shape will be both, so this is a definition by choices.

Definition by parts Definition by choices
  1. Identify the parts, specifying the name and type of each one.
  2. Write a define-struct, using Syntax Rule 5.
  3. Write function contracts for the functions that define-struct gives you.
  4. 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.)
  5. 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.
  1. Identify the choices: what values or types could an instance of the new data type be?
  2. 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.) There should be at least one example for each possible choice. If your data type is defined by subranges of numbers, there should also be examples at the boundaries.
  3. 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.

Last modified: Mon Sep 15 13:41:03 EDT 2003
Stephen Bloch / sbloch@adelphi.edu