[racket] define-match-expander

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Sun Dec 29 15:35:38 EST 2013

2013/12/29 Alexander D. Knauth <alexander at knauth.org>:
> I'm not really sure why this works either, but try this:
>
> (define-syntax (:match stx)
>  (syntax-case stx ()
>    [(:match val-expr [pat . more] ...)
>     (with-syntax ([(new-pat ...) (for/list ([pat-stx (in-list (syntax->list
> #'(pat ...)))])
>                                    (datum->syntax pat-stx `(:pat
> ,(syntax->datum pat-stx))))])
>     #'(match val-expr [new-pat . more] ...))]))
>
> (check-equal? (:match '(42 x) [(list n:num s:sym) (list n s)])
>               (match '(42 x) [(:pat (list n:num s:sym)) (list n s)]))

Nice!

/Jens Axel

Posted on the users mailing list.