Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Lisp trades some of the strictness and static typing of Haskell and Scala, and the convenience of Python I guess; for more raw power. You won't really have a clue what macros are capable of before you see it yourself, I wholeheartedly recommend using Let Over Lambda (https://letoverlambda.com) as a guide. The other side of the coin is that you'll spend the rest of your days waiting for mainstream languages to catch up, but that's true of Haskell as well to some extent.


I don't know about "won't have a clue what macros are capable of before..." -- there are quite a few use cases for macros which can be described easily to any programmer, even those who never used Lisp. I would like to believe it would suffice to say "macros in Lisp allow you to fully extend the the language with new primitives and abstractions in a way that no other language does", but examples are always helpful.

Lisp has a macro called setf, which takes any call which gets a value -- whether from a local function variable, or a class, to an array or hash table entry -- and converts it to the corresponding setter call. (If you define your own value places/types, there are also functions to make setf aware of how they work, so this isn't restricted to just some defined set of features in the language. Anything that holds a value in Lisp can be "setf" to a new value.)

There are a couple of other "big" Lisp macros in the language standard like with-open-file (which ensures a file is closed once control flow exits the body), loop (does pretty much what it says on the tin: provides whatever looping method you might want), or handler-case (try-catch style error handling; yes, Lisp allows for other methods).

Or how about the example I recently saw in a cryptography library. Since a lot of cryptographic algorithms use integer math modulo some giant number, this library defined a macro "mod-n" which took a base and some Lisp code, and re-wrote the code so the common integer math functions (addition, multiplication, etc.) would only return values "modulo {base}" --that is, return only values between 0 (inclusive) and base (exclusive). This meant their modulo math code looked (nearly) the same as normal, just standard arithmetic functions. This wasn't a very large macro either: only around 10 lines long (and about half of that being the list of operator replacements).


Thanks for the pointer, just ordered a copy.


Note that let over lambda is ... let's say controversial among lispers. I think it is very good at teaching how to write macros, but very bad at teaching what macros one ought to write. It's a bit like that bit about making ruby accept bare words in Wat[1]. The fact that lisp lets you do the things that LoL presents is fine, learning how to do this is good. If you actually do this in production code, that's bad.

1: https://www.destroyallsoftware.com/talks/wat (45 seconds in)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: