Browse Source

Use minted package for code

stripped-german
Julian Ospald 9 years ago
parent
commit
70256a9165
No known key found for this signature in database GPG Key ID: 220CD1C5BDEED020
1 changed files with 66 additions and 113 deletions
  1. +66
    -113
      VL1.tex

+ 66
- 113
VL1.tex View File

@@ -8,6 +8,7 @@
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{minted}

% for \verb inside \item
\usepackage[T1]{fontenc}
@@ -17,6 +18,13 @@
\DeclareGraphicsExtensions{.pdf,.png,.jpg}
\beamertemplatenavigationsymbolsempty
\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
\author{Julian Ospald}
@@ -30,36 +38,6 @@

% macros and environments
\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}

@@ -94,19 +72,17 @@ We'll have to see if this holds true.
\begin{frame}[fragile]
\frametitle{1. Why haskell? (ctn.)}
From C++ std:
\setCppCodeStyle
\begin{lstlisting}
\begin{cppcode}
void pop();
\end{lstlisting}
\setCCodeStyle
\end{cppcode}
\onslide<+->
From the C FLINT library:
\begin{lstlisting}
\begin{ccode}
void fmpz_mod_poly_add(
fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly1,
const fmpz_mod_poly_t poly2);
\end{lstlisting}
\end{ccode}
\vspace{\baselineskip}
\onslide<+->
Regular C functions in real-world (omitting examples on purpose):
@@ -289,8 +265,7 @@ Let's reiterate. Haskell is:
\begin{frame}[fragile]
\frametitle{4. Declarations}
Let's go!
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
x :: Int
x = 3

@@ -309,48 +284,46 @@ c = 'k'

s :: String
s = "Hello, world?"
\end{lstlisting}
\end{haskellcode}
\end{frame}

\section{5. Arithmetic and co.}

\begin{frame}[fragile]
\frametitle{5. Arithmetic and co.}
\setHaskellCodeStyle
GHCi:
\begin{lstlisting}
\begin{haskellcode}
> 3 + 5
> (3 :: Integer) + (5 :: Int)
> 6 * 5.0
> "Hello" ++ " world"
> "Haskell" > "C++"
> True && False
\end{lstlisting}
\end{haskellcode}
\end{frame}

\section{6. Functions and control structures}

\begin{frame}[fragile]
\frametitle{6. Functions and control structures}
\setHaskellCodeStyle
Let's make our first function. We want something like the following mathematical function\\
$f(x) = x * x$\\
\vspace{\baselineskip}
How could the haskell code look like?
\pause
Almost the same:
\begin{lstlisting}
\begin{haskellcode}
f x = x * x
\end{lstlisting}
\end{haskellcode}
\vspace{\baselineskip}
GHCi...\\
\pause
\vspace{\baselineskip}
What is a possible type signature for this function?
\begin{lstlisting}
\begin{haskellcode}
f :: Int -> Int
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.
\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.)}
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
\setHaskellCodeStyle
\vspace{\baselineskip}
\begin{lstlisting}
\begin{haskellcode}
isZero :: Int -> Bool
isZero 0 = True
isZero x = False
\end{lstlisting}
\end{haskellcode}
\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.\\
\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.)}
How about \emph{recursion}? Let's define the modulo of any \emph{Int} to \emph{2}.
\pause
\setHaskellCodeStyle
\vspace{\baselineskip}
\begin{lstlisting}
\begin{haskellcode}
mod2 :: Int -> Int
mod2 x
| x - 2 == 0 = 0
| x - 2 < 0 = x
| 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.
\end{frame}

@@ -392,15 +363,14 @@ These \verb#|# things above are called \textbf{guards} and are similar to \emph{
\begin{frame}[fragile]
\frametitle{7. Pairs/Tuples}
Defining a pair is easy.
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
p :: (Int, Char) -- this is the type
p = (2, 'y') -- this is how we construct the pair

-- pattern matching against pairs
sumPair :: (Int, Int) -> Int
sumPair (x, y) = x + y
\end{lstlisting}
\end{haskellcode}
\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.
\end{frame}
@@ -409,36 +379,34 @@ Note: we use \code{(x, y)} notation for both the type and the definition! Those

\begin{frame}[fragile]
\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.
\pause
We build lists by using either the \code{[]} notation:
\begin{lstlisting}
\begin{haskellcode}
list1 :: [Integer]
list1 = [1, 2]
\end{lstlisting}
\end{haskellcode}
\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.
\begin{lstlisting}
\begin{haskellcode}
emptyList = []

list2 = 1 : []

-- is this really a list?
list3 = [1, 2] == 1 : 2 : []
\end{lstlisting}
\end{haskellcode}
\pause
How about something more interesting:
\begin{lstlisting}
\begin{haskellcode}
infiniteList = [1..]
\end{lstlisting}
\end{haskellcode}
\end{frame}

