Fibonacci Series

In continuation with the theme of the last lecture we define another infinite series --- the fibonacci series. Ofcourse all of you know the Fibonacci Series. It is defined by the linear recurrence relation Fi+2=Fi+Fi+1. We assume that F0=1 and F1=1 to begin with.

The defintion is straight forward; it is just a one liner, but we use this as an excuse to introduce two standard list functions

Ziping a list

The zip of the list x0,...,xn and y0,,ym is the list of tuples (x0,y0),,(xk,yk) where k is the minimum of n and m.

1
2
3
4
5

> zip :: [a] -> [b] -> [(a,b)]
> zip [] _ = []
> zip _ [] = []
> zip (x:xs) (y:ys) = (x,y) : zip xs ys

The function zipWith is a general form of ziping which instead of tupling combines the values with an input functions.

1
2
3

> zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
> zipWith f xs ys = map (uncurry f) $ zip xs ys

We can now give the code for fibonacci numbers.

1
2

> fib = 1:1:zipWith (+) fib (tail fib)

Notice that the zip of fib and tail fib gives a set of tuples whose caluse are consecutive fibonacci numbers. Therefore, once the intial values are set all that is required is zipping through with a (+) operation.