Lecture overview -- Keyboard shortcut: 'u'  Previous page: Coroutines -- Keyboard shortcut: 'p'  Next page: A simpel producer and consumer -- Keyboard shortcut: 'n'  Lecture notes - all slides together  Annotated slide -- Keyboard shortcut: 't'  Alphabetic index  Help page about these notes  Course home    Simulation of other Paradigms and Continuations - slide 37 : 43

Simulating Coroutines in Scheme

Coroutines can be simulated in Scheme by (heavy) use of continuations

; In C1:
(let ((v ...)           ; value to C2
      (cont2 ...)       ; continuation in C2
     )
  (let ((from-c2        ; value from C2 - received upon the next resume from C2
          (call-with-current-continuation
             (lambda (cont1)              ; captured continuation in C1
                 (cont2 (cons v cont1))   ; resume C2 - pass v and cont1 
             ))))
    ...                                     
  )
)