如何从列表列表中获取组合列表?
例如:
ghci> getCombinations [[1,2,3],[10,20,30],[100,200,300]]
[[1,10,100],[1,10,200],[1,10,300],[1,20,100],[1,20,200],[1,20,300],[1,30,100],
[1,30,200],[1,30,300],[2,10,100],[2,10,200],[2,10,300],[2,20,100],[2,20,200],
[2,20,300],[2,30,100],[2,30,200],[2,30,300],[3,10,100],[3,10,200],[3,10,300],
[3,20,100],[3,20,200],[3,20,300],[3,30,100],[3,30,200],[3,30,300]]
答案 0 :(得分:2)
import Control.Applicative
getCombinations :: [[a]] -> [[a]]
getCombinations values =
foldl (\acc values -> (\list value -> list ++ [value]) <$> acc <*> values) [[]] values
getCombinations [[1,2,3],[10,20,30],[100,200,300]]
[[1,10,100],[1,10,200],[1,10,300],[1,20,100],[1,20,200],[1,20,300],[1,30,100],[1,30,200 ],[1,30,300],[2,10,100],[2,10,200],[2,10,300],[2,20,100],[2,20,200],[2,20,300],[2,30,100] [2,30,200],[2,30,300],[3,10,100],[3,10,200],[3,10,300],[3,20,100],[3,20,200],[3,20,300],[3 ,30100],[3,30,200],[3,30,300]]