A-level Computing/AQA/Paper 2/Fundamentals of functional programming/List processing





A list is an abstract data type that holds items in a given order. The same item may be be in the same list multiple times. For example the following are lists:

An empty list is defined by empty parenthesis:

Lists can also contain other lists:

In Haskell the items contained in a list must all be of the same datatype.

Whilst some programming languages such as Haskell will only allow datatype for the items in a list, it is sometimes possible to have multiple datatypes in a list. Whilst this might be possible it might not be a good idea and can make some very hard to debug code. For example, in python we might have an unexpected output from some very simple looking code:

Making and checking a list
You can perform a range of functions on lists:

You can make an empty list:

You can make a list that contains items:

You can get the length of a list:

You can check if a list is empty

Chopping up a list
Some very common list commands are head and tail. If you treat a list like a snake, head will give you the first item in the list (its head), and tail will remove the head of the snake and give you the rest a new list.



You can combine multiple head and tail commands together

In the above example we start from the innermost function, in this case  and work our way outwards.

The command  takes the first item off   and returns the rest:. We can then replace  with this returned list:

We now move to next innermost function:

The command  takes the first item off   and returns the rest:. We can then replace  with this returned list:

This leaves us with only one more function to calculate. We know the  function returns the first item of a given list. In this case it takes the first item of, giving:

Adding to lists
Prepend an item to another list, i.e. stick a single element on the front, add a new head to a list:

You might be used to other programming languages treating single and double quotation marks in much the same way, for example in Javascript:

This isn't the case for Haskell. Haskell treats the single quotation mark (') as defining a single element, and the double quotation marks (") as defining a list, even if there is only a single element inside the double speech marks, e.g. . This means that it treats   as a single item of type   and   as a list of type  . If we make sure that we match our datatypes we will be fine.

is the equivalent of

But  is not the equivalent of , as   is a list of lists of type  , whilst   is a list of. We can see this mismatch when trying the following:

And we can see that  is a list of lists of type   by doing:

Prepending the list  to the list   is fine, as the type of the prepended item, a list of , matches the type of the list items it is being prepended to, i.e. lists of type  :

We can also do the same when prepending an item of type,  , to a list of type  ,  :

But attempting to prepend an item of type Char  to a list of type List Char   will not work. The error message here show the difference between the type  and the type list of , defined in the error message by  :

Append (++) an item or a list to another list, i.e. stick something on the end, maybe an item or another list:

The prepend command only allows you to add a single element onto the front of a list. If you want to add a list onto the front of another list you can use the append (++) command:

You can use a combination of append and prepend to combine multiple lists and items