# Lists in F#

In this post I will talk about the lists in F#, one of the fundamental concepts of the language. What should be said from the very beginning is that list are imutable single linked list. That means whenever you change a list, a new list is created.

You can declare a list in the following ways:

To print the content of the list you can do this:

You can concatenate two lists with operator @:

and you can append elements to the beginning of the list with operator ::

You can also use the List (defined in Microsoft.FSharp.Code) functionality to print a list by iterating over its elements:

The same can be achieved using the pipe operator:

You can also iterate and get the index of the list elements, with List.iteri:

List have a special representation, a head followed by a tail, that is in turn another list (including empty list []). Let’s consider the list [1;2;3]. It has the head 1, and the tail [2;3]. The tail, in turn, has the head 2 and the tail [3]. This tail has the head 3 and the tail [], which is the empty list.
You can see the head and tail of a list with List.hd and List.td:

The ouput for list1 [1;2;3] is:

Enough with basic things. Let’s try working with lists.

### 1. Minimum and maximum from a list

We can compute the maximum (or minimum) of a list using the following algorithm:

• if the list is empty, indicate error
• if the list has only one element, that is the maximum (or minimum)
• if the list has at least to elements, compute the maximum between that element and the maximum from the rest of the list

That sounds like a recursive operation, which can be simply put in F# like this:

We can use that like this:

and the output would be:

### 2. Reversing a list

How would we reverse a list? We should take the last element and append to it the one before the last. To the new list we append the one before the one before the end, etc. That again sounds recursive.

And here is the output:

### 3. Inserting in a list

So how could we insert an element in a list, before or after a specified element? We can use the following algorithm:

• if the list is empty, the new list has one element (the one to insert)
• else, if the head is the element we are looking for, create a list, with the new element either before the head, or between the head and the tail
• else, if the head is not the element we are looking for, append the head to a list created by inserting the new element in the tail.

You got that right, recursion again.

And the output is:

### 4. Removing elements from a list

As a last exercise, let’s consider the removing of elements from a list. The following steps can be used to remove elements:

• if the list is empty, return an empty list
• if the list is not empty and the head meets the removing criteria, return a list obtained by reiterating the algorithm on the tail of the list
• if the list is not empty and the head does not meet the removing criteria, return a list obtained by appending the head to a list optained by reiterating the algorithm on the tail of the list

The great thing about this implementation is that we can pass a lambda expression as a predicate, and use it to specify the criteria for removing elements. We can remove like that, for instance, the odd elements, or the even elements, or the negative elements. Here is some sample code:

The output for this sample is:

I hope this will help you to get a grip on how you can work on lists in F#.

This site uses Akismet to reduce spam. Learn how your comment data is processed.