Tuesday, May 13, 2014

Elixir - recursive max

defmodule MyMax do
  
  defp _max([head], mem) when mem < head, do: head
  
  defp _max([_head], mem) , do: mem

  defp _max([ headl | [ headr | tail ]], mem) when headl < headr and mem < headr, do: _max([ headr | tail ], headr)
  
  defp _max([ _headl | [ headr | tail ]], mem), do: _max([ headr | tail ], mem)
  
  def max([ head | tail ]), do: _max([ head |tail], head)


end


IO.inspect MyMax.max([1, 2, 3, 4, 6])



Beautiful, atl least to me. This is an exercise from the great Programming Elixir book I'm currently reading