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

Returns error:

Parse error in pattern: putStr

On the line: "2" -> do putStr "Name of film: "

2 Answers 2

5

You must indent the lines in the do block all to the level of the first token after the do. The same applies to the case 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
Sign up to request clarification or add additional context in comments.

10 Comments

This didn't resolve the issue, I still have the same error: Parse error in pattern: putStr
are you using tabs ? I read that 8 spaces tabs are assumed if that's the case. Maybe that's the source of your error.
I confirm that you have tabs in your code (at least in what you posted in your OP). Try replacing ALL YOUR TABS by spaces. Should run fine.
@JamieB That looks correctly indented. But you never know with tabs, don't use tabs in Haskell code, configure your editor to indent with spaces.
BTW I suggest you tune your editor so that it uses spaces to simulate tabs (it's easily done in Notepad++). This way you won't be bothered later on. You can apply this behaviour only to .hs files quite easily if I'm not mistaken.
|
0

Please do not indent your code in the manner found in many textbooks. It looks nice on paper, but is terrible for actually working with.

Here's a much more sane style guide: https://github.com/tibbe/haskell-style-guide

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.