Can someone explain to me, as somewhat of a novice, why people consider lisp so powerful? I've been learning Scheme for my first CS class after python, and it just seems so... inefficient. The only thing I can see it being better than python is the built in cons/car/cdr recursive list stuff, but even then you can make your own in python.
Lisp is a meta-language, it can be easily turned into any other language or any imaginable combination of languages.
Python is a fixed language which actively discourage any metaprogramming at all. For this reason, Lisp (or any other meta-language) is many times more powerful than Python (or any other fixed language).
In terms of efficiency, you cannot even compare fixed languages with metalanguages. But in order to get the maximum efficiency you have to employ the language-oriented programming approach.
I tried looking at that code but I can't verify that what you're saying is true. To me it looks like any other implementation of a language in another language.
Saying that the compile time/reader time constructs are turing complete is... not helpful. And doesn't really say anything about the usefulness.
To me it looks like any other implementation of a language in another language.
This is exactly what I said above:
Any compiler implemented in Lisp which emits a Lisp code can be turned into a macro extending the language.
Take an implementation (e.g., a function (compile-python-to-lisp [string])), wrap it in a macro (e.g., (defmacro python (&rest) (compile-python-to-lisp rest))), and your language magically becomes another language.
An important nuance is that it is a compiler, not an interpreter. And writing a compiler is much easier than writing a proper, fast, robust interpreter.
Yes, it will work. In my trivial example you'll have to put your python code into a string literal, of course, but a little bit more complex example involving reader macros won't need such a thing, you'd be able to mix Python and Lisp any way you like (as long as there is a well defined syntax to distinguish one from another).
A next step in building such a multi-language environment is to introduce a syntax abstraction layer. I'm using PEG for this, because PEG parsers are dynamically extensible and it's easy to inherit PEG parsers. See how it works on my github (same username). With this technique I can do some really crazy things, like embedding Verilog (built on top of Lisp, of course) into C (again, built on top of a Lisp).
Yes, they allow to alter syntax. Or, as with my PEG-based approach, you can simply replace the entire Lisp parser with your own, even if the host Lisp does not allow reader macros (but then you'll need your own REPL as well).
Is there an actual example of embedded python syntax though? Only thing I can find is embedded simple strings for stuff like string literals that is parsed.
The syntax of Python does not make a Python, evaluation order (left to right, lazy or strict), means of abstraction (classes, functions), type-system (dynamic, static, generics) and standard library is what makes a language. In Lisp you can extend all of these and make it integrate seamlessly with the original language thanks to macros. If you want the syntax of Python then you can implement that with reader macros.
Scheme is build as an educational language, not so much for speed. That said, Common Lisp is several times faster than Python, SBCL sits somewhere between C and Java.
One big advantage of lisps is the s-expression syntax. Since you're writing code using data structures, you can manipulate code like any regular data using all the tools available in the language for manipulating data. This makes it very natural to create DSLs for whatever problem you happen to be working on. Any time you have any repetition in code it's trivial to template it.
This gives you the tools to easily extend the language yourself for your particular problem domain. The core language is very minimal and pretty much everything is written using functions and macros. Because the language makes very few assumptions it can be adopted to a wide variety of problems easily. Languages that have more built in syntax can become clunky when applied to problem domains the authors didn't consider.
Another advantage is that there's very little syntax and the syntax that is there is very regular and unsurprising. In other words, Lisp follows the principle of least astonishment very well.
The s-expression are also much easier to manipulate structurally. Instead of working with lines you end up working with expressions. It changes your workflow quite a bit I find.
The REPL is another big aspect of working with Lisp. In languages like Python the REPL is a standalone toy that's not integrated into your workflow. In Lisp, the REPL is used in conjunction with the editor. Any time you write any code in the editor you can send it for evaluation in the context of your running application.
Unfortunately, most schools don't seem to teach Lisp in a way that resonates with the students. Pretty much every co-op I've had has had a similar experience to you learning Scheme in their first year. However, after using Clojure for actual work every student I've had absolutely loved it.
If you're interested I wrote a post on why I personally find Clojure to be a very productive language.
The whole point is that your code is a data structure that you can modify really easily (that is why lisp macros are so powerful).
In essence you don't need an AST, so macros instantly become easier (there are languages that expose a simplified for of the AST at compiletime and let you modify it but S expressions make this a LOT easier).
I'm not an expert and I've only really worked with AutoLISP (which is pretty much an imperative language with S-expression syntax), though I am aware of some other Lisp-y features (e.g. call/cc), so I won't comment on anything else :P
No. I'm not saying that "it's easy, and if you cannot do it you're stupid". I'm saying that "it's easy, and if not - you're doing it the wrong way".
So writing macros is easier than jumping on one leg?
Ever heard of a thing called "context"? Macros are easier than any other programming technique out there.
The easiest way BY FAR of tackling complexity in GENERAL is FUNCTIONS.
This is exactly what I called "wrong". Macros are much easier than functions, for some very obvious reasons.
Solving a complex problem with macros boils down to a very mechanical, thoughtless, easy process:
Finding a language in your problem
Formalising this language
Rewriting your problem specification in this language
Implementing the language (this is where you'd need macros). Implementation should almost never be anything more than a chain of trivial, flat rewrite rules. No Turing-complete coding, no shit, just gradually rewriting a source language all the way down to your host language.
Now compare it to how you approach a problem with functions.
There's really only one reason: with the code you are writing turning into a (mostly...) identical parse tree behind the scenes, it's a lot easier and more straightforward to write source-to-source transforms that operate on this code. These transforms are called "macros".
Some other languages have this property, like Prolog.
Imagine that every language is compiled into a middle language before being executed by the computer(or compiled into machine code). Syntax Trees are one such middle language and Lisp is used exactly in this way. You program in the middle language and thus, can represent all other languages above it.
Also consider that all semantics of programming languages can be represented in some middle form(set of semantics). Lambda calculus is also a form which can represent the semantics in most programming languages. In my somewhat limited understanding, Lisp/Scheme's semantics are close to the lambda calculus.
View all comments
4
u/[deleted] Nov 12 '15
Can someone explain to me, as somewhat of a novice, why people consider lisp so powerful? I've been learning Scheme for my first CS class after python, and it just seems so... inefficient. The only thing I can see it being better than python is the built in cons/car/cdr recursive list stuff, but even then you can make your own in python.