Analysis error in a simple implementation of Haskell Fibonacci

I tried to make an iterative / tail-recursive version of the function to calculate the number n th of the Fibonacci sequence, but I get parse error (possibly incorrect indentation). Why is this happening? The code I'm using is:

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 am trying to understand the error - why this is happening, how it should be fixed - and not try to implement it effectively fib(I understand the popular implementation zipWith here , for example).

Thank!

+3
source share
1 answer

The security part must be indented by at least one character relative to the function name. So the following works:

fib n
    | n < 2 = n
    | otherwise = fibhelper 0 1 2 n
    where fibhelper a b curr num
           | curr == num = a + b  -- moved one character to the left.
           | curr < num = fibhelper b (a+b) (curr+1) num
+8
source

Source: https://habr.com/ru/post/1780465/


All Articles