0
votes

I'm currently learning Haskell (far too many of my question are starting with this statement lately) and im having issues compiling programs due to syntax errors, mainly in identifying the errors, understanding/resolving the error messages provided by GHC.

For example, its just took me a good while to work out the error in the code below. bear in mind that this was taken from a Haskell tutorial book:

getNums = do
    putStrLn "enter a number (0 to terminate)"
    num <- getLine
    if read num == 0
    then return []
    else do rest <- getNums
    return ((read num :: Int):rest)

The GHCI output error message didn't really help either:

Number.hs:18:17:
    The last statement in a 'do' block must be an expression
      rest <- getNums

I'm currently running GHCI via Linux terminal and compiling manually, coding written in gedit. My question is:

Are there any better environments or setup's available which will provide more in-depth explanation for compile time errors for a beginner like myself?

I.e. something similar to the way the NetBeans IDE will provide hints/tips as to why code is not syntactically correct?

The last thing I want to do is be pasting a code block on SO and being the idiot who says "fix this for me"

EDIT

I appreciate this may not be classed as a very good question because it basically asking peoples opinions.

1
I personally use Sublime Text 3 with the SublimeHaskell plugin, but it can be a bit of a learning curve and has some non-trivial setup, particularly for beginners. I would recommend playing around in the FpComplete IDE if you don't mind having to use an online IDE. If you want some command line tools to help you code, hlint and stylish-haskell are great and installable via cabal. Personally, even though I use SublimeHaskell I prefer to compile and run my code via a terminal, since I just have more direct control over the flow of execution.bheklilr
thanks, im going to have a look at the gedit haskell plugin and will also try sublime. Definitely will be looking at hlint and stylish-haskell. They sound exactly like what i am looking for.Dave0504

1 Answers

7
votes

The problem is with the indentation of your code. Use spaces for indentation. Indenting with 4 spaces is considered as a good practice. This code works perfectly:

getNums = do
    putStrLn "enter a number (0 to terminate)"
    num <- getLine
    if read num == 0
    then return []
    else do rest <- getNums
            return ((read num :: Int):rest)  

Are there any better environments or setup's available which will provide more in-depth explanation for compile time errors for a beginner like myself?

I would suggest you to move out of gedit and use some proper code editors. If you prefer a GUI based one, Eclipse seems to be providing a good support for Haskell or Emacs/Vi for a more advanced one. Or if you want to stay with gedit, install proper Haskell plugin for it (I heard it supports well.)