输入“let”时Haskell解析错误

时间:2014-02-26 18:56:53

标签: haskell

我试图运行此代码,但我在输入'let'上得到一个解析错误:

 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) 

我的语法可能不正确我以前从未使用

2 个答案:

答案 0 :(得分:5)

else

没有if checkIfRated ... then do putStrLn "you already voted"...

答案 1 :(得分:0)

if 关键字表示包含三个组件的表达式的开头:谓词(评估为类型为Bool),然后分支(评估为type say A )和else分支(必须评估为同时键入 A )。 if if 表达式的类型是类型A,任何有效的Haskell类型。

我引用 Real World Haskell 一书,第2章,类型和函数,条件评估

  

回想一下,Haskell是一种面向表达的语言。在命令式语言中,从if中省略else分支是有意义的,因为我们使用语句而不是表达式。但是,当我们使用表达式时,如果谓词被评估为False,那么如果缺少else,则不会有结果或类型,因此它将是无意义的。

在命令式语言中, if 语句告诉计算机执行某些操作但不必返回键入的对象。在Haskell中,它是一个表达式,它尊重类型系统以避免意外的副作用。