I’m working on this problem and I got the answers :
Statically: 13
Dynamically -deep binding: 2 <- i’m not sure about this one
Dynamically -shallow binding: 2 <- i’m not sure about this one
is that correct?
Consider the program below (in a Pascal like language). What is the output of the
language is statically scoped? What is the output of the language is dynamically scoped
and uses deep binding? What is the output of the language is dynamically scoped and
uses shallow binding?
Program main;
x: integer := 2;
y: integer := 1;
procedure f3(z: integer)
begin
x = z + x + y;
end
procedure f2(p: procedure, z: integer)
int x := 5;
begin
p(z)
end
procedure f1(z: integer)
int y := z
begin
f2(f3,y);
end
begin /* main program */
f1(4);
print(x)
end
For the static scope and the dynamic-scope-with-shallow-binding cases, why not try them out? Using Perl with static scope:
I get
7(which is4 + 2 + 1). Changing themys tolocals to get dynamic scope with shallow binding, I get2, as you predicted.Testing out dynamic scope with deep binding is trickier, because so few languages support it. In this answer a while back, I posted Perl code that implemented deep binding “manually” by passing around a hash of references to scalars; using that same approach:
I get
10(which is4 + 2 + 4).