Question: local symbols and global symbols scope question

 

One of the things I like in Maple is that I can return a local symbol from a proc() in some expression and it will not "conflict" with same symbol in the global space and will show the same.

I just do not know how Maple manages to do this.

For example:

foo:=proc(n)
   local x;
   x^n;
end proc;

And now if I do

x:=99;
foo(3);

Will return  x^3. This is even thought I had defined x:=99; before the call.

So there is one global `x` with value 99 and the `x` in the expression returned `x^3` did not get confused with the global `x`. Yet they look the same.

How does Maple manages to do this? In Mathematica, it always return local symbols with $nnn assigned to them to distinguish them from global symbols. (attaches the Module ID). For example, in Mathematica the same example above gives

Notice that the `x` returned from a proc() look different from inside the Module. It is not the same as the x in the global space.

Maple seems to be able to do the same thing, but using the same looking symbol. So it must be keeping track of things internally? It must know that the x in x^3 is not the same x in the x:=99 ofcourse.

Any idea how Maple does this?

 

Please Wait...