haskell-lectures/VL1/content/VL1_lists4.tex

19 satır
1.3 KiB
TeX

\ifger{Haskell erlaubt auch}{Haskell also supports} \textbf{list comprehension} \ifger{, was eigentlich nur Syntaxsugar ist und der mathematischen Schreibweise sehr ähnelt.}{which is basically syntactic sugar for what we already know from maths.}\\
\ifger{Wir wollen ein Set definieren, das die ersten 10 geraden Zahlen beinhaltet:}{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
\\
\ifger{Wie könnte das in Haskell aussehen?}{How does this look in haskell?}
\pause
\begin{haskellcode*}{bgcolor=mygrey,frame=single,numbers=none,label=GHCi}
> [ 2 * x | x <- [1..10]]
\end{haskellcode*}
\pause
\ifger{Jetzt wollen wir alle Zahlen zwischen 50 und 100 die als Rest 0 haben, wenn wir sie modulo 12 nehmen:}{Now let's say we want all numbers between 50 and 100 that have the remainder 0 when divided by 12:}
\pause
\begin{haskellcode*}{bgcolor=mygrey,frame=single,numbers=none,label=GHCi}
> [x | x <- [50..100], mod x 12 == 0]
\end{haskellcode*}
\hinline{x <- [50..100]} \ifger{ist das Binding, während}{is the binding, while} \hinline{mod x 12 == 0} \ifger{das Prädikat ist}{is the predicate}, \ifger{getrennt durch ein Komma}{separated by a comma}. \ifger{Wir können mehrere Prädikate haben}{We can have multiple predicates.}