greater/2 exists as
Kernel.max/2, so you can rewrite your code as:
defmodule Maximum do
# Renamed to max1 to make distinct from Kernel.max/2
def max1(), do: raise "Error: Empty list"
def max1([head]), do: head
def max1([head | tail]), do: max(head, max1(tail))
Note that your version won’t work with duplicates in the list (you need a
<= case in your
Another approach is to use
Enum.reduce/2. You don’t need an accumulator, so the latter is probably best:
# Empty list handling propagates from Enum.reduce/2
def max2(list), do: Enum.reduce(list, &Kernel.max(&1, &2))
This is arguably a more functional way to do it. As Dave writes in the book: Recursion is nice, but very very often you will (or should) end up using map/filter/fold or other higher-order functions instead.
There are probably a lot of other interesting ways to do it. Some functional programming humor about the possibilities: The Evolution of a Haskell Programmer