Re: a dangerous feature of Module[]?
- To: mathgroup at smc.vnet.net
- Subject: [mg57458] Re: [mg57422] a dangerous feature of Module[]?
- From: Chris Chiasson <chris.chiasson at gmail.com>
- Date: Sat, 28 May 2005 05:39:46 -0400 (EDT)
- References: <200505270857.EAA07697@smc.vnet.net>
- Reply-to: Chris Chiasson <chris.chiasson at gmail.com>
- Sender: owner-wri-mathgroup at wolfram.com
Set is evaluating the right hand side before the assignment to g. In
that case, Mathematica uses global value for x before assigning g. Try
??g after both of the methods to see what is happening.
On 5/27/05, Gennady Stupakov <stupakov at yahoo.com> wrote:
> I've spent considerate time recently debugging my Mathematica code until I figured
> out that the problem is in a strange behaviour of the Module[] function.
> Here is a simple example that demonstrate this feature.
>
> I have a gloval variable x:
>
> In[1]:= x=3
>
> Out[1]= 3
>
> Now I define a module with local varibles x and f, call g, and get the
> expected result. The value of global variable x does not interfere with
> the local x, as expected.
>
> In[2]:= g:=Module[{x,f},f[x_]:=x;f[5]]
>
> In[3]:= g
>
> Out[3]= 5
>
> However, if I use Set (=) instead of SetDelayed (:=) in the definition of
> my function f inside the module, the result will be different:
>
> In[4]:= h:=Module[{x,f},f[x_]=x;f[5]]
>
> In[5]:= h
>
> Out[5]= 3
>
> In this case the Module ignores the fact that x is supposed to be a local
> variable, and for some reason uses the value of the global x inside the
> module.
>
> Can somebody explain me if this is an expected behaviour of the Module[]?
>
> I am using version 5.1 on a PC:
> In[7]:= $Version
>
> Out[7]= 5.1 for Microsoft Windows (October 25, 2004)
>
> Thanks.
> Gennady.
>
>
--
Chris Chiasson
http://chrischiasson.com/
1 (810) 265-3161
- References:
- a dangerous feature of Module[]?
- From: Gennady Stupakov <stupakov@yahoo.com>
- a dangerous feature of Module[]?