[Haskell-cafe] stuck with a sample of "programming in haskell"

Stephen Tetley stephen.tetley at gmail.com
Fri Mar 19 06:01:29 EDT 2010


2010/3/19 国平张 <zhangguoping at gmail.com>:
> Sorry. The same error, This is new stuff.

Ah indeed - I didn't spot that one as I only read the code rather than ran it.

With the change the parser type to use /newtype/ all the primitive
parsers have to be encoded inside the newtype's constructor
(primitive parsers being ones that have to look directly at the input
stream).

item :: Parser Char
item = Parser $ \inp -> case inp of
                     [] -> []
                     (x:xs) -> [(x,xs)]

Or in a more prosaic style

item :: Parser Char
item = Parser (\inp -> case inp of
                    [] -> []
                    (x:xs) -> [(x,xs)])



This is slightly tiresome. Fortunately once you have defined a small
set of primitive parsers, many more parsers can be "derived" by
combining the primitives rather than looking at the input stream -
this is the power of the monadic style. The p parser you defined with
the do ... notation is one such derived parser.

Best wishes

Stephen


More information about the Haskell-Cafe mailing list