by michaelwww · 4y ago · 0 comments
[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.
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.
http://www.lispworks.com/documentation/lw50/CLHS/Body/v_mexp...
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]
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.
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-refe...
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.