HACKER Q&A
📣 cloogshicer

A real-life use case for Lisp macros that beginners can understand?


I'm a novice in Lisp (specifically Clojure) and I got interested in the language because of the macro system.

However, all the examples I've seen so far seem pretty convoluted and just made me think "Why would I want to do that?". I think I just didn't "get" it yet.

Is there a simple real-life example that shows the usefulness of the Lisp macro system to a beginner?


  👤 kazinator Accepted Answer ✓
> Is there a simple real-life example that shows the usefulness of the Lisp macro system to a beginner?

No; all the simple macro examples are in fact in the "why would I do that" category. Their value is the how, rather than the why.

A very simple macro can often be eliminated by writing whatever it writes by hand without a lot of additional effort, and without the baggage of carrying the macro definition. It doesn't seem useful, especially if you fail to imagine it being used thousands of times, rather than just the two or three times that occur in the example.

It's the convoluted macros that answer the "why": because if you don't have the convoluted macro to generate code in a complicated way, and you need to do that dozens of times, then your alternative is to do it all by hand, which is tedious and error-prone. THe macro does the job consistently, and if there is a bug, you just fix it in the macro and re-expand all the uses of it to spread the bugfix.


👤 abrax3141
You are thinking about this wrong ... and it’s not your fault, because everyone (a large subset of lispers included) thinks about this wrong. You can approach lis programming as normal programming, if you like, but that’s not the right way to approach lisp. The way to approach lisp is to answer the following question: If there was a way I could wave a magic wand, and a special purpose programming language would be created just for my problem, what would it look like? Lisp macros are that magic wand. This seems abstract, but it’s not. Really, take your favorite problem, imagine a programming language (aka. DSL) that perfect for that problem. Write down what it would look like. (The one constraint is that the top level has to be in sexpr format, but even that's not deeply required!) Now use macros to translate your fantasy into whatever implements it algorithmically. Poof! You’re a lisp hacker!

👤 ksaj
It might sound cliche by now, but I think you can glean a whole lot from the book Practical Common Lisp, by Peter Seibel.

It's one of those books I learned from online, but I like it so much and kept on returning to it. So I bought a copy, and I still read it and write notes in the margins. It's that good. I don't call many things "bibles" but PCL is pretty much the Common Lisp bible. And it is as practical as the title suggests (as per your request!)

Chapters 7 and 8 are both entirely about Lisp macros. But even then you aren't done with learning about macros yet. The natural progression of the book means you keep building on the prior concepts until the last chapter.

Of course, the CD database you create while working through the book is a tad naive, but the idea isn't to make a commercially viable application - it is to demonstrate Lisp thoroughly by creating apps right from the get-go. And you would do exactly the same things in a commercial setting.

PS: It's Common Lisp and not Clojure. But you were asking about macros specifically, and the Lisp flavour doesn't matter much at all when it comes to how macros work. This book will lead you in the right direction.