2015-04-19 22:32:01 +00:00
|
|
|
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
|
2015-04-20 18:15:46 +00:00
|
|
|
\begin{haskellcode*}{bgcolor=mygrey,frame=single,numbers=none,label=GHCi}
|
2015-04-20 13:07:59 +00:00
|
|
|
> [ 2 * x | x <- [1..10]]
|
2015-04-20 18:15:46 +00:00
|
|
|
\end{haskellcode*}
|
2015-04-19 22:32:01 +00:00
|
|
|
\pause
|
|
|
|
Now let's say we want all numbers between 50 and 100 that have the remainder 0 when divided by 12:
|
|
|
|
\pause
|
2015-04-20 18:15:46 +00:00
|
|
|
\begin{haskellcode*}{bgcolor=mygrey,frame=single,numbers=none,label=GHCi}
|
2015-04-19 22:32:01 +00:00
|
|
|
> [x | x <- [50..100], mod x 12 == 0]
|
2015-04-20 18:15:46 +00:00
|
|
|
\end{haskellcode*}
|
2015-04-20 18:55:41 +00:00
|
|
|
\hinline{x <- [50..100]} is the binding, while \hinline{mod x 12 == 0} is the predicate, separated by a comma. We can have multiple predicates.
|