Questions tagged [callcc]

call-with-current-continuation (abbreviated as call/cc) is a control operator in functional programming

The function call-with-current-continuation, commonly abbreviated call/cc, is a control operator in functional programming that originated in the Scheme programming language. It takes a function f as its only argument and applies f to the current continuation (a first-class value represented as a function).

85 questions
8
votes
2 answers

Checkpoint and restore the heap in Ruby

Ruby's callcc captures the current continuation, which can be subsequently called to restore the control, but not the data. I would like to capture the current continuation along with the current image of the memory. It seems to me that capturing…
Gowtham Kaki
  • 357
  • 1
  • 10
8
votes
2 answers

Can we describe Clojure's core.async as 'continuation passing style'?

In Clojure's core.async library we see a macro that creates a state machine that wraps around go blocks to create channels that handle blocking IO. This appears to be modelling on C#'s async and on Go-lang's goroutines. In The Seasoned Schemer -…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
8
votes
1 answer

How is callCC implemented in strict functional languages?

Consider the following example in Haskell of a function quux along with the definitions of the continuation monad and callCC. instance Monad (Cont r) where return x = cont ($ x) s >>= f = cont $ \c -> runCont s $ \x -> runCont (f x)…
user782220
  • 10,677
  • 21
  • 72
  • 135
7
votes
2 answers

Continuation (call/cc) in Scheme

I need to understand Continuations in Scheme for my upcoming exams and I have no idea about continuations at all. Can anyone please suggest me sources of how to go about learning continuations? Regards, darkie
name_masked
  • 9,544
  • 41
  • 118
  • 172
7
votes
4 answers

Understanding Haskell callCC examples

I am having trouble understanding the answers to a previous question. I'm hoping that an explanation of the following will clarify things. The following example comes from fpcomplete import Control.Monad.Trans.Class import…
user782220
  • 10,677
  • 21
  • 72
  • 135
7
votes
4 answers

Specifics of call/cc

This is related to What is call/cc?, but I didn't want to hijack this question for my own purposes, and some of its arguments like the analogy to setjmp/longjmp evade me. I think I have a sufficient idea about what a continuation is, I think of it…
ThomasH
  • 22,276
  • 13
  • 61
  • 62
6
votes
2 answers

Can "if" be implemented using "call/cc"?

I've been told that "call/cc" can be used to implement arbitrary control flow constructs so I'm trying to implement all such constructs using "call/cc" but I'm having trouble. Assuming I didn't have "if" how would I implement it using…
6
votes
5 answers

I can't seem to wrap my mind around call/cc in Scheme

Does anyone have a good guide as to how it works? Something with visual aids would be nice, every guide I've come across all seem to say the same thing I need a fresh take on it.
Brian
  • 61
  • 1
6
votes
0 answers

Save the current continuation in SMLofNJ

I was reading through this funny page explaing continuations in racket. They present code to save the current continuation of a computation (and use this trick later to implement backtracking). The code looks as follows: (define (currcc) (call/cc…
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
3 answers

What distinguishes a continuation from a function?

Continuation describes what happens next with some value, right? Isn't that just a function that takes a value and does some computation? (+ (* 2 3) 5) the continuation of (* 2 3) is (+ _ 5) (define k (lambda (v) (+ v 5))) What is the point of…
ayhid
  • 475
  • 2
  • 10
5
votes
2 answers

Scheme: how does a nested call/cc work for a coroutine?

I am looking at the following example for a coroutine from http://community.schemewiki.org/?call-with-current-continuation: (define (hefty-computation do-other-stuff) (let loop ((n 5)) (display "Hefty computation: ") (display n)…
hxngsun
  • 105
  • 6
5
votes
1 answer

Seasoned Schemer's get-first, get-next, and waddle functions

(define get-first (lambda (l) (call-with-current-continuation (lambda (here) (set! leave here) (waddle l) (leave (quote ())))))) (define get-first (lambda (l) (call-with-current-continuation (lambda…
4
votes
1 answer

How to apply callcc so that it provides an escape continuation mechanism for use with the continuation monad

I try to implement the continuation monad in Javascript to handle continuation passing style and asynchronous control flows. Here is my continuation monad for learning: // auxiliary functions const log = prefix => x => console.log(prefix,…
user6445533