在 Haskell 中迭代所有对组合而不重复 [英] Iterate over all pair combinations without repetition in Haskell
本文介绍了在 Haskell 中迭代所有对组合而不重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在haskell中,给定一个元素列表,xs
,最简单的方法是用重复迭代所有对排列:
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]
我希望能够做同样的事情,但仅限于组合.如果 x 和 y 具有可比性,我可以做
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)
推荐答案
关于:
[ (x,y) | (x:rest) <- tails xs , y <- rest ]
这篇关于在 Haskell 中迭代所有对组合而不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文