Browse Source

Add pattern matching on MaybeInt

Julian Ospald 4 years ago
parent
commit
40fb30148f
No account linked to committer's email address
1 changed files with 12 additions and 0 deletions
  1. 12
    0
      VL1.tex

+ 12
- 0
VL1.tex View File

@@ -520,6 +520,18 @@ calcSomething x
520 520
 \end{lstlisting}
521 521
 \end{frame}
522 522
 
523
+\begin{frame}[fragile]
524
+\frametitle{Algebraic Data Types (ctn.)}
525
+And pattern match on it as well:
526
+\setHaskellCodeStyle
527
+\begin{lstlisting}
528
+addIntToList :: MaybeInt -> [Int]
529
+addIntToList (NoError x) = [x]
530
+addIntToList (Error str) = []
531
+\end{lstlisting}
532
+So if we got an error, we just return an empty list, otherwise we return a list with the \code{Int} as its only element.
533
+\end{frame}
534
+
523 535
 \begin{frame}[fragile]
524 536
 \frametitle{Algebraic Data Types (ctn.)}
525 537
 Let's define something more complex. How about a tree?