什么是解析错误?怎么删除它?

时间:2014-12-29 05:49:13

标签: haskell

在这个脚本中,

approximation :: Int -> (String, Int)
approximation x
  | (x<20000) && (19000<=x) && (numDigits<x) = (text1, x-numDigits)
  | (x<20000) && (19000<=x) && (numDigits>x) = (text1, numDigits-x)
  | (x<19800) && (x>=19700) && (numDigits<x) = (text2, x-numDigits)
  | (x<19800) && (x>=19700) && (numDigits>x) = (text2, numDigits-x) 
  | otherwise                                = ("far from no. of Digits", 0)  
      where
        text1 = "at 1000th place of no. of reallyBig, abosolute error="
        text2  = "at 100th place of  no. of reallyBig, nearly Exact, absolute error"

我为函数text1, text2输入了2个定义:approximation。但是,编译器GHCI表示Parse error on input '='中有text2。我对这个问题很困惑。

1 个答案:

答案 0 :(得分:3)

您有缩进的标签和空格。这是一个糟糕的计划,因为您的编辑器和ghc可以完全不同地考虑制表符。我认为你的编辑器显示标签为(最多)4个字符,而ghc认为标签为(最多)8个空格。我会在标签上写<-->,在最后两行写一个空格.

<-->....text1 = "at 1000th place of no. of reallyBig, abosolute error="
<--><-->text2  = "at 100th place of  no. of reallyBig, nearly Exact, absolute error"

您的编辑器显示它的方式。如果我把ghc的8个空格标签放进去,你就得到了

<-------->....text1 = "at 1000th place of no. of reallyBig, abosolute error="
<--------><-------->text2  = "at 100th place of  no. of reallyBig, nearly Exact, absolute error"

你得到解析错误。

如果你坚持空间,这是最简单的。更改编辑器的设置。

如果只使用空格,则无法解决此问题,因为编辑器必须按照编译器的思考方式显示它。

我的编辑器让我指定当我按Tab键时,它应该插入一个选项卡显示的空格数,所以我使用它,这对于一个4的tabstop是安全的。如果你的编辑器可以这样做,请使用那个选择。 (如果没有,请考虑在编程时获得一个更聪明的编辑器。

我的编辑器也有自动缩进和outdent,其中下一行复制前一行的空白缩进 - 这避免了问题。如果您的编辑器支持它,请启用它,因为它可以节省您的工作量,并且您不太可能得到解析错误。 (当我按下退格键时,我的编辑器会删除回到上一级别的缩进,这很不错。)

几乎所有编辑都可以更改显示标签的方式。如果你不能使用空格作为标签,你应该将tabstop改为8,因为它与ghc相匹配,并且你不太可能得到这个错误,但你仍然是最好使用空格。