Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7498349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:26:36+00:00 2026-05-29T19:26:36+00:00

I’m studying dynamic/static scope with deep/shallow binding and running code manually to see how

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:26:37+00:00Added an answer on May 29, 2026 at 7:26 pm

    Your answer for lexical (static) scope is correct. Your answers for dynamic scope are wrong, but if I’m reading your explanations right, it’s because you got confused between u and v, rather than because of any real misunderstanding about how deep and shallow binding work. (I’m assuming that your u/v confusion was just accidental, and not due to a strange confusion about values vs. references in the call to foo.)

    a) using static scope? answer=180

    Correct.

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

    Your parenthetical explanation is right, but your answer is wrong: u is indeed set to 126, and foo indeed localizes v, but since main prints u, not v, the answer is 126.

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

    The sum for u is actually 97 (42+13+42), but since bar localizes u, the answer is 42. (Your parenthetical explanation is wrong for this one — you seem to have used the global variable w, which is 17, in interpreting the statement int u := w in the definition of bar; but that statement actually refers to foo‘s local variable w, its second parameter, which is 13. But that doesn’t actually affect the answer. Your answer is wrong for this one only because main prints u, not v.)


    For lexical scope, it’s pretty easy to check your answers by translating the pseudo-code into a language with lexical scope. Likewise dynamic scope with shallow binding. (In fact, if you use Perl, you can test both ways almost at once, since it supports both; just use my for lexical scope, then do a find-and-replace to change it to local for dynamic scope. But even if you use, say, JavaScript for lexical scope and Bash for dynamic scope, it should be quick to test both.)

    Dynamic scope with deep binding is much trickier, since few widely-deployed languages support it. If you use Perl, you can implement it manually by using a hash (an associative array) that maps from variable-names to scalar-refs, and passing this hash from function to function. Everywhere that the pseudocode declares a local variable, you save the existing scalar-reference in a Perl lexical variable, then put the new mapping in the hash; and at the end of the function, you restore the original scalar-reference. To support the binding, you create a wrapper function that creates a copy of the hash, and passes that to its wrapped function. Here is a dynamically-scoped, deeply-binding implementation of your program in Perl, using that approach:

    #!/usr/bin/perl -w
    
    use warnings;
    use strict;
    
    # Create a new scalar, initialize it to the specified value,
    # and return a reference to it:
    sub new_scalar($)
      { return \(shift); }
    
    # Bind the specified procedure to the specified environment:
    sub bind_proc(\%$)
    {
      my $V = { %{+shift} };
      my $f = shift;
      return sub { $f->($V, @_); };
    }
    
    my $V = {};
    
    $V->{u} = new_scalar 42; # int u := 42
    $V->{v} = new_scalar 69; # int v := 69
    $V->{w} = new_scalar 17; # int w := 17
    
    sub add(\%$)
    {
      my $V = shift;
      my $z = $V->{z};                     # save existing z
      $V->{z} = new_scalar shift;          # create & initialize new z
      ${$V->{u}} = ${$V->{v}} + ${$V->{u}} + ${$V->{z}};
      $V->{z} = $z;                        # restore old z
    }
    
    sub bar(\%$)
    {
      my $V = shift;
      my $fun = shift;
      my $u = $V->{u};                     # save existing u
      $V->{u} = new_scalar ${$V->{w}};     # create & initialize new u
      $fun->(${$V->{v}});
      $V->{u} = $u;                        # restore old u
    }
    
    sub foo(\%$$)
    {
      my $V = shift;
      my $x = $V->{x};                     # save existing x
      $V->{x} = new_scalar shift;          # create & initialize new x
      my $w = $V->{w};                     # save existing w
      $V->{w} = new_scalar shift;          # create & initialize new w
      my $v = $V->{v};                     # save existing v
      $V->{v} = new_scalar ${$V->{x}};     # create & initialize new v
      bar %$V, bind_proc %$V, \&add;
      $V->{v} = $v;                        # restore old v
      $V->{w} = $w;                        # restore old w
      $V->{x} = $x;                        # restore old x
    }
    
    foo %$V, ${$V->{u}}, 13;
    print "${$V->{u}}\n";
    
    __END__
    

    and indeed it prints 126. It’s obviously messy and error-prone, but it also really helps you understand what’s going on, so for educational purposes I think it’s worth it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.