Get all possible combinations of k elements from a list


Tobias Herman

I need a function that does the same thing as in pythonitertools.combinations(iterable, r)

So far I have come up with this:

{-| forward application -}
x -: f = f x
infixl 0 -:

{-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -}
combinations :: Ord a => Int -> [a] -> [[a]]
combinations k l = (sequence . replicate k) l -: map sort -: sort -: nub
    -: filter (\l -> (length . nub) l == length l)

Is there a more elegant and efficient solution?

Frank Schmidt

(Based on @JoseJuan's answer)

You can also use a list comprehension to filter out characters where the second character is not strictly smaller than the first:

[x| x <- mapM (const "ABCD") [1..2], head x < head (tail x) ]

Related


get all possible combinations of k elements from a list

Tobias Herman I need a function that does the same thing as itertools.combinations(iterable, r) in python So far I came up with this: {-| forward application -} x -: f = f x infixl 0 -: {-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -} combinatio

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Get all possible combinations of elements

sa555 How would you get all possible combinations of 2 elements in an array? E.g: [ 1, 2, 3, 4 ] becomes [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4

Python - How to get all possible combinations from a nested list

Hamza Arshad: I have nested lists like this: values = [['DNO', 0.2], ['Equinor', 0.4], ['Petoro', 0.2], ['Total', 0.2]] How can I get all possible combinations of elements that will sum (the second element of each sublist) greater than 0.5? This is what I'm u

Python - How to get all possible combinations from a nested list

Hamza Arshad: I have nested lists like this: values = [['DNO', 0.2], ['Equinor', 0.4], ['Petoro', 0.2], ['Total', 0.2]] How can I get all possible combinations of elements that will sum (the second element of each sublist) greater than 0.5? This is what I'm u

Python - How to get all possible combinations from a nested list

Hamza Arshad: I have nested lists like this: values = [['DNO', 0.2], ['Equinor', 0.4], ['Petoro', 0.2], ['Total', 0.2]] How can I get all possible combinations of elements that will sum (the second element of each sublist) greater than 0.5? This is what I'm u

intersect with all possible combinations of list elements

Enriferorello I have a list of vectors: > l <- list(A=c("one", "two", "three", "four"), B=c("one", "two"), C=c("two", "four", "five", "six"), D=c("six", "seven")) > l $A [1] "one" "two" "three" "four" $B [1] "one" "two" $C [1] "two" "four" "five" "six"