Re: HoldFirst question
- To: mathgroup at smc.vnet.net
- Subject: [mg65530] Re: [mg65500] HoldFirst question
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Thu, 6 Apr 2006 06:52:44 -0400 (EDT)
- References: <200604051055.GAA21735@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 5 Apr 2006, at 19:55, Yaroslav Bulatov wrote:
> f=5;f[a_]=a gives an error because it evaluates f[a_] to 5[a_]
>
> But Set is HoldFirst, doesn't that mean that f[a_] will be held
> unevaluated?
>
Although Set has a Holdfirst atttribute it actually evaluates parts
of the expression on the LHS including the Head because this is
required for pattern matching. A standard example that shows this
porcess is:
Clear[f]
f = g;
f[a_] = a;
DownValues[f]
{}
DownValues[g]
{HoldPattern[g[a_]] :> a}
There are no DownValues for f but there is DownValue for g, because f
was evaluated to g before the rues were made. The same thing happens
in you case.
Although your example is entirely artificial, sometiems problems of
this tye arise anaturallya dn can be dealt with by usign HoldPattern.
In your case one could do this:
In[1]:=
f=5;HoldPattern[f][a]=a;
No error this time. However, as expected:
In[2]:=
f[a]
Out[2]=
5[a]
However, when we remove the OwnValue of f:
In[3]:=
OwnValues[f]={};
we get
In[4]:=
f[a]
Out[4]=
a
Andrzej Kozlowski
- References:
- HoldFirst question
- From: "Yaroslav Bulatov" <yaroslavvb@gmail.com>
- HoldFirst question