1

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

市のテキスト文字列に20文字を使用するためにapplicativeを使用してみました

> 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ときにghcが要求する原因は何ですか?takeInt -> Text -> Text

アプリケーションと構文の両方でそれを解決するにはどうすればよいですか?

4

1 に答える 1

3

したがって、問題は、take 関数のいくつかのバージョンを非表示にしていることです。特に、モジュールの関数ではなく、takefromを非表示にしています。あなたがする必要があるのはそのようにあなたの輸入を変えることだけですattoparsectakeText

> 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)
于 2013-02-05T19:30:03.147 に答える