MathGroup Archive 2006

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

Search the Archive

Re: Re: HoldFirst confusion


On 6 Apr 2006, at 19:53, Jon Harrop wrote:

> Yaroslav Bulatov wrote:
>> The following gives an error
>> f = 5; f[a_] = a
>>
>> Set is HoldFirst, so why does it evaluate it's first argument f[a_]?
>
> Conversely, why do the attributes of Set and SetDelayed indicate  
> that they
> hold their first argument when the kernel evaluates it.

the attribute HoldFirst does not actually  indicate this. All it  
means  is that the argument may be evaluated in a special way and not  
automatically. This has already been discussed here (see my postings  
in 2005 on the subject " Warning from Piecewise")  and it is  
explained in detail in David Wagner's "Power Programming with  
mathematica", page 205.

>
> On a related note, how can you define and use a downvalue that matches
> g[1+2] without evaluating the 1+2?

Do you mean something like this:

In[3]:=
SetAttributes[g,HoldFirst]

In[4]:=
g[1+2]:=3

In[5]:=
g[3]:=5

In[3]:=
SetAttributes[g, HoldFirst]

In[4]:=
g[1 + 2] := 3

In[5]:=
g[3] := 5

In[6]:=
DownValues[g]

Out[6]=
{HoldPattern[g[3]] :> 5, HoldPattern[g[1 + 2]] :> 3}

In[7]:=
g[1+2]

Out[7]=
3

In[8]:=
g[3]

Out[8]=
5

Andrzej Kozlowski


>
> -- 
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> http://www.ffconsultancy.com/products/ocaml_for_scientists/ 
> chapter1.html
>


  • Prev by Date: Re: Sphere
  • Next by Date: Re: Re: HoldFirst confusion
  • Previous by thread: Re: HoldFirst confusion
  • Next by thread: Re: Re: HoldFirst confusion