Sam Estep
Sam Estep

Reputation: 13294

Is Haskell Mode's default indentation setup OK?

I'm learning Haskell through, appropriately enough, the Learn You a Haskell book. Up until the section on guards, none of the code examples have been indented, and I'm wondering how I should properly indent my Haskell code going forward.

The book indents the guard lines in the bmiTell function with four spaces:

bmiTell :: (RealFloat a) => a -> String
bmiTell bmi
    | bmi <= 18.5 = "You're underweight, you emo, you!"
    | bmi <= 25.0 = "You're supposedly normal. Pffft, I bet you're ugly!"
    | bmi <= 30.0 = "You're fat! Lose some weight, fatty!"
    | otherwise   = "You're a whale, congratulations!"

Using Haskell Mode's default indentation settings, I can toggle between zero spaces (which gives a compilation error) and two spaces (which seems to work fine) using TAB and S-TAB:

bmiTell :: (RealFloat a) => a -> String
bmiTell bmi
  | bmi <= 18.5 = "You're underweight, you emo, you!"
  | bmi <= 25.0 = "You're supposedly normal. Pffft, I bet you're ugly!"
  | bmi <= 30.0 = "You're fat! Lose some weight, fatty!"
  | otherwise = "You're a whale, congratulations!"

The docs make it seem like I shouldn't need to do anything in my Emacs config to get the recommended indentation settings, but after a bit of Googling, I haven't been able to find any code examples with two-space indentation. A search for "Haskell style guide" yields this, which recommends four-space indentation like that used in the book.

Is Haskell Mode's default indentation behavior consistent with the way people commonly format their Haskell code? If not, how should I change my Emacs config to be consistent with the most popular indentation scheme?

Edit: Apparently, I was incorrect about indentation in previous examples. The first use of an if statement is indented like this:

doubleSmallNumber x = if x > 100
                        then x
                        else x*2

But I can't seem to get anything that looks like that using TAB and S-TAB in my current setup.

Upvotes: 3

Views: 1139

Answers (2)

Zach Schuermann
Zach Schuermann

Reputation: 1

Here is something I found useful and included in my init.el file (or similar) to force haskell-mode to use four spaces per tab.

(defun haskell-setup ()
    (make-local-variable 'tab-stop-list)
    (setq tab-stop-list (number-sequence 0 120 4))
    (setq indent-line-function 'tab-to-tab-stop)
    (setq haskell-indent-spaces 4))

(add-hook 'haskell-mode-hook 'haskell-setup)

Upvotes: 0

perplexed
perplexed

Reputation: 131

You can look at some major projects developed in haskell like parsec. In the case of parsec, it seems that they use four spaces for guards.

Upvotes: 0

Related Questions