Re: Re-evaluation of Conditional expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg3474] Re: [mg3460] Re-evaluation of Conditional expressions
- From: Richard Mercer <richard at seuss.math.wright.edu>
- Date: Sat, 16 Mar 1996 22:37:43 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Patrice, This is a good one! To my surprise, "If" turns out the have the HoldAll attribute, which seems to be the cause of the behavior you describe. If you first execute Unprotect[If]; ClearAttributes[If,HoldAll] Then your example works as one would expect. The problem then is: why does the first example work with the HoldAll attribute in place? SetAttributes[If,HoldAll] x = True; y 1 Apparently "If" is willing to evaluate one step in search of a True or False result in spite of the HoldAll attribute. Indeed "If" would be pretty useless without such a capability. This *appears* to be a special exception to HoldAll. I don't have an explanation for why the HoldAll attribute is there in the first place. Probably it is like "Plot": it works well in some cases and not well in others (like yours). It would be interesting to hear a good case for the use of HoldAll for "If". Richard Mercer > While teaching Mathematica, I found the following quite > strange behaviour of Conditional expressions in > Mathematica. Define symbol y as follows: > > y = If[x,1,2] > > (* Then set x to True and revaluate y *) > x=True;y > > (* as expected, the answer is 1 *) > (* Now, set x to z, a fresh symbol, and evaluate y *) > Clear[x,z];x=z;y > > (* The result is If[x,1,2] *) > > (* Finally, set z to True, and evaluate y *) > z=True;y > > The result is still If[x,1,2], although a re-evaluation > of x gives True, as expected. > > The only way to get a re-evaluation of y is to use the > function Update: > > Update[];y > > which gives the result 1. In summary, it seems that If > is re-evaluated only if the expression that assigned to > x has changed, and not when the value of x has changed. > > Is there a special reason for this? Any way to circumvent > this problem? > > Thanks. > > Patrice Quinton ==== [MESSAGE SEPARATOR] ====