0

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.

Anandamide
  • 243
  • 1
  • 15
  • http://stackoverflow.com/help/mcve, http://stackoverflow.com/help/how-to-ask. in particular, always include the output and error message, if any. – Will Ness Mar 31 '16 at 11:43
  • Its not about the output of this function, although this is correct scheme code. I am talking about coroutines of this form in general and what are the best, idiomatic ways of implementing coroutines in scheme. – Anandamide Mar 31 '16 at 13:41
  • I will add the output though – Anandamide Mar 31 '16 at 13:42

0 Answers0