解析 Haskell 中的元组 (String,Int)
Parsing a Tuple (String,Int) in Haskell
我需要解析 (String,Int)
类型,因为 userRatings
是为了正确读取 textFile
,而我正在使用 Parsec
。这是导入时的解析,我的 stringInt
元组函数有这个错误。
Expecting two more arguments to ‘Parsec (String, Int)’
Expected a type, but ‘Parsec (String, Int)’ has kind ‘* -> * -> *’
In the type signature for ‘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)
错误消息基本上是说您没有提供足够的类型参数。如果您将元组解析器的签名与其他解析器的签名进行比较,您就会明白为什么:
stringLit :: Parsec String u String
listOfStrings :: Parsec String u [String]
stringIntTuple :: Parsec (String , Int)
在所有其他情况下,您提供三个类型参数,但对于 stringIntTuple
,您只提供一个。
所以只需添加 String
和 u
作为前两个参数,就像您为其他参数所做的那样,它就会起作用。
我需要解析 (String,Int)
类型,因为 userRatings
是为了正确读取 textFile
,而我正在使用 Parsec
。这是导入时的解析,我的 stringInt
元组函数有这个错误。
Expecting two more arguments to ‘Parsec (String, Int)’ Expected a type, but ‘Parsec (String, Int)’ has kind ‘* -> * -> *’ In the type signature for ‘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)
错误消息基本上是说您没有提供足够的类型参数。如果您将元组解析器的签名与其他解析器的签名进行比较,您就会明白为什么:
stringLit :: Parsec String u String
listOfStrings :: Parsec String u [String]
stringIntTuple :: Parsec (String , Int)
在所有其他情况下,您提供三个类型参数,但对于 stringIntTuple
,您只提供一个。
所以只需添加 String
和 u
作为前两个参数,就像您为其他参数所做的那样,它就会起作用。