Let's say you're going shopping. In my house, we maintain a shopping list on the refrigerator door, adding to it as we think of things; before we put an item on, we check whether it's already there. If we write a program to maintain shopping lists, it would need a way to check whether an item is already in the list. Let's write something that does this.
First, we must define the data. What is a shopping list?
A shopping list is either
How can we be sure we've understood our data? Only by writing down a few examples. What's the simplest example? Well, it's the blank sheet of paper that we first attach to the refrigerator door: empty. Once we have a shopping list, we can put items onto it. These are all examples of shopping lists:
(After all, what good is a shopping list without cake?) Now let's write a function that accepts an item and a shopping list, and checks whether the item is in the list. For starters, what should such a function look like?
in-list? (i, sl) = ...
where i is an item and sl is a shopping list
That's all we know so far; so let's write just that much down. Do we know how in-list? is supposed to work? Well, if we do, we should be able to write down some examples of its behavior.
Now we can start to write the actual function.
PLT / scheme@cs.rice.edu
Last modified at 10:48:05 CST on Monday, November 10, 1997