2015-04-20 18:12:09 +00:00
|
|
|
And now we can do sanity checks:
|
|
|
|
\begin{haskellcode}
|
|
|
|
calcSomething :: Int -> MaybeInt
|
|
|
|
calcSomething x
|
|
|
|
| x < 100 = NoError (x * 5)
|
|
|
|
| otherwise = Error "Int out of range!"
|
|
|
|
\end{haskellcode}
|
|
|
|
\pause
|
2015-04-19 22:32:01 +00:00
|
|
|
And pattern match on it as well:
|
|
|
|
\begin{haskellcode}
|
|
|
|
addIntToList :: MaybeInt -> [Int]
|
|
|
|
addIntToList (NoError x) = [x]
|
|
|
|
addIntToList (Error str) = []
|
|
|
|
\end{haskellcode}
|
2015-04-20 18:55:41 +00:00
|
|
|
So if we got an error, we just return an empty list, otherwise we return a list with the \hinline{Int} as its only element.
|