我需要使用attoparsec解析固定长度的字段,但我现在正在努力编译器。 我仍然是新手,下面的代码是我最接近的解决方案:
> {-# LANGUAGE OverloadedStrings #-}
> import Control.Applicative
> import Data.Text as T
> import Data.Attoparsec.Combinator
> import Data.Attoparsec.Text hiding (take)
> import Data.Char
> import Prelude hiding (take)
>
> type City = Text
> type Ready = Bool
> data CityReady = CR City Ready deriving Show
>
> input = T.unlines ["#London 1",
> "#Seoul 0",
> "#Tokyo 0",
> "#New York 1"]
>
> parseCityReady = many $ CR <$> cityParser <*> readyParser <* endOfLine
>
> cityParser = char '#' *>
> takeTill isSpace <*
> skipWhile isHorizontalSpace
>
>
> readyParser = char '1' *> pure True <|> char '0' *> pure False
>
> main =
> case parseOnly parseCityReady input of
> Left err -> print err
> Right xs -> mapM_ print xs
>
这一切都很棒,但它只返回没有空格的城市。
CR "London" True
CR "Seoul" False
CR "Tokyo" False
我尝试使用applicative为City文本字符串取20个字符
> cityParser = char '#' *>
> take 20
甚至是do syntax
> cityParser = do char '#'
> city <- take 20
> return city
但这两种方法都无法使用此错误进行编译:
Couldn't match expected type `attoparsec-0.10.4.0:Data.Attoparsec.Internal.Types.Parser
Text b0'
with actual type `Text -> Text'
In the return type of a call of `take'
Probable cause: `take' is applied to too few arguments
In the second argument of `(*>)', namely `take 20'
In the expression: char '#' *> take 20
当Text -> Text
的{{1}}类型为take
时,是什么导致ghc要求Int -> Text -> Text
?
如何在applicative和do-syntax中解决它?
答案 0 :(得分:3)
所以,你的问题是你隐藏了take
的几个版本
功能。特别是,您隐藏take
来自attoparsec
而不是take
模块中的Text
函数。你需要做的就是
像这样改变你的进口
> import Control.Applicative
> import Data.Attoparsec.Combinator
> import Data.Attoparsec.Text
> import Data.Char
> import Data.Text as T hiding (take)
> import Prelude hiding (take)