Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro expanding to same operator

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?

like image 511
rwallace Avatar asked Dec 01 '22 09:12

rwallace


1 Answers

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.

like image 175
Rainer Joswig Avatar answered Dec 05 '22 01:12

Rainer Joswig