Is the analyzer making a mistake? The flatten/2 has to be public!
This is the solution:
def flatten(list, acc \\ [])
def flatten([], acc), do: Enum.reverse(acc)
def flatten([nil | tail], acc), do: flatten(tail, acc)
def flatten([head | tail], acc) when is_list(head), do: flatten(head ++ tail, acc)
def flatten([head | tail], acc), do: flatten(tail, [head | acc])
This is the comment the analyzer provided:
I changed the code:
def flatten(list, acc \\ [])
defp flatten([], acc), do: Enum.reverse(acc)
defp flatten([nil | tail], acc), do: flatten(tail, acc)
defp flatten([head | tail], acc) when is_list(head), do: flatten(head ++ tail, acc)
defp flatten([head | tail], acc), do: flatten(tail, [head | acc])
But I get this error:
exercism/elixir/flatten-array$ mix test
Compiling 1 file (.ex)
error: defp flatten/2 already defined as def in lib/flatten_array.ex:20
│
21 │ defp flatten([], acc), do: Enum.reverse(acc)
│ ^
│
└─ lib/flatten_array.ex:21:8
