我有一个列表列表,需要将其与另一个列表列表合并。 输入示例: 答:[[1,2],[3,4],[5,6],[7,8]] B:[[1,2],[3,4],[5,6],[7,8]]
示例输出: [[1,2,1,2],[1,2,3,4],..,[7,8,5,6],[7,8,7,8]]
2个列表列表,其中两个列表都包含4个列表,则会返回一个列表列表,大小为4 * 4 = 16
我已经尝试过递归地合并列表,但是我知道即使通过列表也行不通。
mergeAll [[]] [[]] = [[]]
mergeAll [[]] b = b
mergeAll a [[]] = a
mergeAll xs ys = mergeAll (merge xs ys) (drop 1 ys)
merge :: [[a]] -> [[a]] -> [[a]]
merge [[]] [[]] = [[]]
merge xs [[]] = xs
merge [[]] ys = ys
merge (x:xs) (y:ys) = ((x++y):xs)
答案 0 :(得分:4)
您可以使用列表理解:
[ xs ++ ys | xs <- listOfLists1, ys <- listOfLists2 ]
答案 1 :(得分:1)
您可能会喜欢
Prelude> let doit = \as bs -> as >>= \a -> bs >>= \b -> pure (a ++ b)
Prelude> doit [[1,2],[3,4],[5,6],[7,8]] [[1,2],[3,4],[5,6],[7,8]]
[[1,2,1,2],[1,2,3,4],[1,2,5,6],[1,2,7,8],[3,4,1,2],[3,4,3,4],[3,4,5,6],[3,4,7,8],[5,6,1,2],[5,6,3,4],[5,6,5,6],[5,6,7,8],[7,8,1,2],[7,8,3,4],[7,8,5,6],[7,8,7,8]]
答案 2 :(得分:0)