Chicken Scheme 中的生成器
Generators in Chicken Scheme
这段代码在codepad.org, displaying the numbers 1, 2 and 3 on successive lines before displaying the message "unexpected return", but the same exact code fails using Chicken Scheme at ideone.com提供的Racket Scheme中有效,我不明白Chicken Scheme提供的错误信息。如何让它与 Chicken Scheme 一起使用?
(define-syntax define-generator
(lambda (x)
(syntax-case x (lambda)
((stx name (lambda formals e0 e1 ...))
(with-syntax ((yield (datum->syntax-object (syntax stx) 'yield)))
(syntax (define name
(lambda formals
(let ((resume #f) (return #f))
(define yield
(lambda args
(call-with-current-continuation
(lambda (cont)
(set! resume cont)
(apply return args)))))
(lambda ()
(call-with-current-continuation
(lambda (cont)
(set! return cont)
(cond (resume (resume))
(else (let () e0 e1 ...)
(error 'name "unexpected return"))))))))))))
((stx (name . formals) e0 e1 ...)
(syntax (stx name (lambda formals e0 e1 ...)))))))
(define-generator (test-gen)
(yield 1)
(yield 2)
(yield 3))
(define t (test-gen))
(display (t)) (newline)
(display (t)) (newline)
(display (t)) (newline)
(display (t)) (newline)
Chicken 需要加载 syntax-case
egg 才能支持 syntax-case
。但是ideone没有安装那个egg,所以你不能在那里使用。
因此,为了在 ideone 上进行测试,您必须将宏转换为显式重命名宏。
这段代码在codepad.org, displaying the numbers 1, 2 and 3 on successive lines before displaying the message "unexpected return", but the same exact code fails using Chicken Scheme at ideone.com提供的Racket Scheme中有效,我不明白Chicken Scheme提供的错误信息。如何让它与 Chicken Scheme 一起使用?
(define-syntax define-generator
(lambda (x)
(syntax-case x (lambda)
((stx name (lambda formals e0 e1 ...))
(with-syntax ((yield (datum->syntax-object (syntax stx) 'yield)))
(syntax (define name
(lambda formals
(let ((resume #f) (return #f))
(define yield
(lambda args
(call-with-current-continuation
(lambda (cont)
(set! resume cont)
(apply return args)))))
(lambda ()
(call-with-current-continuation
(lambda (cont)
(set! return cont)
(cond (resume (resume))
(else (let () e0 e1 ...)
(error 'name "unexpected return"))))))))))))
((stx (name . formals) e0 e1 ...)
(syntax (stx name (lambda formals e0 e1 ...)))))))
(define-generator (test-gen)
(yield 1)
(yield 2)
(yield 3))
(define t (test-gen))
(display (t)) (newline)
(display (t)) (newline)
(display (t)) (newline)
(display (t)) (newline)
Chicken 需要加载 syntax-case
egg 才能支持 syntax-case
。但是ideone没有安装那个egg,所以你不能在那里使用。
因此,为了在 ideone 上进行测试,您必须将宏转换为显式重命名宏。