This homework has two types of problems:
Self Check: You are strongly encouraged to think about and work through these questions, but you will not submit answers to them.
Problems: You will turn in answers to these questions.
The programming problems for this week are in Lab 1.
(Required) Mitchell, Chapter 3.
(As Needed) The Lisp Tutorial from the "Resources" web page, as needed for the programming questions.
(Optional) J. McCarthy, Recursive functions of symbolic expressions and their computation by machine, Comm. ACM 3,4 (1960) 184--195. You can find a link to this on the cs334 web site. The most relevant sections are 1, 2 and 4; you can also skim the other sections if you like.
Mitchell, Problem 3.1
Evaluation of a Lisp expression can either terminate normally (and
return a value), terminate abnormally with an error, or run forever.
Some examples of expressions that terminate with an error are
(/ 3 0), division by 0; (car ’a), taking the car of an atom;
and (+ 3 "a"), adding a string to a number. The Lisp system
detects these errors, terminates evaluation, and prints a message to
the screen. Suppose that you work at a software company that builds
word processing software in Impure Lisp (It's been done: Emacs!).
Your boss wants to handle errors in Lisp programs without
terminating the computation, but doesn't know how, so your boss asks
you to ...
...implement a Lisp construct (error? E) that detects whether
an expression E will cause an error. More specifically, your
boss wants evaluation of (error? E) to (1) halt with value
\mathit{true} if evaluation of \tt E would terminate in
error, and (2) halt with value \mathit{false} otherwise.
Explain why it is not possible to implement the error?
construct as part of the Lisp environment.
...implement a Lisp construct (guarded E) that either executes
E and returns its value, or if E would halt with an error,
returns 0 without performing any side effects. This could be
used to try to evaluate E and if an error would occur, just
use 0 instead. For example,
(+ (guarded E) E') ; just E' if E halts with an error; E+E' otherwise
will have the value of \tt E' if evaluation of \tt E would
halt in error, and the value of \tt E + E' otherwise. How
might you implement the guarded construct? What difficulties
might you encounter? Notice that unlike (error? E), evaluation
of (guarded E) does not need to halt if evaluation of \tt E
does not halt.
Mitchell, Problem 3.2
(Based on Mitchell, Problem 3.5)
This question asks you to think about garbage collection in Lisp and compare our definition of garbage in the text to one given in McCarthy's 1960 paper on Lisp. McCarthy's definition is written for Lisp specifically, while our definition is stated generally for any programming language. Answer the question by comparing the definitions as they apply to Lisp only. Here are the two definitions.
Garbage, our definition: At a given point in the execution of a program P a memory location m is garbage if no continued execution of P from this point accesses location m.
Garbage, McCarthy's definition: "Each register that is accessible to the program is accessible because it can be reached from one or more of the base registers by a chain of car and cdr operations. When the contents of a base register are changed, it may happen that the register to which the base register formerly pointed cannot be reached by a car-cdr chain from any base register. Such a register may be considered abandoned by the program because its contents can no longer be found by any possible program.''
a. If a memory location is garbage according to our definition, is it necessarily garbage according to McCarthy's definition? Explain why or why not.
b. If a location is garbage according to McCarthy's definition, is it garbage by our definition? Explain why or why not.
c. There are garbage collectors that collect everything that is garbage according to McCarthy's definition. Would it be possible to write a garbage collector to collect everything that is garbage according to our definition? Explain why or why not.
fastexp Running TimeShow that your fastexp implementation from Lab 1
is indeed faster than the
original by determining a bound on the number of multiplication
operations required to compute (fastexp base e). Prove that
bound is correct by induction (as in the example proof above),
and then compare it to the bound of e-1 from the first
algorithm. Use the proof of power as a model for how to structure the proof.
Hint
For fastexp, it may be easiest to think about the number
of multiplications required when exponent e is 2^k for some
k. Determine the number of multiplies needed for exponents of
this form and then use that to reason about an upper bound for
the others.
The following property of the \log function may be useful in your proof:
For example, 1 + \log_2(n) = \log_2(2) + \log_2(n) = \log_2(2 n).
Submit your answers to the GradeScope assignment named, for example, "HW 0". It should:
You will be asked to resubmit homework not satisfying these requirements. Please select the pages for each question when you submit.