Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule ListUtilities do
- @doc """
- Flattens the given `list` of nested lists.
- ## Examples
- iex> ListUtilities.flatten([[1,2,[3]],4])
- [4, 3, 2, 1]
- """
- @spec flatten(list()) :: list()
- def flatten(list), do: do_flatten(list, [])
- defp do_flatten([head | tail], acc ) when is_list(head), do: do_flatten(tail, do_flatten(head, acc))
- defp do_flatten([head | tail], acc ), do: do_flatten(tail, [head| acc])
- defp do_flatten([], acc), do: acc
- end
- ListUtilities.flatten([[1,2,[3]],4]) |> IO.inspect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement