admin管理员组文章数量:1292358
I try to use a library regex-applicative.
My code is:
nicBeg = ((psym $ not . isSpace) $> (few anySym <> " adapter ")) *> some anySym
result = match nicBeg "abcd adapter NIC 1"
and I see that result
is Just "bcd adapter NIC 1"
.
But the documentation of *>
tells:
*> :: forall (f :: Type -> Type) a b. Applicative f => f a -> f b -> f b
Defined in ‘GHC.Base’ (base-4.16.4.0)
Sequence actions, discarding the value of the first argument.
So, the question is: why does "bcd adapter " part exist in the result
and not only "NIC 1" (it was not discarded)? Is it a problem of the library or I am wrong somewhere?
PS. The library has a concept of "greedy" RE expressions.
PS. It's easy to get an expected result: ((psym $ not . isSpace) *> (few anySym <> " adapter ")) *> some anySym
(ie, to replace $>
with *>
)
I try to use a library regex-applicative.
My code is:
nicBeg = ((psym $ not . isSpace) $> (few anySym <> " adapter ")) *> some anySym
result = match nicBeg "abcd adapter NIC 1"
and I see that result
is Just "bcd adapter NIC 1"
.
But the documentation of *>
tells:
*> :: forall (f :: Type -> Type) a b. Applicative f => f a -> f b -> f b
Defined in ‘GHC.Base’ (base-4.16.4.0)
Sequence actions, discarding the value of the first argument.
So, the question is: why does "bcd adapter " part exist in the result
and not only "NIC 1" (it was not discarded)? Is it a problem of the library or I am wrong somewhere?
PS. The library has a concept of "greedy" RE expressions.
PS. It's easy to get an expected result: ((psym $ not . isSpace) *> (few anySym <> " adapter ")) *> some anySym
(ie, to replace $>
with *>
)
2 Answers
Reset to default 6(psym $ not . isSpace) $> (few anySym <> " adapter ")
is a very weird expression. I'm not sure what you want it to mean, but because ($>)
is a flipped version of (<$)
, it means "match the regular expression (psym $ not . isSpace)
, and then replace the value you parsed with (few anySym <> " adapter ")
". Importantly, (few anySym <> " adapter ")
is not being used as a regular expression at all. It is the value associated with parsing a single non-space character. Then, of course, *> some anySym
throws away that value, just as you expect, and tries to match the remaining string (i.e., "bcd adapter NIC 1"
) against some anySym
, with predictable results.
So in the end you are not confused about (*>)
at all, but about ($>)
.
amalloy explained where you've gone wrong, but let me just get into one small bit of your question:
Is it a problem of the library[...]?
It's not, and it can't be. The fact that *>
discards the result(s) from the left operand is guaranteed by its type.
class Functor f => Applicative f where
...
(*>) :: f a -> f b -> f b
The polymorphic type variable a
only appears in a "negative" position in the function type, with nothing to connect it to types in the result. So *>
doesn't "know" anything about that type and can't use values of that type at all (except to force them).
本文标签: haskellWhy does the implementation of *gt return something from the left argumentStack Overflow
版权声明:本文标题:haskell - Why does the implementation of *> return something from the left argument? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741554327a2385062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论