Browse Source

Add section about list comprehension

Julian Ospald 4 years ago
parent
commit
39e04a49aa
No account linked to committer's email address
1 changed files with 24 additions and 0 deletions
  1. 24
    0
      VL1.tex

+ 24
- 0
VL1.tex View File

@@ -412,6 +412,30 @@ sumEveryTwo (x:(y:zs)) = (x + y) : sumEveryTwo zs
412 412
 Note that \code{(x:(y:zs))} may also be written as \code{(x:y:zs)}.
413 413
 \end{frame}
414 414
 
415
+\begin{frame}[fragile]
416
+\frametitle{Lists (ctn.)}
417
+Haskell also supports \textbf{list comprehension} which is basically syntactic sugar for what we already know from maths.\\
418
+Let's define a set that contains the first ten even natural numbers:\\
419
+\pause
420
+$S = \{2 \times x\ |\ x \in \mathbb{N},\ x \leq 10\}$\\
421
+\vspace{\baselineskip}
422
+\pause
423
+How does this look in haskell?
424
+\pause
425
+\setHaskellCodeStyle
426
+\begin{lstlisting}
427
+> [x*2 | x <- [1..10]]
428
+\end{lstlisting}
429
+\pause
430
+Now let's say we want all numbers between 50 and 100 that have the remainder 0 when divided by 12:
431
+\pause
432
+\setHaskellCodeStyle
433
+\begin{lstlisting}
434
+> [x | x <- [50..100], mod x 12 == 0]
435
+\end{lstlisting}
436
+\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.
437
+\end{frame}
438
+
415 439
 \begin{frame}
416 440
 \frametitle{Toolchain}
417 441
 You need: