Fibonacci sequence: Difference between revisions

Content added Content deleted
Line 7,399: Line 7,399:
(0, 1)
(0, 1)
[1 .. n]</syntaxhighlight>
[1 .. n]</syntaxhighlight>

==== As an unfold ====
Create an infinite list of integers using an unfold. The nth fibonacci number is the nth number in the list.

<syntaxhighlight lang="haskell">import Data.List (unfoldr)

fibs :: [Integer]
fibs = unfoldr (\(x, y) -> Just (x, (y, x + y))) (0, 1)

fib n :: Integer -> Integer
fib n = fibs !! n</syntaxhighlight>


=== With matrix exponentiation ===
=== With matrix exponentiation ===