continuationthe
Continuation-passing style (often abbreviated as CPS) is a programming paradigm and transformation technique used in functional programming. It involves rewriting a function so that its continuation—the sequence of operations to be performed after the function completes—is passed explicitly as an argument. This approach shifts the control flow of the program, allowing for more flexible and composable code structures.
In CPS, a function does not return a value directly but instead invokes its continuation with the
One of the key benefits of CPS is its ability to simplify complex control structures, such as
The transformation to CPS involves systematically replacing each function call with an explicit continuation. For example,
While CPS can improve modularity and control flow, it may introduce additional complexity in some cases, particularly