[plt-scheme] auxiliary macros

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Aug 3 10:45:46 EDT 2007


BTW, here is the proper code:

;; --- using syntax-case ---

(define-syntax (define/y stx)
   (syntax-case stx ()
     [(_ (name arg ...) body ...)
      (with-syntax ((yield-name (datum->syntax-object stx 'yield)))
        (syntax
         (define (name arg ...)
           (define (yield-name x)
             (control resume-here
              (set! name (lambda () (prompt (resume-here 'dummy))))
              x))
           (prompt body ...))))]))

(define/y (step)
   (yield 1)
   (yield 2)
   (yield 3)
   'finished)

(equal? '(1 2 3) (list (step) (step) (step)))

And the two old ones. I left a bug in that I also had on the web page  
but nobody discovered it. LESSON: never 'test' by running things at  
the top-level prompt. It IS a prompt. -- Matthias

(define-syntax define/y.version0
   (syntax-rules ()
     [(_ yield-name (name arg ...) body ...)
      (define (name arg ...)
        (define exit-with #f)
        (define (switch-control-context th)
          (call/cc
           (lambda (k)
             (set! exit-with k)
             (th))))
        (define (yield-name x)
          (call/cc
           (lambda (resume-here)
             (set! name (lambda () (switch-control-context (lambda ()  
(resume-here 'dummy)))))
             (exit-with x))))
        (switch-control-context (lambda () body ...)))]))

;; --- using control.ss ---

(require (lib "control.ss"))

(define-syntax define/y.version1
   (syntax-rules ()
     [(_ yield-name (name arg ...) body ...)
      (define (name arg ...)
        (define (yield-name x)
          (control resume-here
             (set! name (lambda () (prompt (resume-here 'dummy))))
             x))
        (prompt body ...))]))

(define/y.version1 yield (step)
   (yield 1)
   (yield 2)
   (yield 3)
   'finished)

(equal? '(1 2 3) (list (step) (step) (step)))










On Aug 3, 2007, at 8:35 AM, Vladimir Zlatanov wrote:

>  2. How can I use the macro system to eliminate the 'yield-name' part
> of the macro?
> One solution that works, but I do have a few problems with it.
> I had to break the hygiene, which in this case might not be that  
> bad, but it is not nice. More worringly for me, I'm not sure why  
> does it really work the way it works. It is a problem with my  
> understanding of macros, but that might pass with time =)
>
> What would be a hygienic solution?
>
> ===========================
> (require (lib "control.ss"))
>
> (define-syntax (make-gen stx)
>   (syntax-case stx ()
>     [(mkg (name arg ...) body ...)
>      (syntax-case (datum->syntax-object (syntax mkg) 'yield) ()
>        [yield (syntax
>                (define (name arg ...)
>                  (define (control-state) body ...)
>                  (define (yield value) (control resume-here (set!  
> control-state resume-here) value))
>                  (lambda () (prompt (control-state)))))])]))
>
> (make-gen (make-step)
>           (yield 1)
>           (yield 2)
>           (yield 3)
>           'finished)
>
> (define step (make-step))
>
> (step)(step)(step)(step)(step)
>
> ===========================
>
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.