Last active
January 23, 2021 10:58
-
-
Save kasvith/418082b9056b8948895031880432d097 to your computer and use it in GitHub Desktop.
Dave Thomas Programming Elixir Exercise: ListsAndRecursion-2 without Kernel.max
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 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