Skip to content

Instantly share code, notes, and snippets.

@kasvith
Last active January 23, 2021 10:58
Show Gist options
  • Save kasvith/418082b9056b8948895031880432d097 to your computer and use it in GitHub Desktop.
Save kasvith/418082b9056b8948895031880432d097 to your computer and use it in GitHub Desktop.
Dave Thomas Programming Elixir Exercise: ListsAndRecursion-2 without Kernel.max
defmodule MyList do
def max([]), do: nil
def max([head | tail]), do: findmax(head, head, tail)
defp findmax(current, _next, [head | tail]) do
if head > current, do: findmax(head, current, tail), else: findmax(current, current, tail)
end
defp findmax(current, next, []) do
if current > next, do: current, else: next
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment