4 ms·
>It is very possible and practical to parse conventional languages down to an AST tree, work on the tree, and run that code. Yes, of course. >Sometimes I wond
by flavio81 9y ago
>It is very possible and practical to parse conventional languages down to an AST tree, work on the tree, and run that code.
Yes, of course.
>Sometimes I wonder if the LISP cult is just trying to pretend Noam Chomsky was never born.
Life is great at our cult, you see?
Now, seriously, what you propose is to transforme "conventional language" (say, Java) to AST, work on it, and then spit out conventional language again. This is fine.
The problem is that in those cases, when you write a "macro" (an AST->AST function), you then need to learn:
* the semantics and structure of the AST
* all the functions/methods/classes your Conventional Language tells you to use for manipulating the AST
The point of Lisp is that you are writing the code in what is an AST as well.
And this AST is written as Lisp lists.
And Lisp is very good at manipulating lists, it has a ton of built-in functions for them.
And thus, for writing the AST->AST function ("macro"), you don't need to learn anything new, if you already know Lisp.
An additional bonus is that your macros are mostly clear, easy to read. Because they are written in a mostly similar way to the rest of your code.
Another bonus is that Lisp was created with AST->AST transformation in mind from the ground up. Macros work almost transparently; you can specify them to work on read-time or on compile-time. They can also work at run-time if necessary. You can do AST->AST at runtime and have the Lisp implementation compile it to machine language at runtime.
This isn't so easy (or practical) to do with conventional languages...
- skybrian 9y agoIn Lisp, my understanding is that while you can manipulate the AST directly, you will likely introduce a bug unless you use the special functions for handling hygienic macros? And each flavor of Lisp has its own way of doing hygienic macros.
- flavio81 9y ago>>use the special functions for handling hygienic macros? And each flavor of Lisp has its own way of doing hygienic macros. There are two main flavors of Lisp: Common Lisp and Scheme; Scheme already has hygienic macros as default; and writing hygienic macros is trivial (and easy) in Common Lisp. >you will likely introduce a bug unless You won't "introduce a bug" if what you need is specifically an unhygienic macro. They have their uses, so it's good to be able to write unhygienic macros as well.
- jlg23 9y agoYou have the liberty to write anaphoric macros in CL and with it comes the responsibility not to write them by accident.
- Jtsummers 9y agoYou can use gensym in common lisp to introduce a new symbol that is guaranteed not to exist already. If you do this your macros become hygienic. In my experience this isn't that big a burden. And sometimes you want to access existing symbols through your macros.
- offlineguy 9y agoIf you don’t know what you’re doing, you can introduce bugs regardless of programming language.