Your name:
A
CS 3 (Clancy) Exam 1
September 30, 1991
Read and fill in this page now.
Do NOT turn the page until you are told to do so.
Your name:
(please printÑlast name, first name)
Your lab section day and time:
Your lab t.a.:
Name of the person sitting to your left:
Name of the person sitting to your right:
Problem 0 Total: /40
Problem 1
Problem 2
Problem 3
This is an open-book test. You have approximately fifty minutes to
complete it. You may consult any books, notes, or other inanimate objects
available to you. To avoid confusion, read the problems carefully. If you find
it hard to understand a problem, ask us to explain it. If you have a question
during the test, please come to the front or the side of the room to ask it.
This exam comprises 20% of the points on which your final grade
will be based. Partial credit will be given for wrong answers.
You are not to use setf within functions. Any other Lisp construct
described in Touretzky chapters 1-6, in part I of the ÒDifference Between
DatesÓ case study, or in ÒInteger Division and its UsesÓ is ok, though.
Your exam should contain 4 problems (numbered 0 through 3) on 9
pages. Appendix A and Appendix B from part I of the ÒDifference Between
DatesÓ case study appear on the last page of the exam. Please write your
answers in the spaces provided in the test; in particular, we will not grade
anything on the back of an exam page unless we are clearly told on the front
of the page to look there.
RelaxÑthis exam is not worth having heart failure about.
Problem 0 (2 points)
Put your name on each page. Also make sure you have provided the
information requested on the first page.
Problem 1 (7 points)
Fill in the blanks in the following expression so that the result
returned is (january february march april may), and indicate clearly
with arrows where quotes should appear.
(
( january february )
(
march
( april may ) ))
Problem 2
Part a (3 points)
Give the result, if any, of evaluating the following expression with the
ÒDifference Between DatesÓ program in Appendix B. (Note that the inputs to
days-between are out of order.) If an error occurs during evaluation,
indicate what function it occurs in and describe it as completely as possible.
Show your work for full credit.
(days-between '(april 1) '(march 31))
Part b (3 points)
Do the same for the following expression. (Note that the second date
doesnÕt contain enough elements.)
(days-between '(march 1) '(march))
Problem 3
This problem concerns some Lisp functions to manipulate height
measurements. A height is defined as one of the following:
¥ a one-element list ((INCHESÊnumber-of-inches)), where number-
of-inches is between 1 and 11, inclusive;
¥ a one-element list ((FEETÊnumber-of-feet)), where number-of-feet
is greater than 0;
¥ a two-element list ((FEETÊnumber-of-feet)Ê(INCHESÊnumber-of-
inches)), where number-of-feet is greater than 0 and number-of-
inches is between 1 and 11, inclusive.
Some example height measurements and their representations are shown
in the table below.
measurement representation as a height
4 feet 0 inches (or 3 feet 12
inches, or 2 feet 24 inches, or 1
foot 36 inches, or 48 inches) ((FEETÊ4))
4 feet 1 inch (or 3 feet 13
inches, etc.) ((FEETÊ4)Ê(INCHESÊ1))
0 feet 11 inches ((INCHESÊ11))
Part a (4 points)
Use assoc to write a function num-inches that is given a legal height
as input, and returns the number of inches specified in the height. Some
examples:
input to num-inches value returned
((FEETÊ4)Ê(INCHESÊ2)) 2
((FEETÊ1)) 0
((INCHESÊ2)) 2
Write your function in the space below.
(defun num-inches (height)
)
Part b (7 points)
Write a function make-height that, given a number of feet and a
number of inches as inputs, returns the corresponding height. You may
assume that the number of feet is at least 0, that the number of inches is
between 0 and 11, inclusive, and that at least one of the inputs is nonzero.
Here are some examples.
first input
to make-height second input
to make-height value returned
4 2 ((FEETÊ4)Ê(INCHESÊ
))
4 0 ((FEETÊ4))
0 1 ((INCHESÊ1))
Write your function in the space below.
(defun make-height (feet inches)
)
Part c (4 points)
Suppose now that the num-inches and make-height functions that
you wrote for the preceding two parts work correctly. Suppose that in
addition someone has written for you a function num-feet that, given a
height, returns the integer number of feet specified in the height, as shown
below.
input to num-feet value returned
((FEETÊ3)Ê(INCHESÊ4)) 3
((FEETÊ3)) 3
((INCHESÊ4)) 0
Consider now the problem of determining the height that represents
the difference between two heights. To find this, we might write a function
height-between. Given, for instance, the heights ((FEETÊ4)Ê(INCHESÊ8))
and ((FEETÊ3)Ê(INCHES 5)), height-between would return the height
((FEETÊ1)Ê(INCHES 3)).
One may determine the difference between two heights in (at least)
two ways, one corresponding to each approach taken in the ÒDifference
Between DatesÓ case study. One way, similar to the first approach taken in
the case study, is to consider three possibilities for the two heights, as
expressed in the following function framework:
; Given two heights, with the first higher than the second,
; return a height that represents the difference between them.
(defun height-between (height1 height2)
(cond
((= (num-feet height1) (num-feet height2))
(make-height
0
(- (num-inches height1) (num-inches height2)) ))
((< (num-inches height1) (num-inches height2))
(make-height
))
( T
(make-height
(- (num-feet height1) (num-feet height2))
(- (num-inches height1) (num-inches height2)) )) ))
Fill in the arguments to make-height in the second cond pair above.
Part d (3 points)
List three calls to height-between that together would test all parts
of the cond. If you wish, you may use the make-height function in your
calls.
Part e (3 points)
Design a version of height-between according to the second
approach taken in the case study (the one represented by the code in
Appendix B), and write its Lisp code below. DonÕt write the Lisp code for
functions called by height-between; pretend that these have already been
written. Do give them names that suggest their purpose, however.
; Given two heights, with the first higher than the second,
; return a height that represents the difference between them.
(defun height-between (height1 height2)
)
Part f (4 points)
Your height-between function in part e called some functions that
are not already provided in Lisp. For each such function, do the following:
describe the set of inputs that the function will receive if height-between is
called as described in the comment above, and describe the functionÕs
possible outputs if it works correctly.
function description of inputs description of output