First draft of talk:

* Dafunc? concetpually almost ready
 * Started haskell introduction
This commit is contained in:
Oliver Rümpelein 2016-04-20 12:26:05 +02:00
parent 37e1cda3e4
commit 7657c1a493
5 changed files with 226 additions and 1 deletions

2
.gitignore vendored
View file

@ -17,7 +17,7 @@
# these rules might exclude image files for figures etc. # these rules might exclude image files for figures etc.
# *.ps # *.ps
# *.eps # *.eps
# *.pdf *.pdf
## Bibliography auxiliary files (bibtex/biblatex/biber): ## Bibliography auxiliary files (bibtex/biblatex/biber):
*.bbl *.bbl

18
examples/00_dafunc.cpp Normal file
View file

@ -0,0 +1,18 @@
#include <iostream>
#include <cassert>
using namespace std;
int f(int x) { return ++x;}
int g(int& x) { return ++x;}
int main() {
int x = 2;
/* f is "functional", as it has no side-effects */
f(x);
assert(x==2);
/* g changes a variable of an outer scope,
* i.e. it has a side effect
* Thus, g is not functional!
*/
g(x);
assert(x!=2);
}

View file

@ -0,0 +1,12 @@
extract :: [a] -> a
extract [] = error("Too few objects")
extract (x:xs) = x
a = extract [1,2,3,4,5]
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

49
tex/headers/listings.tex Normal file
View file

@ -0,0 +1,49 @@
\ProvidesFile{listings}[Configure listings with minted]
% Fix backticks
\usepackage{upquote}
\usepackage[section, cache=true,]{minted}
\newminted[ccode]{c}%
{
linenos=true,
autogobble=true,
breaklines=true,
showspaces=false,
showtabs=false,
tabsize=2,
frame=single,
fontsize=\small,
}
\newminted[cppcode]{cpp}%
{
linenos=true,
autogobble=true,
breaklines=true,
showspaces=false,
showtabs=false,
tabsize=2,
frame=single,
fontsize=\small,
}
\newminted[haskell]{haskell}
{
linenos=true,
autogobble=true,
breaklines=true,
showspaces=false,
showtabs=false,
tabsize=2,
frame=single,
fontsize=\small,
}
\newmintinline[ccmd]{c}{}
\newmintinline[cppcmd]{cpp}{}
\newmintinline[haskellcmd]{haskell}{}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../linux-script"
%%% End:

146
tex/wtfunctional.tex Normal file
View file

@ -0,0 +1,146 @@
\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: