下面是代码:为什么这会循环'数据'而不是'新类型'?
import Control.Applicative
-- newtype Parser a = Parser { runParser :: String -> [(a, String)] }
data Parser a = Parser { runParser :: String -> [(a, String)] }
instance Functor Parser where
fmap f (Parser p) = Parser (\s -> [(f x, s') | (x, s') <- p s ])
instance Applicative Parser where
pure a = Parser (\s -> [(a, s)])
Parser q <*> Parser p = Parser (\s -> [(f x, s'') | (f, s') <- q s, (x, s'') <- p s'])
instance Alternative Parser where
empty = Parser (\s -> [])
Parser q <|> Parser p = Parser (\s -> q s ++ p s)
item = Parser (\s -> case s of
(x:xs) -> [(x, xs)]
_ -> []
)
在当前的代码,runParser (some item) "abcd"
循环,但如果分析器被声明为newtype
,它工作得很好。
究竟是什么你问?你有没有可以展示这个的例子? – Alec
这个问题几乎没有任何信息。如果您不分享更多细节,我们无法猜测发生了什么。您应该提供[MCVE](http://stackoverflow.com/help/mcve)。 – chi