2014-02-26 59 views
0

我試着運行這段代碼,但我發現在輸入解析錯誤‘讓’上:哈斯克爾解析輸入錯誤'讓」

let database = tempFilm:database 

我無法找到問題的所在,可以你看看嗎?

options 7 (username, database) = do putStrLn "******************" 
             putStrLn " Rate a film " 
             putStrLn "******************" 
             putStrLn "" 
             putStr "Enter the title of the film or nothing to return to the main menu: " 
             title <- getLine 
             if title == "" 
             then return(username, database) 
             else do let filmCheck = findFilm title database 
               if filmCheck == [] 
               then do putStrLn "That film does not exists." 
                 return (username, database) 
               else do putStr "Enter your rate: " 
                 tempRate <- getLine 
                 case reads tempRate :: [(Integer, String)] of 
                  [(n, "")] -> do let rate = read tempRate :: Int 
                      let tempFilm = rateFilm username (username, rate) filmCheck 
                      if checkIfRated username tempFilm == True 
                       then do putStrLn "You already voted for this film\n" 
                         putStrLn "Do you want to modify your voted?\n" 
                         putStrLn "Press y to modify or nothing to return to the main menu:" 
                         input <- getLine 
                         if input /= "y" 
                         then return (username, database) 
                         else putStrLn "Your vote will be modified." 
                      let database = tempFilm:database 
                      putStrLn "You rating has been sumbited successfully!" 
                      putStrLn (displayFilm tempFilm) 
                      return (username, database) 

編輯: 使用時

options 7 (username, database) = do putStrLn "******************" 
             putStrLn " Rate a film " 
             putStrLn "******************" 
             putStrLn "" 
             putStr "Enter the title of the film or nothing to return to the main menu: " 
             title <- getLine 
             if title == "" 
             then return(username, database) 
             else do let filmCheck = findFilm title database 
               if filmCheck == [] 
               then do putStrLn "That film does not exists." 
                 return (username, database) 
               else do putStr "Enter your rate: " 
                 tempRate <- getLine 
                 case reads tempRate :: [(Integer, String)] of 
                  [(n, "")] -> do let rate = read tempRate :: Int 
                      let tempFilm = rateFilm username (username, rate) filmCheck 
                      when (checkIfRated username tempFilm == True) $ 
                       do putStrLn "You already voted for this film\n" 
                        putStrLn "Do you want to modify your vote?\n" 
                        putStrLn "Press y to modify or nothing to return to the main menu:" 
                        input <- getLine 
                        if input /= "y" 
                         then return (username, database) 
                        else putStrLn "Your vote will be modified." 
                      let database = tempFilm:database 
                      putStrLn "You rating has been sumbited successfully!" 
                      putStrLn (displayFilm tempFilm) 
                      return (username, database) 

我的語法可能不正確我以前從來沒有

回答

5

時,沒有elseif checkIfRated ... then do putStrLn "you already voted"...

+0

感謝您的回覆Aaron。有沒有辦法寫一個if語句沒有別的? like(if A then blah blah) – Bobys

+0

@ user3207652:在do-block中,您可以使用['when'](http://hackage.haskell.org/package/base/docs/Control-Monad.html#v :什麼時候),例如'當cond $做...' – hammar

+0

它給出一個關於「何時」不在範圍內的錯誤 – Bobys

0

使用如果關鍵字指示開始克具有三個分量的表達式:謂詞(計算結果爲BOOL類型),則然後分支(計算結果爲鍵入說),以及其他分支(即必須評估鍵入也) 。 如果如果表達式是類型A,則爲任何有效的Haskell類型。

我在這裏引用從真實世界哈斯克爾書,第2章,類型和功能,可根據條件評估

回想一下,Haskell是一種面向表達式的語言。在命令式語言中,從if省略else分支是有意義的,因爲我們正在處理語句而不是表達式。但是,當我們使用表達式時,如果缺少else,那麼如果謂詞評估爲False,那麼它將不會有結果或類型,所以這將是無意義的。

在命令式語言如果語句告訴計算機執行的東西,但它沒有返回一個類型的對象。在Haskell中,它是一個表達式,用於表示類型系統以避免意外的副作用。