8 ms·
Why Lisp macros are cool, a Perl perspective (2005)
- rtpg 12y agoVery excellent argument against C macros in any case.
- deleted 12y ago[deleted]
- Elrac 12y agoYep. Even as a long time C hacker who knows to avoid the common pitfalls, I wasn't aware of just how many ways there are to botch up the use of macros. There are lots of cases where macros look like the obvious and elegant solution to a problem. Some of those cases are bugs waiting to happen.
- patrickmay 12y agoNicely summarized, indeed. His bemusement at the reaction of C++ proponents made me laugh: "But I did not think at the time that this would be controversial. I was sure that even the most rabid C++ fans would agree with me that the C++ macro system blows goat dick...."
- creatio 12y agoOT, I wish JS has macros. There is sweet.js, but I get the feeling its not used very much....
- sjolsen 12y ago> In Lisp, source filters never break. > Never. To be perfectly fair, this is not quite true. In Common Lisp (the dialect being used here), macros can break in rare, subtle ways. The obvious happens when you don't use GENSYM: (defmacro foo (var) `(let* ((x 3)) (setf ,var x))) CL-USER> (let ((x 7)) (foo x) x) 7 CL-USER> (let ((y 7)) (foo y) y) 3 but is fairly easy to avoid. In practice, as long as you make judicious use of GENSYM and packages, the only thing you really need to worry about is someone FLETing a symbol in the package COMMON-LISP (or maybe an external symbol in your package): (defmacro bar (x y z) `(list ,x ,y ,z)) CL-USER> (let ((list '(7))) (bar 1 2 3)) (1 2 3) CL-USER> (flet ((list (a b c) (+ a b c))) (bar 1 2 3)) 6 SBCL actually yelled at me when I tried this for violating the package lock on COMMON-LISP. So in practice, it's really not much of an issue, and is certainly a hell of a lot better than C macros. If you're interested in learning more about this, look at Scheme's hygienic macros.
- nabla9 12y agoVariable capture in CL can be avoided with few helpful macros. (defmacro WITH-GENSYMS (syms &body body) `(let ,(loop for s in syms collect `(,s (gensym))) ,@body)) (defmacro WITH-UNIQUE-NAMES (vars &body body) `(let ,(loop for var in vars collect `(,var (make-symbol ,(symbol-name var)))) ,@body)) (defmacro REBINDING (vars &body body) (loop for var in vars for name = (make-symbol (symbol-name var)) collect `(,name ,var) into renames collect ``(,,var ,,name) into temps finally (return `(let ,renames (with-unique-names ,vars `(let (,,@temps) ,,@body)))))) (defmacro ONCE-ONLY ((&rest names) &body body) (let ((gensyms (loop for n in names collect (gensym)))) `(let (,@(loop for g in gensyms collect `(,g (gensym)))) `(let (,,@(loop for g in gensyms for n in names collect ``(,,g ,,n))) ,(let (,@(loop for n in names for g in gensyms collect `(,n ,g))) ,@body))))) edit: added once-only
- fabriceleal 12y agoLet over Lambda uses this (maybe something like this?). Very cool book. http://letoverlambda.com/ http://letoverlambda.com/
- ICWiener 12y agoDon't forget ONCE-ONLY
- gus_massa 12y agoAnother place where macros in Lisp fail is that they may evaluate an expression twice. (Using the Scheme dialect, because I don't write good enough CL.) (define (inc! x) (set! x (+ x 1)) x) (define-syntax-rule (square x) (* x x)) (define n 5) (square (inc! n)) ;==> 42 This can be solved using a temporary variable as in the original article, (define-syntax-rule (skuare x) (let ([temp x]) (* temp temp))) (define m 5) (skuare (inc! m)) ;==> 42 But it's easier because you don't have to declare the type of temp. Also Scheme has hygienic macros, so you don't need to use gensym to avoid the variable capture of temp.
- jbert 12y agoUsing setf is a nice example of something which a macro can do which a function cannot (as well as being a good example of code-is-data). Lots of posts discussing lisp macrology lack that kind of compelling use case. The 'magic' used by setf is a 'destructuring bind': http://dunsmor.com/lisp/onlisp/onlisp_22.html http://dunsmor.com/lisp/onlisp/onlisp_22.html
- lmm 12y agoMacros are powerful but still a bad idea. Like GOTO - using GOTO, you can implement absolutely any kind of weird control flow structure you can imagine. You have a lot more freedom than the usual if/else/for/while. And you can, if you use it wisely, make code that's more coherent and consistent than any other. But people don't. Coherence of assignment is a very good idea. But you don't need macros to get it. If you have a language with simple syntax where assignment is just another function call (perhaps via a typeclass), everything in this example still works; it's not the fact that lisp has macros for assignment that's important here, it's the fact that assignment is not special, not handled specially in the parser; it's just another function. You do that, and you get the advantages, with or without macros. Lisp's lack of syntax makes macros easy, but they're still a mistake; they still lead to unreadable code because you can't tell what a given piece of code does until you understand which parts are macros and understand all of them. Give users more structured ways of achieving the (often quite small) number of things they want to do with macros. If they need more things, add those things as first-class features, the same way e.g. exceptions were added when people realized if/else/for/while didn't capture all the use cases for goto. But don't throw up your hands and let every library rewrite code willy-nilly.
- ICWiener 12y agoIt is true that macros are regarded as a last resort mechanism: whenever you can use functions, or generic functions, you are encouraged to do so. Also, macros are nothing more than functions: they operate on the syntax tree, at compile-time, but they are still functions. All language implementations (compilers, interpreters), will have somehow a need to manipulate syntax trees: in CL, this facility is exposed to the programmer (hence, the "programmable programming language" quote from John Foderaro). > If they need more things, add those things as first-class features, That's not the philosophy behind Lisp. You don't wait for a new relase of the standard (C++11, C++14, ...) and for compiler support: if you need to do crazy things with syntax trees, you are welcome. Then you may pulbish it and other people will use your libraries, like it is done for any other libraries in any languages. > they still lead to unreadable code because you can't tell what a given piece of code does until you understand which parts are macros and understand all of them So, in order to understand a pice of code, I must: (1) know what is, or isn't, a macro, and (2) if it is a macro, know what it does. The same applies for any function: don't apply a function if you don't know what it does. You can rely on docstrings, macroexpand, as well as live coding techniques (describe, inspect) to help you understand complex operations. But saying that macros make code inherently "unreadable" is not justified: what about monads? do they simplify code or complexify it? like macros, they introduce an abstraction (loop) and can help with consistency (with-open): some people dislike it, because they feel they loose sight of what is "really" happening, but this is not a bad idea by itself. > But don't throw up your hands and let every library rewrite code willy-nilly Strawman: people don't really do that. Regarding your example, I really would like to know how typeclasses can be used to solve `(setf (car list) x)`. If you have a concrete example of what you mean, for example in Haskell, please explain.
- bmn_ 12y agoSource filters are bad because you get a string and parse it yourself and then get it wrong for edge cases. What if Perl already parsed a piece of source for you and handed you a data structure instead? That fixes the greatest shortcoming. Solution 1: B::Deparse <http://yapceurope.lv/ye2011/talk/3597> http://yapceurope.lv/ye2011/talk/3597>, <http://act.yapc.eu/gpw2014/talk/5266> http://act.yapc.eu/gpw2014/talk/5266> Solution 2: B::CallChecker / <http://perldoc.perl.org/perlapi.html#cv_set_call_checker> http://perldoc.perl.org/perlapi.html#cv_set_call_checker> In other words, you do have access to the parser. It's not as neat as in Lisp because of the lack of uniform syntax, but still allows for powerful program transformations: <http://p3rl.org/Kavorka::Manual::Signatures> http://p3rl.org/Kavorka::Manual::Signatures>
- GotAnyMegadeth 12y agoCould someone explain why all of the = signs have 3D after them? $x =3D ...
- jmartinpetersen 12y agoThe link is to a web archive of a a mailing list. I'm guessing the software doesn't properly handle the quoted-printable format of the e-mail. In QP a = is used as the escape character, and =3D is the escape sequence for =.
- agumonkey 12y agoreddit oldest (from 8ya) thread about it is interesting http://www.reddit.com/r/hackernews/duplicates/2o90ty/why_lisp_macros_are_cool_a_perl_perspective_2005/ http://www.reddit.com/r/hackernews/duplicates/2o90ty/why_lis...
- Ultimatt 12y agoPerl6 advent calendar post (from 2012) on macros might be of interest http://perl6advent.wordpress.com/2012/12/23/day-23-macros/ http://perl6advent.wordpress.com/2012/12/23/day-23-macros/
- pmoriarty 12y agoAn easier to read ("=3D" converted back to "=") version of this post: [1] Previous discussion of this post on HN: [2] Also see: [3] [1] - https://bpaste.net/show/a9bb9668beac https://bpaste.net/show/a9bb9668beac [2] - https://news.ycombinator.com/item?id=795344 https://news.ycombinator.com/item?id=795344 [3] - http://stackoverflow.com/questions/267862/what-makes-lisp-macros-so-special/4621882 http://stackoverflow.com/questions/267862/what-makes-lisp-ma...
- CyberDildonics 12y agoDoes anyone know the basic differences between Julia's macros and Lisp macros? Are they the same or is one more powerful/flexible than the other?
- zaphar 12y agoJulias AST is very similar to an S-Exp so the macros in that sense have a lot of the same benefits of a lisp macro. However they don't have the same ease since the AST does not necessarily match the way the source looks. In Lisp the AST looks exactly like the source so it's easier to visualize what a macro needs to do. Julia requires you to mentally translate the source code syntax into an AST as an extra step. That extra step doesn't make them any less powerful/flexible though. It just makes them slightly harder to use.
- grayclhn 12y agoOnly loosely related, but I would love to see multiple dispatch come to Julia's macros. I seem to spend a fair bit of each macro switching on the expression's head, and would happily offload that step. (Of course, that might be growing pains since I don't really have a good feel for writing macros in the first place.) Naturally, the best way to make that happen would be for me to write a macro implementing it... :)
- areyousure 12y agoSo how does one write the (square x) macro in Common Lisp? Presumably, one would like to avoid evaluating the argument twice as well as accidental variable capture.
- deleted 12y ago[deleted]