MathGroup Archive 2011

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

Search the Archive

Re: Interaction of Remove and Global variables in a Module

  • To: mathgroup at smc.vnet.net
  • Subject: [mg120105] Re: Interaction of Remove and Global variables in a Module
  • From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
  • Date: Fri, 8 Jul 2011 04:54:39 -0400 (EDT)
  • References: <iv45c7$et1$1@smc.vnet.net>

On Thu, 07 Jul 2011 12:30:15 +0100, blamm64 <blamm64 at charter.net> 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.

This puzzle is explained by noting that after Remove is called, the  
definition of f is updated to reflect the removal of a:

f[b_] := Module[{t}, Removed[a][t_] = b*t^2;]

which coincides with the documented behaviour; you are now free to  
redefine f if you wish the definition to refer to a "new" a rather than  
the one that was expunged by Remove. The fact that it does not work as  
might have been anticipated indicates that Remove is not appropriate in  
this context; Clear or ClearAll would be better choices.


  • Prev by Date: Re: How to NSolve equation which involves NIntegrate?
  • Next by Date: Re: Interaction of Remove and Global variables in a Module
  • Previous by thread: Re: Interaction of Remove and Global variables in a Module
  • Next by thread: Re: Interaction of Remove and Global variables in a Module