Class 17: Lexical Scope with Closures and Frames

Downloads

Homework

Reading (for next class): PLP, Chapter 7 through 7.1.1, the box at the bottom of page 292 on "Dynamic typing", and 7.10.

Exercises

Note: you will have an opportunity to correct this homework while we (briefly) go over it at the beginning of class. You might want to use pencil.
  1. Consider the following SPL code, which we will imagine is lexically scoped:
    new mkcd := lambda a { 
      ret := lambda x { 
        if (a < x) { ret := true; } 
        else{ a := a - x; ret := false; }
      };
    };
    new A := mkcd(10);
    new B := mkcd(12);
    write A(11);
    write B(11);
        
    Draw all the frames and links that result after executing this program. See the reading assigned last class for exactly how these should be drawn, particularly Section 3.2.3.
    In particular, 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.)