Add section about list comprehension

This commit is contained in:
Julian Ospald 2015-04-15 17:47:56 +02:00
parent b0c2e8ce9e
commit 39e04a49aa
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
1 changed files with 24 additions and 0 deletions

24
VL1.tex
View File

@ -412,6 +412,30 @@ sumEveryTwo (x:(y:zs)) = (x + y) : sumEveryTwo zs
Note that \code{(x:(y:zs))} may also be written as \code{(x:y:zs)}.
\end{frame}
\begin{frame}[fragile]
\frametitle{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
$S = \{2 \times x\ |\ x \in \mathbb{N},\ x \leq 10\}$\\
\vspace{\baselineskip}
\pause
How does this look in haskell?
\pause
\setHaskellCodeStyle
\begin{lstlisting}
> [x*2 | x <- [1..10]]
\end{lstlisting}
\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}
> [x | x <- [50..100], mod x 12 == 0]
\end{lstlisting}
\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}
\begin{frame}
\frametitle{Toolchain}
You need: