Haskell格式问题

Jam*_*ieB 1 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
Run Code Online (Sandbox Code Playgroud)

返回错误:

Parse error in pattern: putStr

On the line: "2" -> do putStr "Name of film: "
Run Code Online (Sandbox Code Playgroud)

Dan*_*her 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
Run Code Online (Sandbox Code Playgroud)

  • 我确认您的代码中有选项卡(至少在您在OP中发布的内容中).尝试用空格替换所有你的TABS.应该运行正常. (3认同)
  • 顺便说一句,我建议你调整你的编辑器,以便它使用空格来模拟标签(它可以在Notepad ++中轻松完成).这样你以后就不会被打扰了.如果我没有弄错的话,你可以很容易地将此​​行为应用于.hs文件. (2认同)