3 ms·
I don't think that macros are the singular thing that sets Lisps apart. I do think that they're an extremely useful feature to have, especially if you build pro
by mikelevins 4y ago
I don't think that macros are the singular thing that sets Lisps apart. I do think that they're an extremely useful feature to have, especially if you build programs and systems by building languages in which to express them.
Lisp's macros are a way to define a syntax that you would like to be able to use, and arrange for Lisp to rewrite that syntax into something that Lisp already supports.
Two circumstances where they come in really handy are:
1. You have to repeatedly write a bunch of boilerplate that never changes, except for maybe a few small particulars. You can write a macro to turn all that boilerplate into a succinct expression that writes the boilerplate for you. As an example, DEFCLASS can be implemented as a macro that writes all of the low-level machinery of setting up a class and all its appurtenances so that you don't have to.
2. You need to write some code that is error prone in some way, and you want to ensure that relevant protective or recovery measures are never ever forgotten. You can write a macro that writes the protective or recovery measures for you around the error-prone code. As an example, WITH-OPEN-FILE is a macro that ensures that an opened file is properly closed, no matter how control exits the WITH-OPEN-FILE form.
- pfdietz 4y ago3. You want to do something that in another language would require a separate preprocessor. You can do arbitrary computation in the macro, so it becomes a preprocessor.
- mikelevins 4y agoYep. Good point.
- pfdietz 4y ago4. In Common Lisp, you can intercept the macroexpansion process with the macroexpand hook. This special variable, when bound to a hook function, allows macro expansion to be arbitrarily modified. All sorts of modifications to code can be performed in a dynamically controlled way without changing the source code being compiled. http://www.lispworks.com/documentation/lw50/CLHS/Body/v_mexp_h.htm http://www.lispworks.com/documentation/lw50/CLHS/Body/v_mexp...