我需要将(String,Int)
类型解析为userRatings
,以便正确地从textFile
读取并使用Parsec
。这是解析和导入,我的stringInt
元组函数有这个错误。
期待'Parsec(String,Int)'的两个参数 期望一个类型,但'Parsec(String,Int)'有种'* - > * - > *” 在'stringIntTuple'的类型签名中: stringIntTuple :: Parsec(String,Int)
import Control.Monad
import Control.Applicative((<*))
import Text.Parsec
( Parsec, ParseError, parse -- Types and parser
, between, noneOf, sepBy, many1 -- Combinators
, char, spaces, digit, newline -- Simple parsers
)
-- Types
type Title = String
type Director = String
type Year = Int
type UserRatings = (String,Int)
type Film = (Title, Director, Year , [UserRatings])
type Period = (Year, Year)
type Database = [Film]
-- Parse a string to a string
stringLit :: Parsec String u String
stringLit = between (char '"') (char '"') $ many1 $ noneOf "\"\n"
-- Parse a string to a list of strings
listOfStrings :: Parsec String u [String]
listOfStrings = stringLit `sepBy` (char ',' >> spaces)
-- Parse a string to an int
intLit :: Parsec String u Int
intLit = fmap read $ many1 digit
-- Or `read <$> many1 digit` with Control.Applicative
stringIntTuple :: Parsec (String , Int)
stringIntTuple = liftM2 (,) stringLit intLit
film :: Parsec String u Film
film = do
title <- stringLit
newline
director <- stringLit
newline
year <- intLit
newline
userRatings <- stringIntTuple
newline
return (title, director, year, userRatings)
答案 0 :(得分:0)
错误消息基本上是说您没有提供足够的类型参数。如果你将元组解析器的签名与其他解析器进行比较,你就会明白为什么:
stringLit :: Parsec String u String
listOfStrings :: Parsec String u [String]
stringIntTuple :: Parsec (String , Int)
在所有其他情况下,您提供三种类型参数,但对于stringIntTuple
,您只提供一种。
因此,只需添加String
和u
作为前两个参数,就像您为其他参数所做的那样,并且它会起作用。