Re: About Condition and HoldAll
- To: mathgroup at smc.vnet.net
- Subject: [mg76088] Re: [mg76030] About Condition and HoldAll
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Wed, 16 May 2007 05:25:19 -0400 (EDT)
- References: <200705150848.EAA16743@smc.vnet.net>
On 15 May 2007, at 17:48, solidifire wrote: > In[1]:= {f[1 + 1, 2 + 3], g[1 + 1, 2 + 3], h[1 + 1, 2 + 3]} > Out[1]= {f[2, 5], g[2, 5], h[2, 5]} > > In[2]:= SetAttributes[f, HoldAll] > > In[3]:= SetAttributes[g, HoldRest] > > In[4]:= {f[1 + 1, 2 + 3], g[1 + 1, 2 + 3], h[1 + 1, 2 + 3]} > Out[4]= {f[1 + 1, 2 + 3], g[2, 2 + 3], h[2, 5]} > > In[5]:= Attributes[Condition] > Out[5]= {HoldAll, Protected} > > In[6]:= Condition[1 + 1, 2 + 3] > Out[6]= 2 /; 2 + 3 > > The version of Mathematica is 5.0 for Microsoft Windows. > > My question is that why does Condition behave as having HoldRest > as attribute although the attribute is HoldAll actually? > > The "evaluation" of 1+1 that you see is not "pre-evaluation" but "post-evaluation". The difference between pre-evaluatio nand post- evlaution can be seen on this simple example: In[1]:= SetAttributes[f, HoldAll] In[2]:= f[x_] := x In[3]:= f[1 + 1] Out[3]= 2 Since f has a HoldAll attribute 1+1 was not pre-evaluated by it was still post-evaluated. You can see the working of the HodlAll attribute here: g[t_] := Condition[Print["evaluated"], t] Note that: g[True] "evaluated" g[False] g(False) Clearly, if Condition pre-evaluated the first argument it would have printed "evalauted" no matter what the value of t was. By contrast, If has attribute HoldRest so, if we define: h[t_] := If[Print["evaluated"]; t, Print[1], Print[0]] h[True] "evaluated" 1 h[False] "evaluated" 0 "evaluated" always gets printed, but 1 or 0 are printed depending on the value of t. ALso, note that the behaviour of Condition seems hard coded and can't be changed by changing its attributes. Andrzej Kozlowski
- References:
- About Condition and HoldAll
- From: solidifire <barabbasgospel@yahoo.com.tw>
- About Condition and HoldAll