I have trouble understanding how default parameters interact with multiple clauses in named functions. It boils down to, why does the following snippet work?
defmodule Lists do
def sum([], total \\ 0), do: total
def sum([h|t], total), do: h + sum(t, total)
end
From my understanding this gets expanded by the compiler into:
defmodule Lists do
def sum([]), do: sum([], 0)
def sum([], total), do: total
def sum([h|t], total), do: h + sum(t, total)
end
So I would expect the following to happen:
iex(1)> Lists.sum [1,2,3,4]
** (FunctionClauseError) no function clause matching in Lists.sum/1
instead it works:
iex(1)> Lists.sum [1,2,3,4]
10
Using Elixir 0.12.4.