Your name:
A 1
A
CS 3 (Clancy) Exam 1
February 24, 1992
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: /30
Problem 1
Problem 2
Problem 3
Problem 4
Problem 5
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 15% 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 6 problems (numbered 0 through 5) on 8
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 (6 points)
In each part of this problem, you are to insert parentheses and quotes
in an expression so that it evaluates to a given result. Indicate quote marks
with arrows so we can distinguish them from stray marks on the paper.
Some of the parts may require use of the empty list, represented by ( ).
2 pts each: -1 per error category (quoting,
parenthesization)
Part a
Insert parentheses and quotes in the following expression so that the
result returned is the list ((januaryÊfebruary)).
list january february
(list '(january february))
Part b
Insert parentheses and quotes in the following expression so that the
result returned is the list ((januaryÊfebruary)).
cons january february
(cons '(january february) ())
Part c
Insert parentheses and quotes in the following expression so that the
result returned is the list ((januaryÊfebruary)).
append january february
(append '((january february)) ())
Problem 2 (4 points)
Part a
Suppose that the functions in the program in Appendix A of the
Difference Between Dates case study have been defined. What is the result
of evaluating the expression (days-remaining '(may 12))?
answer is 20
1 pt
Part b
List below which other functions in the program in Appendix A of
the Difference Between Dates case study are called as a result of evaluating
the expression (days-remaining '(may 12)). For each function, indicate
what inputs it is given and what result it returns.
function input(s) result returned
days-in-month may 31
month-name (may 12) may
date-in-month (may 12) 12
1 pt each; must be completely correct; must give list where
appropriate
Problem 3 (9 points)
Part a
Write a function earliest-to-latest? that, given three dates as
inputs, returns true exactly when the first date comes at or before the
second date and the second date comes at or before the third. (I.e. the three
inputs are ordered from earliest to latest.) You may use any functions from
the Difference Between Dates programs without defining them. If your
answer includes auxiliary functions, supply their definitions along with
the definition of earliest-to-latest?.
(defun earliest-to-latest? (d1 d2 d3)
(<= (day-of-year d1) (day-of-year d2) (day-of-year d3)) )
or
(defun earliest-to-latest? (d1 d2 d3)
(and
(<= (day-of-year d1) (day-of-year d2))
(<= (day-of-year d2) (day-of-year d3)) ) )
4 pts (-1 per small error, -2 for logical error)
Part b
The program in Appendix B of the Difference Between Dates case
study computes the difference between two dates in a non-leap year.
Complete the function days-between-in-leap-year below, which returns
the days between two dates in a leap year. (Recall that a leap year has an
extra day at the end of February.)
You may use the function earliest-to-latest? from part a; assume
that it works as intended, regardless of what you wrote for part a. If your
answer includes auxiliary functions, supply their definitions along with
the definition of days-between-in-leap-year.
; Return the difference in days between date1 and date2.
; date1 and date2 both represent dates in a leap year, with
; date1 being the earlier of the two.
(defun days-between-in-leap-year (date1 date2)
(if
(not (earliest-to-latest? date1 '(february 29) date2))
(days-between date1 date2)
(+ 1 (days-between date1 date2)) ) )
3 pts (-1 per error, like quoting, dates out of order, or
missing not)
Part c
Provide a call to days-between-in-leap-year for which the
condition in the if statement is true, and another call for which the
condition is false.
2 pts (1 pt each test case; no credit if quoting bad)
Problem 4 (5 points)
Consider the function below.
(defun exam1 (x)
(member x '(january february march)) )
Part a
What are the possible results that the exam1 function can return?
2 pts total
nil, (january february march), (february march), (march)
-1 per error, up to a total of 2
Part b
Supply a table in the exam2 function so that, if exam1 and exam2
are given the same input, they return the same result.
(defun exam2 (x)
(assoc x
'((january february march) (february march) (march))
3 pts total
1 pt if three-element table
2 pts if small error (e.g. no quote, bad parens)
3 pts for perfect answer (note that order of the elements
doesn't matter.
) )
Problem 5 (4 points)
Suppose that, along with the functions from Appendix B in the
Difference Between Dates case study, the following functions have been
defined:
corresponding-month
takes an integer between 1 and 365 as inputÑa day of the yearÑand
returns the name of the month that contains the day. For example,
(corresponding-month 30) returns january, (corresponding-
month 32) returns february, and (corresponding-month 365)
returns december.
corresponding-date
takes an integer between 1 and 365 as inputÑa day of the yearÑand
returns the date in the month that contains the day. For example,
(corresponding-date 30) returns 30, (corresponding-date 32)
returns 1, and (corresponding-month 365) returns 31.
Complete the function works-correctly? below. Given a day of the
year as input, it returns true exactly when the results of calling
corresponding-month and corresponding-date give a date that, when
given as input to the day-of-year function, returns the original day of the
year.
(defun works-correctly? (day-number)
(= day-number
(day-of-year
(list (corresponding-month day-number)
(corresponding-date day-number) ) )
2 pts for correct order of function application
all or none?
2 pts for constructing the list argument correctly
-1 for using cons rather than list
)
)