*This preview shows
pages
1–3. Sign up to
view the full content.*

CS 61A
Fall 1997
Final exam solutions
1.
Applicative/normal order.
Applicative order means we evaluate the argument subexpression first, so
(square (inc))
==>
(square 6)
==>
36
Normal order means we pass the argument expression to the procedure, so
(square (inc))
==>
(* (inc) (inc))
==>
(* 6 7)
==>
42
Scoring:
1 point each, or 1 point for 42 and 36 (in the wrong order).
2.
Order of growth.
If f is Theta(g), then f+g is Theta(f):
True.
Handwavy explanation:
f is Theta(g) means that f(x) is close to a*g(x)
for some constant a.
Then f(x)+g(x) is close to f(x) + (1/a)*f(x),
which is (1 + 1/a)*f(x), which is a constant times f(x), which makes it
Theta(f).
More formal proof:
f is Theta(g) means that for large enough x,
a*g(x) < f(x) < b*g(x)
This implies that g is Theta(f), because
(1/b)*f(x) < g(x) < (1/a)*f(x)
So what about f(x) + g(x)?
(1 + 1/b)*f(x) < f(x) + g(x) < (1 + 1/a)*f(x)
So f+g is Theta(f) as desired.
Note on notation:
Many people write f=Theta(g) for "f is Theta(g)" but
there really is no equality involved, and Prof. Hilfinger won't let you
use the equal sign next semester!
Instead he uses the "is an element of"
symbol that sort of looks like a greek epsilon, taking Theta(g) as the
set of all functions that are Theta(g).
Scoring: 1 point for True, 0 points for False!
3.
Tree searching
You saw the code for breadth-first search in lecture:
(define (bfs tree pred)
(define (helper tasks)
(cond ((null? tasks) #f)
((pred (datum (car tasks))) (datum (car tasks)))
(else (helper (append (CDR TASKS) (CHILDREN (CAR TASKS)))))))
(helper (list tree)))
;
----------- ----------------------

This ** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*For breadth-first search, we want to see the already-queued tasks
(which include the siblings of the node we're looking at right now)
*before* we see the children of this node.
That's why we append
those two sets of tasks in the order seen above.
For depth-first search, we still want to see all the nodes eventually,
but we want to see the children of this node first.
So we want to have
the same tasks in the list for the recursive call, but in the other order:
(define (dfs tree pred)
(define (helper tasks)
(cond ((null? tasks) #f)
((pred (datum (car tasks))) (datum (car tasks)))
(else (helper (append (CHILDREN (CAR TASKS)) (CDR TASKS))))))
(helper (list tree)))
;
---------------------- -----------
Most of the errors on this question were either wild guesses (e.g., answers
containing LEFT-BRANCH and RIGHT-BRANCH) or wild data abstraction violations
(e.g., (CHILDREN TASKS) -- tasks is a forest, not a tree).
Scoring: 2 points if correct; 1 point if correct but swapped (depth before
breadth) or if one of the two is correct.
(That latter is a generous point,
since it probably means you copied the depth-first version from your
lecture notes without understanding it.
But at least you came to lecture.
:-)
4.
DDP according to Ben.
This was a subtle question, definitely requiring thought rather than just

This is the end of the preview. Sign up
to
access the rest of the document.