News for hackers. Stuff that matters.
Re:An Intuition for Lisp Syntax by dgb23 on Monday October 26, 2020 @05:42AM (#24894344)
Macros are in practice a tool to reduce boilerplate. Which is why they are typically written for libraries and used in application code.

For example parametrization and modularization are used to keep code DRY and separated at a functional level.

However this can only get you as far as you still write ceremonial repetition around using those functions/modules/objects. Macros can get your code to that extra level of brevity and clarity.

Re:An Intuition for Lisp Syntax by chriswarbo on Monday October 26, 2020 @06:35AM (#24894629)
I've also used macros to execute some expensive data processing at compile-time. I had a fixed dataset containing named sets of strings, and I wanted a program which reads in a set of strings and returns the names of any supersets which appear in the dataset. For example, if the dataset contains 'x = {a, b, c}', 'y = {a, b, d}' and 'z = {a, c, d, e}' then a query of '{a, c}' should return '{x, z}'.

I managed to speed this up a lot by using a (TemplateHaskell) macro to (a) read in the dataset from an external file at compile time, (b) transform it into a structure that has fast lookups and (c) serialise that structure to a Haskell syntax tree. Not only did this make lookups faster at runtime, it also eliminated all the error checking for locating/reading/parsing/etc. of the dataset: any problem would cause a compile error; if compilation succeeded, the resulting program didn't have to know or care about any of that.