Page 1 sur 1

testing conjunction

Publié : ven. juil. 28, 2017 7:49 am
par lukamar
Hi,

when testing conjunction of boolean expressions in a program, it seems that Xcas evaluates all operands even if the first one is false. This makes the following code erroneous:

Code : Tout sélectionner

lst:=[];
if size(lst)>0 and lst[0]==0 then
  ...
fi;
Namely, lst[0]==0 is checked even if size(lst)==0. Since it seems that Xcas programming language adopts several C standards, it would be nice that conjunction testing is optimized so that expressions are evaluated in given order, stopping after first one that evaluates to false (as in C). That could not break existing programs and would make programming in Xcas much more easy in some cases. For example, in the current version the above code needs to be rewritten using nested if-clauses:

Code : Tout sélectionner

lst:=[];
if size(lst)>0 then
  if lst[0]==0 then ... fi;
fi;
which is somewhat awkward. Is there a more compact way to write a piece of code similar to the one above? Maybe I'm missing something...

Re: testing conjunction

Publié : lun. juil. 31, 2017 7:14 am
par parisse
Indeed. Looking at the source code, it happens when and has 2 arguments and I think it was added for list processing. Fixed in source now.

Re: testing conjunction

Publié : lun. juil. 31, 2017 7:53 pm
par lukamar
parisse a écrit :Indeed. Looking at the source code, it happens when and has 2 arguments and I think it was added for list processing. Fixed in source now.
Glad to hear that it was an easy fix. Thanks.