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)
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
Your fib example can be expressed as corecursion, and in fact, its an example often used to explain it.
That's scala, but should work anywhere where we can produce a lazy stream of values.Here is a python version from wikipedia
Is DP a techique to arrive at the corecursive solution?