8

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.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Mel
  • 123
  • 2
  • 7
  • 5
    Check your whitespace to make sure there are no tab characters in it (see e.g. [this question](http://stackoverflow.com/questions/16870038/haskell-syntax-error-for-where-statement)). – hammar Jun 02 '13 at 05:38
  • OH GOD! Thank you very much! It works! I think it is a little bit tricky. :D – Mel Jun 02 '13 at 05:46
  • 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. :-) – shachaf Jun 02 '13 at 08:06
  • 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. – Ralph Jun 02 '13 at 09:21
  • 2
    GHC should warn by default when it finds _any_ tab in a source file. – leftaroundabout Jun 02 '13 at 09:37

1 Answers1

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.

Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
  • Yes, that is the problem. I really did not notice this when I write it. Your answer is really helpful. Thank you! – Mel Jun 02 '13 at 05:55
  • 2
    @Mel : Actually, you can mix tabs and spaces in various combinations when indenting multiple lines; the important thing here is that [the Haskell Report](http://www.haskell.org/onlinereport/haskell2010/haskellch10.html#x17-17800010.3) specifies that a tab character counts for 8 spaces and not 4 as you seem to have your editor set up. – yatima2975 Jun 04 '13 at 09:02