Published: February 27, 2018
by Tobias Pleyer
Tags: haskell

Haskell’s prelude list index function

Indexing lists in Haskell

Haskell’s Prelude and the Data.List module both export the !! operator to take the index of a list.

Haskell’s standard list type is defined as follows

Which means that a list is either empty (Nil) or consists of an element and a reference (link) to the rest of the list, which can be empty or have more elements. Such an implementation is called singly linked lists.

Singly linked list do not provide a direct index lookup, because they do not hold information how to extract the element directly. Instead, if one wants to to have the element at index i, he has to walk the linked list and follow the links i times.

One possible implementation would be

The implementation is very straight forward: We pattern match the list. If we reached the empty list we have no chance to find an element anymore and we error out. Note that this will throw an exception at run time and is thus unsafe. This is equivalent to the implementation of Haskell’s !! operator. Otherwise we pattern match the current element and the rest of the list, if our index is 0, we know what to do, the 0th index of a list is the first element, so we return it. But if the index is bigger than zero then we decrement it and recurse with the rest of the list.

Note: I left out a check for negative indices.

The Data.List implementation

Now we come to the reason for this post: The actual implementation of !! in Data.List. I think it is a very nice and simple example of the fascinating options functional and lazy programming provide.

First the implementation:

Note: Actually there are two implementations of !! in the package, guarded by macros to choose only one of them. The version above is the second option which avoids inlining calls to error. The first option is actually the implementation I gave in the introduction.

Let’s have a closer look at how this piece of code works. First the implementation of foldr:

So far so good. Often f used in foldr is a binary function which produces a ‘primitive’ result, like an integer. For example in the case of one of the most used folding examples:

Disclaimer: Memory usage and strictness considerations are not of interest here. The given implementation of sum is one of many possible implementations.

But the return value of the folding function is not limited to these cases. Functions for example are also first class values in Haskell and totally valid as return value of the folding function.

Because Haskell features currying the effect might be a bit obfuscated in the above example, but we can make the implicit function return value explicit:

The more interesting point is that the returned function is a closure (lexically scoped name binding) of the other two parameters, namely the current element of the list (x) and the remaining fold of functions (r). The variable k is later bound to the requested index.

So what this function does is create a function which takes one parameter, the index, and either returns the bound value of the list element if the index is zero, or calls the continuation function (created by foldr) with the decremented index. This is equivalent to the sample implementation given at the beginning.

Let’s see the implementation in action:

Note that due to lazy evaluation we just calculate as much as necessary to yield a result and do not have to fear the creation of the additional helper closures.