Sunday, January 18, 2009

Ch2, Ex2

Its a necessary step because N is Free variable identifier and it may or may not exist in the environment of the caller. Hence to make sure of the presence of N, N-> 3 (in general mapping for all Free identifiers) is added to contextual environment.This is in terms with Lexical scoping or else it'll become dynamic scoping.

An example where N does not exist at time of call:

declare MulByN in
local N in
N = 3
proc {MulByN X ?Y}
Y = N*X
end
end
{Browse {MulByN 5 $}}

An exmple where N is not bound to 3 at the time of the call

declare MulByN N in
local N in
N = 3
proc {MulByN X ?Y}
Y = N*X
end
end
N = 6
{Browse {MulByN 5 $}}

1 comment:

  1. Example where N is not bound to 3 at the time of the call:

    declare MulByN in
    proc {MulByN X ?Y}
    Y = N*X
    end

    local N in
    N = 6
    {Browse {MulByN 5 $}}
    end

    The above code outputs 30.
    Your code uses N=3, so it prints 15

    ReplyDelete