[Date Index]
[Thread Index]
[Author Index]
Re: Clear variables in a list
*To*: mathgroup at smc.vnet.net
*Subject*: [mg112071] Re: Clear variables in a list
*From*: Leonid Shifrin <lshifr at gmail.com>
*Date*: Sun, 29 Aug 2010 02:49:02 -0400 (EDT)
Chris,
nothing will help if your variable list is defined as in your example (with
Set, and after the variables got their definitions), since it
contains already evaluated values:
?vars
Global`vars
vars={1,2,3}
If you defined it with SetDelayed (or used Set, but before you assigned
values to your variables):
vars := {a, b, c}
?vars
Global`vars
vars:={a,b,c}
Then this is possible, although I would not say it is trivial:
ReleaseHold[Clear @@@ (Hold[vars] /. OwnValues[vars])]
If the only purpose of your <vars> variable is to be used to clear the
variables, then I
suggest that you use Hold instead of List, as a container head:
a = 1; b = 2; c = 3;
heldvars = Hold[a, b, c];
Then clearing the variables becomes trivial, and all of the above problems
are avoided:
Clear @@ heldvars
This is what I often use for similar purposes.
As a side remark, the idiom f[Unevaluated[#]]& is flawed, because the
argument gets
evaluated during pure-function parameter passing stage, long before it is
substituted
into the body of a pure function. This is one place where the current
documentation is
wrong: f is *not* equivalent to f[#]&. Simple counter-example:
In[17]:= Hold[Print["*"]]
Out[17]= Hold[Print["*"]]
In[16]:= Hold[#] &[Print["*"]]
During evaluation of In[16]:= *
Out[16]= Hold[Null]
Returning to f[Unevaluated[#]]&: it leaks evaluation in the same way:
In[13]:= ClearAll[f];
f[Unevaluated[#]] &[Print["*"]]
During evaluation of In[13]:= *
Out[14]= f[Unevaluated[Null]]
This is what should be used instead, to achieve the desired effect:
In[15]:= Function[x, f[Unevaluated[x]], HoldAll][Print["*"]]
Out[15]= f[Unevaluated[Print["*"]]]
Hope this helps.
Regards,
Leonid
On Sat, Aug 28, 2010 at 3:01 PM, Chris Degnen <degnen at cwgsy.net> wrote:
> Any suggestions how to clear variables in a list, eg:
>
> a = 1; b = 2; c = 3; vars = {a, b, c};
>
> None of these work:
>
> Map[Clear, vars]
> Map[Clear[Unevaluated[#]] &, vars]
> Apply[Clear, vars, {1}]
>
> Only this works: Clear[a, b, c]
>
>
>
Prev by Date:
**Version-independent palette with no warnings?**
Next by Date:
**Re: Getting started with 3D cardioid**
Previous by thread:
**Re: Clear variables in a list**
Next by thread:
**find selected function / F1**
| |