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