Haskell:使用';其中';还有一个警卫
因此,我刚开始从《真实世界哈斯克尔》一书中自学哈斯克尔,在做其中一个练习的过程中,我编写了以下代码:Haskell:使用';其中';还有一个警卫,haskell,syntax-error,Haskell,Syntax Error,因此,我刚开始从《真实世界哈斯克尔》一书中自学哈斯克尔,在做其中一个练习的过程中,我编写了以下代码: step acc ch | isDigit ch = if res < acc then error "asInt_fold: \ \result overflowed" else r
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))
我几乎可以肯定,这个错误是由于“where”子句和后续保护的交互作用造成的;注释掉这个保护就消除了它,用等效的“let”子句替换“where”子句也是如此。我也很确定我一定是不知怎么弄坏了压痕,但我不知道是怎么弄的
提前感谢您提供的任何提示。请尝试:
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)
步骤acc ch
|isDigit ch=如果res
其中
不能放置在防护装置之间。摘自Haskell报告中的段落
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)