2012-05-02 142 views
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: " 

回答

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 
+0

這並沒有解決這個問題,我仍然有同樣的錯誤:在模式解析錯誤:putStr – JamieB

+1

您使用的標籤?如果是這種情況,我讀了8個空格標籤。也許這就是你錯誤的根源。 – m09

+0

@JamieB必須像Mog說的那樣,在這裏解析得很好。 –