CSCI 2322 Homework 1
Consider the following definition of the natural numbers (zero and the
positive integers):
zero ()
one (())
two ((()) ())
three (((()) ()) (()) ())
four ((((()) ()) (()) ()) ((()) ()) (()) ())
and so on.
Your assignment is to define two procedures, add and subtract, that
operate on numbers defined in the above manner. Also define two helper
procedures, successor and predecessor, and use them in your definitions
for add and subtract. Successor returns the number that is one greater
than its argument, and predecessor returns the number that is one less
than its argument. Add and subtract return the sum and difference of
their two arguments, respectively. Since we are not dealing with
negative numbers, subtract should only be defined to handle positive
differences (e.g., 4 - 2, not 6 - 9). You may not convert numbers in
the above form to regular numbers and use the Scheme procedures + and
-. Add and subtract must be defined to operate on numbers defined as
lists. If you wish to define procedures to convert between regular
numbers and numbers defined as lists, you may do so for extra credit.
This assignment will use concepts we have already reviewed in class,
such as procedure definition, conditionals, and recursion. Remember to
analyze the base case and general case when defining a recursive
procedure.
Once you have written your add and subtract procedures, run them on
several different arguments to demonstrate they work. Use
(transcript-on "filename") and (transcript-off) to save your work.
Email me your code and transcript as text files in separate
attachments.
Your homework is due at the beginning of class on Sept. 22. Warning: I
do not check my office email on the weekends. If you need help with
the assignment, come see me during office hours.