Last active
October 21, 2018 18:55
-
-
Save Kottakji/b7f015166dee1a8a079e9bb9c4d26e56 to your computer and use it in GitHub Desktop.
Flatten in elixir without library functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyList do | |
def flatten([]) do | |
[] | |
end | |
def flatten(value, []) when is_integer(value) do | |
value | |
end | |
def flatten([head | []], enumerates) when is_list(enumerates) do | |
[head | flatten(enumerates)] | |
end | |
def flatten([head | tail]) when is_integer(head) and is_list(tail) do | |
[head | flatten(tail)] | |
end | |
def flatten([head | tail], enumerate) when is_integer(head) and is_integer(tail) and is_list(enumerate) do | |
[ head, tail | flatten(enumerate)] | |
end | |
def flatten([head | tail], enumerate) when is_integer(head) and is_list(tail) and is_list(enumerate) do | |
[ head | flatten(tail, enumerate)] | |
end | |
def flatten([head | []]) when is_list(head) do | |
flatten(head) | |
end | |
def flatten([head | tail]) when is_list(head) and is_list(tail) do | |
flatten(head, tail) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment