Many Lisp-family languages have a little bit of syntax sugar for things like addition or comparison allowing more than two operands, if
optionally omitting the alternate branch etc. There would be something to be said for implementing these with macros, that would expand (+ a b c)
to (+ a (+ b c))
etc; this would make the actual runtime code cleaner, simpler and slightly faster (because the logic to check for extra arguments would not have to run every time you add a pair of numbers).
However, the usual macro expansion algorithm is 'keep expanding the outermost form over and over until you get a non-macro result'. So that means e.g. +
had better not be a macro that expands to +
, even a reduced version, or you get an infinite loop.
Is there any existing Lisp that solves this problem at macro expansion time? If so, how does it do it?
Common Lisp provides compiler macros.
These can be used for such optimizations. A compiler macro can conditionally decline to provide an expansion by just returning the form itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With