[racket] Expanding a macro inside a cond
I might be wrong, but I don’t think you can do that with the existing cond.
What you can do though is make your own version of cond that does that. I actually did something like that here for a for/cond-clause form:
https://github.com/AlexKnauth/my-cond
If you wanted you could define a version of cond that had something like a “cond expander”, and then define $: as one.
Or I could add cond expanders to https://github.com/AlexKnauth/my-cond.
On Nov 1, 2014, at 4:15 PM, Andrew Kent <andmkent at indiana.edu> wrote:
> 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
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users