我需要用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来获取20个字符作为City文本字符串
> 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当take的类型为Int -> Text -> Text时,是什么导致ghc请求Text -> Text
我如何在应用语法和do语法中解决这个问题?
发布于 2013-02-06 03:30:03
因此,您的问题是隐藏了take函数的多个版本。特别是,您向attoparsec隐藏了take,而不是向Text模块隐藏了take函数。您所需要做的就是像这样更改您的导入
> 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)https://stackoverflow.com/questions/14714112
复制相似问题