3
$\begingroup$

I am perpetually confused when it comes to static scoping and dynamic scoping and their differences and Wikipedia isn't of any help, so here's my question:

Given the following pseudo code in a hypothetical programming environment, what are the outputs under the following conditions?

  • call by need using static scope
  • call by name using dynamic scope

global int n = 100, m = 5;
void fun(x){
    int n = 10;
    print(x + 10);
    n = 200;
    m = 20;
    print(x);
}
main(){
    fun(n + m);
}

Additionally, what would be two environments where I can run this code to check the output with the conditions? C uses lexical scope, but it does not support call by need while Bash supports dynamic scope, I guess!

$\endgroup$
3
$\begingroup$

Here is the simulation by hand.

  • call by need using static scope

    The function receives as x parameter the thunk [n+m] bound to the globals. Then local n gets 10. The print statement requires evaluation of the thunk, which becomes 100+5, i.e. 105. It prints 105+10, i.e. 115. Local n and global m are changed, but this cannot affect x for two reasons: the thunk did not depend on these local variables (because statically bound to the global ones), and it is already evaluated to 105 anyway, which is printed by the second print.

    result: 115 105

  • call by name using dynamic scope

    The function receives as x parameter the unbound expression [n+m]. Then local n gets 10. Then x gets evaluated, with local n=10 and global m=100, i.e. 110, and added 10 to print 120. Local n=200 and global m=20. The parameter x is reevaluated as 200+20, i.e. 220, which gets printed.

    result: 120 220

How to test it? One would have to check various implementations of programming languages. I do not remember off hand.

Call by need is usually found in functional languages, which normally do not have assignment. But some still allow it. So I would look for that regarding the first case. I think CAML may provide it.

Call by name is fairly rare, and rather weird in a dynamic scope context. I do not know of any language that will offer that combination. The best is to use a dynamic binding langage and simulate the example by explicitly replacing the call by name argument by a parameterless function that has the parameter as body.

In the example, you would call in main(): fun(lambda().n+m), and in the body of the function fun you would replace x by the call x().

$\endgroup$
-3
$\begingroup$

resault of static and dynamic scope:

step 1 : print 115

then n=200 , m=20 it is not affect on x so :

step 2 : print 105

$\endgroup$
  • 2
    $\begingroup$ can you explain how you got these results? anyways the code two print statements per execution so there should be four results! $\endgroup$ – mellartach Oct 13 '13 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.