'Alternative'类型的函数 [英] Functions from 'Alternative' type class
问题描述
可能存在重复:
Haskell:一些和很多
Haskell - 什么是Control.Applicative.Alternative for?
另类
类型中的某些
和多
函数是什么类有用吗? 文档提供一个我无法理解的递归定义。
一些
和许多
可以定义为:
一些f =(:)< ; $> f *许多f
许多f =一些f< |>纯[]
也许有助于了解某些$ c $
一些f =做
x< - f
xs< - 许多f
return(x:xs)
因此,一些f
运行 f
一次,然后很多次,结果。 许多f
运行 f
有些次,或者可选只是返回空列表。这个想法是,他们都尽可能经常地运行 f
,直到它失败,将结果收集到列表中。不同的是,如果 f
立即失败,某些f
失败,而多f
将成功并返回空列表。但是,这一切意味着完全取决于如何定义< |>
。
解析?让我们来看看它对基础实例的作用: Maybe
, []
和 STM
。
第一个也许
。 Nothing
表示失败,因此 some Nothing
也会失败,并且计算结果为 Nothing
,而 many nothing
成功并计算为 Just []
。 some(Just())
和 many(Just())
永远不会返回,因为 Just()
永远不会失败!在某种意义上,他们评价为 Just(repeat())
。
对于列表 []
意味着失败,所以某些[]
的计算结果为 []
(无答案)而 many []
的计算结果为 [[]]
(有一个答案,它是空列表)。再次某些[()]
和多个[()]
不会返回。展开实例,某些[()]
表示 fmap(():)(many [()])
和 many [()]
表示 some [()] ++ [[]]
,所以你可以说 many [()]
与 tails(repeat())
相同。
对于 STM
,失败意味着交易必须重试。因此一些重试
会自动重试,而多次重试
只会返回空列表。 一些f
和许多f
会反复运行 f
,直到它重试。我不确定这是否有用,但我猜测它不是。
因此,对于可能
, []
和 STM
许多
和一些
似乎并不那么有用。只有当应用程序具有某种状态时,一旦反复运行相同的事件会使失败的可能性越来越大。对于解析器来说,这是每次成功匹配都会缩小的输入。
Possible Duplicate:
Haskell: some and many
Haskell - What is Control.Applicative.Alternative good for?
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
and many
can be defined as:
some f = (:) <$> 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
fails if f
fails immediately, while many f
will succeed and "return" the empty list. But what this all means exactly 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.
这篇关于'Alternative'类型的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!