Recursive functions are functions that call themselves. Let's see an example of a recursive function, getState:
fun getState(state: State, n: Int): State = if (n <= 0) state // 1 else getState(nextState(state), n - 1)
They are an important part of the functional programming style, but the problem is that each recursive function call needs to keep the return address of the previous function on the stack. When an application recurses too deeply (there are too many functions on the stack), StackOverflowError is thrown. This limitation presents a very serious problem for recurrence usage.
A classic solution for this problem was to use iteration instead of recurrence, but this approach is less expressive:
fun getState(state: State, n: Int): State { var state = state for (i in 1..n) { state = state...