elixir "hd" programming
To find the head of a list in Elixir, you can use the hd/1
function. This function takes a list as an argument and returns the first element of that list. Here's an example:
list = [1, 2, 3, 4, 5]
head = hd(list)
IO.inspect(head) # Output: 1
In this example, we have a list [1, 2, 3, 4, 5]
. By calling hd(list)
, we retrieve the first element of the list, which is 1
. The IO.inspect/1
function is used to print the value of head
to the console.
It's important to note that hd/1
will raise an ArgumentError
if called with an empty list. Therefore, it's a good practice to check if the list is empty before calling hd/1
to avoid runtime errors.