Previous | Table of Contents | Next |
In this chapter when I speak of the Lisp interpreter, I mean either of these two interpreters. Customarily, when you write Emacs Lisp, you work with the printed representation; when you are satisfied with your work and need the extra speed, you can byte-compile it.
Most often, people run Emacs Lisp programs within Emacs. In this chapter, I assume that is what you will do.
However, you can run Emacs in batch mode just as you run a standalone awk, perl, sed, or sh script.
For example, just as you might run awk like this:
awk -f program-file input-file &> log-file
you can run an Emacs Lisp program like this (note the files order):
emacs --batch input-file -l program-file -f save-buffer &> log-file
This says to visit input-file, load program-file, which makes changes in the visited file, save input-file, and then exit, with messages or errors placed in log-file.
In this discussion, as I said previously, I presume you are working within Emacs.
This means that you should become somewhat familiar with Emacs as an editor if you are not already. For example, you should know that C-u means to hold down the Ctrl (control) key and at the same time type a u; M-x means to hold down the Meta key (which may have some other name, such as Alt) and type an x at the same time. If you lack a Meta key, type Esc and then the x.
A statement such as C-h f (describe-function) means type C-h f to invoke the command describe-function.
Lists are the basis of Lisp. The word Lisp stands for list processing. The programming language handles lists (and lists of lists) by putting them between parentheses.
For example, (pine hickory oak maple) is a list. Whitespace makes no difference to a Lisp list, and the preceding list could have been written like this:
(pine hickory oak maple)
The elements of this list are the names of four kinds of trees, separated from each other by whitespace and surrounded by parentheses.
Lists can also have numbers in them, as in this list: (+ 2 2). This list has a plus-sign + followed by two 2s, each separated by whitespace.
Furthermore, a list may have a list inside of it, like this:
(this list has (a list inside it))
The components of this list are the words this, list, has, and the list (a list inside it). The interior list is made up of the words a, list, inside, and it.
In Lisp, what I have to this point in the chapter been calling words are called atoms. This term comes from the historical meaning of the word atom: indivisible. As far as Lisp is concerned, the words I have been using in the lists cannot be divided into any smaller parts and still mean the same thing as part of a program; likewise with numbers and single character symbols such as +. On the other hand, unlike an atom, a list can be split into parts.
In a list, atoms are separated from each other by whitespace. They can be right next to a parenthesis.
Technically speaking, a list in Lisp consists of parentheses surrounding atoms separated by whitespace, or surrounding other lists, or surrounding both atoms and other lists. A list can have just one atom in it or nothing in it at all. A list with nothing in it looks like this: (). It is called an empty list. Unlike anything else, an empty list is considered both an atom and a list at the same time. The empty list is also known as nil, a different representation of exactly the same thing, and is considered false in true-or-false tests.
The lists I have been showing are the printed representations of the lists as seen by you or me; these are not what the computer sees. Generally speaking, you need not concern yourself with the difference between the internal representation used by a computer and the printed representation as seen by a human. However, sometimes an awareness of the distinction helps in understanding.
The printed representations of both atoms and lists are called symbolic expressions or, more concisely, s-expressions. The word expression by itself can refer to either the printed representation or to the atom or list as it is held internally in the computer. Often, people use the term expression indiscriminately. (Also, in many texts, the word form is used as a synonym for expression.)
A list in Lispany listis a program ready to run. If you run (or evaluate) it, the computer will do one of four things: do nothing except return to you the list itself; send you an error message; repeat an infinite loop; or treat the first symbol in the list as a command to do something. (Usually, of course, it is the last of these four things that you really want!)
The single apostrophe, , that I put in front of some of the sample lists in preceding sections is called a quote; when it precedes a list, it tells the Lisp interpreter to do nothing with the list other than take it as it is written.3 But if there is no quote preceding a list, the first item of the list is special: It is an instruction for the computer to obey. (In Lisp, these instructions are called functions.) The list (+ 2 2) shown previously does not have a quote in front of it, so the Lisp interpreter understands that the + is an instruction to do something with the rest of the listin this case, to add the numbers that follow.
3The single apostrophe, , is actually no more than a convenience. x is an abbreviation for (quote x), a list that starts with the quote built-in function.
The art of programming in Lisp is to write lists.
Previous | Table of Contents | Next |