Skip to content

Instantly share code, notes, and snippets.

@bullno1
Last active August 29, 2015 13:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bullno1/9324443 to your computer and use it in GitHub Desktop.
Permutations & Combinations
-module(pnc).
-export([combinations/2, permutations/2]).
combinations(0, _Elements) -> [[]];
combinations(_Size, []) -> [];
combinations(Size, [Elem | Elements]) ->
[[Elem | Combination] || Combination <- combinations(Size - 1, Elements)]
++ combinations(Size, Elements).
permutations(0, _Elements) -> [[]];
permutations(Size, Elements) ->
lists:flatmap(
fun(Element) ->
[[Element | Permutation] || Permutation <- permutations(Size - 1, Elements -- [Element])]
end,
Elements).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment