DEV Community

Discussion on: Challenge: Write the recursive Fibonacci algorithm in a different language.

Collapse
 
jvanbruegge profile image
Jan van Brügge • Edited

I have haskell here with the best form of recursion: Self-recursion!

fibs = 1:1:zipWith (+) fibs (tail fibs)
fib n = fibs !! n

Here fibs is an infinite List of all fibonacci numbers, the !! function returns the nth element of the list.

Collapse
 
jvanbruegge profile image
Jan van Brügge

I also have the boring tail recursive version here:

fib n = fibHelper n 1 1
    where fibHelper  0 a  _ = a
          fibHelper !n a !b = fibHelper (n-1) b (a+b)