Skip to content

Instantly share code, notes, and snippets.

@AndyDangerous
Created December 8, 2016 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndyDangerous/1dfa2fc0b305042b40558694ced24a95 to your computer and use it in GitHub Desktop.
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
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