Copyright | Copyright (C) 2021 Yoo Chung |
---|---|
License | GPL-3.0-or-later |
Maintainer | dev@chungyc.org |
Safe Haskell | Safe-Inferred |
Language | GHC2021 |
Part of Ninety-Nine Haskell Problems. Some solutions are in Solutions.P26.
Synopsis
- combinations :: Int -> [a] -> [[a]]
Documentation
combinations :: Int -> [a] -> [[a]] Source #
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 \({12 \choose 3} = 220\) possibilities, where \(n \choose k\) denotes the binomial coefficient. For pure mathematicians, this result may be great. But we want to really generate all the possibilities in a list.
Examples
>>>
length $ combinations 3 [1..12]
220
>>>
sort $ combinations 3 "abcdef"
["abc","abd","abe",...]