4 ms·
After years of reading Lisp articles, my take is that the macro system is what sets Lisp languages apart. I was surprised then that the author didn't find them
by michaelwww 4y ago
After years of reading Lisp articles, my take is that the macro system is what sets Lisp languages apart. I was surprised then that the author didn't find them that useful.
[Paul Graham] describes Lisp as his “secret weapon”. OK, so what’s the secret? He says “programming languages vary in power”. Fine, but what exactly makes Lisp more powerful? ...Lisp’s macro facility, which he describes as its ability to make “programs that write programs”. After four years using a Lisp language, I’d agree with Graham that macros are great when you need them. But for someone new to Lisp languages, they’re not necessarily a bread-and-butter benefit.
Further down...
Paul Graham calls Lisp a “secret weapon”. I would clarify: Lisp itself isn’t the secret weapon. Rather, you are—because a Lisp language offers you the chance to discover your potential as a programmer and a thinker, and thereby raise your expectations for what you can accomplish.
What's the secret weapon if it isn't macros? I know what I need from a language that matches my particular quirks as a programmer and helps me shine and it's not Lisp.
This article made me interested in Racket's libraries because it sounds like they might be more interesting than other languages libraries.
- openfuture 4y agoNot everything is about secret weapons... it is just simple and clear to use S-expressions, people like simple and clear.
- runevault 4y agoI took the first quote as you need to hold a certain amount of comfort with lisp before macros matter, not that they aren't an incredible once you understand everything well enough to use them.
- michaelwww 4y agoA couple of people were nice enough to take the time to explain Lisp macros to me in case you're interested: https://news.ycombinator.com/item?id=32730470 https://news.ycombinator.com/item?id=32730470 I can definitely see their power, but I don't have any ideas on what I would do with them. I guess I'd have to write Lisp for awhile to figure that out.
- qazwse_ 4y agoThis is the case for me as well. I was learning Common Lisp for fun, and I loved it, but macros never clicked for me. I could understand how they're powerful, but I never developed a sense for when using a macro would make things easier. Probably just not enough time with the language.
- runevault 4y agoOne way to think about macros is custom syntactic sugar that you can build yourself. So for example, I don't think rust has parameter array (see params/param arrays in c# for an example [1]). So for things like their println that takes variable number of strings to inject into the string where it finds {} it uses the println! and format! macros. Because the parser doesn't support the sugar of a comma separated list and turning that into an array the way c# does (or however other languages handle the same idea), the macro pass of the compiler instead generates the code necessary to make that number of arguments work. There are likely other uses I've never considered, but that is a simple one. [1]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...
- BeetleB 4y agoThis article is an appendix for the online book Beautiful Racket, and most of the book is about how useful macros can be. It's a very accessible book - not at all advanced. A number of folks have listed the book as one of their best all time programming books. I finally read it last year, and I agree with that characterization. It's a very well written book.
- felideon 4y ago> I know what I need from a language that matches my particular quirks as a programmer and helps me shine and it's not Lisp. Yes, but the argument is that, once you've experienced Lisp, your quirks as a programmer completely change. It might not be so much that you need to decide whether you need macros. Instead, decide whether there might be a different approach to writing code that could let you express yourself more naturally. See PG's essay on Programming Bottom-up.[1] [1] http://paulgraham.com/progbot.html http://paulgraham.com/progbot.html
- mikelevins 4y agoI 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...