mirror of
https://github.com/wnagrodzki/ProgrammingElixir1.6-MyTurns.git
synced 2025-05-03 17:41:41 +02:00
25 lines
472 B
Elixir
25 lines
472 B
Elixir
# Write a max(list) that returns the element with the maximum value in the list. (This is slightly trickier than it sounds.)
|
|
|
|
defmodule MyList do
|
|
|
|
def maxlist([]) do
|
|
nil
|
|
end
|
|
|
|
def maxlist([hd | []]) do
|
|
hd
|
|
end
|
|
|
|
def maxlist([hd | tl]) do
|
|
if hd > maxlist(tl) do
|
|
hd
|
|
else
|
|
maxlist(tl)
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
IO.puts MyList.maxlist([])
|
|
IO.puts MyList.maxlist([1])
|
|
IO.puts MyList.maxlist([1, 2, 5, 3])
|