Parsec question

Derek Elkins ddarius at
Thu Jan 1 04:27:11 EST 2004

On Wed, 31 Dec 2003 19:21:54 -0500 (EST)
Mark Carroll <mark at> wrote:

> I tried posting this before but, from my point of view, it vanished.
> My apologies if it's a duplicate.
> In we read,
> > testOr2 =   try (string "(a)")
> >         <|> string "(b)"
> >
> > or an even better version:
> >
> > testOr3 =   do{ try (string "(a"); char ')'; return "(a)" }
> >         <|> string "(b)"
> Why is the latter better?

As soon as you reach a point where a syntax error means no parse will
succeed you want to commit to that alternative for two reasons.  1) As
long as there appears to be a possibility for an alternative parse the
input needs to be saved and 2) there's no point backtracking if all
other alternatives will fail; it just wastes time.  In the above
example both issues come up.  If we successfully parse the
"(a" then the second alternative "(b)" can't possibly succeed and since
it can't succeed there's no point in saving the input "(a" to be
reparsed when backtracking since there's no point in backtracking. 
Obviously, this example is merely to illustrate the idea as there'd be
little to no inefficiency in this case.

More information about the Haskell-Cafe mailing list