MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: HoldFirst question


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


  • Prev by Date: Re: Count Function
  • Next by Date: Re: Count Function
  • Previous by thread: HoldFirst question
  • Next by thread: Re: HoldFirst question