/
functor.tex
55 lines (50 loc) · 1.35 KB
/
functor.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
\begin{frame}[fragile]
\frametitle{Functor}
\begin{block}{A functor F is given by the function:}
\begin{lstlisting}
(a -> b) -> (F a -> F b)
\end{lstlisting}
\end{block}
\emph{lifts a unary function into an environment F}
\end{frame}
\begin{frame}[fragile]
\frametitle{Functor Haskell}
\begin{block}{Functor using Haskell syntax}
\begin{lstlisting}[language=haskell]
class Functor f where
fmap :: (a -> b) -> (f a -> f b)
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Functor Examples}
\begin{block}{Examples of Functor instances}
\begin{itemize}
\item The list functor maps a function on head of each cons cell:
\begin{lstlisting}
(a -> b) -> ([] a -> [] b)
(a -> b) -> ([a] -> [b])
\end{lstlisting}
\item The maybe functor maps a function on Just:
\begin{lstlisting}
(a -> b) -> (Maybe a -> Maybe b)
\end{lstlisting}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Functor Examples in Haskell}
\begin{block}{Examples of Functor instances using Haskell syntax}
\begin{lstlisting}[language=haskell]
instance Functor [] where
fmap f =
foldr ((:) . f) []
instance Functor Maybe where
fmap f =
maybe Nothing (Just . f)
instance Functor ((->) t) where
fmap f g =
\x -> f (g x)
\end{lstlisting}
\end{block}
\end{frame}