Fix polymorphism since the ADT section of VL1 is gone
This commit is contained in:
parent
d9750e6b34
commit
4f67c48b6f
@ -2,19 +2,4 @@ So when we said that haskell is good for abstraction, what did we actually mean?
|
||||
\vspace{\baselineskip}
|
||||
\\
|
||||
\pause
|
||||
Even better! Haskell supports polymorphism for both data types and functions.\\
|
||||
Let's start with a polymorphic data type:
|
||||
\begin{haskellcode}
|
||||
data List t = Empty | Cons t (List t)
|
||||
\end{haskellcode}
|
||||
So we just implemented our own singly-linked List. For any type! Let's use it:
|
||||
\pause
|
||||
\begin{haskellcode}
|
||||
intList :: List Int
|
||||
intList = Cons 3 (Cons 5 (Cons 2 Empty))
|
||||
|
||||
charList :: List Char
|
||||
charList = Cons 'x' (Cons 'y' (Cons 'z' Empty))
|
||||
|
||||
-- whatever you can imagine goes here
|
||||
\end{haskellcode}
|
||||
Even better! Haskell supports polymorphism for both data types and functions.
|
@ -1,20 +1,19 @@
|
||||
And now we are going to write functions to use it:
|
||||
\begin{haskellcode}
|
||||
isListEmpty :: List t -> Bool
|
||||
isListEmpty Emtpy = True
|
||||
isListEmpty x = False
|
||||
\end{haskellcode}
|
||||
\pause
|
||||
We can even have more generic stuff like:
|
||||
\begin{haskellcode}
|
||||
f :: a -> b
|
||||
\end{haskellcode}
|
||||
Whatever the function does... it has something of one type and returns something of another type (it could be the same type, but doesn't need to). That's all we know.
|
||||
\vspace{\baselineskip}
|
||||
\\
|
||||
\pause
|
||||
Similarly, remember the function \hinline{head} which gives us the first element of a list? The type signature actually looks like this:
|
||||
Let's start with a polymorphic function that you already know:
|
||||
\begin{haskellcode}
|
||||
head :: [a] -> a
|
||||
\end{haskellcode}
|
||||
Makes sense?
|
||||
\pause
|
||||
So \hinline{head} takes a list of any type and returns an element which must have the \emph{exact same} type of that list.
|
||||
\vspace{\baselineskip}
|
||||
\\
|
||||
\pause
|
||||
We can also have:
|
||||
\begin{haskellcode}
|
||||
f :: a -> b
|
||||
\end{haskellcode}
|
||||
\pause
|
||||
So, whatever the function does... it has something of one type and returns something of another type. \hinline{b} \emph{could} be the same type as \hinline{a} here, but it doesn't need to! That's all we know about this function.
|
||||
\vspace{\baselineskip}
|
||||
\\
|
||||
\pause
|
||||
You don't have to use \hinline{a} or \hinline{b} here. These letters are just commonly used for generic types.
|
Loading…
Reference in New Issue
Block a user