Вопрос

I tried to run the following code which is taken from 'Programming in Haskell' by Graham Hutton

type Parser a = [(a, String)]

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

when loading the module in GHCI 7.6.3 the following error occurs:

Couldn't match expected type `t0 -> [(a, t0)]'
    with actual type `[(a, String)]'
The lambda expression `\ inp -> ...' has one argument,
but its type `Parser a' has none
In the expression: \ inp -> [(v, inp)]
In an equation for `return': return v = \ inp -> [(v, inp)]
Failed, modules loaded: none.

I changed the sample to:

type Parser a = [(a, String)]

return :: a -> String -> Parser a
return v inp = [(v,inp)]

which worked, I'd like to get to run the original sample however and was wondering what I've missed.

Это было полезно?

Решение

I don't have this book, but it's clearly a typo. Should be:

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

By the way, the author's website has all the code from the book, and if you have a look at Parsing.lhs, you'll see the definition

newtype Parser a  =  P (String -> [(a,String)])

That's probably a more advanced version which is obtained incrementally during the chapter, but any way, the fact that a parser has an input string is crucial.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top