Why is Haskell giving an analysis error for this code?

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

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

If you use parentheses in your do expression, you must use semicolons. In addition, 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 the code I saw uses the second version, but they are both valid. Please note that in the second version, spaces become significant.

+10
source

Haskell recognizes the Tab character, and because of this, your program may fail. If you use tabs, change them to spaces.

+1
source

Source: https://habr.com/ru/post/906073/


All Articles