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:
https://gist.github.com/ruliana/5aee7f979e36fabea8f99d9b6fdafd61
My macro is simple(istic), just an extra line and some generalization:
https://gist.github.com/ruliana/484a4ee0b1c4e2e3084319eec84c737a
And it can be used like this:
https://gist.github.com/ruliana/7842dd555e0a674b0281c3f7fea64d73
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 ^_^