In this chapter, you’ll learn about lists and why are they so useful. You will learn what a list is, which basic functions there are for lists, which operations are faster, and in which context you might use lists.
Basic Functions on Lists
The first example is intuitive. In the next example, observe that Haskell has represented the list ['a', 'x'] as String "ax" because, as you learned, a String is actually a list of characters. You can find out the type of elements using the :t command, and you can give a name to your list using the let keyword. In the previous example, observe that you cannot create a list with elements of different types. Also, note that all the numbers in numList are represented as Fractional.
You can use : or ++ operators. What is the difference? You use : when you want to add a new head to the list, so on the left side of : is an element and on its right side is a list. You use ++ when you want to concatenate two lists, so on both sides of the ++ operator you write lists. This fact is emphasized in the previous example, where, to use ++, we wrote [0], namely, a list with just one element. The list [5, 6, 7] is actually the condensed representation of the operations 5:6:7:[].
Note that if you want to define a rule, you need to provide the first two elements of the list.
You can even work with infinite lists, for example multiples of 5: [5,10..]. Be careful when you work with infinite lists.
Other Functions
reverse reverses the list.
take takes from the list a certain number of elements (resulting in another list, but without doing any changes in the elements because in Haskell expressions are immutable), beginning with the first element.
drop deletes from the list a certain number of elements, beginning with the first element.
maximum extracts the maximum element of the list.
minimum extracts the minimum element of the list.
sum sums the elements of the list.
product computes the product of elements in the list.
elem checks whether an item is an element of the list.
splitAt splits a list into two lists at a certain position.
cycle creates an infinite list by replicating a given list.
repeat creates an infinite list by repeating a given element.
The operations that are always fast are the ones appending an element (i.e., the : operator), head function, or tail function. The functions that imply working with the nth element of a list work pretty well, too, but they become slower as n becomes larger. Intuitively, the slowest functions are those that process an entire list, and they become even slower when the length of the list increases.
Summary
What lists are
What the basic functions for a list are
That you can represent a list in different ways
That you can work with infinite lists
Which operations are faster and which are slower
References
- 1.
G. Hutton, Programming in Haskell (Cambridge University Press, 2016)
- 2.
D. Coutts, D. Stewart, and R. Leshchinskiy, “Rewriting Haskell Strings.” International Symposium on Practical Aspects of Declarative Languages (Springer, 2007)
- 3.
How to work on lists, https://wiki.haskell.org/How_to_work_on_lists
- 4.