Questions tagged [continuation-passing]

In functional programming, continuation-passing style (CPS) is a style of programming in which control is passed explicitly in the form of continuation function(s).

In CPS, in addition to receiving arguments as usual, each function also receives an additional function to be used as a continuation, so that instead of being returned as usual, a value is rather passed to this continuation function, as the function's final act.

Continuation functions are not expected to return in a normal sense, but rather return their values in the same manner, by further calling other continuation functions.

A computation either simply calls its continuation function, or constructs a new, more complex one, to express more complex patterns of computation, like recursion, or iteration.

Sometimes more than one continuation are used for each call, as e.g. with two continuations, one for a "successful" computation, another for a "failed" one.

Example:

nlog(n, x, onSuccess, onFailure) =            (* iterated logarithm *)
     onFailure(x)                      , if x <= 0 or n < 0
     onSuccess(x)                      , if n == 0
     onSucess(log(x))                  , if n == 1
     nlog(n-1, x, 
           x => onSuccess(log(x)),            (* new success continuation constructed *)
           onFailure                          (* same failure continuation *)
           )                           , otherwise
164 questions
2
votes
1 answer

call/cc example in JavaScript

Now that ES6 supports Proper Tail Call, and since according to Wikipedia, "In any language which supports closures and proper tail calls, it is possible to write programs in continuation-passing style and manually implement call/cc.", we should be…
2
votes
1 answer

Translating call/comp to equivalent CPS style in JS

I read lots of article already on how to translate call/cc into equivalent CPS style and kinda know the basic already but currently I don't understand how racket perform transformation with call/comp (a.k.a call-with-composable-continuation) and…
2
votes
1 answer

Scheme equal-tree procedure

Given a new implementation of tree in Scheme using list: (define make-tree list) (define add-subtree cons) (define make-leaf (lambda (x) x)) (define empty-tree? empty?) (define first-subtree car) (define rest-tree cdr) (define composite-tree?…
2
votes
0 answers

Get a subtree by breadth-first index using continuation-passing style

This question is a follow-up on How do I get a subtree by index?. That question deals with depth-first indexing (for which I have provided a depth-first continuation-passing style solution). This question here is about breadth-first indexing, and…
Flux
  • 9,805
  • 5
  • 46
  • 92
2
votes
0 answers

Can you write a generator in continuation passing style in Javascript?

I'm trying to understand continuations and continuation passing style better. They're supposed to be good for writing generators, iterators, coroutines, and the like. I know Javascript already has builtin generators, but I thought building a…
ziggurism
  • 2,264
  • 2
  • 16
  • 23
2
votes
1 answer

How to convert a variation of ackermann function to support tail call?

I'm currently solving a problem which is to implement a variation of ackermann function in scala with tail call optimization support so that the stack does not overflow. The problem is, I cannot find a way to tail-call optimize it. I'm told…
2
votes
1 answer

Continuation passing style procedures

I have troubles understanding how procedures in continuation passing style "remember" values from previous function calls. As an example I have the following procedure that will filter the even values from a list: (define (get-pairs alist proc) …
fabrizzio_gz
  • 223
  • 1
  • 9
2
votes
1 answer

How does call/cc work with the CPS transformation from "Lisp in Small Pieces"?

The book Lisp in Small Pieces demonstrates a transformation from Scheme into continuation passing style (chapter 5.9.1, for those who have access to the book). The transformation represents continuations by lambda forms and call/cc is supposed to…
MB-F
  • 22,770
  • 4
  • 61
  • 116
2
votes
2 answers

Is the function argument to `call/cc` written in CPS?

The parameter of call/cc is a procedure taking as its argument a continuation. Is the procedure written in CPS?
user10082400
2
votes
2 answers

How does a function written in CPS makes a number of things explicit?

https://en.wikipedia.org/wiki/Continuation-passing_style says A function written in continuation-passing style takes an extra argument: an explicit "continuation", i.e. a function of one argument. When the CPS function has computed its result…
user10082400
2
votes
1 answer

Implementing Cont Applicative instance

I got the following error when implementing the Applicative instance for Cont. Couldn't match expected type ‘r’ with actual type ‘Cont r b’ ‘r’ is a rigid type variable bound by ... newtype Cont r a = Cont {(>>-) :: (a -> r) -> r} instance…
user28522
  • 311
  • 1
  • 7
2
votes
1 answer

product of list using continuation

my goal is to write a times function of type int list -> int that that takes a list of ints and, using continuations, returns an int such that that int = multiplication of all the ints in the int list. for instance times [2;2;3] returns 12. Here is…
2
votes
1 answer

F# How to Flatten a Binary Search Tree

I have a tree, structured as : type 'a Tree =| Leaf of 'a| Branch of 'a Tree * 'a Tree I am using continuation-passing style tail recursion over my tree and trying to flatten it. let rec loop tree k acc = match tree with | Leaf v -> v :: acc …
2
votes
2 answers

On nested CPS suspension types

Let's begin with the familiar type of CPS suspended computations, (a -> r) -> r, spelled as Cont r a in mtl-speak. We know that it is isomorphic to a as long as it is kept polymorphic in r. If we nest this type, we get this rank-3 type: forall r.…
duplode
  • 33,731
  • 7
  • 79
  • 150
2
votes
2 answers

Continuation Passing Style in ocaml

I am a bit confused on the concept. So I have the following function let rec sumlist lst = match lst with | [] -> 0 | (h::t) -> h + (sumlist t) With continuation, it can be written as let rec cont_sumlist…
john maniaci
  • 119
  • 1
  • 1
  • 7