在IO Haskell中使用Proper printF命令

时间:2016-03-09 18:00:19

标签: haskell io printf

  

输入UserRatings =(String,Int)
  类型电影=(标题,导演,年份,   [UserRatings])

我正在尝试为IO使用正确的printF方法,但我无法弄清楚出了什么问题。我知道问题与userRatings有关。

在neo.txt中有25部电影类型我试图在我制作的用户界面菜单中打印它们。

用户输入main - >那么他的用户名 - >然后是期权选择" 2"然后应该显示电影。

    displayFilmsIO :: Database -> IO()
    displayFilmsIO [] = putStrLn "There are no films in the database"
    displayFilmsIO filmList = sequence_[printf("|%-25s| |%-65s| |%-10d| |(%s,%d) |\n") title director year [(name,rating)] | (title, director, year, [(name,rating)]) <- filmList] 

    mainMenu :: [Film] -> String -> IO()
    mainMenu list user  = do
        putStrLn "What do you want to do ?(choose one of the options below)"
        putStrLn "[1] Add a film to the database \n[2] View all Films \n[3] View all films that you are fan of of\n[4] View all fans of a particular film \n[5] View all the films that were released during a particular period\n[6] Be a fan of a particular film\n[7] View the average number of fans for the films starring a particular actor \n[8] View the names of actors who have co-starred in at least one film with a particular actor \n[9] Save \n[0] Exit"
        putStrLn "__________________________________"
        choice <-getLine
        case choice of
             "2" -> do
                printf ("|%-25s| |%-65s| |%-10d| | %s,%d |\n") "Title" "Cast" "Year" "Fans"
                displayFilmsIO list
                mainMenu list user

这是主要方法。 没有任何问题,它会编译,但只是为了让您有更多信息。

main :: IO ()
main = do
    films <- loadFile2
    getName2 films

loadFile :: FilePath -> IO (Either ParseError [Film])
loadFile filename = do
  handle <- openFile filename ReadMode
  !database <- hGetContents handle
  hClose handle
  return $ parse films "Films" database

loadFile2 :: IO [Film]
loadFile2 = do
    allFilms <- readFile "neo.txt"
    let films = read allFilms :: [Film]
    return films

getName2 :: [Film] -> IO ()
getName2 films = do

1 个答案:

答案 0 :(得分:4)

摘录

for EL in self.root.iter():
    for child in list(EL):
        if child.tag == "tagString":
            EL.remove(child)

看起来错误:5个格式字符串printf("|%-25s| |%-65s| |%-10d| |(%s,%d) |\n") title director year [(name,rating)] 参数,但有4个参数。也许你想要

%

此外,整个列表理解看起来不对:

printf("|%-25s| |%-65s| |%-10d| |(%s,%d) |\n") title director year name rating

这只会处理sequence_[printf | (title, director, year, [(name,rating)]) <- filmList] 只有一个名字和评级的电影,默默地忽略其他一切。

最后,请查看filmListmapM_:它们比forM_ +列表理解更简单。 E.g。

sequence_

您可能需要一些嵌套循环,如上图所示:一个用于扫描胶片,另一个用于扫描相关名称/等级。