MathGroup Archive 2007

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

Search the Archive

Re: Module inside Module. Conflict between inner Module local variable

  • To: mathgroup at smc.vnet.net
  • Subject: [mg79591] Re: Module inside Module. Conflict between inner Module local variable
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Mon, 30 Jul 2007 06:46:52 -0400 (EDT)
  • References: <f8h4k8$6s5$1@smc.vnet.net>

Hi,

your function definition is translated into an replacement
rule

DownValues[foo]

gives

{HoldPattern[foo[i_]] :> Module[{},
        boo[] := Module[{i = 1}, Print[i]; ]; boo[]; ]}

when foo[5] is evaluated, the pattern i_ is bound to 5
and all occurrences of i in the right hand side of the rule
are replaced by i -> 5 and so you run into trouble
because the definition of boo[] is now Module[{5=1}..]
and that is clearly nonsense.

And you definion is not allowed, because the right hand side
must be evaluated when the pattern i is already bound to
5.

Regards
   Jens



Nasser Abbasi wrote:
> This Mathematica 6.0.1
> 
> I just found out that I can't declare a local variable inside a Module
> to be the same name as an input parameter for the outer module.
> 
> foo[i_] := Module[{},
> 
>              boo[] := Module[{i = 1},
>                          Print[i];
>              ];
> 
>              boo[];
> ]
> 
> It seems an inner Module is being treated on the same level as the
> outer module as far are variable scope is concerned. When I call
> foo[5] for example, the local variable 'i' for boo[] was set to 5,
> when it was supposed to be LOCAL to boo[] !    It looks like local
> variables to inner modules are treated just like they are local
> variables to the outer module.
> 
> I understand that one can't make a local variable with the same name
> as the argument, but boo[] above is supposed to be a separate module
> (even though it is an inner module) except its scope is limited to
> inside foo[].
> 
> So I do not see why the above would not be allowed. This restriction
> does not seem to make too much sense to me.
> 
> Is there a trick to make the above legal without moving boo[] outside
> of foo[] ?
> 
> thanks,
> Nasser
> 
> 


  • Prev by Date: Factorise with respect to a variable
  • Next by Date: Re: Combining a parametric plot with a Graphics3D plot
  • Previous by thread: Re: Factorise with respect to a variable
  • Next by thread: Re: Module inside Module. Conflict between inner Module local variable