正确调用runParser Haskell

时间:2016-03-09 20:51:48

标签: parsing haskell compilation

我编写了一组解析函数,但不知道如何正确调用它们。

以下是我尝试调用parser函数的方法。

main :: IO ()
main = do 
    tempDatabase <- readFile "films.txt"
    let i = lines tempDatabase
    let database = runParser parser () database
    putStrLn "Enter your name: "
    username <- getLine
    userInterface database username
    appendFile "films.txt" (show database)
    putStrLn "Your changes to the database have been successfully saved."

我收到以下类型错误:

Couldn't match type ‘String -> Either ParseError Film’
                   with ‘[Char]’
    Expected type: SourceName
      Actual type: String -> Either ParseError Film

Couldn't match expected type ‘[Film]’
            with actual type ‘String -> Either ParseError Film’
Probable cause: ‘database’ is applied to too few arguments

对于更多上下文,这些是解析器的所有函数(正常工作)。

str :: Parser String
str = many1 (noneOf ",")

int :: Parser Int
int = read <$> many1 digit

tup :: Parser UserRatings
tup = do user <- str
         _ <- oneOf ","
         rating <- int
         return (user, rating)

parser :: Parser Film
parser = do title <- str
            _ <- oneOf ","
            director <- str
            _ <- oneOf ","
            year <- int
            _ <- oneOf ","
            ratings <- sepBy tup (oneOf ",")
            eof
            return (title, director, year, ratings)

这是userInterface

userInterface :: [Film] -> String -> IO()
userInterface filmList user = do
                              putStrLn "\n............................\n"
                              putStrLn "Welcome to the Film web Database     "
                              putStrLn "............................"
                              putStrLn "1. - Add a new film to the database\n"
                              putStrLn "2. - Print all films in the database\n"
                              putStrLn "3. - Print all films that a particular user is a fan of\n"
                              putStrLn "4. - Print all fans of the film of your choice\n"
                              putStrLn "5. - Print all films that were released during a particular period\n"
                              putStrLn "6. - Be a fan of the film of your choice!\n"
                              putStrLn "7. - Print the average number of fans for the films starring a particular actor\n"
                              putStrLn "8. - Print the names of actors who have co-starred in at least one film\n"
                              putStrLn "0. - Quit and update database\n"
                              putStrLn "****************************************************************************\n"
                              putStr "Enter a number to perform an action or 0 to exit and save the database: "
                              userChoice <-getLine
                              case userChoice of
                                  "1" -> do
                                         putStrLn $ displayAllFilmsString filmList
                                         userInterface filmList user

0 个答案:

没有答案