Re: Re: Is this a bug?

*To*: mathgroup at smc.vnet.net*Subject*: [mg13445] Re: Re: Is this a bug?*From*: jfreeze at lexmark.com*Date*: Fri, 24 Jul 1998 01:45:33 -0400*Sender*: owner-wri-mathgroup at wolfram.com

Thanks for all of the responses. My original question was (basically) why does If[True, Evaluate[x=5], Evaluate[y=5]] give {x,y} as {5,5}. It was explained by several people that the reason is that Evaluate overides HoldRest. Nevertheless, it seems to me that expressions not returned from If should not be executed, regardless if Evaluate is used. One responder suggested that it was returning what I asked it to. Well, no, I did not ask it to execute a false expression. Consider the C statement if(condition) { // do if true } else { // do if false } Here, no matter what assignments to variables are made in "false", they are not made if condition == true. I guess some will say that here lies my real sin, thinking in C. : ) Well, we could start a real good thread here about the evaluation/execution of all expressions in If regardless of the value of condition, which I am not opposed to, but, being a pragmatist at the moment, I am looking for a solution to the following (seeing that If will not work now): If[ test, Evaluate[ToExpression["valid statement"]], Evaluate[ToExpression["invalid statement"]]] What this statement does it to test for validity of a string before converting it to an expression. Since If executes all its arguments, regardless of test, can someone suggest a method accomplish this task? Thanks Jim Freeze jfreeze at lexmark.com "Allan Hayes" <hay%haystack.demon.cc.uk at interlock.lexmark.com> on 07/23/98 07:18:20 AM Please respond to "Allan Hayes" <hay%haystack.demon.co.uk at interlock.lexmark.com> cc: bcc: Subject: [mg13445] Re: Is this a bug? jfreeze at lexmark.com wrote in message <6p6oa9$56m at smc.vnet.net>... > >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 > This is not a bug. To try and explain the evaluation I will modify your example slightly to (1) In[1]:= Clear[x,y]; In[2]:= If[True,x=5,y=7] Out[2]= 5 In[3]:= {x,y} Out[3]= {5,y} (2) In[4]:= Clear[x,y]; In[5]:= If[True,Evaluate[x=5],Evaluate[y=7]] Out[5]= 5 In[6]:= {x,y} Out[6]= {5,7} Notice that the If has the attrribute HoldRest Here is what happens (1) For If[True,x=5,y=7] The *attribute* Hold Rest for If prevents evaluation of x=5 and x=7; so we get If[True,x=5,y=7] The *rules* for If are now used and give x=5 This is evaluated to ouput 5 and the rule x = 5 is stored Because of the stored rule {x,y} gives {5, y} (2) For If[True,Evaluate[x=5],Evaluate[x=7]] The *attribute* Hold Rest is overidden at Evaluate[x=5] which evaluates to 5 and stores x =5; similarly for Evaluate[y=7]; so we get If[True, 5 , 7 ] and the rules x = 5 and y = 7 have been stored The *rules* for If are now used and give 5 Because of the stored rule {x,y} gives {5, 7} You can see thes stages displayed by using TracePrint Clear[x,y]; If[True,x=5,y=7]//TracePrint Clear[x,y]; If[True,Evaluate[x=5],Evaluate[y=7]]//TracePrint ------------------------------------------------------------- Allan Hayes Training and Consulting Leicester UK http://www.haystack.demon.co.uk hay at haystack.demon.co.uk voice: +44 (0)116 271 4198 fax: +44(0)116 271 8642