© Stefania Loredana Nita and Marius Mihailescu 2019
Stefania Loredana Nita and Marius MihailescuHaskell Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-4507-1_6

6. Lists

Stefania Loredana Nita1  and Marius Mihailescu1
(1)
Bucharest, Romania
 

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

A list is a similar data structure to a tuple, but lists can be used in more scenarios than tuples. Lists are pretty self-explanatory, but you need to know that they are homogenous data structures, which means the elements are of the same type. You represent lists using square brackets, []. Here are some examples:
Prelude> [1, 2, 3]
[1,2,3]
Prelude> ['a', 'x']
"ax"
Prelude> [True, False, False, True]
[True,False,False,True]
Prelude> :t [True, False, False, True]
[True, False, False, True] :: [Bool]
Prelude> let numList = [3, 1, 0.5]
Prelude> :t numList
numList :: Fractional a => [a]
Prelude> ['a', 5]
<interactive>:13:7: error:
    • No instance for (Num Char) arising from the literal '5'
    • In the expression: 5
      In the expression: ['a', 5]
      In an equation for 'it': it = ['a', 5]

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.

Note that [] represents an empty list (containing no elements), as shown here:
Prelude> []
[]
The [], [[]], and [[], []] examples are different. The first is an empty list, the next is a list with one element (an empty list), and the last is a list with two elements (two empty lists). You can check whether a list is null (i.e., an empty list) using the null function.
Prelude> null []
True
Prelude> null [3.2]
False
A list can have two parts, depending the perspective (you will see in a moment why we used can here): the head and the tail. The head is the first element of the list, and the tail is made up of the remaining elements. There are two function for this.
Prelude> head [0,1,2,3,4,5]
0
Prelude> tail [0,1,2,3,4,5,6]
[1,2,3,4,5,6]
Somewhat opposite to head and tail are the last and init functions. The last function returns the last element of the list, while the init function returns the entire list of elements except the last one.
Prelude> last [1,2,3,4,5,6,7]
7
Prelude> init [1,2,3,4,5,6,7]
[1,2,3,4,5,6]
You can add more elements to a list in two ways, as shown here:
Prelude> [1,2,3] ++ [4,5]
[1,2,3,4,5]
Prelude> "Haskell" ++ " " ++ "programming"
"Haskell programming"
Prelude> 0 : [1,2]
[0,1,2]
Prelude> [0] ++ [1,2]
[0,1,2]

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:[].

To determine the length of a list, you use the length function .
Prelude> length [1,2,3,4,5,6,7,8,9,10]
10
Prelude> length []
0
-
If you need an element in a certain position, you use the !! operator (note that the first index of a list is 0).
Prelude> "Haskell programming" !! 10
'o'
Prelude> let listOfLists = [[1,2,3], [0], [-5, 3, 8]]
Prelude> length listOfLists
3
Prelude> listOfLists !! 2
[-5,3,8]
You can compare the elements of two lists. The comparison begins with the first elements, followed by a comparison of the second elements, and so on.
Prelude> [1,2,3] < [4,5]
True
Prelude> [1,2,3] < [4,1,2]
True
If you have many elements in a list, it is difficult to write them all. For example, what if you need all numbers from 1 to 100 or the letters from a to z or all odd numbers? How do you write them? Well, it’s simple in Haskell. Haskell provides you with the following way:
Prelude> [1..20]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
Prelude> ['a'..'z']
"abcdefghijklmnopqrstuvwxyz"
Prelude> [1,3..20]
[1,3,5,7,9,11,13,15,17,19]
Prelude> [14,18..30]
[14,18,22,26,30]

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

The following are other useful functions for lists:
  • 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.

Here are some examples:
Prelude> reverse []
[]
Prelude> reverse [1,2,3]
[3,2,1]
Prelude> take 5 [1,2,3,4,5,6,7]
[1,2,3,4,5]
Prelude> take 5 [1,2,3,4]
[1,2,3,4]
Prelude> drop 5 [1,2,3,4]
[]
Prelude> drop 5 [1,2,3,4,5,6,7]
[6,7]
Prelude> maximum [10, 3, 6, 132, 5]
132
Prelude> minimum [10, 3, 6, 132, 5]
3
Prelude> minimum []
*** Exception: Prelude.minimum: empty list
Prelude> minimum [True, False]
False
Prelude> minimum ['a', 'b']
'a'
Prelude> sum [10, 3, 6, 132, 5]
156
Prelude> sum []
0
Prelude> sum [True, False]
<interactive>:50:1: error:
    • No instance for (Num Bool) arising from a use of 'sum'
    • In the expression: sum [True, False]
      In an equation for 'it': it = sum [True, False]
Prelude> sum ['a', 'b']
<interactive>:51:1: error:
    • No instance for (Num Char) arising from a use of 'sum'
    • In the expression: sum ['a', 'b']
      In an equation for 'it': it = sum ['a', 'b']
Prelude> product [10, 3, 6, 132, 5]
118800
Prelude>  elem 3 [10, 3, 6, 132, 5]
True
Prelude>  elem True [10, 3, 6, 132, 5]
<interactive>:54:13: error:
    • No instance for (Num Bool) arising from the literal '10'
    • In the expression: 10
      In the second argument of 'elem', namely '[10, 3, 6, 132, ....]'
      In the expression: elem True [10, 3, 6, 132, ....]
Prelude>  elem 0 [10, 3, 6, 132, 5]
False
Prelude> splitAt 3 [10, 3, 6, 132, 5]
([10,3,6],[132,5])
Prelude> take 15 (cycle [1,2,3,4])
[1,2,3,4,1,2,3,4,1,2,3,4,1,2,3]
Prelude> take 15 (repeat 2)
[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]

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

In this chapter, you learned the following:
  • 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. 1.

    G. Hutton, Programming in Haskell (Cambridge University Press, 2016)

     
  2. 2.

    D. Coutts, D. Stewart, and R. Leshchinskiy, “Rewriting Haskell Strings.” International Symposium on Practical Aspects of Declarative Languages (Springer, 2007)

     
  3. 3.
     
  4. 4.
     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.80.3