MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re-evaluation of Conditional expressions

  • To: mathgroup at
  • Subject: [mg3471] Re: Re-evaluation of Conditional expressions
  • From: Harald Berndt <haraldb at>
  • Date: Sat, 16 Mar 1996 22:37:11 -0500
  • Organization: University of California Forest Prodcts Lab
  • Sender: owner-wri-mathgroup at

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?


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


  • Prev by Date: Re: Mathematica[2.2.3] && Win[95] := Poor[Performance]
  • Next by Date: Re: Re-evaluation of Conditional expressions
  • Previous by thread: Re: Re-evaluation of Conditional expressions
  • Next by thread: Re: Re-evaluation of Conditional expressions