fibonacci series what Iam is missing ?

Rustom Mody rustompmody at gmail.com
Tue Mar 24 00:13:33 EDT 2015


On Tuesday, March 24, 2015 at 8:33:24 AM UTC+5:30, Chris Angelico wrote:
> Mathematics doesn't like defining sequences, except by defining
> functions, and so it has to convert the concept of "defining the
> Fibonacci sequence" into "defining a function F(N) which returns the
> Nth Fibonacci number", and that's where the double recursion comes
> from. It's not an inherent part of the sequence, which is, uhh,
> sequential.

Here is a pure 'recursive-data' definition of fib.
[Of course assuming you can buy that generators are data not functions]

==================
from itertools import tee

def fib():
    yield 0
    yield 1
    f, fn = tee(fib())
    next(fn)
    yield from (x+y for (x,y) in zip(f, fn))

The fact that this is data may be more clear if you see it as a rendering
(uglification) of the Haskell list-definition

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)



More information about the Python-list mailing list