Last active
February 22, 2016 11:19
-
-
Save jadlr/59ed178ab900bdae3635 to your computer and use it in GitHub Desktop.
Permutations of a certain length of a list in elixir (v2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note: instead of
fn(l) -> [h|l] endone could have written&[h|&1]which I don't really like