Heads and Tails

Earlier we said a list may either be empty or consist of a head and a tail. The head contains a value and the tail is itself a list. This is a recursive definition.

We’ll represent the empty list like this: [ ].

Let’s imagine we could represent the split between the head and the tail using a pipe character, |. The single element list we normally write as [3] can be written as the value 3 joined to the empty list:

 [ 3 | [] ]

When we see the pipe character, we say that what is on the left is the head of a list and what’s on the right is the tail.

Let’s look at the list [2, 3]. The head is 2, and the tail is the single-element list containing 3. And we know what that list looks like—it is our previous example. So we could write ...

Get Programming Elixir 1.2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.