MathGroup Archive 2006

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

Search the Archive

Re: How to tell Mathematica to stop conditional testing in an If statment if one condition is niether True or False? McCarthy evaluation rules? 'and then' test?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69551] Re: [mg69525] How to tell Mathematica to stop conditional testing in an If statment if one condition is niether True or False? McCarthy evaluation rules? 'and then' test?
  • From: Sseziwa Mukasa <mukasa at jeol.com>
  • Date: Fri, 15 Sep 2006 06:45:37 -0400 (EDT)
  • References: <200609141057.GAA21660@smc.vnet.net>

On Sep 14, 2006, at 6:57 AM, Nasser Abbasi wrote:

> I can better describe this with simple example:
>
> ------------- code ------------
>
> Remove["Global`*"];
> xc = Table[i, {i, 1, 3}]
> x = 5;
> If[x == b && xc[[10]] == 4, Print["True"], Print["False"], Print 
> ["Can't
> decide"]]
>
> ----- end code -------------
> In the above, 'x==b' is neither True nor False, since 'b' has no  
> numerical
> value.
>
> But what I want is when this happens, for Mathematica to NOT  
> continue with
> the  testing if xc[[10]]==4  is True (because even if it is True,  
> it will
> not change the outcome, which is can't decide.
>
> I am looking for something like 'and then'  which says to test the  
> next
> condition only if the one just tested was true.
> The interesting thing is that if 'b' had a value, say 7, which  
> makes the
> first test (the x==b) to be False, then Mathematica does the right  
> thing,
> and will not try to check the xc[[10]]==4 condition.  I need it to  
> do the
> same thing when also the result of the check is 'undecided', not just
> 'False' or 'True'.

And does work like and then, but it only short circuits evaluation if  
the result of that argument was False.

The expression If[x==b,xc[[10]]==4,,False,x==b] will behave as you  
want.  Thus you can make your own operator:

andThen[term_,{}]:=term

andThen[term_,{terms__}]:=If[term,andThen[First[{terms}],Rest 
[{terms}]],False,term]

andThen[a__]:=If[Length[{a}]==1,First[{a}],andThen[First[{a}],Rest 
[{a}]]]

so that

If[andThen[x==b,xc[[10]]==4],Print["True"], Print["False"], Print 
["Can't decide"]]

will work as you want.

> It is clear to me that the way Mathematica does it now is not the  
> right way.

&& is the And from Boolean logic so it only makes sense when its  
arguments only have the values True or False.  You want a different  
form of logic and unfortunately have to write your own operators.

> I do not see why it tries to check for xc[[10]]==4 when it will not  
> make a
> difference to the final result.

Because it is not designed to work with logics other than Boolean.

Regards,

Ssezi


  • Prev by Date: RE: How to tell Mathematica to stop conditional testing in an If statment if one condition is niether True or False? McCarthy evaluation rules? 'and then' test?
  • Next by Date: Re: Known recursion link to Hermite polynomials not solved in Mathematica
  • Previous by thread: Re: How to tell Mathematica to stop conditional testing in an If statment if one condition is niether True or False? McCarthy evaluation rules? 'and then' test?
  • Next by thread: Re: How to tell Mathematica to stop conditional testing in an If statment if one condition is niether True or False? McCarthy evaluation rules? 'and then' test?