Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing logical operators in Perl 6

Tags:

raku

I'm trying to reduce and and get a strange output:

> [and] 2>1, 3>2, put 1, put 2
2
1True

Meanwhile if I put and between each expression, everything is OK.

> 2>1 and 3>2 and put 1 and put 2
1
2

What's the reason of this difference?

like image 419
Eugene Barsky Avatar asked Sep 26 '18 21:09

Eugene Barsky


1 Answers

To elaborate on Håkon's comment. The difference is in precedence.

The two statements are equivalent to these:

> [and] (2>1), (3>2), (put 1,(put 2))
2
1True
> (2>1) and (3>2) and (put 1) and (put 2)
1
2

In the first case, it first evaluates put 2, printing the 2 and returning True, then the put 1,True, printing 1True, returning True. The whole expression evaluates to True, but doesn't print anything more.

In the second case, The put 1 evaluates first, printing the 1 and returning True, then the put 2 evaluates, printing the 2 and also returning True. Again, the whole expression evaluates to True but doesn't print anything more.

like image 104
Curt Tilmes Avatar answered Nov 15 '22 09:11

Curt Tilmes