continuation-passing style (CPS) does: makes control flow explicit via callbacks

Continuations pass control explicitly via callbacks

Image: kees torn, CC BY-SA 2.0, via Wikimedia Commons

continuation-passing style (CPS) does: makes control flow explicit via callbacks

Continuations pass control explicitly via callbacks

CPS makes several aspects of programming explicit: procedure returns, intermediate values, order of argument evaluation, and tail calls. This explicitness can lead to clearer and more predictable code.

Example

A simple CPS function in Scheme might look like this: ```scheme (define (cps-func x) (lambda (k) (k (+ x 1)))) ``` Invoking `(cps-func 5)` would return a continuation that adds 1 to its argument.

Remember this

Understanding CPS helps programmers write clearer, more predictable code by making control flow explicit.

Related concepts

Swipe through 100 ML concepts daily

Open Pocket Polymath