[Haskell-cafe] Badly designed Parsec combinators?

Juan Carlos Arevalo Baeza jcab.lists at JCABs-Rumblings.com
Sat Feb 11 22:23:20 EST 2006


   So... I see no reason why someone can't just do it themselves, but... 
I was playing around with Parsec (as included in GHC 6.4.1), and I found 
two functions that are... not quite what I believe they should be.

optional :: GenParser tok st a -> GenParser tok st ()
optional p          = do{ p; return ()} <|> return ()

   Now, this completely loses the result of the optional parser. Better 
would be:

optional :: GenParser tok st a -> GenParser tok st (Maybe a)
optional p          = do{ x <- p; return (Just x) } <|> return Nothing

   Same thing with manyTill:

manyTill :: GenParser tok st a -> GenParser tok st end -> GenParser tok 
st [a]
manyTill p end      = scan
                    where
                      scan  = do{ end; return [] }
                            <|>
                              do{ x <- p; xs <- scan; return (x:xs) }

   Better:

manyTill :: GenParser tok st a -> GenParser tok st end -> GenParser tok 
st ([a], end)
manyTill p end      = scan
                where
                  scan  = do{ endr <- end; return ([], endr) }
                        <|>
                          do{ x <- p; (xs, endr) <- scan; return (x:xs, 
endr) }

   Is there any reason I can't see why they are the way they are?

JCAB



More information about the Haskell-Cafe mailing list