Haskell: parse error on input 'putStrLn'
Asked Answered
S

1

7

I just wrote my first Haskell program, but there is an error that I cannot understand. I think it is right because I just wrote it like the example from a book. Could anyone help me please?

main = do
    putStrLn "Hello, what's your name?"
    name <- getLine
    putStrLn ("Hey" ++ name ++ ", nice to meet you!")

The error message is:

parse error on input 'putStrLn'

It is strange.

Supersession answered 2/6, 2013 at 5:34 Comment(5)
Check your whitespace to make sure there are no tab characters in it (see e.g. this question).Pancratium
OH GOD! Thank you very much! It works! I think it is a little bit tricky. :DSupersession
As an aside: That doesn't look like a full error message. GHC errors have a line and column number. This would have been easier for people (including you!) with that information, so make sure you always include the full error message. :-)Onestep
Most editors will allow substituting spaces for tabs. When writing Haskell, or any whitespace-sensitive language, it is a good idea to turn that on.Turnpike
GHC should warn by default when it finds any tab in a source file.Hardship
C
11

Though it's impossible to tell from your posted code because SO converts tabs to spaces at least some of the time, the problem is likely that you input a literal tab character before putStrLn instead of four spaces as you did for the other two lines in your do block, or vice versa.

All of the statements in a do block must start with the exact same whitespace, and not just appear to line up visually. If you're using a text editor that can display literal tabs in a special way, set it up to do so; it will save you some headaches.

Cortezcortical answered 2/6, 2013 at 5:45 Comment(2)
Yes, that is the problem. I really did not notice this when I write it. Your answer is really helpful. Thank you!Supersession
@Supersession : Actually, you can mix tabs and spaces in various combinations when indenting multiple lines; the important thing here is that the Haskell Report specifies that a tab character counts for 8 spaces and not 4 as you seem to have your editor set up.Breathing

© 2022 - 2024 — McMap. All rights reserved.