Homework 7

This is the archived website of SI 413 from the Fall 2012 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

Use a separate sheet of paper for your answers! Everything should be submitted in one packet, all printed out for me to see.

0.1 Example Program A

This program is written in pseudocode that's sort of a cross between Python and Javascript. Hopefully the meaning of the syntax is straightforward; if not, ask!

def account(a):
    def withdraw(x):
        if a < x:
            return False
        else:
            a = a - x
            return True
    return withdraw # Notice a function is being returned!
 
var A = account(10)
var B = account(12)
 
print A(11)
print B(11)

0.2 Example Program B

This one looks like C++:

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;
  }
}
 
cout << foo(3) << endl;

1 AST

Draw the abstract syntax tree (AST) for Program A above.

2 Dynamic vs Lexical Scope

Give an example of a program in our spl language from labs that will have a different result under dynamic scoping vs. lexical scoping.

Show the program and indicate what the result will be in each case.

(Your examples do not have to be very complicated, but they should be unique - I want you to come up with your own!)

3 CRT

Trace the execution of Program B above using dynamic scoping implemented with a Central Reference Table (CRT). In what you turn in, show what the CRT looks like at the time when the stack of bindings for x has exactly 3 values in it.

Be sure to clearly indicate what is printed as the result of the entire program.