Thanks @rokitna, good to know it does in fact work correctly. Apologies for my embarrassing mathematics skills, haha.
Nob question’: I understand the macro vaguely however I can’t find any use case in real projects
I mean, what could be a real usage of a macro for an opensource project? Any input appreciated :grin: :racket-flat:
If you experience repetition, a pattern in some kind in your program, it is time to give the pattern a name.
If you experience that you have a lot of similar expressions, you make a function.
The arguments of the function represent what’s different and the body is what’s common (i.e. the pattern).
If you experience a pattern in your program that consists of declarations, bindings, control structures or other patterns that aren’t expressions — you need a macro.
A simple example: You have a file where you define all your structs.
These structs needs to be provided, so you write:
(provide (struct-out foo) (struct-out bar) (struct-out baz))
You realize, that you repeat (struct-out name) and want to write (provide-structs foo var baz) instead.
You now need to write a macro.
I was working on a macro today to allow specifying routes for a web application. After some help on the mailing list, I have the following: https://gist.github.com/lojic/842619ab2c59a2c96960f9ccd30bc136 See the routes.rkt
file for an example of using the macro to specify a few web application routes.
the macro code is in the router-stx.rkt file
If you look in the manual there are tags on entries that they are either ‘procedure’ or ‘syntax’. Almost all of the ones marked ‘syntax’ are macros written in Racket.
Some of the useful builtin macros are match
and for
(and all of the for
variants.)