COMP 210, Spring 2001
Homework 3
Due Sept.19 (wed), start of class
All problems will require all (applicable) steps from the design recipe, including a template. Before you tackle the homework, remind yourself of our General Advice, Advice on Homeworks (in particular: staple and provide questions), and the Grading Guidelines.
(6pts) Programs on Lists
Write down a data definition for lists of numbers, and examples of lists of numbers. Now, develop the following programs that consume a list of real numbers:
A function sum that returns the sum of all the numbers in the list.
A function count-positives that returns the number of positive numbers in the list.
A function any-negative? that returns true if and only if ("iff") there is some negative number in the list.
A function all-nonnegative?, which
returns true iff every number in the list
is zero or positive.
Hint: use the function you just wrote.
This might be even easier than modifying the template.
(6pts) Functions returning lists
A function
map-square
which consumes a list of numbers, and returns a list
of the squares of those numbers.
(The name "map" is traditional, coming from
the math sense:
a function mapping a bunch of inputs to a bunch of
outputs.)
A function substitute-millenium which takes in a List-of-Number, and returns a list like the input, except that wherever the input list contains 2000, the output list contains 2001. (If you prefer, you can instead write the more general substitute, which takes two additional arguments: the before- and after-numbers to substitute.)
(8pts) Telephone Directory
Once a year, the local telephone company publishes a directory. For our purposes, the directory is a list of entries. An entry consists of a symbol, called the key, and a phone number, represented as a number.
Note: when talking about types of data, this class is precise. Thus be aware that, for this problem, directory and entry are technical terms, used exactly where intended.
Write out the data definitions, examples, and template pertaining to this simple online phone directory. You should have one data definition for entries, and a second data definition for directories.
Write a program lookup that consumes a symbol and a phone directory and Clarify by deleting this clause: produces a phone number. lookup should examine the list for a key that matches the symbol given as input. If it finds a matching key, it returns the matching entry. If no matching entry is found, it returns false.
What, exactly, is the type of data returned by lookup? Write a quick data definition for it.
Write lookup-elvis, which takes in a directory,
and returns either Elvis's number in the directory,
or '411 (if Elvis isn't listed).
Update: return a symbol such as
'not-found instead.
Hint: use lookup.
Note that once you do so,
the more pertinent data template is the type returned by lookup,
instad of the template for directories, in this case.
Be sure to create a reasonable set of test data and enter it in the definitions window of DrScheme.
Schemers sometimes call a directory in this form an association list
Note: The fact that the second part of an entry is a number
was irrelevant to lookup; we could actually allow any
type of data to be associated with a key -- a symbol, a structure,
or even another list (perhaps, you have a "meta-directory",
where each person has their own personal directory ...
or even their own list of directories!).
(Slow down, breathe, and re-read that;
it takes a moment to think it through.)
To ponder (no response required):
Why did we ask to return the entire key/datum entry --
wouldn't it be more convenient to just return the associated datum?
(What would be problematic about this?)
An aside:
It turns out, 'dictionary' is a technical term, in computer science --
it is the Abstract Data Type where you insert some value & it's associated
key, and later lookup the value by key. E.g., the registrar software may
use a dictionary where the key is your student-id, and the associated value
is a struct containing all your data.
So more precisely: an association-list is one possible implementation of a Dictionary. There are other ways of implementing Dictionary (e.g., a list of key/value pairs which is sorted by key. If you go on to take comp212, you'll learn of hash tables, another implementation of Dictionary.)
We call it an *abstract* data type, because if you write code which uses a Dictionary somebody else wrote (say, in a Teachpack), your code just calls "lookup" and "update" [usually called "insert"]; you could care less what particular implementation the teachpack uses. (More importantly, you can later decide to switch to somebody else's Dictionary teachpack without having to modify one character of your own code.)