I am having trouble understanding they way the next two lines of code work.
ghci> take 6 [[(i,j) | i<-[2,4]] | j<-[1,3,5]]
[[(2,1),(4,1)],[(2,3),(4,3)],[(2,5),(4,5)]]
ghci> take 6 [[(i,j)] | i<-[2,4], j<-[1,3,5]]
[[(2,1)],[(2,3)],[(2,5)],[(4,1)],[(4,3)],[(4,5)]]
I understand what take does but i dont understand the difference between the two lines and the order of the iterration and what the paranthesis do for the code. I am new at Haskell sorry if my question is easy.
The difference is not in the
take, the difference is the list comprehension. The first one has two list comprehensions, withj <- [1,3,5]the outer list comprehension:whereas the latter has one list comprehension, since
j <- [1,3,5]is the second "generator" in the list comprehsnion, that is the "inner" generator.This thus means that in the first one, the
iis the fastest changing variable, andjonly changes afteriis fully exhausted, whereas for the second one, it is the opposite way.If we thus would convert it to some generator syntax in Python, it would look like:
so this means the order how the same elements are generated changes.