3 ms·
> An example is any instance of language extension where, say, the maintainers of the compiler for a functional language have to ship a new compiler to the user
by the_af 12y ago
> An example is any instance of language extension where, say, the maintainers of the compiler for a functional language have to ship a new compiler to the users to get them to use the new feature.
> Functional languages with lazy evaluation are not finished, right? They are developed actively.
Agreed, they are actively developed.
Correct me if I'm wrong, but you seem to be saying "many interesting features can be implemented in a Lisp language with a macro, therefore Lisp programmers don't need to wait for a new release of their programming language when they want these features".
I'm not convinced this is the case, or rather, that this is such a relevant case. Aren't Lisps actively developed too? Why is there such a multitude of Lisp implementations? Is any relevant real-world feature truly implementable with Lisp macros? Why is that more convenient than implementing them with functions in languages with lazy evaluation?
Maybe I'm falling prey to the Blub paradox. I'm only passingly familiar with Racket, thanks to a course in Coursera, where they introduce macros and why they are so powerful in Lisp. But I still don't see the compelling "killer example"...
- kazinator 12y ago> Correct me if I'm wrong, but you seem to be saying "many interesting features can be implemented in a Lisp language with a macro, therefore Lisp programmers don't need to wait for a new release of their programming language when they want these features". That is correct. However, macros have to translate to something. From time to time you need an upgraded something for some macros to be feasible. E.g. it's hard to "macro your way" into having first-class continuations, if they aren't in the substrate. That is to say, without the macro treating all of its arguments as a self-contained language. You usually want the code which is in the macro forms to smoothly interoperate with outside code, such as make lexical references to surrounding bindings. This is the fuzzy limit. Macros have to write stuff in some language, which is no longer macro-expandable. That language has to be reasonably expressive in its semantics for what the macros want to do.
- rapala 12y agoOne could write a macro that allows infix notation for arithmetics: (arithmetics 1 + 2 - 3) = (- (+ 1 2) 3) These kind of syntactic transformations are what macros enable.
- kazinator 12y agoThese kinds of easy, context-independent reshufflings of expressions is what macros enable macro newbies to achieve.
- zck 12y agoSo you think of a feature you'd like in your language. Let's consider the process you'd have to go through to use that feature. In most languages, you have to write the maintainers about the feature. You then have to convince them that the idea is good -- and this is by no means guaranteed; if they think your idea is bad, you're out of luck, and can never use the feature you'd like. Then someone has to implement it. Then you have to wait for a release containing the feature. Then you have to test your code with the new version. Then you have to upgrade all your deploys, development environments, and testing machines to the new version. Then you can use the new feature. In a Lisp with macros, you have to implement the feature. Then you can use it. This is why macros are useful. You get to modify the language you're using, but still cut out the entire loop of the maintainers of that language.
- the_af 12y agoYes, I understand that argument, but I still find it unconvincing. Some features you just can't implement with macros, you need a change in the "substrate" (see kazinator's answer below). For the rest, I simply don't see how they are language-level features. They are just things you need for your project, in which case, why can't you simply implement them as a library? Even if you ignore the above, there's probably a good reason why the language designers don't want to approve your language-level feature. Yes, sometimes it's simply red tape or politics, but it can also be that you -- the applications programmer -- simply aren't well-versed in language design and can't think past your particular use case :) This wouldn't mean the feature is worthless (after all, you need it!) but maybe it's not meant to be a language-level feature, but instead... a library function, which you can write in most general purpose languages.
- zck 12y agoBecause macros let you control code in a way libraries don't^1. Java now has this way to iterate over a collection of things: for(String exclamation: exclamations) { System.out.println("I yell " + exclamation + " at you"); } But this was only added in 2004!^2 So for almost 10 years, you had to manually iterate over stuff. How would you implement this as a library? Well, you could write a function that lets you write: iterate_over_collection(exclamations, function(collection) { System.out.println("I yell " + exclamation + " at you"); } But this is much uglier than the prior code. Also, it might not act the same. For example: int highest = ages[0]; for(int age: ages) { highest = Math.max(highest, age); } Would this work in a lambda? Well, if the language you're using has real closures, yes -- but does it? Do you know offhand? With a macro, your code will work. > Even if you ignore the above, there's probably a good reason why the language designers don't want to approve your language-level feature. ... it can also be that you -- the applications programmer -- simply aren't well-versed in language design and can't think past your particular use case :) This wouldn't mean the feature is worthless (after all, you need it!) That seems like evidence for my point -- macros let you build the language up for your own use case, not anyone else's. Without macros, your choices are "either everyone can use it, or no one can use it". Macros let you have a choice of "well, I can use it, even if no one else wants it, if I find it useful." Macros can be viewed as libraries that act on the language itself. There isn't a difference between language-level features and "library functions" in a language with macros. [1] Without getting into a Turing Tarpit. We're talking about using things in easy ways, not what is technically possible but ugly and kludgy. [2] It was released in Java 5.0: http://www.java-tips.org/java-se-tips/java.lang/the-enhanced-for-loop.html http://www.java-tips.org/java-se-tips/java.lang/the-enhanced...