Re: Re-evaluation of Conditional expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg3469] Re: Re-evaluation of Conditional expressions
- From: withoff (David Withoff)
- Date: Sat, 16 Mar 1996 22:36:50 -0500
- Organization: Wolfram Research, Inc.
- Sender: owner-wri-mathgroup at wolfram.com
In article <4hr6d7$84u at dragonfly.wolfram.com> quinton at irisa.fr (Patrice Quinton) writes: > > 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 > > Irisa, Campus de Beaulieu, > University of Rennes 1, France > Phone: 99 84 71 85, Fax: 99 84 71 71 > This and similar situations represent the entire purpose of Update. Once an expression has evaluated to itself, Mathematica will not re-evaluate the expression unless something inside the expression changes. In this example, nothing inside If[x,1,2] changed, so the expression is not re-evaluated. This is an important optimization, and is exactly what you want in the vast majority of cases. The Update function is included for those rare situations when you want an expression to be re-evaluated even though nothing inside the expression has changed. Dave Withoff Research and Development Wolfram Research ==== [MESSAGE SEPARATOR] ====