CSc 372 - Comparative Programming Languages
7 : Haskell -- Patterns

Christian Collberg

Department of Computer Science

University of Arizona

1 Pattern Matching

Pattern Syntax:
\begin{gprogram}
function\_name pattern\_1 = expression\_1 \\
function\_name pa...
...2 \\
\xx $\cdots$\ \\
function\_name pattern\_n = expression\_n
\end{gprogram}

2 Pattern Matching...


\begin{gprogram}
fact n = if n == 0 then \\
\xx 1 \\
\x else \\
\xx n * fact (n-1)
\end{gprogram}

fact Revisited:
\begin{gprogram}
fact :: Int -> Int \\
fact 0 = 1 \\
fact n = n * fact (n-1)
\end{gprogram}

3 Pattern Matching...


\begin{gprogram}
isNice ''Jenny'' = ''Definitely'' \\
isNice ''Johanna'' = ''Maybe'' \\
isNice ''Chris'' = ''No Way''
\end{gprogram}

4 Pattern Matching...


\begin{gprogram}
fun (x:xs) = x $\oplus$\ fun xs \\
\xx $\Leftrightarrow$\ \\
fun xs = head xs $\oplus$\ fun (tail xs)
\end{gprogram}

5 Pattern Matching...


\begin{gprogram}
not True = False \\
not False = True
\end{gprogram}

6 Pattern Matching...


\begin{gprogram}
diary ''Monday'' = ''Woke up'' \\
diary ''Sunday'' = ''Slept i...
... in'' \\
diary ''Tuesday'' $\Rightarrow$\ ''Did something else''
\end{gprogram}

7 Pattern Matching - Integer Patterns

Pattern Syntax Example Description
variable var_name fact n = $\cdots$ n matches any argument
constant literal fact 0 = $\cdots$ matches the value
wildcard _ five _ = 5 _ matches any argument
(n+k) pat. (n+k) fact (n+1) = $\cdots$ (n+k) matches any integer $\geq k$

8 Pattern Matching - List Patterns

Pattern Syntax Example Description
cons (x:xs) len (x:xs) = $\cdots$ matches non-empty list
empty [ ] len [ ] = 0 matches the empty list
one-elem [x] len [x] = 1 matches a list with exactly 1 element.
two-elem [x,y] len [x,y] = 2 matches a list with exactly 2 elements.

9 The sumlist Function

Using conditional expr:
\begin{gprogram}
sumlist :: [Int] -> Int \\
sumlist xs = \= if xs == [\ ] then 0 \\
\x else head xs + sumlist(tail xs)
\end{gprogram}

Using patterns:
\begin{gprogram}
sumlist :: [Int] -> Int \\
sumlist [\ ] = 0 \\
sumlist (x:xs) = x + sumlist xs
\end{gprogram}

10 The length Function Revisited

Using conditional expr:
\begin{gprogram}
len :: [Int] -> Int \\
len s = \xxx if s == [\ ] then 0 else 1 + len (tail s)
\end{gprogram}

Using patterns:
\begin{gprogram}
len :: [Int] -> Int \\
len [\ ] = 0 \\
len (\_:xs) = 1 + len xs
\end{gprogram}

11 The fact Function Revisited

Using conditional expr:
\begin{gprogram}
fact n = if n == 0 then 1 else n * fact (n-1)
\end{gprogram}

Using patterns:
\begin{gprogram}
fact' :: Int -> Int \\
fact' 0 = 1 \\
fact' (n+1) = (n+1) * fact' n
\end{gprogram}

12 Summary

13 Homework


\begin{gprogram}
addints :: Int -> Int \\
addints a = $\cdots$\ \\
\\
\redtxt{? addints 5} \\
\x 15 \\
\\
\redtxt{? addints 2} \\
\x 3
\end{gprogram}

14 Homework


\begin{gprogram}
member :: Int -> [Int] -> Bool \\
member x L = $\cdots$\ \\
\...
...[1,2,3]} \\
\x True \\
\redtxt{? member 4 [1,2,3]} \\
\x False
\end{gprogram}

15 Homework


\begin{gprogram}
memberNum :: Int -> [Int] -> Int \\
unique :: [Int] -> Int \\ ...
...[1,5,2,3,5,5]} \\
\x 3\\
\redtxt{? unique [2,4,2,1,4]} \\
\x 1
\end{gprogram}

16 Homework


\begin{displaymath}\begin{array}{lll}
A(0,n) & = & n + 1 \\
A(m,0) & = & A(m - 1, 1) \\
A(m,n) & = & A(m - 1, A(m,n - 1))
\end{array} \end{displaymath}


\begin{gprogram}
ackerman :: Int -> Int -> Int \\
ackerman 0 5 $\Rightarrow$\ 6 \\
ackerman (-1) 5 $\Rightarrow$\ \redtxt{ERROR}
\end{gprogram}



Christian S. Collberg
2007-09-03