August wrote:Gerenuk wrote:
I'd highly appreciate real, short examples in this thread. I hardly know enough Lisp to make up my own, but I can guess what example code means. Can you write out an example, which cannot easily be replicated with Python?
I'm learning Lisp at the moment and use Python fairly regularly but am certainly not an expert. This question is for my own edification as much as anything. Before Python introduced the "with" statement in 2.5, could you have implemented "with" yourself with the same level of integration into the language?
Code: Select all
with open("hello.txt") as f:
for line in f:
---arbitrary code in here----
print line
In Lisp, this kind of thing is easy.
This is a really important point, and it's one that is easily lost. There is something bigger at work here than just macros (said in an awesome voice).
Lisp, you see, is self-contained and very, very, very evolvable in ways that other languages aren't. The "core" of Lisp, it's special forms, is insanely small. The rules for interpreting Lisp code fit in a few lines (I actually have a coffee cup with the original McCarthy interpreter printed on its side). Everything else is essentially a library routine or macro on top of that core. A full-featured Lisp, like Common Lisp, has slightly more infrastructure, but even that is built on that insanely small core.
Essentially, a Common Lisp implementation works by translating ASCII program text into data structures (sexprs). That step is customizable and a programmer can, using reader macros, change the way the Lisp READ function creates the data structures. This potentially allows you great freedom in customizing the surface syntax that Lisp offers. In practice, most people don't go that far and find that the basic sexpr syntax works just fine. Then, just before the interpreter or compiler gets a crack at the sexpr, it is subject to macro expansion (standard macros, symbol macros, etc.) that again allows potentially radical transformation of the code. Finally, the code gets interpreted/compiled/executed.
It's the small, almost trivially simple core, couple with a flexible set of processing steps that allow radical transformation of the data structures that represent the program code that makes Lisp so flexible. And, of course, since these data structures can be constructed on the fly (code is data is code) it's very easy to write code that writes code.
To me, those things are what Lisp has that no other programming language can really match. The only other language in the same league is Forth. If you look at Lisp and barf because of the parenthesis, then you simply don't get it (yet). I know that I didn't get it for a long time, either. But once you understand it, you never look at another programming language the same. I should also point out that if you look at Forth and just see RPN, you also don't get it.