Haskell:解析'where'和后卫的错误

Pil*_*lsy 3 haskell syntax-error

所以,我刚刚开始自学Haskell出自Real World 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))
Run Code Online (Sandbox Code Playgroud)

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

IntParse.hs:12:12: parse error on input `|'
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)

我几乎可以肯定这个错误是由于"where"子句与后续的后卫的相互作用造成的; 注释掉守卫会消除它,就像用等效的"let"子句替换"where"子句一样.我也很确定我必须以某种方式破坏缩进,但我无法理清.

提前感谢任何提示.

Ale*_*nov 11

where不能放在警卫之间.来自Haskell报告中的第4.4.3.1功能绑定.


Jos*_*ger 9

尝试:

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)
Run Code Online (Sandbox Code Playgroud)