Member-only story
Elixir — Differences between hd and List.first
2 min readOct 13, 2017
A tip to BOOST your elixir coding…
To boost up your Elixir coding, you need to understand a few differences between these two even though they are used for the same purpose.
hd ( vs ) List.first [ diff…]
We all knew that hd
& List.first
are used to pick out the first
element in a list
. But, why two functions for the same purpose?
However, they differ slightly in dealing with the empty list []
and in guard tests.
Differences
- on Empty lists
The functionhd
on empty list[]
results inArgument Error
The functionList.first
on empty list[]
results innil
- on guard tests
The functionhd
is allowed in guard tests. In lined by the compiler.
The functionList.first
is not allowed in guard tests.
Lets check this in live…
Hope now you got some idea and understood where and when to use them.