Re: Re-evaluation of Conditional expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg3471] Re: Re-evaluation of Conditional expressions
- From: Harald Berndt <haraldb at nature.berkeley.edu>
- Date: Sat, 16 Mar 1996 22:37:11 -0500
- Organization: University of California Forest Prodcts Lab
- Sender: owner-wri-mathgroup at wolfram.com
Patrice Quinton wrote: > > 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] > ... > > Is there a special reason for this? Any way to circumvent this problem? > Patrice: I can't really explain why it happens. I'm sure it has to do with the evaluation chain of Mma. But I'll give you a solution first: when using SetDelayed[] rather than Set[], i.e. defining y := If[x,1,2] everything works smoothly. I believe it is generally referrable to use SetDelayed[] (lhs := rhs) when defining rules. Now for some hint of explanation: If[] has attribute HoldAll, so when you Set[] y, x is held (as it should be, I guess). In the definition of Update[] in The Book, conditionals evaluation is mentioned as a specific need for the command. It says something about efficiency. My guess is that they'll not always evaluate the held expression If[] as long as no obvious change occurs to the held variables inside it (?!). Anyway, SetDelayed[] works so well that I'm tempted not to worry about the "why"s. ______________________________________________________________________ Harald Berndt, University of California Research Specialist Forest Products Laboratory Phone: 510-215-4224 FAX: 510-215-4299 ==== [MESSAGE SEPARATOR] ====