concept scope in category functional programming

This is an excerpt from Manning's book Functional Programming with Kotlin.
The way we write loops functionally, without mutating a loop variable, is with a recursive function. Here we’re defining a recursive helper function inside the body of the factorial function. This function will typically handle recursive calls that requires an accumulator parameter, or some other signature change that the enclosing function does not have. Such a helper function is often called
goorloopby convention. In Kotlin, we can define functions inside any block, including within another function definition. Since it’s local, thegofunction can only be referred to from within the scope of the body of the factorial function, just like a local variable would. The definition offactorialfinally just consists of a call togowith the initial conditions for the loop. The arguments togoare the state for the loop. In this case, they’re the remaining valuen, and the current accumulated factorialacc. To advance to the next iteration, we simply callgorecursively with the new loop state (here,go(n-1, n*acc)), and to exit from the loop, we return a value without a recursive call (here, we returnaccin the case thatn ⇐ 0).
Listing 9.14. Implementation of
scopethat records errors usingpushfun <A> scope(msg: String, pa: Parser<A>): Parser<A> = { state -> pa(state).mapError { pe -> pe.push(state, msg) } }