【发布时间】:2011-03-24 04:12:19
【问题描述】:
给定两个列表,我可以生成 所有排列的列表这两个列表的笛卡尔积:
permute :: [a] -> [a] -> [[a]]
permute xs ys = [ [x, y] | x <- xs, y <- ys ]
Example> permute [1,2] [3,4] == [ [1,3], [1,4], [2,3], [2,4] ]
我如何扩展 permute 以便不是采用两个列表,而是采用一个列表(长度 n)并返回一个列表列表(长度 n)
permute :: [[a]] -> [[a]]
Example> permute [ [1,2], [3,4], [5,6] ]
== [ [1,3,5], [1,3,6], [1,4,5], [1,4,6] ] --etc
我在 Hoogle 上找不到任何相关内容。与签名匹配的唯一函数是 transpose,它不会产生所需的输出。
编辑:我认为它的 2-list 版本本质上是 Cartesian Product,但我无法完全实现 n-ary Cartesian Product。有什么指点吗?
【问题讨论】:
标签: haskell combinatorics cartesian-product