haskell-lectures/VL1/content/VL1_ADT4.tex

15 righe
768 B
TeX

\ifger{Und jetzt können wir Sicherheitsabfragen machen:}{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
\ifger{Und darauf \emph{pattern matchen}}{And pattern match on it as well}:
\begin{haskellcode}
addIntToList :: MaybeInt -> [Int]
addIntToList (NoError x) = [x]
addIntToList (Error str) = []
\end{haskellcode}
\ifger{D.h. wenn wir hier einen Error bekommen, geben wir einfach eine leere Liste zurück. Ansonsten geben wir die Liste mit dem \hinline{Int} als einziges Element zurück.}{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.}