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 | [ ] ] |
(I’ve highlighted the inner list.)
When we see the pipe character, we say that what’s 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access