# LISP 1.5 Programmer's Manual
**The Computation Center and Research Laboratory of Electronics**
**Massachusetts Institute of Technology**
> [John McCarthy](https://en.wikipedia.org/wiki/John_McCarthy_(computer_scientist))
> [Paul W. Abrahams](https://mitpress.mit.edu/author/paul-w-abrahams-31449/)
> [Daniel J. Edwards](https://www.chessprogramming.org/Daniel_Edwards)
> [Timothy P. Hart](https://www.chessprogramming.org/Timothy_Hart)
> The M. I.T. Press
> Massachusetts Institute of Technology
> Cambridge, Massachusetts
The Research Laboratory af Electronics is an interdepartmental laboratory in which faculty members and graduate students from numerous academic departments conduct research.
The research reported in this document was made possible in part by support extended the Massachusetts Institute of Technology, Research Laboratory of Electronics, jointly by the U.S. Army, the U.S. Navy (Office of Naval Research), and the U.S. Air Force (Office of Scientific Research) under Contract DA36-039-sc-78108, Department of the Army Task 3-99-25-001-08; and in part by Contract DA-SIG-36-039-61-G14; additional support was received from the National Science Foundation (Grant G-16526) and the National Institutes of Health (Grant MH-04737-02).
Reproduction in whole or in part is permitted for any purpose of the United States Government.
SECOND EDITION Fifteenth printing, 1985
ISBN 0 262 130 1 1 4 (paperback)
-----
#### Note regarding this Markdown document
This Markdown version of the manual was created by me, [Simon Brooke](mailto:simon@journeyman.cc), by passing the PDF version found at [Software Preservation](https://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf) through a [PDF to
Markdown processor](https://pdf2md.morethan.io/), and hand-editing the resulting document.
**This document is not authorised by the copyright holders.** It was made for the purposes of study, only.
Generally I have tried to keep the text unaltered. Some minor headings, especially of examples, have been deliberately changed in order to aid navigation, and some apparent typographic errors have been corrected. *I have also added spaces between syntactic elements in M-expression examples to aid legibility.* Page numbers are taken from the original. Notes which I have added during editing are *NOTE: given in italics, like this*.
-----
## PREFACE
The over-all design of the LISP Programming System is the work of John McCarthy and is based on his paper "[Recursive Functions of Symbolic Expressions and Their Computation by Machine](http://www-formal.stanford.edu/jmc/recursive/recursive.html)" which was published in Communications of the ACM, April 1960.
This manual was written by Michael I. Levin.
The interpreter was programmed by [Stephen B. Russell](https://en.wikipedia.org/wiki/Steve_Russell_(computer_scientist)) and Daniel J. Edwards. The print and read programs were written by John McCarthy, Klim Maling, Daniel J. Edwards, and Paul W. Abrahams.
The garbage collector and arithmetic features Were written by Daniel J. Edwards. The compiler and assembler were written by Timothy P. Hart and Michael I. Levin. An earlier compiler was written by Robert Brayton.
The "LISP 1 Programmer's Manual" March 1, 1960, was written by [Phyllis A. Fox](https://en.wikipedia.org/wiki/Phyllis_Fox). Additional programs and suggestions were contributed by the following members of the Artificial Intelligence Group of the Research Laboratory of Electronics: Marvin L. Minsky, Bertram Raphael, Louis Hodes, David M. R. Park, David C. Luckham, Daniel G. Bobrow, James R. Slagle, and Nathaniel Rochester.
August 17, 1962
## TABLE OF CONTENTS
1. THE LISP LANGUAGE
1. Symbolic Expressions
2. Elementary Functions
3. List Notation
4. The LISP Meta-language
5. Syntactic Summary
6. A Universal LISP Function
2. THE LISP INTERPRETER SYSTEM
1. Variables
2. Constants
3. Functions
4. Machine Language Functions
5. Special Forms
6. Programming for the Interpreter
3. EXTENSION OF THE LISP LANGUAGE
1. Functional Arguments
2. Logical Connectives
3. Predicates and Truth in LISP
4. ARITHMETIC IN LISP
1. Reading and Printing Numbers
2. Arithmetic Functions and Predicates
3. Programming with Arithmetic
4. The Array Feature
5. THE PROGRAM FEATURE
6. RUNNING THE LISP SYSTEM
1. Preparing a Card Deck
2. Tracing
3. Error Diagnostics
4. The cons Counter and errorset
7. LIST STRUCTURES
1. Representation of List Structure
2. Construction of List Structure
3. Property Lists
4. List Structure Operators
5. The Free-Storage List and the Garbage Collector
8. A COMPLETE LISP PROGRAM - THE WANG ALGORITHM FOR THE PROPOSITIONAL CALCULUS
## APPENDICES
A. Functions and Constants in the LISP System
B. The LISP Interpreter
C. The LISP Assembly Program (LAP)
D. The LISP Compiler
E. OVERLORD - The Monitor
F. LISP Input and Output
G. Memory Allocation and the Garbage Collector
H. Recursion and the Push-Down List
I. LISP for SHARE Distribution
* INDEX TO FUNCTION DESCRIPTIONS
* GLOSSARY
page 1
## I. THE LISP LANGUAGE
The LISP language is designed primarily for symbolic data processing. It has been used for symbolic calculations in differential and integral calculus, electrical circuit theory, mathematical logic, game playing, and other fields of artificial intelligence.
LISP is a formal mathematical language. It is therefore possible to give a concise yet complete description of it. Such is the purpose of this first section of the manual. Other sections will describe ways of using LISP to advantage and will explain extensions of the language which make it a convenient programming system.
LISP differs from most programming languages in three important ways. The first way is in the nature of the data. In the LISP language, all data are in the form of symbolic expressions usually referred to as S-expressions. S-expressions are of indefinite length and have a branching tree type of structure, so that significant sub-expressions can be readily isolated. In the LISP programming system, the bulk of available memory is used for storing S-expressions in the form of list structures. This type of memory organization frees the programmer from the necessity of allocating storage for the different sections of his program.
The second important part of the LISP language is the source language itself which specifies in what way the S-expressions are to be processed. This consists of recursive functions of S-expressions. Since the notation for the writing of recursive functions of S-expressions is itself outside the S-expression notation, it will be called the meta language. These expressions will therefore be called M-expressions.
Third, LISP can interpret and execute programs written in the form of S-expressions. Thus, like machine language, and unlike most other higher level languages, it can be used to generate programs for further execution.
### 1.1 Symbolic Expressions
The most elementary type of S-expression is the atomic symbol.
**Definition**: An atomic symbol is a string of no more than thirty numerals and capital letters; the first character must be a letter.
#### Examples - atomic symbols
* A
* APPLE
* PART
* EXTRALONGSTRINGOFLETTERS
* A4B66XYZ
These symbols are called atomic because they are taken as a whole and are not capable of being split within LISP into individual characters, Thus A, B, and AB have no relation to each other except in so far as they are three distinct atomic symbols.
All S-expressions are built out of atomic symbols and the punctuation marks
page 2
`(` `)` and `.`. The basic operation for forming S-expressions is to combine two of them to produce a larger one. From the two atomic symbols A1 and A2, one can form the S-expression `(A1 . A2)`.
**Definition**: An S-expression is either an atomic symbol or it is composed of these elements in the following order: a left parenthesis, an S-expression, a dot, an S-expression, and a right parenthesis.
Notice that this definition is recursive.
#### Examples - S-expressions
* ATOM
* (A B)
* (A . (B C))
* ((A1 . A2) . B)
* ((U V) . (X . Y))
* ((U VI . (X (Y Z)))
### 1.2 Elementary Functions
We shall introduce some elementary functions of S-expressions. To distinguish the functions from the S-expressions themselves, we shall write function names in lower case letters, since atomic symbols consist of only upper case letters. Furthermore, the arguments of functions will be grouped in square brackets rather than parentheses. As a separator or punctuation mark we shall use the semicolon.
The first function that we shall introduce is the function `cons`. It has two arguments and is in fact the function that is used to build S-expressions from smaller S-expressions.
#### Examples - the cons function
```
cons[A; B]=(A . B)
cons[(A . B); C] = ((A . B) . C)
cons[cons[A; B]; C] = ((A . B) . C)
```
The last example is an instance of composition of functions. It is possible to build any S-expression from its atomic components by compositions of the function cons. The next pair of functions do just the opposite of cons. They produce the subexpressions of a given expression.
The function `car` has one argument. Its value is the first part of its composite argument. `car` of an atomic symbol is undefined.
*Note that where this says 'car of an atomic symbol is undefined', it seems to mean it literally. There seems to have been no mechanism for distinguishing cons cells from other items in memory, so that the car of, for example, a decimal number could be taken, although the result
#### Examples - the car function
```
car[(A . B)] = A
car[(A . (B1 . B2))] = A
car[((A1 . A2) . B)] = (A1 . A2)
car[A] is undefined
```
page 3
The function `cdr` has one argument. Its value is the second part of its composite
argument. `cdr` is also undefined if its argument is atomic.
#### Examples - the cdr function
```
cdr[(A . B)] = B
cdr[(A . (B1 . B2))] = (B1 . B2)
cdr[((A1 . A2) . B)] = B
cdr[A] is undefined
car[cdr[(A . (B1 . B2))]] = B1
car[cdr[(A . B)]] is undefined
car[cons[A; B]] = A
```
Given any S-expression, it is possible to produce any subexpression of it by a
suitable composition of `car`s and `cdr`s. If `x` and `y` represent any two S-expressions,
the following identities are true:
```
car[ cons[x; y]] = x
cdr[ cons[x; y]] = y
```
The following identity is also true for any S-expression x such that x is composite
(non-atomic):
```
cons[car[x]; cdr[x]] = x
```
The symbols `x` and `y` used in these identities are called variables. In LISP, variables are used to represent S-expressions. In choosing names for variables and functions, we shall use the same type of character strings that are used in forming atomic
symbols, except that we shall use lower case letters.
A function whose value is either `true` or `false` is called a predicate. In LISP, the
values `true` and `false` are represented by the atomic symbols `T` and `F`, respectively.
A LISP predicate is therefore a function whose value is either `T` or `F`.
The predicate `eq` is a test for equality on atomic symbols. It is undefined for
non-atomic arguments. *NOTE: this differs from the statement given on [page 57](#page57).*
#### Examples - eq
```
eq[A; A] = T
eq[A; B] = F
eq[A; (A . B)] is undefined
eq[(A . B);(A . B)] is undefined
```
The predicate `atom` is true if its argument is an atomic symbol, and false if its
argument is composite.
#### Examples - atom
```
atom[EXTRALONGSTRINGOFLETTERS] = T
atom[(u . v)] = F
atom[car[(u . v)]] = T
```
page 4
### 1.3 List Notation
The S-expressions that have been used heretofore have been written in dot notation. It is usually more convenient to be able to write lists of expressions of indefinite length, such as `(A B C D E)`.
Any S-expression can be expressed in terms of the dot notation. However, LISP has an alternative form of S-expression called the list notation. The list `(m1 m2... mn)` can be defined in terms of dot notation. It is identical to `(m1 . (m2 . (... . (mn . NIL)... )))`.
The atomic symbol NIL serves as a terminator for lists. The null list `()` is identical to `NIL`. Lists may have sublists. The dot notation and the list notation may be used in the same S-expression,
Historically, the separator for elements of lists was the comma `(,)`; however, the blank is now generally used. The two are entirely equivalent in LISP. `(A, B, C)` is identical to `(A B C)`.
#### Examples - list notation
```lisp
(A B C) = (A . (B . (C . NIL)))
((A B) C) = ((A . (B . NIL)) . (C . NIL))
(A B (C D)) = (A . (B . ((C . (D . NIL)). NIL)))
(A) = (A . NIL)
((A))=((A . NIL) . NIL)
(A (B . C)) = (A . ((B . C) . NIL))
```
It Is important to become familiar with the results of elementary functions on S-expressions written in list notation. These can always be determined by translating into dot notation.
#### Examples - list notation 2
```
car[(A B C)] = A
cdr[(A B C)] = (B C)
cons[A; (B C)] = (A B C)
car[((A B) C)] = (A B)
cdr[(A)] = NIL
car[cdr[(A B C)]] = B
```
It is convenient to abbreviate multiple `car`s and `cdr`s. This is done by forming function names that begin with `c`, end with `r`, and have several `a`s and `d`s between them.
#### Examples - composed accessor functions
```
cadr[(A B C)] = car[cdr[(A B C)]] = B
caddr[(A B C)] = C
cadadr[(A (B C) D)] = C
```
page 5
The last `a` or `d` in the name actually signifies the first operation in order to be
performed, since it is nearest to the argument.
### 1.4 The LISP Meta-language
We have introduced a type of data called S-expressions, and five elementary functions of S-expressions. We have also discussed the following features of the meta-language.
1. Function names and variable names are like atomic symbols except that they use lower case letters.
2. The arguments of a function are bound by square brackets and separated from each other by semicolons.
3. Compositions of functions may be written by using nested sets of brackets. These rules allow one to write function definitions such as `third[x]=car[cdr[cdr[x]]]`.
This function selects the third item on a list. For example, `third` is actually the same function as `caddr`.
The class of functions that can be formed in this way is quite limited and not very interesting. A much larger class of functions can be defined by means of the conditional expression, a device for providing branches in function definitions. A conditional expression has the following form:
> where each pi is an expression whose value may be truth or falsity, and each ei is
> any expression. The meaning of a conditional expression is: if p1 is true. then the
> value of e1 is the value of the entire expression. If p1 is false, then if p2 is true
> the value of e2 is the value of the entire expression. The pi are searched from left
> to right until the first true one is found. Then the corresponding ei is selected. If
> none of the pi are true, then the value of the entire expression is undefined.
>
> Each pi or ei can itself be either an S-expression, a function, a composition of
> functions or may itself be another conditional expression.
#### Example - conditional expression
`[eq[car[x]; A] -> cons[B; cdr[x]]; T -> x]`
The atomic symbol `T` represents truth. The value of this expression is obtained
if one replaces `car` of `x` by B if it happens to be A, but leaving `x` unchanged if `car` of
it is not A.
page 6
The main application of conditional expressions is in defining functions recursively.
#### Example - recursive function
`ff[x] = [atom[x] -> x; T -> ff[car[x]]]`
This example defines the function `ff` which selects the first atomic symbol of any
given expression. This expression can be read: If `x` is an atomic symbol, then `x`
itself is the answer. Otherwise the function `ff` is to be applied to car of `x`.
If `x` is atomic, then the first branch which is `x` will be selected. Otherwise, the
second branch `ff[car[x]]` will be selected, since `T` is always true.
The definition of `ff` is recursive in that `ff` is actually defined in terms of itself. If
one keeps taking `car` of any S-expression, one will eventually produce an atomic symbol; therefore the process is always well defined.
Some recursive functions may be well defined for certain arguments only, but infinitely recursive for certain other arguments. When such a function is interpreted in the LISP programming system, it will either use up all of the available memory, or loop until the program is halted artificially.
We shall now work out the evaluation of `ff[((A. B). C)]`. First, we substitute the
arguments in place of the variable `x` in the definition and obtain
```
ff[((A . B) . C)]=[atom[((A . B) . C)]->((A . B) . C); T->ff[car[((A . B) . C)]]]
```
but `((A. B). C)` is not atomic, and so we have
```
= [T->ff [car[((A . B) . C)]]
= ff[car[((A . B) . C)]]
= ff[(A . B)]
```
At this point, the definition of ff must be used recursively. Substituting `(A . B)`
for `x` gives
```
= [atom[(A . B)] -> (A . B); T -> ff[car[(A . B)]]]
= [T -> ff[car[(A . B)]]]
= ff[car[(A . B)]]
= ff[A]
= [atom[A] -> A; T -> ff[car[A]]]
= A
```
The conditional expression is useful for defining numerical computations, as well as computations with S-expressions. The absolute value of a number can be defined by
```
|x| = [x<0 -> -x; T -> x]
```
The factorial of a non-negative integer can be defined by
```
n! = [n=0 -> 1; T -> n.[n-l]!]
```
This recursive definition does not terminate for negative arguments. A function that
page 7
is defined only for certain arguments is called a partial function.
The Euclidean algorithm for finding the greatest common divisor of two positive integers can be defined by using conditional expressions as follows:
```
gcd[x; y]=[x>y -> gcd[y; x];
rem[y;x]=0 -> x]
```
`rem[u; v]` is the remainder when `u` is divided by `v`.
A detailed discussion of the theory of functions defined recursively by conditional expressions is found in [A Basis for a Mathematical Theory of Computation](http://jmc.stanford.edu/articles/basis/basis.pdf) by J. McCarthy, Proceedings of the Western Joint Computer Conference, May 1961 (published by the Institute of Radio Engineers).
It is usual for most mathematicians -- exclusive of those devoted to logic -- to use the word 'function' imprecisely, and to apply it to forms such as y2+x. Because we shall later compute with expressions that stand for functions, we need a notation that expresses the distinction between functions and forms. The notation that we shall use is the [lambda notation of Alonzo Church](https://compcalc.github.io/public/church/church_calculi_1941.pdf).
Let `f`be an expression that stands for a function of two integer variables. It
should make sense to write `f[3; 4]` and to be able to determine the value of this expres-
sion. For example, `sum[3; 4] = 7`. The expression y2 + x does not meet this requirement.
It is not at all clear whether the value of y2 + x[3; 4] is 13 or 19. An expression such as
y2 + x will be called a form rather than a function. A form can be converted to a function by specifying the correspondence between the variables in the form and the arguments of the desired function.
If ε is a form in the variables x1;... ;xn, then the expression λ[[x1;... ;xn]ε] represents the function of n variables obtained by substituting the n arguments in order for the variables x1;... ;xn, respectively. For example, the function λ[[x; y]; y2 + x] is a function of two variables, and λ[[x; y]; y2 + x][3; 4] =42 + 3 = 19. λ[[x; y]; y2 + x][4; 3] = 32 + 4 = 13.
*TODO: detail formatting in the above paragraph is still slightly wrong.*
The variables in a lambda expression are dummy or bound variables because systematically changing them does not alter the meaning of the expression. Thus λ[[u; v]; u2 + v] means the same thing as λ[[x; y]; y2 + x].
We shall sometimes use expressions in which a variable is not bound by a lambda. For example, in the function of two variables λ[[x; y]; xn + yn] the variable `n` is not bound. This is called a free variable. It may be regarded as a parameter. Unless `n` has been given a value before trying to compute with this function, the value of the function must be undefined.
page 8
The lambda notation alone is inadequate for naming recursive functions. Not only must the variables be bound, but the name of the function must be bound, since it is used inside an expression to stand for the entire expression. The function `ff` was previously defined by the identity
`ff[x] = [atom[x] -> x; T -> ff[car[x]]]`
Using the lambda notation, we can write
`ff =` λ`[x] = [atom[x] -> x; T -> ff[car[x]]]`
The equality sign in these identities is actually not part of the LISP meta-language and is only a crutch until we develop the correct notation. The right side of the last equation cannot serve as an expression for the function `ff` because there is nothing to indicate that the occurrence of `ff` inside it stands for the function that is being defined.
In order to be able to write expressions that bear their own name, we introduce
the label notation. If ε is an expression, and α is its name, we write label[α; ε].
The function `ff` can now be written without an equal sign:
`label[ff =` λ`[[x]; [atom[x] -> x; T -> ff[car[x]]]]`
In this expression, `x` is a bound variable, and `ff` is a bound function name.
### 1.5 Syntactic Summary
[This section is for completeness and may be skipped upon first reading.]
All parts of the LISP language have now been explained. That which follows is a complete syntactic definition of the LISP language, together with semantic comments. The definition is given in [Backus notation](https://www.softwarepreservation.org/projects/ALGOL/paper/Backus-ICIP-1959.pdf) with the addition of three dots(...) to avoid naming unnecessary syntactic types.
In Backus notation the symbols `::=`, `<`, `>`, and `|` are used. The rule
```BNF
::= | ( . )
```
means that an S-expression is either an atomic symbol, or it is a left parenthesis followed by an S-expression followed by a dot followed by an S-expression followed by a right parenthesis. The vertical bar means "or" , and the angular brackets always enclose elements of the syntax that is being defined.
#### The Data Language
```BNF
::= A|B|C| ... |Z
::= 0|1|2| ... |9
::=
::= | |
```
Atomic symbols are the smallest entities in LISP. Their decomposition into characters has no significance.
page 9
```BNF
::= |
( . ) |
( ... )
```
When three dots are used in this manner, they mean that any number of the given type of symbol may occur, including none at all. According to this rule, `( )` is a valid S-expression. (It is equivalent to `NIL`. )
The dot notation is the fundamental notation of S-expressions, although the list notation is often more convenient. Any S-expression can be written in dot notation.
#### The Meta-Language
```BNF
::= a|b|c| ... |z
::=
::= | |
```
The names of functions and variables are fornied in the same manner as atomic symbols but with lower-case letters.
```BNF