2 ms·
Your code is doing something pretty different from the examples above. You're just calculating the nth fibonacci number, not creating an infinite fibonacci _seq
by fireflies_ 8y ago
Your code is doing something pretty different from the examples above. You're just calculating the nth fibonacci number, not creating an infinite fibonacci _sequence_. Eliding the explicit recursive structure is the point of the exercise!
Admittedly Python's facilities for doing that with some mutable state are pretty nice:
from itertools import islice
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
list(islice(fib(), 10)) == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
- andrewprock 8y agoI wasn't speaking to the goal of generating a sequence that can be lazily evaluated. I was speaking to readability.