我需要解析(String,Int)
类型为userRatings
是为了正确读取textFile
并且我正在使用Parsec
。这是解析与导入,我的stringInt
元组函数有这个错误。解析Haskell中的元组(字符串,Int)
期待的另外两个参数为“秒差距(字符串,整数)” 期望一个类型,但“秒差距(字符串,整数)”有一种“* - > * - > *” 在类型签名'stringIntTuple': stringIntTuple ::秒差距(字符串,整数)
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)
请不要多次发表相同的问题。堆栈溢出具有吸引人们关注现有问题的机制。 – Jubobs