Skip to content

Instantly share code, notes, and snippets.

@jadlr
Last active February 22, 2016 11:19
Show Gist options
  • Save jadlr/59ed178ab900bdae3635 to your computer and use it in GitHub Desktop.
Save jadlr/59ed178ab900bdae3635 to your computer and use it in GitHub Desktop.
Permutations of a certain length of a list in elixir (v2)
defmodule ListPermutations do
def list_permutations(_, 0), do: [[]]
def list_permutations([], _), do: []
def list_permutations([h|t], n), do: Enum.map(list_permutations(t, n-1), fn(l) -> [h|l] end) ++ list_permutations(t, n)
end
@jadlr
Copy link
Author

jadlr commented Feb 22, 2016

note: instead of fn(l) -> [h|l] end one could have written &[h|&1] which I don't really like

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment