links: [[Elixir MOC]] --- Lists are built-in to Elixir language, they are considered as basic type, denoted by square bracket notation. Lists maybe empty or hold any number of items of any type. ```elixir empty_list = [] one_item_list = [1] two_item_list = [1, 2] multi_type_list = [1, :pi, 2.14, "four"] ``` Elixir implements lists as a Linked List, where each node stores two items: the first item and another list with all the remaining items. The first item in the list is referred to as the *head* and the remaining list of items is called the *tail*. We can use this notation in code ```elixir # [1] represented in [head | tail] notation [1 | []] # [1, 2, 3] represented in [head | tail] notation [1 | [2 | [3 | []]]] ``` We can use *`[head | tail]`* notation to prepend elements to a list ```elixir # suppose list = [2, 1] [3, 2, 1] == [3 | list] # => true ``` There are several functions in the `Kernel` module for working with lists, as well as the whole `List` module ```elixir # Check if 1 is a member of the list 1 in [1, 2, 3, 4, 5] # => true ``` --- tags: #elixir #list sources: - [Linked Lists Elixir Lang documentation](https://elixir-lang.org/getting-started/basic-types.html#linked-lists) - [List module hexdocs](https://hexdocs.pm/elixir/List.html) -