2016-03-06 32 views
0

我需要解析(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) 
+0

请不要多次发表相同的问题。堆栈溢出具有吸引人们关注现有问题的机制。 – Jubobs

回答

0

错误消息基本上是说,你没有提供足够的类型参数。如果你与别人比较你的元组分析器的签名,你就会明白为什么:

stringLit :: Parsec String u String 
listOfStrings :: Parsec String u [String] 
stringIntTuple :: Parsec (String , Int) 

在您提供三条类型参数的所有其他情况,但对于stringIntTuple你只提供一个。

因此,只需将Stringu作为前两个参数添加,就像您为其他人所做的那样,它就可以工作。

+0

谢谢!尽管如此显而易见:P – Max