CMPS 240 Summer 2019
Prog. Assg. #5: Recursive List Utilities
Background
In the late 1950's, John McCarthy of MIT developed LISP
("LISt Processing"), the first
functional programming language.
In such languages, the standard way to perform iteration is via
recursion, as opposed to loops.
As suggested in its name, lists play a central role in LISP.
In particular, lists are viewed as being recursive in structure,
which makes it natural to process them using recursive code.
Specifically, a list can be either
- empty, or
- composed of a head and a tail, the former of
which is a single element and the latter of which is a
list.
Suppose that we use [] to denote the empty list
and [x,y] to denote the (nonempty) list whose
head is x and whose tail is y.
Then, as an example, the 4-element list containing dog,
cat, elk, and cow (in that order) would be
viewed as having the structure suggested by
[dog, [cat, [elk, [cow, []]]]]
That is, this list's head is dog and its tail is
[cat, [elk, [cow, []]]]
For reasons having to do with the names of the registers in the
computer on which the language was first implemented, in LISP
the head of a list is referred to as its CAR and the
tail as its CDR (pronounced "cudder"). In our work,
we will use the more meaningful words "head" and "tail".
However, we will retain "cons", which is LISP's name for the
function that constructs a list from an element x and a list y,
to form the list [x,y].
Task
Here are the Java source code files that you will need:
- RecursiveList: interface listing
the signatures of methods that any implementing class must include.
In effect, it describes the minimal set of capabilities that must
be possessed by any "recursive list" in the spirit of LISP.
- RecursiveListEmpty:
implements the RecursiveList interface;
instances represent empty lists.
- RecursiveListNonEmpty:
implements the RecursiveList interface;
instances represent nonempty lists.
- RecursiveListUtils:
has several "utility" methods that operate upon recursive lists.
It is included here mostly to provide the student with a model
of how to develop utilities that operate on recursive lists.
(The only method really needed for this assignment is
listify(), as it is invoked in the Tester application.)
- RecursiveListUtils2:
a sister to the RecursiveListUtils class (see above),
this class also has several "utility" methods that
operate upon recursive lists.
Seven of them are unfinished; your task is to complete them.
- RecListUtils2Tester:
application program for the purpose of testing the methods that
you developed in the RecursiveListUtils2 class.
Of course, you are free to adjust this class to suit your
testing needs. (But do not submit it!)
- IntegerComparator:
implements the java.util.Comparator interface;
given two Integer objects, the compare() method
reports whether the relationship between them is <, >, or =.
In case you want to see the methods in RecursiveListUtils
"in action", you can make use of
RecListUtilsTester.