So I have the following "cor.scm"
(define (range-gen str stp inc)
(call/cc
(lambda (yield)
(yield (lambda ()
(let ([i str])
(cond ([< str stp]
(set! str (+ i inc))
i)
(else #f))))))))
(define continue? (range-gen 0 10 1))
(define it (range-gen 0 10 1))
(define (routine-a cc)
(let loop ()
(cond ((continue?)
(printf "Blah ~A~N" (it))
(set! cc (call/cc cc))
(loop)))))
(define (routine-b cc)
(let loop ()
(cond ((continue?)
(printf "Blar ~A~N" (it))
(set! cc (call/cc cc))
(loop)))))
Routine-a and b are coroutines. I would like to have a third(nth) routine that would go in cycle, or some other control scheme, i.e a->b->c...->c until the end conditions are met, or even a->b->a->c..a->c.
EDIT:
The output from calling (routine-a routine-b)
is:
; loading cor.scm ...
1> (routine-a routine-b) ;Starts coroutines, notice how they use the continue?
Blah 0 ;generator to share state, only going through the cycle
Blar 1 ;5 times each out of the total 10 continue? calls that
Blah 2 ;will return true.
Blar 3
Blah 4
Blar 5
Blah 6
Blar 7
Blah 8
Blar 9
2> (routine-a routine-b) ;returns nothing, the continue generator now will only return #f
3> ;which is the expected behaivor :)
Also, whats the best way of passing data between the two coroutines? Right now I would use a variable in the scope of both functions and allow them to mutate its state(which is what im doing with the continue
generator to keep track of what cycle this is) but I would like to just pass the values if possible.