There is a special case when the list has only one element, where the rest of the list will be []. You don’t need to match against [head] since [head | rest] will work, but just to keep that in mind
iex(1)> [head | rest] = [1, 2]
[1, 2]
iex(2)> head
1
iex(3)> rest
[2]
iex(4)> [head | rest] = [1]
[1]
iex(5)> head
1
iex(6)> rest
[]
iex(7)> [head] = [1]
[1]
iex(8)> [head] = [1, 2]
** (MatchError) no match of right hand side value: [1, 2]
Once you learn how to iterate recursively over a list, study how Enum.reduce/3 and Enum.reduce_while/3 work because pretty much everything can be implemented with these two functions. For example, your initial question could be implemented with reduce_while.


















