How does `try` behave in Parsec?

In an attempt to figure out how it tryworks in parsec, I put this in ghci:

λ> parse (try $ string "a") "" "b"
Left (line 1, column 1):
unexpected "b"
expecting "a"

I am not quite sure what I expected, but this is not what I expected, since I thought that the whole goal tryis not to cause an error when the expected thing does not exist.

+4
source share
1 answer

tryIt does not contain errors without errors, but if an error occurs, it will not consume any tokens (it resets the stream token, AKA return path). The error will still occur, but you can continue as if the parser was not consuming any tokens at all (using <|>).

+9
source

Source: https://habr.com/ru/post/1535197/


All Articles