質問

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