From 39e04a49aab8f92151007f0b28ef62764a7fa407 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Wed, 15 Apr 2015 17:47:56 +0200 Subject: [PATCH] Add section about list comprehension --- VL1.tex | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/VL1.tex b/VL1.tex index 68bc24c..96bcbe7 100644 --- a/VL1.tex +++ b/VL1.tex @@ -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: