haskell - Generate a list of unique combinations from a list -
i want generate list of unique ways choose 2 list of numbers in haskell. list [1,2,3]
[[1,2],[2,3],[1,3]]
. order not important want avoid producing both [1,2]
, [2,1]
example.
my current solution is:
pairs :: ord => [a] -> [[a]] pairs x = nub $ map sort $ map (take 2) (permutations x)
this isn't particularly nice solution , has serious performance issues. there simple , efficient solution problem?
pairs xs = [[x1, x2] | (x1:xs1) <- tails xs, x2 <- xs1]
...assuming list starts out unique, or compose nub
otherwise.
Comments
Post a Comment