12 ms·
Macros 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 hav
by lmm 12y ago
Macros 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.
- lmm 12y ago> 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. The problem is the nonlocality. A function call can only ever have an effect at the exact point in the syntax tree where it is (side-effecting functions are bad); you can work your way up, understand each argument in isolation, then understand the function call applied to those arguments, and then you understand the value of the whole function call expression and can continue working your way up the tree. There's no such assurance with macros, because a macro arbitrarily far up in the tree could be changing the meaning of a token many lines further down. > 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. I'm a Scala guy rather than Haskell. You can't do it with language-level state, but you shouldn't be using that anyway; lenses (inside a state monad) give you that symmetry between get and set (apologies if I mix up the syntax, there are a couple of different implementations): (for { x ← list_ |-> head toState y = x * 2 _ ← list_ |-> head := y } yield {}).run(someList) := is just a method and Lens is just a typeclass; you can write your own instances (though for most "normal" cases you just derive them with a 1- or even 0-liner) for your own types and then the same state-manipulation code will work when that state is contained inside one of your objects.
- ICWiener 12y agoI forgot about lenses, thanks. About non-locality of macros vs. functions: Macros, being functions, provide this kind of assurance, too at the syntax tree level: you only operate on the local, current subtree. They can indeed modify the meaning of symbols in that subtree, but I think you are taking things to the extreme. Off course, an arbitrarly nasty macro "far up in the tree" (how deep are the typical functions, anyway?) could do horrible things. Also, we must ban sugar because if someone eats too much sugar, that someone dies. However, the average usage is more subtle. For example, when you DEFUN a function, you are registering a function in your current environment (side-effects) through a macro, which does not dramatically change the semantics of the body of the function, compared to, say, an anonymous LAMBDA (the only change is, I think, the ability to RETURN-FROM a named function). CL is not a functional language: you can loop over lists or across vectors, you can setf special variables and mutate strings. You have static and dynamic non-local exits (return, throw/catch, signal) and even goto's (tagbody). That does not mean that you should use them all and go against best practices and common sense. Just because a feature could be misused does not make it irrelevant. For the record, I have nothing against functional languages (or any other language, really (except of course PHP ;-)). It just seem unfair to attack macros. They should be avoided whenever possible, but they have some uses: C (macros), C++ (templates), and even Haskell and Scala try to have them: http://scalamacros.org/usecases/index.html http://scalamacros.org/usecases/index.html https://downloads.haskell.org/~ghc/7.6.1/docs/html/users_guide/template-haskell.html https://downloads.haskell.org/~ghc/7.6.1/docs/html/users_gui... http://neilmitchell.blogspot.fr/2007/01/does-haskell-need-macros.html http://neilmitchell.blogspot.fr/2007/01/does-haskell-need-ma... Finally: macros are not a hack used to fix a supposedly missing feature of the language (e.g. "I don't need macros because I have lazy evaluation", etc.). Macros allow programmers to interact with the compiler (reader macros, compiler-macros): they are the primitive provided to perform meta-programming tasks.
- davexunit 12y agoSigh, the tired argument that because people may use a powerful feature wrongly, that feature shouldn't exist. >they still lead to unreadable code Complete unsubstantiated nonsense. >If they need more things, add those things as first-class features If JavaScript had macros we could have had 'let' before ES6 instead of writing '(function(x) { ... })(42)'. Do I really need to know that 'let' compiles to a lambda with arguments applied? No, I just need to know that 'let' binds some values to variables and evaluates the body. Do I need to know that 'cond' compiles to a nested 'if' chain to know how to use it? No. I don't understand why people think that syntax has to be behind a gate that only the language implementors have access to. Syntactic abstraction makes talking about problem domains easier, not unreadable.
- hajile 12y agoJavascript has sweetJS. It isn't lisp, but it's about as close as most infix languages can get (with the exception of Dylan). The let in ES6 cannot be expressed in the same terms as a let in lisp. Mozilla proposed that kind of let with the let block ('let (<assignments>) { <statements> }'). The issue is that other idiots (in some quest to turn JS into Java or C#) decided that a block scoping kluge was a better. After all, it makes interoperability harder (old code using var), makes scoping rules implicit (blocks without let are still bound to function), adds unnecessary cognitive overhead (leading to more bugs as code gets harder to reason about and visually parse) and generally adds a whole new set of ways for new and old JS programmers to screw up.
- ihnorton 12y agoJulia has macros denoted by an at-sign (e.g. @inline) to make clear that something special is going on. Understanding how to write effective macros takes some work - perhaps more than Lisp; I can't fully say, myself - because expression representation is less apparent to the casual user. This, combined with an emphasis on "don't use macros when a function will do", seems to have lead to fairly judicious use of macros in most of the code I have seen. Good tooling can also help with the locality problem. For example, by showing macro expansions in-place when possible (and desired).
- gioioso 12y agoI've encountered countless situations where even a tiny macro made the code more concise, more readable and less error-prone. Functions are there for direct transformation of some data. Macros are there for some more sophisticated, not immediately obvious transformation of some meta data. Functions are at a lower level, macros are at a higher level. Please remember: your users will use your programming language in unexpected ways: you (as a language designer) are most probably not the smartest human on Planet Earth: so please let someone smarter than you add the things which you were not able to conceive beforehand, thanks! Lisp thinks it this way. Congrats!!