我试图理解解析器。因此,我创建了自己的解析器。不幸的是,它不起作用。为什么?
type Parser a = String -> [(a, String)]
preturn :: a -> Parser a
preturn t = \inp -> [(t,inp)]
pfailure :: Parser a
pfailure = \inp -> []
pitem :: Parser Char
pitem = \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 应该接受一个字符串并返回第一个和第三个字符。不幸的是,它返回奇数值,并且它不接受它的类型(Parser (Char,Char))
例如,
*Main> firstlast "abc"
[(([('a',"bc")],[('a',"bc")]),"abc")]
应该发生的是:
*Main> firstlast "abc"
[("ac",[])]