23 lines
624 B
TeX
23 lines
624 B
TeX
|
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?
|