Parsec provides an operator to choose between two parsers:
(<|>)
:: Text.Parsec.Prim.ParsecT s u m a
-> Text.Parsec.Prim.ParsecT s u m a
-> Text.Parsec.Prim.ParsecT s u m a
Is there a similar function to chain two parsers? I didn't find one with the same signature using Hoogle.
As an example, let's say I want to parse any word optionally followed by a single digit. My first idea was to use >>
but it doesn't seem to work.
parser = many1 letter >> optional (fmap pure digit)
I used fmap pure
in order to convert the digit to an actual string and thus match the parsed type of many1 letter
. I don't know if it is useful.
>>
or*>
do sequence parsers, through theirMonad
/Applicative
instances, but they only return the result of the second parser. (Likewise,<*
returns the result of the first; and as a convenience,x <$ p
returns a constant valuex
, equivalent to the common patternp *> pure x
). You don’t strictly need thefmap pure
there, since the result types don’t need to match, but it may often be more convenient to combine results using<$>
and<*>
(as in @SergeyKuz1001’s answer) if the types are the same. – Jon Purdy