MathGroup Archive 2007

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

Search the Archive

Re: Function definition within a module (about variable renaming)


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


  • Prev by Date: RE: Re: Re: Where is the Navigate menu
  • Next by Date: Re: Re: Re: Where is the Navigate menu
  • Previous by thread: Re: Re: Re: Where is the Navigate menu
  • Next by thread: Re: Function definition within a module (about variable renaming)