MathGroup Archive 2006

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

Search the Archive

Re: HoldAll


MR,

HoldAll doesn't mean it will hold the values forever.  It will not evaluated the 
arguments until after they've been passed into the function.

Example:

In[1]:= f[{a_ + b_}] := a

In[2]:= f[{1 + 2}]

Out[2]= f[{3}]

In the code above, Mathematica first evaluated {1+2}, then passed it as an 
argument to the function f.  Since there's no definition of f to handle f[{x_}], 
it returned unevaluated.

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

In[4]:= g[{a_ + b_}] := a + b

In[5]:= g[{1 + 2}]

Out[5]= 3

With HoldAll, {1+2} was passed to the function as it is.  It matched the pattern 
and then once "inside" the function, then it was evaluated normally.

If you don't want something to evaluate until you want to, use Hold.

Igor




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?
> 
> Thanks to all
> MR
> 
> -------------------------------------------------
> This mail sent through IMP: http://horde.org/imp/


-- 

--
Igor C. Antonio
Wolfram Research, Inc.
http://www.wolfram.com

To email me personally, remove the dash.


  • Prev by Date: Re: derivative of cubic spline
  • Next by Date: Re: (Newbie question): New types of numbers
  • Previous by thread: Re: HoldAll
  • Next by thread: Re: HoldAll