Continuationpassing
Continuation-passing style (CPS) is a programming style in which control is passed explicitly as a continuation. Instead of returning a result to the caller, a function receives an additional argument, the continuation, which it invokes with the result. The continuation represents the rest of the computation and determines what happens next. In CPS, functions do not return in the traditional sense; they always pass their result to the provided continuation.
Mechanics: in CPS, every function has an extra parameter, typically named k or cont, for the continuation.
def add_cps(a, b, k): k(a + b)
A small composition in CPS would chain continuations to represent subsequent steps without returning to a
Applications: CPS is foundational in compiler design and language theory. It enables advanced control-flow features, such
Advantages and limitations: CPS makes control flow explicit, simplifying certain optimizations and features (e.g., non-local exits,
Related concepts include delimited continuations and the continuation monad, which provide structured ways to manage and