gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:48:53 25 4
gpt4 key购买 nike

我是一名新程序员,正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com