我是一名新程序员,正在使用 Haskell。我找到了一行名为 find 的代码,它匹配一个 String 及其在列表中的对应对。像这样
find a b = [x|(y,x) <- b, a==y]
我不完全理解这个程序在说什么,对于列表理解,因为我以前从未见过以这种方式使用的 x|(y,x)
,我'我们主要看到它用作 x|x
或 x|x^2
。那么,这是否意味着从输入列表 B
中找到字符串 A
= x
的列表理解,其中 x
是来自 B
的一对 (String, Char)
,y
等于您输入的 a
?这里有点困惑。
最佳答案
我会发音理解
The list of all
x
such that(y, x)
is drawn fromb
anda == y
.
另一种阅读方式,更按顺序:
Draw pairs
(y, x)
fromb
. For each such pair, check whethera == y
. If so, producex
; 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/