How to rewrite Erlang combinations algorithm in Elixir?

前端 未结 1 864
情深已故
情深已故 2021-01-04 13:31

I\'ve been tinkering with Elixir for the last few weeks. I just came across this succinct combinations algorithm in Erlang, which I tried rewriting in Elixir but got stuck.<

相关标签:
1条回答
  • 2021-01-04 14:09

    You need to wrap the for expression in parentheses like the Erlang code.

    def combination(n, [x|xs]) do
      (for y <- combination(n - 1, xs), do: [x|y]) ++ combination(n, xs)
    end
    

    Demo:

    iex(1)> defmodule Foo do
    ...(1)>   def combination(0, _), do: [[]]
    ...(1)>   def combination(_, []), do: []
    ...(1)>   def combination(n, [x|xs]) do
    ...(1)>     (for y <- combination(n - 1, xs), do: [x|y]) ++ combination(n, xs)
    ...(1)>   end
    ...(1)> end
    {:module, Foo,
     <<70, 79, 82, 49, 0, 0, 6, 100, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 137, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 2, 104, 2, ...>>,
     {:combination, 2}}
    iex(2)> Foo.combination 2, [1, 2, 3]
    [[1, 2], [1, 3], [2, 3]]
    
    0 讨论(0)
提交回复
热议问题