MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: can one make local symbol in a pure function?

On 7/20/2012 10:46 PM, Bill Rowe wrote:

> This makes it sound like you want to have a local variable in
> your function that does not create a new global symbol.

Yes, of course. I simply wanted to localize everything at the
point where I wanted to use it. Did not want make a separate

  If this
> is your goal, you should be aware Module does not do this. If
> you start a fresh session and do
> ?Global`*
> f[x_]:=Module[{a=2},x^a]
> f[Range@3];
> ?Global`*
> you will find that new symbols a, a$ appear in the Global
> context. They simply don't have values in the Global context

I think this is terrible. Why does this happen? It makes no
sense to me. A local symbol should not move to the global name
space. You did not even need to make a call. Just the definition
itself was enough:

a	a$	f	x

For me, I would have expected to see ONLY 'f' here in the
global name space. Everything else would be contained inside
the module namespace.  i.e. can be seen inside the Module.

'x' above is a parameter name (ok pattern name in Mathematica),
but from programming point of view, it is a formal parameter.

Why would the name of a formal parameter suddenly become global
just by defining a function?

Why does Mathematica do this? It seems it should be possible
to easily fix this. Simply do not put these symbols into the global
name space table when reading the Module. Why is this hard to do?

Will fixing break things? Any one depending on the local
names defined inside a module be also a global symbol should not doing
that to start with as it is against everything we learned about
programming in school.

The evaluator knows it is reading a Module. Hence make a symbol table
for this Module only.  It would have been less bad if it added things

f  f`a$  f`x$

or such, to the global name space. At least this avoids a clash
with the global namespace.

I like Mathematica, and it is really a nice system to try things
with it and explore mathematics quickly, but issues things like the
above makes it very hard to write very large applications with it.

The above for me is even worst than using GOTO.


  • Prev by Date: Re: How to Scale and vary plot for a Differential Equation
  • Next by Date: Re: Solving Third Order differential equation using Mathematica.
  • Previous by thread: Re: can one make local symbol in a pure function?
  • Next by thread: Re: can one make local symbol in a pure function?