MathGroup Archive 1999

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

Search the Archive

Re: Question on symbols in modules

  • To: mathgroup at smc.vnet.net
  • Subject: [mg17959] Re: Question on symbols in modules
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Mon, 7 Jun 1999 02:51:18 -0400
  • References: <7jaj3f$k13@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

David,
I insert some explanations below.

Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


David Keith <dkeith at hevanet.com> wrote in message
news:7jaj3f$k13 at smc.vnet.net...
> Perhaps I am confused about symbols in modules, and also about evaluation.
I
> execute the lines below in V4.0 on NT4.0/SP3:
>
> In[1]:= Names["Global`*"]
> Out[1]= {}
>
> In[2]:= test[] := Module[{tmp}, Return[tmp]]
>
> In[3]:= Names["Global`*"]
> Out[3]= {"test", "tmp"}

Before any evaluation the expression
        test[] := Module[{tmp}, Return[tmp]]
has been parsed and the symbols  test and tmp have been created in the
context "Global`"
The right side has not been evaluated, so no local symbol has been created.

> In[4]:= test[]
> Out[4]= tmp$5

Module[{tmp}, Return[tmp]] has been evaluated; the local symbol tmp$5 has
been created (in the context "Global`").

Incidentally, Return is not needed.


> What confuses me is this: line In[2] defines a module with local symbol
> "tmp".

 -- rather: a Module that when evaluated will create a local variable from
tmp has been stores as the right side of a rule.

> But following this line, a symbol "tmp" has been created in Global
> context. I thought this would not happen, that the only symbol ever
created
> would be a temporary symbol, some "tmp$N". Further, I thought that the
right
> hand side of In[2] would not actually be evaluated until called.
>
> In[4] / Out[4] does display the behaviour I expected, a temporary symbol
is
> used within the scope of the module, but why is "tmp" ever created?

Symbols (tmp) have to be created *before any evaluation* as part of the
process of trying to interpret the input.
Local versions of these symbols are created only when Module is evaluated.







  • Prev by Date: Question on NonlinearFit
  • Next by Date: NDSolve - large ODE systems
  • Previous by thread: Re: Question on symbols in modules
  • Next by thread: reynolds and dimensionless