有人能帮助我理解如何使用应用风格来编写Parsec解析器吗?这是我的代码:
module Main where
import Control.Applicative hiding (many)
import Text.Parsec
import Data.Functor.Identity
data Cmd = A | B deriving (Show)
main = do
line <- getContents
putStrLn . show $ parseCmd line
parseCmd :: String -> Either ParseError String
parseCmd input = parse cmdParse "(parser)" input
cmdParse :: Parsec String () String
cmdParse = do
slash <- char '/'
whatever <- many alphaNum
return (slash:whatever)
cmdParse2 :: String -> Parsec String () String
cmdParse2 = (:) <$> (char '/') <*> many alphaNum但是当我尝试编译它时,我得到了以下结果:
/home/tomasherman/Desktop/funinthesun.hs:21:13:
Couldn't match expected type `Parsec String () String'
with actual type `[a0]'
Expected type: a0 -> [a0] -> Parsec String () String
Actual type: a0 -> [a0] -> [a0]
In the first argument of `(<$>)', namely `(:)'
In the first argument of `(<*>)', namely `(:) <$> (char '/')'
Failed, modules loaded: none.我的想法是,我希望cmdParse2做与cmdParse相同的事情,但使用应用型stuff...my方法可能是完全错误的,我是新手
发布于 2012-10-25 17:26:39
你的应用用法是正确的,你只是有一个错误的签名。尝试:
cmdParse2 :: Parsec String () String发布于 2012-10-25 17:24:27
你的方法在我看来是正确的,问题是cmdParse2的类型错误。它应该与cmdParse具有相同的类型。顺便说一句,您可以在应用风格的解析器中省略char '/'两边的括号。
https://stackoverflow.com/questions/13065102
复制相似问题