Improve function composition slides
This commit is contained in:
parent
410b9f252f
commit
b79743db4c
22
VL2.tex
22
VL2.tex
@ -376,7 +376,10 @@ Solution:
|
||||
(.) :: (b -> c) -> (a -> b) -> a -> c
|
||||
(.) f g x = f (g x)
|
||||
\end{haskellcode}
|
||||
\pause
|
||||
\end{frame}
|
||||
|
||||
\begin{frame}[fragile]
|
||||
\frametitle{5. Function composition}
|
||||
And now you can chain functions together. Not just two! Look:
|
||||
\begin{haskellcode}
|
||||
f :: String -> Bool
|
||||
@ -385,6 +388,23 @@ f xs = (even . length . (\x -> x ++ "Hello world")) xs
|
||||
f xs = even . length . (\x -> x ++ "Hello world") $ xs
|
||||
\end{haskellcode}
|
||||
% $
|
||||
\pause
|
||||
Another example where currying is actually important:
|
||||
\begin{haskellcode}
|
||||
f :: Int -> Bool
|
||||
f x = even . (+) 3 . (-) 4 $ x
|
||||
|
||||
-- why not this?
|
||||
f x = even . (+) 3 . (-) $ 4 x
|
||||
\end{haskellcode}
|
||||
%$
|
||||
\pause
|
||||
So there a few things that we have to be aware of:
|
||||
\begin{itemize}
|
||||
\item the types have to fit!
|
||||
\item the arity has to fit!
|
||||
\end{itemize}
|
||||
That sounds complicated at first, because it counts for the whole composition chain. But you'll get used to it.
|
||||
\end{frame}
|
||||
|
||||
\section{6. Recursion patterns}
|
||||
|
Loading…
Reference in New Issue
Block a user