Re: Question on symbols in modules
- To: mathgroup at smc.vnet.net
- Subject: [mg17954] Re: [mg17945] Question on symbols in modules
- From: "Andrzej Kozlowski" <andrzej at tuins.ac.jp>
- Date: Mon, 7 Jun 1999 02:51:15 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Mathematica creates symbols as it "reads" your programs. Even in the following case: In[1]:= Names["Global`*"] Out[1]= {} In[2]:= Clear[mysymbol1] In[3]:= Names["Global`*"] Out[3]= {mysymbol1} The only case I can think of when new symbol is not added is In[4]:= Remove[mysymbol2] In[5]:= Names["Global`*"] Out[5]= {mysymbol1} but I believe it was actually first created and then removed. When you use Module the global variable tmp which is created when your programm is read has no relatioin to the local variable tmp (actually tmp$somenumber) which in your case was in fact created only when you actually evaluated test[] (that's because you used := and not =. It has nothing to do with using Module). Finally you do not actually need an explicit Return in this sort of situation. In Mathematica Return[] is basically used to break out of loops: in this case there is no difference between your program and In[1]:= test[] := Module[{tmp,},tmp] In[2]:= test[] Out[2]= tmp$28 -- Andrzej Kozlowski Toyama International University JAPAN http://sigma.tuins.ac.jp http://eri2.tuins.ac.jp ---------- >From: "David Keith" <dkeith at hevanet.com> To: mathgroup at smc.vnet.net >To: mathgroup at smc.vnet.net >Subject: [mg17954] [mg17945] Question on symbols in modules >Date: Sat, Jun 5, 1999, 3:56 PM > > 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"} > > In[4]:= test[] > Out[4]= tmp$5 > > What confuses me is this: line In[2] defines a module with local symbol > "tmp". 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? > > Thanks. > > >