Lists are widely used in functional programming languages, and Elixir is no exception.
Although lists might resemble other languages' arrays, they actually behave more like single-linked lists. Operations with lists are quite common, so in this recipe, we will show you how to add two lists or subtract one list from another.
To add lists, we will use the ++
operator. The steps are as follows:
Create a list named
list_one
:iex(1)> list_one = [1, 3, 5] [1, 3, 5]
Create a list named
list_two
:iex(2)> list_two = [2, 4, 6, 5] [2, 4, 6, 5]
Add
list_one
tolist_two
:iex(3)> list_one ++ list_two [1, 3, 5, 2, 4, 6, 5]
Add
list_two
tolist_one
:iex(4)> list_two ++ list_one [2, 4, 6, 5, 1, 3, 5]