haskell-lectures/VL2/content/VL2_rec_patterns6.tex

23 lines
624 B
TeX
Raw Normal View History

Imagine we want to filter all even numbers of a list and throw away all others. I'll give you the type signature:
\begin{haskellcode}
filterEven :: [Int] -> [Int]
\end{haskellcode}
Solution?
\pause
\begin{haskellcode}
filterEven :: [Int] -> [Int]
filterEven [] = []
filterEven (x:xs)
| even x = x : filterEven xs
| otherwise = filterEven xs
\end{haskellcode}
\pause
Or: filter out all 0's, so we can count them later:
\begin{haskellcode}
filterZero :: [Int] -> [Int]
filterZero [] = []
filterZero (x:xs)
| x == 0 = x : filterZero xs
| otherwise = filterZero xs
\end{haskellcode}
Again: do you notice something?