如何获得列表的排列? [英] How can I get permutations of a list?
本文介绍了如何获得列表的排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Elixir中获得列表的排列?
How can I get permutations of a list in Elixir?
例如,对于 [ a, b, c ]
,我希望:
# [["a", "b", "c"], ["a", "c", "b"],
# ["b", "a", "c"], ["b", "c", "a"],
# ["c", "a", "b"], ["c", "b", "a"]]
推荐答案
像这样:
defmodule Permutations do
def of([]) do
[[]]
end
def of(list) do
for h <- list, t <- of(list -- [h]), do: [h | t]
end
end
这篇关于如何获得列表的排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文