\begin{frame}[fragile]
\frametitle{8. Lists (ctn.)}
Let's check on a few very common list operations:
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
> [1, 2] ++ [4, 5] -- append two lists
> head [1, 2, 3] -- first element
> 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
> sum [1, 2, 3]
> elem 7 [1, 2, 3] -- is there a 7 in the list?
\end{lstlisting}
\end{haskellcode}
\pause
A String in haskell is just a list of Chars!
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
> ['a', 'b', 'c']
> 'a' : []
> head "abc"
> 'a' ++ 'c'
\end{lstlisting}
\end{haskellcode}
\end{frame}

\begin{frame}[fragile]
\frametitle{8. Lists (ctn.)}
Again, we can do pattern matching on lists.
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
listLen :: [Integer] -> Integer
listLen [] = 0
listLen (x:xs) = 1 + listLen xs
\end{lstlisting}
\end{haskellcode}
\pause
We can also nest pattern matching:
\begin{lstlisting}
\begin{haskellcode}
sumEveryTwo :: [Integer] -> [Integer]
sumEveryTwo [] = 0
sumEveryTwo (x:[]) = [x]
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)}.
\end{frame}

@@ -489,17 +455,15 @@ $S = \{2 \times x\ |\ x \in \mathbb{N},\ x \leq 10\}$\\
\pause
How does this look in haskell?
\pause
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
> [x*2 | x <- [1..10]]
\end{lstlisting}
\end{haskellcode}
\pause
Now let's say we want all numbers between 50 and 100 that have the remainder 0 when divided by 12:
\pause
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
> [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.
\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]
\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:
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
data WeekDay = Monday
| Tuesday
| Thursday
@@ -517,70 +480,64 @@ data WeekDay = Monday
| Friday
| Saturday
| 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}.
\pause
\\
We could now define a whole week, by creating a list:
\pause
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
week :: [WeekDay]
week = [Monday, Tuesday, Thursday, Wednesday
, Friday, Saturday, Sunday]
\end{lstlisting}
\end{haskellcode}
\end{frame}

\begin{frame}[fragile]
\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:
\pause
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
isMonday :: WeekDay -> Bool
isMonday Monday = True
isMonday x = False
\end{lstlisting}
\end{haskellcode}
\end{frame}

\begin{frame}[fragile]
\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:
\pause
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
data MaybeInt = NoError Int
| Error String
\end{lstlisting}
\end{haskellcode}
\pause
So constructors are just \emph{functions}! And they can have arguments, just like functions. Let's check their types:
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
> :t NoError
NoError :: Int -> MaybeInt
> :t Error
Error :: String -> MaybeInt
\end{lstlisting}
\end{haskellcode}
\pause
And now we can do sanity checks:
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
calcSomething :: Int -> MaybeInt
calcSomething x
| x < 100 = NoError (x * 5)
| otherwise = Error "Int out of range!"
\end{lstlisting}
\end{haskellcode}
\end{frame}

\begin{frame}[fragile]
\frametitle{9. Algebraic Data Types (ctn.)}
And pattern match on it as well:
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
addIntToList :: MaybeInt -> [Int]
addIntToList (NoError x) = [x]
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.
\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.)}
Let's define something more complex. How about a tree?
\pause
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
data Tree = Leaf Char
| Node Tree Int Tree
\end{lstlisting}
\end{haskellcode}
Uh... that looks mean. Let's examine this.\\
\pause
We have:
@@ -615,8 +571,7 @@ This is just an example. There are endless more ways of trees.
\begin{frame}[fragile]
\frametitle{9. Algebraic Data Types (ctn.)}
Let's build our tree:
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
tree :: Tree
tree = Node
(Leaf 'x')
@@ -626,20 +581,19 @@ tree = Node
2
(Leaf 'z')
)
\end{lstlisting}
\end{haskellcode}
See board...
\end{frame}

\begin{frame}[fragile]
\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.:
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
data AlgDataType = Constr1 Type11 Type12
| Constr2 Type21
| Constr3 Type31 Type32 Type33
| Constr4
\end{lstlisting}
\end{haskellcode}
\end{frame}

\section{10. Questions so far?}
@@ -737,14 +691,13 @@ For haskell IDEs, see \url{https://wiki.haskell.org/IDEs}
\end{itemize}
\vspace{\baselineskip}
Does this compile? If not, fix it. Is this a total function?
\setHaskellCodeStyle
\begin{lstlisting}
\begin{haskellcode}
data IntOrDouble = MkDouble Double
| MkInt Int

f :: Int -> IntOrDouble
f 0 = 0.5
\end{lstlisting}
\end{haskellcode}
\end{frame}

\section{16. Links}


Loading…
Cancel
Save