sorteerOpY :: (Int, Int) -> [[(Int, Int)]]
sorteerOpY (x,y) = [[(a,b)]|b<-[0..y-1],a<-[0..x-1]]
这就是我现在所拥有的,sorteerOpY (2,3)
导致:
[[(0,0)],[(1,0)],[(0,1)],[(1,1)],[(0,2)],[(1,2)]]
但这就是我想要的结果:
[[(0,0),(1,0)],[(0,1),(1,1)],[(0,2),(1,2)]]
我需要更改哪些内容才能正确创建列表?
答案 0 :(得分:1)
您可以将列表推导视为[[(a, b) | b <- [0..y-1]]| a<-[0..x-1]]
,如下所示:
for every b <- [0..y-1]
xs <- for every a <- [0..x-1]
add (a, b) to the resulting list
add xs to the resulting list
因此,如果a
中的(a, b)
变化更快,则需要交换这些行:
for every a <- [0..x-1]
xs <- for every b <- [0..y-1]
add (a, b) to the resulting list
add xs to the resulting list
直接导致[[(a,b) | a <- [0..x-1]]| b <- [0..y-1]]
。