A mystery involving putStrLn

  • A+
Category:Languages

Why does the piece of code below produce the error parse error on input ‘putStrLn’?

main = do line <- fmap reverse getLine    putStrLn $ "You said " ++ line ++ " backwards!"    putStrLn $ "Yes, you said " ++ line ++ " backwards!"  <interactive>:11:4: error: parse error on input ‘putStrLn’ 

Also, why does the following piece of code produce the error parse error on input ‘let’?

main = do line <- getLine   let line' = reverse line   putStrLn $ "You said " ++ line' ++ " backwards!"   putStrLn $ "Yes, you said " ++ line' ++ " backwards!"    <interactive>:31:4: error: parse error on input ‘let’ 

 


Both snippets have the same problem. If you put the first action of a do block on the same line as the do itself, you still have to indent the rest of the actions in the do block as far as the first one. Two choices to fix it:

main = do line <- fmap reverse getLine           putStrLn $ "You said " ++ line ++ " backwards!"           putStrLn $ "Yes, you said " ++ line ++ " backwards!" 

or

main = do    line <- fmap reverse getLine    putStrLn $ "You said " ++ line ++ " backwards!"    putStrLn $ "Yes, you said " ++ line ++ " backwards!" 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: