A table is a data structure that can hold any number of elements, as a list can, but accesses those arguments through user-specified keys rather than by position. Let's say that the interface to tables consists of five procedures:
make-table, a procedure that takes no
arguments and returns an empty table.look-up-in-table, which takes two arguments -- a
table and a key -- and returns either (1) a pair in which the car is the
given key and the cdr is the value stored in the given table under that
key, or (2) the Boolean value #f, which should be returned when the
given table does not contain any value stored under the given key.add-to-table, which takes three arguments -- a
table, a value to be stored in the table, and the key through which the
value should be accessed -- and returns a table just like the given table,
except that the given value is now accessible (if one uses look-up-in-table) through the given key. If the last argument in a call
to the add-to-table procedure is a key that is already in use, the
new value should supersede the old one. In other words, the value that was
accessible through the given key in the given table will not be accessible
through the same key in the table that add-to-table returns;
instead, that key provides access to the new value in the new table.size-of-table procedure takes any table as argument and
returns the number of values stored in it.keys-of-table procedure takes any table as argument and
returns a list of the keys through which values stored in the table can be
accessed.
Keys may be symbols, numbers, or strings, so you should use Scheme's
built-in equal? predicate to compare them.
Once you have implemented this table interface, design, write, and test the following procedures, using your implementation of tables:
print-table: Using display and newline, print
out the values stored in a given table, indicating also the key through
which each value can be accessed.spectrum: Given any list ls of symbols, spectrum should construct and return a table in which the keys are the
symbols that occur in ls and each of the corresponding values is a
positive integer indicating the number of times the key symbol occurs in
ls. For instance, given the list (the cat sat on the mat),
spectrum should return a table in which the keys are the symbols
the, cat, sat, on, and mat and the
corresponding values are respectively 2, 1, 1, 1, and 1.invert-table: Given any table tbl that contains no
duplicate values, construct and return a new table in which the values are
the keys of tbl and the keys are the values of tbl.This exercise will be due at 9 a.m. on Wednesday, March 10.
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~stone/courses/scheme/exercises/6.xhtml
created March 5, 2004
last revised March 5, 2004