Use minted package for code

This commit is contained in:
Julian Ospald 2015-04-17 15:54:32 +02:00
parent 769c10eb70
commit 70256a9165
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
1 changed files with 66 additions and 113 deletions

179
VL1.tex
View File

@ -8,6 +8,7 @@
\usepackage{amssymb} \usepackage{amssymb}
\usepackage{graphicx} \usepackage{graphicx}
\usepackage{listings} \usepackage{listings}
\usepackage{minted}
% for \verb inside \item % for \verb inside \item
\usepackage[T1]{fontenc} \usepackage[T1]{fontenc}
@ -17,6 +18,13 @@
\DeclareGraphicsExtensions{.pdf,.png,.jpg} \DeclareGraphicsExtensions{.pdf,.png,.jpg}
\beamertemplatenavigationsymbolsempty \beamertemplatenavigationsymbolsempty
\setbeamertemplate{footline}[frame number] \setbeamertemplate{footline}[frame number]
\usemintedstyle{friendly}
\newminted{haskell}{frame=single,numbers=left}
\newminted{cpp}{frame=single,numbers=left}
\newminted{c}{frame=single,numbers=left}
\renewcommand{\theFancyVerbLine}{\ttfamily
\textcolor[rgb]{0.0,0.0,0.0}{\footnotesize
\oldstylenums{\arabic{FancyVerbLine}}}}
% title page information % title page information
\author{Julian Ospald} \author{Julian Ospald}
@ -30,36 +38,6 @@
% macros and environments % macros and environments
\newcommand{\code}[1]{\texttt{#1}} \newcommand{\code}[1]{\texttt{#1}}
\newcommand{\setHaskellCodeStyle}{
\lstset{
language=Haskell,
backgroundcolor=\color{lightgray},
commentstyle=\color{mygreen},
keywordstyle=\color{blue},
frame=single,
keepspaces=true
}
}
\newcommand{\setCCodeStyle}{
\lstset{
language=C,
backgroundcolor=\color{lightgray},
commentstyle=\color{mygreen},
keywordstyle=\color{blue},
frame=single,
keepspaces=true
}
}
\newcommand{\setCppCodeStyle}{
\lstset{
language=C++,
backgroundcolor=\color{lightgray},
commentstyle=\color{mygreen},
keywordstyle=\color{blue},
frame=single,
keepspaces=true
}
}
\begin{document} \begin{document}
@ -94,19 +72,17 @@ We'll have to see if this holds true.
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{1. Why haskell? (ctn.)} \frametitle{1. Why haskell? (ctn.)}
From C++ std: From C++ std:
\setCppCodeStyle \begin{cppcode}
\begin{lstlisting}
void pop(); void pop();
\end{lstlisting} \end{cppcode}
\setCCodeStyle
\onslide<+-> \onslide<+->
From the C FLINT library: From the C FLINT library:
\begin{lstlisting} \begin{ccode}
void fmpz_mod_poly_add( void fmpz_mod_poly_add(
fmpz_mod_poly_t res, fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly1, const fmpz_mod_poly_t poly1,
const fmpz_mod_poly_t poly2); const fmpz_mod_poly_t poly2);
\end{lstlisting} \end{ccode}
\vspace{\baselineskip} \vspace{\baselineskip}
\onslide<+-> \onslide<+->
Regular C functions in real-world (omitting examples on purpose): Regular C functions in real-world (omitting examples on purpose):
@ -289,8 +265,7 @@ Let's reiterate. Haskell is:
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{4. Declarations} \frametitle{4. Declarations}
Let's go! Let's go!
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
x :: Int x :: Int
x = 3 x = 3
@ -309,48 +284,46 @@ c = 'k'
s :: String s :: String
s = "Hello, world?" s = "Hello, world?"
\end{lstlisting} \end{haskellcode}
\end{frame} \end{frame}
\section{5. Arithmetic and co.} \section{5. Arithmetic and co.}
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{5. Arithmetic and co.} \frametitle{5. Arithmetic and co.}
\setHaskellCodeStyle
GHCi: GHCi:
\begin{lstlisting} \begin{haskellcode}
> 3 + 5 > 3 + 5
> (3 :: Integer) + (5 :: Int) > (3 :: Integer) + (5 :: Int)
> 6 * 5.0 > 6 * 5.0
> "Hello" ++ " world" > "Hello" ++ " world"
> "Haskell" > "C++" > "Haskell" > "C++"
> True && False > True && False
\end{lstlisting} \end{haskellcode}
\end{frame} \end{frame}
\section{6. Functions and control structures} \section{6. Functions and control structures}
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{6. Functions and control structures} \frametitle{6. Functions and control structures}
\setHaskellCodeStyle
Let's make our first function. We want something like the following mathematical function\\ Let's make our first function. We want something like the following mathematical function\\
$f(x) = x * x$\\ $f(x) = x * x$\\
\vspace{\baselineskip} \vspace{\baselineskip}
How could the haskell code look like? How could the haskell code look like?
\pause \pause
Almost the same: Almost the same:
\begin{lstlisting} \begin{haskellcode}
f x = x * x f x = x * x
\end{lstlisting} \end{haskellcode}
\vspace{\baselineskip} \vspace{\baselineskip}
GHCi...\\ GHCi...\\
\pause \pause
\vspace{\baselineskip} \vspace{\baselineskip}
What is a possible type signature for this function? What is a possible type signature for this function?
\begin{lstlisting} \begin{haskellcode}
f :: Int -> Int f :: Int -> Int
f x = x * x f x = x * x
\end{lstlisting} \end{haskellcode}
So the function gets an Int and returns an Int. Don't get confused by "\verb|->|". Just think of it as a symbol for separating input and output. So the function gets an Int and returns an Int. Don't get confused by "\verb|->|". Just think of it as a symbol for separating input and output.
\end{frame} \end{frame}
@ -358,13 +331,12 @@ So the function gets an Int and returns an Int. Don't get confused by "\verb|->|
\frametitle{6. Functions and control structures (ctn.)} \frametitle{6. Functions and control structures (ctn.)}
In haskell we often use \textbf{pattern matching}. That means we define a function multiple times, but e.g. for different values of its input arguments. Let's see: In haskell we often use \textbf{pattern matching}. That means we define a function multiple times, but e.g. for different values of its input arguments. Let's see:
\pause \pause
\setHaskellCodeStyle
\vspace{\baselineskip} \vspace{\baselineskip}
\begin{lstlisting} \begin{haskellcode}
isZero :: Int -> Bool isZero :: Int -> Bool
isZero 0 = True isZero 0 = True
isZero x = False isZero x = False
\end{lstlisting} \end{haskellcode}
\vspace{\baselineskip} \vspace{\baselineskip}
So if we pass it 0, we get True. If we do not pass 0, we get False and the value we passed gets basically ignored.\\ So if we pass it 0, we get True. If we do not pass 0, we get False and the value we passed gets basically ignored.\\
\vspace{\baselineskip} \vspace{\baselineskip}
@ -375,15 +347,14 @@ What might happen if we remove the second or the third line? What is a \textbf{p
\frametitle{6. Functions and control structures (ctn.)} \frametitle{6. Functions and control structures (ctn.)}
How about \emph{recursion}? Let's define the modulo of any \emph{Int} to \emph{2}. How about \emph{recursion}? Let's define the modulo of any \emph{Int} to \emph{2}.
\pause \pause
\setHaskellCodeStyle
\vspace{\baselineskip} \vspace{\baselineskip}
\begin{lstlisting} \begin{haskellcode}
mod2 :: Int -> Int mod2 :: Int -> Int
mod2 x mod2 x
| x - 2 == 0 = 0 | x - 2 == 0 = 0
| x - 2 < 0 = x | x - 2 < 0 = x
| otherwise = mod2 (x - 2) | otherwise = mod2 (x - 2)
\end{lstlisting} \end{haskellcode}
These \verb#|# things above are called \textbf{guards} and are similar to \emph{pattern matching}. They are processed in order. If the condition on the left side of the equation is true, then it returns what stands on the right side of the equation. If it's false, then it processes the next line.\\ \code{otherwise} on the last line is just defined as \code{True}, to make these constructs easier to read and catch all other cases of input. These \verb#|# things above are called \textbf{guards} and are similar to \emph{pattern matching}. They are processed in order. If the condition on the left side of the equation is true, then it returns what stands on the right side of the equation. If it's false, then it processes the next line.\\ \code{otherwise} on the last line is just defined as \code{True}, to make these constructs easier to read and catch all other cases of input.
\end{frame} \end{frame}
@ -392,15 +363,14 @@ These \verb#|# things above are called \textbf{guards} and are similar to \emph{
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{7. Pairs/Tuples} \frametitle{7. Pairs/Tuples}
Defining a pair is easy. Defining a pair is easy.
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
p :: (Int, Char) -- this is the type p :: (Int, Char) -- this is the type
p = (2, 'y') -- this is how we construct the pair p = (2, 'y') -- this is how we construct the pair
-- pattern matching against pairs -- pattern matching against pairs
sumPair :: (Int, Int) -> Int sumPair :: (Int, Int) -> Int
sumPair (x, y) = x + y sumPair (x, y) = x + y
\end{lstlisting} \end{haskellcode}
\pause \pause
Note: we use \code{(x, y)} notation for both the type and the definition! Those are still two different things. We can also have triples, quadruples etc. Note: we use \code{(x, y)} notation for both the type and the definition! Those are still two different things. We can also have triples, quadruples etc.
\end{frame} \end{frame}
@ -409,36 +379,34 @@ Note: we use \code{(x, y)} notation for both the type and the definition! Those
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{8. Lists} \frametitle{8. Lists}
\setHaskellCodeStyle
The list is probably the most basic data structure in Haskell. Like the array in C. It is a singly-linked list and is very lazy. The compiler has numerous ways to optimize lists, so don't be afraid to use them, even for huge things. The list is probably the most basic data structure in Haskell. Like the array in C. It is a singly-linked list and is very lazy. The compiler has numerous ways to optimize lists, so don't be afraid to use them, even for huge things.
\pause \pause
We build lists by using either the \code{[]} notation: We build lists by using either the \code{[]} notation:
\begin{lstlisting} \begin{haskellcode}
list1 :: [Integer] list1 :: [Integer]
list1 = [1, 2] list1 = [1, 2]
\end{lstlisting} \end{haskellcode}
\pause \pause
or by using the \emph{cons} operator \code{(:)} which takes an element and a list and produces a new list with the element prepended to the front. or by using the \emph{cons} operator \code{(:)} which takes an element and a list and produces a new list with the element prepended to the front.
\begin{lstlisting} \begin{haskellcode}
emptyList = [] emptyList = []
list2 = 1 : [] list2 = 1 : []
-- is this really a list? -- is this really a list?
list3 = [1, 2] == 1 : 2 : [] list3 = [1, 2] == 1 : 2 : []
\end{lstlisting} \end{haskellcode}
\pause \pause
How about something more interesting: How about something more interesting:
\begin{lstlisting} \begin{haskellcode}
infiniteList = [1..] infiniteList = [1..]
\end{lstlisting} \end{haskellcode}
\end{frame} \end{frame}
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{8. Lists (ctn.)} \frametitle{8. Lists (ctn.)}
Let's check on a few very common list operations: Let's check on a few very common list operations:
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
> [1, 2] ++ [4, 5] -- append two lists > [1, 2] ++ [4, 5] -- append two lists
> head [1, 2, 3] -- first element > head [1, 2, 3] -- first element
> tail [1, 2, 3] -- everything after the head > tail [1, 2, 3] -- everything after the head
@ -447,35 +415,33 @@ Let's check on a few very common list operations:
> drop 2 [1, 2, 3] -- drop the first two elements > drop 2 [1, 2, 3] -- drop the first two elements
> sum [1, 2, 3] > sum [1, 2, 3]
> elem 7 [1, 2, 3] -- is there a 7 in the list? > elem 7 [1, 2, 3] -- is there a 7 in the list?
\end{lstlisting} \end{haskellcode}
\pause \pause
A String in haskell is just a list of Chars! A String in haskell is just a list of Chars!
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
> ['a', 'b', 'c'] > ['a', 'b', 'c']
> 'a' : [] > 'a' : []
> head "abc" > head "abc"
> 'a' ++ 'c' > 'a' ++ 'c'
\end{lstlisting} \end{haskellcode}
\end{frame} \end{frame}
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{8. Lists (ctn.)} \frametitle{8. Lists (ctn.)}
Again, we can do pattern matching on lists. Again, we can do pattern matching on lists.
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
listLen :: [Integer] -> Integer listLen :: [Integer] -> Integer
listLen [] = 0 listLen [] = 0
listLen (x:xs) = 1 + listLen xs listLen (x:xs) = 1 + listLen xs
\end{lstlisting} \end{haskellcode}
\pause \pause
We can also nest pattern matching: We can also nest pattern matching:
\begin{lstlisting} \begin{haskellcode}
sumEveryTwo :: [Integer] -> [Integer] sumEveryTwo :: [Integer] -> [Integer]
sumEveryTwo [] = 0 sumEveryTwo [] = 0
sumEveryTwo (x:[]) = [x] sumEveryTwo (x:[]) = [x]
sumEveryTwo (x:(y:zs)) = (x + y) : sumEveryTwo zs sumEveryTwo (x:(y:zs)) = (x + y) : sumEveryTwo zs
\end{lstlisting} \end{haskellcode}
Note that \code{(x:(y:zs))} may also be written as \code{(x:y:zs)}. Note that \code{(x:(y:zs))} may also be written as \code{(x:y:zs)}.
\end{frame} \end{frame}
@ -489,17 +455,15 @@ $S = \{2 \times x\ |\ x \in \mathbb{N},\ x \leq 10\}$\\
\pause \pause
How does this look in haskell? How does this look in haskell?
\pause \pause
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
> [x*2 | x <- [1..10]] > [x*2 | x <- [1..10]]
\end{lstlisting} \end{haskellcode}
\pause \pause
Now let's say we want all numbers between 50 and 100 that have the remainder 0 when divided by 12: Now let's say we want all numbers between 50 and 100 that have the remainder 0 when divided by 12:
\pause \pause
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
> [x | x <- [50..100], mod x 12 == 0] > [x | x <- [50..100], mod x 12 == 0]
\end{lstlisting} \end{haskellcode}
\code{x <- [50..100]} is the binding, while \code{mod x 12 == 0} is the predicate, separated by a comma. We can have multiple predicates. \code{x <- [50..100]} is the binding, while \code{mod x 12 == 0} is the predicate, separated by a comma. We can have multiple predicates.
\end{frame} \end{frame}
@ -508,8 +472,7 @@ Now let's say we want all numbers between 50 and 100 that have the remainder 0 w
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{9. Algebraic Data Types} \frametitle{9. Algebraic Data Types}
Of course we can also define our own data types in haskell. One very common type is the \emph{enumeration}. For example, we could define a data type for the Week: Of course we can also define our own data types in haskell. One very common type is the \emph{enumeration}. For example, we could define a data type for the Week:
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
data WeekDay = Monday data WeekDay = Monday
| Tuesday | Tuesday
| Thursday | Thursday
@ -517,70 +480,64 @@ data WeekDay = Monday
| Friday | Friday
| Saturday | Saturday
| Sunday | Sunday
\end{lstlisting} \end{haskellcode}
This declares the new data type \code{WeekDay} with 7 \emph{constructors}. That means \code{Monday}, \code{Tuesday} etc. are all values of the type \code{WeekDay}. This declares the new data type \code{WeekDay} with 7 \emph{constructors}. That means \code{Monday}, \code{Tuesday} etc. are all values of the type \code{WeekDay}.
\pause \pause
\\ \\
We could now define a whole week, by creating a list: We could now define a whole week, by creating a list:
\pause \pause
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
week :: [WeekDay] week :: [WeekDay]
week = [Monday, Tuesday, Thursday, Wednesday week = [Monday, Tuesday, Thursday, Wednesday
, Friday, Saturday, Sunday] , Friday, Saturday, Sunday]
\end{lstlisting} \end{haskellcode}
\end{frame} \end{frame}
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{9. Algebraic Data Types (ctn.)} \frametitle{9. Algebraic Data Types (ctn.)}
And we can again \emph{pattern match} on our \code{WeekDay} type. Let's find out if a given day is a Monday: And we can again \emph{pattern match} on our \code{WeekDay} type. Let's find out if a given day is a Monday:
\pause \pause
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
isMonday :: WeekDay -> Bool isMonday :: WeekDay -> Bool
isMonday Monday = True isMonday Monday = True
isMonday x = False isMonday x = False
\end{lstlisting} \end{haskellcode}
\end{frame} \end{frame}
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{9. Algebraic Data Types (ctn.)} \frametitle{9. Algebraic Data Types (ctn.)}
But we can do more than enumerations. How about we do some error handling? Let's say we want a function to return an \code{Int}, but in case something went horribly wrong, we don't just want to return a 0 or some magic number, but a proper error message. Here we go: But we can do more than enumerations. How about we do some error handling? Let's say we want a function to return an \code{Int}, but in case something went horribly wrong, we don't just want to return a 0 or some magic number, but a proper error message. Here we go:
\pause \pause
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
data MaybeInt = NoError Int data MaybeInt = NoError Int
| Error String | Error String
\end{lstlisting} \end{haskellcode}
\pause \pause
So constructors are just \emph{functions}! And they can have arguments, just like functions. Let's check their types: So constructors are just \emph{functions}! And they can have arguments, just like functions. Let's check their types:
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
> :t NoError > :t NoError
NoError :: Int -> MaybeInt NoError :: Int -> MaybeInt
> :t Error > :t Error
Error :: String -> MaybeInt Error :: String -> MaybeInt
\end{lstlisting} \end{haskellcode}
\pause \pause
And now we can do sanity checks: And now we can do sanity checks:
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
calcSomething :: Int -> MaybeInt calcSomething :: Int -> MaybeInt
calcSomething x calcSomething x
| x < 100 = NoError (x * 5) | x < 100 = NoError (x * 5)
| otherwise = Error "Int out of range!" | otherwise = Error "Int out of range!"
\end{lstlisting} \end{haskellcode}
\end{frame} \end{frame}
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{9. Algebraic Data Types (ctn.)} \frametitle{9. Algebraic Data Types (ctn.)}
And pattern match on it as well: And pattern match on it as well:
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
addIntToList :: MaybeInt -> [Int] addIntToList :: MaybeInt -> [Int]
addIntToList (NoError x) = [x] addIntToList (NoError x) = [x]
addIntToList (Error str) = [] addIntToList (Error str) = []
\end{lstlisting} \end{haskellcode}
So if we got an error, we just return an empty list, otherwise we return a list with the \code{Int} as its only element. So if we got an error, we just return an empty list, otherwise we return a list with the \code{Int} as its only element.
\end{frame} \end{frame}
@ -588,11 +545,10 @@ So if we got an error, we just return an empty list, otherwise we return a list
\frametitle{9. Algebraic Data Types (ctn.)} \frametitle{9. Algebraic Data Types (ctn.)}
Let's define something more complex. How about a tree? Let's define something more complex. How about a tree?
\pause \pause
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
data Tree = Leaf Char data Tree = Leaf Char
| Node Tree Int Tree | Node Tree Int Tree
\end{lstlisting} \end{haskellcode}
Uh... that looks mean. Let's examine this.\\ Uh... that looks mean. Let's examine this.\\
\pause \pause
We have: We have:
@ -615,8 +571,7 @@ This is just an example. There are endless more ways of trees.
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{9. Algebraic Data Types (ctn.)} \frametitle{9. Algebraic Data Types (ctn.)}
Let's build our tree: Let's build our tree:
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
tree :: Tree tree :: Tree
tree = Node tree = Node
(Leaf 'x') (Leaf 'x')
@ -626,20 +581,19 @@ tree = Node
2 2
(Leaf 'z') (Leaf 'z')
) )
\end{lstlisting} \end{haskellcode}
See board... See board...
\end{frame} \end{frame}
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{9. Algebraic Data Types (ctn.)} \frametitle{9. Algebraic Data Types (ctn.)}
So if we want to generalize it, an algebraic data type has one or more \textbf{constructors}, and each of them can have zero or more arguments. E.g.: So if we want to generalize it, an algebraic data type has one or more \textbf{constructors}, and each of them can have zero or more arguments. E.g.:
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
data AlgDataType = Constr1 Type11 Type12 data AlgDataType = Constr1 Type11 Type12
| Constr2 Type21 | Constr2 Type21
| Constr3 Type31 Type32 Type33 | Constr3 Type31 Type32 Type33
| Constr4 | Constr4
\end{lstlisting} \end{haskellcode}
\end{frame} \end{frame}
\section{10. Questions so far?} \section{10. Questions so far?}
@ -737,14 +691,13 @@ For haskell IDEs, see \url{https://wiki.haskell.org/IDEs}
\end{itemize} \end{itemize}
\vspace{\baselineskip} \vspace{\baselineskip}
Does this compile? If not, fix it. Is this a total function? Does this compile? If not, fix it. Is this a total function?
\setHaskellCodeStyle \begin{haskellcode}
\begin{lstlisting}
data IntOrDouble = MkDouble Double data IntOrDouble = MkDouble Double
| MkInt Int | MkInt Int
f :: Int -> IntOrDouble f :: Int -> IntOrDouble
f 0 = 0.5 f 0 = 0.5
\end{lstlisting} \end{haskellcode}
\end{frame} \end{frame}
\section{16. Links} \section{16. Links}