A macro for new pattern matchings

In the last post, I explained very quickly how pattern matching works in Racket and how to create new patterns.

However, there is a little boilerplate code that bothers me. It’s a “macro-thing” that’s not directly related create new patterns. So, I create a macro to remove this macro from the view. Err … something tells me that’s not the sanest approach, but … =/

Here is a new pattern to match things nested in lists:

My macro is simple(istic), just an extra line and some generalization:

And it can be used like this:

This macro is a nested one, i.e., it’s a macro that generates another macro. It’s not a problem for Racket (it can handle that easily), but it IS a little weird for human minds.

Notice, in both cases, the “…” is not interpreted by the pattern matching, it’s expanded by the macro. We need to use “(… …)” to escape it, so it doesn’t get interpreted.

I’m sure this macro can be vastly improved, especially the ellipsis escaping! But it was fun to create it anyway. If someone with better macro-fu than I could help me improve it, it would be greatly appreciated ^_^

Written by

Just a common guy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store