### 37.1 Lisp and Maxima

Maxima is a fairly complete programming language. But since it is written in Lisp, it additionally can provide easy access to Lisp functions and variables from Maxima and vice versa. Lisp and Maxima symbols are distinguished by a naming convention. A Lisp symbol which begins with a dollar sign `$` corresponds to a Maxima symbol without the dollar sign.

A Maxima symbol which begins with a question mark `?` corresponds to a Lisp symbol without the question mark. For example, the Maxima symbol `foo` corresponds to the Lisp symbol `$FOO`, while the Maxima symbol `?foo` corresponds to the Lisp symbol `FOO`. Note that `?foo` is written without a space between `?` and `foo`; otherwise it might be mistaken for `describe ("foo")`.

Hyphen `-`, asterisk `*`, or other special characters in Lisp symbols must be escaped by backslash `\` where they appear in Maxima code. For example, the Lisp identifier `*foo-bar*` is written `?\*foo\-bar\*` in Maxima.

Lisp code may be executed from within a Maxima session. A single line of Lisp (containing one or more forms) may be executed by the special command `:lisp`. For example,

    (%i1) :lisp (foo $x $y)

calls the Lisp function `foo` with Maxima variables `x` and `y` as arguments. The `:lisp` construct can appear at the interactive prompt or in a file processed by `batch` or `demo`, but not in a file processed by `load`, `batchload`, `translate_file`, or `compile_file`.

The function `to_lisp` opens an interactive Lisp session. Entering `(to-maxima)` closes the Lisp session and returns to Maxima.

Lisp functions and variables which are to be visible in Maxima as functions and variables with ordinary names (no special punctuation) must have Lisp names beginning with the dollar sign `$`.

Maxima is case-sensitive, distinguishing between lowercase and uppercase letters in identifiers. There are some rules governing the translation of names between Lisp and Maxima.

1. A Lisp identifier not enclosed in vertical bars corresponds to a Maxima identifier in lowercase. Whether the Lisp identifier is uppercase, lowercase, or mixed case, is ignored. E.g., Lisp `$foo`, `$FOO`, and `$Foo` all correspond to Maxima `foo`. But this is because `$foo`, `$FOO` and `$Foo` are converted by the Lisp reader by default to the Lisp symbol `$FOO`.
2. A Lisp identifier which is all uppercase or all lowercase and enclosed in vertical bars corresponds to a Maxima identifier with case reversed. That is, uppercase is changed to lowercase and lowercase to uppercase. E.g., Lisp `|$FOO|` and `|$foo|` correspond to Maxima `foo` and `FOO`, respectively.
3. A Lisp identifier which is mixed uppercase and lowercase and enclosed in vertical bars corresponds to a Maxima identifier with the same case. E.g., Lisp `|$Foo|` corresponds to Maxima `Foo`.

The `#$` Lisp macro allows the use of Maxima expressions in Lisp code. `#$expr$` expands to a Lisp expression equivalent to the Maxima expression expr.

    (msetq $foo #$[x, y]$)

This has the same effect as entering

    (%i1) foo: [x, y];

The Lisp function `displa` prints an expression in Maxima format.

    (%i1) :lisp #$[x, y, z]$
    ((MLIST SIMP) $X $Y $Z)
    (%i1) :lisp (displa '((MLIST SIMP) $X $Y $Z))
    [x, y, z]
    NIL

Functions defined in Maxima are not ordinary Lisp functions. The Lisp function `mfuncall` calls a Maxima function. For example:

    (%i1) foo(x,y) := x*y$
    (%i2) :lisp (mfuncall '$foo 'a 'b)
    ((MTIMES SIMP) A B)

Some Lisp functions are shadowed in the Maxima package, namely the following.

       complement     continue      //
       float          functionp     array
       exp            listen        signum
       atan           asin          acos
       asinh          acosh         atanh
       tanh           cosh          sinh
       tan            break         gcd

Categories:Programming ·