|
That is interesting but I still don't totally get how DP is different from simple memoization. Your fib example can be expressed as corecursion, and in fact, its an example often used to explain it. val fibsViaUnfold =
unfold((res0, res1)) { case (f0, f1) => Some((f0, (f1, f0 + f1))) }
fibsViaUnfold.take(7).toList shouldBe List(0, 1, 1, 2, 3, 5, 8)
That's scala, but should work anywhere where we can produce a lazy stream of values.Here is a python version from wikipedia def nth_factorial(k):
n, f = 0, 1
while n < k:
n, f = n + 1, f * (n + 1)
yield f
Is DP a techique to arrive at the corecursive solution? |