Rm obsolete slides

This commit is contained in:
Julian Ospald 2015-04-21 14:27:26 +02:00
parent 5b4be12e0b
commit d9750e6b34
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
7 changed files with 0 additions and 99 deletions

View File

@ -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}

View File

@ -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}

View File

@ -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}

View File

@ -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.

View File

@ -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.

View File

@ -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...

View File

@ -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}