The long list of variables in the where clause are there for educational purpose.

The long list of variables in the where clause are there for educational purpose.

+

+

+

[[Category:Programming exercise spoilers]]

Latest revision as of 19:40, 18 January 2014

(**) Generate the combinations of K distinct objects chosen from the N elements of a list

In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the
well-known binomial coefficients). For pure mathematicians, this result may be great. But we want to really generate all the possibilities in a list.

Variant: This version avoids getting a result consisting of the empty choice if we ask for, say, 10 elements from the empty list. (Instead, an empty list, suggesting "no options available" is returned.)

A funny solution using Applicative in Control.Applicative. Funny because it's the opposite of other solutions.
The idea is to generate all possible combinations by allowing permutations and by allowing multiple instances of one value in the list.
Then we filter the list to stick with the exercice.