testing conjunction

Messages in english

Modérateur : xcasadmin

lukamar
Messages : 331
Inscription : ven. juin 30, 2017 9:55 am
Localisation : Zagreb, Croatia

testing conjunction

Message par lukamar » ven. juil. 28, 2017 7:49 am

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...

parisse
Messages : 5744
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: testing conjunction

Message par parisse » lun. juil. 31, 2017 7:14 am

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.

lukamar
Messages : 331
Inscription : ven. juin 30, 2017 9:55 am
Localisation : Zagreb, Croatia

Re: testing conjunction

Message par lukamar » lun. juil. 31, 2017 7:53 pm

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.

Répondre