Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rewrite Erlang combinations algorithm in Elixir?

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.

Erlang version:

comb(0,_) ->
  [[]];
comb(_,[]) ->
  [];
comb(N,[H|T]) ->
  [[H|L] || L <- comb(N-1,T)]++comb(N,T).

Elixir version I came up with this, but it's not correct:

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

Example usage, with incorrect results:

iex> combination(2, [1,2,3])
[[1, 2, [3], [2, 3]]]

Any pointers on what I'm doing wrong?

Thanks!
Sean

like image 749
seanomlor Avatar asked Jun 02 '15 01:06

seanomlor


1 Answers

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]]
like image 197
Dogbert Avatar answered Nov 09 '22 16:11

Dogbert