Here's a simple function that calculates the n'th Fibonacci number:
fib :: Integer -> Integer
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
The function in your question works like this:
Assume you already had an infinite list of the Fibonacci numbers:
[ 1, 1, 2, 3, 5, 8, 13, .... ]
The tail
of this list is
[ 1, 2, 3, 5, 8, 13, 21, .... ]
zipWith
combines two lists element by element using the given operator:
[ 1, 1, 2, 3, 5, 8, 13, .... ]
+ [ 1, 2, 3, 5, 8, 13, 21, .... ]
= [ 2, 3, 5, 8, 13, 21, 34, .... ]
So the infinite list of Fibonacci numbers can be calculated by prepending the elements 1
and 1
to the result of zipping the infinite list of Fibonacci numbers with the tail of the infinite list of Fibonacci numbers using the +
operator.
Now, to get the n'th Fibonacci number, just get the n'th element of the infinite list of Fibonacci numbers:
fib n = fibs !! n
The beauty of Haskell is that it doesn't calculate any element of the list of Fibonacci numbers until its needed.
Did I make your head explode? :)