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
6
votes
2 answers

Why does traversing a large binary tree result in a stack overflow even when using continuation-passing style?

Chapter 9 of the book Expert F# 3.0 shows how to use continuation-passing style to avoid stack overflows when traversing binary trees. I have written tree traversal code that is almost identical to the code from the book, but I get stack overflows…
6
votes
2 answers

call-with-current-continuation - state saving concept

After reading The Seasoned Schemer I felt I understood call/cc properly. But, after seeing some WOW tricks with call/cc I found I was wrong. (define cc 0) (define (f) (call/cc (lambda (k) (set! cc k) 3))) (+ 1 2 4 (f)) ;…
Shakil Ahamed
  • 587
  • 3
  • 18
5
votes
2 answers

Palindrome and Danvy's remark on direct style

Here is some code deciding whether a list is a palindrome in n+1 comparisons, in "direct style" pal_d1 :: Eq a => [a] -> Bool pal_d1 l = let (r,_) = walk l l in r where walk l [] = (True,l) walk l (_:[]) =…
nicolas
  • 9,549
  • 3
  • 39
  • 83
5
votes
2 answers

Practical continuation passing style in C?

I have been writing a lot of C code recently, and I've been running into a problem similar to the one I ran into with Go where I have a lot of code that looks like this: if (foo() != 0) { return -1; } bar(); which is similar to the constant if…
michaelsnowden
  • 6,031
  • 2
  • 38
  • 83
5
votes
3 answers

Event listeners with Scala continuations

Suppose I have to write some GUI code as follows: widget1.addListener(event1 => handle1(event1) widget2.addListener(event2 => handle2(event2) widget3.addListener(event3 => handle3(event3)) ) ) How would you write it in CPS-style…
Michael
  • 10,185
  • 12
  • 59
  • 110
5
votes
1 answer

Continuation Passing Style Computation Expression

Can you implement CPS using computation expressions in F#? Brian McNamara's blog gives this solution: type ContinuationBuilder() = member this.Return(x) = (fun k -> k x) member this.ReturnFrom(x) = x member this.Bind(m,f) = (fun k -> m (fun…
J D
  • 48,105
  • 13
  • 171
  • 274
5
votes
1 answer

Real-world usage of Lua co-routines/continuation serialization to simplify async logic?

The Pluto library for Lua claims to be able to serialize Lua co-routines. I interpret this as meaning 'serializeable continuations', which is an important feature for making asyncronous programming writeable in a syncronous style. For example, a…
5
votes
1 answer

Attempting to use continuation passing style to avoid stack overflow with minimax algorithm

Summary of my objective: Figure out how to use continuation-passing style to avoid a stack overflow when using an algorithm I believe cannot be made tail-recursive. Alternatively, find a way to make the function tail-recursive. Details: I am new to…
5
votes
3 answers

F# continuation-based tail recursion

Can someone clarify the need for acc "" when terminating a continuation-based tail-recursive function like in the following example: let rec repeat_cont i s acc = if i = 0 then acc "" else repeat_cont (i-1) s (fun x -> acc(s + x)) repeat_cont 4…
Kagemand Andersen
  • 1,470
  • 2
  • 16
  • 30
5
votes
1 answer

Is it possible to implement coroutines using only LISP primitives?

First, I'm a LISP newbie. What I want to get is a cooperative micro-threading feature. And this can be gained with coroutine. As I know, Scheme supports coroutines via continuations. However, not all Scheme implementation may have continuations. …
eonil
  • 83,476
  • 81
  • 317
  • 516
5
votes
2 answers

Haskell Continuation passing style index of element in list

There's a series of examples I'm trying to do to practice Haskell. I'm currently learning about continuation passing, but I'm a bit confused as to how to implement a function like find index of element in list that works like this: index 3 [1,2,3]…
5
votes
1 answer

continuation in common lisp by macros -- regarding an implemetation in OnLisp

In On Lisp, p. 267, Paul Graham provides an implementation of continuation passing macros: (setq *cont* #'identity) (defmacro =lambda (parms &body body) `#'(lambda (*cont* ,@parms) ,@body)) (defmacro =defun (name parms &body body) (let ((f…
5
votes
1 answer

Rewriting code with continuations

I have some code that evaluates primitive programs. Program is a list of statements (expression, block, return statement). Result of evaluation is last evaluated expression. Also evaluator should properly treat return statement (i.e. stop evaluation…
sergeyz
  • 1,308
  • 2
  • 14
  • 23
5
votes
2 answers

Non-blocking IO with Haskell

Possible Duplicate: What is the Haskell response to Node.js? How can I watch multiple files/socket to become readable/writable in Haskell? Is it possible to write a Haskell program that performs IO in a non-blocking way like in nodejs? For…
mmaroti
  • 109
  • 2
  • 6
5
votes
1 answer

memoize continuation passing style function

I'm wondering if there is a way to implement a generic "memoize" functional (as in a function with a function as input and a function as output, as python's decorators) capable of handling also cps-style functions. for a normal function (as in "the…
Vito De Tullio
  • 2,332
  • 3
  • 33
  • 52
1 2
3
10 11