15 lines
452 B
TeX
15 lines
452 B
TeX
|
Again, we can do pattern matching on lists.
|
||
|
\begin{haskellcode}
|
||
|
listLen :: [Integer] -> Integer
|
||
|
listLen [] = 0
|
||
|
listLen (x:xs) = 1 + listLen xs
|
||
|
\end{haskellcode}
|
||
|
\pause
|
||
|
We can also nest pattern matching:
|
||
|
\begin{haskellcode}
|
||
|
sumEveryTwo :: [Integer] -> [Integer]
|
||
|
sumEveryTwo [] = 0
|
||
|
sumEveryTwo (x:[]) = [x]
|
||
|
sumEveryTwo (x:(y:zs)) = (x + y) : sumEveryTwo zs
|
||
|
\end{haskellcode}
|
||
|
Note that \code{(x:(y:zs))} may also be written as \code{(x:y:zs)}.
|