Lisp programs intended to be run in Emacs should be edited in Emacs-Lisp mode; this happens automatically for file names ending in .el. By contrast, Lisp mode itself is used for editing Lisp programs intended for other Lisp systems. To switch to Emacs-Lisp mode explicitly, use the command M-x emacs-lisp-mode.
A D V E R T I S E M E N T
Numbers Lists: List have numbers, other lists, in them.
Lisp Atoms: Elemental entities.
Whitespace in Lists: Formating lists to be readable.
Typing Lists: How GNU Emacs helps you type lists.
USER(1):
The Common LISP environment follows the algorithm below when interacting with
users:
loop
read in an expression from the console;
evaluate the expression;
print the result of evaluation to the console;
end loop.
Common LISP reads in an expression, evaluates it, and then prints out the
result. For example, if you want to compute the value of (2 * cos(0) * (4 +
6)), you type in:
USER(1): (* 2 (cos 0) (+ 4 6))
Common LISP replies:
20.0
before prompting you to enter the next expression. Several things are worth
noting:
LISP expressions are composed of forms. The most common LISP
form is function application. LISP represents a function call
f(x) as (f x). For example, cos(0) is written as
(cos 0).
LISP expressions are case-insensitive. It makes no difference whether we
type (cos 0) or (COS 0).
Similarly, "+" is the name of the addition function that
returns the sum of its arguments.
Some functions, like "+" and "*", could take an
arbitrary number of arguments. In our example, "*" took three
arguments. It could as well take 2 arguments, as in "(* 2 3)", or 4
arguments, as in "(* 2 3 4 5)".
In general, a function application form looks like (functionargument1argument2 ... argumentn).
As in many programming languages (e.g. C/C++), LISP evaluates function calls
in applicative order, which means that all the argument forms are
evaluated before the function is invoked. That is to say, the argument forms
(cos 0) and (+ 4 6) are respectively evaluated to the
values 1 and 10 before they are passed as arguments to the
* function. Some other forms, like the conditionals we
will see later, are not evaluated in applicative order.
Numeric values like 4 and 6 are called
self-evaluating forms: they evaluate to themselves. To evaluate (+
4 6) in applicative order, the forms 4 and 6 are
respectively evaluated to the values 4 and 6 before they
are passed as arguments to the + function.
M-:
Read a single Lisp expression in the minibuffer, evaluate it, and print the value in the echo area (eval-expression).
C-x C-e
Evaluate the Lisp expression before point, and print the value in the echo area (eval-last-sexp).
C-M-x
Evaluate the defun containing or after point, and print the value in the echo area (eval-defun).
M-x eval-region
Evaluate all the Lisp expressions in the region.
M-x eval-current-buffer
Evaluate all the Lisp expressions in the buffer.
M-: (eval-expression) is the most basic command for evaluating a Lisp expression interactively. It reads the expression using the minibuffer, so you can execute any expression on a buffer regardless of what the buffer contains. When the expression is evaluated, the current buffer is once again the buffer that was current when M-: was typed.
Numeric Functions
Meaning
(+ x1x2 ... xn)
The sum of x1, x2, ...,
xn
(* x1x2 ... xn)
The product of x1, x2, ...,
xn
(- xy)
Subtract y from x
(/ xy)
Divide x by y
(rem xy)
The remainder of dividing x by y
(abs x)
The absolute value of x
(max x1x2 ... xn)
The maximum of x1, x2, ...,
xn
(min x1x2 ... xn)
The minimum of x1, x2, ...,
xn
Common LISP has a rich set of pre-defined numerical functions., Common
LISP, The Language (2nd Edition) (CLTL2) by Guy Steele. In general, we will
not be able to cover all aspects of Common LISP in this tutorial. Adventurous
readers should consult CLTL2 frequently for more in-depth explanation of various
features of the language.