Rm obsolete slides
This commit is contained in:
parent
5b4be12e0b
commit
d9750e6b34
@ -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}
|
|
@ -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}
|
|
@ -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}
|
|
@ -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.
|
|
@ -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.
|
|
@ -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...
|
|
@ -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}
|
|
Loading…
Reference in New Issue
Block a user