In this shot, we will learn how to use the length()
function to calculate the length of a list in Elixir.
A list is a heterogeneous collection of items in Elixir.
[]
and separated with a comma ,
.length(list)
This method accepts a list
as a parameter.
The length()
function returns the length of the list that is passed as a parameter.
In the example below, we will calculate the length of the list of numbers.
# declare and initialize the listn = [11, 22, 33, 44, 55, 66]#calculate lengthl = length(n)#print the lengthIO.puts "The length of the list is #{l}"
In the code above:
n
with some numbers.length()
function to calculate the length of the list n
and assign the returned length to variable l
.IO.puts
to print the length of the variable l
.