Re: Function definition within a module (about variable renaming)
- To: mathgroup at smc.vnet.net
- Subject: [mg79695] Re: Function definition within a module (about variable renaming)
- From: "Zeng.Shixin at gmail.com" <Zeng.Shixin at gmail.com>
- Date: Thu, 2 Aug 2007 03:46:09 -0400 (EDT)
- References: <f8n20f$khj$1@smc.vnet.net><f8pkh3$3kj$1@smc.vnet.net>
On Aug 1, 3:41 am, Jean-Marc Gulliet <jeanmarc.gull... at gmail.com> wrote: > Zeng.Shi... 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]. > That's exactly what I think. > 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 Thanks for your information. I think I should try to work around it. > > Regards, > Jean-Marc > > [1]http://reference.wolfram.com/mathematica/tutorial/HowModulesWork.html