Why can't my code coding with haskell from Functional Programming in Haskell be successfully interpreted?

The code is as follows:

type Parser a = String -> [(a, String)]

retrn :: a -> Parser a
retrn v = \inp -> [(v, inp)]

parse :: Parser a -> String -> [(a, String)]
parse p inp = p inp

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

--problem code
p :: Parser (Char, Char)
p = do x <- item
       item
       y <- item
       retrn (x, y)

It gives the following error like:

SO-34035520.hs:19:8:
    Couldn't match type `[(Char, String)]' with `Char'
    Expected type: String -> [((Char, Char), String)]
      Actual type: Parser ([(Char, String)], [(Char, String)])
    In a stmt of a 'do' block: retrn (x, y)
    In the expression:
      do { x <- item;
           item;
           y <- item;
           retrn (x, y) }

What is noteworthy, the example code that is on the official website of the book can be interpreted smoothly, this is the * .lhs format.

So can someone tell me why? I have been working on this fight for several days.

Thanks in advance.

+4
source share
1 answer

(->) Stringhas a copy of Monad , but that’s not what you are looking for. When you use do-notation in a definition p, this instance is selected.

, Monad Parser ( newtype String -> [(a, String)]), p.

, return ( retrn), , , return (->) String ( be retrn v = \inp -> v.

+9

Source: https://habr.com/ru/post/1618230/


All Articles