【问题标题】:Haskell: Parsing error with 'where' and a guardHaskell:使用“where”和警卫解析错误
【发布时间】:2009-08-22 15:23:05
【问题描述】:

所以,我刚刚开始从Real World Haskell这本书中自学 Haskell,在做其中一个练习的过程中,我编写了以下代码:

step acc ch | isDigit ch = if res < acc   
                              then error "asInt_fold: \
                                         \result overflowed"
                              else res
                      where res = 10 * acc + (digitToInt ch)
            | otherwise  = error ("asInt_fold: \
                                  \not a digit " ++ (show ch))

当我将它加载到 GHCi 6.6 中时,出现以下错误:

IntParse.hs:12:12: parse error on input `|'
Failed, modules loaded: none.

我几乎可以肯定该错误是由于“where”子句和后续保护的相互作用造成的;注释掉守卫会消除它,就像用等效的“let”子句替换“where”子句一样。我也很确定我一定是以某种方式破坏了缩进,但我不知道是怎么回事。

提前感谢您的任何提示。

【问题讨论】:

    标签: haskell syntax-error


    【解决方案1】:

    where 不能放在守卫之间。来自 Haskell 报告中的 4.4.3.1 Function bindings 段。

    【讨论】:

      【解决方案2】:

      试试:

      step acc ch
          | isDigit ch = if res < acc then error "asInt_fold: result overflowed" else res
          | otherwise  = error ("asInt_fold: not a digit " ++ (show ch))
          where res = 10 * acc + (digitToInt ch)
      

      【讨论】:

        猜你喜欢
        • 2014-06-19
        • 1970-01-01
        • 2023-04-02
        • 2017-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-12
        相关资源
        最近更新 更多