1

I am currently learning Haskell from the online version of Learn You a Haskell, and I'm in Chapter 4: Syntax in Functions. While going through the book, I code up all the sample functions verbatim into my text editor (Notepad++) and run it on GHCI.

The most recent function I did is starting to annoy me (it's in the Guards, Guards! section of Chapter 4).

initials :: String -> String -> String
initials firstname lastname = [f] ++ "." ++ [l]
    where   (f:_) = firstname
            (l:_) = lastname

This is my code, and it's also the code that is shown in the book. Whenever I write it myself, the GCHI always gives me the parse error. However, when I copy/paste it from the book, it works. The funny part is that there's no difference. I copy/pasted the code over mine and there was literally no difference between them. I did this several times, so I'm certain I'm not being delusional.

Why is this happening? How can I fix it? I looked it up at first, but all I see is that the two statements after the "where" have to be aligned in the same column. I'm doing that. It's still not working.

kullalok
  • 813
  • 2
  • 13
  • 19
  • Pasting code over other code in Notepad++ is not a good way to detect whether or not it is identical. Use tools that compute and visualize the difference between two files, like Unix/GNU diff, or GUI tools. Your Haskell compiler probably does not change how it parses the input based on time of day, or random sources; there must be a difference. – Kaz Jun 05 '13 at 20:44
  • 3
    Are you using tabs? If you are, switch to spaces. Tabs can often cause compilation issues even when everything looks right. – Jeff Burka Jun 05 '13 at 20:46
  • 3
    Go to Settings > Preferences, select the `Language Menu/Tab Settings` tab, in the right-hand scrollbox select "haskell", then uncheck "Use default value" and check "Replace by space". I usually like to have the tab size at 2. That'll change it so when it auto-indents it'll use spaces. – paul Jun 05 '13 at 20:57
  • 1
    There are definitely tabs in the source you pasted. I don't understand why GHC doesn't just make them a syntax error. – Carl Jun 05 '13 at 21:36
  • 1
    If you click on the paragraph button in the toolbar in Notepad++, it'll show you what symbols are used in your document. Spaces are shown as little orange dots, tabs as little orange arrows. That should help you see where the tabs are. – paul Jun 05 '13 at 21:43
  • Thanks @paul for your first comment. It works now :) – kullalok Jun 05 '13 at 21:50

1 Answers1

3

Could it be that you are using tabs and doing it inconsistently (i.e., use tabs in some lines and spaces in others)?

The Haskell report says:

  • Tab stops are 8 characters apart.
  • A tab character causes the insertion of enough spaces to align the current position with the next tab stop.

It is really recommended to never use tabs and always use spaces!

Stefan Holdermans
  • 7,990
  • 1
  • 26
  • 31