Re: Clearing a symbol known only through a definition
- To: mathgroup at smc.vnet.net
- Subject: [mg66673] Re: [mg66658] Clearing a symbol known only through a definition
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sat, 27 May 2006 03:50:39 -0400 (EDT)
- References: <200605260817.EAA01750@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 26 May 2006, at 17:17, Andrew Moylan wrote:
> Suppose I have a symbol "a" that is defined to be equal to another
> symbol. (E.g. It might be that a := b.) How can I clear the value
> of the
> symbol 'pointed to' by "a", without knowing explicity what symbol
> that is?
>
> Clear[a] won't do, of course.
> Clear[Evaluate[a]] won't do, because that will evaluate to
> Clear[Evaluate[b]], which will in turn (in general) evalulate to
> Clear["whatever b evalulates to"].
I am not sure if I understand you correctly, but here is a try. I am
assuming you have a situation like this:
In[1]:=
a := b
In[2]:=
b=3;
and you would like to clear the value of b without directly referring
to b? Is that right? Here is a hack that I just came up with which
appears to do that:
OwnValues[a] /. HoldPattern[Verbatim[HoldPattern[a]] :> x_] :> Clear[x];
In[4]:=
b
Out[4]=
b
In[5]:=
a
Out[5]=
b
So the value of b has been Cleared.
Now for your "particular" problem:
>
> ----
> Alternatively, here is the particular problem I want to solve:
>
> For any expression e, either e is a symbol, or Head[e] is, or
> Head[Head[e]] is, etc. Call this the "topmost symbol" in the
> expression.
> Thus, the topmost symbol in f[x][y][z] is f.
>
> I want to write a function that takes an expression and calls Clear
> [] on
> its topmost symbol. Can anyone think of a simple way to write such a
> function?
>
Very simple, no. But here one way that seems reasonably so:
In[1]:=
f:=g
In[1]:=
Clear @@ ToExpression[First[Characters[
ToString[Unevaluated[f[x][y][z]]]]], InputForm, Hold]
In[2]:=
f
Out[2]=
f
The "topmost symbol" has been cleared. One can also make a function
that does this:
In[1]:=
SetAttributes[F,HoldFirst];
In[2]:=
F[expr_]:=Clear@@ToExpression[First[Characters[ToString[Unevaluated
[expr]]]],\
InputForm,Hold]
In[3]:=
f:=g;
In[4]:=
F[f[x][y][z]]
In[5]:=
f
Out[5]=
f
Andrzej Kozlowski
Tokyo, Japan
- References:
- Clearing a symbol known only through a definition
- From: Andrew Moylan <andrew.moylan@anu.edu.au>
- Clearing a symbol known only through a definition