Haskell格式问题

时间:2012-05-02 14:16:22

标签: haskell

main :: IO ()
main = do 
    contents <- readFile "text.txt"
    let database = (read contents :: [Film])
    putStr "Please enter your username: "
    userName <- getLine
    menu database        
    where menu newDb = do putStrLn "\nPlease select an option:"
                          putStrLn "1: Display all films currently in the database"
                          putStrLn "2: Add a new film to the database"
                          putStrLn "3: Search for films by director"
                          putStrLn "5: Exit"
                          putStr "\nSelected option: "
                          option <- getLine
                          case option of 
                                        "1" -> putStrLn(formatDatabase newDb)
                                        "2" -> do putStr "Name of film: "
                                               title <- getLine
                                               putStr "Name of director: "
                                               director <- getLine
                                               putStr "Year of release: "
                                               year <- getLine
                                               putStrLn(formatDatabase $ addFilm title director (read year) [] newDb)
                                        "3" -> do putStr "Name of director: "
                                               director <- getLine
                                               putStrLn $ formattedByDirector director
                          menu newDb

返回错误:

Parse error in pattern: putStr

On the line: "2" -> do putStr "Name of film: "

2 个答案:

答案 0 :(得分:5)

您必须将do块中的行全部缩进到 do之后的第一个令牌的级别。这同样适用于案例3.

case option of 
  "1" -> putStrLn(formatDatabase newDb)
  "2" -> do putStr "Name of film: "
            title <- getLine
            putStr "Name of director: "
            director <- getLine
            putStr "Year of release: "
            year <- getLine
            putStrLn(formatDatabase $ addFilm title director (read year) [] newDb)
  "3" -> do putStr "Name of director: "
            director <- getLine
            putStrLn $ formattedByDirector director

答案 1 :(得分:0)

请不要以许多教科书中的方式缩进代码。它在纸上看起来很不错,但实际上却很糟糕。

这是一个更加理智的风格指南:https://github.com/tibbe/haskell-style-guide