## Use of Lisp

Maxima is written in Lisp, a really unique programming language that was developed
by John McCarthy at MIT. The earliest publication about Lisp is possibly:

McCarthy, John:
**Recursive Functions of Symbolic Expressions and Their Computation, Part I**
Communications of the ACM, Vol. 3, April 1960, pp. 184-195

To enter a piece of Lisp, you write:

:lisp (list 'a 'b 'c)
:q(a b c)

To enter the Lisp mode, you write **:lisp**, to quite the Lisp mode and return to
Maxima, you write **:q** into a new line. The Lisp text itself must not contain a line break.

Alternatively, you can enter

to_lisp();

to enter the list mode. Here is the complete transcript of a short Lisp session:
(%i1) to_lisp();
Type (to-maxima) to restart
MAXIMA> (cons 'a 'b)
(A . B)
MAXIMA> (append '(a b c) '(d e f))
(A B C D E F)
MAXIMA> (reverse '(a b c))
(C B A)
MAXIMA> (mapcar '(lambda(x) (list x)) '(a b c))
((A) (B) (C))
MAXIMA> (run)
Maxima restarted.

These are indeed very simple Lisp expressions. They should bring your Lisp knowledge
back to live and give you a few hints about the dialect that is used for Maxima.

### The internal respresentation of expressions

In Lisp mode, you can access the internal representation of expressions
stored in result cells. To obtain a result, you enter the label of the
result cell written between vertical slashes and with a prefixed dollar
sign:

(%i1) (sin(x) + cos(x) + sin(x)/sec(x));
sin(x)
(%o1) ------ + sin(x) + cos(x)
sec(x)
(%i2) to_lisp();
Type (to-maxima) to restart
MAXIMA> |$%o1|
((MPLUS SIMP) ((%COS SIMP) |$x|) ((%SIN SIMP) |$x|)
((MTIMES SIMP) ((MEXPT SIMP) ((%SEC SIMP) |$x|) -1)
((%SIN SIMP) |$x|)))
MAXIMA> (to-maxima)

**|$%o1|** answers the internal representation of the expression
in value cell **%o1**. For better readability, it is given here in
formatted form:

((MPLUS SIMP)
((%COS SIMP) |$x|)
((%SIN SIMP) |$x|)
((MTIMES SIMP)
((MEXPT SIMP)
((%SEC SIMP) |$x|)
-1
)
((%SIN SIMP) |$x|)
) )

(%i1) 'integrate(sin(x)*exp(x), x);
/
[ x
(%o1) I %e sin(x) dx
]
/
(%i2) to_lisp();
Type (to-maxima) to restart
MAXIMA> |$%o1|
((%INTEGRATE SIMP)
((MTIMES SIMP) ((MEXPT SIMP) $%E |$x|) ((%SIN SIMP) |$x|))
|$x|)

The second top-level element **(cadr |$%o1|)** is the integrand, the
third top-level element **(caddr |$%o1|)** is the integration variable.
With this knowledge we can carry out the integration in Lisp:

MAXIMA> ($INTEGRATE (cadr |$%o1|) (caddr |$%o1|))
((MTIMES SIMP) ((RAT SIMP) 1 2) ((MEXPT SIMP) $%E |$x|)
((MPLUS SIMP) ((MTIMES SIMP) -1 ((%COS SIMP) |$x|))
((%SIN SIMP) |$x|)))
MAXIMA> (to-maxima)