functional-programming

Enter the vast world of programming languages by following the tutorials provided here to learn about functional programming in Haskell.


Project maintained by jd-anabi Hosted on GitHub Pages — Theme by mattgraham

Back to main

Now that we know how functions work in Haskell, let’s try some applications with them using lists.

Working with lists

Before we make functions with lists, we should learn how they work in Haskell.

Constructing Lists

A list can be constructed in the following manner:

[]

is the empty list in Haskell.

x:xs

is a list with x as its first element, which continues with xs. An example would be:
[1,2,3] = 1:[2,3] = 1:(2:[3]) = 1:(2:(3:[]))
If you look closely, you can see how recursion is inherent to the structure of a list. In more mathematical notation:
Xx0,xn = A(x0, Xx1,xn),
where Xx0,xn is a list X starting with element x0 and running up to element xn, A(yp, Y) is a function that appends the element yp to the front of list Y.

Deconstructing Lists

A list can be deconstructured in the following manner:

head(x:xs) = x
tail(x:xs) = xs

All of this should give us a solid basis for working with lists.

Examples

Now that we know how to make functions and lists, we can do some pretty neat things.

Length

We can compute the length of a list by doing the following:

len :: [a] -> Int
len [] = 0
len x:xs = 1 + (len xs)

For example, suppose we have the list [1,2,3]. Then,
len [1,2,3]
= 1 + (len [2,3])
= 1 + 2 + (len [3])
= 1 + 1 + 1 + (len [])
= 1 + 1 + 1 + 0
= 3

End Append

We can append an element y to the end of a list x:xs by doing the following:

end_append :: [a] -> Int -> List
end_append [] y = y:[]
end_append (x:xs) y = x : (end_append xs y)

For example, let’s take the list [1,2,3] and append 4 to the end of it:
end_append [1,2,3] 4
= 1:(end_append [2,3] 4)
= 1:(2:(end_append [3] 4))
= 1:(2:(3:(end_append [] 4)))
= 1:(2:(3:(4:[])))
= [1,2,3,4]

These are just a couple of examples that we could have down. Check the Haskell Exercises section for more examples. When you are ready, we can begin learning guards.