相关疑难解决方法(0)

计算n元笛卡儿积

给定两个列表,我可以生成这两个列表的笛卡尔积的所有排列的列表:

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] ]
Run Code Online (Sandbox Code Playgroud)

如何扩展置换,以便不使用两个列表,而是获取列表的列表(长度为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
Run Code Online (Sandbox Code Playgroud)

我在Hoogle上找不到任何相关的东西..唯一与签名相匹配的功能是transpose,它不会产生所需的输出.

编辑:我认为这个2列表版本基本上是笛卡尔积,但我不能完全实现n-ary笛卡尔积.有什么指针吗?

haskell combinatorics cartesian-product

11
推荐指数
3
解决办法
4626
查看次数

标签 统计

cartesian-product ×1

combinatorics ×1

haskell ×1