Re: Interaction of Remove and Global variables in a Module

*To*: mathgroup at smc.vnet.net*Subject*: [mg120131] Re: Interaction of Remove and Global variables in a Module*From*: DrMajorBob <btreat1 at austin.rr.com>*Date*: Sat, 9 Jul 2011 07:32:26 -0400 (EDT)

So far, I haven't heard a purpose for Remove that ClearAll does not serve just as well... without the side-effect of "spoiling" other symbols. Bobby On Fri, 08 Jul 2011 03:53:55 -0500, Leonid Shifrin <lshifr at gmail.com> wrote: > Brian, > > The problem you encountered is actually more subtle. Remember that when > you > use Remove (as compared to Clear or ClearAll), the symbol is completely > removed > from the system. This is a pretty disruptive operation. Now, what should > the system do if the symbol you are removing is referenced by some other > symbols? > Keeping it there unchanged would mean that the symbol has not been > really removed > from the system. The solution used in Mathematica is to change a > reference to a > symbol > (say "a") to Removed[a]. In practice, this means that, even when you > re-introduce > the symbol, those definitions that were involving it are still "spoiled" > and > can not > be used. IMO, this is a very sensible design, but this is what leads to > the > behavior > that puzzled you. Have a look: > > f[b_]:=Module[{t},a[t_]=b*t^2;] > Remove[a]; > ?f > > Global`f > f[b_]:=Module[{t},Removed[a][t_]=b t^2;] > > What you have to do in your approach is to re-run the definition for > "f", to > be able to > use it. This is however pretty error-prone. You can cure this by calling > Clear or ClearAll > instead of Remove, but even this approach I don't consider a good > practice. > Even though > you use "a" as a function, calling Clear or Remove on it means that in a > way, you use > it as a global variable. In this post: > > http://stackoverflow.com/questions/6236458/plot-using-with-versus-plot-using-block-mathematica/6236808#6236808 > > there is a lengthy discussion why making implicit dependencies on global > variables is a > bad practice. > > Here are a few ways out. What you seem to want is to generate a function > with embedded > parameters (a closure). One way is to generate a pure function and return > it: > > In[31]:= Clear[f]; > f[b_]:=Function[t,b*t^2]; > a = f[3]; > a[t] > a=f[4]; > a[t] > Remove[a]; > a = f[5]; > a[t] > > Out[34]= 3 t^2 > Out[36]= 4 t^2 > Out[39]= 5 t^2 > > Another way is to explicitly pass to "f" the symbol to which you want to > assign > the definition: > > In[40]:= Clear[ff,a]; > ff[fname_Symbol,b_]:=Module[{t},fname[t_]=b*t^2;]; > ff[a,3]; > a[t] > ff[a,4]; > a[t] > Remove[a]; > ff[a,5]; > a[t] > > Out[43]= 3 t^2 > Out[45]= 4 t^2 > Out[48]= 5 t^2 > > By making the function name an explicit parameter to "ff", you make the > problematic > situation above impossible to happen. > > HTH > > Regards, > Leonid > > > > > > On Thu, Jul 7, 2011 at 3:28 PM, 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. >> >> -- DrMajorBob at yahoo.com

**Follow-Ups**:**Re: Interaction of Remove and Global variables in a Module***From:*Gianluca Gorni <gianluca.gorni@uniud.it>

**Re: How to write a "proper" math document**

**Re: Interaction of Remove and Global variables in a Module**

**Re: Interaction of Remove and Global variables in a Module**

**Re: Interaction of Remove and Global variables in a Module**