How to do it...

  1. Open src/Main.hs in an editor. After the module definition, we will add the definition for our fibonacci number.
  2. Add the function declaration for fib. Let's assume that we have an infinite list of fibonacci numbers, fiblistFinding the nth fibonacci number in the list is easy using the List index function (!!):
        fib :: Int -> Integer
fib n = fiblist !! n
  1. Implement the function fiblist. The function fiblist is obviously a list of integers. Define fiblist as follows:
        fiblist :: [Integer]
fiblist = 0 : 1 : zipWith (+) fiblist (tail fiblist)
  1. This is an efficient implementation (O(n)) of the fibonacci number calculator. We can test it by calculating the 10000th fibonacci number:
        main :: IO ()
main = do
let fib10k = fib 10000
putStrLn $ "10000th fibonacci number is " ++ (show fib10k)
  1. If you build and run the application, it will print a very large number (thanks to big integer support in Haskell):
  1. You can check whether our number is correct by checking the 1000th fibonacci number at http://www.bigprimes.net/archive/fibonacci/10000/.