[Haskell-cafe] why 'try' not work in parsec

Changying Li lchangying at gmail.com
Sat Nov 22 12:46:12 EST 2008


Hi.
I read 'write yourself a scheme in 48 hours' and try to modify its code.
now part of code is :

parseList :: Parser LispVal
parseList = liftM List $ sepEndBy parseExpr spaces

parseDottedList :: Parser LispVal
parseDottedList = do
  head <- endBy parseExpr spaces
  tail <- char '.' >> spaces >> parseExpr
  return $ DottedList head tail

parseExpr :: Parser LispVal
parseExpr = parseAtom
            <|> parseString
            <|> parseNumber
            <|> parseQuoted
            <|> do char '('
                   skipMany space
                   x <- (try parseList) <|> parseDottedList
                   char ')'
                   return x


but I got an error when parse (1 . 2):
Lisp>>> (1 . 2)
Parse error at "lisp" (line 1, column 4):
unexpected "."
expecting space, letter, "\"", digit, "'", "(" or ")"

I think it failed in parseList because when I rewrite 7th line in
parseExpr as:
                   x <- (try parseDottedList) <|> parseList

it work.

so my question is: if it is failed in parseList, why didn't 'try' work?
why not the parseExpr try parseDottedList ?



-- 

Thanks & Regards

Changying Li



More information about the Haskell-Cafe mailing list