- Open src/Main.hs in an editor. After the module definition, we will add the definition for our fibonacci number.
- Add the function declaration for fib. Let's assume that we have an infinite list of fibonacci numbers, fiblist. Finding the nth fibonacci number in the list is easy using the List index function (!!):
fib :: Int -> Integer fib n = fiblist !! n
- 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)
- 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 ...