1
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: "
4

2 に答える 2

5

ブロック内の行をすべて、。の後do最初のトークンのレベルまでインデントする必要があります。同じことがケース3にも当てはまります。do

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
于 2012-05-02T14:17:48.897 に答える
0

多くの教科書に見られるようにコードをインデントしないでください。紙の上では見栄えがしますが、実際に作業するにはひどいものです。

これがはるかに健全なスタイルガイドです:https ://github.com/tibbe/haskell-style-guide

于 2012-05-02T16:24:10.797 に答える