Homework 8: Scope Tree, Frames and Closures
- Print version, with cover page (pdf)
- Due Date: Monday, October 25
1 Scope Tree
Consider the following program in a C-like syntax:
int x = 10;
int i = 5;
int foo(x) {
if (x == i) {
return 3;
}
else {
int i = x - 1;
int j = foo(i);
return 3 * j;
}
}
print foo(3);
Draw the scope tree for the program above. Then indicate what the final printed value would be using lexical scoping.
2 Frames and closures
Consider the following SPL code, which we will imagine is lexically scoped:
new counter := lambda start {
new increment := lambda by {
start := start + by;
ret := start;
};
ret := increment;
};
new A := counter @ 0;
new B := counter @ 5;
write A@0;
write B@0;
write A@6;
Draw all the frames and links that result after executing this program. See the reading assigned from Unit 6 for exactly how these should be drawn, particularly Section 3.2.3 of SICP.
Specifically, every variable name that refers to a function should point to a closure, which is represented by a pair of circles, pointing to the referencing environment and the function definition, respectively. (You do NOT have to write out the complete body of every function.)