Re: Re: Re: Is this a bug?

*To*: mathgroup at smc.vnet.net*Subject*: [mg13479] Re: [mg13445] Re: Re: Is this a bug?*From*: "Jurgen Tischer" <jtischer at col2.telecom.com.co>*Date*: Sun, 26 Jul 1998 02:33:38 -0400*Sender*: owner-wri-mathgroup at wolfram.com

Hi Jim, If of course does NOT evaluate all it's arguments regardless of test, only if you "ask" for it (even if you don't notice that you asked for it). For me the answer for your "pragmatic question" is just If[ test, ToExpression["valid statement"],ToExpression["invalid statement"]] Now in case there is a situation where you need the Evaluate (I can't see one, but who knows) you could do the following: If[ test, Null ; Evaluate[expr1] , Null ; Evaluate[expr2] ] Now I have to confess that I had a wrong idea about the order of evaluation (looking at Trace); I thought Mathematica first evaluates the forced items, then the test and then picks the corresponding (now already evaluated) item. The following shows that I was wrong and Mathematica did as it should by it's rules: In[1]:= If[ Print["hello1"] ;True,Evaluate[ Print["hello2"] ] , Evaluate[ Print["hello3"] ] ] "hello1" "hello2" "hello3" If you wonder why there is no second hello2 at the end (at least I did for a moment), look at In[2]:= If[ Print["hello1"] ;test,Evaluate[ Print["hello2"] ] , Evaluate[ Print["hello3"] ] ] "hello1" "hello2" "hello3" Out[2]= If[ test , Null , Null ] (where test is undefined to produce the situation inside of Mathematica after the first step). If you want to read the full story, execute the following in a notebook. And don't miss the following items (A.4.2 NonStandard Argument Evaluation, A.4.3 Overriding NonStandard Argument Evaluation) FrontEndExecute[FrontEnd`HelpBrowserLookup["MainBook", "A.4.1"]] (Oh Paul, I did it again!) Jurgen -----Original Message----- From: jfreeze at lexmark.com <jfreeze at lexmark.com> To: mathgroup at smc.vnet.net Subject: [mg13479] [mg13445] Re: Re: Is this a bug? >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. > This amounts to not allow the user to decide on the order of evaluation, for the benefit of unexperienced beginners. I don't approve! >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 >