diff --git a/VL1/content/VL1_ADT1.tex b/VL1/content/VL1_ADT1.tex deleted file mode 100644 index 7059f16..0000000 --- a/VL1/content/VL1_ADT1.tex +++ /dev/null @@ -1,20 +0,0 @@ -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: -\begin{haskellcode} -data WeekDay = Monday - | Tuesday - | Thursday - | Wednesday - | Friday - | Saturday - | Sunday -\end{haskellcode} -This declares the new data type \hinline{WeekDay} with 7 \emph{constructors}. That means \hinline{Monday}, \hinline{Tuesday} etc. are all values of the type \hinline{WeekDay}. -\pause -\\ -We could now define a whole week, by creating a list: -\pause -\begin{haskellcode} -week :: [WeekDay] -week = [Monday, Tuesday, Thursday, Wednesday - , Friday, Saturday, Sunday] -\end{haskellcode} \ No newline at end of file diff --git a/VL1/content/VL1_ADT2.tex b/VL1/content/VL1_ADT2.tex deleted file mode 100644 index 0344a59..0000000 --- a/VL1/content/VL1_ADT2.tex +++ /dev/null @@ -1,7 +0,0 @@ -And we can again \emph{pattern match} on our \hinline{WeekDay} type. Let's find out if a given day is a Monday: -\pause -\begin{haskellcode} -isMonday :: WeekDay -> Bool -isMonday Monday = True -isMonday x = False -\end{haskellcode} \ No newline at end of file diff --git a/VL1/content/VL1_ADT3.tex b/VL1/content/VL1_ADT3.tex deleted file mode 100644 index 47b2c02..0000000 --- a/VL1/content/VL1_ADT3.tex +++ /dev/null @@ -1,14 +0,0 @@ -But we can do more than enumerations. How about we do some error handling? Let's say we want a function to return an \hinline{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: -\pause -\begin{haskellcode} -data MaybeInt = NoError Int - | Error String -\end{haskellcode} -\pause -So constructors are just \emph{functions}! And they can have arguments, just like functions. Let's check their types: -\begin{haskellcode} -> :t NoError -NoError :: Int -> MaybeInt -> :t Error -Error :: String -> MaybeInt -\end{haskellcode} \ No newline at end of file diff --git a/VL1/content/VL1_ADT4.tex b/VL1/content/VL1_ADT4.tex deleted file mode 100644 index 2ca229a..0000000 --- a/VL1/content/VL1_ADT4.tex +++ /dev/null @@ -1,15 +0,0 @@ -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. \ No newline at end of file diff --git a/VL1/content/VL1_ADT5.tex b/VL1/content/VL1_ADT5.tex deleted file mode 100644 index a3f27a6..0000000 --- a/VL1/content/VL1_ADT5.tex +++ /dev/null @@ -1,23 +0,0 @@ -Let's define something more complex. How about a tree? -\pause -\begin{haskellcode} -data Tree = Leaf Char - | Node Tree Int Tree -\end{haskellcode} -Uh... that looks mean. Let's examine this.\\ -\pause -We have: -\begin{itemizep} -\item defined a data type \hinline{Tree} -\item a constructor \hinline{Leaf} of type \hinline{Tree} with one arguments of type \hinline{Char} -\item a constructor \hinline{Node} of type \hinline{Tree} with 3 arguments -\begin{itemizep} -\item \hinline{Tree} -\item \hinline{Int} -\item \hinline{Tree} -\end{itemizep} -\end{itemizep} -\slidep -That means: a \hinline{Tree} can either be a \hinline{Leaf} or an internal \hinline{Node} with two sub-trees. If we want to create a \hinline{Leaf}, we have to pass the constructor a \hinline{Char}. If we want to create a \hinline{Node}, we have to pass 3 arguments, in order: another \hinline{Tree}, an \hinline{Int} and yet another \hinline{Tree}.\\ -So we can save information in the leafs (\hinline{Char}) and in the internal nodes (\hinline{Int}).\\ -This is just an example. There are endless more ways of trees. \ No newline at end of file diff --git a/VL1/content/VL1_ADT6.tex b/VL1/content/VL1_ADT6.tex deleted file mode 100644 index 25c7120..0000000 --- a/VL1/content/VL1_ADT6.tex +++ /dev/null @@ -1,13 +0,0 @@ -Let's build our tree: -\begin{haskellcode} -tree :: Tree -tree = Node - (Leaf 'x') - 1 - (Node - (Leaf 'y') - 2 - (Leaf 'z') - ) -\end{haskellcode} -See board... \ No newline at end of file diff --git a/VL1/content/VL1_ADT7.tex b/VL1/content/VL1_ADT7.tex deleted file mode 100644 index ff5e8d3..0000000 --- a/VL1/content/VL1_ADT7.tex +++ /dev/null @@ -1,7 +0,0 @@ -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.: -\begin{haskellcode} -data AlgDataType = Constr1 Type11 Type12 - | Constr2 Type21 - | Constr3 Type31 Type32 Type33 - | Constr4 -\end{haskellcode} \ No newline at end of file