Romil Agrawal
Romil Agrawal

Reputation: 71

Parse Error on Input '<-' : Haskell

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.

Upvotes: 0

Views: 223

Answers (2)

Tsuneo Yoshioka
Tsuneo Yoshioka

Reputation: 7874

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

Upvotes: 0

jamshidh
jamshidh

Reputation: 12070

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.

Upvotes: 5

Related Questions