I am trying to understand Parsers. Therefore I have created my own parser. Unfortunately it does not work. Why?
type Parser a = String -> [(a, String)]preturn :: a -> Parser a preturn t = \inp -> [(t,inp)] pfailure :: Parser a pfailure = \inp -> []pitem :: Parser Charpitem = \inp -> case inp of [] -> [] (x:xs) -> [(x,xs)]parse :: Parser a -> Parser a --parse :: Parser a -> String -> [(a,String)]parse p inp = p inp{-combine :: Parser a -> Parser b -> Parser (a,b)combine p1 p2 = \inp -> p2 t output where p1 inp = ([-}-- firstlast :: Parser (Char,Char)firstlast = do x <- pitem z <- pitem y <- pitem preturn (x,y)another = do x <- pitem y <- pitem
Firstlast is supposed to take a string and return the first and third character. Unfortunately, it returns odd values, and it does not accept its type (Parser (Char,Char))
For example,
*Main> firstlast "abc"[(([('a',"bc")],[('a',"bc")]),"abc")]
What should happen is:
*Main> firstlast "abc"[("ac",[])]