Explanation of specific comprehension of a list in Haskell

I have a question about list comprehension

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

      

Why it is rated as:

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

      

?

Where is z located?

thank

+3


source to share


1 answer


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

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

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

      



Based on a combination of 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 match x = 1 and y = 2 and x = 2 and y = 3 => [(1,2), (2,3)]

+4


source







All Articles