我在 Haskell 中实现了一个简单的解释器,但我遇到了这个问题。代码是这样的:
import Control.Applicative
import Data.Char
newtype Parser a = P (String -> [(a,String)])
parse :: Parser a -> String -> [(a,String)]
parse (P p) inp = p inp
item :: Parser Char
item = P (\inp -> case inp of
[] -> []
(x:xs) -> [(x,xs)])
instance Functor Parser where
fmap :: (a -> b) -> Parser a -> Parser b
fmap g p = P (\inp -> case parse p inp of
[] -> []
[(v,out)] -> [(g v, out)])
instance Monad Parser where
(>>=) :: Parser a -> (a -> Parser b) -> Parser b
p >>= f = P (\inp -> case parse p inp of
[] -> []
[(v,out)] -> parse (f v) out)
three :: Parser (Char,Char)
three = do {x <- item;
item;
z <- item;
return (x,z);}
如果我在拥抱中运行脚本,一切似乎都很好。但是当我尝试运行命令时
parse three "abcdef"
我收到一个错误:
Program error: undefined member: >>=
请问有人可以帮我吗?