Computer Science 61A  Spring 1996  Harvey  Final Exam
Answers to CS61 A Final of May 23, 1997
We repeat the questions; some have typos corrected with respect
to the actual exam handed out.
Question 1 (5 points):
Let us represent a set S of unique expressions, atoms or
pairs, as a Lisp list of those expressions.
As you know,
H is a subset of S if each element e of H is also an
element of S.
The
powerset
of S is the set of all subsets of S.
The powerset of S includes the empty set and S.
Here is a recursive way of thinking about powersets as lists:
First, the powerset of () is (()). That is, it has one element, namely ().
Next, assume that S has at least one element e, and
let K be the rest of S. Let P be the powerset of K.
The powerset of S is the union of P and a set formed
by sticking e in front of each element of P.
Here are two examples:
The powerset of (1 2 3) is (() (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3))
The powerset of (1 (2 3)) is (() (1) ((2 3)) (1 (2 3)))
What is the powerset of '(1 (2 3) 4)?
Answer: 1 point.
no partial credit
( () (1) ((2 3)) (1 (2 3))
(4) (1 4) ((2 3) 4) (1 (2 3) 4))
all 8 elements.
What is the length of the list representing
the powerset of '(1 2 3 4 5)?
Answer: 1 point. no partial credit.
32.
(2^5 = 32)
Write a procedure
powerset which takes as its only argument a set S and
returns the powerset of S.
Answer: 3 points total
(define (powerset set)
(if (null? set)
(list '())
; 1 point for the base case
(append (powerset (cdr set))
; 1 point for the append
(map (lambda (s) (cons (car set) s))
;1 point for the map/lambda
(powerset (cdr set))))))
Some people copied the "subsets "
program. That was ok except if
you called it subsets instead of powerset.
Question 2 (7 points):
Draw the environment diagram resulting from the
following inputs to the Scheme system. What is
the value of the global variable
x at the end?
(define x 10)
(define (foo x) (define (bar y) (set! x y) x)
bar)
((foo 50) 60)
Answer:
global env has x:10 ,
foo pointing to function def.
1pt
Page 1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentComputer Science 61A  Spring 1996  Harvey  Final Exam
sub environment has x:50, then 60 and bar in it.
1 pt
sub sub environment has y:50 in it
1 pt
correct arrows
2 pt
x is 10 at the end
2 pt
Question 3 (5 points):
Consider the following piece of code:
(define foo (lambda (x) ( y x)))
(define bar foo)
(define y 7)
(parallelexecute
(lambda () (set! foo (let ((z y))
(lambda (x) (+ (bar x) z)))))
(lambda () (set! y (foo 3)))
)
What are the possible values of y?
ANSWER:
4, 11
(2 pts.)
1 off if you had wrong answers too.
The easy part is if branch 2 runs first.
y is set to ( 7 3), or 4
Next foo is redefined as though we did
(define foo (let ((z y))
(lambda (x) (+ (bar x) z))))
which is actually the same as
(define (foo x)(+ (bar x) 4))
where we have just put the value of z, namely the current
value of y, namely 4
in there.
So foo is
(define (foo x)(+ ( y x) 4))
(*)
in which case (foo 3) will be (+ ( 4 3) 4)
or 5
Another possibility is that branch 1 is executed first,
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 Harvey
 Computer Science, Empty set

Click to edit the document details