所以,我刚刚开始自学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))
当我将其加载到GHCi 6.6中时,我收到以下错误:
IntParse.hs:12:12: parse error on input `|'
Failed, modules loaded: none.
我几乎可以肯定这个错误是由于“where”子句与后续守卫的相互作用造成的;注释掉守卫会消除它,就像用等效的“let”子句替换“where”子句一样。我也非常肯定我必须以某种方式破坏缩进,但我无法理清。
提前感谢任何提示。
答案 0 :(得分:11)
where
不能放在警卫之间。来自Haskell报告中的4.4.3.1 Function bindings段。
答案 1 :(得分: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)