Browse Source

Add algebraic data types section

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

+ 125
- 0
VL1.tex View File

@@ -436,6 +436,131 @@ Now let's say we want all numbers between 50 and 100 that have the remainder 0 w
436 436
 \code{x <- [50..100]} is the binding, while \code{mod x 12 == 0} is the predicate, separated by a comma. We can have multiple predicates.
437 437
 \end{frame}
438 438
 
439
+\begin{frame}[fragile]
440
+\frametitle{Algebraic Data Types}
441
+Of course we can also define our own data types in haskell. One very common type is the \emph{enumeration}. For example, we could define a data type for the Week:
442
+\setHaskellCodeStyle
443
+\begin{lstlisting}
444
+data WeekDay = Monday
445
+             | Tuesday
446
+             | Thursday
447
+             | Wednsday
448
+             | Friday
449
+             | Saturday
450
+             | Sunday
451
+\end{lstlisting}
452
+This declares the new data type \code{WeekDay} with 7 \emph{constructors}. That means \code{Monday}, \code{Tuesday} etc. are all values of the type \code{WeekDay}.
453
+\pause
454
+\\
455
+We could now define a whole week, by creating a list:
456
+\pause
457
+\setHaskellCodeStyle
458
+\begin{lstlisting}
459
+week :: [WeekDay]
460
+week = [Monday, Tuesday, Thursday, Wednsday
461
+       , Friday, Saturday, Sunday]
462
+\end{lstlisting}
463
+\end{frame}
464
+
465
+\begin{frame}[fragile]
466
+\frametitle{Algebraic Data Types (ctn.)}
467
+And we can again \emph{pattern match} on our \code{WeekDay} type. Let's find out if a given day is a monday:
468
+\pause
469
+\setHaskellCodeStyle
470
+\begin{lstlisting}
471
+isMonday :: WeekDay -> Bool
472
+isMonday Monday = True
473
+isMonday x      = False
474
+\end{lstlisting}
475
+\end{frame}
476
+
477
+\begin{frame}[fragile]
478
+\frametitle{Algebraic Data Types (ctn.)}
479
+But we can do more than enumerations. How about we do some error handling? Let's say we want a function to return an \code{Int}, but in case something went horribly wrong, we don't just want to return a 0 or some magic number, but a proper error message. Here we go:
480
+\pause
481
+\setHaskellCodeStyle
482
+\begin{lstlisting}
483
+data MaybeInt = NoError Int
484
+              | Error String
485
+\end{lstlisting}
486
+\pause
487
+And now we can do sanity checks:
488
+\pause
489
+\setHaskellCodeStyle
490
+\begin{lstlisting}
491
+calcSomething :: Int -> MaybeInt
492
+calcSomething x
493
+  | x < 100   = NoError (x * 5)
494
+  | otherwise = Error "Int out of range!"
495
+\end{lstlisting}
496
+\pause
497
+So constructors are just \emph{functions}! And they can have arguments, just like functions. Let's check their types:
498
+\setHaskellCodeStyle
499
+\begin{lstlisting}
500
+> :t NoError
501
+NoError :: Int -> MaybeInt
502
+> :t Error
503
+Error :: String -> MaybeInt
504
+\end{lstlisting}
505
+\end{frame}
506
+
507
+\begin{frame}[fragile]
508
+\frametitle{Algebraic Data Types (ctn.)}
509
+Let's define something more complex. How about a tree?
510
+\pause
511
+\setHaskellCodeStyle
512
+\begin{lstlisting}
513
+data Tree = Leaf Char
514
+          | Node Tree Int Tree
515
+\end{lstlisting}
516
+Uh... that looks mean. Let's examine this.\\
517
+\pause
518
+We have:
519
+\begin{itemize}[<+->]
520
+\item defined a data type \code{Tree}
521
+\item a constructor \code{Leaf} of type \code{Tree} with one arguments of type \code{Char}
522
+\item a constructor \code{Node} of type \code{Tree} with 3 arguments
523
+\begin{itemize}[<+->]
524
+\item \code{Tree}
525
+\item \code{Int}
526
+\item \code{Tree}
527
+\end{itemize}
528
+\end{itemize}
529
+\onslide<+->
530
+That means: a \code{Tree} can either be a \code{Leaf} or an internal \code{Node} with two sub-trees. If we want to create a \code{Leaf}, we have to pass the constructor a \code{Char}. If we want to create a \code{Node}, we have to pass 3 arguments, in order: another \code{Tree}, an \code{Int} and yet another \code{Tree}.\\
531
+So we can save information in the leafs (\code{Char}) and in the internal nodes (\code{Int}).\\
532
+This is just an example. There are endless more ways of trees.
533
+\end{frame}
534
+
535
+\begin{frame}[fragile]
536
+\frametitle{Algebraic Data Types (ctn.)}
537
+Let's build our tree:
538
+\setHaskellCodeStyle
539
+\begin{lstlisting}
540
+tree :: Tree
541
+tree = Node
542
+       (Leaf 'x')
543
+       1
544
+       (Node
545
+            (Leaf 'y')
546
+            2
547
+            (Leaf 'z')
548
+       )
549
+\end{lstlisting}
550
+See board...
551
+\end{frame}
552
+
553
+\begin{frame}[fragile]
554
+\frametitle{Algebraic Data Types (ctn.)}
555
+So if we want to generalize it, an algebraic data type has one or more \textbf{constructors}, and each of them can have zero or more arguments. E.g.:
556
+\setHaskellCodeStyle
557
+\begin{lstlisting}
558
+data AlgDataType = Constr1 Type11 Type12
559
+                 | Constr2 Type21
560
+                 | Constr3 Type31 Type32 Type33
561
+                 | Constr4
562
+\end{lstlisting}
563
+\end{frame}
439 564
 \begin{frame}
440 565
 \frametitle{Toolchain}
441 566
 You need: