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
1
vote
1 answer

CPS Transformation [Scala]

I am trying to convert some functions into CPS in Scala using examples i found online in other languages. We only had one lecture on CPS and everything should be really basic but still i don't quite understand how to properly do the…
TolleWurst
  • 51
  • 3
1
vote
1 answer

Continuation-passing-style does not seem to make a difference in Clojure

I have been experimenting with continuation-passing style, as I may need to deal with some non tail-recursive functions soon. Good technique to know, in any case! I wrote a test function, both in Lua and in Clojure; ran the Lua one on my little…
Alex Gian
  • 482
  • 4
  • 10
1
vote
1 answer

How to use a Reader type encoded with continuation passing style

I was under the impression that every value of type a can be described with a rank-2 polymorphic type newtype Id a = Id {runId :: forall r. (a -> r) -> r } in continuation passing style. So I derived the following type to define the Reader…
user6445533
1
vote
1 answer

How to dispatch different types from a ContT?

I want to create a generalized form of IO based on ContT. I created a GADT to represent different IO actions: data Cmd a where PutChar :: Char -> Cmd () GetChar :: Cmd Char I wrote a function that transforms these into IO commands for use in…
1
vote
1 answer

Kotlin Either monad: Refactoring a constructor call to handle CPS

I am an absolute beginner in functional programming and Kotlin, trying to solve exercises that I created from questions I'm asking myself; my current question being "How to put in practice functional programming onto real world applications using a…
Hay
  • 2,246
  • 20
  • 30
1
vote
1 answer

How to get the value from a Scott encoded GADT with type equality constraints?

I am reading the Rank-N-Types section of 24 days of GHC Extensions and came across the following GADT: {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} import Data.Char data Some :: * -> * where SomeInt :: Int -> Some Int SomeChar…
user6445533
1
vote
1 answer

Change a function into CPS, to not use call/cc

I would like to understand how to implement coroutines without call/cc. I began with this little example to understand how to modify a code so that I can use it without call/cc: (define x 0) ( + 2 (call-with-current-continuation (lambda (cont) …
1
vote
1 answer

Understanding this continuation passing style

A function fact is defined to find the factorial in a continuation passing style, function fact(n,myFun) { if(n==1) myFun(1); else fact(n-1,function inner(t0){ myFun(n*t0) }); } called with, fact(4,…
Cloverr
  • 208
  • 3
  • 12
1
vote
1 answer

How to mutate a Vector passed around in CPS style functions?

{-# LANGUAGE ScopedTypeVariables,BangPatterns #-} import qualified Data.Attoparsec.Internal as I import qualified Data.Attoparsec.Internal.Types as T import qualified Data.Vector.Unboxed as UVec import qualified Data.Vector.Unboxed.Mutable as…
Marko Grdinić
  • 3,798
  • 3
  • 18
  • 21
1
vote
1 answer

What is the type of the variables in chainCPS?

I am looking at the continuation passing style tutorial and cannot understand the types in the following function. chainCPS :: ((a -> r) -> r) -> (a -> ((b -> r) -> r)) -> ((b -> r) -> r) chainCPS s f k = s z where -- z :: (a -> r) -> a ->…
Marko Grdinić
  • 3,798
  • 3
  • 18
  • 21
1
vote
4 answers

Is MapReduce one form of Continuation-Passing Style (CPS)?

As the title says. I was reading Yet Another Language Geek: Continuation-Passing Style and I was sort of wondering if MapReduce can be categorized as one form of Continuation-Passing Style aka CPS. I am also wondering how can CPS utilise more than…
Jeff
  • 13,079
  • 23
  • 71
  • 102
1
vote
1 answer

SICP, Continuation Passing Style and Clojure's trampoline

I am working with SICP and exercise 2.29-b gave me the opportunity to have fun with the Continuation Passing Style while traversing mobiles and branches. To make the story short, each mobile has left and right branch, which are composed by a length…
1
vote
1 answer

Scheme Symbols: Continuation Passing Style Examples

In a Wikipedia article about CPS there is the following code snippet, ostensibly from Scheme: (define (pyth& x y k) (*& x x (lambda (x2) (*& y y (lambda (y2) (+& x2 y2 (lambda (x2py2) …
Schemer
  • 1,635
  • 4
  • 19
  • 39
1
vote
1 answer

Haskell CPS: How to implement map and filter functions using Cont monad?

I've been trying to learn CPS, seems I didn't really get my head around it, could you implement basic filter and map using Cont monad?
esengie
  • 71
  • 4
1
vote
0 answers

Trampoline over Recursion for Array and Object iteration

It's probably just the "Friday after 5:00 and I want to go home" effect, but I'm stumped on this problem. I have a recursive iterator method I'll call each: function canIterate(obj) { return (obj && (typeof obj === 'object' ||…