在Haskell中重复使用所有对组合而不重复 [英] Iterate over all pair combinations without repetition in Haskell
本文介绍了在Haskell中重复使用所有对组合而不重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
xs
,对所有对重复进行迭代遍历的最简单方法是: [(x,y)| x < - xs,y < - xs]
我希望能够做同样的事情,但只限于组合。如果x和y是可比较的,我可以做
pre $ [(x,y)| x< - xs,y< - xs,x> y]
但我更喜欢一个更通用更高效的解决方案(我知道asympotitic复杂度将保持平方,但我们可以通过避免使用过滤条件将实际的运行时复杂度减半)。解决方案
那么:
/ p>
[(x,y)| (x:rest)< - tails xs,y< - rest]
In haskell, given a list of elements, xs
, the simplest way to iterate over all pair permutations with repetitions is:
[(x,y) | x <- xs, y <- xs]
I wish to be able to do the same, but only on combinations. If x and y were comparable, I could do
[(x,y) | x <- xs, y <- xs, x > y]
But I'd prefer a solution that is more generic and more efficient (I know that asympotitic complexity will remain squared, but we can halve actual runtime complexity by avoiding the usage of a filtering condition)
解决方案
What about:
[ (x,y) | (x:rest) <- tails xs , y <- rest ]
这篇关于在Haskell中重复使用所有对组合而不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文