[racket] Expanding a macro inside a cond

From: Andrew Kent (andmkent at indiana.edu)
Date: Sat Nov 1 16:15:00 EDT 2014

Good afternoon!

Macro newbie here,

I wanted to define a macro $: so that I could write this:

(cond
  [$: p (Point x y) => (+ x y)])

and it would expand into this:

(cond
    [(if (Point? p) p #f)
     => (match-lambda [(Point x y) (+ x y)])]
    [else #f])

I gave it a try:

(define-syntax ($: stx)
  (syntax-case stx (=>)
    [(_ arg (struct-name field ...) => body)
     (with-syntax ([pred? (format-id stx "~a?" #'struct-name)])
       #'[(if (pred? arg)
              arg
              #f)
          =>
          (match-lambda ([(struct-name field ...) body]))])]))


But this macro expands (or attempts to) after cond has expanded... which is
not what I wanted.

Is there a quick fix for this? Or can someone point me towards a particular
spot in the documentation where I might educate myself?

I know about match (we're great friends)---I was just toying around with
this to see if I could get cond to look pretty w/ structs as well.

Best,
Andrew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20141101/05f12e02/attachment.html>

Posted on the users mailing list.