Lists

Let’s learn about the properties and uses of lists in Elixir.

One of the most important data structures in Elixir is the list. If you’re thinking about skipping this section because lists are arrays, please stop and read on. In Elixir, lists are singly linked, meaning that each node of a list points to the next node. That’s hugely different than arrays. Arrays make random access cheap, but traversing lists takes longer.

Here’s the main point. In Elixir, a list with n elements is n different lists. Said another way, we can accurately represent [1, 2, 3] with a list construction operator, called cons cells, like this:

We have four different lists. Each list starts with an open bracket, and any code can bind to any one of those individual lists. Each | operator will create a brand-new list, leaving the tail intact. ...