3 ms·
That code is not at all readable, even knowing Haskell. Even the recursive python version with memoization is a bit difficult to read, but least it has the rec
by andrewprock 8y ago
That code is not at all readable, even knowing Haskell.
Even the recursive python version with memoization is a bit difficult to read, but least it has the recurrence relation in it.
vals = {}
def fib(n):
if n < 2:
return n
if n in vals.keys():
return vals[n]
val = fib(n-1) + fib(n-2)
vals[n] = val
return val
- fireflies_ 8y agoYour 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.