Reputation: 61
I assume its simple as its in a past paper of my university but the function is:
[(x,y) | x <- [0..2], y<-[0,x])
and the output
[(0,0),(0,0),(1,0), (1,1), (2,0), (2,2)]
The (2,0) confuses me, if y maps to 0 to x whilst x is equal to 1 = (1,1) wouldnt it be
[(0,0),(0,0),(1,0), (1,1), **(2,1)**, (2,2)]
or is it because due to the y using all its numbers in its list [0,1] it reverts back to 0?
Upvotes: 0
Views: 84
Reputation: 52270
[(x,y) | x <- [0..2], y<-[0,x]]
[0,x]
is:
[0,0]
for x=0
[0,1]
for x=1
[0,2]
for x=2
so if you pair each of those y
s up with the corresponding x
you get:
[(0,0),(0,0)] -- x = 0
++ [(1,0),(1,1)] -- x = 1
++ [(2,0),(2,2)] -- x = 2
which yields your given output
note: [0,2]
has length 2 and is quite different from [0..2]
which has length 3 and contains 1
[(x,y) | x <- [0..2], y<-[0..x]]
it's not that different - [0..x]
is:
[0]
for x=0
[0,1]
for x=1
[0,1,2]
for x=2
and if you pair each of those y
s up with the corresponding x
you get
[(0,0))] -- x = 0
++ [(1,0),(1,1)] -- x = 1
++ [(2,0),(2,1),(2,2)] -- x = 2
which then would give you the result
[(0,0),(1,0),(1,1),(2,0),(2,1),(2,2)]
Upvotes: 6