Reputation: 93
I'm writing a parser
And it's working as it is :
type Parser a = String -> Maybe (a ,String)
parseInt :: Parser Int
parseInt "" = Nothing
parseInt s = case reads s ::[(Int, String)] of
[(x, s)]-> Just(x, s)
_ -> Nothing
parseChar :: Char -> Parser Char
parseChar a str | a == head str = Just (a, tail str)
| otherwise = Nothing
But i need too change
type Parser a = String -> Maybe (a ,String)
to
data Parser a = Parser { runParser :: String -> Maybe (a , String ) }
As soon as I do, it every function i wrote do not compile anymore, what are the step to do this change, and, what is happening ?
Upvotes: 1
Views: 63
Reputation: 476594
You will need to use the Parser
data constructor and thus wrap the function into that data constructor, so:
parseInt :: Parser Int
parseInt = Parser f
where f "" = Nothing
f s = case reads s ::[(Int, String)] of
[(x, s)]-> Just(x, s)
_ -> Nothing
parseChar :: Char -> Parser Char
parseChar a = Parser f
where f str | a == head str = Just (a, tail str)
| otherwise = Nothing
Upvotes: 1