简体   繁体   English

Haskell调试-语法问题

[英]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. 我目前正在学习Haskell(最近我的问题太多了,从这个语句开始),并且由于语法错误,在编译程序时遇到了问题,主要是在识别错误​​,理解/解决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: 请记住,这取自于Haskell教程书:

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: GHCI输出错误消息也没有真正帮助:

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. 我目前正在通过Linux终端运行GHCI,并以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? 即类似于NetBeans IDE将提供提示/提示的代码为何语法上不正确的方式。

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" 我要做的最后一件事是在SO上粘贴一个代码块,并成为说“为我解决此问题”的白痴。

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. 缩进4个空格被认为是一个好习惯。 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. 我建议您退出gedit并使用一些适当的代码编辑器。 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. 如果您更喜欢基于GUI的GUI,那么Eclipse似乎可以为Haskell或Emacs / Vi提供更好的支持。 Or if you want to stay with gedit , install proper Haskell plugin for it (I heard it supports well.) 或者,如果您希望继续使用gedit ,请为其安装适当的Haskell插件(我听说它支持得很好)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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