Created
December 8, 2016 22:58
-
-
Save AndyDangerous/1dfa2fc0b305042b40558694ced24a95 to your computer and use it in GitHub Desktop.
a bunch of recursive functions based on the little elixir and OTP book
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 Mods do | |
def list_len(list) do | |
list_len(0, list) | |
end | |
defp list_len(acc, []), do: acc | |
defp list_len(acc, [_h|t]) do | |
list_len((acc + 1), t) | |
end | |
def range(from, to) do | |
range([], to, from) | |
end | |
defp range(acc, to, to), do: acc |> Enum.reverse | |
defp range(acc, to, from) do | |
range([from | acc], to, (from + 1)) | |
end | |
def positive(list) do | |
positive([], list) | |
end | |
def positive(acc, []), do: acc |> Enum.reverse | |
def positive(acc, [h|t]) when h >= 0 do | |
positive([h | acc], t) | |
end | |
def positive(acc, [h|t]) when h < 0 do | |
positive(acc, t) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment