views:

73

answers:

0

I'm studying dynamic/static scope with deep/shallow binding and running code manually to see how these different scopes/bindings actually work. I read the theory and googled some example exercises and the ones I found are very simple (like this one which was very helpful with dynamic scoping) But I'm having trouble understanding how static scope works.

Here I post an exercise I did to check if I got the right solution:

considering the following program written in pseudocode:

int u = 42; 
int v = 69;
int w = 17;
proc add( z:int )
  u := v + u + z
proc bar( fun:proc )
  int u := w;
  fun(v)
proc foo( x:int, w:int )
  int v := x;
  bar(add)
main
  foo(u,13)
  print(u)
end;

What is printed to screen

a) using static scope? answer=180

b) using dynamic scope and deep binding? answer=69 (sum for u = 126 but it's foo's local v, right?)

c) using dynamic scope and shallow binding? answer=69 (sum for u = 101 but it's foo's local v, right?)

PS: I'm trying to practice doing some exercises like this if you know where I can find these types of problems (preferable with solutions) please give the link, thanks!