浏览代码

Add section about list comprehension

stripped-german
Julian Ospald 9 年前
父节点
当前提交
39e04a49aa
找不到此签名对应的密钥 GPG 密钥 ID: 220CD1C5BDEED020
共有 1 个文件被更改,包括 24 次插入0 次删除
  1. +24
    -0
      VL1.tex

+ 24
- 0
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)}.
\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:


正在加载...
取消
保存