list - 对 Haskell 列表理解的工作感到困惑

标签 list haskell

我是一名新程序员,正在使用 Haskell。我找到了一行名为 find 的代码,它匹配一个 String 及其在列表中的对应对。像这样

find a b = [x|(y,x) <- b, a==y]

我不完全理解这个程序在说什么,对于列表理解,因为我以前从未见过以这种方式使用的 x|(y,x),我'我们主要看到它用作 x|xx|x^2。那么,这是否意味着从输入列表 B 中找到字符串 A = x 的列表理解,其中 x 是来自 B 的一对 (String, Char)y 等于您输入的 a?这里有点困惑。

最佳答案

我会发音理解

The list of all x such that (y, x) is drawn from b and a == y.

另一种阅读方式,更按顺序:

Draw pairs (y, x) from b. For each such pair, check whether a == y. If so, produce x; otherwise, move on to the next one.

我还应该注意到变量名称有点困惑。在 Haskell 中,给列表或其他容器赋予复数名称是惯用的。所以更像是

find a abs = [x | (y, x) <- abs, a == y]

关于list - 对 Haskell 列表理解的工作感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33263134/

相关文章:

.net - List<T> 是否保证项目将按添加顺序返回?

list - Groovy/Grails : How to sort the list of objects by id

string - Haskell 将字符串转换为整数列表

python 组中的列表值

python - 将逗号分隔的字符串转换为 float 列表

haskell - 我似乎无法在 haskell 中设置字符的值?

haskell - 如何组成 "Maybe"镜头?

haskell - 将计算与状态单子(monad)绑定(bind)?

java - 我需要哪种 Java 对象类型(集合/列表/集合/其他)?

haskell - 如何在 haskell 中使用 ffmpeg-light 查找 mp4 元数据?