Elixir Fiddle
Home
Fragments
About
Examples
Create New Fragment
Edit Fragment
Name
Content
# Exercise: ListsAndRecursion-2 # 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 max([a]), do: a def max([a,b | tail]) do max([greater(a,b)] ++ tail) end defp greater(a,b) when a > b do a end defp greater(a,b) when b > a do b end defp greater(a,b) when a == b do a end end MyList.max([1,4,2,3])
Run Elixir Fragment
Submit
Back