简单 Haskell Fibonacci 实现中的解析错误
我尝试制作函数的迭代/尾递归版本来计算斐波那契数列的第 n 个数字,但出现解析错误(可能是不正确的缩进)< /代码>。为什么会发生这种情况?我正在使用的代码:
fib n
| n < 2 = n
| otherwise = fibhelper 0 1 2 n
where fibhelper a b curr num
| curr == num = a + b
| curr < num = fibhelper b (a+b) (curr+1) num
为了清楚起见,我试图理解错误 - 为什么会发生,应该如何纠正 - 而不是试图有效地实现 fib
(我理解流行的 <例如,code>zipWith 实现已经此处)。
谢谢!
I've tried to make an iterative/tail-recursive version of a function to compute the nth number of the Fibonacci sequence, but I'm getting parse error (possibly incorrect indentation)
. Why is this happening? The code I'm using:
fib n
| n < 2 = n
| otherwise = fibhelper 0 1 2 n
where fibhelper a b curr num
| curr == num = a + b
| curr < num = fibhelper b (a+b) (curr+1) num
To be clear, I'm trying to understand the error - why it's happening, how it should be corrected - and not trying to implement fib
efficiently (I understand the popular zipWith
implementation here already, for instance).
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
防护部分必须相对于函数名称缩进至少一个字符。因此,以下工作有效:
The guard part has to be indented at least one character relative to the function name. The following thus works: