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

Which simplest evaluation model explains call/cc?

TL;DR: What does call/cc do, (semi-)formally speaking? The long version: I'm vaguely familiar with continuations and call/cc, but I don't have a strong formal understanding. I would like one. In the SICP video lectures we are presented with the…
Jonas Kölker
  • 7,680
  • 3
  • 44
  • 51
1
vote
2 answers

Can the function argument to `call/cc` equivalently either invoke the continuation or return without invoking the continuation?

The Scheme Programming Language says Scheme allows the continuation of any expression to be captured with the procedure call/cc. call/cc must be passed a procedure p of one argument. call/cc constructs a concrete representation of the current …
user10082400
1
vote
1 answer

How to use continuations in scheme?

I'm trying to understand call/cc operator in Scheme. I'm planing of implementing this in my JavaScript lisp. This is my simple code: (letrec ((x 0) (f (lambda (r) (set! x r) (display (r 20)) …
jcubic
  • 61,973
  • 54
  • 229
  • 402
1
vote
1 answer

Control flow in a Scheme program having call-with-current-continuation

I am new to Scheme programming and have been trying to understand the control flow of the programs having call-with-current-continuation in them. To be more specific, I want to know when the call to any continuation is invoked where is the control…
New User
  • 31
  • 3
1
vote
3 answers

How to make the function in a lambda be called in another function used inside the body of lambda

I want to using call/cc to simulating the exception handling statement: try...(throw)...exception. Here's the code: (define (month n) ; check if it's a month number (1-12) (if (or (not (integer? n)) (< n 1) (> n 12)) (throw -1) (display…
Tan Kian-teng
  • 81
  • 1
  • 10
1
vote
0 answers

Continuations in Scheme

Here is the code: (define (first lst) (call/cc (lambda (exit) (for-each (lambda (x) (call/cc (lambda (nested) (exit (cons x nested))))) lst)))) (define (main) …
BigMeister
  • 361
  • 2
  • 11
1
vote
1 answer

Transform coroutine in continuation passing style

I have this coroutine example (define p1 (lambda (continuation) (display "1") (newline) (p1 (call/cc continuation)))) (define p2 (lambda (continuation) (display "2") (newline) (p2 (call/cc continuation)))) (p1 p2) And I…
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

how does call/cc jump from two subrutine

I had written a scheme interpreter in scala, it really works, but I still can't figure out how does it switch between subroutines. for example: (call/cc (lambda (k) (k 1) (display 2))) I suppose the display procedure should be executed, but…
user6102088
1
vote
0 answers

What makes the action of this lwp program different between directly loading in commend line and using load procedure?

I use guile 2.0.13 as my scheme interpreter and i wrote file 3.3.3.scm as follow: (define lwp-list '()) (define quit-k #f) (define lwp (lambda (thunk) (set! lwp-list (append lwp-list (list thunk))))) (define start (lambda () (if (not…
linux40
  • 368
  • 1
  • 9
1
vote
2 answers

Execute the following call/cc expression

I use racket and I got the result 4 for following simple code: (let/cc done ((let/cc esc (done (+ 1 (let/cc k (esc k))))) 3)) and I was going to execute this code step-by-step. First, I changed the first let/cc into the…
User
  • 37
  • 4
1
vote
1 answer

How to make just part of a macro hygienic

I'd like to have a version of lambda, called lambda-r, from within which you can return. An example: (+ ((lambda-r () (return 1) 2)) 5) This would give the value 6. Although you might expect the value to be 7, it's 6 because 1 is returned…
Cam
  • 14,930
  • 16
  • 77
  • 128
1
vote
1 answer

Funky call/cc usage. How does it work?

Consider following definitions. I am using Racket. (define fact/k (lambda (n k) (cond ((zero? n) (call/cc (lambda (f) (k f)))) (else (* n (fact/k (sub1 n) k)))))) (define five (call/cc (lambda (k) (fact/k 5 k)))) Now if it…
chamibuddhika
  • 1,419
  • 2
  • 20
  • 36
1
vote
1 answer

Type of a continuation in Racket and determining current continuation

What is the type of a continuation in Racket? And how to determine current continuation looking at a call/cc invocation? (e.g : Is it a correct strategy to assume that the current continuation is what follows immediately after call/cc closing…
chamibuddhika
  • 1,419
  • 2
  • 20
  • 36
1
vote
0 answers

Ruby Enumerator using callcc doesn't terminate

I'm having a hard time figuring out why Enumerable#take never terminates given my enumerator: require 'continuation' fib = Enumerator.new do |yielder| c, x, y = callcc {|cc| [cc, 0, 1]} yielder << x c.call c, y, x+y end # this works as I'd…
Charles
  • 6,199
  • 6
  • 50
  • 66