Q:

Problems with Parsers

i hope somebody can help me to understand the following code
type Parser a = String -> [(a,String)]

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

returnP :: Parser a
returnP a  = \s -> [(a,s)] 

(>>=) :: Parser a -> (a -> Parser b) -> Parser b
p>>=f = \s -> case p s of
    [(x,xs)]-> f x xs
    _ -> []

twochars :: Parser (Char,Char)
twochars= item >>= \a -> item >>= \b -> returnP (a,b)

Everything seems to be clear but i dont understand the lampda function in the last line in the twochars-function. It would be nice if somebody can give me a explanation about that.

A:

Rewriting the twochars function for clarity and it is basically:
twochars = 
  item >>= \a -> -- parse a character and call it `a`
  item >>= \b -> -- parse another character and call it `b`
  returnP (a,b)  -- return the tuple of `a` and `b`

The lambdas here just introduce names for the parsed characters, and let them be passed along to a later part of the computation.
They correspond to the second argument in the bind you have defined:
(>>=) :: Parser a        -- your item
      -> (a -> Parser b) -- your lambda returning another parse result
      -> Parser b        

