How can I get permutations of a list in Elixir?
Eg, for ["a", "b", "c"]
, I would expect:
# [["a", "b", "c"], ["a", "c", "b"],
# ["b", "a", "c"], ["b", "c", "a"],
# ["c", "a", "b"], ["c", "b", "a"]]
Like this:
defmodule Permutations do
def of([]) do
[[]]
end
def of(list) do
for h <- list, t <- of(list -- [h]), do: [h | t]
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With