Lesson 11 of 16
Lists
Lists
Lists are ordered collections in Elixir:
nums = [1, 2, 3, 4]
IO.puts(length(nums)) # 4
hd and tl
hd returns the head (first element), tl returns the tail (rest):
IO.puts(hd([1, 2, 3])) # 1
IO.puts(length(tl([1, 2, 3]))) # 2
Concatenation with ++
a = [1, 2, 3]
b = [4, 5, 6, 7]
IO.puts(Enum.sum(a ++ b)) # 28
Your Turn
Given nums = [1, 2, 3, 4, 5]:
- Print
length(nums)→ 5 - Print
hd(nums)→ 1 - Print
length(tl(nums))→ 4 - Print
Enum.sum(nums ++ [6, 7])→ 28
JS Transpiler loading...
Loading...
Click "Run" to execute your code.