Homework #4

Assigned: February 10
Due: February 17, 4:30pm


  1. (5 points) Identify a programming language where there is no distinction between global variables and those local to the main program. Is this distinction useful? Explain why or why not.

  2. (5 points) Assume the following Ada program was compiled and executed using static scoping rules. What value of X is printed in procedure Sub1? Under dynamic scoping rules, what value of X is printed in procedure Sub1?
    procedure Main is
       X : Integer;
     
       procedure Sub1 is
          begin -- of Sub1 is
             Put(X);
          end; -- of Sub1
    
       procedure Sub2 is
          X : Integer;
          begin -- of Sub 2
            X := 10;
            Sub1
          end; -- of Sub2
    
       begin -- of Main
          X := 5;
          Sub2
       end -- of Main 
    
  3. (10 points) Consider the following skeletal C program:
    void fun1() {
       int b, c, d;
       ...
    }
    
    void fun2() {
       int c, d, e;
       ...
    }
    
    void fun3() {
       int d, e, f;
       ...
    }
    
    main() {
       int a, b, c;
       ...
    }
    
    Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during the execution of the last function called? Include with each visible variable the name of the function in which it is defined.

    1. main calls fun1; fun1 calls fun2; fun2 calls fun3
    2. main calls fun2; fun2 calls fun3; fun3 calls fun1
    3. main calls fun3; fun3 calls fun1
    4. main calls fun1; fun1 calls fun3; fun3 calls fun2
    5. main calls fun3; fun3 calls fun2; fun2 calls fun1

  4. (10 points) We said in class that COMMON LISP, like Perl, gives the programmer the option of using static or dynamic scoping. Figure out how to set the scoping method in a COMMON LISP program and write a COMMON LISP program which clearly illustrates the difference between static and dynamic scoping akin to the Perl program in the lecture notes (no credit awarded for replicating that program in COMMON LISP). Use the clisp implementation of COMMON LISP which is freely available for most common operating systems (UNIX, Windows). Just writing a program demonstrating how to set the scoping method in COMMON is not enough. Your program must clearly demonstrate the difference between static and dynamic scoping akin to the Perl programs in the course notes.

  5. (10 points) [EOPL] Exercise 2.5 on p. 47.

  6. (15 points) [EOPL] Exercise 2.7 on p. 52. Your lexical-address function must be definited as follows:
    (define lexical-address
      (lambda (exp)
        (unparse-expression (parse-expression exp))))
    

Return Home