146 lines
4 KiB
TeX
146 lines
4 KiB
TeX
|
\documentclass[english]{beamer}
|
|||
|
|
|||
|
\usepackage{babel}
|
|||
|
\usepackage{csquotes}
|
|||
|
\usepackage{tabularx}
|
|||
|
|
|||
|
\usepackage{fontspec}
|
|||
|
\setsansfont{Fira Sans}
|
|||
|
\setmonofont{Inconsolata-g}
|
|||
|
|
|||
|
\usetheme{Antibes}
|
|||
|
%\usecolortheme{beaver}
|
|||
|
\setbeamercovered{transparent}
|
|||
|
|
|||
|
\title{WTFunctional}
|
|||
|
\author{Oliver Rümpelein}
|
|||
|
\subtitle{Using functional structures in non-functional languages}
|
|||
|
|
|||
|
\input{headers/listings}
|
|||
|
|
|||
|
\begin{document}
|
|||
|
\frame{\titlepage}
|
|||
|
|
|||
|
\section{Dafunc?}
|
|||
|
\subsection{Functional programming}
|
|||
|
\begin{frame}{Understanding functional paradigms}
|
|||
|
Here: so called \enquote{purely functional} paradigm.
|
|||
|
\begin{itemize}
|
|||
|
\item<+-> Programming without \enquote{side-effects}
|
|||
|
\begin{itemize}
|
|||
|
\item<+-> No mutability
|
|||
|
\item<+-> Functions work only in local context
|
|||
|
\end{itemize}
|
|||
|
\item<+-> Extensive use of lists and so called maps/reduces (later)
|
|||
|
\item<+-> Do not mix up with \enquote{procedural} programming (using only functions)!
|
|||
|
\end{itemize}
|
|||
|
\end{frame}
|
|||
|
|
|||
|
\begin{frame}[fragile]{Example}
|
|||
|
%ToDo: C-code call by value, call by reference.
|
|||
|
\begin{cppcode}
|
|||
|
int f(int x) { return ++x;}
|
|||
|
int g(int& x) { return ++x;}
|
|||
|
int main() {
|
|||
|
int x = 2;
|
|||
|
f(x);
|
|||
|
assert(x==2); // f is “functional”
|
|||
|
g(x);
|
|||
|
assert(x!=2); // g is not!
|
|||
|
}
|
|||
|
\end{cppcode}
|
|||
|
\end{frame}
|
|||
|
|
|||
|
\begin{frame}{Pros and Cons}
|
|||
|
Pros:
|
|||
|
\begin{itemize}[<+->]
|
|||
|
\item Maintainability
|
|||
|
\item Testing
|
|||
|
\item (often) shorter code
|
|||
|
\end{itemize}
|
|||
|
Cons:
|
|||
|
\begin{itemize}[<+->]
|
|||
|
\item harder to learn
|
|||
|
\item harder to understand
|
|||
|
\item slower due to abstraction
|
|||
|
\end{itemize}
|
|||
|
\end{frame}
|
|||
|
|
|||
|
\subsection{Case study: Haskell}
|
|||
|
\begin{frame}{Overview}
|
|||
|
\begin{itemize}[<+->]
|
|||
|
\item \emph{Haskell} is a purely functional, compiled programming language
|
|||
|
developed since 1990.
|
|||
|
\item It is typed and has a strong meta-type system (comparable to
|
|||
|
interfaces in OOP)
|
|||
|
\item The most important implementation is \emph{GHC} (Glasgow Haskell
|
|||
|
Compiler)
|
|||
|
\item Haskell is lazy. Statements get evaluated only when needed, if ever.
|
|||
|
\end{itemize}
|
|||
|
\end{frame}
|
|||
|
|
|||
|
\begin{frame}[fragile]{Syntax – Functions}
|
|||
|
Function constraints, definition and calls:
|
|||
|
\begin{haskell}
|
|||
|
mysum :: Num a => a -> a -> a -> a
|
|||
|
mysum x y z = x + y + z
|
|||
|
-- b == 6
|
|||
|
b = mysum 1 2 3
|
|||
|
\end{haskell}
|
|||
|
\pause
|
|||
|
Functions always get evaluated left to right, thus the following works (\emph{Currying}):
|
|||
|
\begin{haskell}
|
|||
|
mysum2 = mysum 2
|
|||
|
-- c == 12
|
|||
|
c = mysum2 4 6
|
|||
|
\end{haskell}
|
|||
|
\end{frame}
|
|||
|
|
|||
|
\begin{frame}[fragile]{Syntax – Lists (1)}
|
|||
|
Lists in Haskell can only hold data of one type. They are defined using
|
|||
|
\haskellcmd{a = [1,2,3,4]} or similar.
|
|||
|
|
|||
|
\pause
|
|||
|
Furthermore, an automatic range can be obtained by using
|
|||
|
\haskellcmd{b = [1..4]}, where the last number is inclusive.
|
|||
|
|
|||
|
\pause
|
|||
|
If possible, Haskell will try to inhibit the step automatically
|
|||
|
\haskellcmd{c = [1,3..7]} yields \haskellcmd{[1,3,5,7]}.
|
|||
|
|
|||
|
\pause
|
|||
|
When leaving out the end specifier, a range can be infinite. In this case,
|
|||
|
it's up to the programmer to constrain things.
|
|||
|
\end{frame}
|
|||
|
|
|||
|
\begin{frame}[fragile]{Syntax – Lists (2)}
|
|||
|
Two arrays can be concatenated using the \haskellcmd{++} operator
|
|||
|
\haskellcmd{[1,2,3] ++ [4..7]}
|
|||
|
|
|||
|
Single objects get pushed to the front using \haskellcmd{:}:
|
|||
|
\haskellcmd{1:[2..7]}.
|
|||
|
|
|||
|
This can also be used vice versa to extract single values from lists
|
|||
|
\begin{haskell}
|
|||
|
extract (x:xs) = x
|
|||
|
-- a = 1
|
|||
|
a = extract [1..5]
|
|||
|
\end{haskell}
|
|||
|
\end{frame}
|
|||
|
|
|||
|
\begin{frame}[fragile]{Syntax – Recursion}
|
|||
|
Example: Add a value to every entry in an array
|
|||
|
\begin{haskell}
|
|||
|
addto :: (Num a) => [a] -> a -> [a]
|
|||
|
addto [] _ = [] -- edge case (list empty)
|
|||
|
addto (x:xs) y = (x+y) : addto xs y
|
|||
|
b = [1..4]
|
|||
|
-- c == [5,6,7,8]
|
|||
|
c = addto b 4
|
|||
|
\end{haskell}
|
|||
|
\end{frame}
|
|||
|
\end{document}
|
|||
|
|
|||
|
%%% Local Variables:
|
|||
|
%%% mode: latex
|
|||
|
%%% End:
|