2

The following result appears to me as a strange result.

rascal> [x|int x<-[0..3],x==2||x==2];

list[int]: [2,2]

I expected this.

rascal> [x|int x<-[0..3],(x==2||x==2)?true:false];
list[int]: [2]

1 Answers1

1

Yes, this is indeed a bit strange but consistent.

The answer is that the "loop" iterates for every time it can make the conditions true, backtracking over commas , and over && and || and !, but not (yet) over ?:, function calls, and other operators. So,

x==2 || x==2 

can be made true in two ways and that is why you get 2 answers, and with the ?: it only tries once.

This iteration behavior is necessary, since we would want this result as well:

rascal>[x|int x<-[0..3],x==2||x==3];
list[int]: [2,3]
Jurgen Vinju
  • 6,393
  • 1
  • 15
  • 26