2 ms·
The standard way to expel stated mutable state us to push it into function parameters and returned values. With Fibonacci numbers, you cab just compute two if
by nine_k 2y ago
The standard way to expel stated mutable state us to push it into function parameters and returned values.
With Fibonacci numbers, you cab just compute two if them outright:
def fib_(n: int) -> tuple[int, int]:
if n == 0:
return (1, 1)
prev, this = fib_(n - 1)
return (this, this + prev)
def fib(n): return fib_(n)[0]
Now there is no mutable state that survives between function calls, the performance is linear.
With true memoization though accessing a previously computed value.would be constant time.
- narski 2y ago>The standard way to expel stated mutable state us to push it into function parameters and returned values. This is precisely what I did in my Hoon solution :) However, I wasn't aware that this approach is the standard way, and I'm glad to have learned that! Thanks