All permutations
Permutations module
defmodule Permutations do
def all(list) do
1..length(list)
|> Enum.reduce([], fn n, acc ->
acc ++ all(list, n)
end)
end
def all(_, 0), do: [[]]
def all([], _), do: []
def all(list, n) do
for x <- list,
rest = list -- [x],
perm <- all(rest, n - 1),
do: [x | perm]
end
end
Usage example
list = [1, 2, 3, 4]
Permutations.all(list, 2)
Permutations.all(list, 3)
Permutations.all(list)