Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of specific list comprehension in Haskell

I've a question regarding list comprehension

[(x,y)| x<-[1..2], y<-[x..3], let z = x+y, odd z]

Why does this evaluate to:

[(1,2),(2,3)]

?

Where is the z going?

Thanks

like image 823
Nico S. Avatar asked Oct 18 '25 16:10

Nico S.


1 Answers

Your predicate is "z = x + y for all z odd". If you "unroll" the flow:

z = predicate, and y(x) so for:

x = 1,2
y (1) = 1,2,3
y (2) = 2,3

Based on the combination of the values filtered by the predicate:

x+y <= filter(z)

1+1 = 2 NO
1+2 = 3 OK
1+3 = 4 NO

2+2 = 4 NO
2+3 = 5 OK

so the ok answers are for x = 1 and y = 2 and x = 2 and y =3 => [(1,2), (2,3)]

like image 121
Randomize Avatar answered Oct 22 '25 03:10

Randomize