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

How to best declare a val being a Seq of previously declared vals in the block in Scala?

A quite typical use case: an object (or class) declares several public vals of related types, and it would like to declare an accessor returning a collection containing all of them: case class Ball(dia :Int) object Balls { val tennis = Ball(7) …
Turin
  • 2,208
  • 15
  • 23
0
votes
2 answers

Defining bind in the double barrelled continuation Monad in Haskell

Trying to define bind for this monad in haskell. If the calculation succeeds it calls the SCont continuation, if it fails it calls the FCont continuation. type M a = SCont a -> FCont -> Answer type SCont a = a -> FCont -> Answer type FCont = () ->…
David Furlong
  • 1,343
  • 1
  • 12
  • 15
0
votes
1 answer

Writing an interpreter for a language with breaks

I'm trying to write an interpreter for a simple programming language in Scheme. Right now, I'm writing a procedure to handle while-loops with break statements. To approach this problem, I'm using call/cc. When the language is parsed, it looks like…
0
votes
1 answer

What are good ways to avoid copying if method's caller doesn't need ownership of the data?

Here is the problem I was thinking about lately. Let's say our interface is a member function that returns object which is expensive to copy and cheap to move (std::string, std::vector, et cetera). Some implementations may compute the result and…
sawyer
  • 473
  • 4
  • 15
0
votes
1 answer

Why does this function CPS function work in Chrome but not other browsers?

i have tried to execute this simple code, about CPS. This works in Chrome 43, but not in Firefox and Opera ... what's wrong ? (s.o. Linux Mint 17 ) (function(){ var i = 0; function forloop(){ if(i<10){ document.write(i) i++; …
user2107435
0
votes
1 answer

Trying to get a Search bar to work in laravel 5

I'm trying to get a search bar to go through my database and grab all the numbers that include these 2 random numbers a user will input. The 2 consecutive numbers can be anywhere in the string and then it should grab the whole string. One issue…
Vantheman6
  • 1,719
  • 3
  • 13
  • 13
0
votes
0 answers

passing variable to an email in laravel 5

I can hard code an email an get it to send, but for life of me I can't get a variable to pass through an email or to get it to send to an address entered in my form. When i'm trying to send it to the email entered in the form I'm getting this error…
Vantheman6
  • 1,719
  • 3
  • 13
  • 13
0
votes
2 answers

How can I Implement my own List.partition using tail-recursion?

I'm trying to write my own List.partition function for F# practice. Here's my first (naive) attempt: let rec mypartition_naive func list = match list with | [] -> ([],[]) | head::tail -> let (h1,h2) = mypartition_naive func…
user2023861
  • 8,030
  • 9
  • 57
  • 86
0
votes
2 answers

How to achieve tail call optimization while traversing tree-like structure using continuation-passing style

I try to implement tail call optimization to traverse tree-line structure using continuation-passing style in scala. Unfortunately my previous experience with fsharp does not help much. I have recursive call w/o tail optimization: def…
Akim
  • 8,469
  • 2
  • 31
  • 49
0
votes
2 answers

Performance tools for erlang

When writing a function like factorial: fac(Val) when is_integer(Val)-> Visit = fun (X, _F) when X < 2 -> 1; (X, F) -> X * F(X -1, F) end, Visit(Val, Visit). one cannot…
0
votes
2 answers

Using continuation / CPS to implement tail-recursive MergeSort in OCaml

I am trying to implement a tail-recursive MergeSort in OCaml. Since Mergesort naturally is not tail-recursive, so I am using CPS to implement it. Also my implementation is inspired by Tail-recursive merge sort in OCaml Below is my code let merge…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
0
votes
1 answer

how to transform let expression to cps?

I want to know how to transform a let expression to continuation passing style like this: (let ([a (lambda (x) (+ 1 x))]) (a 4)) Please show me some examples.Thanks.
0
votes
1 answer

converting list to tuple

I have few command-line options (5 for example) and I want to convert them to tuple. The problem is that I expect them to appear in correct order, so tuple can be easily built from list using pattern-match, but in real life options can be provided…
jdevelop
  • 12,176
  • 10
  • 56
  • 112
-1
votes
1 answer

Does it make sense to call continuation-passing style the rescue for functional programming languages?

I am unsure if the following statements make sense: Invoking a non-tail-recursive function in a functional programming languages has, typically, a space efficiency problem due to growing call stacks. Each non-tail-recursive function can be…
zell
  • 9,830
  • 10
  • 62
  • 115
1 2 3
10
11