Re: Function definition within a module (about variable renaming)
- To: mathgroup at smc.vnet.net
- Subject: [mg79681] Re: Function definition within a module (about variable renaming)
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 1 Aug 2007 05:15:34 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <f8n20f$khj$1@smc.vnet.net>
Zeng.Shixin at gmail.com wrote:
> I have following code:
>
> fun[] := Module[{a},
> a = {3x};
> f1[x_] = a[[1]]; (*change this line to f1[x_] = 3x, it works
> fine (no variable renaming) *)
> ];
>
> Then I try to find out the definition about f1, I got:
>
> ?f1
> Global`f1
> f1[x$_] = 3 x
>
> I think It should be "f1[x_] = 3 x" instead. I just can't understand
> why the variable x in f1 was renamed to x$. I went to the help system,
> but I didn't find any useful information.
>
> Thanks in advance.
I believe you have found a bug for the symbol x has no reason to be
localized in the code you provided.
Even though the localization has to be done for whatever mysterious
reason, x$ is not a correct name for a local symbol that is
automatically generated by Mathematica. Local symbols are constructed
according to the following pattern: name_of_the_symbol + $ +
a_sequence_number, i.e. a$38, x$39 are valid local symbols [1].
Here, the spurious half-baked localization creates in the Global context
a lasting temporary symbol named x$.
In[1]:=
Clear[fun, f1, x]
fun[] := Module[{a}, a = {3*x};
f1[x_] = (x = 3; a[[1]]); f1[2]];
fun[]
Out[3]=
3*x
In[4]:=
Information["f1", LongForm -> False]
f1
f1[x$_] = 3*x
In[5]:=
Information["x$", LongForm -> False]
x$
Attributes[x$] = {Temporary}
x$ = 3
Regards,
Jean-Marc
[1] http://reference.wolfram.com/mathematica/tutorial/HowModulesWork.html