Parse Error on Input '<-' : Haskell

224 views Asked by At

I am taking input from the user and the code is as follows:

putStrLn $ "Enter number"
num <- getLine
main = print $ num

When I run this code, the compiler gives following error:

ra.hs:10:5: parse error on input `<-'

How can I remove this error? I have tried to use spaces, as well as tab characters, but the error persists. Please help.

2

There are 2 answers

0
jamshidh On BEST ANSWER

You have to move all of your code into main

main = do
    putStrLn "Enter number"
    num <- getLine
    print num

The area outside of main is for declarations, etc. You use <- inside of a do.

Also, you don't need the extra $'s when there is a single parameter.

0
Tsuneo Yoshioka On

It just works for me, if I put lines to inside main.

main:: IO ()
main = do
    putStrLn $ "Enter number"
    num <- getLine
    print $ num

Here is runnable code: https://paiza.io/projects/CCc2kEbxXWfRRdVYAqbXww