Haskell People Don't Know How to Indent
I've been learning more about Haskell recently, and I think the language itself is fine. But, I really can't stand how Haskell people indent their code. Take this for example (from the Haskell wikibook):
doGuessing num = do
putStrLn "Enter your guess:"
guess <- getLine
if (read guess) < num
then do putStrLn "Too low!"
doGuessing num
else if (read guess) > num
then do putStrLn "Too high!"
doGuessing num
else putStrLn "You Win!"
For code this simple, it still makes my eyes hurt. I think it's because of these reasons:
Totally inconsistent indentation
There are 3 spaces between
doGuessingandputStrLn, but 2 betweenifandthen. Now it's 8 spaces betweenthenanddoGuessing? And for theelse ifbranch, it's 7 spaces now, like what???putStrLnis so uglyWhy can't people use
printLinefor the function name?Lack of empty lines to separte different parts
After using the "correct" indentation and alignment style, changing functions names, and adding some empty lines, the code becomes:
doGuessing num = do
printLine "Enter your guess:"
guess <- getLine
if (read guess) < num then do
printLine "Too low!"
doGuessing num
else if (read guess) > num then do
printLine "Too high!"
doGuessing num
else
printLine "You Win!"
That's much better.
If you like more nesting, you can write it like this:
doGuessing num = do
printLine "Enter your guess:"
guess <- getLine
if (read guess) < num
then do
printLine "Too low!"
doGuessing num
else if (read guess) > num
then do
printLine "Too high!"
doGuessing num
else
printLine "You Win!"