简体   繁体   中英

Haskell debugging - syntax issues

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM