0

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.

2
  • 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. Commented Jun 25, 2014 at 20:55
  • 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. Commented Jun 26, 2014 at 22:19

1 Answer 1

6

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

the code actually doesn't compile in ghci, it states rest is out of scope. As I said in the question I knew the error and had fixed it. it was as you said due to the indentations. Putting rest <- getNums on a new line cleared it up. The error message didn't state this though, which is my bug bear!!! Thanks for the tips on eclipse/emacs/Vi. im not a big fan of eclipse (could be a net beans prejudice) however I will definitely try out the gedit haskell plugin. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.