[Haskell] readList oddity

Johannes Waldmann waldmann at imn.htwk-leipzig.de
Thu Oct 21 03:55:08 EDT 2004

recently I was surprised by readList's behaviour (I'm no implying it is 
wrong). Look at this:

data R = R deriving Show

instance Read R where
     readsPrec p cs = do
         ( x, cs' ) <- lex cs
         case x of "R" -> return (R, cs')
	          _   -> error "no R"

that is, we have a "very eager" parser: if it does not accept the token,
it will raise an exception. now - which of the following will work?

check0 :: [R]
check0 = read "[ ]"

check1 :: [R]
check1 = read "[ R ]"

check2 :: [R]
check2 = read "[ R, R ]"

turns out that check1 and check2 work, but check0 will not (I thought it 
would). The implementation (in the Prelude) seems to think that "]" (in 
check0) could possibly be the beginning of a list element.
-- Johannes Waldmann,  Tel/Fax: (0341) 3076 6479 / 6480 --
------ http://www.imn.htwk-leipzig.de/~waldmann/ ---------

More information about the Haskell mailing list