Rog Matthews
Rog Matthews

Reputation: 3247

Why Haskell is giving a parse error for this code?

I am trying to make a program that reads a number given by a user and then prints it. the number has to be an integer when I print it, but this code gives me a parse error:

main = do
{
       putStrLn "Please enter the number"
       number <- getLine
       putStrLn "The num is:" ++ show (read number:: Int)
}

Upvotes: 6

Views: 2898

Answers (2)

Tikhon Jelvis
Tikhon Jelvis

Reputation: 68172

If you use brackets in your do statement, you have to use semicolons. Also, the last line should be putStrLn $ "The num is:" ++ show (read number :: Int)

So you have two options:

main = do
{
   putStrLn "Please enter the number";
   number <- getLine;
   putStrLn $ "The num is:" ++ show (read number:: Int)
}

or:

main = do
   putStrLn "Please enter the number"
   number <- getLine
   putStrLn $ "The num is:" ++ show (read number:: Int)

Almost all of the code I've seen uses the second version, but they are both valid. Note that in the second version, whitespace becomes significant.

Upvotes: 10

Kaostias
Kaostias

Reputation: 321

Haskell recognizes the Tab character and your program could be failing because of that. If you're using Tabs, change them to whitespaces.

Upvotes: 1

Related Questions