Search code examples
schemechicken-scheme

Generators in Chicken Scheme


This code works in the Racket Scheme provided at 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, and I don't understand the error message that Chicken Scheme provides. How can I make it work with 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)

Solution

  • Chicken requires the syntax-case egg to be loaded in order to support syntax-case. However, ideone does not have that egg installed, so you can't use it there.

    So, in order to test on ideone, you're going to have to convert your macro to an explicit-renaming one.