我正在尝试将yield
和yield from
从Python移植到Scheme。
这是我已经完成的实现:
(define (coroutine routine)
(let ((current routine)
(status 'new))
(lambda* (#:optional value)
(let ((continuation-and-value
(call/cc (lambda (return)
(let ((returner
(lambda (value)
(call/cc (lambda (next)
(return (cons next value)))))))
(if (equal? status 'new)
(begin
(set! status 'running)
(current returner))
(current (cons value returner)))
(set! status 'dead))))))
(if (pair? continuation-and-value)
(begin (set! current (car continuation-and-value))
(cdr continuation-and-value))
continuation-and-value)))))
这个实现的问题在于它必须被调用的方式看起来不像Python的yield
。
(define why (call/cc (lambda (yield)
(format #t "love me or leave me!")
(yield "I leave!")
;; the program never reach this part
(format #t "it probably left :("))))
(format #t "return actually populates WHY variable\n")
(format #t "WHY: ~a\n")
除此之外,每次我需要重新启动协同程序时,我必须 let
一个新的return
变量才能使协同程序exit
。基本上,我觉得语法太冗长了。还有另一种语法更清晰吗?
应该可以将yield
和 send
值赋予协程。以下是必须如何使用协程的示例:
(define-coroutine (zrange start step)
"compute a range of values starting a START with STEP between
each value. The coroutine must be restarted with 0 or more, which
is added to the step"
(let loop ((n start))
(loop (+ n step (yield n)))))
(coroutine-map (zrange 0 10) '(1 100 1000 10000 100000))
;; => 0 110 1120 11130 111140
在上文中,1
被忽略,然后100
,1000
为send
生成器。我已经基于@sylwester代码完成了一个实现,但我对宏有麻烦:
(define (make-generator procedure)
(define last-return #f)
(define last-value #f)
(define last-continuation (lambda (_) (procedure yield)))
(define (return value)
(newline)(display "fuuu")(newline)
(call/cc (lambda (continuation)
(set! last-continuation continuation)
(set! last-value value)
(last-return value))))
(lambda* (. rest) ; ignore arguments
(call/cc (lambda (yield)
(set! last-return yield)
(apply last-continuation rest)))))
(define-syntax define-coroutine
(syntax-rules ()
((_ (name args ...) body ...)
(define (name args ...)
(make-generator
(lambda (yield)
body ...))))))
(define-coroutine (zrange start step)
(let loop ((n start))
(loop (+ n step (yield n)))))
(display (map (zrange 0 10) '(1 100 1000 10000 100000)))
答案 0 :(得分:4)
这样的事情:
(define (make-generator procedure)
(define last-return values)
(define last-value #f)
(define (last-continuation _)
(let ((result (procedure yield)))
(last-return result)))
(define (yield value)
(call/cc (lambda (continuation)
(set! last-continuation continuation)
(set! last-value value)
(last-return value))))
(lambda args
(call/cc (lambda (return)
(set! last-return return)
(if (null? args)
(last-continuation last-value)
(apply last-continuation args))))))
像这样使用:
(define test
(make-generator
(lambda (collect)
(collect 1)
(collect 5)
(collect 10)
#f)))
(test) ; ==> 1
(test) ; ==> 5
(test) ; ==> 10
(test) ; ==> #f (procedure finished)
现在我们可以将内部包装成一个宏:
(define-syntax (define-coroutine stx)
(syntax-case stx ()
((_ (name . args) . body )
#`(define (name . args)
(make-generator
(lambda (#,(datum->syntax stx 'yield))
. body))))))
请注意define-coroutine
是使用syntax-case实现的,因为我们需要使yield
不卫生。
(define-coroutine (countdown-from n)
(let loop ((n n))
(if (= n 0)
0
(loop (- (yield n) 1)))))
(define countdown-from-10 (countdown-from 10))
(define (ignore procedure)
(lambda ignore
(procedure)))
(map (ignore countdown-from-10) '(1 1 1 1 1 1)) ; ==> (10 9 8 7 6 5)
;; reset
(countdown-from-10 10) ; ==> 9
(countdown-from-10) ; ==> 8
;; reset again
(countdown-from-10 100) ; ==> 99
答案 1 :(得分:2)
向@Sylwester致以良好的答复。
困难的部分是使yield
对生成器函数可用。 datum->syntax
创建一个语法对象,并要求您提供另一个语法对象,从中获取新对象的上下文。在这种情况下,我们可以使用与传递给宏的函数具有相同上下文的stx。
如果人们发现它有用,我会使用一个简单的版本:
(define-syntax (set-continuation! stx)
"Simplifies the common continuation idiom
(call/cc (λ (k) (set! name k) <do stuff>))"
(syntax-case stx ()
[(_ name . body)
#`(call/cc (λ (k)
(set! name k)
. body))]))
(define-syntax (make-generator stx)
"Creates a Python-like generator.
Functions passed in can use the `yield` keyword to return values
while temporarily suspending operation and returning to where they left off
the next time they are called."
(syntax-case stx ()
[(_ fn)
#`(let ((resume #f)
(break #f))
(define #,(datum->syntax stx 'yield)
(λ (v)
(set-continuation! resume
(break v))))
(λ ()
(if resume
(resume #f)
(set-continuation! break
(fn)
'done))))]))
其用法示例:
(define countdown
(make-generator
(λ ()
(for ([n (range 5 0 -1)])
(yield n)))))
(countdown)
=> 5
...
(countdown)
=> 1
(countdown)
=> 'done
(countdown)
=> 'done
答案 2 :(得分:1)
这里有一种方法。如果你使用的是guile,你应该使用提示(它们比使用guile的完整延续快两个数量级):
How to implement Python-style generator in Scheme (Racket or ChezScheme)?