Re: HoldAll
- To: mathgroup at smc.vnet.net
- Subject: [mg66554] Re: HoldAll
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sat, 20 May 2006 04:46:47 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <e4jukh$d94$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
umrakmm at cc.umanitoba.ca wrote:
> Hello to everyone
>
> Quick question with regards to why the HoldAll attribute no longer works once
> you define the function. For example, before the definition, it serves the
> purpose it was meant to serve:
>
> Input
> Clear[ff]
> SetAttributes[ff, HoldAll]
> ff[1, 2, 3 + 4]
>
> Output
> ff[1, 2, 3 + 4]
>
> But once I actually assign the "functional guts", it doesn't do what it did
> above:
> ff[x_, y_, z_] := x + y + z;
> ff[1, 2, 3 + 4]
>
> 10
>
> Why?
It still works indeed; however it does not do what you think.
First, use *Remove* rather than *Clear* since clear delete the
definitions associated to a symbol but not its attributes (se In[1]).
Second, *HoldAll* blocks the evaluation of the arguments of a function
that is whatever lays between the square brackets (compare Out[4] and
Out[6]).
To achieve what you are thinking of, you must enclose the definition of
ff within a HoldAll function as in In[7]. You can still get the final
result by using ReleaseHold (see In[9]).
In[1]:=
Remove[ff]
In[2]:=
ff[1, 2, 3 + 4]
Out[2]=
ff[1, 2, 7]
In[3]:=
ff[x_, y_, z_] := x + y + z
In[4]:=
Trace[ff[1, 2, 3 + 4]]
Out[4]=
{{3 + 4, 7}, ff[1, 2, 7], 1 + 2 + 7, 10}
In[5]:=
SetAttributes[ff, HoldAll]
In[6]:=
Trace[ff[1, 2, 3 + 4]]
Out[6]=
{ff[1, 2, 3 + 4], 1 + 2 + (3 + 4), {3 + 4, 7},
1 + 2 + 7, 10}
In[7]:=
ff[x_, y_, z_] := HoldForm[x + y + z]
In[8]:=
ff[1, 2, 3 + 4]
Out[8]=
1 + 2 + (3 + 4)
In[9]:=
ReleaseHold[%]
Out[9]=
10
HTH,
Jean-Marc