1
0
Derivar 0

Add section about list comprehension

Este cometimento está contido em:
Julian Ospald 2015-04-15 17:47:56 +02:00
ascendente b0c2e8ce9e
cometimento 39e04a49aa
Não foi encontrada uma chave conhecida para esta assinatura, na base de dados
ID da chave GPG: 220CD1C5BDEED020
1 ficheiros modificados com 24 adições e 0 eliminações

24
VL1.tex
Ver ficheiro

@ -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: