[Date Index]
[Thread Index]
[Author Index]
RE: Clearing a symbol known only through a definition
*To*: mathgroup at smc.vnet.net
*Subject*: [mg66683] RE: [mg66658] Clearing a symbol known only through a definition
*From*: Andrew Moylan <andrew.j.moylan at gmail.com>
*Date*: Sat, 27 May 2006 03:51:11 -0400 (EDT)
*Reply-to*: <andrew.moylan at anu.edu.au>
*Sender*: owner-wri-mathgroup at wolfram.com
Thanks Carl.
The function "OwnValues" does allow me to easily solve my problem; but I
can't find any documentation for this function. Do you know why it doesn't
appear in the help system together with e.g. UpValues and DownValues?
I realised a little while after posting this question that my "particular
problem" had that simple recursive solution. (I became interested in the
solution to the first problem while trying to write a (more convoluted)
solution to the "particular" problem.)
-----Original Message-----
From: Carl K. Woll [mailto:carlw at wolfram.com]
To: mathgroup at smc.vnet.net
Subject: [mg66683] Re: [mg66658] Clearing a symbol known only through a definition
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"].
>
In this case, the function OwnValues is useful. For example:
a:=b
In[42]:= OwnValues[a]
Out[42]= {HoldPattern[a] :> b}
So, a function to clear the symbol pointed to by a could be:
SetAttributes[clearownsymbol, HoldFirst];
clearownsymbol[x_] := Module[{own},
own = OwnValues[x];
If[MatchQ[own, {_ :> _Symbol}],
Clear @@ Extract[own, {1, 2}, Hold]
]
]
For your example:
a:=b
b=3;
In[48]:= b
Out[48]= 3
In[49]:= clearownsymbol[a]
In[50]:= b
Out[50]= b
> ----
> 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?
Unless there is more going on here, I don't see how your first issue relates
to this problem. At any rate, here is a function that does what you want:
SetAttributes[tophead, HoldFirst]
topheadclear[a_Symbol] := Clear[a]
topheadclear[b_[___]] := topheadclear[b]
f=g;
In[57]:= f
Out[57]= g
In[58]:= topheadclear[f[x][y][z]]
In[59]:= f
Out[59]= f
Of course, f[x][y][z] without a hold gets evaluated to
g[x][y][z]
and there is no way to clear f based on the input g[x][y][z].
Carl Woll
Wolfram Research
Prev by Date:
**RE: Graphics3D axes**
Next by Date:
**Re: How to make results from Integrate and NIntegrate agree**
Previous by thread:
** Re: Clearing a symbol known only through a definition**
Next by thread:
**Using Mathematica to locate a PDE singularity**
| |