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 ^_^