Shallow & Deep Binding - What would this program print?
Asked Answered
H

3

13

I'm not sure how to do this...

function f1()
{
    var x = 10;
    function f2(fx)
    {
        var x;
        x = 6;
        fx();
    };

    function f3()
    {
        print x;
    };

    f2(f3);
};

For each of the following two binding methods, what would the program print? A) Shallow Binding B) Deep Binding

Thanks for the help!

Heathcote answered 21/3, 2013 at 14:43 Comment(1)
see here. in a nutshell: deep binding prints 10 as the binding of x takes place when f2 is called, shallow binding prints 6 as x is bound when f3 (being a procedure parameter to f2) is called from f2.Lice
H
26

Deep/shallow binding makes sense only when a procedure can be passed as an argument to a function.

  • Deep binding binds the environment at the time a procedure is passed as an argument.
  • Shallow binding binds the environment at the time a procedure is actually called.

Deep binding.

Here f3() gets the environment of f1() and prints the value of x as 10 which is local variable of f1().

Shallow binding.

f3() is called in f2() and hence gets the environment of f2() and prints the value of x as 6 which is local to f2()

Headon answered 29/7, 2015 at 17:20 Comment(0)
K
1

• The environment of the call statement that enacts the passed subprogram (shallow binding)

• The environment of the definition of the passed subprogram (deep binding).

In some cases, the subprogram that declares a subprogram also passes that subprogram as a parameter. In those cases, deep binding and ad hoc binding are the same.

Kilah answered 24/4, 2017 at 1:48 Comment(0)
P
-1

Shallow binding : the environment of call statement that enacts the passed subprogram Deep binding : the environment of the definition of the passed subprogram Ad hoc binding : the environment of call statement that passed the subproblem as an actual parameter

Plutocrat answered 19/9, 2018 at 0:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.