[plt-scheme] composing syntax-case macros?

From: YC (yinso.chen at gmail.com)
Date: Fri Jan 16 17:33:47 EST 2009

Hi all -

I am running into issues composing a couple of syntax-case macros, and not
sure where things go wrong. When I try to write a `cond-it` using an working
`if-it` and `when-it`, I was unable to capture `it` within `cond-it`.

;; the working if-it & when-it

(define-syntax (if-it stx)
  (syntax-case stx ()
    ((if-it test? then else)
     (with-syntax ((it (datum->syntax #'if-it 'it)))
       #'(let ((it test?))
           (if it then else))))))

(define-syntax (when-it stx)
  (syntax-case stx ()
    ((~ test? exp exp2 ...)
     (with-syntax ((it (datum->syntax #'~ 'it)))
       #'(let ((it test?)) (when it exp exp2 ...))))))

;; the non-working cond-it
(define-syntax (cond-it stx)
  (syntax-case stx (else)
    ((cond-it (else exp exp2 ...))
     #'(begin exp exp2 ...))
    ((cond-it (test? exp exp2 ...))
     #'(when-it test? exp exp2 ...))
    ((cond-it (test? exp exp2 ...) cond1 cond2 ...)
     #'(if-it test? (begin exp exp2 ...)
              (cond-it cond1 cond2 ...)))))

I tried to capture the `it` binding as well - but it still doesn't work.

(define-syntax (cond-it stx)
  (syntax-case stx (else)
    ((cond-it (else exp exp2 ...))
     #'(begin exp exp2 ...))
    ((cond-it (test? exp exp2 ...))
     (with-syntax ((it (datum->syntax #'cond-it 'it)))
       #'(when-it test? exp exp2 ...)))
    ((cond-it (test? exp exp2 ...) cond1 cond2 ...)
     (with-syntax ((it (datum->syntax #'cond-it 'it)))
       #'(if-it test? (begin exp exp2 ...)
                (cond-it cond1 cond2 ...))))))

When I finally write `cond-it` without using `if-it` and `when-it`, it
worked.

(define-syntax (cond-it stx)
  (syntax-case stx (else)
    ((cond-it (else exp exp2 ...))
     #'(begin exp exp2 ...))
    ((cond-it (test? exp exp2 ...))
     (with-syntax ((it (datum->syntax #'cond-it 'it)))
       #'(let ((it test?)) (when it exp exp2 ...))))
    ((cond-it (test? exp exp2 ...) cond cond2 ...)
     (with-syntax ((it (datum->syntax #'cond-it 'it)))
       #'(let ((it test?))
           (if it (begin exp exp2 ...)
               (cond-it cond cond2 ...)))))))

What am I missing here?  How can `cond-it` be composed of `if-it` and
`when-it`?

Any tips are appreciated.  Thanks,
yc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090116/36fda0f4/attachment.html>

Posted on the users mailing list.