Homework #4Assigned: February 11
Due: February 18, 4:30pm, in class
- (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.
- (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
- (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.
- main calls fun1;
fun1 calls fun2;
fun2 calls fun3
- main calls fun2;
fun2 calls fun3;
fun3 calls fun1
- main calls fun3;
fun3 calls fun1
- main calls fun1;
fun1 calls fun3;
fun3 calls fun2
- main calls fun3;
fun3 calls fun2;
fun2 calls fun1
- (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).
- (10 points) [EOPL] Exercise 2.5 on p. 47.
- (15 points) [EOPL] Exercise 2.7 on p. 52.
|