concept recursion in category kotlin

appears as: recursion, recursion
Functional Programming with Kotlin

This is an excerpt from Manning's book Functional Programming with Kotlin.

In order to adapt our existing program to demonstrate HOFs, we should introduce some new behaviour. We will do so by adding a new function that calculates the nth factorial. To write this simple function, we will need to take a short detour by showing how loops are written in a purely functional way. We do this by introducing recursion.

Listing 5.6. The foldRight function on Stream is used to generalize recursion.
fun <A, B> Stream<A>.foldRight(
    z: () -> B,
    f: (A, () -> B) -> B
): B = #1
    when (this) {
        is Cons -> f(this.head()) { tail().foldRight(z, f) } #2
        else -> z()
    }
The Joy of Kotlin

This is an excerpt from Manning's book The Joy of Kotlin.

4.1 Corecursion and recursion

Corecursion is composing computation steps by using the output of one step as the input of the next one, starting with the first step. Recursion is the same operation but starts with the last step. Let’s take as an example a list of characters that you want to join into a string representation.