NFC.cool
NFC.cool

Reputation: 3312

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

Upvotes: 4

Views: 86

Answers (1)

Randomize
Randomize

Reputation: 9103

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)]

Upvotes: 4

Related Questions