Emacs Lisp
The Great Radar Ovens Recommended Beginner Language To Learn.
Table of Contents
Intro
LISP (LISt-Processing) is an older programming language characterised by a prefix notation and a simple syntax based around nested parenthesis. It was eventually superseded by other languages, but lives on today in the form of modernised versions and adaptations, 'dialects'.
Emacs Lisp is a dialect based around the function of the text editor GNU Emacs. It may be easier to consider GNU Emacs closer to an Emacs Lisp interpreter with some built-in functions dedicated to managing text, giving some truth to the common Vi/Vim tease that Emacs is a good operating system lacking only a decent editor.
Recently
, I have been learning and using Emacs Lisp, despite my prior reservations for reasons discussed in this article.Other dialects include Common Lisp, Scheme, Racket, Clojure (runs on the JVM), and Guile (technically a Scheme implementation).
This article feels a tad ironic as I happen to have a slight speech defect that means that Lisp is one of the harder programming languages for me to pronounce. Doesn't help that I have a strong west country accent that meaning it ends up sounding more like 'lethp'.
Example
Here is an Erlang program that sets a variable and checks whether it is equal to 4 using a separate function:
-module(test). -export([to4/1,do/0]). do() -> Var = 4, to4(Var). to4(Test) -> Test == 4.
Which you may run with test:do().
at an Erlang prompt.
And the same program in Emacs Lisp:
(defun is4 (Test) (= Test 4)) (defun do () "Tests whether local var is 4." (let ((Var 4)) (is4 Var)))
Which you may run by evaluating (do)
.
Why Lisp?
Powerful
Many programmers agree that Lisp dialects are some of the most powerful languages available. It has a very simple and regular syntax. The Lisp macro (basically code that forms code) has been hailed as one of the best features for any language trying to get a lot done in less lines of code.
A wise programmer friend of mine often says that as a language becomes more powerful, it starts to become a Lisp dialect. In particular, I have heard that this has sort of happened with some of the more powerful features of Python though, as a non-pythonista, I have no personal experience of this.
Simple
In Lisp, everything is a list (also known as s-expression or sexp
), hence 'List Processor'. There is nothing else to it. You may specify different lists by putting them inside parentheses.
The start of a list specifies the function that the computer should apply to the list (or prefix notation).
(+ 4 4) ;; Returns 8
Syntactic sugar, while rare, is present. A common structure is using a quote mark to indicate that the computer should interpret the next list literally, without a function.
'(+ 4 4) ;; Returns (+ 4 4) (quote (+ 4 4)) ;; Returns (+ 4 4)
As seen above, this is just shorthand for the quote
function.
Readable
After reading a few blog posts and web sites (and a little bit of my own experience), I have concluded that contrary to popular belief, Lisp is readable. I have heard people say that they hate the way that Lisp parenthesises everything so much that it has driven them away from Emacs entirely, but I feel that this is an unfair judgement as these people seem to have only dipped their toes into Emacs and its Lisp.
Why Emacs Lisp
This section will outline the specific reasons why Emacs lisp is the best language to learn.
Simple
Emacs Lisp tends to be a simpler version of Lisp. It lacks some features that other Lisps use, but is a good way to get into Lisp programming.
Some features are strange, like using car
for finding the first member of a list (and cdr
for the rest of the list).
Emacs Lisp has less stuff to worry about and less stuff to learn, but slightly less power. You can achieve the same result as any Turing complete language, but I wouldn't recommend making it your sole language.
Emacs Integration
I feel that it goes without saying that Emacs Lisp is heavily integrated with Emacs, providing many advantages to writing Emacs Lisp in Emacs.
Incremental Improvement
As previously stated, Emacs is more of an Emacs Lisp interpreter with some special functions for dealing with text and Emacs-specific features.
GNU Emacs by default includes an easy way to execute s-expressions. Simply place the cursor against the last parenthesis of the list, and press C-x C-e to run the command eval-last-sexp
.
This may sound trivial, but it means that incremental code improvement and learning is much faster. Traditionally, the programmer needs to re-interpret or re-compile the whole file to see the changes.
In Emacs Lisp, you only need to re-evaluate the new expression. Any unchanged code stays the same, making the term 'incremental improvement' much more apt.
Portability
Emacs also means that when you write a program in Emacs, you do not have to mess around writing your own interface or making it work properly it X and in a terminal. The only prerequisite is that the user have GNU Emacs (something they should be using anyway in my opinion).
Those programs will also work pretty well on any computer running a compatible version of Emacs, as well as being (seemingly) easy to put into the Emacs package repositories, such as Marmalade, MELPA, and ELPA.
Practice
I often have the problem of 'programmers block'. I.e. I don't know what to actually sit down and write as a bit of practice. As I use Emacs a lot, I often think of little improvements that I could make to it's workings.
I have realised recently that I have unintentionally, by looking up and writing Emacs Lisp, started learning Emacs Lisp (in a practical setting).
Full Language
A mistake I made, and the main reason for not starting to learn Emacs Lisp sooner, is that I didn't think about it as a full language, more of a little extension structure for GNU Emacs.
This is not true, it is a full language with full capabilities.
I previously thought that Emacs Lisp has a heavy reliance on Emacs to run, but I happened to be looking at the web-page of the programming language Guile and read 'in addition to Scheme, Guile includes compiler front-ends for ECMAScript and Emacs Lisp'. If I have gotten this the right way round, this means that you could compile and run Emacs Lisp without needing to drop into Emacs.
Documentation
GNU Emacs is often called the 'self-documenting editor' in reference to how Emacs has documentation that updates when parts of the running source changes. For example, I can (re)map a function to a key in Emacs and upon checking the help documentation, I find that the key is present and correct.
Emacs Lisp also has a feature in that the structure for defining a function has a documentation string that, while not imperative, is highly suggested. This feature means that when writing Emacs Lisp, the programmer can quickly check a feature by navigating to the function and pressing C-h f or by using the command describe-function
.
For further help, one may turn to the community for help. They are active and vocal on the Emacs Wiki and on the Emacs Stack Exchange. Some sites dedicate themselves to teaching Emacs Lisp such as Xah Lee's Practical Emacs Lisp Tutorial.
GNU Emacs also contains, by default, for free, a full copy of the Introduction To Emacs Lisp, documentation also sold as an actual book. It is easy to read, has sections devoted to different subjects and contains practice tasks for you to try. As you will most likely be reading this within Emacs, you can run any example given to see its effect, giving the tutorial a nice interactive feel. I also feel that introduction is a bit of a misnomer, as just from reading the contents, it is apparent that it is quite exhaustive in what it covers. You may access the manual by using M-` h m i
Lastly, there is the Emacs Lisp Reference Manual, an even more exhaustive documentation of the usage and conventions of Emacs Lisp, again included for free, that contains more documentation devoted to interacting with Emacs-specific features.
The open-source and interpreted nature of Emacs Lisp means that GNU Emacs has the source code for all of the non-C functions. This creates the interesting situation where you can access and steal borrow code as you please, similar to how you might when making a web-page.
Conclusion
Emacs Lisp is in no way the be-all-end-all programming language. It has some strange conventions left over from its Maclisp heritage and is a (little bit) slow. While a full language, it's main purpose is still for the extension and programming of GNU Emacs, though it is amazing to me what people have done with it. Amazon's internal mail system used to use an Emacs-based interface programmed in Emacs Lisp, mailman, that older members of the Amazon team still apparently miss and reminisce about and preferred over the newer Java system (I have tried and tried to find the blog post talking about this so I could link it, but in vain).
Emacs Lisp will not replace my love of Erlang and functional programming (I am also learning Clojure), I see it more as a very convenient learning tool. Well, well, well, it would seem there is a language known as LFE, or Lisp Flavoured Erlang. Another language to add to my ever-growing to learn list.
Improve This
I am in no way any sort of Lisp-er.
I have probably gotten at least one thing wrong/missed something out.
Please let me know by getting in contact with me here.