elixir - 如何获得列表的排列?

标签 elixir

如何在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

关于elixir - 如何获得列表的排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33756396/

相关文章:

templates - 在 Phoenix Framework 的模板文件中加载环境变量

elixir - 离线构建十六进制注册表文件

elixir - 在 Phoenix 中渲染 JSON

pattern-matching - 针对空 map 的模式匹配功能

elixir - Ecto 以科学(指数)表示法返回数字

generics - 如何在 Elixir 中类型指定泛型

elixir - 在 Elixir 中,put_in 中的叶节点是列表,而不是映射

elixir - 从 SQL 查询中填充虚拟字段

erlang - 转换包含抽象树的文件

elixir - Ecto 模式默认值不包含在变更集更改中