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 and notes together  slide -- Keyboard shortcut: 't'  Help page about these notes  Alphabetic index  Course home  Lecture 3 - Page 37 : 43
Programming Paradigms
Simulation of other Paradigms and Continuations
Simulating Coroutines in Scheme

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

  • Upon resuming C2 from C1

    • Activate a continuation cont2 in C2 - pass some value v

    • Also send the current continuation cont1 of the resume point in C1

    • C2 receives an aggregate consisting of: (v , cont1)

; 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 
             ))))
    ...                                     
  )
)

Template for resuming C2 from C1.