CSc 372 - Comparative Programming Languages
6 : Haskell -- Lists

Christian Collberg

Department of Computer Science

University of Arizona

1 The List Datatype

2 The List Datatype...

3 The List Datatype...

4 The List Datatype...

5 Internal Representation

6 Internal Representation...


Standard Operations on Lists


7 head and tail

8 head and tail...


\begin{gprogram}
head [1,2,3] \xxxxx $\Rightarrow$\ \x 1 \\
tail [1,2,3] \xxxxx...
...row$\ 2 \\
head (tail [[1],[2],[3,3]]) \xxxxx $\Rightarrow$\ [2]
\end{gprogram}

9 length and ++

Examples:
\begin{gprogram}
length [1,2,3] \xxxxxxx $\Rightarrow$\ \x 3 \\
length [\ ] \xx...
...\\
\mbox{[1] ++ [length [2,3]]} \xxxxxxx $\Rightarrow$\ \x [1,2]
\end{gprogram}

10 concat


\begin{gprogram}
concat [[1],[4,5],[6]] \xxxxxxxx $\Rightarrow$\ [1,4,5,6] \\
\end{gprogram}

11 map


\begin{gprogram}
map even [1,2,3] \xxxxxx $\Rightarrow$\ [False,True,False] \\
map square [1,2,3] \xxxxxxxx $\Rightarrow$\ [1,4,9]
\end{gprogram}

12 More list operation examples


\begin{gprogram}
head ([1,2] ++ [3,4]) $\Rightarrow$\ \\
\x head [1,2,3,4] $\Ri...
... [1,3,4]) $\Rightarrow$\ \\
\x tail [2,6,8] $\Rightarrow$\ [6,8]
\end{gprogram}

13 The String Type


\begin{gprogram}
''Chris'' \xxxx $\Leftrightarrow$\ ['C','h','r','i','s'] \\
he...
...'cow, '',''man!''] \\
\x $\Leftrightarrow$\ ''Have a cow, man!''
\end{gprogram}

14 Variable Naming Conventions


Recursion Over Lists


15 Recursion on the Tail


\begin{program}
len :: [Int] -> Int \\
len xs = if xs == [] then \\
\xxx 0\\
\xx else\\
\xxx 1 + len (tail xs)
\end{program}

16 Map Function


\begin{program}
abslist :: [Int] -> [Int]\\
abslist xs = if xs == [] then \\
\xxx []\\
\xx else\\
\xxx abs (head xs) : abslist (tail xs)
\end{program}

17 Map Function...


\begin{program}
\redtt{> (abs-list '(1 -1 2 -3 5))} \\
\redtt{ abslist []} \\
...
...abslist [1]} \\
{[1]} \\
\redtt{ abslist [1,-2]} \\
{[1,2]} \\
\end{program}

18 Recursion Over Two Lists


\begin{program}
listeq :: [Int] -> [Int] -> Bool \\
listeq xs ys = if xs==[] \&...
...en \\
\xxx False \\
\xx else \\
\xxx listeq (tail xs) (tail ys)
\end{program}

19 Recursion Over Two Lists...


\begin{program}
\redtt{> listeq [1] [2]}\\
False\\
\redtt{> listeq [1] [1]}\\ ...
...isteq [1] [1,2]}\\
False \\
\redtt{> listeq [1,2] [1,2]}\\
True
\end{program}

20 Append


\begin{program}
append :: [Int] -> [Int] -> [Int]\\
append xs ys = if xs==[] then\\
\xxx ys\\
\xx else\\
\xxx (head xs) : (append (tail xs) ys)
\end{program}

21 Append...


\begin{program}
\redtt{> append [] []}\\
{[]}\\
\redtt{> append [1] []}\\
{[1...
...\
{[1,2]}\\
\redtt{> append [1,2,3] [4,5,6]} \\
{[1,2,3,4,5,6]}
\end{program}


Arithmetic Sequences


22 Arithmetic Sequences

23 Arithmetic Sequences...

24 Summary

25 Summary...

26 Homework

  1. 1 : []
  2. 1 : [] : []
  3. 1 : [1]
  4. [] : [1]
  5. [1] : [1] : []

27 Homework

  1. [7..11]
  2. [11..7]
  3. [3,6..12]
  4. [12,9..2]

28 Homework

  1. Write a function getelmt xs n which returns the $n$:th element of a list of integers.
  2. Write a function evenelmts xs which returns a new list consisting of the 0:th, 2:nd, 4:th, ... elements of an integer list xs.



Christian S. Collberg
2005-08-31