Re: Re: Is this a bug?
- To: mathgroup at smc.vnet.net
- Subject: [mg13452] Re: [mg13386] Re: Is this a bug?
- From: Carl Woll <carlw at fermi.phys.washington.edu>
- Date: Fri, 24 Jul 1998 01:45:38 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Hi, This isn't a bug, it's a result of the way Mathematica processes expressions. Suppose you have an expression head[ arg1, arg2 , arg3 ] Normally, Mathematica will evaluate each of the arguments in turn, and then evaluate head with the evaluated arguments. Of course, this doesn't make sense for all functions, so some functions have attributes which prevent this behavior. Hence, If has the attribute HoldRest, which says evaluate only the first argument. Thus the normal execution of the If statement If[ arg1, arg2, arg3] will proceed by evaluating arg1, and then evaluating If with arg1 evaluated and arg2 and arg3 unevaluated. Depending on the value of arg1, If will evaluate arg2 or arg3. However, there are times when it would be convenient to change the execution order which Mathematica normally uses, and the command that does this is Execute. When mathematica decides whether or not to evaluate arguments it checks the attributes of the head, and it also checks whether any arguments have the head Evaluate. It then evaluates both arguments which the attributes allow, and arguments with the head Evaluate. So, in your example, If has the attribute HoldRest, and both arguments arg2 and arg3 have the head Evaluate, so all three arguments are evaluated. By the way, why did you put Evaluate around the arguments? Carl Woll Dept of Physics U of Washington On Thu, 23 Jul 1998 jfreeze at lexmark.com wrote: > > The following seems to be a bug. In the first example, both the true and > the false parts of If are being evaluated. In the secon example, only > the true part. If this is not a bug, could someone please explain this > behavior to me. Thanks > > In[1]:= > Clear[x,y]; > > If[True,Evaluate[ToExpression["x=5"]],Evaluate[ToExpression["y=5"]]]; > Print[x,y]; > Out[1]:= > 55 > In[2]:= > Clear[x,y]; > If[True,x=5,y=5]; > Print[x,y]; > Out[2]:= > 5y > >