From 3a1a7f668b8a65d356779dfc6f59af48b0262c5c Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Fri, 17 Apr 2015 18:40:58 +0200 Subject: [PATCH] Move Pairs after Lists --- VL1.tex | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/VL1.tex b/VL1.tex index 010405a..0a9c643 100644 --- a/VL1.tex +++ b/VL1.tex @@ -358,27 +358,10 @@ mod2 x 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} -\section{7. Pairs/Tuples} +\section{7. Lists} \begin{frame}[fragile] -\frametitle{7. Pairs/Tuples} -Defining a pair is easy. -\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{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} - -\section{8. Lists} - -\begin{frame}[fragile] -\frametitle{8. Lists} +\frametitle{7. Lists} 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: @@ -404,7 +387,7 @@ infiniteList = [1..] \end{frame} \begin{frame}[fragile] -\frametitle{8. Lists (ctn.)} +\frametitle{7. Lists (ctn.)} Let's check on a few very common list operations: \begin{haskellcode} > [1, 2] ++ [4, 5] -- append two lists @@ -427,7 +410,7 @@ A String in haskell is just a list of Chars! \end{frame} \begin{frame}[fragile] -\frametitle{8. Lists (ctn.)} +\frametitle{7. Lists (ctn.)} Again, we can do pattern matching on lists. \begin{haskellcode} listLen :: [Integer] -> Integer @@ -446,7 +429,7 @@ Note that \code{(x:(y:zs))} may also be written as \code{(x:y:zs)}. \end{frame} \begin{frame}[fragile] -\frametitle{8. Lists (ctn.)} +\frametitle{7. Lists (ctn.)} Haskell also supports \textbf{list comprehension} which is basically syntactic sugar for what we already know from maths.\\ Let's define a set that contains the first ten even natural numbers:\\ \pause @@ -467,6 +450,23 @@ Now let's say we want all numbers between 50 and 100 that have the remainder 0 w \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} +\section{8. Pairs} + +\begin{frame}[fragile] +\frametitle{8. Pairs} +Defining a pair is easy. +\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{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} + \section{9. Algebraic Data Types} \begin{frame}[fragile]