Question

In an attempt to figure out how try works in parsec, I put this into ghci:

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

I'm not exactly sure what I was expecting, but this is not what I was expecting, since I thought the entire purpose of try is to not cause an error when the expected thing isn't there.

Était-ce utile?

La solution

try does not silently eat errors, but it will not consume any tokens when an error occurs (it resets the token stream, AKA backtracks). The error will still occur, but you can continue as if the parser didn’t consume any tokens at all (by using <|>).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top