Add section about list comprehension
This commit is contained in:
parent
b0c2e8ce9e
commit
39e04a49aa
24
VL1.tex
24
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)}.
|
Note that \code{(x:(y:zs))} may also be written as \code{(x:y:zs)}.
|
||||||
\end{frame}
|
\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}
|
\begin{frame}
|
||||||
\frametitle{Toolchain}
|
\frametitle{Toolchain}
|
||||||
You need:
|
You need:
|
||||||
|
Loading…
Reference in New Issue
Block a user