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
0
votes
1 answer

Antlr adding additional parameters to visit methods, accommodations for continuation passing style

I am trying to do continuation passing style for a simple programming language made with antlr. Typically you would have an eval procedure that takes as arguments the expression to be evaluated as well as the continuation of that expression. I guess…
0
votes
2 answers

Why would wrapping recursive function in promise improves the performance?

I was playing around with recursion and looking at the benchmark of different implementation. function plusOne(xs) { if (xs.length <= 0) return [] return [xs[0], ...plusOne(xs.slice(1))] } function plusOne2(xs) { if (xs.length <= 0) return…
Jason Yu
  • 311
  • 2
  • 12
0
votes
1 answer

Continuation Passing Style in Haskell with Binary Tree

I'm just learning CPS and I'm trying to pass the following program to this style mirror Void = Void mirror (Node x left right) = Node x (mirror right) (mirror left) From what I understand I have to pass the continuation the base case and in the…
Márquez
  • 91
  • 7
0
votes
2 answers

What are the continuation passing style conversion rules?

I am trying to understand continuation passing style conversion. I am trying to build a Scheme to C compiler and I want to use continuation passing style. Whether or not continuation passing style is the right way to do this can you guys explain me…
chez93
  • 131
  • 6
0
votes
1 answer

Relationship between a higher order programming language, and contuation passing style?

Suppose I have a higher order language defined with the (rough) BNF (using Lisp notation): c ::= constants v ::= variables e ::= c | v | (if e e e) | (e e_1 e_2 ... e_n) | (fn [v_1 v_2 ... v_n] e) In the above, the option (e e_1 e_2 ... e_n)…
bzm3r
  • 3,113
  • 6
  • 34
  • 67
0
votes
2 answers

F# continuation recursion bug

I'm having an issue with a recursive function that runs into a stack overflow on larger data sets so I've attempted to rewrite the function to use continuous recursion but to say I'm new to this would be an understatement. In the below example the…
Scott Wilson
  • 649
  • 1
  • 7
  • 17
0
votes
1 answer

How to await an Array of async Tasks without blowing the stack?

A large array of Tasks blows the stack if you want to await them all, even if the array fold is stack-safe, because it yields a large deferred function call tree: const record = (type, o) => (o[type.name || type] = type.name || type, o); const…
0
votes
2 answers

Map-filter CPS version in Scheme

I'm wondering, is there any implementation of map-filter in CPS version in Scheme? example: (map-filter square even? '(1 2 3 4)) -> '(4 16)
0
votes
1 answer

How can we convert between a program using `call/cc` and a program using functions written in CPS?

The Scheme Programming Language says It turns out that any program that uses call/cc can be rewritten in CPS without call/cc, but a total rewrite of the program (sometimes including even system-defined primitives) might be necessary. What are…
user10082400
0
votes
0 answers

Do the continuation created by `call/cc` and the continuation used for making a call to a function in CPS both make jump?

When a continuation is called as a procedure, does some jump occur? For example, I have seen two use cases of continuation: Are the continuation created by call/cc and the continuation used for making a call to a function in CPS the same concept,…
user10082400
0
votes
1 answer

CPS style for linked-list style function calls

I am trying to convert the following code to continuation passing style. The code originally returned a String so I changed it to call a continue lambda that takes a String. I am wondering how to remove the next field and use CPS style…
Node.JS
  • 1,042
  • 6
  • 44
  • 114
0
votes
1 answer

Is there macros for rewrite CPS?

For example I have two async methods (get-a 10 (lambda (a) (get-b a (lambda (b) (display b))) but I want to write something similar to (define (a (get-a 10))) (define (b (get-b a))) (display b)
0
votes
0 answers

Visual basic Passing values from last datagrid form and update it to new Form

I am having a problem with my code. The problem is I cannot fetch the ID. Here is my code. Private Sub UpdatePurchase_Load(sender As Object, e As EventArgs) Handles MyBase.Load txtID.Text = PassName txtName.Text = PassID …
0
votes
1 answer

Continuation-passing style sum elements

i try to transform this code into the CPS form: def sum ( lst : List [ Int ]) : Int = lst match { case Nil => 0 case first :: rest => first + sum ( rest ) } def sumC1(lst : List [ Int ], k : Int => Unit ) : Unit = lst…
0
votes
1 answer

How do spray's directives correspond to continuations?

I fail to see how spray's directives correspond to continuation passing style (CPS). More specifically, a continuation is (a -> r) -> r (in Haskell), but I cannot find where is this type ((a -> r) -> r) when using spray directives (which have the…
jhegedus
  • 20,244
  • 16
  • 99
  • 167
1 2 3
10
11