CS 61A
Week 13 homework solutions
4.22
LET in analyzing evaluator
This is easy, given the hint about 4.6.
We don't have to change the
procedure LET>COMBINATION we wrote for that exercise; since it deals
entirely with the expression, and not with the values of variables,
all of its work can be done in the analysis phase.
All we do is
change this COND clause in EVAL:
((let? exp) (eval (let>combination exp) env))
to this COND clause in ANALYZE:
((let? exp) (analyze (let>combination exp)))
4.23
Efficiency of analyzesequence
For a sequence with just one expression, the book's version does the
following analysis:
First, the body of analyzesequence is the LET.
Suppose that the result of analyzing the one expression is PROC.
Then the variable PROCS will have as its value a list whose only
element is PROC.
That's not null, so (still in the analysis part)
we call (LOOP PROC '()).
In LOOP, since (in this case) RESTPROCS
is null, LOOP just returns PROC.
So if the analysis of EXP gives
PROC, then the analysis of (BEGIN EXP) also gives PROC.
In the same oneexpression case, Alyssa's version returns
(lambda (env) (executesequence (list PROC) env))
So every time this execution procedure is called, executesequence
will check that (cdr procs) is empty, which it is, and then
calls PROC with the environment as its argument.
This test of
(null? (cdr procs)) is done for every execution, whereas in the
book's version it was done just once.
How about the twoexpression case.
Suppose that the analysis of
EXP1 gives PROC1, and the anaylsis of EXP2 gives PROC2.
The book's
version will call, in effect, (loop PROC1 (list PROC2)).
This
in turn calls (sequentially PROC1 PROC2), which returns
(lambda (env) (PROC1 env) (PROC2 env))
as the execution procedure.
(There is a recursive call to LOOP,
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 Harvey
 Prime number, Alyssa, kVp

Click to edit the document details