问题描述
Alternative
类型类中的 some
和 many
函数有什么用?文档提供一个我无法理解的递归定义.
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.
推荐答案
some
和 many
可以定义为:
some f = (:) <$> f <*> many f
many f = some f <|> pure []
也许有助于了解 some
如何用 monadic do
语法编写:
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)
所以 some f
运行一次 f
,然后many"运行一次.次,并得出结果.many f
运行 f
"some";次,或替代地"只返回空列表.这个想法是他们都尽可能多地运行 f
直到它失败",将结果收集在一个列表中.区别在于,如果 f
失败,some f
会立即失败,而 many f
仍然会成功并返回".在这种情况下的空列表.但这一切意味着什么取决于<|>
是如何定义的.
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.
是否只对解析有用?让我们看看它对 base 中的实例做了什么:Maybe
、[]
和 STM
.
Is it only useful for parsing? Let's see what it does for the instances in base: Maybe
, []
and STM
.
首先也许
.Nothing
意味着失败,所以 some Nothing
也会失败并评估为 Nothing
而 many Nothing
成功并评估为 .some (Just ())
和 many (Just ())
都不会返回,因为 Just ()
永远不会失败!从某种意义上说,他们评估为 Just (repeat ())
.
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 ())
.
对于列表,[]
意味着失败,所以 some []
计算结果为 []
(没有答案)而 many []
计算结果为 [[]]
(有一个答案,它是空列表).再次 some [()]
和 many [()]
不返回.展开实例,some [()]
表示 fmap(():) (many [()])
和 many [()]
表示 some [()] ++ [[]]
,所以你可以说 many [()]
和 tails (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 ())
.
对于STM
,失败意味着事务必须重试.所以 some retry
将重试自身,而 many retry
将简单地返回空列表.some f
和 many f
将重复运行 f
直到它重试.我不确定这是否有用,但我猜不是.
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.
所以,对于Maybe
、[]
和 STM
many
和 some
好像没那么好用只有当应用程序具有某种状态使得在一遍又一遍地运行相同的事情时越来越可能失败时,它才有用.对于解析器,这是随着每次成功匹配而缩小的输入.
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.
这篇关于“替代"类型类中的“一些"和“许多"函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!