In The Scheme Programming Language by Kent Dybvig (4th edition) section 3.4, he describes very clearly what continuation passing style is. For the why he gives two reasons:
Since the first reason can also be done using the values
procedure and the second using case-lambda
, I'm not clear the advantages of using continuation passing style. Could someone show me some examples of where continuation passing style is appropriate, where it makes the code better, clearer, etc.?
Continuation passing style makes the control flow of programs more explicit as every procedure has the power to change the execution of the remainder of the program, contrast this to the traditional model in which procedures have no control over the behavior of the program once they return to their caller.
Continuation-Passing-Style, Tail Recursion, and Efficiency is not tail recursive, because the recursive call fact(n-1) is not the last thing the function does before returning. Instead, the function waits for the result of the recursive call, then multiples that by the value of n.
A continuation is a callback function k that represents the current state of the program's execution. More precisely, the continuation k is a function of one argument, namely the value that has been computed so far, that returns the final value of the computation after the rest of the program has run to completion.
A continuation implements (reifies) the program control state, i.e. the continuation is a data structure that represents the computational process at a given point in the process's execution; the created data structure can be accessed by the programming language, instead of being hidden in the runtime environment.
Dybvig uses the explicit continuations in this section to motivate having call/cc
as part of the language. The main point is made near the end of the section when he mentions that writing code without it requires a global tranformation of all code that is used, including functions that you call. So in Scheme you usually build your own construct using macros, and continuations are one of these useful constructs -- but you cannot implement them via macros since they implement only local transformations.
But using a CPS style directly can still be useful: for example, as he mentions, you could write a function that has more than one continuation, possibly with different arrities -- like a parsing function that receives a single-input function to send a parses value to and a nullary failure function to call when parsing fail (and this function might abort with an error or backtrack and try using other parsing rules). Another possible use is when you want to control exactly what goes into the continuation rather than letting call/cc
grab the full context.
There also the obvious case of writing code in a language that has no first-class continuation, making CPSed code your only choice. An example of that would be lots of node.js programs that use IO and pretty much force you to write code in explicit CPS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With