“一些”和‘许多’来自“替代”的功能类型类别

发布于 2024-12-08 23:04:03 字数 237 浏览 1 评论 0原文

Alternative 类型类中的函数 somemany 有什么用处? 文档提供我无法理解的递归定义。

What are the functions some and many in the Alternative type class useful for? Docs provide a recursive definition which I was unable to comprehend.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泅人 2024-12-15 23:04:04

somemany 可以定义为:

some f = (:) <
gt; f <*> many f
many f = some f <|> pure []

也许有助于了解如何使用单子 do 语法编写 some

some f = do
  x <- f
  xs <- many f
  return (x:xs)

所以 some f 运行 f 一次,然后“很多”次,并整理结果。 many f 运行 f “一些”次,或者“或者”只返回空列表。这个想法是,它们都尽可能频繁地运行 f 直到它“失败”,并将结果收集在列表中。区别在于,如果 f 失败,some f 会立即失败,而在这种情况下,many f 仍然会成功并“返回”空列表。但这一切的确切含义取决于<|> 的定义方式。

它只对解析有用吗?让我们看看它对基类中的实例做了什么:Maybe[]STM

首先也许Nothing 表示失败,因此 some Nothing 也会失败并计算为 Nothing,而 many Nothing 成功并计算为 <代码>只是[]。 some (Just ())many (Just ()) 都不会返回,因为 Just () 永远不会失败!从某种意义上说,它们的计算结果为Just (repeat ())

对于列表,[] 表示失败,因此 some [] 计算结果为 [](无答案),而 many [] code> 的计算结果为 [[]] (只有一个答案,它是空列表)。同样,一些 [()]许多 [()] 不会返回。展开实例,some [()] 表示 fmap (():) (many [()])many [()]表示 some [()] ++ [[]],因此您可以说 many [()]tails (repeat ()) 相同

对于STM,失败意味着必须重试事务。因此,some retry 将重试自身,而 many retry 将仅返回空列表。 some fmany f 将重复运行 f 直到重试。我不确定这是否有用,但我猜不是。

因此,对于 Maybe 来说,[]STM manysome 不会似乎很有用。仅当应用程序具有某种状态,使得在一遍又一遍地运行相同的事情时越来越可能出现失败时,它才有用。对于解析器来说,这是随着每次成功匹配而缩小的输入。

some and many can be defined as:

some f = (:) <
gt; f <*> many f
many f = some f <|> pure []

Perhaps it helps to see how some would be written with monadic do syntax:

some f = do
  x <- f
  xs <- many f
  return (x:xs)

So some f runs f once, then "many" times, and conses the results. many f runs f "some" times, or "alternatively" just returns the empty list. The idea is that they both run f as often as possible until it "fails", collecting the results in a list. The difference is that some f immediately fails if f fails, while many f will still succeed and "return" the empty list in such a case. But what all this exactly means depends on how <|> is defined.

Is it only useful for parsing? Let's see what it does for the instances in base: Maybe, [] and STM.

First Maybe. Nothing means failure, so some Nothing fails as well and evaluates to Nothing while many Nothing succeeds and evaluates to Just []. Both some (Just ()) and many (Just ()) never return, because Just () never fails! In a sense they evaluate to Just (repeat ()).

For lists, [] means failure, so some [] evaluates to [] (no answers) while many [] evaluates to [[]] (there's one answer and it is the empty list). Again some [()] and many [()] don't return. Expanding the instances, some [()] means fmap (():) (many [()]) and many [()] means some [()] ++ [[]], so you could say that many [()] is the same as tails (repeat ()).

For STM, failure means that the transaction has to be retried. So some retry will retry itself, while many retry will simply return the empty list. some f and many f will run f repeatedly until it retries. I'm not sure if this is useful thing, but I'm guessing it isn't.

So, for Maybe, [] and STM many and some don't seem to be that useful. It is only useful if the applicative has some kind of state that makes failure increasingly likely when running the same thing over and over. For parsers this is the input which is shrinking with every successful match.

清晨说晚安 2024-12-15 23:04:04

例如,解析(请参阅“示例应用解析”部分)。

E.g. for parsing (see the "Applicative parsing by example" section).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文