Scheme 使用两个以上的协程
所以我有下面的“cor.scm”Scheme 使用两个以上的协程,scheme,coroutine,continuations,Scheme,Coroutine,Continuations,所以我有下面的“cor.scm” 另外,在两个协同路由之间传递数据的最佳方式是什么?现在我想在这两个函数的作用域中使用一个变量,并允许它们改变其状态(这是我使用continue生成器所做的,以跟踪这是什么循环),但如果可能的话,我只想传递值。。特别是,始终包括输出和错误消息(如果有)。这与此函数的输出无关,尽管这是正确的方案代码。我通常谈论这种形式的协同路由,以及在scheme中实现协同路由的最佳、惯用的方法是什么 (define (range-gen str stp inc) (call/
另外,在两个协同路由之间传递数据的最佳方式是什么?现在我想在这两个函数的作用域中使用一个变量,并允许它们改变其状态(这是我使用
continue
生成器所做的,以跟踪这是什么循环),但如果可能的话,我只想传递值。。特别是,始终包括输出和错误消息(如果有)。这与此函数的输出无关,尽管这是正确的方案代码。我通常谈论这种形式的协同路由,以及在scheme中实现协同路由的最佳、惯用的方法是什么
(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)))))
; 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 :)