[Haskell-beginners] parser comparison question

Walck, Scott walck at lvc.edu
Fri Apr 24 22:31:29 EDT 2009


I've been comparing Graham Hutton's simple parser with Parsec.  Here is some example code.

-- Comparison of Hutton's simple parser with Parsec

-- Hutton's simple parser is available at
-- http://www.cs.nott.ac.uk/~gmh/Parsing.lhs

-- Hutton simple parser called H
-- Parsec called P

import qualified Parsing as H
import qualified Text.ParserCombinators.Parsec as P

exH = H.parse (H.string "hi") "hiho"
exP = P.parse (P.string "hi") "" "hiho"

charH = H.parse (H.char 'a' H.+++ H.char 'b') "bbb"
charP = P.parse (P.char 'a' P.<|> P.char 'b') "" "bbb"

choiceH = H.parse (H.string "hoo" H.+++ H.string "ho") "hono"
choiceP1 = P.parse (P.string "bb" P.<|> P.string "ba") "" "bbb"
choiceP2 = P.parse (P.string "ba" P.<|> P.string "bb") "" "bbb"

choiceP22 = P.parse (P.try (P.string "ba") P.<|> P.string "bb") "" "bbc"

I am interested if anyone could comment on the design of the Parsec 'try' function.
For example, choiceP2 fails and returns Left error, while choiceP22 succeeds.

Hutton's simple parser doesn't need try.  It seems so simple and elegant.
I'm wondering why Parsec requires me to use 'try' for a string parse that might fail.

Thanks,

Scott


Scott N. Walck
Associate Professor of Physics
Lebanon Valley College



More information about the Beginners mailing list