Editor agnosticism in haskell examples

Nov 27, 2006 15:50

pozorvlak recently discovered a bit of a gotcha in writing haskell examples:

If I'm in emacs, and I write this:

tick = do n <- get; put (n+1); return n;

on three lines, it comes out like this:

tick = do n <- get
put (n+1)
return n

This is because emacs has a particularly smart indent mode - it notices the "do", and realises that I want the next line to line up with the first word after the "do" on the previous line.

Vim doesn't do this. If I write it (using almost exactly the same keypresses) in vim, I get this:

tick = do n <- get
put (n+1)
return n

That's because vim doesn't understand the "do". It just knows that I've hit the tab key, so I want it more indented than it was before. Note that there are no literal tabs in there (which I think we're probably all agreed would be bad) - it's just that there aren't the right number of spaces. The "put" doesn't line up with the "n <-", so haskell thinks that the "n <-" was the last statement in the do block. Which is illegal, so it won't compile.

This could be particularly frustrating for a beginner (who might be using notepad for all we know), trying to copy a worked example from a web page.

If anyone's out there writing docs for haskell libraries, or tutorials, or whatever, I would like to suggest the following form:

tick = do
n <- get
put (n+1)
return n

Now it doesn't matter how much you indent your do block - it's obvious to all concerned that the "n <-" should line up with the "put" - so it'll compile first time - whichever text editor you type it in :)

Of course, it may be that all the experienced haskellers out there are already doing this - but I didn't know about it, so I figured it couldn't hurt to mention it again.

docs, haskell, code

Previous post Next post
Up