why 'try' not work in parsec

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

On Sat, Nov 22, 2008 at 9:46 AM, Changying Li
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
<|> do char '(' skipMany space x <- (try parseList) <|> parseDottedList char ')' return x
parseList is succeeding and returning (1); then "char ')'" is failing (because the next character in the input is the '.' -- ryan
participants (2)
-
Changying Li
-
Ryan Ingram