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.

有帮助吗?

解决方案

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 <|>).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top