about the define-match-expansion, there are rare materials and example codes to illustrate the concepts. I am having a hard time to “decode” what the documentation says:
(define-match-expander id proc-expr)
(define-match-expander id proc-expr proc-expr)
Binds id to a match expander.
The first proc-expr subexpression must
evaluate to a transformer that
produces a pat for match. Whenever id
appears as the beginning of a pattern,
this transformer is given, at
expansion time, a syntax object
corresponding to the entire pattern
(including id). The pattern is the
replaced with the result of the
transformer.A transformer produced by a second
proc-expr subexpression is used when
id is used in an expression context.
Using the second proc-expr, id can be
given meaning both inside and outside
patterns.
Can anyone give some example codes to illustrate the two usages of the define-match-expander here?
The idea behind match-expander is that you can extend the ‘match’ form to handle new pattern forms of your own design.
So, here’s a (somewhat pointless) example that defines an "aba" match form that matches patterns of one thing followed by another thing followed by the first thing again (hence, "aba"):
The second form allows you to add a separate expansion to be used outside of match patterns, like this:
Caveat: whuffo the extra pair of parens around the pattern? Not sure, sorry.