select
根据@Carl 的回答,我实现了select :: [a] -> [(a, [a])]功能。它的任务是生成一个 tuples 列表(a, [a]),其中 tuple 的第一部分是列表中的一个元素,而 tuple 的第二部分是列表中除该元素之外的所有元素。
select :: [a] -> [(a, [a])]
select [] = []
select (x:xs) = select' x [] xs
where
select' x left [] = [(x, left)]
select' x left right@(r:rs) = (x, left++right) : select' r (x:left) rs
但是,我select在Haskell Libraries 邮件列表中发现了更简单的实现:
select :: [a] -> [(a,[a])]
select [] = []
select (x:xs) = (x,xs) : [(y,x:ys) | (y,ys) <- select xs]
请记住,这 3 个是等效的(second是来自 的函数Control.Arrow):
[(y,x:ys) | (y,ys) <- select xs]
map (\(y,ys) -> (y,x:ys)) (select2 xs)
map (second (x:)) (select2 xs)
这是如何使用的示例select:
select [1,2,3] -- [(1,[2,3]),(2,[1,3]),(3,[2,1])]
在我实现之前,我尝试在Hayooselect中找到具有类型的函数,各种库中有几种实现:[a] -> [(a, [a])]
permutations
问题是,我们select还不足以生成所有排列。uncurry (:)我们可以使用具有类型的 对每个元组的两个部分进行 cons (a, [a]) -> [a],但我们只能得到一些排列,而不是全部:
map (uncurry (:)) (select [1,2,3]) -- [[1,2,3],[2,1,3],[3,2,1]]
很清楚为什么select [1,2,3]会创建一个列表[(1,[2,3]),(2,[1,3]),(3,[2,1])],但我们必须置换子列表,它们也是每个元组的第二部分!换句话说,如果我们有(1, [2,3]),我们也必须添加(1, [3,2])。
查找列表的所有排列的完整代码如下:
select :: [a] -> [(a,[a])]
select [] = []
select (x:xs) = (x,xs) : map (\(y,ys) -> (y,x:ys)) (select xs)
permutations :: [a] -> [[a]]
permutations [] = [[]]
permutations xs = [cons s2 | s <- select2 xs, s2 <- subpermutations s]
where cons :: (a, [a]) -> [a]
cons = uncurry (:)
subpermutations :: (a, [a]) -> [(a, [a])]
subpermutations (x,xs) = map (\e -> (x, e)) $ permutations xs
请注意,我们函数的排列顺序将不同于Data.List.permutations. 我们的函数有字典顺序,而Data.List.permutations没有:
permutations [1,2,3] -- [[1,2,3],[2,1,3],[2,3,1],[1,3,2],[3,1,2],[3,2,1]]
Data.List.permutations [1,2,3] -- [[1,2,3],[2,1,3],[3,2,1],[2,3,1],[3,1,2],[1,3,2]]
最后,如果我们进一步简化我们的permutations函数,我们会得到Rosetta Code中的实现:
select :: [a] -> [(a,[a])]
select [] = []
select (x:xs) = (x,xs) : map (\(y,ys) -> (y,x:ys)) (select xs)
permutations :: [a] -> [[a]]
permutations [] = [[]]
permutations xs = [ y:zs | (y,ys) <- select xs, zs <- permutations ys]
另请注意,使用基于插入的方法的 Rosetta Code 实现具有与Data.List.permutations.
笔记
FWIW,有一个scc :: [(a, [a])] -> [[a]]来自 package的函数uhc-util,它可以找到Graph 的强连通分量。元组的第一部分是一个顶点,第二部分是所有顶点,从顶点到边。IOW,图1 --> 2 --> 3变为[(1, [2]), (2, [3])]。
scc [(1,[2,3,4]),(2,[1,3,4]),(3,[2,1,4]),(4,[3,2,1])] -- [[3,4], [1,2]]