Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am trying to learn continuations and use them to implement coroutines in Scheme.

I have two procedures (coroutines) a and b, and I switch between them in the following way:

;; c is a continuation.
(define (a c)
   ...
   ;; Switch to the other coroutine.
   (call/cc c)
   ...


(define (b c)
   ...
   ;; Switch to the other coroutine.
   (call/cc c)
   ...

I start the execution with

(a b)

I have tested a small example and, indeed, the execution switches back and forth between the two procedures until one of the two terminates without calling the continuation it was given as an argument.

I have two questions:

  1. Is my example an appropriate / idiomatic implementation of coroutines using continuations?
  2. Is tail-call optimization mandatory for implementing coroutines with continuations? In my solution, switching too often between the two coroutines might cause a stack overflow if Scheme did not provide TCO. Are there other solutions that do not require TCO?

EDIT

Basile's comment seems to be sufficient to answer my question. The background of the question is that I would like this approach to implement coroutines in Python, where I do not have TCO.

share|improve this question
    
Your first question is answerable with "yes" or "no." Are you sure that's your question, or would you prefer a bit more explanation? –  Robert Harvey Jul 6 at 19:16
    
Your second question seems trivially answerable with "Not if the stack doesn't go too deep." –  Robert Harvey Jul 6 at 19:16
    
@RobertHarvey: I have tried my example and it seems to work: the execution switches back and forth between the two coroutines, but I am not sure whether this solution is idiomatic in Scheme. Maybe there is a different and more idiomatic implementation of coroutines using continuations. –  Giorgio Jul 6 at 19:18
    
Yes to both questions. –  Basile Starynkevitch Jul 6 at 19:19
    
Also, I am not sure if there does not exist an alternative implementation that does not require TCO: I think such an implementation does not exist, but I am not sure. I would like to use a similar solution in another language and I hoped there does exist a solution that works without TCO. –  Giorgio Jul 6 at 19:20

1 Answer 1

up vote 1 down vote accepted

As I commented:

  • yes, your example is an appropriate implementation of coroutines using continuations.

  • yes, tail-call optimization is mandatory for implementing coroutines with continuations.

share|improve this answer
    
Thanks, I am not an expert with continuation and it is good to know that I am on a good track. –  Giorgio Jul 12 at 7:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.