This preview shows pages 1–2. Sign up to view the full content.
Page 1
CS 61AWeek 7 solutions
LAB ASSIGNMENT:
1.
Scheme1 stuff.
(a)
((lambda (x) (+ x 3)) 5)
Here's how Scheme1 handles procedure calls (this is a COND clause
inside EVAL1):
((pair? exp) (apply1 (eval1 (car exp))
(map eval1 (cdr exp))))
The expression we're given is a procedure call, in which the procedure
(lambda (x) (+ x 3)) is called with the argument 5.
So the COND clause ends up, in effect, doing this:
(apply1 (eval1 '(lambda (x) (+ x 3))) (map eval1 '(5)))
Both lambda expressions and numbers are selfevaluating in Scheme1,
so after the calls to EVAL1, we are effectively saying
(apply1 '(lambda (x) (+ x 3)) '(5))
APPLY1 will substitute 5 for X in the body of the
lambda, giving the expression (+ 5 3), and calls EVAL1
with that expression as argument.
This, too, is a procedure call.
EVAL1 calls itself recursively to evaluate
the symbol + and the numbers 5 and 3.
The numbers are selfevaluating
EVAL1 evaluates symbols by using STk's EVAL, so it gets the primitive
addition procedure.
Then it calls APPLY1 with that procedure and
the list (5 3) as its arguments.
APPLY1 recognizes that the addition
procedure is primitive, so it calls STk's APPLY, which does the
actual addition.
(b) As another example, here's FILTER:
((lambda (f seq)
((lambda (filter) (filter filter pred seq))
(lambda (filter pred seq)
(if (null? seq)
'()
(if (pred (car seq))
(cons (car seq) (filter filter pred (cdr seq)))
(filter filter pred (cdr seq)))))))
even?
'(5 77 86 42 9 15 8))
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
 mckenzie

Click to edit the document details