Re: Re-evaluation of Conditional expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg3479] Re: Re-evaluation of Conditional expressions
- From: pecora at zoltar.nrl.navy.mil (Louis M. Pecora)
- Date: Sat, 16 Mar 1996 22:38:35 -0500
- Organization: Naval Research Laboratory
- Sender: owner-wri-mathgroup at wolfram.com
In article <4i5q7t$rtt at dragonfly.wolfram.com>, withoff at wolfram.com (David Withoff) wrote: > 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. > > 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. How about holding the evaluation in the definition of y? Then it would evaluate correctly each time. Would y := If[x,1,2] do it? -- Louis M. Pecora pecora at zoltar.nrl.navy.mil /* My views and opinions are not those of the U.S. Navy. If you want those, you have to start a war. */ ==== [MESSAGE SEPARATOR] ====