使用Parsec键入错误

问题描述 投票:2回答:1

我正在使用Parsec 3.1.2和GHC 7.4.1来尝试编写一个有点多毛的数据文件格式的解析器。我认为这是一个非常简单的案例,但我遇到了类型错误。我正在尝试遵循Real World Haskell中的applicative functor示例。

import Text.ParserCombinators.Parsec hiding (many, optional, (<|>))
import Text.ParserCombinators.Parsec.Char
import Text.Parsec.String
import Control.Applicative
p_int = many char ' ' *> many1 digit <* many char ' '

现在,我最初得到以下类型错误:

Couldn't match expected type `[Char]'
            with actual type `Text.Parsec.Prim.ParsecT s0 u0 m0 [a0]'
In the return type of a call of `many1'
In the second argument of `(*>)', namely `many1 digit'
In the first argument of `(<*)', namely
  `many char ' ' *> many1 digit'

基于Trivial parsec example produces a type error,我尝试添加NoMonomorphismRestriction语言编译指示,但这没有帮助。

我承认,我发现Parsec的学习曲线相当陡峭,尽管我有一些Haskell经验。真实世界Haskell书的例子基于Parsec 2并没有帮助。

haskell parsec
1个回答
3
投票

您正在编写此代码:

many char ' '

这会将2个参数传递给many函数:char' '。你想要做的是将char ' '的结果传递给many函数,这样做是这样的:

many (char ' ')
© www.soinside.com 2019 - 2024. All rights reserved.