Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Haskell is giving a parse error for this code?

Tags:

haskell

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)
}
like image 844
Rog Matthews Avatar asked Jan 16 '12 05:01

Rog Matthews


1 Answers

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.

like image 149
Tikhon Jelvis Avatar answered Sep 19 '22 14:09

Tikhon Jelvis