J'ai besoin d'analyser un type (String,Int)
comme userRatings
est afin de lire à partir d'un textFile
correctement et j'utilise Parsec
. C'est l'analyse avec l'importation, ma fonction tuple stringInt
a cette erreur.Analyse d'un Tuple (String, Int) dans Haskell
Attendons deux arguments 'parsec (String, Int)' prévu un type, mais 'parsec (String, Int)' a en quelque sorte '* -> * -> *' Dans la signature de type pour '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)
Veuillez ne pas poster la même question plusieurs fois. Stack Overflow a des mécanismes pour attirer l'attention sur une question existante. – Jubobs