haskell-lectures/VL1/content/VL1_ADT4.tex

15 lines
490 B
TeX

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
And pattern match on it as well:
\begin{haskellcode}
addIntToList :: MaybeInt -> [Int]
addIntToList (NoError x) = [x]
addIntToList (Error str) = []
\end{haskellcode}
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.