I’m new to Haskell. This is my parser:
data Parser a = MkParser (String -> Maybe a)
This parses any string, gives the first character:
-- anyChar
anyChar :: Parser Char
anyChar = MkParser sf
where
sf "" = Nothing
sf (c:cs) = Just c
It works. Now, I am learning from a tutorial. It says I can convert an answer of a parser and run it through a function, like this (creating a new parser):
-- convert a parsers answer based on a function
convert :: (a -> b) -> Parser a -> Parser b
convert f (MkParser p1) = MkParser sf
where
sf inp = case p1 inp of
Nothing -> Nothing
Just x -> Just (f x)
This looks like it works! I realized here that I can access the input string in my parser functions. (Even though the definition of convert
doesn’t take an input string. I want to recreate anyChar
so that it also uses the input string (Just so I can understand the syntax and what is going on). (I came from using Python and I’m a rookie at Haskell)
This is what I tried
-- apparently we can use "inp" to signifiy the input string
-- let's recreate anyChar to use input
anyCharInp :: Parser Char
anyCharInp = MkParser sf
where
case inp of
(c:cs) -> Just c
_ -> Nothing
But it’s giving an indentation error. Any ideas?
user20102550 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.