Re: Interaction of Remove and Global variables in a Module
- To: mathgroup at smc.vnet.net
- Subject: [mg120102] Re: Interaction of Remove and Global variables in a Module
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Fri, 8 Jul 2011 04:54:06 -0400 (EDT)
- References: <iv45c7$et1$1@smc.vnet.net>
On 07/07/2011 12:30, blamm64 wrote: > This is what I get for querying SetDelayed > > In[1]:= ?SetDelayed > lhs:=rhs assigns rhs to be the delayed value of lhs. rhs is maintained > in an unevaluated form. When lhs appears, it is replaced by rhs, > evaluated afresh each time.>> > > Note particularly the above reads AFRESH EACH time. It appears then > the following is inconsistent behavior based on the above description: > > In[2]:= f[b_]:=Module[{t},a[t_]=b*t^2;] > In[3]:= a[t] > Out[3]= a[t] > In[4]:= f[3] > In[5]:= a[t]//InputForm > Out[5]//InputForm= > 3*t^2 > In[6]:= f[5] > In[7]:= a[t]//InputForm > Out[7]//InputForm= > 5*t^2 > In[8]:= Remove[a] > In[9]:= f[4] > In[10]:= a[t]//InputForm > Out[10]//InputForm= > a[t] > > Apparently AFRESH is not an accurate description of how SetDelayed > operates in this case, or I am missing something about this particular > interaction of Module, Remove, and global variables inside Modules. > > However, if I go back, after executing the last line above (<a> has > been Removed), and place the cursor in the input line where<f> is > defined and hit Enter, which I thought would be identical to just > evaluating<f> AFRESH again, and then execute the<f[4]> line again, > then the global<a> definition is re-constituted. > > The documentation for Remove reads the name is no longer recognized by > Mathematica. My understanding is that if the same name is defined > AFRESH, it will once again be recognized. > > So if anyone would let me know what I am missing, regarding why the > definition of<a> is not created AFRESH each time<f> is evaluated, I > would appreciate it. > > Please don't construe the definition of<f> as my way of > 'parameterizing' a function definition, I just use that definition to > convey the apparent inconsistency. > > -Brian L. > Here is a simpler example of the same phenomenon: In[7]:= f[] := (a = 2) In[8]:= ?f Global`f f[]:=a=2 In[9]:= Remove[a] In[10]:= ?f Global`f f[]:=Removed[a]=2 In[11]:= a Out[11]= a In[13]:= f[] Out[13]= 2 In[14]:= a Out[14]= a As you can see, by removing 'a', you effectively trash the function f - so reintroducing 'a' doesn't put you back where you were before. If it had, the Remove operation would have done nothing! Removing symbols is a fairly unusual thing to do - it might help to explain why you want to do this. David Bailey http://www.dbaileyconsultancy.co.uk