Fibonacci Sequence and Long numbers.

Alex Martelli aleaxit at yahoo.com
Sat Oct 7 07:06:08 EDT 2000


"Grant Griffin" <g2 at seebelow.org> wrote in message
news:39DE3A2D.2E047F3D at seebelow.org...
    [snip]
> BTW, I was pondering the other day if there would be a way to do Fibo
> using Python 2.0's new "list comprehension" feature.  I still don't
> comprehend those well enough to come up with the answer (in the whopping
> 10 minutes I spent on it ;-), but maybe one of you folks can.

I don't think so, because Python list comprehensions are not lazy.


In Haskell, you basically can:

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

but the key difference is not so much with the syntax sugar, or
even the scoping -- it's with *when* the right-side references
to the fibonacci list are evaluated.  Python is "strict" (in FP
terms): it fully evaluates the arguments before calling the
function.  Haskell is "lazy": it does evaluation just when the
result is really needed.

Compared to this key semantic issue, distinctions such as
Haskell's zipWith (+) versus Python's map(operator.add are
almost trivial...:-).

You can get a taste of lazy programming, and other FP approaches,
in Python, by downloading Bryn Keller's functional.py from
    http://sourceforge.net/projects/xoltar-toolkit/

But that just supplies the "bricks" -- you can use it to do some
FP with Python if you already know what you're doing.  Maybe a
tutorial on the issue (why might you want to do that, etc) would
be nice...


Alex






More information about the Python-list mailing list