Weismann Lisp1.5 Primer 1967 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 243

(LISP 1.

5 PRIMER
' INDEX
FUNC 'FION DESCRIPTIONS

l!msuaL No. Arnuments Type & Pqze Function No. Arnumcnts . Type & Page

ABSVAL 1 function, 85 FACTORIAL 1 example, 96, 109

ADD1 1 function, 83 FMP 1 predicate, 76

AND indefinite predicate, special FLOAT 1 function, 85

form, 78 FLOATP 1 predicate, 76

APPEND 2 example, 97 FUNCTION 1 special form, 137

ASSOC 2 example, 98 GENSYM 0 pseudo -funct ion, 124

ATOM 1 predicate, 74 GET 2 function, 150

CA--AR, CD--DR 1 function, 35,37, 56 GO 1 pseudo-function, 108

CAR 1 function, 31 GREATERP 2 predicate, 77

CDR 1 function, 32 INTERSECTION 2 example INTERSECT,

COND indefinite special form, 70 104

CONS 2 function, 30 LABEL 1 special form, 98

CSET 2 pseudo-function, 118 LAMBDA 2 special form, 39

CSETQ pseudo-funct ion, 118 LEFTSHIFT 2 function, 86

(macro), (156) LENGTH 1 example LENGTHS,

DEFINE pseudo-function, 66 103, 111


DELETE example, 104, i12 LESSP 2 predicate, 77

DIFFERENC E function, 83 LIST indefinite special form, 58

DIVIDE function, 84 (macro) , (159)


ENTIER function, 85 LOGAND indefinite special form, 86

EQ predicate, 75 LOGOR indefinite special form, 86

EQUAL predicate, 75 LOGXm indefinite special form, 86

(example) , (97) MACRO 1 pseudo-function, 152

function, 61 MAP 2 functional, 138

special form, 60, MAPC 2 functional, 140


131 NAPCAR 2 functional, 139

EVENP predicate, 77 WLIST 2 functional, 138


*ExPAM) function, 153 MAX indefinite special form, 84.

M PT 2 function, 84 (macro), (159)

(Continued on inside back cover)


(LISP 1.5 PRIMER
DICKENSON SERIES IN COMPUTER
AND INFORMATION SCIENCE

Fred M. Tonge, Editor

Introduction to Business Data Processing


Norman H. Carter

Principles of Data Processing


William J. Claffey

Principles of Programming the IBM 1620 Camputer


William J. Claffey

Autocodei. Programming for the IBM 1400 Series Computers


Leeland R. O'Neal

An Introduction to COBOL Computer Programming for


Accounting and Business Analysis
Donald L. Raun

An Introduction to FORTRAN Computer Programming for


Business Analysis
Donald L. Raun

An Introduction to Business Data Processing and Computer


Programming
Robert W. Swanson

LISP 1.5 Primer


Clark Weissrnan

FORTRAN 11-D on the IBM 1620: Introduction to Digital


Computer Programming
S. Thomas Cleveland

System 360/20 RPG Programming


James A. Saxon
(LISP 1.5 PRIMER

Programming Systems Staff Head


Research and Technology Division
System Development Corporation

DICKENSON PUBLISHING COMPANY, INC., BlELMBNT, CALIFORNIA


@I967 b y D i c k e n s o n P u b l i s h i n g C o m p a n y , Inc.,
Belmont, Califorqia. All rights reserved. No part of
this book may bq reproduced in any form, by mimeo-
graph or any other means, without permission in
writing from the 'publisher.

Library of Congress catalog card number: 67-20611


Printed in the ~ d i t e dStates of America
PREFACE

The original LISP programming system was implemented at M.1.T.l based


upon a paper by John McCarthy, "~ecursiveFunctions of Symbolic Expressions
and Their Computation by Machine," which was published in Communications of
the ACM, April 1960. Since then, numerous other LISP systems have been imple-
mented on a variety of computers.2'394'536 All these systems are based
upon the formal LISP language; however, they do possess differences in
implementation brought about by machine differences and technological advances
in efficient machine utilization. Earlier systems used interpreters lor
evaluating LISP expressions; more recent systems compile machine code to perfom
the desired symbolic manipulations. Other technological innovations, such as
time-sharing operating systems, have also influenced the outward, user-view of
LISP systems. To transcend these and other differences between LISP systems,
various pedagogical devices are used in this manual. Hopefully, these devices
will approximate features available with specific LISP implementations. They
include: a compiler-based LISP 1.5 system, an EVALQUOTE supervisor, a full
complement of character handling primitives, MACRO capability, and a canonical
form for input and output of numbers, atoms, and symbolic expressions.

The primer starts simply with a formaL definition of a symbolic expression, its
syntax and graphical representation. Two alternative machine-readable notations
are defined for representing symbolic expressions. Once the student becomes
familiar with recognizing symbolic expressions, he learns how to take them apart
into smaller elements or put them together into larger expressions using the
elementary LISP functions CAR, CDR, and CONS. The early chapters of this book
are essential to the understanding of LISP. They expose the reader to the
LISP formalism and give him an opportunity to acquire the necessary skills for
processing symbolic data. Learning these skills is analogous to learning the
rules of arithmetic.

Lambda expressions are introduced immediately after the fundamentals of symbolic


expressions. Lambda expressions are the basic functional syntax of LISP. LISP
functions are analogous to procedures used in algebraic languages, and derive
d i r e c t l y from t h e mathematics of r e c u r s i v e f u n c t i o n t h e o r y . The c o n c e p t s of
e v a l u a t i o n and q u o t i n g a r e e s t a b l i s h e d and used t o e x p l a i n t h e n a t u r e of
EVALQUOTE, t h e " u n i v e r s a l " LISP f u n c t i o n and system s u p e r v i s o r t h a t c a n
compute t h e v a l u e of any computable f u n c t i o n a p p l i e d t o i t s arguments.

C o n d i t i o n a l e x p r e s s i o n s , p r e d i c a t e s , and a r i t h m e t i c f u n c t i o n s add controll and


c o m p u t a t i o n a l power t o t h e language and complete t h e s e t of b a s i c e x p r e s s i o n s
t h a t may b e composed i n LISP. A t t h i s point recursion is introduced.

R e c u r s i o n i s a t e c h n i q u e f o r d e f i n i n g a computation on a g i v e n datum. The


p r o c e s s u s u a l l y produces a p a r t i a l s o l u t i o n and r e d u c e s t h e datum t o a s i m p l e r
form. The same p r o c e s s i s t h e n r e a p p l i e d t o t h i s s i m p l e r form of t h e datum.
Again a p a r t i a l s o l u t i o n and a s i m p l e r form a r e o b t a i n e d . The p r o c e s s con-
t i n u e s u n t i l some t e r m i n a l datum o b t a i n s , whereupon a l l p a r t i a l s o l u t i o n s a r e
combined i n some f a s h i o n t o produce t h e f i n a l s o l u t i o n . To compute r e c u r s i v e l y
t h e f a c t o r i a l of N , f o r example, we have

where N i s t h e p a r t i a l s o l u t i o n and (N-1) i s t h e s i m p l e r form upon which we


r e p e a t t h e f a c t o r i a l computation. This process recurs u n t i l t h e terminal
c o n d i t i o n N = 0 i s r e a c h e d , whereupon t h e p a r t i a l r e s u l t s a r e combined t o
form t h e f i n a l answer; e . g . ,

Recursion i s a s n a t u r a l t o symbolic d a t a m a n i p u l a t i o n a s i t e r a t i o n i s t o
numerical d a t a processing. LISP i s d e s i g n e d t o make r e c u r s i o n e a s y t o u s e ,
and r e c u r s i v e f u n c t i o n s a r e a s i g n i f i c a n t p a r t of t h e domain of LISP expres-
sions. S i n c e n u m e r i c a l d a t a i s a l s o allowed i n LISP, i t e r a t i v e f u n c t i o n s c a n
b e d e f i n e d u s i n g t h e PROG x e a t u r e , i n which s t a t e m e n t s a r e e v a l u a t e d i n ALGOL-
l i k e s e r i a l fashion.

Beyond Chapter 1 5 , t h e primer i s devoted t o advanced t e c h n i q u e s f o r e x t e n d i n g


t h e domain of symbolic e x p r e s s i o n s u s i n g f u n c t i o n a l arguments, macros, p r o p e r t y
l i s t s , and l i s t s t r u c t u r e s . Also', t h i s p o r t i o n of t h e primer d e s c r i b e s i n p u t -
o u t p u t , and d e l v e s more d e e p l y i n t o t h e n a t u r e of v a r i a b l e s , v a l u e s #and t h e i r
association. The last chapter provides a review of foregoing chapters by
describing a complete LISP program that differentiates an algebraic polynomial.

LISP is not an easy language to learn because of the functional syntax and
insidious parenthetical format; this is particularly true for those experienced
with more conventional programming languages. However, LISP is consistent in
its syntax no matter how complex the expression. Careful attention to this
fact may make learning easier. The carefully graduated sets of exercises can
help in this regard. They have been selected for use with or without a computer.
They may be used on-line if a time-sharing system is at hand. Otherwise, the
solutions given in Appendix A can be used for comparison. These solutions have
been computer-checked for accuracy and correctness.

Santa Monica, California Clark Weissman


December, 1966

vii
ACKNOWLEDGMENTS

I wish t o acknowledge t h e c o n s i d e r a b l e s u p p o r t I r e c e i v e d from t h e


System Development Corporation i n p r e p a r i n g a n e a r l i e r v e r s i o n of
t h i s t e x t f o r t h e AN/FSQ-32 computer time-sharing system.

Kudos t o D r . Daniel G. Bobrow of B o l t , Beranek and Newman and t o


Michael L e v i t t of Systems Concepts f o r t h e i r p e r s e v e r a n c e i n
reviewing t h e t e x t and f o r t h e i r s u g g e s t i o n s r e g a r d i n g t e c h n i c a l
c o n t e n t and p r e s e n t a t i o n . Thanks a r e extended t o Robert W i l l s of
SDC f o r h i s e d i t o r i a l h e l p and f o r l i n e a r i z i n g my o f t - t i m e s
" r e c u r s i v e " E n g l i s h grammar. Thanks a l s o t o P a t r i c i a G i c i e f o r
c a r e f u l l y counting p a r e n t h e s e s a s s h e typed and retyped t h e many
f i n a l manuscripts.

To D r . S t a n l e y Kameny, Robert Saunders, Donna F i r t h , J e f f B a r n e t t ,


D r . Marvin Minsky, a l l my s t u d e n t s , and o t h e r s i n t h e LISP communit:y,
my s i n c e r e a p p r e c i a t i o n f o r t h e i r e n t h u s i a s t i c r e c e p t i o n of t h e
e a r l i e r v e r s i o n 05 t h e primer; f o r t h e i r s u g g e s t i o n s based upon
t r i a l experience; f o r c o n t r i b u t i n g e x e r c i s e s , and f o r checking
t e c h n i c a l consistency.

Above a l l , I wish t o thank my w i f e , Marcia, f o r h e r encouragement,


self-imposed e x i l e and t h e p a t i e n t c a r e s h e bestowed on me d u r i n g
t h e p r e p a r a t i o n of t h i s manuscript.

C l a r k Weissman

viii
TABLE OF CONTENTS

Page
PREFACE . ......................... .... . iii

ACKNOWLEDGMENTS. . . . . . . . . . . . . . . . . . . . . . .... .
CHAPTER 1 INTRODUCTION . . . . . . . . . . . . . . . . . .... .
1.1 Purpose of This Document . . . . . . . . . . . . .
1.2 Document Conventions . . . . . . . . . . . . . . .
1.3 LISPApplications. . . . . . . . . . . . . . . . .
CHAPTER2 SYMBOLICEXPRESSIONS . . . . . . . . . . . . . .... .
2.1 AtomicSymbols . . . . . . . . . . . . . . . . . .
2.2 Dot Notation . . . . . . . . . . . . . . .... .
2.3 Graphical Representation of Dotted Pairs .... .
2.4 Exercises . . . . . . . . . . . . . . . . .... .
CHAF'TER 3 SYMBOLIC EXPRESSIONS IN LIST NOTATION . . . . . .... .
3.1 ListElements . . . . . . . . . . . . . . .... .
3.2 NIL . . . . . . . . . . . . . . . . . . . .... .
3.3 Transforming List Notation to Dot Notation . . . .
3.4 Transforming Dot Notation to List Notation . . . .
3.5 Graphical Representation of Lists . . . . . . . . .
3.6 Exercises . . . . . . . . . . . . . . . . . . . . .

CHAPTER 4 NUMBERS . . . . . . . . . . . . . . . . . . . . . . . . .
4.1 Integer Numbers . . . . . . . . . . . . . . . . . .
4.2 OctalNumbers. . . . . . . . . . . . . . . . . . . .
4.3 Floating-Point Numbers . . . . . . . . . . . . . . .
4.4 Decimal Point or Dotted Pair Resolution . . . . . .
4.5 Exercises . . . . . . . . . . . . . . . . . . . . .

CHAPTER 5 ELEMENTARY FUNCTIONS . . . . . . . . . .


. . . . . . . .
5.1 Rapport with the Supervisor. . . .
. . . . . . . .
5.2 CONS . . . . . . . . . . . . . . .
. . . . . . . .
5.3 CAR . . . . . . . . . . . . . . . .
. . . . . . . .
5.4 CDR . . . . . . . . . . . . . . . . . . . . . . . .
5.5 Graphical Interpretation of CAR and CDR . . . . . .
5.6 Exercises . . . . . . . . . . . . . . . . . . . . .
CHAPTER 6 LAMBDA NUCATION .. . . . . . . . . . . . . . . . . . . . 38
6.1 Forms and Functions . . . . . . . . . . . . . . . . 38
6.2 Lambda Expressions . . . . . . . . . . . . . . . . 39
6.3 Lambda Conversion. . . . ............. 41
6.4 Parentheses. . . . . . . ............. 43
6.5 Durmny Variables . . . . . ............. 44
6.6 Exercises . . . . . . . . ............. 45

CHAPTER 7 ELEMENTARY FORMS . . . . . . . ............. 46


7.1 Variables . . . . . . . . ............. 46
7.2 Constants. . . . . . . . ............. 47
7.3 Simple Forms . . . . . . ............. 47
7.4 Exercises . . . . . . . . . . . . . . . . . . . . . 49
CHAPTER8 COMPOSITIONOFFORMS . . . . . ............. 50
8.1 Composed Forms . . . . . ............. 50
8.2 Evaluating Composed Forms. . . . . . . . . . . . . 51
8.3 Nested Lambda Expressions . . . . . . . . . . . . . 53
8.4 E v a l u a t i n g N e s t e d L a m b d a E x p r e s s i o n s . . . . . . . 54
8.5 Exercises . . . . . . . . . . . . . . . . . . . . . 56

CHAPTER 9 SPECIAL FORMS. . . . . . . . . . . . . . . . . . . . . . 58


9.1 LIST . . . . . . . . . . . . . . . . . . . . . . . 58
9.2 QUOTE . . . . . . . . . . . . . . . . . . . . . . . 59
9.3 EVALQUOTE . . . . . . . . . . . . . . . . . . . . . 60
9.4 Exercises . . . . . . . . . . . . . . . . . . . . . 64
CHAPTEX 10 DEFINE . . . . . . . . . . . . . . . . . . . . . . . . . 66
10.1 DEFINE Syntax. . . . . . . . . . . . . . . . . . . 66
10.2 Evaluating DEFINE . . . . . . . . . . . . . . . . . 68
10.3 Redefining . . . . . . . . . . . . . . . . . . . . 68
10.4 Exercises . . . . . . . . . . . . . . . . . . . . . 69
CHAPTER 11 CONDITIONAL EXPRESSIONS ................. 70
11.1 Syntax of Conditional Expressions . . . . . . . . . 70
11.2 Evaluating Conditional Expressions . . . . . . . . 70
11.3 SEUCT . . . . . . . . . . . . . . . . . . . . . . 72
11.4 Exercises . . . . . . . . . . . . . . . . . . . . . 73
Page
CHAPTER 1 2 PREDICATEFUNCTIONS . .... . .............
12.1 ATOM . . . . . .... . .............
12.2 EQ . . . . . . .... . .............
12.3 EQUAL . . . . . .... . .............
12.4 Arithmetic Predicates . . .............
12.5 L i s t p r e d i c a t e s . . . . . .............
12.6 LogicalConnectives . . . .............
12.7 Exercises . . . . . . . . .............
CHAPTER 1 3 ARITHMETIC FUNCTIONS . . . . . .............
1 3 . 1 G e n e r a l Comments . . . . .............
1 3 . 2 LISP A r i t h m e t i c F u n c t i o n s . . . . . . . . . . . .
.
13.3 Logical Arithmetic Functions . . . . . . . . . . .
1 3 . 4 An A r i t h m e t i c Example . . . . . . . . . . . . . . .
13.5 Exercises . . . . . . . . . . . . . . . . . . . . .

CHAPTER 1 4 RECURSIVE FUNCTIONS . . . . . . . . . . . . . . . . . . .


1 4 . 1 A R e c u r s i v e Example . . . . . . . . . . . . . . . .
1 4 . 2 Some H e l p f u l H e u r i s t i c s . . . . . . . . . . . . . .
1 4 . 3 More R e c u r s i v e Examples . . . . . . . . . . . . . .
14.4 LabelNotation . . . . . . . . . . . . . . . . . .
14.5 Exercises . . . . . . . . . . . . . . . . . . . . .

CHAPTER 1 5 THE PROGRAM FEATURE . . . . . . . . . . . . . . . . . . .


1 5 . 1 PROGFormat . . . . . . . . . . . . . . . . . . . .
1 5 . 2 Program V a r i a b l e s . . . . . . . . . . . . . . . . .
1 5 . 3 Flow of C o n t r o l . . . . . . . . . . . . . . . . . .
15.4 Somecautions . . . . . . . . . . . . . . . . . . .
1 5 . 5 Examples . . . . . . . . . . . . . . . . . . . . .
1 5 . 6 PROG2 . . . . . . . . . . . . . . . . . . . . . . .
15.7 Exercises . . . . . . . . . . . . . . . . . . . . .

CHAPTER 16 VARIABLES AND THEIR BINDING . . . . . ..........


16.1 Boundvariables ... . . . . . ..........
16.2 The A-List . . . . . . . . . . . . . . . . . . . .
16.3 Free Variables ... . . . . . ..........
16.4 Constants ...... . . . . . ..........
16.5 CompilerBindings . . . . . . . . . . . . . . . . .
16.6 Exercises . . . . . . . . . . . . . . . . . . . . .
CHAPTER 1 7 DEBUGGING. 1NWT.OUTPUT. AND SUPERVISORS . . . . . . . . 121
Program Debugging . .
....... . . . . . . ..
Diagnostic Tools . .
....... . . . . . . ..
I n p u t -Output .. . .
....... . . . . . . ..
Input-Output P r i m i t i v e s . . . . . . . . . . . . . .
The OBLIST ... . .
...............
..........
U n u s u a l l y S p e l l e d L i t e r a l Atoms
Character Objects . .
. . . . . . . . . . . . . . .
Supervisors ... . .
...............
Exercises .... . .
...............
CHAPTER 1 8 FUNCTIONAL ARGUMENTS . . .
............... 137
18.1 FUNCTION . . . . . .
............... 137
1 8 . 2 MAP . . . . . . . . .
............... 138
18.3 MAPLIST . . . . . . .
. . . . . . . . . . . . . . . 138
1 8 . 4 MAPCAR . . . . . . .
. . . . . . . . . . . . . . . 139
1 8 . 5 MAPC . . . . . . . .
............... 140
18.6 Cautions . . . . . .
. . . . . . . . . . . . . . . 141
18.7 Exercises. . . . . .
............... 141

CHAPTER 1 9 LIST STRUCTURES. PROPERTY LISTS. AND MACROS . . . . . . . 144


Graphical Representation of L i s t S t r u c t u r e . . . .
Manipulating L i s t Structure . . . . . . . . . . . .
Examples o f M o d i f y i n g L i s t S t r u c t u r e . . . . . . .
Property L i s t s . . . . . . . . . . . . . . . . . .
Examples o f P r o p e r t y L i s t F u n c t i o n s . . . . . . . .
Macros . . . . . . . . . . . . . . . . . . . . . .
MACRO E x p a n s i o n . . . . . . . . . . . . . . . . . .
MACRO D e f i n i t i o n s o f New F u n c t i o n s . . . . . . . .
Exercises . . . . . . . . . . . . . . . . . . . . .

CHAPTER 20 A COMPLETE LISP PROGRAM . . . . . . . . . . . . . . . . . 161


20.1 Polynomial Syntax. . . . . . . . . . . . . . . . . 161
2 0 . 2 Program S t r a t e g y . . . . . . . . . . . . . . . . . 162
2 0 . 3 IN2PRE . . . . . . . . . . . . . . . . . . . . . . 163
2 0 . 4 DERIV . . . . . . . . . . . . . . . . . . . . . . . 166
20.5 SIMPLIFY . . . . . . . . . . . . . . . . . . . . . 170
2 0 . 5 . 1 SPLUS . . . . . . . . . . . . . . . . . . . 172
2 0 . 5 . 2 STIMES . . . . . . . . . . . . . . . . . . 173
20 .. 5 . 3 COLLEXT . . . . . . . . . . . . . . . . . . 174
2 0 . 5 . 4 SQUOTIENT. . . . . . . . . . . . . . . . . 175
2 0 . 5 . 5 SEXPT . . . . . . . . . . . . . . . . . . . 176
20.5.6 SMINUS . . . . . . . . . . . . . . . . . . 176
20.6 PRE2IN . . . . . . . . . . . . . . . . . . . . . . 177
20.7 DIFF . . . . . . . . . . . . . . . . . . . . . . . 179

xii
Page
APPENDIX A EXERCISE ANSWERS ................... 183
APPENDIX B GLOSSARY . . . . . . . . . . . . . . . . . . . . . . .

APPENDIX C REFERENCES......................
APPENDIX D INDEX TO TECHNICAL TERMS . . . . . . . . . . . . . . .

xiii
CHAPTER 1.
INTRODUCTION

PURPOSE OF THIS DOCUMENT

This manual has two principal goals: (1) to introduce the programming language
LISP and present a systematic exposition of symbolic computation, and (2) to
serve as a self-tutor for those wishing to acquire a practical facility with
the LISP 1.5 programming language.

LISP is an unusual language in that it is both a formal mathematical language,


and (with extensions) a convenient programming language. As a formal mathemat-
ical language, it is founded upon a particular part of mathematical logic known
as recursive function theory. As a programming language, LISP is concerned
primarily with the computer processing of symbolic data rather than numeric data.

From childhood we are exposed to numbers and to ways of processing numerical


data, such as basic arithmetic and solutions to algebraic equations. This
exposure is based upon a well-established and rigorously formalized science of
dealing with numbers. We are also exposed to symbolic data--such as names,
labels, and words--and to ways of processing such data when we sort, alphabetize,
file, or give and take directions. Yet the processing of symbolic data is not
a well-established science. In learnzng an algebraic programming language, such
as FORTRAN or ALGOL, we call upon our experience ,withnumbers to help us under-
stand the structure and meaning (syntax and semantics) of the language.

In learning a symbolic programming language such as LISP, however, we cannot call


upon our experience, because the formalism of symbolic data processing is not
part of this experience. Thus, we have the added task of learning a basic set
of formal skills for representing and manipulating symbolic data before we can
study the syntax and semantics of the LISP 1.5 programming language.

LISP is designed to allow symbolic expressions of arbitrary complexity to be


evaluated by a computer. To achieve a thorough understanding of the meaning,
structure, construction, and evaluation of symbolic expressions, is to learn
how t o program i n LISP. T h i s primer s e e k s t o develop such understanding
g r a d u a l l y by b u i l d i n g new m a t e r i a l upon o l d e r m a t e r i a l , and by expanding t h e
scope of d e f i n i t i o n of p r i o r concepts. The primer provides a p r a c t i c a l
f o u n d a t i o n f o r understanding t h e programming language; supplemental r e a d i n g s
can t h e n augment .the m a t e r i a l p r e s e n t e d h e r e .

DOCUMENT CONVENTIONS

I n t h e n a r r a t i v e p o r t i o n s of t h i s manual, c e r t a i n elements of LISP can e a s i l y


be confused w i t h normal English. To h e l p c l a r i f y word and p h r a s e usage from
sample LISP code, a number of conventions a r e followed h e r e i n .

When r e f e r e n c e i s made s p e c i f i c a l l y t o LISP elements, LISP s y n t a x , o r LISP


s e m a n t i c s , t h e f i r s t occurrence of a s i g n i f i c a n t term o r d e f i n i t i o n i s s i g n i -
f i e d by t h e u s e of i t a l i c s . An example, d e f i n e d i n t h e n e x t c h a p t e r , i s t h e
t e r m S-expression.

Most LISP program examples, p a r t s of programs, and f u n c t i o n and v a r i a b l e names


a r e typed i n c a p i t a l l e t t e r s , analogous t o t h e form of i n p u t accepted by LISP
systems from keypunched c a r d decks o r on-line keyboards. C e r t a i n o t h e r program
examples c o n t a i n a mix of c a p i t a l l e t t e r s and i t a l i c i z e d l e t t e r s . I n suck1
c a s e s , t h e i t a l i c s a r e used t o denote "meta-linguistic" variables t h a t a r e not
p a r t of t h e LISP language, b u t a r e used i n t h e accompanying t e x t t o d e s c r i b e
l e g a l LISP forms. An example, d i s c u s s e d i n Chapter 6, i s t h e form of a lambda
expression:

(LAMBDA v u r l i s t body)

where t h e meta-variables v a r l i s t and body a i d i n d e s c r i b i n g t h e s y n t a x of


lambda e x p r e s s i o n s . Also t h e e l l i p s i s ( ... ) i s n o t p a r t of t h e LISP language,
b u t merely a d e v i c e used f o r d e s i g n a t i n g a sequence of i n d e f i n i t e l e n g t h . Lower-
c a s e e l i t e l e t t e r s a r e a l s o used i n many c a s e s a s "pedagogict1 v a r i a b l e s 01:

examples of l e g a l c l a s s e s . These a r e used t o a i d i n d e s c r i b i n g t h e semant:ics


of a given e x p r e s s i o n . For example, i n Chapter 11, t h e c o n d i t i o n a l e x p r e s s i o n
uses p and e as such pedagogic variables.
i i

When a programming example is given, it is usua1:Ly separated from the body of


the text and placed on a separate line without normal English punctuation,
exactly as it might be entered into the computer. At other times, explicit
references to code and function or variable names are capitalized and imbedded
in the text; all normal English punctuation may then be used.

Finally, underscoring is occasionally used to emphasize or draw the reader's


attention to a particular point.

1.3 LISP APPLICATIONS

LISP enlarges the problem-domain to which computers may be applied by extending


the nature of mathematical objects beyond numbers, to include a great variety of
structures and the processes for computing with them. As a general programming
language, LISP can be applied to a wide variety of non-numerical, mixed
numerical, and symbolic data processing problems.

The problem-domain is typically characterized by problems with "ill-defined"


data requirements. These problems have data that change dynamically in size
and content as the problem moves nearer solution. Also, the structure of the
data is as important as its content. Hierarchical structures are most common;
however, linear lists of numbers, arbitrary character strings, and paired objects
are frequently required. Problems with these data characteristics encompass
iterative and recursive computational algorithms for solution.

More specifically, LISP has been applied to problems in machine checking of


mathematical proofs, inductive inference on sequences, computations in particle
physics, meta-compilers, pattern matching string transformations, programming
language syntax translation, integral and differential calculus, electrical
circuit theory, game playing, simulation, questionlanswering systems, lin-
guistics, information retrieval, graphics and display programming, and on-line,
interactive text editing.
The new expansion of man's view of t h e n a t u r e of m a t h e m a t i c a l o b j e c t s , made
p o s s i b l e by LISP, i s e x c i t i n g . There a p p e a r s t o b e no l i m i t t o t h e d i v e r s i t y
of problems t o which LISP w i l l b e a p p l i e d . It seems t o b e a t r u l y g e n e r a l
language, w i t h commensurate computing power.
CHAPTER 2.
SYMBOLIC EXPRESSIONS

All programs and data in the LISP programming language are in the form of
symbolic expressions usually referred to as S-expressions. S-expressions are
of indefinite length and have a branching binary tree structure, so that signi-
ficant sub-expressions can be readily isolated. The bulk of available memory
in a computer is used for storing S-expressions in list-structure form. This
type of memory organization frees the programmer from the necessity of allocating
storage for different sections of his program or data. It also makes LISP
programs and data homogeneous (i.e., programs can be treated as data and vice
versa by other programs), a unique feature of the language.

ATOMIC SYMBOLS

The most elementary type of S-expression is called an atomic symboi! or an atom.


Atoms may be numeric or non-numeric. (We will discuss numeric atoms later.)
Non-numeric atoms are called literal atoms.

Definition:

A literal atom is a string of capital letters and


decimal digits of indefinite length, the first
.f.
character of which is a letter.

Examples :

A
APPLE
PART2
EXTRALONGSTRINGOFLETTERS
AlB66X4ZZ

These symbols are called atomic because they are taken as a whole and are not
viewed as individual characters. Thus A, B, and AB are three distinct and
unrelated atomic symbols.

'some recent LISP implementations have liberalized this definition. They accept
as literal atoms any character string that cannot be interpreted as a number.
2.2 DOT NOTATION

All non-atomic S-expressions are written in what is called dot notation. They
are built of atomic symbols and the punctuation marks:

( left parenthesis
) right parenthesis
. period or dot

These larger S-expressions (non-atomic S-expressions) are always parenthesized


and always have two parts--a left part and a right part. A dot is used to
delimit the two halves. For example, the S-expression

has atomic symbol A as its left part, and atomic symbol B as its right part.
Thus, a non-atomic S-expression is always a dotted pair.

Definition:
An S-expression is either:
1. An atom, e.g., A1
2. A dotted pair of atoms, e.g., (A . B)
3. A dotted pair of S-expressions, e.g., ( (A . B) . C )
The general form of a dotted pair is: a left parenthesis, an S-expression, one
or more spaces, a dot, one or more spaces, an S-expression, and a right
parenthesis.

Notice that in this definition an S-expression is defined in terms of itself.


We speak of this as a "recursive1'definition.

Examples:

ATOM
(A B)
(A . ATOM)
(ATOM1 . (BETA . C))
((U V) x)
((U V) (x (Y z)))
GRAPHICAL REPRESENTATION OF DOTTED PAIRS

A l l non-atomic S-expressions a r e i n t e r n a l l y r e p r e s e n t e d a s a b i n a r y t r e e
structure, i.e., a t r e e s t r u c t u r e w i t h b u t two branches a t each node. It i s
o f t e n h e l p f u l t o t h e s t u d e n t t o "see" t h e g r a p h i c a l r e p r e s e n t a t i o n of t h i s t r e e
structure.

We assume t h e f o l l o w i n g g r a p h i c a l symbols and t h e i r a s s o c i a t e d meanings:

Symbol Meaning

'
A g r a p h i c a l node w i t h a l e f t and r i g h t branch ( i . e . , a memory cel:)

A pointer, i.e., a n i n t e r n a l machine a d d r e s s , t o t h e n e x t element


Or of t h e graph

atom names The unique machine a d d r e s s t o which t h e atom named i s a s s i g n e d


and r e p r e s e n t e d i n t h e g r a p h i c a l s t r u c t u r e s a s t h e atom name
itself.

F i r s t t h e graph of

i s g i v e n by

where t h e l e f t p a r t of t h e d o t t e d p a i r , atom A , i s named i n t h e l e f t branch of


t h e node, and t h e r i g h t p a r t of t h e d o t t e d p a i r , atom B, i s named i n t h e r i g h t
branch of t h e node.

The graph of

( ( A . B ) . C )

i s s l i g h t l y more complicated, namely


I n t h i s c a s e , t h e l e f t branch of t h e h i g h e r node p o i n t s t o t h e lower node,
w h i l e t h e r i g h t branch of t h e h i g h e r node c o n t a i n s t h e name of atom C. The
lower node i s e x a c t l y t h e graph of

shown above because i t i s t h e same S-expression. I n t h i s example, however, i t


i s a sub-expression of t h e S-expression

We s e e h e r e g r a p h i c a l l y t h e meaning of sub-expression: i t i s a n S-expression


a t a lower l e v e l and appears i n d o t n o t a t i o n a s a more deeply parenthesi.zed
S-expression.

Examples :
((((A . B) . C) D) (DD . (CC (BB AA))))
((((A . B) . (A . B ) ) (A B)) (A B))

(top level) Level 1

Level 2

Level 3

Level 4

I n t h i s example we have numbered t h e p a r e n t h e s e s (a t u t o r i a l a i d t h a t i s n o t a


l e g a l p a r t of S-expressions) and l a b e l e d t h e graph nodes according t o t h e i r sub-
'expression depth. The correspondence between a p a r e n t h e s i s s u b s c r i p t and a
graph l e v e l i s one-to-one and c l e a r l y i l l u s t r a t e s t h e s t r u c t u r a l meaning of t h e
S-expression. With more complicated S-expressions we have a deeper and larger
graph. Thus, we can see that S-expressions can be of unlimited size and com-
plexity, constrained only by the physical memory capacity of the computer.

2.4 EXERCISES

Which of the following are atomic symbols?

ATOM
A B
AlB2C3
NIL
(X)
LISP
432
ONE
(MY . NAME)
2TIMES

Identify the dotted pairs.

11. A .B
12. X . Y . Z
13. (YOU . AND . ME)
14. (X . Y)
15. (NIL . NIL)

Graph these dotted pairs.

16. (ONE . (TWO . THREE) )


17. (((THREE . NIL) . TWO) . ONE)
18. ((A . B) . (B . (C . D)))
What S-expressions are these structures?
CHAPTER 3.
SYMBOLIC EXPRESSIONS IN LIST NOTATION

Dot n o t a t i o n i s n e c e s s a r y and s u f f i c i e n t t o r e p r e s e n t a l l l i s t s t r u c t u r e s i n
LISP, and, i n f a c t , i s t h e fundamental concept upon which t h e programming
language is b u i l t . However, i t l e a v e s much t o b e d e s i r e d a s a convenient
programming n o t a t i o n f o r S-expressions, p a r t i c u l a r l y because of i t s e x c e s s of
p a r e n t h e s e s and d o t s . L i s t notation was invented t o improve t h i s s i t u a t i o n and
s i m p l i f y t h e r e a d i n g and w r i t i n g of S-expressions.

For example, t h e l i s t

i s a n S-expression i n l i s t n o t a t i o n f o r t h e same S-expression

(A . (B . (C . (D . NIL))))
W

written i n dot notation. (The atom NIL h a s s p e c i a l s i g n i f i c a n c e and w i l l be


discussed s h o r t l y . )

LIST ELEMENTS

A l i s t may have s u b l i s t s , and t h e s e s u b l i s t s may a l s o have s u b l i s t s . It i s


u s u a l l y convenient t o speak of elements of a l i s t . An element t h e n may b e a n
atom, a l i s t of atoms, o r a l i s t of l i s t s . A l i s t of l i s t s i s c a l l e d a
Zist structure.

For example, (A B C) i s a l i s t w i t h t h r e e atomic elements, A, B, and C , whereas


(A (B C)) i s a l i s t of two elements, t h e atom A, and t h e l i s t (B C ) . The
second element, (B C), i s a l i s t of two atomic elements, B and C. Finally, the
l i s t s t r u c t u r e ((A) (B) (C)) i s a l i s t of t h r e e elements, t h e l i s t s (A), (B) ,
and (C).

H i s t o r i c a l l y , t h e s e p a r a t o r f o r elements of l i s t s was t h e comma; however, one o r


more blanks o r s p a c e s a r e now g e n e r a l l y used. Either is acceptable.
Thus, t h e two S-expressions

and

a r e e n t i r e l y e q u i v a l e n t i n LISP.

The s t u d e n t should be cautioned t h a t though much of t h e LISP programming language


i s w r i t t e n i n l i s t n o t a t i o n , t h e b a s i s f o r t h i s form i s always d o t n o t a t i o n . In
f a c t , a l l S-expressions i n l i s t n o t a t i o n can be transformed i n t o t h e i r dot-
n o t a t i o n e q u i v a l e n t s , b u t n o t a l l S-expressions i n d o t n o t a t i o n can be transformed
into list notation. This w i l l be e v i d e n t a f t e r w e examine t h e r u l e s and i d e n t i -
t i e s r e q u i r e d f o r t r a n s l a t i n g between n o t a t i o n s .

3.2 -
NIL

About t h e t u r n of t h e c e n t u r y , t h e o r e t i c a l p h y s i c s was i n a dilemma. Was l i g h t


emission a wave o r a p a r t i c l e phenomenon? Ample evidence e x i s t e d t o s u p p o r t
e i t h e r s c h o o l of thought. P h y s i c s r e s o l v e d i t s dilemma by c o n s i d e r i n g l i g h t a s
a wavicle.

LISP a l s o h a s a dilemma, r e s o l v e d i n a s i m i l a r f a s h i o n . The dilemma d e r i v e s


from t h e f l e x i b i l i t y of l i s t s t o r e p r e s e n t d a t a sequences of a r b i t r a r y l e n g t h .
To p r o p e r l y manipulate l i s t s , i t i s i m p e r a t i v e t h a t some convention e x i s t f o r
r e c o g n i z i n g t h e end of a l i s t .

For example, c o n s i d e r t h e l i s t (A B ) . I f we remove t h e f i r s t element, w e have


t h e remaining l i s t , ( B ) . I f we a g a i n remove t h e f i r s t element, we have t h e
remaining l i s t , ( ) . T h i s i s t h e t e r m i n a l p o i n t of t h e o r i g i n a l l i s t , (A B).
It i s t h e empty l i s t . The empty l i s t i s t h e LISP convention f o r t h e end of a
l i s t , and i t i s d e f i n e d a s t h e atom NIL.

L i k e t h e wavicle of p h y s i c s , NIL i s s i m u l t a n e o u s l y a n atom and a l i s t . LISP


programmers can u s e e i t h e r " s p e l l i n g 1 ' , ( ) o r NIL, a s t h e y a r e i d e n t i c a l l y
r e p r e s e n t e d i n t e r n a l l y by LISP. We c o n s i d e r NIL, n o t t h e l a s t element of a
l i s t , b u t t h e t e r m i n a t o r of a l i s t .
3.3 TRANSFORMING LIST NOTATION TO DOT NOTATION

All non-atomic S-expressions are defined as dotted pairs. It is therefore


possible to transform a list to its equivalent form in dot notation. The
following rules and identities define the transformation.

Identity 1:

A list of one atom is a dotted pair of the atom and


NIL, with NIL always the right part of the dotted
pair, i.e.,

(atom) : (atom . NIL)

Examples:
or equivalently

(atom) - (atom
I

. ( ))

(A) 5 (A . NIL)
(EXTRALONGATOM) E (EXTRALONGATOM . NIL)
(NIL . MIL)
(( 1 ( )>

When transforming a multi-element list to its equivalent form in dot notation,


we begin by composing the dot notation equivalent for only the top-level elements
of the list. We then compose the dot notation equivalent for each sublist, and
so on until the list is completely transformed to dot notation. All we need then
is a rule for transforming a simple list to its dot notation equivalent, re-
peating that rule for all sublists. We can now state that rule.

Rule 1:

The first (left-most) list element, when transformed


to dot notation, is always the left part of a dotted
pair. If the first element is also the last element
of the list, by Identity 1, it is dotted with NIL. If
the first element is not the last element of the list,
then t h e r i g h t p a r t of t h e d o t t e d p a i r i s t h e l i s t
formed by removing t h e f i r s t element. Then apply
Rule 1 t o t h e r i g h t p a r t of t h e d o t t e d p a i r .

For example, given t h e l i s t

we apply Rule 1 and g e t

Since

i s t h e r i g h t p a r t of t h e d o t t e d p a i r and i s i t s e l f a l i s t , we apply Rule 1


again to get

Again, t h e r i g h t p a r t i s a l i s t (C) s o we apply Rule 1 once more. We n o t e ,


however, t h a t t h e l i s t (C) s a t i s f i e s I d e n t i t y 1 and i s e q u i v a l e n t t o

(C . NIL)
Hence, t h e f i n a l S-expression i s given by

For another example, t h e l i s t

y i e l d s t h e s e p a r t i a l expansions f o r each a p p l i c a t i o n of Rule 1.

(A ((B C) Dl)
(A c) (Dl))
(A . ((B C) . .
(D NIL)))
Now, expanding the sublist (B C) we find

(A ((B (C)) (D NIL)))


(A . ((B . (C . NIL)) . .
(D :NIL)))

Examples :

/(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)))
. NIL) . NIL)
-
((A) E ((A
( (NIL) r ((NIL . NIL) . NIL)
(0> (NIL . NIL)
(A (B . C)) E (A . ((B . C) . NIL))

Prom the above examples you can see that Identity 1 can be stated alternatively
as: When converting from list to dot notation, the only atom that appears
adjacent to a right parenthesis is NIL.

3.4 TRANSFORMING DOT NOTATION TO LIST NOTATION

It is always possible to convert list notation to dot notation, since S-


expressions are defined by dot notation. However, we cannot always convert
dot notation to list notation. For example, we cannot transform

The rule that is in effect follows from Identity 1.

Only those dotted pairs in which the only atom


adjacent to a right parenthesis is NIL can be
represented in list notation.

For complicated dotted pairs, the following procedure can be followed starting
with any dotted pair:
1. I f t h e r i g h t p a r t of t h e d o t t e d p a i r i s a n atom and n o t NIL,
conversion t o l i s t n o t a t i o n i s impossible.

2. I f t h e r i g h t p a r t of t h e d o t t e d p a i r i s non-atomic (i.e., a
l i s t o r a d o t t e d p a i r ) o r NIL--treat NIL h e r e a s ()--then

a. d e l e t e t h e l a s t r i g h t p a r e n t h e s i s of t h e d o t t e d p a i r
b. d e l e t e the dot
c. d e l e t e t h e f i r s t l e f t p a r e n t h e s i s of t h e r i g h t p a r t ; t h e
l e f t p a r t t h e r e b y becomes t h e f i r s t element of t h e l i s t
d. r e p e a t t h e procedure on t h e remaining d o t t e d p a i r s .

For example, given t h e d o t t e d p a i r

(A . (B . N I L ) )
t h e most n e s t e d d o t t e d p a i r i s

(B . NIL)
Representing NIL by ( ) and applying t h e procedure above, w e f i n d

Applying t h e procedure a g a i n , we g e t t h e l i s t

For t h e c a s e

(A . ( ( B . C) . (D . NIL)))
r e p e a t e d a p p l i c a t i o n of t h e procedure y i e l d s t h e s e e x p r e s s i o n s :

We can reduce t h i s l i s t no f u r t h e r , a s t h e second element of t h e l i s t


i s a d o t t e d p a i r t h a t cannot be r e p r e s e n t e d a s a l i s t . We c a l l t h e e x p r e s s i o n

a l i s t , b u t r e c o g n i z e t h a t i t i s i n mixed n o t a t i o n . Mixed n o t a t i o n i s p e r f e c t l y
a c c e p t a b l e t o LISP and i s q u i t e common i n LISP S-expressions.

3.5 GRAPHICAL REPRESENTATION OF LISTS

L i s t s can b e transformed i n t o t h e i r e q u i v a l e n t d o t n o t a t i o n ; g r a p h i c a l r e p r e -
s e n t a t i o n of d o t t e d p a i r s i s covered i n S e c t i o n 2 . 3 . T h i s s e c t i o n w i l l review
t h a t m a t e r i a l , b u t w i t h t h e i n t r o d u c t i o n of NIL.

Inasmuch a s NIL i s a n atom, we need n o t i n t r o d u c e any new g r a p h i c a l symbology.


However, s i n c e we u s e NIL a s a l i s t t e r m i n a t o r , a d i a g o n a l s l a s h i s o f t e n used
t o r e p r e s e n t NIL, and i s adbpted h e r e . Thus t h e graph f o r

(A . NIL)

But

(A . NIL) I (A)

s o t h e graph a l s o shows a s i n g l e element list.

For more complicated l i s t s , we s h a l l show t h e l i s t : , i t s d o t t e d p a i r


equivalence, and i t s graph.
Examples :

List Dotted P a i r

(A B C) (A . (B . (C . NIL)))

((A . NIL) . (B . (C . NIL)))


List Dotted Pair

(A (B) C) (A . ( ( B . N I L ) . (C . N I L ) ) )

(A . (B . ((C . NIL) . N I L ) ) )
-
List D o t t e d Pair

((A) (B) (C)) ((A . NIL) . ((B . NIL) . ( ( C . NIL) . N I L ) ) )

(((A . (B . NIL)) . NIL) . NIL)


3.6 EXERCISES

T r a n s f o r m t h e s e lists t o t h e i r f u l l y expanded d o t n o t a t i o n e q u i v a l e n t s .

1. (ATOM)
2. ((LISP))
3. (((MORE YET)))
4. (HOW ABOUT THIS)
5. (DONT (GET (FOOLED)))

, Now go t h e o t h e r w a y - - d o t t e d p a i r s t o lists.

6. (XI . NIL)
7. (NIL . (X1 . N I L ) )
8. (KNOW . (THY . (SELF . N I L ) ) )
9. ((BEFORE . (AND . (AFTER . NIL))) . NIL)
lo. (A . ( ( ( B . (C . N I L ) ) . NIL) . N I L ) )

To w h a t S - e x p r e s s i o n s d o t h e s e g r a p h s c o r r e s p o n d ?
CHAPTER 4.
NUMBERS

I n LISP, numbers a r e atoms and may be used i n S-expressions e x a c t l y a s t h e


p r e v i o u s l y d e f i n e d l i t e r a l atoms a r e used. Thus,

(ALPHA . 960)
a r e l e g a l S-expressions.

S i n c e LISP systems a r e implemented on many d i f f e r e n t computers w i t h v a r y i n g


hardware f e a t u r e s , t h e convent i o n s f o r l e g a l numbers d i f f e r widely. A l l imple-
mentations a c c e p t i n t e g e r and o c t a l f i x e d - p o i n t numbers. Systems on l a r g e r
machines a l s o a c c e p t f l o a t i n g - p o i n t numbers. A l l o c t a l numbers end w i t h t h e
l e t t e r "Q" t o d i s t i n g u i s h them from i n t e g e r s . Floating-point numbers f o l l o w
s c i e n t i f i c n o t a t i o n , which i n c l u d e s a decimal p o i n t and an o p t i o n a l decimal
s c a l e f a c t o r d i s t i n g u i s h e d by t h e l e t t e r "Eft. Beyond t h e s e simple, g e n e r a l
f e a t u r e s , you should c o n s u l t your p a r t i c u l a r LISP system r e f e r e n c e l i t e r a t u r e
f o r i t s number conventions.

T h i s c h a p t e r s t i p u l a t e s a s e t of s y n t a x conventions used c o n s i s t e n t l y throughout


t h e primer. Because of t h e l i k e l i h o o d t h a t t h e s e conventions w i l l c l a s h w i t h
t h o s e of your system, c a r e has been taken, i n t h e b a l a n c e of t h e primer, t o
avoid examples and e x e r c i s e s t h a t depend on t h i s pedagogy.

4.1 INTEGER NUMBERS

Fixed-point numbers a r e p o s i t i v e o r n e g a t i v e i n t e g e r s , w i t h o r without a s c a l e


factor. The s c a l e f a c t o r i s denoted by t h e l e t t e r "Elf, followed by a blank o r
any p o s i t i v e i n t e g e r . Negative s c a l i n g i s i l l e g a l and n o t meaningful f o r
f i x e d - p o i n t numbers. Thus,

i s u n a c c e p t a b l e f o r LISP.
Examples :

LISP Number Meaning

+123
+I23
-321
-1~10
=~-1000
+53

4.2 OCTAL NUMBERS

I n t e g e r s may a l s o be r e p r e s e n t e d i n o c t a l . O c t a l numbers a r e denoted by o c t a l


d i g i t s , followed by t h e l e t t e r "Q", followed by a blank o r any p o s i t i v e decimal
integer. The Q must be p r e s e n t . The decimal i n t e g e r following Q i s a s c a l e
f a c t o r showing t h e power of e i g h t . Negative s c a l e f a c t o r s a r e i l l e g a l and n o t
meaningful f o r o c t a l o r decimal numbers.

Thus,

i s unacceptable f o r LISP.

The l a r g e s t o c t a l number allowed i s a machine-dependent c h a r a c t e r i s t i c . A


machine having 48 b i t s per word has been assumed f o r a l l LISP a r i t h m e t i c i.n
t h i s primer. Implementations of LISP on machines w i t h l e s s e r word l e n g t h w i l l
y i e l d numerical r e s u l t s having fewer s i g n i f i c a n t d i g i t s than given h e r e .

Examples :

LISP Number Meaning

123Q
12342
77743
248
3410
4.3 FLOATING-POINT NUMBERS

Floating-point numbers must contain a decimal point. Floating-point numbers may


be positive or negative, with an optional scale factor. The scale factor is
always denoted by the letter "E", and may be followed by a blank or any
positive or negative integer.

Examples :

LISP Number Meaning

3.14159 +3.14159
+l.OE-3 +O .001
-976.00333 -976003.00
0.2733+2 +27.30
23.E-1 +2.30
17. +17.00

4.4 DECIMAL POINT OR DOTTED PAIR RESOLUTIOX

When floating-point numbers are used in S-expressions, the computer can be


confused as to the meaning of the period. Is it treated as a decimal point or
as the dot in a dotted pair? To eliminate confusion and avoid ambiguity, always
surround the dot with blanks when writing a dotted pair, and never surround
the decimal point with blanks when writing a floating-point number.

For instance,

(1.2.3.4)

is an illegal S-expression. Whereas

(1.2 . 3.4)
is perfectly proper.
EXERCISES-

Which of the following are S-expressions?

(Q IQ)
(5E (E . . NIL))
(E5 . 5E)
(1.E * 1Q)
43
4.4
(A. 9)
(B .9.9)
(9.9.9)
(1.23 77Q3 27 2735 0.3213-7 ALPHA Q . 32)
Convert the following to list notation, if possible.

11. (99.9 . NIL)


12. (NIL . 99.9)
13. ((PI) . 3.1415930 . NIL)
14. (5 . (5.5 . (545 . (55.OE-1 . (5E2 NIL)) 1))
15. ((13.13 . NIL) . ((25Q2 . NIL) . NIL))
CHAPTER 5.
E LEME NTARY FUNCTIONS

LISP is a language for manipulation of S-expressions. Fundamental to this


manipulation is the ability to build S-expressions from smaller S-expressions
and produce sub-expressions from a given S-expression. These abilities are
possible with the elementary LISP functions CONS, CAR, and CDR.

5.1 RAPPORT WITH THE SUPERVISOR

Before we examine the elementary functions, we must understand a basic element


of the syntax of the communication language accepted by the LISP system.
Chapter 9 covers the subject in greater detail. At this juncture we will only
consider the requisite parenthesization.

When we input to the LISP system, we are communicating with a supervisor program
that always expects two inputs, both S-expressions. If we call this pair of
S-expressions s1 and s2 respectively, the first S-expression, sl, is always
treated by the supervisor as:

1. The name of a function, or


2. An S-expression that behaves as a funct:Lon.

(We will focus on the former case here, and examine the latter case in subse-
quent chapters.) Since functions have arguments, the second S-expression, s2'
is always a list of the arguments for the function whose name is the S-expression

S1*

Consider the trigonometric function

SIN 90'

If SIN were a LISP function, we would write

SIN (90)

where the first S-expression, sl, is SIN and the second S-expression, s2, is
the list (90)--the list of the single argument required by SIN.
As another example, in LISP the function PLUS performs the operation of addition
of its arguments. We can compute the sum of three numbers by giving the
following pair of S-expressions to the supervisor:

--
PLUS (1 2 3)
S
1 S2

The S-expression s is the name of the function PLUS. The S-expression s is a


1 2
list with three elements (i.e., 1, 2, 3), each an argument for the function PLUS.

5.2 CONS

CONS refers to "the construct of" and is the function that is used to build S-
expressions. It has two arguments that are both S-expressions.

Definition :

The CONS of two S-expressions is the dotted pair


of these S-expressions, with the first S-expression
being the left part, and the second S-expression
being the right part of the dotted pair.

For example, given the arguments A and B, we can CONS them by giving the
supervisor

--
CONS (A B)

s1 s2

which means (A . B) .
If the arguments were the lists (A) and (B) , we would write

which is equivalent to
Examples :

CONS(M N) = (M . N)
CONS((A . B) C) = ((A . B) . C)
CONS(A (B C D)) = (A . (B C D ) ) = (A B C D)

5.3 CAR

CAR (pronounced "car") i s one LISP f u n c t i o n used t o e x t r a c t a sub-expression


from a n S-expression. I t s meaning i s " t h e f i r s t of." It has one argument,
a non-atomic S-expression (i.e., a dotted pair, or a l i s t ) .

Definition:

The CAR of a non-atomic S-expression i s t h e l e f t


p a r t of t h e S-expression when r e p r e s e n t e d i n d o t
n o t a t i o n , o r t h e f i r s t element of t h e S-expression
when r e p r e s e n t e d i n l i s t n o t a t i o n . The CAR of a n
atom i s undefined.

For example, t h e CAR of t h e argument (M . N) would be w r i t t e n

--
CAR ( (M
S
1 S2
. N) )

which i s e q u i v a l e n t t o M. Note t h a t f o r t h i s example t h e l i s t s


2
is ((M . N)),
a l i s t of one element--the s i n g l e argument f o r t h e f u n c t i o n CAR.

Examples :

CAR((A . B)) = A
CAR(((A . B) . C ) ) = (A . B)
CAR((A B C D)) = A
CAR(((A B C) D E)) = (A B C)
CAR(FO0) = undefined f o r atoms
5.4 -
CDR

CDR (pronounced "could-er") i s a n o t h e r LISP f u n c t i o n used t o e x t r a c t a sub-


e x p r e s s i o n from an S-expression. I t s meaning i s " t h e r e s t of1'. It h a s one
argument, a non-atomic S-expression s i m i l a r t o t h a t accepted by CAR. CAR of
a g i v e n non-atomic S-expression y i e l d s t h e f i r s t element of a n S-expression;
CDR y i e l d s t h e r e s t of t h a t S-expression a f t e r t h e CAR i s removed.

Definition:

The CDR of a non-atomic S-expression i s t h e r i g h t


p a r t of t h e S-expression when r e p r e s e n t e d i n d o t
n o t a t i o n , o r t h e balance of t h e S-expression a f t e r
t h e f i r s t element i s removed when r e p r e s e n t e d i n
list notation. The CDR of an atom i s undefined.

For example, t h e CDR of t h e argument (M . N) would be w r i t t e n

CDR ( (M
r---
. N) )

which i s e q u i v a l e n t t o N.

Do n o t confuse l i s t and d o t n o t a t i o n when e v a l u a t i n g t h e CDR. I f t h e CAR


of l i s t

i s removed, t h e remainder i s s t i l l a l i s t , i . e . , (B) .


I f t h e CAR of t h e d o t t e d p a i r

(A B)

i s removed, t h e remainder i s t h e r i g h t p a r t , B.
Thus,

CAR ( ( A . B)) = A
CDR ((A . B)) = B
CAR ((A B ) ) = A
CDR ((A B ) ) = (B)

Examples :

CDR((A . Y)) = Y
CDR((A . (ATOM) ) ) = (ATOM)
CDR((A B C D ) ) = (B C D)
CDR(FO0) = undefined f o r atoms

Note: The CDR of a l i s t w i t h only one element, e.g.,


(A), i s t h e atomic symbol NIL. For example:

5.5 GRAPHICAL INTERPRETATION OF CAR AND CDR

I n t h e previous c h a p t e r s w e examined t h e graphs of LISP S-expressions and


noted t h e b i n a r y t r e e s t r u c t u r e of t h e s e e x p r e s s i o n s . L e t u s now examine t h e
meaning of t h e elementary f u n c t i o n s t h a t o p e r a t e on t h e s e t r e e s t r u c t u r e s .

I f someone asked f o r d i r e c t i o n s t o g e t t o your home, you would most n a t u r a l l y


couch such d i r e c t i o n s i n terms of c i t y b l o c k s , and house numbers. I n LISP,
we a r e f a c e d w i t h a s i m i l a r problem--to p r o v i d e t h e LISP system w i t h d i r e c t i o n s
f o r "traveling" through t h e b i n a r y t r e e s t r u c t u r e of a n S-expression. We
couch such d i r e c t i o n s i n terms of CAR and CDR, which d e s i g n a t e t h e a p p r o p r i a t e
II
t u r n " a t each b i n a r y "fork i n t h e road".

For example, g i v e n t h e S-expression

((A B) C D) = ((A . (B . NIL)) . (C . (D . NIL)))


i t s graph i s g i v e n by
The CAR and CDR of t h i s S-expression y i e l d

CAR ( ((A B) C D) ) = (AB)


CDR ( ((A B) C D) ) = (C D)

I n t h e graph we n o t e t h a t

i s t h e l e f t branch of t h e t o p node and

i s t h e r i g h t branch. The connecting arrows of t h i s graph were c a l l e d pointers


i n Chapter 2 .

We s e e now t h a t they a r e p o i n t e r s t o t h e CAR and CDR. We o f t e n s a y they p o i n t


t o t h e "C.AR chain1' o r "CDR chainr1 of t h e s t r u c t u r e . I f we t a k e t h e CAR arid CDR
r e p e a t e d l y a t each node, we can completely " t r a v e r s e 1 ' t h e S-expression, arid
r e a c h any sub-expression o r atom of t h e o r i g i n a l S-expression. The f o l l o w i n g
graph i s completely l a b e l e d according t o t h e s e CAR and CDR d i r e c t i o n s , and t h e
11
l o c a t i o n names1' of each p o i n t e r .
To get from t h e o r i g i n a l S-expression t o the atom D , w e require t h e f o l l o w i n g
directions :

1. CDR ( ((A B) C D) ) = (C D)
2. CDR of t h e o u t p u t of ( I ) , i.e . ,

CDR ( (C D) ) = (D)

3. CAR of t h e o c t p u t of ( 2 ) , i . e . ,

CAR ( (D) ) = D

A s t a n d a r d convention, more f u l l y covered i n subsequent c h a p t e r s , i s t o d e f i n e


and name a c l a s s of new f u n c t i o n s t h a t perform t h e s e s u c c e s s i v e CAR, CDR opera-
tions. For each f u n c t i o n i n t h e c l a s s , t h e naming convention i s t o c o n c a t e n a t e
t h e names of a l l t h e CAR, CDR o p e r a t i o n s t o b e a p p l i e d i n t o one uniquely s p e l l e d
abbreviation. The a b b r e v i a t i o n r u l e u s e s t h e common l e t t e r s "C" and "R" as
t h e s t a r t and end c h a r a c t e r s of t h e name. Various combinations of t h e l e t t e r s
"A" ( f o r C F ) , and "D" ( f o r CER), c o n s t i t u t e t h e b a l a n c e of t h e s p e l l i n g . The
o r d e r of t h e l e t t e r s i n combination f o l l o w s d i r e c t l y from l e f t t o r i g h t , t h e
o r d e r of a p p l i c a t i o n of t h e CAR, CDR o p e r a t i o n s , from l a s t t o f i r s t . Thus,
f o r t h e above example

y i e l d s t h e name
CADDR

and the new function may be applied; e.g.,

CADDR ( ((A B) C D) ) = D

Much of LISP programming consists of composing "directions" of this variety.


Problems 21-31 of this chapter give us some exercise in "finding our way
home" .
5.6 EXERCISES

Evaluate the following functions:

CAR((LEFT . RIGHT))
CDR((LEFT . RIGHT))
CONS (LEFT RIGHT)
CAR((A B C D))
CAR(((A) B C D))
CAR((A (B C Dl))
CAR(((A . B) C D E))
CDR((TH1S SENTENCE IS A LIST))
CDR((H0W (ABOUT THIS)))
CDR(((D0T . PAIR1) (DOT . PAIR2)))
CONS (CAR CDR)
CDR ( (EMPTY) )
CDR((CAR CDR))
CAR ( ( (CAR) CDR) )
CONS(A ( 1)
CONS (754 100)
CAR((1 . (2.0 . (30.OE-1 . 774))))
CDR((1 . (2.0 . (30.OE-1 . 774))))
CONS((A . B) NIL)
CAR((((((ALPHA))))))

Note: Problems 1, 2, and 3 above demonstrate the relationship


among CONS, CAR, and CDR. Can you state this relationship?
L i s t from r i g h t t o l e f t t h e sequence of CAR-CDR LISP f u n c t i o n s which, when
each i s a p p l i e d t o t h e v a l u e of t h e p r i o r f u n c t i o n , w i l l f i n d t h e "A" in
each of t h e following S-expressions. For example:

CAR CDR

i s t h e answer f o r t h e argument (C A T) by t h e following reasoning:

CDR ( (C A T) ) = (A T)

then

CAR ( (A T) ) = A

Q.E.D.

T h i s sequence may b e a b b r e v i a t e d a s f o l l o w s :

C& CgR = CADR


CAR CAJX -
CgR = CAADR
CER CgR C& -
= CDDAR
CHAPTER 6.
LAMBDA NOTATION

In LISP 1.5, as in other programming languages, we wish to write programs that


are parameterized and that compute answers when values are assigned to the
parameters of the pr0gra.m. However, in LISP 1.5, we do not use the syntax and
program structure of algebraic languages. LISP programs are conceived and
written with mathematical rigor based upon the formalism of recursive function
theory. As such, procedures are functions; parameters are constants and
variables that can be passed between functions as arguments; and computati-on
is a process of evaluating S-expressions consisting of functions applied to
their arguments.

6.1 FORMS AND FUNCTIONS

Given the algebraic expression

evaluate the expression for the values 3 and 4.

For this problem statement, we immediately see a notational problem. Is x=3


and y=4, or vice versa? The value of the expression changes with our ass~mption.
To resolve this ambiguity we need a notation that explicitly states the corres-
pondence between variables and their values. LISP uses such a notation, the
7
Zmnbda notation, of Alonzo Church.

In Church's lambda notation the expression

is called a form. In LISP 1.5 this form would be written as

where the format is given in Polish prefix notation. (LISP programmers p:refer
calling this format "function notation", where the function always precedes its
arguments.)
Furthermore, in Church's lambda notation

is a function named f, since it provides the two necessary ingredients for a


function:

1. A form to be evaluated, and


2. A correspondence between the variables of the form and
the arguments of the function.

If we now ask the value of the function f for

the previous ambiguity is resolved, as church's lambda notation explicitly


gives the number and order of the arguments of f and defines the correspond-
ence of 3 with x, and 4 with y such that

In LISP, f (3,4) could be written as

(LAMBDA (X Y) (EXPT Y X)) (3 4)


- * I * r J
list fo m
of
variables

where s is called a Zambda expression. A lambda expression is a functionaZ


1
expression, i.e., an S-expression that acts like a function. We shall explore
lambda expressions more fully below.

6.2 LAMBDA EXPRESSIONS

Definition:

A Zambda expression is an S-expression. This S-


expression is a list of three elements in the
following order:
1. The word LAMBDA
2. A l i s t of l i t e r a l atoms t h a t may be used a s v a r i a b l e s
i n t h e form. These atoms a r e c a l l e d Zambda variabZes
3. The form

The g e n e r a l s y n t a x accepted by LISP is:

(LAMBDA var 2;s t body)

wherevarZist i s t h e .list of t h e l i t e r a l atoms used a s v a r i a b l e s ( v a r z i s t may


be empty, i . e . , NIL), and body i s any LISP form. For example:

I I l i s t of v a r i a b l e s , i . e . ,
varZist

--
(LAMBDA ( J K)
I--
(CONS K J ) )
form, i . e . , body

A lambda e x p r e s s i o n i s a f u n c t i o n a l e x p r e s s i o n and may be used wherever


f u n c t i o n s a r e a c c e p t a b l e i n forms. A lambda e x p r e s s i o n a c t s l i k e a f u n c t i o n
s i n c e i t s p e c i f i e s t h e correspondence between t h e v a r i a b l e s i n t h e form and
t h e arguments of t h e f u n c t i o n . T h e r e f o r e , i t can be a p p l i e d t o arguments j u s t
a s t h e elementary f u n c t i o n s CONS, CAR, and CDR were used e a r l i e r .

Examp l e g :

A number of simple lambda e x p r e s s i o n s a r e d e s c r i b e d below.


They a r e simple because they have elementary forms; however,
they show p e r t i n e n t p r o p e r t i e s of lambda e x p r e s s i o n s t h a t
hold t r u e f o r a l l lambda e x p r e s s i o n s .

1. (LAMBDA ( ) 1) T h i s i s a lambda e x p r e s s i o n w i t h no
lambda v a r i a b l e s , i . e . , varzist is
NIL. The form i s t h e numerical
c o n s t a n t 1.
2. (LAMBDA (X) 1) This lambda expression has X as the
only lambda variable. Again, the form
is the numerical constant 1. This
example shows that it is not necessary
for the variable to appear in the form.

3. (LAMBDA (X ANYNAME Y2) 1) This lambda expression is analogous to


example 2, 'butwith three lambda varia-
bles. It further shows that lambda
variables may be arbitrary literal atoms.

4. (LAMBDA (X) X) This lambda expression has X as the only


lambda variable. Also, the form is the
lambda variable itself.

6.3 LAMBDA CONVERSION

With Church's lambda notation, both the form and the correspondence between
variables of the function and their values are made explicit by the syntax
of lambda expressions. A lambda expression is the definition of a function.

When a function is called to compute a value from a given set of arguments,


its definition is used to properly associate variables with arguments. After
pairing of variables with arguments (a process called binding of variables)
the form is evaluated with the current variable bindings. This process of
.
binding variables and then evaluating the form inside the lambda expression is
called lambda conversion.

The importance of lambda conversion cannot be over-stressed. It is the method


by which all lambda expressions are evaluated, and all called functions receive
their arguments. The conditions and mechanics for lambda conversion are given
below.

Conditions:

1. A lambda expression definition must exist, as either an explicit


lambda expression, or as a "built-in" function, e.g., a previously
compiled function.
2. Only l i t e r a l atoms may be used a s lambda v a r i a b l e s i n t h e d e f i n i t i o n .
( I n subsequent c h a p t e r s , we s h a l l s e e t h a t t h e l i t e r a l atoms T and
N I L may not be used a s lambda v a r i a b l e s . )

3. The number of arguments i n t h e f u n c t i o n c a l l must a g r e e w i t h t h e number


of lambda v a r i a b l e s . I f t h e r e a r e no lambda v a r i a b l e s , i . e . , vadist
i s NIL, we have a f u n c t i o n of no arguments.

Mechanics.:

1. Lambda v a r i a b l e s a r e p a i r e d w i t h t h e i r corresponding arguments. The


o r d e r i s important s i n c e t h e f i r s t lambda v a r i a b l e i s p a i r e d t o t h e
f i r s t argument; t h e second lambda v a r i a b l e i s p a i r e d t o t h e second
argument; e t c . This p a i r i n g b i n d s t h e v a r i a b l e t o t h e argument.

2. The form i s evaluated.

For example, consider t h e e v a l u a t i o n of t h e following t o p - l e v e l ( i . e e 3


s u p e r v i s o r l e v e l ) funct:ion c a l l :

(LAMBDA (J K) (CONS J K) ) (A B)
1

This f u n c t i o n c a l l y i e l d s t h e same v a l u e , (A . B), a s t h e top-level c a l l

--
CONS (A B)

S
1 s2

However, s i n c e CONS i s a b u i l t - i n f u n c t i o n , t h e former example more c l e a r l y


demonstrates t h e mechanics of lambda conversion.

When using a lambda expression a t t h e t o p l e v e l , t h e lambda e x p r e s s i o n i s


t h e f i r s t S-expression, sl, of t h e p a i r presented t o t h e s u p e r v i s o r . Again,
t h e second S-expression, of t h e p a i r i s t h e l i s t of arguments f o r sl; i n
s
2'
t h i s c a s e , t h e l i s t of arguments f o r t h e lambda expression. It i s important t o
understand t h a t t h e arguments i n t h e l i s t , s2, a r e p a i r e d w i t h t h e lambda v a r i a -
b l e s of t h e lambda e x p r e s s i o n , sl. The arguments i n t h e l i s t , s2, a r e matched
i n number and p o s i t i o n w i t h t h e v a r i a b l e s i n t h e l i s t of v a r i a b l e s following
t h e LAMBDA. Thus, by lambda conversion, t h e v a r i a b l e J i s p a i r e d w i t h t h e
argument A, and t h e v a r i a b l e K w i t h t h e argument B. Then t h e form

(CONS J K)

w i t h i n t h e lambda e x p r e s s i o n i s e v a l u a t e d . The b i n d i n g s of v a r i a b l e s J and K


a r e r e t r i e v e d t o y i e l d A and B, r e s p e c t i v e l y ; i t i s t h e s e v a l u e s t o which CONS
i s a p p l i e d t o y i e l d t h e d o t t e d p a i r , (A . B) .

(LAMBDA (X) X) (1234) = 1234


(LAMBDA (ABLE) (CAR ABLE) ) ( (THIS I S A LIST) ) = THIS
(LAMBDA ( ) 77) NIL = 77
(LAMBDA (ONE TWO) (CONS TWO ONE)) (A B) = (B . A)
(LAMBDA (K) (CADAR K)) ( ( ( 1 2 3) 4 5) ) = 2

6.4 PARENTHESES

The lambda e x p r e s s i o n

(LAMBDA (A B) (CONS A B ) )

uses s i x parentheses. They a r e v e r y i m p o r t a n t . They d e s i g n a t e scope o r e x t e n t


of e x p r e s s i o n s , i . e . , where they b e g i n and where they end. P a r e n t h e s e s have t o
be v e r y p r e c i s e l y p o s i t i o n e d . I n o r d e r t o understand them, we s h a l l f i r s t number
them i n a s s o c i a t e d p a i r s :

(LAMBDA (A B) (CONS A B))


1 2 2 2 21

The f i r s t l e f t p a r e n t h e s i s (No. 1 ) t e l l s t h e LISP system t h a t this i s t h e s t a r t


of a n e x p r e s s i o n . The f i n a l r i g h t p a r e n t h e s i s (No. 1 ) t e l l s t h e system t h a t
t h i s i s t h e end of t h e e x p r e s s i o n .

The f i r s t l e f t p a r e n t h e s i s marks t h e beginning of t h e scope of t h e LAMBDA, i . e . ,


t h e e x t e n t of t h e e x p r e s s i o n t o which LAMBDA appl'es. The second No. 1 paren-
t h e s i s marks t h e end of t h e scope of LAMBDA.
The second l e f t p a r e n t h e s i s No. 2 marks t h e beginning of t h e scope of CONS,
w i t h t h e L a s t p a r e n t h e s i s No. 2 ending t h a t scope.

A l l p a r e n t h e s e s i n t h e S-expressions of LISP always occur i n p a i r s of l e f t and


r i g h t p a r e n t h e s e s ; g e n e r a l l y , each p a i r marks t h e s c o p e of a n e x p r e s s i o n , o r
bounds a l i s t . The p a r e n t h e s e s i n LISP a r e n e v e r o p t i o n a l a s t h e y a r e some-
t i m e s i n mathematics; t h e y a r e r e q u i r e d p a r t s of e x p r e s s i o n s .

Note t h a t i n t h e example above, t h e sub-expressions

and

(CONS A B)

a r e b o t h bounded by p a r e n t h e s i s - p a i r s l a b e l e d No. 2. I f we c o n s i d e r t h e paren-


t h e s i s numbers a s "depth1' c o u n t e r s o r " l e v e l s " , we s e e t h a t t h e s e two sub--
e x p r e s s i o n s a r e a t t h e same d e p t h , namely l e v e l two. Since the only occurrences
of p a r e n t h e s e s No. 1 completely b r a c k e t t h e lambda e x p r e s s i o n s , w e s a y t h a t t h e
lambda e x p r e s s i o n i s a t l e v e l one.

P a r e n t h e s i s c o u n t i n g i s a good " c r u t c h " i n t h a t i t immediately i d e n t i f i e s sub-


e x p r e s s i o n s a t t h e same l e v e l w i t h i n a l a r g e r S-expression--a very useful
debugging and f o r m a t t i n g t o o l . I n fact,'^^^^ p r i n t o u t s a r e u s u a l l y f o r m a t t e d o r
" p r e t t y p r i n t e d 1 ' by t h e system, which i n d e n t s sub-expressions a c c o r d i n g t o t h e i r
level. T h i s i n d e n t i n g scheme i s a l s o a u s e f u l a i d f o r e n t e r i n g i n p u t t o LISP.

6.5 DUMMY VARIABLES

Before we l e a v e lambda e x p r e s s i o n s , n o t e t h e f o l l o w i n g e x p r e s s i o n s :

(LAMBDA (A B) (CONS A B ) ) (Q R) = (Q R) [I]

The two e x p r e s s i o n s e v a l u a t e t o t h e same v a l u e . More i m p o r t a n t l y , t h e sy:stemdtic


s u b s t i t u t i o n of 3 and K f o r A and B , r e s p e c t i v e l y , i n e x p r e s s i o n [ I ] t r a n s l a t e s
t h a t e x p r e s s i o n t o e x p r e s s i o n [ 2 ] , w i t h o u t changing t h e form o r i t s meaning. This
is a s i g n i f i c a n t p a r t of C h u r c h ' s lambda notation. Thus, a l m o s t any literal
a t o m w i l l s u f f i c e as a variable i n a l a m b d a e x p r e s s i o n . So we s p e a k o f them
as dummy variables.

6.6 EXERCISES

F o r t h e f o l l o w i n g function calls, g i v e t h e variable b i n d i n g s r e s u l t i n g f r o m


lambda conversion.

(LAMBDA (X) X) (ATOM)


(LAMBDA (Y) Y) ( ( L I S T ) )
(LAMBDA (J) (CAR J ) ) ((THREE ELEMENT L I S T ) )
(LAMBDA (K) (CDR K) ) ( (THREE ELEMENT ILLST))
(LAMBDA (U V) (CONS U V)) (VERY GOOD)
(LAMBDA (Y X) (CONS Y X)) (ONE (THEN . ANOTHER))
(LAMBDA (A) (CAADR A ) ) ((A (B . 7742)))
(LAMBDA (VARIABLE) (CDAR VARIABLE) ) ( ( (A B) ) )
(LAMBDA (J) 3 . 1 4 1 5 9 ) (NIL)
(LAMBDA ( ) 3 . 1 4 1 5 9 ) ( )
(LAMBDA (U V) U) (ALPHA BETA)
(LAMBDA (U V) U) (BETA ALPHA)
(LAMBDA (U V) V) (ALPHA BETA)
(LAMBDA (V U) V) (ALPHA BETA)
(LAMBDA (FIRST SECOND) (CAR F I R S T ) ) ((FIRST) SECOND)
CHAPTER 7 .
ELEMENTARY FORMS

Computation i n LISP i s done by simply e v a l u a t i n g forms. A l l forms have v a l u e ,


whether they be simple numerical c o n s t a n t s , v a r i a b l e s , o r deeply n e s t e d S-
expressions. The v a l u e of a form i s t h e r e s u l t of e v a l u a t i n g i t ; t h e form i s
e v a l u a t e d only once--no more, no l e s s .

I f we view lambda conversion i n LISP from a more c o n v e n t i o n a l programming stand-


p o i n t , forms look l i k e programs o r "pieces" of a program; a n ALGOL "block" i s
a n example. Arguments look l i k e d a t a f o r t h e s e b l o c k s . S i n c e LISP p r o c e s s e s
symbolic d a t a r e p r e s e n t e d a s S-expressions, d a t a i n LISP a r e S-expressions.
Note a v e r y s i g n i f i c a n t p o i n t : forms a r e themselves S-expressions, and t h u s ,
can be d a t a . We speak of t h i s f a c t a s t h e "homogeneity of programs and d a t a "
i n LISP--a unique and powerful f e a t u r e of LISP. By proper s e p a r a t i o n of con-
t e x t , programs can g e n e r a t e o t h e r programs a s d a t a . The LISP compiler i t s e l f
i s a LISP program t h a t does j u s t t h a t . I n Chapter 1 9 , we s h a l l examine MACRO,
a f u n c t i o n t h a t does nothing e l s e b u t t r a n s f o r m forms i n t o o t h e r forms.

These c o n s i d e r a t i o n s a r e brought up t o p r o v i d e m o t i v a t i o n f o r d i s c u s s i n g LISP


forms a t t h i s time. To h e l p t h e beginner comprehend LISP forms, t h i s cha.pter
and t h e n e x t two c h a p t e r s t r e a t ( i n o r d e r ) elementary forms, composed forms,
and s p e c i a l forms.

7.1 VARIABLES

A l l v a r i a b l e s a r e e1ernenta.r~forms. I n t h e preceding c h a p t e r , we encountered


lambda v a r i a b l e s . I n subsequent c h a p t e r s we s h a l l c o n s i d e r o t h e r v a r i a b l e s .
Regardless of t h e i r o r i g i n , v a r i a b l e s a r e elementary forms.

The p r o c e s s of e v a l u a t i n g an elementary form which i s j u s t a v a r i a b l e i s one


of simply r e t r i e v i n g t h e binding of t h e v a r i a b l e and r e t u r n i n g t h a t b i n d i n g
a s t h e v a l u e of t h e form. With lambda v a r i a b l e s , t h e binding r e t r i e v e d
t h e binding e s t a b l i s h e d by lambda conversion. I n t h e lambda e x p r e s s i o n

(LAMBDA (X) X)
the lambda variable X is the form. If we call this function

with the argument A, by lambda conversion X is bound to A and the form is


evaluated by simply retrieving that binding.

7.2 CONSTANTS

All constants are elementary forms. Since LISP allows numerical and symbolic
data, there can be numerical and symbolic constants. All numbers are con-
stants in LISP. In addition, most LISP implementations have T and NIL as
symbolic constants for ease of programming condit:ional expressions (as we shall
see in Chapter 11). Apart from these cases, any S-expression may be "quoted"
to make it a symbolic constant. Quoting is performed by the special form QUOTE
described in Chapter 9.

The process of evaluating an elementary form which is just a constant is one


of simply returning that constant as the value of the form. Constants, as in
all programming languages, provide a way of representing data within programs.

7.3 SIMPLE FORMS

Simple forms in LISP consist of a left parenthesis, a function name, parameters,


and a right parenthesis. Syntactically, this looks like

Cfname parameters)

where fname is the name of a built-in function, and parameters may be one or
more variables or constants. In fact, parameters may be empty (i.e., there
may be no variables or constants) if fname is a function of no arguments.

To illustrate, the simple form X* is written in LISP as

(EXPT X 2)

where EXPT is the fname, and variable X and constant 2 are parameters. Other
examples include
(CONS T NIL)
(CAR X)
(CDRABLE)
(NOARGFUNCTION)

The p r o c e s s of e v a l u a t i n g a simple form i s a s follows:

1. A l l parameters a r e evaluated: c o n s t a n t s e v a l u a t e t o themselves;


v a r i a b l e s e v a l u a t e t o t h e i r bindings.

2. The atom following t h e l e f t p a r e n t h e s i s ( i . e . , fname) i s assumed


t o be t h e name of a b u i l t - i n f u n c t i o n . That f u n c t i o n i s c a l l e d
w i t h t h e v a l u e s of t h e parmeters a s arguments.

3. The v a l u e of a simple form i s t h e v a l u e of t h e f u n c t i o n a p p l i e d


t o t h e arguments.

For t h e simple form

(CONS T NIL)

t h e parameters a r e t h e symbolic c o n s t a n t s T and NIL, which e v a l u a t e t o them-


selves. CONS i s c a l l e d w i t h t h e s e v a l u e s t o y i e l d t h e v a l u e (T . NIL).
For t h e lambda e x p r e s s i o n

(LAMBDA (ABLE BAKER) (CONS ABLE BAKER)) (A B)


4 / -
S
1 s2

lambda v a r i a b l e s ABLE and BAKER a r e bound t o A and B, r e s p e c t i v e l y , by lambda


conversion. Then t h e simple form

(CONS ABLE BAKER)

i s evaluated. The parameters of t h e simple form a r e v a r i a b l e s t h a t e v a l u a t e


t o t h e i r bindings, A and B. CONS i s t h e n a p p l i e d t o t h e s e arguments t o y i e l d
t h e v a l u e (A . B) .
7.4 EXERCISES

Try e v a l u a t i n g t h e s e lambda e x p r e s s i o n s :

1. (LAMBDA (X) X) (ATOM)


2. (LAMBDA (Y) Y) ((LIST))
3. (LAMBDA (J) (CAR J ) ) ((THREE ELEMENT L I S T ) )
4. (LAMBDA (K) (CDR K)) ((THREE ELEMENT L I S T ) )
5. (LAMBDA (U V) (CONS U V ) ) (VERY GOOD)
6. (LAMBDA (Y X) (CONS Y X)) (ONE (THEN . ANOTHER))
7. (LAMBDA (A) (CAADR A) ) ( (A (B . 7742) ))
8. (LAMBDA (VARIABLE) (CDAR VARIABLE)) (((A B)))
9. (LAMBDA (J) 3.14159) (NIL)
lo. (LAMBDA ( ) 3.14159) ( )

Note:
Problems 1 and 2 a r e "identit:yl' f u n c t i o n s i n t h a t
t h e y always e v a l u a t e t o t h k i r arguments. Problems
9 and 1 0 a r e " c o n s t a n t " f u n c t i o n s which always
evaluate t o t h e constant specified, i n t h i s case
3.14159, r e g a r d l e s s of t h e v a l u e of t h e argument.
However, t h e s e arguments a r e r e q u i r e d by lambda
conversion. Also, t h e s u p e r v i s o r e x p e c t s a p a i r
of S-expressions a t t h e t o p l e v e l . Further, note
t h a t t h e l i s t of v a r i a b l e s i n problem 1 0 i s empty.
I n LISP, a f u n c t i o n w i t h a n empty v a r i a b l e l i s t i s
a f u n c t i o n of no arguments. For p r o p e r LISP s y n t a x ,
we must always i n c l u d e t h e l i s t of v a r i a b l e s , even
when empty. I n such c a s e s , NIL i s a s a c c e p t a b l e
as ( 1 -

Evaluate :

11. (LAMBDA (U V) U) (ALPHA BETA)


12. (LAMBDA (U V) U) (BETA ALPHA)
13. (LAMBDA (U V) V) (ALPHA BETA)
14. (LAMBDA (V U) V) (ALPHA BETA)
15. (LAMBDA (FIRST SECOND) (CAR FIRST) ) ((FIRST) SECOND)
CHAPTER 8.
COMPOSITION O F FORMS

To apply LISP to more complex problems, we must be able to create more powerful
programs, i.e., more complex forms. This chapter takes a major step in that
direction. It generalizes the concept of a simple form by introducing
composition of forms.

8.1 COMPOSED FORMS

Recall that a simple form has the syntax

(fname parameters)

If we let args stand for zero or more elementary or composed forms, we can write
down the syntax for a composed form as

It follows from this syntax that, if fname is CAR and args is the elementary
form (CDR J) , then

(CAR (CDR J))

is a composed form. It also follows from the recursive nature of the syntax
that, if fnme is CONS and args is the composed form (CAR (CDR J)), then

(CONS (CAR (CDR J)) (CAR (CDR J)))

is a composed form. By similar reasoning, any depth of composition is possible.

Definition:

Form composition is the concatenation of forms


in such a fashion that an argument for a function
at level n is the value resulting from the
evaluation of a form at level n + 1.
8.2 EVALUATING COMPOSED FORMS

The p r o c e s s of e v a l u a t i n g a composed form i s r e c u r s i v e . I f t h e arguments of


t h e composed form are elementary forms, t h e y a r e e v a l u a t e d a s d e s c r i b e d i n
Chapter 7 . I f any of t h e arguments a r e themselves composed forms, t h e evalua-
t i o n p r o c e s s r e c u r s on t h e s e composed forms.

I n g r e a t e r d e t a i l , e v a l u a t i o n c o n s i s t s of e v a l u a t i n g a l l args of t h e composed
form, one a t a t i m e ( g e n e r a l l y from l e f t t o r i g h t ) , by t h e f o l l o w i n g s t e p s :

1. I f args i s a c o n s t a n t , t h e c o n s t a n t i s r e t u r n e d a s t h e v a l u e of args.

2. I f a r g s i s a v a r i a b l e , i t s b i n d i n g i s r e t r i e v e d and r e t u r n e d as t h e
v a l u e of args.

3. I f args i s a s i m p l e form, t h e v a l u e of t h e f u n c t i o n of t h a t form,


a p p l i e d t o t h e v a l u e s of i t s p a r a m e t e r s , i s r e t u r n e d as t h e v a l u e
of a r g s .

4. I f a r g s i s a composed form, a l l p a r t i a l r e s u l t s ( i . e . , t h e v a l u e s of
a l r e a d y e v a l u a t e d a r g s ) a r e saved, and s t e p s 1 through 5 a r e a p p l i e d
recursively t o that args.

5. A f t e r a l l args a r e e v a l u a t e d , t h e v a l u e of t h e f u n c t i o n ( i . e . , fname),
a p p l i e d t o t h e v a l u e s of i t s arguments ( i . e . , a l l t h e a r g s ) , is
r e t u r n e d a s t h e v a l u e of t h e composed form.

It i s u s u a l l y e a s i e r t o e v a l u a t e composed forms t h a n t o u n d e r s t a n d t h e above


steps. Some examples should c l e a r t h e a i r .

Examples :

(LAMBDA ( J ) (CONS (CAR J), (CAR (CDR J ) ) ) ) ((A B)

By lambda c o n v e r s i o n , lambda v a r i a b l e J i s bound t o t h e argument (.A B) , and


t h e composed form

(CONS (CAR J ) (CAR (CDR J ) ) )

i s evaluated. T h i s form h a s t h e s y n t a x

(CONS argl a r g 2 )

-51-
where a r g i s t h e simple form (CAR J ) , and a r g 2 i s t h e composed form
1
(CAR (CDR J ) ) . Evaluating t h e s e arguments from l e f t t o r i g h t , we g e t

a r g l = (CAR J ) = A

But a r g i s a composed form i t s e l f , w i t h t h e syntax


2

where a r g
21
i s t h e simple f o m (CDR J ) .
To e v a l u a t e a r g 2 , we r e c u r and f i r s t e v a l u a t e

arg,, = (CDR J ) = (B)

Then, r e t u r n i n g t h i s v a l u e of t h e simple form a r g we get


21'

a r g 2 = (CAR arg21) = B

Now, having evaluated a l l arguments of t h e composed form, we apply CONS t o


t h e s e arguments and y i e l d

(CONS argl a r g 2 ) = (A . B)
The v a l u e of t h e composed form, (A . B), i s t h e v a l u e of t h e o r i g i n a l lambda
expression.

Consider another example:

(LAMBDA (X Y Z) (CONS X (CONS Y (CONS Z NIL)) ) ) (A B C)

By lambda conversion, X, Y , and Z a r e bound t o A, B, and C , respective:Ly.


The composed form and i t s arguments have t h e syntax

(CONS X argl)
argl = (CONS Y arg12)
arg12 = (CONS Z NIL)

Evaluation of t h e s e forms y i e l d s
arg12 = (CONS Z NIL) = (C)
argl = (CONS Y arg12) = (B C)
(CONS X argl) = (A B C)

and the last value, (A B C), is the value of the lambda expression.

8.3 NESTED LAMBDA EXPRESSIONS

As we have seen, composed forms are generalizations of simple forms. By


examining the syntactic structures,

(f'name parameters) simple form

( f m e args) composed form

we see that the generalization is achieved by allowing any composed form, args,
to appear in lieu of any parameter of a simple form. The discussion of form
composition is not complete, however, until one further generalization is
considered: the generalization of fname.

Lambda expressions are functional expressions, and functional expressions may be


used wherever functions are acceptable. We have already observed this truth
in top-level function calls; e.g.,

CAR ( (A)) = A
'.yL'-,

--
(LAT%BDA (J) (CAR J ) ) ((A))
S
1 "2
= .A

This truth holds equally well in composed forms. For this truth to be self
evident, consider the syntactic entity fexp, defined as either a function name,
i.e., fname, or a functional expression. Then the syntax of the most general
composed form is given by
Examples :

(CONS (CAR J) (CDR J))

( (LAMBDA (X Y) (CONS X Y) ) ( (LAMBDA (K) (CAR K) ) J) (CDR J) ) [4]

These four forms aptly demonstrate the complexity that is possible with form
composition. Observe that forms [l] and [2] are semantically equivalent forms,
as are fo m s [3] and [4] . Note further that form [4] is obtained from form [3]
by substituting an equivalent lambda expression for CONS and by substituting
form [2] for form [I].

In the spirit of generality of structures in LISP, forms may be composed with


arbitrary complexity. In fact, by reason of constructs of the variety of form
[4], it is theoretically possible to have an infinite number of semantically
equivalent forms.

EVALUATING NESTED LAMBDA EXPRl2SSIONS

The rule for evaluating nested lambda expressions is exactly the same as that
for evaluating composed forms given in Paragraph 8.2 (with one addition to step
5). Since we have generalized the syntax of composed forms as given in that
paragraph, by replacing fname by f e q , step 5 should now read (with the addition
underscored) :

"5. A f t e r a l l a r g s a r e e v a l u a t e d , t h e v a l u e of t h e
f u n c t i o n ( i . e . , f e w ) , a p p l i e d t o t h e v a l u e s of i t s
arguments ( i . , a l l t h e a r g s ) , i s r e t u r n e d a s t h e
v a l u e of t h e composed form. I f - f e w i s a lambda
e x p r e s s i o n , a l l lambda v a r i a b l e s a r e bound by lambda
conversion t o t h e v a l u e s of i t s arguments ( i . e . , all
t h e a r g s ) and t h e v a l u e of t h e lambda form i s t h e v a l u e
of t h e composed form."

For compiler-based LISP systems, fname and fexp a r e t r e a t e d n e a r l y a l i k e . If


fay i s a lambda e x p r e s s i o n , i t i s f i r s t compiled and then t r e a t e d e x a c t l y a s
fme. The d i f f e r e n c e l i e s between t h e name of a f u n c t i o n and t h e e x p l i c i t
d e f i n i t i o n of t h a t f u n c t i o n . Nested lambda e x p r e s s i o n s a r e analogous t o " l o c a l
procedures" w i t h i n an ALGOL procedure.

Examples :

I f J i s bound t o (A B) , then

(CAR J ) = A
( (LAMBDA (K) (CAR K)) J ) = A
((LAMBDA (K) (CAR K)) (CDR J ) ) = B

I n form [ 2 ] , lambda v a r i a b l e K i s bound t o t h e v a l u e of J, i . e . , (A B ) , by lambda


conversion. Then e v a l u a t i n g t h e simple form (CAR K), y i e l d s A. I n form [ 3 ] , K i s
bound t o t h e v a l u e of t h e simple form (CDR J ) , i . e . , ( B ) , by lambda conversion,
w i t h t h e answer, B, r e s u l t i n g a s t h e v a l u e of (CAR K) .
(LAMBDA (J) ((LAMBDA (K) (CAR K) ) (CDR J ) ) ) ((A B) ) = B
- - [41

This t o p - l e v e l f u n c t i o n c a l l demonstrates how v a r i a b l e J might b e bound f o r


form [ 3 ] above. It i s , of c o u r s e , bound by lambda conversion.
8.5 EXERCISES

E v a l u a t e the f o l l o w i n g :

(LAMBDA (A B) (CAR (CONS A B ) ) ) ( 4 3 NUMBER)


(LAMBDA (A) (CAR (CDR A) ) ) ((ARG L I S T ) )
(LAMBDA (A) (CDR (CAR A ) ) ( ((A)))
(LAMBDA (A B) (CDR (CONS A B ) ) ) (NUMBER 4 3 )
(LAMBDA (B A) (CDR (CONS A B ) ) ) (NUMBER 4 3 )
(LAMBDA (A B) (CAR (CDR (CONS A B) ) ) ) (A (Y) )
(LAMBDA (J) (CONS (CONS J N I L ) N I L ) ) ((LIST))
(LAMBDA (J) (CAR (CAR (CONS (CDR J) (CDR J ) ) ) ) ) ((A B ) )
(LAMBDA (J) (CAR (CONS 1 2 3 4 3 J ) ) ) (NIL)
(LAMBDA (J) (CONS (CAR J ) (CDR J ) ) ) ((A . B) )
N o t e t h a t p r o b l e m 10 c l e a r l y d e m o n s t r a t e s t h e r e l a t i o n s h i p b e t w e e n CAR, CDR,
and CONS.

11. CAR could be called F I R S T since i t f i n d s t h e f i r s t e l e m e n t of a l i s t .


W r i t e a l a m b d a e x p r e s s i o n ( u s i n g o n l y CAR and CDR) by c o m p o s i t l . o n of
f o r m s w h i c h f i n d s t h e t h i r d e l e m e n t of a l i s t .

For t h e a r g u m e n t

c o m p o s e and evaluate your own l a m b d a expressions (using only CAR and CDR)
t h a t evaluate exactly as t h e f o l l o w i n g abbreviations (see P a r a g r a p h 5.5).

12. CAAR
13. CADR
14. CDAR
15. CADAR

E v a l u a t e the f o l l o w i n g :

16. (LAMBDA (U V) ((LAMBDA (X Y) (CONS (CAR X) (CDR Y ) ) ) U V)) ((A) (B C))


17. (LAMBDA (U V) ( (LAMBDA (X Y) (CONS (CAR X) Y) ) U (CDR V ) ) ) ((A) (B C ) )
18. (LAMBDA (W X) (CAR ((LAMBDA (Y Z ) (CONS Y Z ) ) W X))) ((A) (B C))
19. (LAMBDA (W X) (CDR ((LAMBDA (Y Z) (CONS Y Z)) (CAR W) (CDR X ) ) ) ) ((A) (B C) )
20. (LAMBDA (J) (CONS
3 1 '
( (LAMBDA (X Y) (CONS
% -+ * Y
Y (CONS X N I L ) ) ) (CAR
%' 5 4 % 3
Jj (cADR J)
'Ir
\
((LAMBDA (U V) (CONS (CONS V (CONS U N I L ) ) N I L ) ) (CADDR J ) (cADDDR J ) )
CHAPTER 9.
SPECIAL FORMS

I n LISP, t h e r e e x i s t s a c o l l e c t i o n of b u i l t - i n forms t h a t s u p e r f i c i a l l y look


l i k e functions. They a r e c a l l e d special f o m s , and a r e t r e a t e d d i f f e r e n t l y
than a r e functions. When they appear i n t h e fexp p o s i t i o n of a form, t h a t form
i s e v a l u a t e d i n a s p e c i a l manner; t h e manner of e v a l u a t i o n depends upon t h e
g i v e n s p e c i a l form. I n g e n e r a l , s p e c i a l forms f a l l i n t o two c l a s s e s :

1. S p e c i a l forms w i t h a n i n d e f i n i t e number of arguments.

2. S p e c i a l forms t h a t wish t h e i r arguments unevaluated s o t h a t they


may c o n t r o l t h e way t h e i r arguments a r e e v a l u a t e d .

Some s p e c i a l forms, such a s AND and OR ( s e e Chapter 12) r e s i d e i n b o t h c l a s s e s .

Each s p e c i a l form must be s t u d i e d i n d e t a i l t o determine i t s r u l e s of e v a l u a t i o n .


One such s p e c i a l form, LAMBDA, we have a l r e a d y l e a r n e d how t o e v a l u a t e . Many
of t h e remaining c h a p t e r s a r e devoted t o s i m i l a r examination. I n t h i s chapter,
we s h a l l c o n s i d e r a few important s p e c i a l forms which belong t o t h e s e two
classes.

9.1 -
LIST

LIST, t h e f i r s t s p e c i a l form we s h a l l c o n s i d e r , h a s a n i n d e f i n i t e number of


arguments. These arguments may be any form, i . e . , elementary, composed, o r
special. The arguments a r e e v a l u a t e d ; t h e v a l u e of LIST i s a l i s t of t h e v a l u e s
of t h e s e arguments. It may be d e f i n e d by t h e f o l l o w i n g i d e n t i t i e s :

(LIST) = NIL
(LIST ~ 1 ) = (CONSAI NIL)
(LIST A 1 A2) = (CONS A 1 (CONS A 2 NIL))

(LIST A1 A2 ... AN) = (CONS A 1 (CONS A 2 (CONS ... (CONS AN NIL) ... ) ) )
Examples :

(LIST 1 2) = ( 1 2)
(CONS 1 2) = (1 . 2)
(LIST T NIL 35) = (T NIL 35)
(LIST T (LIST NIL (LIST 3 5 ) ) ) = (T (NIL ( 3 5 ) ) )
(LAMBDA (X Y) (LIST (CONS X Y) (LIST X Y ) ) ) (A B) = ((A . B) (A B))

9.2 QUOTE

QUOTE i s a s p e c i a l form t h a t wishes t o r e c e i v e i t s s i n g l e argument unevaluated.


The argument may b e any S-expression. QUOTE r e t u r n s t h e S-expression, still
unevaluated, a s i t s v a l u e .

The s y n t a x of QUOTE i s g i v e n by

(QUOTE e )

where e may be any S-expression.

Consider t h e f o l l o w i n g two examples:

(LAMBDA (X) (CAR X) ) ((A B C) ) = A

(LAMBDA (X) (QUOTE X) ) ((A B C) ) = X

I n both examples, t h e v a r i a b l e X i s bound t o t h e l i s t (A B C) by lambda


conversion. I n t h e f i r s t example, t h e form

i s e v a l u a t e d by f i r s t e v a l u a t i n g t h e v a r i a b l e X and t h e n applying t h e f u n c t i o n
CAR t o t h a t v a l u e . I n t h e second example, however, we s e e t h e s u p p r e s s i o n of
e v a l u a t i o n y i e l d e d by QUOTE. The form

(QUOTE X)

i s e v a l u a t e d by simply r e t u r n i n g t h e argument X. We do n o t e v a l u a t e X a s we
d i d i n t h e f i r s t example. We speak of X a s being quoted.
Examples :

(LAMBDA NIL (QUOTE ALPHA)) ( )

e v a l u a t e s t o ALPHA.

(LAMBDA (X) (CONS (QUOTE ALPHA) X)) ( BETA )

e v a l u a t e s t o (ALPHA . BETA) .
(LAMBDA (J) (CONS (QUOTE J ) J ) ) (FOO)

e v a l u a t e s t o (J . FOO) .
S i n c e a r b i t r a r y S-expressions i n LISP may look l i k e forms, QUOTE must be u.sed
t o r e p r e s e n t symbolic c o n s t a n t s a s d a t a of forms. Otherwise, an a t t e m p t w i l l
be made t o e v a l u a t e t h e d a t a a s a form--a situation that usually results i n
an e r r o r . For example, t h e form

(CAR ( 1 2 3 ) )

w i l l y i e l d an e r r o r , s i n c e t h e l i s t , ( 1 2 3 ) , i s n o t quoted and obviously can-


n o t be evaluated a s a meaningful form. The c o r r e c t form should be

(CAR (QUOTE (1 2 3 ) ) ) = 1

9.3 EVALQUOTE

I n Chapter 5 we introduced t h e syntax f o r communicating w i t h t h e LISP s u p e r v i s o r .


We s h a l l now examine t h i s communication more c l o s e l y and s e e t h a t i t i s a method
of p a s s i n g a p a i r of unevaluated arguments t o a s p e c i a l form, c a l l e d EVALQUOTE.
The r e a d e r i s cautioned t h a t t h e t r e a t m e n t g i v e n h e r e f o r EVALQUOTE i s peclagogical
and n o t t h e a c t u a l way i t i s implemented i n most systems, though t h e e f f e c t i s
t h e same.

Consider t h e following form:

(CONS (QUOTE CONS) (QUOTE (T NIL)))

When e v a l u a t e d , t h e form y i e l d s a v a l u e

(CONS T NIL)

-60-
which i s i t s e l f a l e g a l form. I f we e v a l u a t e form [2], we g e t

(T . NIL) = (T)

But, form [ 2 ] i s t h e v a l u e of form [ I ] , i . e . , i t i s d a t a , and LISP e v a l u a t e s


a form only once. To e v a l u a t e form [2] would, i n e f f e c t , be e v a l u a t i n g form [ l ]
twice. How t h e n do we e v a l u a t e a computed form? EVAL t o t h e rescue!

L e t u s assume t h e r e e x i s t s a p r i m i t i v e system f u n c t i o n , c a l l e d EVAL, t h a t t a k e s


one argument, t h e v a l u e of which must b e a form. EVAL w i l l simply e v a l u a t e
t h a t form and r e t u r n t h e v a l u e . (This EVAL may d i f f e r s l i g h t l y from t h a t
a v a i l a b l e i n a g i v e n LISP implementation.)

The s y n t a x of EVAL i s g i v e n by

(EVAL e )

where e must e v a l u a t e t o a form.

For example, t h e composed form

(EVAL (QUOTE (CONS T NIL)))

has t h e s t r u c t u r e

(EVAL e )

where

e = (QUOTE (CONS T NIL))

The v a l u e of e i s (CONS T N I L ) , a simple form. It i s t h i s v a l u e t o which


EVAL i s a p p l i e d , y i e l d i n g t h e v a l u e (T).

It i s i n t e r e s t i n g t o n o t e t h a t t h e v a l u e of form [ l ] above i s e x a c t l y t h e v a l u e
of e i n t h i s example. Hence, t h e v a l u e of

(EVAL (CONS (QUOTE CONS) CQUOTE (.T NIL)) 1 >


i s a l s o (T).
With EVAL now d e f i n e d , l e t u s examine EVALQUOTE.

A s we saw e a r l i e r , t h e p a i r of S-expressions t o t h e s u p e r v i s o r (which w e c a l l e d


s1 and s p r e v i o u s l y ) c o n s i s t s of a f u n c t i o n a l e x p r e s s i o n , o r f u n c t i o n name,
2 "1'
and a l i s t of arguments, s 2 , f o r s Forexample,
1'

--
CONS (A B)

S
1 s2

Consider t h e g e n e r a l t o p - l e v e l p a i r of S-expressions f o r t h e s u p e r v i s o r a s

The manner i n which t h e s u p e r v i s o r e v a l u a t e s t h i s p a i r can be explained by


c o n s i d e r i n g t h e e v a l u a t i o n of t h e f o l l o w i n g form:

(EVAL E)

where t h e b i n d i n g of E i s g i v e n a s

E = ( f n (QUOTE argl) (QUOTE a r g 2 ) ... (QUOTE arg,))


To e v a l u a t e t h i s form, t h e v a r i a b l e E i s e v a l u a t e d y i e l d i n g t h e v a l u e

(fn (QUOTE argl) (QUOTE a r g 2 ) ... (QUOTE arg,))


I t - i s t h i s v a l u e t o which EVAL i s a p p l i e d . The f i n a l v a l u e , t h e n , i s t h e
f u n c t i o n f n a p p l i e d t o t h e v a l u e s of i t s arguments. S i n c e t h e arguments of f n
a r e quoted c o n s t a n t s t h a t e v a l u a t e t o themselves, t h e f i n a l v a l u e of (EVAL E) i s
t h e v a l u e of f
n
applied t o argl, arg2, ..., argn--exactly the result we desire.

The important r o l e played by t h e f u n c t i o n EVAL and t h e s p e c i a l form QUOTE i n t h e


o p e r a t i o n of t h e s u p e r v i s o r l e a d s t o t h e name EVALQUOTE. Indeed, most LISP
s u p e r v i s o r s a r e EVALQUOTE s u p e r v i s o r s .

To i l l u s t r a t e t h e EVALQUOTE p r o c e s s more c l e a r l y , l e t u s s t e p through t h e


p r o c e s s once a g a i n , b u t w i t h t h e EVALQUOTE p a i r
CONS (A B)
w -

We a r e t o e v a l u a t e t h e form

(EVAL E)

where t h e binding of E i s

E = ( f n (QUOTE argl) (QUOTE a r g 2 ) ... (QUOTE arg,) )

For t h i s EVALQUOTE p a i r , f n i s CONS, a r g l i s A, and a r g i s B. Therefore, t h e


2
binding of E i s t h e form

(CONS (QUOTE A) (QUOTE B)) [31

The v a l u e , t h e n , of (EVAL E) i s (A . B), which i s the v a l u e of form [ 3 ] . Another


way of s t a t i n g i t i s t h i s : (EVAL E) i s t h e double e v a l u a t i o n of E.

The behavior of t h e EVALQUOTE s u p e r v i s o r should b e more u n d e r s t a n d a b l e now t h a t


w e have "peeked" behind t h e scenes. EVALQUOTE quotes each argument i n t h e l i s t ,
s before it applies t h e function, T h i s e x p l a i n s why i t i s proper t o
2' sl.
enter the pair

CONS (A B)

a t t h e t o p l e v e l , and n e c e s s a r y t o write

(CONS (QUOTE A) (QUOTE B))

a s a n e q u i v a l e n t form a t a l l o t h e r l e v e l s . EVALQUOTE i s a programming


convenience t h a t does our q u o t i n g f o r u s .

I n Chapter 17 we s h a l l review t h e above and a c t u a l . 1 ~d e f i n e EVALQUOTE a s a LISP


s p e c i a l form. We cannot do s o now a s we have n o t a s y e t a c q u i r e d t h e r e c u r s i v e
o r i t e r a t i v e programming s k i l l s n e c e s s a r y t o c o n s t r u c t t h e binding of E.
9.4 EXERCISES

Evaluate:

(LAMBDA NIL (QUOTE X) ) ( )


(LAMBDA (J) (QUOTE J ) ) (ALPHA)
(LAMBDA (J) (QUOTE (AN S EXPRESSION) ) ) (ALPHA)
(LAMBDA (J) (CAR (QUOTE (A B C ) ) ) ) (ALPHA)
(LAMBDA (J) (CDR (QUOTE (J J ) ) ) ) (NOTJ)
(LAMBDA (A B) (CONS A B ) ) (QUOTE EXPR)
(LAMBDA (A B) (CAR (CONS (QUOTE A) B) ) ) (ALPHA BETA)
(LAMBDA N I L (QUOTE
(NOW I S THE TIME FOR ALL GOOD 'MEN TO COME; TO
THE AID OF THE PARTY))) ( )
(LAMBDA N I L (CONS (QUOTE A) (QUOTE B) ) ) ( )
(LAMBDA NIL (QUOTE
(LAMBDA (XI XI)) (
(LAMBDA (A B C) ( L I S T A B C ) ) (ONE TWO THREE)
(LAMBDA (A B C) (CONS A (CONS B (CONS c NIL)))) (ONE TWO THREE)
(LAMBDA (A B C) ( L I S T N I L A N I L B N I L C)) (F F F )
(LAMBDA (A B C) ( L I S T (QUOTE F ) A (QUOTE F) B (QUOTE F ) C)) (P F F )
(LAMBDA (A B C D) ( L I S T ( L I S T N I L (QUOTE F) A)
( L I S T T (QUOTE T) B)
( L I S T N I L (QUOTE N I L ) C)
( L I S T 1 2 3 (QUOTE 1 2 3 ) D ) ) ) (F T N I L 1 2 3 )
16. EVAL ((QUOTE X ) )
17. EVAL ((QUOTE J ) )
18. EVAL ((QUOTE (AN S EXPRESSION)))
19. EVAL ((CAR (QUOTE (A B c ) ) ) )
20. EVAL ((CDR (QUOTE (J J ) ) ) )

C o m p a r e y o u r a n s w e r s f o r p r o b l e m s 1-5 a n d 1 6 - 2 0 .

21. (LAMBDA (J) (EVAL J ) ) ((QUOTE ABLE) )


22. (LAMBDA (J) (CONS (EVAL J) J ) ) ((QUOTE ABLE))
23. (LAMBDA (J K) (EVAL (LIST J K ) ) ) (CAR (QUOTE (ABLE) ) )
24. (LAMBDA (J K) (CONS (EVAL J ) (EVAL K))) ((CAR (QUOTE (CONS))) ( L I S T A B))
25. (LAMBDA (J) (EVAL ( L I S T (QUOTE CDR)
( L I S T (QUOTE QUOTE) (EVAL J) ) ) ) ) ( (QUOTE (A B) ) )
CHAPTER 10.
DE FINE

E v a l u a t i n g lambda e x p r e s s i o n s a t t h e t o p l e v e l i s a one-shot p r o p o s i t i o n . If
we wish t o e v a l u a t e t h e same e x p r e s s i o n f o r d i f f e r e n t arguments, w e must e n t e r
t h e e n t i r e doublet again. A f t e r e v a l u a t i o n , t h e s t a t e of t h e LISP system i s a s
i t was p r i o r t o execution. This i s d e s i r a b l e f o r many s i t u a t i o n s , i n c l u d i n g
debugging, code execution, and program f o r m u l a t i o n . However, f o r t h e m a j o r i t y
of c a s e s , we would l i k e t o s a v e t h e e x p r e s s i o n a s p a r t of t h e LISP system, g i v e
i t a f u n c t i o n name, and u s e i t r e p e a t e d l y t o b u i l d l a r g e r programs. We can do
t h i s w i t h t h e pseudo-function DEFINE.

Pseudo-functions a r e e x p r e s s i o n s t h a t a r e used l i k e f u n c t i o n s , b u t f o r t h e i r
e f f e c t r a t h e r than f o r t h e i r value. They have " s i d e e f f e c t s 1 ' of i n t e r e s t , b u t
e f f e c t s n o t r e f l e c t e d i n t h e v a l u e of t h e e x p r e s s i o n . Input-output functions
a r e o t h e r examples of pseudo-functions.

10.1 DEFINE SYNTAX

DEFINE i s a pseudo-function t h a t t a k e s one argument, a l i s t of f u n c t i o n s t o b e


defined. Like CAR, CDR, and a l l o t h e r single-argument functions evaluated a t
t h e top l e v e l , t h e general syntax is:

DEFINE ( e )
1 1

where t h e argument, e, i s a list of f u n c t i o n s t o be d e f i n e d .

The format of e is:

where t h e f i a r e t h e d e f i n i t i o n s f o r t h e f u n c t i o n s w e wish t o d e f i n e . The


formats a r e a l l ,the same, namely a lambda e x p r e s s i o n , p r e f i x e d w i t h a name.
This name, a l i t e r a l atom, w i l l become t h e f u n c t i o n name. Thus

(name (LAMBDA var l i s t body) )


3 4 43
i s t h e g e n e r a l s y n t a x f o r any of t h e f f u n c t i o n d e f i n i t i o n s , and
i

DEFINE ((
12

(name (LAMBDA var Zis t body) )


3 l 4 43

(name (LAMBDA varzis t body) )


3 4 43

(name (LAMBDA var Zis t body) )


3 " 4 43

i s t h e g e n e r a l s y n t a x f o r t h e complete DEFINE e x p r e s s i o n .

Note t h e p a r e n t h e s e s , t h e i r d e p t h and meaning. The No. 1 p a i r d e l i m i t s t h e


second S-expression, f o r EVALQUOTE; p a i r No. 2 d e l i m i t s t h e s i n g l e argument
s
2'
of DEFINE a s a l i s t ; t h e No. 3 p a i r s d e l i m i t each of t h e n f u n c t i o n a l e x p r e s s i o n s
t o be defined.

Example :

DEFINE ( (
12

(THIRD (LAMBDA (X) (CAR (CDR (CDR X) ) ) ) )


3 4 5 5 5 6 7 76543

(IN3 (LAMBDA (X) (CAR (CAR (CAR X) 1) 1)


3 4 5 5 5 6 7 76543

(SECONDOFlST (LAMBDA (X) (CAR (CDR (CAR X) ) ) ) )


3 4 5 5 5 6 7 76543

I f we wish t o d e f i n e only one f u n c t i o n , t h e s y n t a x i s s t i l l t h e same, w i t h t h e


argument l i s t of DEFINE c o n t a i n i n g one f u n c t i o n a l e x p r e s s i o n .
10.2 EVALUATING DEFINE

The v a l u e of t h e pseudo-function DEFINE i s a l i s t of t h e names of t h e f u n c t i o n s


defined. For t h e example above, LISP would r e t u r n

(THIRD I N 3 SECONDOFlST)

What have we r e a l l y done by e v a l u a t i n g DEFINE? For a compiler-based LISP


system, we have compiled machine code f o r each of t h e f u n c t i o n a l e x p r e s s i o n s i n
t h e argument l i s t . This machine code becomes a permanent p a r t of t h e LISP
system, which can b e r e f e r e n c e d by t h e name given i n t h e f u n c t i o n a l e x p r e s s i o n ,
and can be used t o e v a l u a t e d a t a l i k e a l l o t h e r system f u n c t i o n s . For i n t e r -
p r e t i v e LISP systems, t h e f u n c t i o n a l e x p r e s s i o n d e f i n e s t h e f u n c t i o n , and t h e
e x p r e s s i o n i s r e t a i n e d by t h e system f o r r e c a l l and i n t e r p r e t a t i o n when t h e
function is called.

10.3 REDEFINING

I f , a f t e r d e f i n i n g a f u n c t i o n , you f i n d t h e d e f i n i t i o n t o be i n e r r o r o r you
wish t o change t h e f u n c t i o n ' s d e f i n i t i o n ( i . e . , change t h e f u n c t i o n ' s lambda
expression) f o r o t h e r r e a s o n s , you need o n l y u s e DEFINE a g a i n w i t h t h e o l d
f u n c t i o n name and a new lambda expression. The new lambda e x p r e s s i o n w i l l b e
compiled and r e f e r e n c e d under t h e o l d name. The o l d compiled code i s d i s c a r d e d
and cannot be r e f e r e n c e d a g a i n . A s w i t h l i s t s t r u c t u r e t h a t i s d i s c a r d e d and
c o l l e c t e d l a t e r , d i s c a r d e d compiled code may b e c o l l e c t e d l a t e r . The g e n e r a l
problem of i n t e r n a l s t o r a g e management i n LISP systems i s handled by a program
c a l l e d t h e "garbage col.lector". I n some LISP systems, t h e s p a c e occupied by
t h e o l d code cannot be reclaimed and i s l o s t t o t h e system. Repeated r e d e f i n i -
t i o n s b u i l d up such "garbage" and should be avoided.

F e e l f r e e t o r e d e f i n e programs a t w i l l . I f you t r y v a r i o u s d e f i n i t i o n s on an
a c t u a l computer, be c a r e f u l n o t t o d e f i n e programs whose names a r e system
f u n c t i o n s , a s you w i l l r e d e f i n e f u n c t i o n s p o s s i b l y used i n t e r n a l l y by t h e system
and thereby g e t i n t o t r o u b l e . A r e p r e s e n t a t i v e s e t of system f u n c t i o n names t o
be avoided i s l i s t e d on t h e i n s i d e f r o n t and back covers of t h i s book.
10.4 EXERCISES

Define t h e f o l l o w i n g new f u n c t i o n s and t e s t them on l i s t (A B C D E)

1. ( F I R S T (LAMBDA (X) (CAR X) ) )


2. (SECOND (LAMBDA (Y) (CADR Y)))
3. (THIRD (LAMBDA (Z) (CAR (CDDR 2 ) ) ) )
4. CADDDDR
5. Define a f u n c t i o n , c a l l e d REVDOT, t h a t r e v e r s e s t h e CAR and CDR
of any d o t t e d p a i r . T r y i t on t h e fol.lowing arguments:
CHAPTER 11.
CONDITIONAL EXPRESSIONS

The class of functions that can be formed with what we know so far is quite
limited and not very interesting. We need functions that branch conditionally
on the value of forms and thereby allow a much larger class of functions to be
defined. The special form COND accepts an indefinite number of arguments and
conditionally evaluates these arguments based upon their values. COND thus
allows us to perform analysis of differing cases.

11.1 SYNTAX OF CONDITIONAL EXPRESSIONS

A conditiomZ expression is a special form in LISP and has the following syntax:

where the p are forms that evaluate to NIL or not NIL, and the ei are forms
i
that can have any desired value.

COND takes an indefinite number of arguments, called ezauses, each of which is


gl\i a list containing a pi and its corresponding 7.&' It accepts all its arguments
unevaluated; as a special form, it evaluates these arguments from left to right
under strict control as described below.

EVALUATING CONDITIONAL EXPRESSIONS

LISP evaluates the arguments of a conditional expression from left to right as


follows:

If the value of pl is not NIL,


then the value of COND is the value of el; else,
If the value of p2 is not NIL,
then the value of COND is the value of e2; else,
IS the value of p is not NIL, etc.
3
The e n t i r e e x p r e s s i o n i s searched by e v a l u a t i n g t h e p of each c l a u s e , u n t i l
i
t h e f i r s t pi t h a t i s n o t NIL i s found, and t h e n t h e corresponding e of t h a t
i
c l a u s e i s e v a l u a t e d . Note t h a t e i s never e v a l u a t e d i f t h e corresponding p
i i
of t h a t c l a u s e i s NIL.

I f a non-NIL c l a u s e cannot b e found ( i . e . , a l l pi a r e NIL), t h e n t h e v a l u e of


t h e e n t i r e e x p r e s s i o n i s undefined. To p r o t e c t a g a i n s t t h i s occurrence, LISP
programmers u s u a l l y set t h e p of t h e l a s t c l a u s e e q u a l t o T and s e t t h e l a s t
n
expression, e of t h a t c l a u s e , e q u a l t o some t e r m i n a t i n g e x p r e s s i o n . S i n c e T
n
i s a symbolic c o n s t a n t , i t always e v a l u a t e s non-NIL and COND i s always d e f i n e d .
I f n o t h i n g e l s e proves non-NIL, w i l l be t h e v a l u e of t h e
t h e n t h e v a l u e of e
n
conditional expression. I n some LISP systems, a n undefined c o n d i t i o n a l expres-
s i o n u s u a l l y r e s u l t s i n a n e r r o r d u r i n g e v a l u a t i o n of t h e e x p r e s s i o n . On o t h e r
LISP systems, a n undefined c o n d i t i o n a l e x p r e s s i o n r e t u r n s a v a l u e of NIL.

Examples :

1. The f u n c t i o n NOT r e t u r n s a s i t s v a l u e t h e n e g a t i o n of i t s s i n g l e
argument. I f i t s argument i s NIL, i t r e t u r n s n o t NIL, i . e . , T.
I f i t s argument i s n o t NIL, i t r e t u r n s NIL. It can b e d e f i n e d i n
LISP a s f o l l o w s :

DEFINE ( ( (NOT (LAMBDA (J) (COND ( J NIL) (T T ) ) ) ) ) )

2. Using NOT, w e can d e f i n e t h e f u n c t i o n LN t h a t "counts" t h e l e n g t h


of a l i s t . It r e t u r n s e i t h e r 0, 1, 2, o r LOTS--depending on whether
t h e r e a r e none (empty l i s t ) , one, two, t h r e e o r more elements of t h e
list, respectively. For example,

DEFINE ( ( (LN (LAMBDA (L)


(COND ((NOT L) 0 )
((NOT (CDR L) ) 1 )
((NOT (CDDR L) ) 2)
(T (QUOTE LOTS))))) )I
3. The p r o p o s i t i o n a l connective "implies" has t h e following t r u t h t a b l e :

X
- -
Y X + Y
true true true
true false false
false true true
false false true

I f we l e t T and NIL r e p r e s e n t t r u e and f a l s e , r e s p e c t i v e l y , using


COND, w e can d e f i n e IMPLIES i n two ways:

DEFINE ( ( (IMPLIES (LAMBDA (X Y) (COND (X (COND (Y T) (T NIL)))


(T TI))) 1)

The second d e f i n i t i o n demonstrates t h e n e s t i n g of c o n d i t i o n a l s ;


however, t h e f i r s t d e f i n i t i o n i s more e l e g a n t , s i n c e i t t a k e s f u l l
advantage of t h e n a t u r e of t h e d a t a by l e t t i n g t h e v a r i a b l e s a c t a s
fo m s .

11.3 SELECT

The s p e c i a l form SELECT i s a more g e n e r a l c o n d i t i o n a l form. I n most LISP


systems, SELECT i s u s e d , a s a "switch" t o c o n t r o l program flow, w h i l e COND i s
used a s d e s c r i b e d above.

SELECT has t h e syntax

where t h e pi a r e evalua,ted i n sequence from l e f t t o r i g h t u n t i l one i s found


t h a t i s equal t o p.

Then t h e v a l u e of SELECT i s t h e v a l u e of t h e corresponding ei. I f no such p


i
i s found, t h e v a l u e of SELECT i s t h a t of e.

The LISP f u n c t i o n IMPLIES can be w r i t t e n u s i n g SELECT. Compare t h i s d e f i n i t i o n


with t h o s e of Paragraph 11.2.
DEFINE ( ( (IMPLIES (LAMBDA (X Y) (SELECT X (Y T) Y) ) ) ) )

11.4 EXERCISES
Evaluate the following:

/1. (LAMBDA NIL (COND (NIL (QUOTE FALSE) ) (T (QUOTE TRUE))) ) ( )


2. (LAMBDA (A B C) (COND (T A) (T B) (T C))) (1 2 3)
/
3. (LAMBDA (A B C) (COND (NIL A) (NIL B) (NIL C))) (1 2 3)
!J

,4. (LAMBDA (J) (CONS J (COND (J NIL) (T J)))) (T)


,/ 5. (LAMBDA (J K) (SELECT J ( (QUOTE ODD) (CAR K) )
((QUOTE EVEN) (CDR K))
(QUOTE NOP) 1) (2 (1 2)
6. Define the function OR2 (X Y) that returns NIL as its value if both
X and Y are NIL, and returns T otherwise.
7. Define the function XOR2 (X Y) that returns T as its value if X or Y
are non-NIL, but not both, and returns NIL otherwise.
8. Define the function AND2 (X Y) that returns T as its value if X and Y
are both non-NIL, and returns NIL otherwise.
9. For the COND expression

give an equivalent expression using only SELECT.


10. For the SELECT expression

give an equivalent expression using COND. Assume there exists a


function EQUAL (X Y) that returns T if X and Y are the same S-
expression.
CHAPTER 12.
PREDICATE FUNCTIONS

To perform interesting computations with conditional forms, we need a variety


of functions that test data to determine if a datum is a number, an empty list,
an atom, equal to some other datum, less than or greater than some other number,
a member of a list, etc.

The LISP world is divided on this matter into two camps: purists and pragmatists.
Purists call the test functions predicates, and require such functions to return
one of two values--true or false. The pragmatist argues that conditional forms,
as implemented on all LISP systems, test only for NIL. He, like the purist,
defines a predicate as a function that returns either T (true) or NIL (false).
However, he postulates that an additional class of functions exist that may have
NIL as but one value in an infinite domain of non-NIL values. Since NIL is half
the domain of predicate functions, these functions are called semi-predicutes
and may be used in the predicate position of conditional expressions, e.g,, the
pi of a COND clause. CDR is a perfect example of a semi-predicate.

This chapter examines a number of predicates built into most LISP systems.

12.1 ATOM

The predicate ATOM has one argument. The value of ATOM is T if the value of
the argument is an atomic symbol; otherwise, the value of ATOM is NIL.

Examples :

(ATOM T) = T
(ATOM 1.23) = T
(ATOM NIL) = T
(ATOM (QUOTE AVERYLONGSTRINGOFLETTERS)] = T
(ATOM (QUOTE (A B C)) ) = NIL
(ATOM (CONS T NIL)) = NIL
(ATOM (CDR (QUOTE (A)))) = T
12.2

Note:

The p r e d i c a t e EQ i s v e r y implementation-dependent,
based upon t h e c a n o n i c a l form of s t r u c t u r e s i n t e r -
n a l l y used.by a given LISP system. You a r e t h e r e f o r e
advised t o c o n s u l t your own p a r t i c u l a r r e f e r e n c e
literature.

EQ i s a p r e d i c a t e t h a t has two arguments. The v a l u e of EQ i s T i f t h e v a l u e s


of t h e arguments a r e t h e same p o i n t e r , i . e . , t h e same i n t e r n a l a d d r e s s ; o t h e r -
wise, t h e v a l u e of EQ i s N I L . I n a l l LISP implementations, l i t e r a l atoms have
unique i n t e r n a l a d d r e s s e s , t h e r e f o r e , EQ w i l l be T i f t h e v a l u e s of t h e argu-
ments a r e t h e same l i t e r a l atom.

I n some r e c e n t l y developed systems, s m a l l numbers (e.g., 0 t o 1000) a r e a l s o


unique and can be s u c c e s s f u l l y t e s t e d w i t h EQ. Also, some systems s t o r e l i s t
s t r u c t u r e uniquely, and f o r t h e s e systems EQ can compare two e q u i v a l e n t l i s t s .

For purposes of t h i s t e x t , w e s h a l l assume EQ behaves according t o more conven-


t i o n a l systems and can o n l y t e s t l i t e r a l atoms successfully.

Examples :

(EQ T T) = T
(EQ T NIL) = N I L
(EQ ( ) NIL) = T
(EQ (QUOTE A) (QUOTE B)) = NIL
(EQ (ATOM (QUOTE B)) T) = T

12.3 EQUAL

The p r e d i c a t e EQUAL t a k e s two arguments. The v a l u e of EQUAL i s T i f t h e v a l u e s


of i t s arguments a r e i d e n t i c a l S-expressions; o t h e r w i s e , t h e v a l u e of EQUAL i s
NIL. EQUAL may be used t o t e s t t h e e q u a l i t y of numbers, l i t e r a l atoms, and
list s t r u c t u r e s . It i s more g e n e r a l t h a n EQ, b u t commensurately more time-
consuming i n i t s o p e r a t i o n .
Examples :

(EQUAL T T) = T
(EQUAL NIL NIL) = T
(EQUAL 1.23 1.23) = T
(EQUAL (QUOTE (A B ) ) (QUOTE (A B ) ) ) = T
(EQUAL (LIST T) T) = NIL
(EQUAL 174 1 5 ) = T

Note:

EQUAL w i l l a c c e p t and c o n v e r t numbers of d i f f e r i n g


t y p e s b e f o r e i t performs t h e t e s t f o r e q u a l i t y .
For t h e c a s e of f l o a t i n g - p o i n t numbers, t h e y must
a g r e e w i t h i n a s p e c i f i e d a c c u r a c y t h a t i s dependent
upon t h e c a p a c i t y of t h e machine.

12.4 ARITHMETIC PREDICATES

A l l of t h e f o l l o w i n g p r e d i c a t e s end w i t h t h e l e t t e r "P", f o r "predicate", as a


mnemonic a i d . T h e i r arguments must e v a l u a t e t o numbers, o r e l s e t h e y a r e
undefined .
(NUMBERP N) = T i f N e v a l u a t e s t o a number of any t y p e
= NIL i f N e v a l u a t e s t o a non-atomic S-expression
= NIL i f N e v a l u a t e s t o a l i t e r a l atom

(FIXP N) = T i f N e v a l u a t e s t o a n i n t e g e r o r o c t a l number
= NIL i f N e v a l u a t e s t o a f l o a t i n g - p o i n t number
= Undefined i f N e v a l u a t e s t o a non-numeric S-expression

(FLOATP N) = T i f N e v a l u a t e s t o a f l o a t i n g - p o i n t number
= NIL i f N e v a l u a t e s t o a n i n t e g e r o r o c t a l number
= Undefined i f N e v a l u a t e s t o a non-numeric S-expression

(ZEROP N) = T i f N e v a l u a t e s t o z e r o of any numeric t y p e


= NIL i f N e v a l u a t e s t o a non-zero number
= Undefined i f N e v a l u a t e s t o a non-numeric S-expression
(ONEP N) = T i f N e v a l u a t e s t o one of any numeric t y p e
= NIL i f N e v a l u a t e s t o a number o t h e r t h a n one
= Undefined i f N e v a l u a t e s t o n non-numeric S-expression

(MINUSP N) = T i f N e v a l u a t e s t o a n e g a t i v e number of any numeric type


= NIL i f N e v a l u a t e s t o a p o s i t i v e number 2 .:I -e6:0

= Undefined i f N e v a l u a t e s t o a non-numeric S-expression

(GREATERP N 1 N2) = T i f t h e v a l u e of N 1 i s g r e a t e r than t h e v a l u e of N2,


where N 1 and N2 may be any numeric t y p e s
= NIL i f t h e v a l u e of N 1 i s l e s s than o r e q u a l t o t h e
v a l u e of N2
= Undefined i f N 1 o r N2 e v a l u a t e s t o a non-numeric S-expression

(EVENP N) = T i f N e v a l u a t e s t o an even number of any numeric t y p e


= NIL i f N e v a l u a t e s t o an odd number
= Undefined i f N e v a l u a t e s t o a.non-numeric S-expression

(LESSP N 1 N2) = T i f t h e v a l u e of N 1 i s l e s s than t h e v a l u e of N2,


where N 1 and N2 may be any numeric types
= NIL i f t h e v a l u e of N 1 i s g r e a t e r t h a n o r e q u a l t o t h e
v a l u e of N2
= Undefined i f N 1 o r N2 e v a l u a t e s t o a non-numeric S-expression

12.5 LIST PREDICATES

(NULL L) = T i f t h e v a l u e of L i s t h e empty l i s t ( ) o r NIL


= NIL i f t h e v a l u e of L i s n o t NIL o r ( )

(MEMBERLlL2) = T i f t h e v a l u e o f L l i s atop-level elementof a l i s t


r e t u r n e d a s t h e v a l u e of L2
= NIL i f t h e v a l u e of L1 i s not an element of t h e v a l u e of L2
= NIL i f t h e v a l u e of L1 i s a n element of a s u b l i s t of t h e
v a l u e of L2; e . g . ,

(MEMBER (QUOTE A) (QUOTE ((A) ) ) ) = NIL


Note:

I f t h e d e f i n i t i o n of MEMBER u s e s EQ r a t h e r than
EQUAL, t h e v a l u e of L 1 must be a l i t e r a l atom
i f MEMBER i s ever t o r e t u r n T . I f EQUAL i s used,
t h e v a l u e of L1 may be any S-expression.

12.6 LOGICAL CONNECTIVES

(NOT P) = T i f P e v a l u a t e s t o NIL
= N I L i f P e v a l u a t e s t o any non-NIL S-expression

Note:

NOT and NULL a r e synonymous f u n c t i o n s .

(AND xl x 2 ... xn) = T i f t h e v a l u e s of a l l x i a r e non-NIL


= NIL i f t h e v a l u e of any x i i s NIL

Note:

AND i s a s p e c i a l form and t a k e s an i n d e f i n i t e


number of arguments, n o t a l i s t of arguments. The
arguments of AND a r e e v a l u a t e d i n sequence from l e f t
-
t o right, u n t i l one i s found t h a t i s f a l s e , o r u n t i l
t h e end of t h e l i s t i s reached. The v a l u e of AND i s
T i f a l l arguments a r e non-NIL. The v a l u e of AND i s
NIL i f any argument i s NIL; t h e remaining arguments
a r e unevaluated. Also,

(AND) = T

(OR x1 x 2 ... xn) = T i f t h e v a l u e of any x i i s non-NIL


= NIL i f t h e v a l u e s of a l l x i a r e NIL
Note:

OR i s a s p e c i a l f o b and t a k e s an i n d e f i n i t e number
of arguments, n o t a l i s t of arguments. The argu-
ments of OR a r e e v a l u a t e d i n sequence from l e f t t o
r i g h t , u n t i l one i s found t h a t i s non-NIL, or until
t h e end of t h e l i s t i s reached. The v a l u e of OR i s
T i f any argument i s non-NIL; t h e remaining argu-
ments a r e unevaluated. The v a l u e of OR i s NIL i f
a l l arguments a r e NIL. Also,

(OR ) = NIL

12.7 EXERCISES

Evaluate t h e s e p a i r s f o r EVALQUOTE:

(LAMBDA (J) (CONS (EQ J J ) (QUOTE (F T F) ) ) ) (X)


ATOM (NIL)
NULL (NIL)
NULL ((NIL))
NULL ( ( 1)
EQUAL (0 NIL)
NUMBERP (1965)
NUMBERP ((1965))
(LAMBDA (A B C) (OR (ZEROP A)
(FIXP B)
(FLOATP C ) ) ) ( 1 2 3)
(LAMBDA ( J ) (NOT (AND (ATOM J )
(NUMBERP J )
(FLOATP J )
(MINUSP J )
(NOT (ZEROP J ) ) ) ) ) (-1.0)
GREATERP (1964 1965)
GREATERP (1965 1964)
LESSP (10Q 10)
MEMBER (HEAR (NOW HEAR THIS))
15. MEMBER (HEAR (NOW (HEAR THIS)))
16 (LAMBDA (J) (OR (ONEP J ) (EVENP J) ) ) (2.0)
i 17. The p r o p o s i t i o n a l connective "equivalent" has t h e following t r u t h t a b l e :

X
- Y
- X EQUIV Y
true true true
true false false
false true false
false false true

If T and NIL r e p r e s e n t t r u e and f a l s e , r e s p e c t i v e l y , d e f i n e t h e LISP


f u n c t i o n EQUIV u s i n g only p r e d i c a t e s . T e s t i t on t h e following
EVALQUOTE p a i r s :

EQUIV (T T) = T
EQUIV (T NIL) = NIL
EQUIV (NIL T) = NIL
EQUIV (NIL NIL) = T

.b18. The p r o p o s i t i o n a l c o n n e c t i v e "implies" h a s t h e f o l l o w i n g t r u t h t a b l e :

X
- -
Y X + Y
true true true
true false false
false true true
false false true

If T and NIL r e p r e s e n t t r u e and f a l s e , r e s p e c t i v e l y , d e f i n e t h e LISP


f u n c t i o n IMPLIES using only p r e d i c a t e s . T e s t i t on t h e following
EVALQUOTE p a i r s :

IMPLIES (T T) = T
IMPLIES (T NIL) = NIL
IMPLIES (NIL T) = T
IMPLIES (NIL NIL) = T
19. Define t h e p r e d i c a t e INSEQ t h a t i s T i f a l i s t of 5 elements a r e a l l
numbers i n ascending o r descending o r d e r and NIL otherwise. Do n o t
u s e COND; u s e n e s t e d lambda e x p r e s s i o n s . T e s t i t w i t h t h e s e EVALQUOTE
pairs :

INSEQ ( ( 1 2 3 4 5 ) ) = T
INSEQ ( ( 5 4 3 2 1 ) ) = T
INSEQ ((1Q 2.0 99 l O O O Q 1080.0)) = T
INSEQ ((10Q 1 0 10.0 11.0 1 2 4 ) ) = NIL
INSEQ ((10 9 8 74 7 ) ) = NIL

'20. Given t h e f u n c t i o n DIFFERENCE t h a t y i e l d s t h e d i f f e r e n c e a f two


numbers, d e f i n e t h e p r e d i c a t e EQN t h a t i s T i f i t s two arguments a r e
t h e i d e n t i c a l atom and NIL o t h e r w i s e . Do n o t u s e EQUAL. Test i t with
t h e s e EVALQUOTE p a i r s :

EQN (A A) = T
EQN ( 1 1.0) = NIL
EQN (77Q 774) = T
EQN ((A) A) = NIL
CHAPTER 13.
ARITHMETIC FUNCTIONS

Chapter 4 discusses the LISP syntax of numbers; it might pay to review that
chapter. Let us review three important points:

I. Numbers may occur in S-expressions because they are atomic symbols.

2. Numbers are constants. They do not need to be quoted.

3. Numbers should not be used as variables or function names, e.g., never


in the varZist of a lambda expression.

13.1 GENERAL COMMENTS

All the arithmetic functions must be given numbers as arguments, or S-expres-


sions that: evaluate to numbers; otherwise an error condition will result for
most implementations of LISP.

The numerical arguments to arithmetic functions may be any type of number, i.e.,
integer, octal, or floating-point. An arithmetic function may be given some
fixed-point (i.e., integer or octal) and some floating-point arguments at the
same time, If all of the arguments for a function are fixed-point numbers,
then the value will be a fixed-point number. (Integer and octal arguments
always yield an integer value.) If at least one argument is a floating-point
number, then the value of that function will be a floating-point number.

LISP ARITHMETIC FUNCTIONS

In much of the published LISP literature, functions are presented as top-level


calls to EVALQUOTE, as this style is analogous to mathematical convention. I
have avoided that style until now as it confuses the issue of variables and
constants, particularly since EVALQUOTE quotes its arguments. By writing
functions as simple forms, we know that the arguments of the form are evaluated
before the function is applied. For example, the atoms A and B are constants
in the top-level expression

CONS (A B)
whereas A and B a r e v a r i a b l e s i n t h e simple form

(CONS A B)

With t h e i n t r o d u c t i o n of a r i t h m e t i c f u n c t i o n s below, we have t h e o p p o r t u n i t y


t o adopt t h e more conventional s t y l e ; we cannot confuse t h e "pedagogic
v a r i a b l e s f t of d e f i n i t i o n w i t h t h e numbers a c t u a l l y r e q u i r e d by t h e f u n c t i o n s .

PLUS (xl x2 ... xn) = X1 + X2 + ... + Xn

PLUS i s a s p e c i a l form of a n i n d e f i n i t e number of arguments, t h e v a l u e of which


i s t h e a l g e b r a i c sum of t h e arguments.

DIFFERENCE (x y) = x - y

DIFFERENCE h a s f o r i t s v a l u e t h e a l g e b r a i c d i f f e r e n c e of i t s arguments.

MINUS (x) = -x

MINUS has f o r i t s v a l u e t h e n e g a t i v e of i t s argument.

TIMES ( x l x 2 ... xn) = x


1
* x
2
* ... * xn
TIMES i s a s p e c i a l form of a n i n d e f i n i t e number of arguments, t h e v a l u e of
which i s t h e product (with c o r r e c t s i g n ) of i t s arguments.

ADDl (x) = x + 1

ADDl adds one t o i t s argument and r e t u r n s t h e sum a s i t s v a l u e . The v a l u e i s


f i x e d - p o i n t o r f l o a t i n g - p o i n t according t o t h e argument type.

SUBl (x) = x - 1

SUBl s u b t r a c t s one from i t s argument and r e t u r n s t h e d i f f e r e n c e a s i t s v a l u e .


The v a l u e i s f i x e d - p o i n t o r f l o a t i n g - p o i n t according t o t h e argument type.
MAX (xl x2 ... xn)
MAX chooses the largest of its arguments for its value. Note that

yields a floating-point number, since at least one argument was floating-point.

MIN (xI x 2 - rn xn)

MIN chooses the smallest of its arguments for its value.

QUOTIENT (x y) = x / y

QUOTIENT computes the quotient of its arguments. For fixed-point arguments,


the value is the number-theoretic quotient, e.g., QUOTIENT (5 2) = 2. An
out-of-range result will cause a LISP error.

REMAINDER (x y)

REMAINDER computes the number-theoretic remainder for fixed-point arguments


(e.g., REMAINDER (5 2) = l), and the floating-point residue for floating-point
arguments.

DIVIDE (x y)

DIVIDE returns as its value a list of the QUOTIENT and the REAMINDER of its
arguments. It could be defined by:

(DIVIDE (LAMBDA (X Y) (LIST (QUOTIENT X Y) (REMAINDER X Y))))

EXPT (x y) = xY

If both x and y are fixed-point numbers, xY is computed by iterative mu1t:ipli-


cation. Otherwise, the yth power of x is computed by using logarithms. The
first argument, x, cannot be negative if y is not an integer.
SQRT i s a LISP f u n c t i o n , t h e v a l u e of which i s t h e square r o o t of t h e a b s o l u t e
v a l u e of t h e argument. The v a l u e i s always given a s a f l o a t i n g - p o i n t number.

RECIP (x) = 1 / x

RECIP computes and r e t u r n s a s i t s v a l u e t h e r e c i p r o c a l of i t s argument. The


r e c i p r o c a l of any fixed-point number i s d e f i n e d t o be zero.

ABSVAL (x) = I x 1
ABSVAL r e t u r n s a s i t s v a l u e t h e a b s o l u t e v a l u e of i t s argument. If x is
p o s i t i v e , i t r e t u r n s x. I f x i s n e g a t i v e , i t r e t u r n s t h e v a l u e of MINUS(x).

FLOAT (x)

FLOAT i s a LISP f u n c t i o n , t h e v a l u e of which i s t h e f l o a t i n g - p o i n t e q u i v a l e n t


of i t s argument. It could be d e f i n e d by:

(FLOAT (LAMBDA (X) (PLUS X 0 . 0 ) ) )

ENTIER (x)

ENTIER i s a LISP f u n c t i o n , t h e v a l u e of which, f o r p o s i t i v e numbers, i s t h e


l a r g e s t i n t e g e r l e s s t h a n o r equal t o i t s argument. For n e g a t i v e numbers i t i s
MINUS t h e ENTIER of t h e magnitude of t h e argument. For example:

ENTIER (93.75) = 93
ENTIER (-3.75) = -3
ENTIER (0.35) = 0
ENTIER (-0.35) = 0

Whereas FLOAT c o n v e r t s a fixed-point number t o a f l o a t i n g - p o i n t number, ENTIER


c o n v e r t s a f l o a t i n g - p o i n t number t o a fixed-point number.
13.3 LOGICAL ARITHMETIC FUNCTIONS

The f o l l o w i n g f u n c t i o n s o p e r a t e on f u l l machine words. The only a c c e p t a b l e


arguments a r e f i x e d - p o i n t numbers. These may be e n t e r e d a s o c t a l o r decimal
i n t e g e r s , o r t h e y may b e t h e r e s u l t of a p r e v i o u s computation.

LOGOR i s a s p e c i a l form of a n i n d e f i n i t e number of arguments, t h e v a l u e of


which i s t h e l o g i c a l OR of a l l i t s arguments.

LOGXOR (xl x2 ... xn)


LOGXOR i s a s p e c i a l form of an i n d e f i n i t e number of arguments, t h e v a l u e of
which i s t h e l o g i c a l e x c l u s i v e OR of a l l i t s arguments.

LOGAND (xl x2 ... xn)


LOGAND i s a s p e c i a l form of a n i n d e f i n i t e number of arguments, t h e v a l u e of
which i s t h e l o g i c a l AND of a l l i t s arguments.

LEFTSHIFT s h i f t s i t s f i r s t argument l e f t by t h e number of b i t s s p e c i f i e d by


i t s second argument. I f t h e second argument i s n e g a t i v e , t h e f i r s t argument
w i l l be s h i f t e d r i g h t .

Note:

Various o t h e r numerical f u n c t i o n s can be found on


p a r t i c u l a r LISP systems. They a r e n o t s t a n d a r d , s o
they a r e not described here. Logarithmic and t r i g o -
nometric f u n c t i o n s a r e t y p i c a l c l a s s e s n o t covered
here.
13.4 AN ARITHMETIC EXAMPLE

The power s e r i e s expansion f o r SIN i s given8 by:

where x i s i n r a d i a n s .

we can approximate t h e power s e r i e s a s :

The LISP f u n c t i o n SIN (x), where x i s i n r a d i a n s , can now be d e f i n e d i n terms


of t h i s power s e r i e s approximation.

DEFINE ( (
(SIN (LAMBDA (X) (PLUS X (TIMES -1.6666666673-1 X X X)
(TIMES 8.3333333333-3 X X X X X)
(TIMES -1.9841269843-4 X X X X X X X)
(TIMES 2.7557319223-6 X X X X X X X X X)))) ))

I f we f a c t o r o u t x2 and w r i t e t h e power s e r i e s i n t h e form

SIN x = x ( c l + x 2 (c3 + x 2 (c5 + x 2 (c7 + c9x2) ) ) )


a more c o m p u t a t i o n a l l y e f f i c i e n t LISP program f o r SIN can be d e f i n e d by using
a n e s t e d lambda e x p r e s s i o n , a s we need t o compute x2 only once.
DEFINE ( (
(SIN (LAMBDA (X) ((LAMBDA (XSQ) (TIMES X (PLUS 1 (TIMES XSQ (PLUS -1.66666673-1
(TIMES XSQ (PLUS 8.3333333333-3 (TIMES XSQ (PLUS -1.9841269843-4
(TIMES XSQ 2.7557319223-6)))))))))) (TIMES X X)))) ))

EXERCISES

Evaluate :

PLUS (1 2 3 4 5 6 7 8 9 10)
DIFFERENCE (99 3.14159)
TIMES (2 2 2 2 2 2 2 2 2 2)
ADD1 (777774)
SUB1 (1.0)
MINUS (-0)
MAX (10 124 10.000000001)
MIN (10 124 9.999999999)
QUOTIENT (55 3)
QUOTIENT (55.0 34)
REMAINDER (55 3)
REMAINDER (55 3.0)
DIVIDE (55 3)
DIVIDE (55 3.0)
DIVIDE (55 34)
ENTIER (123.4)
ENTIER (-123.4
ENTIER (0.7)
ENTIER (-0.7)
SQRT (25)
RECIP (3.0)
:RECIP (3)
F'LOAT (123456789)
ABSVAL (-3.14159)
LOGOR (777774 123454)
LOGOR (7070741 123454)
LOGXOR (7777741 123454)
28. LOGXOR (7070741 123454)
29. LOGAND (777774 123454)
30. LOGAND (7070741 123454)
31. LEFTSHIFT (741 1)
32. LEFTSHIFT (741 -1)

Define the following functions:

33. TRIPLE (X) = X +X+X


34. CUBE (X) = X3
35. SIMPLEINTEREST (PRINCIPAL RATE YEARS) = P (1 4- YR)
Y
36. ANNUALCOMPOUND (P R Y) = P (1 + R)
37. TIMECOMPOUND (P R Y T) = ~ ( +1 R/T)
TY
38. The value of a two-by-two determinant is defined by:

Define the LISP function

39. The value of a three-by-three determinant is defined by:

Define the LISP function

THREEBY (all a12 ... a32 a33)


40. Given the three simultaneous equations
we can solve for any variable u by dividing two determinants. The
k
denominator determinant, D, is as defined in problem 39. The numerator
determinant is similar, but with the ck terms replacing the coeffi-
cients of the u variables. For example:
k

Define the LISP function

SOLVE (all a12 ... a32 a33 1' 2


' '3)

which computes the value of all variables uk for three simultaneous


equations in three variables. (Hint: Use your definition of THREEBY
and QUOTIENT.)

Try these equation sets:


CHAPTER 14.
RE CURSIVE FUNC TIONS

The functions we have thus far defined have used lambda expressions, composition
of forms, and conditional expressions. A still wider class of functions can be
defined using these methods and the method of recursion.

It takes time and practice to think recursively, particularly if you have pro-
gramming experience with the linear flow of control common with algebraic
languages. You cannot be taught to think recursively, but you can learn t:o
think recursively. To help you learn, I give some helpful heuristics, exa-mples,
and more examples.

Recursive functions may be defined in a manner similar to other functions


using form composition. When we construct a form, such as

(CONS X Y)

we are making an explicit call upon the function CONS. CONS, in this case, is
an already existing function. In a recursive function definition, for (say)
function f, we likewise make explicit calls upon functions; however, one or
more such calls are made upon the function f itself. The only apparent differ-
ence between calls upon CONS and calls upon f is that f is the function being
described. But LISP doesn't mind. In most algebraic languages, the programmer
is cautioned not to write subroutines that call upon themselves, since that is
recursion and most algebraic languages cannot handle recursion. In LISP we do
it all the time. For example, it is syntactically and semantically proper to
write

(EXAMPLE (LAMBDA (L) (COND ((NULL L) NIL)


(T (CONS (CAR L) (EXAMPLE (CDR L))))) ))

We note that in this "do-nothing" function definition (i.e., EXAMPLE returns as


its value a copy of the input list L), EXAMPLE makes an explicit call upon itself.
EXAMPLE is thereby a recursive function.
Recursive d e f i n i t i o n s always d e f i n e a n i d e a i n one o r more s p e c i a l s t a r t i n g o r
f i n i s h i n g c a s e s , and t h e n d e f i n e t h e i d e a i n t h e g e n e r a l c a s e i n terms of a
preceding o r a d j a c e n t c a s e . L e t ' s s e e how t h i s s t a t e m e n t a p p l i e s t o a LISP
problem.

14.1 A RECURSIVE EXAMPLE

Problem: g i v e n any l i s t , such a s

define the predicate

ATOMLIST (R)

which i s T i f a l l elements of R a r e atoms, and NIL otherwise. How s h a l l we


proceed? E s s e n t i a l l y , we wish t o perform t h e t e s t

I f ATOM A , then
If ATOM B , t h e n
I f ATOM C , t h e n T;
E l s e NIL
E l s e NIL
E l s e NIL

which, a s a LISP f u n c t i o n , would be d e f i n e d by

(ATOMLIST (LAMBDA (A B C)
(COND ((ATOM A) (COND ((ATOM B) (ATOM C))
(T F)))
(T F))))

But t h i s i s n o t a s o l u t i o n t o our problem. W e a r e n o t g i v e n A, B and C


e x p l i c i t l y , b u t r a t h e r l i s t R , which can have any number of elements. We
must do

(ATOM (CAR L ) )

t o t e s t a n element of R. Thus, we could w r i t e


(ATOMLIST (LAMBDA (L) (COND ((ATOM (CAR L))
(COND ((ATOM (CADR L) ) (ATOM (CADDR L) ) )
(T F))) (T F))))

But t h i s l a s t d e f i n i t i o n s o l v e s t h e problem when we know l i s t II has e x a c t l y


t h r e e elements. How about t h e g e n e r a l c a s e where we do n o t know t h e l e n g t h
of l i s t k , o r even when we do know, b u t where II i s v e r y long? W e d o n ' t want
t o write

(CADDDDDDDDDDDDDDDDDDDR L)

even i f w e could, f o r a 20-element l i s t .

The proper s t r a t e g y i s t o t e s t t h e f i r s t element of t h e l i s t w i t h t h e e x p r e s s i o n

(ATOM (CAR L))

I f i t i s NIL, we e x i t NIL. I f i t i s T , we need t o t e s t t h e second l i s t element.


I f i t proves T, then w e t e s t t h e t h i r d element, e t c . But n o t e , i f a f t e r we
t e s t t h e f i r s t element, we remove t h e f i r s t element from t h e l i s t , t h e n t h e
second element becomes t h e f i r s t element of t h e new l i s t and we can apply t h e
same t e s t t o t h e new l i s t . The new l i s t i s

(CDR L)

and t h e t e s t i s a p p l i e d r e c u r s i v e l y . Thus, we can w r i t e

(ATOMLIST (LAMBDA (L)


(COND ((ATOM (CAR L)) (ATOMLIST (CDR L ) ) ) (T F ) ) ) )

which i s r e c u r s i v e . What we have done i s t o f i r s t examine t h e (CAR L). If it


i s a n atom, we reduce t h e l i s t R by t a k i n g t h e (CDR L) t o g e t a new l i s t . Then
we t e s t t h i s new l i s t w i t h ATOMLIST. I f we ever f i n d a non-atomic element, t h e
c o n d i t i o n a l w i l l r e t u r n NIL.

This l a s t d e f i n i t i o n almost works, b u t n o t q u i t e . It f a i l s because we haven't


s e t up a t e r m i n a l c o n d i t i o n . A s i t s t a n d s now, u n l e s s we e x i t NIL because some
element of t h e l i s t was non-atomic, we w i l l r e c u r a g a i n and a g a i n , reducing R
each time u n t i l R no longer has elements b u t i s NIL. And t h e n we would t r y
r e c u r r i n g once more and t r y t o t a k e t h e CDR of NIL. T h e r e ' s t h e rub: (CDR NIL)
i s undefined. To e x i t p r o p e r l y , we must t e s t f o r t h e t e r m i n a l c o n d i t i o n . In
t h i s case

(NULL L)

w i l l suffice. Thus our f i n a l , c o r r e c t r e c u r s i v e d e f i n i t i o n f o r ATOMLIST i s :

DEFINE ( (
(ATOMLIST (LAMBDA (L) (COND ((NULL L) ,T )
( (ATOM (CAR L) ) (ATOMLIST (CDR L ) ) )

Note t h a t i f we e v e r encounter t h e n u l l c o n d i t i o n , ATOMLIST i s T s i n c e a l l p r i o r


elements must have t e s t e d t r u e . We perform t h e n u l l t e s t f i r s t t o a l l o w R t o
be completely g e n e r a l , i n c l u d i n g t h e empty l i s t , NIL. Note t h e n t h a t

ATOMLIST (NIL) = T

14.2 SOME HELPFUL HEURISTICS

The f o l l o w i n g h e u r i s t i c s can b e used a s a i d s i n d e f i n i n g r e c u r s i v e f u n c t i o n s :

1. S t a r t w i t h a t r i v i a l c a s e , o r a t e r m i n a l c a s e i n which t h e r u l e f o r
computation i s known. Some t y p i c a l t r i v i a l o r t e r m i n a l c a s e s a r e :

f o r S-expressions, atoms;
f o r l i s t s , NIL;
f o r numbers, 0 , l .

2. For t h e n o n - t r i v i a l , non-terminal c a s e , t r y t o reduce t h e e x p r e s s i o n


t o a c a s e "nearer" t h e t r i v i a l c a s e .

3. Combine t h e t r i v i a l o r t e r m i n a l c a s e w i t h t h e o t h e r , u s i n g t h e t r i v i a l
o r terminal case f i r s t i n a conditional expression.

4. Always check your d e f i n i t i o n by t r y i n g s e v e r a l simple--but not a l l


trivial--examples .
L e t ' s t r y t h e s e h e u r i s t i c s on t h e r e c u r s i v e d e f i n i t i o n of FACTORIAL, where
n! = UNDEFINED, f o r n < 0
= 1, f o r n = 0
= n * (n-l)!, for n > 0

1. The argument of FACTORIAL i s a number. Therefore, t h e t r i v i a l c a s e


i s f o r n = 0.

2. I n t h e t r i v i a l c a s e where n = 0, then

FACTORIAL (N) = (COND ((ZEROP N) 1 ) )

3. I f n i s not zero, then we can break n! i n t o t h e product of two p a . r t s ,


n and ( n - l ) ! , s i n c e (n-l)! moves us n e a r e r t h e t r i v i a l c a s e ( 2 ) . Thus,

FACTORIAL (N) = (TIMES N (FACTORIAL (SUB1 N) ) )

4. Now, combining t h e two c a s e s ( 2 ) and (3) c o n d i t i o n a l l y w i t h t h e


t r i v i a l c a s e f i r s t , we g e t

DEFINE ( (
(FACTORIAL (LAMBDA (N) (COND ((ZEROP N) 1 )
( T (TIMES N (FACTORIAL (SUB1 M ) ) ) ) ) ) ) ) )

L e t ' s t r a c e through t h i s example f o r n = 3 ,

Arguments of FACTORIAL = 3 , descend ( r e c u r s i o n )


Arguments of FACTORIAL = 2, descend ( r e c u r s i o n )
Arguments of FACTORIAL = 1, descend ( r e c u r s i o n )
Arguments of FACTORIAL = 0, t e r m i n a l c o n d i t i o n
Value of FACTORIAL = 1, ascend
Value of FACTORIAL = 1, ascend
Value of FACTORIAL = 2, ascend
Value of FACTORIAL = 6, complete

What we have e f f e c t i v e l y done i n t h i s example i s t o c r e a t e

FACTORIAL ( 3 ) = (TIMES 3 (TIMES 2 (TIMES 1 1 ) ) )


In general, we will descend as deep as is necessary to reach the terminal case
and the effective computation will be

FACTORIAL (n) = (TIMES n (TIMES n-1 ... (TIMES 2 (TIMES 1 1)) ... ) )
14.3 MORE RECURSIVE EXAMPLES
The following function definitions are pedagogical devices. Although these
functions are available in LISP, these definitions may not exactly replicate
those in a given system.

1. The function EQUAL (x y) that we saw in Chapter 12 can be defined by:

DEFINE ( ( (EQUAL (LAMBDA (X Y)


(COND ( (ATOM X) (EQ X .'Y))
((ATOM Y) NIL)
( (EQUAL (CAR X) (CAR Y) ) (EQUAL (CDR X) (CDR Y) ) )
(T NIL)))) ))

This definition uses EQ and hence does not handle numbers.

2. The function APPEND has two arguments, both lists. The value is a
list formed by appending the second list to the first. For example:

APPEND ((A B) (D E F)) = (A B D E F)

DEFINE ( ( (APPEND (LAMBDA (X Y)


(COND ((NULL X) Y)
(T (CONS (CAR X) (APPEND (CDR X) Y)))))) ))

3. The function LAST has one argument, a list. The value is the last
top-level element of the list.

DEFINE ( ( (LAST (LAMBDA (L)


(COND ((NULL L) NIL)
((NULL (CDR L) ) (CAR L) )
(T (LAST (CDR L)))Z 1) 1).
4. Given a l i s t of p a i r s of t h e format

((a, bl) (a2 b2) . (an bn))

t h e f u n c t i o n ASSOC (E L) s e a r c h e s t h e l i s t L f o r a p a i r , t h e f i r s t .
element of which i s e q u a l t o Em I f such a p a i r i s found, i t i s
r e t u r n e d a s t h e v a l u e of ASSOC. Otherwise, t h e v a l u e i s NIL, e . g . ,

ASSOC (AA ((A B) (C D) (AA FOO))) = (AA FOO)

DEFINE ( ( (ASSOC (LAMBDA (E L)


(COND ((NULL L) NIL)
((EQUAL E (CAAR L)) (CAR L))
(T (ASSOC E (CDR L ) ) ) ) 1) ) )

5. The p r e d i c a t e MEMBER d e s c r i b e d i n Chapter 12 can be d e f i n e d by:

DEFINE ( ( (MEMBER (LAMBDA (A X)


(COND ((ATOM X) (EQUAL A X))
( (EQUAL A (CAR X ) ) T)
(T (MEMBER A (CDR X) 1) 1) ) )

This d e f i n i t i o n u s e s EQUAL, and t h u s a l l o w s t h e v a l u e of A t o be


any S-expression.

14.4 LABEL NOTATION

E a r l i e r w e saw t h a t w e could compose and e v a l u a t e t o p - l e v e l lambda e x p r e s s i o n s .


These were temporary lambda e x p r e s s i o n s . I f we named them, we could w i t h DEFINE
make them permanent f u n c t i o n s . Recursive e x p r e s s i o n s p o i n t up a n inadequacy i n
lambda n o t a t i o n t h a t r e q u i r e s u s t o d e f i n e a s permanent, r e c u r s i v e f u n c t i o n s t h a t
we wish t o u s e a s temporary f u n c t i o n s . This d i f f i c u l t y stems from t h e i n a b i l i t y
t o c a l l t h e f u n c t i o n from w i t h i n i t s e l f , s i n c e t h e lambda e x p r e s s i o n i s n o t
named. (When a f u n c t i o n i s r e c u r s i v e , i t must be g i v e n a name.) To r e s o l v e
t h i s d i f f i c u l t y and t h e r e b y a l l o w composition and e v a l u a t i o n of temporary r e -
c u r s i v e f u n c t i o n s , we u s e t h e s p e c i a l form LABEL.

To w r i t e temporary f u n c t i o n s t h a t can c a l l themselves r e c u r s i v e l y , we w r i t e


(LABEL name lambda-expression)

where "name" i s any l i t e r a l atom you choose as t h e name f o r t h e g i v e n lambda


expression.

Example :

(LABEL DUMMY (LAMBDA (X)


(COND ((ATOM X) X)
(T (DUMMY (CAR ~ ) ) ) 1 ) ) ) (argument l i s t )

Lube2 notation, a s t h i s i s c a l l e d , c r e a t e s temporary e x p r e s s i o n s t h a t , l i k e t h e


temporary lambda e x p r e s s i o n s seen e a r l i e r , may be e v a l u a t e d a t t h e t o p l e v e l .
Also, l i k e temporary lambda e x p r e s s i o n s , the e x p r e s s i o n must be e n t e r e d a g a i n
each time i t i s a p p l i e d t o a d i f f e r e n t argument l i s t . I n fact, that is the
meaning of "temporary expression" a s used h e r e . Of c o u r s e , we can always u s e
DEFINE t o c r e a t e permanent f u n c t i o n s r a t h e r t h a n r e p e a t e d l y u s i n g LAMBDA o r
LABEL. I n p r a c t i c e , temporary lambda e x p r e s s i o n s a r e used f r e q u e n t l y , b u t LABEL
i s seldom used, t h e p r e f e r e n c e being t o a t t a c h t h e name by u s e of DEFINE.

14.5 EXERCISES

1. Evaluate

(LABEL NAME (LAMBDA (X) (COND ((ATOM X) X) (T ( N W (CDR X)) ) ) ) )

f o r t h e f o l l o w i n g arguments:

j! A
J f (A B)

d
( ( X . Y) . (X. z))
-8 (A B C)
)
C
(A (C . E))
2. Evaluate

(LABEL MATCH (LAMBDA (X Y)


(COND ((OR (NULL X) (NULL Y) 1 (QUOTE NO) )
((EQ (CAR X) (CAR Y) (CAR X)
(T (MATCH (CDR X) (CDR Y ) ) ) ) ) )
-99-
f o r t h e following arguments:

1,'
(K A Y) (E V E)

I*
/;> ( E L L I N ) (HE L EN)

3. Define

(TWIST (LAMBDA (S)


(COND ((ATOM S) S)
(T (CONS (TWIST (CDR S ) )
(TWIST (CAR S ) ) ) 1))

Evaluate

u' 64) TWIST (A)


/,, TWIST ( ( A . B))

J.
TWIST (((A . B) . C ) )
* >
TWIST ( ( A B C))
TWIST (((A . B)))
4. Let u s p l a n how t o d e f i n e , r e c u r s i v e l y , t h e f u n c t i o n

SUM (x y) = x + y

using only t h e f u n c t i o n s ADD1 and SUB1, and t h e p r e d i c a t e ZEROP.

The t r i v i a l c a s e i s y = 0. Then t h e v a l u e of SUM would be t h e


v a l u e of x.

We can reduce t h e g e n e r a l c a s e , y $ 0, t o t h e t r i v i a l one by reducing


y by 1, i n c r e a s i n g x by 1, and r e c u r r i n g on t h e s e v a l u e s . This g i v e s
r i s e t o t h e following d e f i n i t i o n :

(SUM (LAMBDA (X Y)
(COND ( (ZEROP Y) X)
(T (SUM (ADD1 X) (SUB1 Y)))))
Using t h i s d e f i n i t i o n , show t h e arguments and v a l u e s of SUM each time
i t i s entered f o r

SUM ( 1 2)

5. Define r e c u r s i v e l y , u s i n g o n l y t h e f u n c t i o n s ADD1, SUB1, and ZEROP

PROD (x y) = x * y

Hint :

I f y = 0, t h e n t h e product i s t r i v i a l l y zero. If not,


t h e n t h e product i s t h e SUM of x and t h e PROD of x and
y-1.

6. We know t h a t d i v i s i o n i s e s s e n t i a l l y r e p e a t e d s u b t r a c t i o n , and t h a t
t h e remainder i n d i v i s i o n i s t h e r e s i d u e when s u b t r a c t i o n i s no
longer possible. Therefore, d e f i n e recursively

which y i e l d s t h e remainder r e s u l t i n g from t h e d i v i s i o n of x by y.

7. Define t h e r e c u r s i v e f u n c t i o n COUNT having one argument. The


argument may be any S-expression. The v a l u e of COUNT i s t h e number
of atoms i n t h e argument.

8. The Fibonacci s e r i e s i s a sequence of i n t e g e r s . The f i r s t two terms


a r e 1 and 1, r e s p e c t i v e l y . A f t e r t h a t , each term of t h e s e r i e s i s
t h e sum of t h e preceding two terms.

The Fibonacci s e r i e s begins a s


r4 r- $1
1, 1, 2 , 3 , 5, 8, 13, 21, ...
Define t h e f u n c t i o n FIBB(N) t h a t r e t u r n s a s i t s v a l u e t h e Nth
term of t h e s e r i e s .
9. The g r e a t e s t common d i v i s o r (G.C.D.) of two whole numbers i s t h e
l a r g e s t number t h a t w i l l e x a c t l y d i v i d e b o t h of them. Euclid gave
a n a l g o r i t h m , which can b e s t a t e d i n English.

The G.C.D. of x and y i s :


I f x i s g r e a t e r t h a n y , t h e n f i n d t h e G.C.D. of y and x. Else,
i f t h e remainder of y d i v i d e d by x i s z e r o , t h e n t h e v a l u e i s x .
E l s e , t h e v a l u e i s t h e G.C.D. of x and t h e remainder of y d i v i d e d
by x.

Use t h i s a l g o r i t h m t o d e f i n e GCD (x y ) ; e . g . ,

GCD (7 7) = 7
GCD (19 7 ) = 1
GCD (28 35) = 7

10. Define

AMONG ( a R)

which is a p r e d i c a t e t h a t i s T i f and o n l y i f t h e atom a i s among


t h e t o p - l e v e l elements of l i s t R.

AMONG (X (A B X)) = T
AMONG (X (A B ( X ) ) ) = NIL

11. Define

INSIDE (a e )

which i s a p r e d i c a t e t h a t i s T i f and only i f t h e atom a a p p e a r s


anywhere a t any l e v e l i n t h e S-expression e .

INSIDE (X (A B X)) = T
INSIDE (X (A (X) B ) ) = T
INSIDE (X ( A . (B . x))) = T
12. Define

COPYN (x n)

which will put n copies of x on a list; e.g.,

COPYN ( (A B) 3) = ((A B) (A B) (A B) )

Most LISP systems have functions LENGTH, UNION, INTERSECTION, REVERSE, and
SUBST available as built-in functions. If you e,rrin redefining such functions
to the computer, you can "crash" the system. For that reason, I have avoided
name clashes in the following problems, but realize the direct correspondence
between these names and LENGTHS, UNIONS, INTERSECT, REVERSAL, and REPLACE,
respectively.

13. Define

LENGTHS (R)

which counts the number of top-level elements of a list, e.g.,

LENGTHS ((A B (C D) E)) = 4

14. Define

UNIONS (x y)

which returns a list that contains every element that is in one list
or the other or both. The order in which the elements are presented
is first, all the elements that are in the first list, x, and not in
the second list, y, and second, all elements in the second list, y,
whether or not they are in list x.

Hint :

Use the &unction MEMBER.

UNIONS ((U V k?) (W X Y)) = (U V W X Y)


UNIONS ((A B C) (B C D)) = (A B C D)
15. Define

INTERSECT (x y )

which r e t u r n s a l i s t of elements common t o b o t h l i s t x and l i s t y.

INTERSECT ((A B C) (B C D ) ) = (B C)
INTERSECT ((A B C) (D E F ) ) = NIL

16. Define

REVERSAL (R)

which r e v e r s e s t h e o r d e r of t o p - l e v e l elements of t h e l i s t R; e . g . ,

REVERSAL ( ( ( A B) D (D E) G) ) = (G (D E) D (A B ) )

Hint :

Use APPEND a s given i n t h e e a r l i e r examples.

17. Define

PAIRS (R1 R2)

which produces a l i s t of d o t t e d p a i r s of t h e elements of two l i s t s


of e q u a l l e n g t h ; e . g . ,

PAIRS ((ONE TWO THREE) (1 2 3 ) ) = ((ONE . 1) (TWO . 2) (THREE . 3))


18. Define

DELETE (a R)

which produces a new l i s t i n which a l l r e f e r e n c e s t o t h e atom a have


been d e l e t e d from t h e t o p l e v e l of l i s t R ; e.g.,

DELETE (Y (X Y z ) ) = (X Z)

19. Define t h e p r e d i c a t e

INSEQ (R)
which is T if list P. contains a numerical sequence in proper ascending
or descending order, and NIL otherwise.

Hint :

Use an auxiliary function INSEQA that tests ascending


order only. Use INSEQA with REVERSE (a system function
analogous to REVERSAL above) to test descending order.

INSEQ (1 2 3 4) = T
INSEQ (40 30 2 1) = T
INSEQ (1 23 24 27 26 30) = NIL
INSEQ (10.0 9 8 7.4 2.3) = T
INSEQ (A B C D E) = NIL

20. Define

REPLACE (a b x)

which is a function that replaces the atom b by the atom a for every
occurrence of b in x. a, b, and x are S-expressions.

REPLACE (A B (A B C D)) = (A A C D)
REPLACE (TWO TO (WE TO HAVE TO CATS)) = (WE TWO HAVE TWO CATS)
CHAPTER 15.
THE PROGRAM FEATURE

The LISP 1.5 program f e a t u r e , which i s c a l l e d by t h e s p e c i a l form PROG, a l l o w s


u s t o w r i t e a n ALGOL-like program c o n t a i n i n g s t a t e m e n t s t o be executed. 'It
p r o v i d e s t h e c a p a b i l i t y t o perform i t e r a t i o n by allowing looping and t h e u s e of
temporary v a r i a b l e s .

15.1 PROG FORMAT

A s you r e c a l l , a lambda e x p r e s s i o n h a s t h e f o l l o w i n g syntax:

(LAMBDA v a r Z i s t body)

The PROG form becomes t h e body o r p a r t of t h e body of a lambda e x p r e s s i o n . Like


a l l forms, i t i s a n S-expression; i t has t h e s t r u c t u r e

(PROG v a r Zist s t a t e m e n t s )

The l i s t of v a r i a b l e s , v a r Z i s t , a s w i t h lambda e x p r e s s i o n s , c o n t a i n s t h e
v a r i a b l e s of t h e PROG r e q u i r e d by t h e s t a t e m e n t s . The s t a t e m e n t s a r e themselves
S-expressions.

Thus, t h e complete lambda e x p r e s s i o n w i t h t h e PROG form h a s t h e s t r u c t u r e

(LAMBDA ( lambda-variab Zes ) (PROG (program-variab l e s ) s t a t e m e n t s ) )

15.2 PROGRAM VARIABLES

We u s u a l l y c a l l t h e v a r i a b l e s a s s o c i a t e d w i t h t h e lambda e x p r e s s i o n lambda
v a r i a b l e s , and t h o s e a s s o c i a t e d w i t h t h e PROG, program o r prog v a r i a b l e s . The
l i s t of program v a r i a b l e s , j u s t l i k e t h e l i s t of lambda v a r i a b l e s , must always
b e p r e s e n t i n t h e s t r u c t u r e of t h e e x p r e s s i o n . I f w e have none, t h e n t h e l i s t
i s e n t e r e d a s NIL o r ( ) .

A s w i t h lambda v a r i a b l e s , program v a r i a b l e s a r e a l s o d m y v a r i a b l e s ; we: u s u a l l y


t h i n k of dummy v a r i a b l e s a s being e i t h e r lambda o r program v a r i a b l e s .
Unlike lambda v a r i a b l e s , which have no v a l u e u n t i . 1 lambda conversion, program
v a r i a b l e s always have v a l u e NIL u n t i l they a r e changed o r s e t by s t a t e m e n t s
w i t h i n t h e PROG.

Two f u n c t i o n s a r e used t o s e t dummy v a r i a b l e s ( i . e . , lambda o r program v a r i a b l e s ) ,


SET and SETQ.

SET i s a f u n c t i o n of two v a r i a b l e s , and has t h e form:

which can be r e a d a s " s e t t h e v a l u e of v e q u a l t o t h e v a l u e of v " Both


1 2 .
variables v and v can be (and u s u a l l y a r e ) S-expressions themselves. They
1 2
a r e e v a l u a t e d and t h e v a l u e of v must be a l i t e r a l atom. The v a l u e of v 2 i s
1
bound t o t h e v a l u e of vl. I f we wish t o s e t a s p e c i f i c program v a r i a b l e , w e
must always u s e QUOTE, e.g.,

(SET (QUOTE P I ) 3.14159)

SETQ i s l i k e SET, b u t f o r convenience, SETQ always quotes i t s f i r s t argument.


The "Q" i n t h e name "SETQ" i s t o remind u s of t h i s f a c t , e. g .,
(SETQ P I 3 -14159)

SET and SETQ r e t u r n a s t h e i r v a l u e s t h e v a l u e of t h e second argument,


v2'

15.3 FLOW OF CONTROL

Within t h e PROG form, t h e flow of c o n t r o l i s l i k e t h a t of FORTRAN o r ALGOL.


Each program s t a t e m e n t i s a form, and t h e sequence of s t a t e m e n t s i s a sequence
of forms. The s i m p l e s t S-expressions a r e l i t e r a l atoms, and t h e s e a r e used a s
statement ZabeZs o r names f o r t h e s t a t e m e n t s t h a t follow. For example,

(SETQ P I 3.14159)
LOCl (SETQ R N)
(SETQ AREA (TIMl3S R P I R ) )

has atomic symbol LOCl a s a name f o r t h e statement

(SETQ R N)
Statements are normally executed in sequence. Executing a statement means
evaluating the form. Program statements are often executed for their effect
rather than for their value, as with SETQ above. The GO statement is a perfect
example of execution for effect rather than value. GO is a function used to
cause a transfer to a named statement. It is a function of one argument that
is not evaluated--that argument being a statement label, e.g.,

(GO LOC1)

To exit from a PROG, we use RETURN. RETURN is a function of one argument, and
the argument is evaluated. The value of the argument is returned as the value
of the PROG. A RETURN statement terminates the PROG form and no further
statements are executed.

We can also exit from a PROG without the RETURN statement by just "running outs'
of statements. In that case, the value of the PROG is always NIL.

PROG statements can be constructed from any of the expressions available in


LISP. They may 'beconditional or recursive expressions. They may even be
lambda or program expressions, thereby allowing nesting of program expressions.

15.4 SOME CAUTIONS

Conditional expressions as program statements have a useful peculiarity. If


there are only NIL clauses, instead of an error indication (which would other-
wise occur on some systems), the program continues with the next statement. In
other words, the program "falls through" the conditional expression if there are
no true conditions. This peculiarity is true only for conditional expressions
that are statements of a PROG. Nested conditional forms used in the PROG
expression behave in the normal manner (see Chapter 11).

For some LISP systems, GO may be used only as a statement of a PROG or


immediately inside a COND that is a statement of a PROG. Most implementations
have removed this restriction.

If we nest a PROG within a PROG, within a PROG, etc., the GO, RETURN, SETQ,
etc., will have a scope local to the most recent PROG. For example, GO cannot
transfer to a statement label within another higher- or lower-level PROG.
S i m i l a r l y , RETURN t a k e s you "up" one l e v e l t o t h e n e x t h i g h e r e x p r e s s i o n . In
c e r t a i n s p e c i a l c a s e s , SETQ may b e used on v a r i a b l - e s d e f i n e d by a h i g h e r - l e v e l
PROG. These v a r i a b l e s are t h e n c a l l e d free variables and r e q u i r e s p e c i a l
attention. W e w i l l d i s c u s s v a r i a b l e s and t h e i r b i n d i n g s i n t h e n e x t c h a p t e r .

15.5 EXAMPLES

I n t h e l a s t c h a p t e r we saw t h e r e c u r s i v e d e f i n i t i o n of FACTORIAL. ~ e t ' s


c o n t r a s t t h a t e x p r e s s i o n w i t h one u s i n g t h e program f e a t u r e .

FACTORIAL--recursive definition

DEFINE ( (
(FACTORIAL (LAMBDA (N)
(COND ((ZEROP N) 1 ) (T (TIMES N (FACTORIAL (SUB1 N ) ) ) ) ) ) )
1)

DEFINE ( (
(FACTORIAL (LAMBDA (N) (PROG (Y) ,
(SETQ Y 1 )
TAG1 (COND ((ZEROP N) (RET'URN Y) ) )
(SETQ Y (TIMES N Y))
(SETQ N (SUB1 N))

I n t h e s e examples, t h e r e c u r s i v e d e f i n i t i o n a p p e a r s t o b e s i m p l e r t h a n t h e
one u s i n g t h e program f e a t u r e . I n o t h e r problems i t may b e o t h e r w i s e . The
c h o i c e of whether t o u s e t h e program f e a t u r e o r t o u s e "pure LISP" i n writing a
program, depends i n l a r g e measure on t h e problem. S t y l e i n programming i s o f t e n ,
however, t h e s t r o n g e r influence--as noted by F i s h e r Black.
9

The f u n c t i o n PROG2 i s d i s t a n t l y r e l a t e d t o PROG. I t i s a f u n c t i o n of two argu-


ments t h a t e v a l u a t e s both i t s arguments i n o r d e r , i . e . , argument one f i r s t ,
argument two second. PROG2 h a s a s i t s v a l u e t h e v a l u e of i t s second argument.
Thus,

(LAMBDA (X Y) (PROG2 (CONS X Y) Y ) ) (A B) = B

T h e u t i l i t y of PROG2 can b e seen i n t h e f o l l o w i n g e x a m p l e .

Example:

G i v e n a l i s t of n u m b e r s , d e f i n e the f u n c t i o n SORT,
w h i c h s o r t s these n u m b e r s i n t o odd o r even and
r e t u r n s a l i s t of t w o s u b l i s t s of t h e f o r m :

((odd-count l i s t - o f - o d d - n u m b e r s ) (even-count l i s t - o f - e v e n - n u m b e r s ) )

SORT ((12 3 4 5 ) ) = ((3 (5 3 1)) (2 (4 2 ) ) )


SORT ( ( 1 3 5 7 9 ) ) = ( ( 5 (9 7 5 3 1)) (0 N I L ) )
SORT ( ( 2 4 6 8 1 0 ) ) = ( ( 0 N I L ) (5 (10 8 6 4 2 ) ) )

DEFINE ((
(SORT (LAMBDA (X) (PROG (ODD EVEN ODDCNT EVENCNT L )
(SETQ L X) (SETQ ODDCNT 0) (SETQ EVENCNT 0)
LOOP (COND ((NULL L) (RETURN ( L I S T ( L I S T ODDCNT ODD)
( L I S T EVENCNT E V E N ) ) ) )
((EVENP (CAR L ) )
(SETQ EVEN (PROG2 (SETQ EVENCNT (ADD1 EVENiCNT) )
(CONS (CAR L ) EVEN) ) ) )
(T (SETQ ODD (PROG2 (SETQ ODDCNT (ADD1 ODDCNT))
(CONS (CAR L ) ODD)) ) ) )
(SETQ L (CDR L ) )
(GO LOOP) 1)) ))

Note:

T h e c o n d i t i o n a l clause

(T (SETQ ODD (PROG2 (SETQ ODDCNT (ADD1 ODDCNT))


(CONS (CAR L ) O D D ) ) ) )

c o u l d have been w r i t t e n

-110-
(T (PROG2 (SETQ ODDCNT (ADD1 ODDCNT) )
(SETQ ODD (CONS (CAR L) ODD) ) ) )

I chose the former method to emphasize the return of the value of the last argu-
ment. In fact, some systems generalize PROG2 to PROGN, a special form of an
indefinite number of arguments that returns the value of the last argument.
PROGN can be defined as a macro (see Chapter 19) in terms of PROG2.

15.7 EXERCISES

1. Using PROG, define the function

NEGCNT (2)

which counts the number of negative numbers at the top level of list R.

2. The discriminant b2-4ac, of a second degree equation of the form

can be used to determine the type of curve represented by the equation,


according to the following schedule:

1. A parabola if discriminant = 0
2. An ellipse if discriminant < 0
3. A hyperbola if discriminant > 0

Define

CURVE (a b c)

which evaluates to PARABOLA, ELLIPSE, or HYPERBOLA as a function of


the numerical values of arguments a, b, and c.

3. The recursive definition for LENGTHS is:

DEFINE ( (
(LENGTHS (LAMBDA (M)
(COND ((NULL M) 0)
(T (ADD1 (LENGTHS (CDR M) 1)) 1) 1 )

Define LENGTHS using PROG.


-111-
4, The r e c u r s i v e d e f i n i t i o n f o r LAST i s :

DEFINE ( (
(LAST (LAMBDA (L)
(COND ((NULL L) NIL)
((NULL (CDR L) ) (CAR L) )
(T (LAST (CDR L ) ) ) ) ) ) 1)

D e f i n e LAST u s i n g PROG.

D e f i n e t h e f o l l o w i n g f u n c t i o n s u s i n g PROG. (See Chapter 1 4 , problems 1 6 , 1 7 ,


and 1 8 . )

5. REVERSAL
6. PAIRS
7. DELETE

8. Each d i f f e r e n t arrangement of all o r a p a r t of a s e t of t h i n g s i s


c a l l e d a "permutation". The number of p e r m u t a t i o n s of n d i f f e r e n t
t h i n g s taken r a t a time i s

P ( n , r ) = n! / (n-r)!

Define, w i t h and w i t h o u t PROG ( u s i n g FACTORIAL),

PERMUT (n r ) = n! / (n-r)!

9. Each of t h e groups o r r e l a t i o n s which can b e made by t a k i n g p a r t o r


a l l of a s e t of t h i n g s , w i t h o u t r e g a r d t o t h e arrangement of t h e
t h i n g s i n a group, i s c a l l e d a "combination". The number of combi-
n a t i o n s of n d i f f e r e n t t h i n g s t a k e n r a t a t i m e i s

C(n,r) = n! / r ! (n-r)!

D e f i n e , w i t h and w i t h o u t PROG

COMBIN (n r ) = n! / r! (n-r)!
10. An interesting way to obtain the combinations of n different things
taken r at a time is to construct Pascal's triangle. The triangle
looks like this:
r=O
n=O -+ 1 r=l
n=l + 1 1 r=2
Y
n=2 -+ 1 2 1 r=3
n=3 -+ 1 3 3 1 r=4
n=4 -+ 1 4 6 4 l Y r +
n=5 -+ 1 5 1 0 1 0 5 1

Given the pseudo-function PRINT, which takes one S-expression as its


argument and prints the value of that argument, e.g.,

.... (PRINT (LIST (QUOTE A) (QUOTE B) 3 (QUOTE C))) = (A B 3 C)

and ignoring the triangular format, use your definition for COMBIN to
define

PASCAL (n)

which prints Pascal's triangle to depth n, e.g.,

PASCAL (5) =

(1)
(1 1)
(1 2 1)
(1 3 3 1)
(1 4 6 4 1)
(1 5 10 10 5 1)
NIL
CHAPTER 16.
VARIABLES AND THEIR BINDING

So f a r , f o r t h e sake of convenience, I have been i n t e n t i o n a l l y vague i n d e s c r i b i n g


t h e mechanisms used by LISP t o bind v a r i a b l e s . This c h a p t e r a t t e m p t s t o c l a r i f y
what we have been doing i n t h i s regard.

A v a r i a b l e i s a symbol t h a t i s used t o r e p r e s e n t an argument of a f u n c t i o ~ i . Thus,


one might w r i t e a + b, where a = 341 and b = 216. I n t h i s s i t u a t i o n , no con-
f u s i o n can r e s u l t , and a l l w i l l a g r e e t h a t t h e answer i s 557. I n order t o a r r i v e
a t t h i s r e s u l t , i t i s necessary t o s u b s t i t u t e t h e a c t u a l numbers f o r t h e v a r i a -
b l e s , and t h e n add t h e two numbers (on an adding machine, f o r i n s t a n c e ) .

One reason why t h e r e i s no ambiguity i n t h i s c a s e i s t h a t "a" and "b" a r e not


a c c e p t a b l e i n p u t s f o r an adding machine, and i t i s t h e r e f o r e obvious t h a t they
merely r e p r e s e n t t h e a c t u a l arguments. I n LISP, t h e s i t u a t i o n can be mucln more
complicated, An atomic symbol may be e i t h e r a v a r i a b l e o r a c o n s t a n t . To
f u r t h e r complicate t h e s i t u a t i o n , an argument may be an a r b i t r a r y symbolic con-
s t a n t t h a t s u p e r f i c i a l l y looks l i k e an e x p r e s s i o n t o be e v a l u a t e d . The
i n t u i t i v e approach i s no longer adequate. I n t h e examples s o f a r , we have seen
f u n c t i o n s a p p l i e d t o s p e c i f i c arguments t o g e t s p e c i f i c r e s u l t s . We have a l s o
provided f o r a r b i t r a r y arguments by means of bound variables i n LAMBDA and PROG
expressions.

BOUND VARIABLES

Rule:

An atom i s always e v a l u a t e d . I f you wish t o suppress


e v a l u a t i o n , quote i t . Some atoms, such a s T , NIL, and
numbers i n p a r t i c u l a r , a r e permanent c o n s t a n t s .

The i m p l i c a t i o n of t h i s r u l e i s t h a t a l l non-quoted atoms a r e bound v a r i a ' b l e s ,


But what does t h i s mean? It means t h a t a l l non-quoted l i t e r a l atoms a r e con-
s i d e r e d t o be v a r i a b l e s t h a t have v a l u e s , and we say t h a t a v a l u e i s "bou,nd t o
t h e variable". This v a l u e can be any S-expression. The binding i s i n a c t u a l i t y
an a s s o c i a t i o n of d a t a i n t e r n a l t o t h e LISP system t h a t i s recognized and
manipulated by t h e LISP system when an e x p r e s s i o n i s e v a l u a t e d . To understand
bound v a r i a b l e s adequately, we must examine more c l o s e l y how t h e s e a s s o c i a t i o n s
a r e c o n s t r u c t e d i n t e r n a l t o LISP. The methods f o r binding depend upon t h e
n a t u r e of t h e LISP system. I n t e r p r e t e r s bind v a r i a b l e s d i f f e r e n t l y than do
compilers. Lambda and program v a r i a b l e s a r e examples of bound v a r i a b l e s . In
t h e expression

(LAMBDA (A B C) (PROG (X Y Z ) ... ) )


t h e lambda v a r i a b l e s A, B , C , and t h e program v a r i a b l e s , X, Y , Z a r e a l l bound
variables.

16.2 THE A-LIST

I n some i n t e r p r e t i v e LISP systems, whenever a lambda o r program e x p r e s s i o n i s


encountered, t h e v a r i a b l e s t o be bound a r e placed on t h e a s s o c i a t i o n l i s t o r
a-Zist. The a - l i s t i s a l i s t of d o t t e d p a i r s of t h e form

where u i s t h e name of a v a r i a b l e and v i s i t s v a l u e , o r binding.


i i

Lambda o r program v a r i a b l e s a r e p a i r e d w i t h t h e i r v a l u e s and t h e p a i r s a r e


appended t o t h e f r o n t ( l e f t m o s t ) end of t h e e x i s t i n g a - l i s t , w i t h o l d e r bindings
f u r t h e r back. When we e x i t from a f u n c t i o n , t h e 'bindings f o r t h a t e x p r e s s i o n
a r e removed, and t h e a - l i s t i s shortened. The bi,ndings f o r a p a r t i c u l a r ex-
pression a r e i t s context.

During form e v a l u a t i o n , a l l r e f e r e n c e s t o dummy v a r i a b l e s a r e r e f e r e n c e s t o t h e


a-list. The a - l i s t i s searched, f r o n t t o back, f o r t h e f i r s t occurrence of t h e
variable. (The f u n c t i o n ASSOC i s u s e f u l f o r t h i s purpose.) The CDR of t h e
p a i r i s then t h e v a l u e of t h e v a r i a b l e . I f a function is recursive, t h e r e w i l l
be m u l t i p l e occurrences of i t s v a r i a b l e s on t h e a - l i s t , each r e p r e s e n t i n g t h e
c o n t e x t of t h e f u n c t i o n a t some p r i o r c a l l . During e v a l u a t i o n , only t h e f i r s t
occurrence of t h e v a r i a b l e i s used, corresponding t o t h e c u r r e n t c o n t e x t of t h e
function.
16.3 FREE VARIABLES

An e x p r e s s i o n t o b e e v a l u a t e d h a s a c o n t e x t g i v e n by t h e c u r r e n t b i n d i n g s of a l l
its variables. S i n c e t h e v a l u e of t h e e x p r e s s i o n depends upon t h e c u r r e n t
b i n d i n g s , t h e v a l u e of t h e e x p r e s s i o n depends upon i t s c o n t e x t . Change t h e
c o n t e x t and you (probably) change t h e v a l u e of t h e e x p r e s s i o n .

On t h e o t h e r hand, v a r i a b l e s a r e d e f i n e d by e x p r e s s i o n s ; t h e i r scope of d e f i n i -
t i o n , i n which a g i v e n b i n d i n g may b e r e t r i e v e d , i s r e s t r i c t e d t o t h e e x p r e s s i o n
i n which t h e y were d e f i n e d . O u t s i d e t h a t scope, t h e b i n d i n g , o r even t h e
v a r i a b l e i t s e l f , does n o t e x i s t . For lambda v a r i a b l e s , t h e s c o p e i s t h e body
of t h e lambda e x p r e s s i o n . For program v a r i a b l e s , t h e s c o p e i s t h e statements
of t h e program e x p r e s s i o n .

F r e q u e n t l y , however, a n e x p r e s s i o n may r e f e r e n c e a v a r i a b l e i t h a s n o t d e f i n e d .
I f t h i s r e f e r e n c e i s t o b e meaningful, t h e v a r i a b l e must b e d e f i n e d and bound a t
some h i g h e r l e v e l such t h a t i t s scope encompasses t h e immediate e x p r e s s i o n . We
speak of such r e f e r e n c e s a s " f r e e r e f e r e n c e s " and c a l l t h e v a r i a b l e s free
variables .
Examine t h e e x p r e s s i o n

(LAMBDA ( P I J ) ((LAMBDA (R) (TIMES 2 P I R)) J ) ) (3.14159 2)

P I i s a f r e e v a r i a b l e i n t h e innermost lambda e x p r e s s i o n ,
C2*

A f t e r b i n d i n g s have been e s t a b l i s h e d by lambda c o n v e r s i o n , t h e a - l i s t l o o k s a s

-
f o l l o w s d u r i n g t h e evaluation of t h e innermost lambda e x p r e s s i o n .

--
((R

C
. 2)
2
(PI . 3.14159)
C
1
(J . 2) ... )

The c o n t e x t s f o r e x p r e s s i o n s c and c a r e noted.


1 2
When t h e form

(TIMES 2 P I R)

i s e v a l u a t e d , t h e a - l i s t i s searched f o r t h e v a l u e s of v a r i a b l e s R and P I . R
i s found i n t h e c u r r e n t c o n t e x t , l a b e l e d c 2 , b u t P I i s found a t a h i g h e r c o n t e x t
l e v e l , labeled c Thus, f r e e v a r i a b l e s a r e e v a l u a t e d by searching through t h e
1'
complete a - l i s t , independent of c o n t e x t , u n t i l t h e f i r s t occurrence of t h e
v a r i a b l e i s encountered. I f a f r e e v a r i a b l e h a s rrot been bound a t some h i g h e r
l e v e l , no a s s o c i a t i o n w i l l be found on t h e a - l i s t and an e r r o r w i l l r e s u l t .

CONSTANTS

Quoting d a t a i s one way t o r e a l i z e c o n s t a n t s i n a form. Using f r e e v a r i a b l e s i s


another method. But what i f we wish t o u s e names f o r c o n s t a n t d a t a , f o r c l a r i t y
i n programming o r f o r t h e simple, p r a c t i c a l reason t h a t t h e name may be s h o r t e r
t h a n t h e d a t a and hence e a s i e r t o w r i t e ? When a v a r i a b l e always has a c e r t a i n
v a l u e , i r r e s p e c t i v e of t h e c u r r e n t a - l i s t , we c a l l i t a constant. This i s
accomplished by means of value c e l l s a l s o c a l l e d special c e l l s .

Every l i t e r a l atom h a s a v a l u e c e l l t h a t may c o n t a i n t h e binding t o a c o n s t a n t .


For conventional LISP i n t e r p r e t i v e systems, t h e v a l u e c e l l i s on t h e atom's
p r o p e r t y l i s t ( s e e Chapter 19) under t h e i n d i c a t o r APVAL ( f o r "apply value").
For compiler-based systems, and some more r e c e n t i n t e r p r e t i v e systems, t h e v a l u e
c e l l i s p a r t of t h e atomic s t r u c t u r e of a l i t e r a l atom ( s e e Chapter 1 9 ) .

When a c o n s t a n t i s used, i t i s used a s a f r e e v a r i a b l e and t h e binding i s


r e t r i e v e d from t h e v a l u e c e l l , n o t t h e a - l i s t . The i n t e r n a l mechanism which
t e l l s LISP when t o examine t h e v a l u e c e l l and when t o s e a r c h t h e a - l i s t i s
extremely implementation-dependent and beyond t h e scope of t h i s t e x t . In fact,
on r e c e n t systems t h e r e i s no a - l i s t a t a l l , b u t some o t h e r more e f f i c i e n t
s t o r a g e mechanism. Our primary concern h e r e i s t o l e a r n how t o e s t a b l i s h
bindings i n t h e v a l u e c e l l .

Lambda conversion e s t a b l i s h e s b i n d i n g s on t h e a - l i s t (or equivalent repository


w i t h more r e c e n t systems), n o t i n t h e v a l u e c e l l . Value c e l l bindings a r e
o f t e n c a l l e d "zero-level" bindings s i n c e they a r e e s t a b l i s h e d a t t h e top-most
l e v e l of LISP. They a r e a l s o c a l l e d "global" b i n d i n g s s i n c e t h e scope of t h e
b i n d i n g s encompasses a l l l e v e l s of e x p r e s s i o n s . Zero-level b i n d i n g s can be
e s t a b l i s h e d on most systems w i t h t h e pseudo-functions CSET and CSETQ.

CSET a c t s l i k e a f u n c t i o n of two v a r i a b l e s of t h e form

(CSET vl v2)

Both v and v are e v a l u a t e d ; v must e v a l u a t e t o a l i t e r a l atom. The v a l u e of


1 2 1
v 2 i s p l a c e d i n t h e v a l u e c e l l of t h e l i t e r a l atom v a l u e of v For example,
1'

(CSET (QUOTE P I ) 3.14159)

w i l l bind t h e number 3.14159 t o t h e atom P I .

CSETQ i s l i k e CSET e x c e p t t h a t i t q u o t e s i t s f i r s t argument, e . g . ,

(CSETQ P I 3.14159)

Both CSET and CSETQ r e t u r n t h e v a l u e of v 2 .

Note t h a t CSET a t t h e t o p l e v e l i s

CSET ( P I 3.14159)

s i n c e EVALQUOTE q u o t e s i t s arguments. W e cannot u s e CSETQ a t t h e t o p l e v e l


because a double q u o t e would occur (once by EVALQUOTE, once by CSETQ i t s e l f )
for v and i t would n o t t h e n e v a l u a t e t o t h e manditory l i t e r a l atom.
1

16.5 COMPILER BINDINGS

The b i n d i n g of v a r i a b l e s on a n a - l i s t i s a d e q u a t e f o r i n t e r p r e t e r s where t h e
lambda e x p r e s s i o n i s saved and e v a l u a t e d each t i m e ; b u t i t i s i n a d e q u a t e f o r
compiled f u n c t i o n s s i n c e t h e symbolic e x p r e s s i o n s a r e t r a n s l a t e d i n t o machine
code and d i s c a r d e d . Compiled f u n c t i o n s a l s o r u n one o r two o r d e r s of magnitude
f a s t e r t h a n i n t e r p r e t e d f u n c t i o n s by e l i m i n a t i n g t h e c o s t l y a - l i s t s e a r c h e s
during evaluation.

There are two t y p e s of v a r i a b l e s i n compiled f u n c t i o n s : speciaZ v a r i a b z e s and


ordinary variables.
Accordingly, v a r i a b l e s may be bound i n one of two p l a c e s , i n t h e v a l u e c e l l ,
o r on an i n t e r n a l l a s t - i n - f i r s t - o u t (LIFO) s t a c k o r t a b l e r e f e r r e d t o a s t h e
push-down l i s t .

I f a v a r i a b l e i s an o r d i n a r y one, a s t o r a g e l o c a t i o n i s r e s e r v e d f o r i t on t h e
push-down l i s t when t h e e x p r e s s i o n i n which i t i s d e f i n e d i s e v a l u a t e d . Other
f u n c t i o n s cannot f i n d t h i s p r i v a t e c e l l , making i t impossible t o u s e i t a s a
f r e e v a r i a b l e on most systems. Ordinary v a r i a b l e s a r e lambda and program
variables, i.e., dummy v a r i a b l e s . After t h e parent expression is evaluated
and e v a l u a t i o n moves t o a h i g h e r - l e v e l e x p r e s s i o n , t h e r e s e r v e d l o c a t i o n i s
r e l e a s e d , and i t s c o n t e n t s a r e l o s t , analogous t o r e l e a s i n g c o n t e x t on an
a-list .
S p e c i a l v a r i a b l e s a r e used f o r c o n s t a n t s and f r e e v a r i a b l e s . Such v a r i a b l e s
have i n d i c a t o r s a s s o c i a t e d w i t h t h e i r names ( i n some implementation-dependent
manner) d e s i g n a t i n g them a s s p e c i a l v a r i a b l e s . When a f u n c t i o n u s e s a v a r i a b l e
f r e e l y , t h e q u a n t i t y i n t h e v a l u e c e l l i s t h e v a l u e of t h e v a r i a b l e .

Free v a r i a b l e s must be bound i n t h e v a l u e c e l l i f t h e proper binding i s t o be


retrieved during evaluation. To convey t h i s i n f o r m a t i o n t o t h e LISP compiler,
you must d e c l a r e f r e e v a r i a b l e s a s s p e c i a l c a s e s b e f o r e compilation. I n most
systems, d e c l a r a t i o n s a r e made w i t h t h e pseudo-function SPECIAL.

SPECIAL i s a pseudo-function of one argument, a l i s t of a l l t h e v a r i a b l e s used


f r e e i n t h e f u n c t i o n s being defined. Thus,

SPECIAL ( ( P I DOG))

d e c l a r e s t h e atoms P I and DOG a s s p e c i a l c a s e s ; any time P I o r DOG i s used a s a


f r e e v a r i a b l e , v a l u e s w i l l be bound and r e t r i e v e d from t h a t atom's v a l u e c e l l .
You may "un-special" any s p e c i a l v a r i a b l e w i t h t h e f u n c t i o n UNSPECIAL. UNSPECIAL
i s t h e i n v e r s e o f , but i n t h e same form a s , SPECIAL. Thus,

UNSPECIAL ( (PI DOG) )

removes t h e s p e c i a l s t a t u s from t h e atoms P I and DOG.


EXERCISES

T h i s c h a p t e r h a s no e x e r c i s e s because of t h e d i f f e r e n t t r e a t m e n t g i v e n v a r i a b l e s
by d i f f e r e n t implementations. The a r e a s of common t r e a t m e n t , s u c h a s dummy
v a r i a b l e s , have been covered by p r e v i o u s c h a p t e r s . For more i n f o r m a t i o n and
e x e r c i s e s , c o n s u l t t h e LISP e x p e r t f o r your system.
C H A P T E R 17.
DEBUGGING, INPUT-OUTPUT, AND SUPERVISORS

LISP, l i k e many c u r r e n t programming l a n g u a g e s , s u f f e r s from a l a c k of


s t a n d a r d i z a t i o n i n t h e a r e a s of debugging and i n p u t - o u t p u t . I don't wish
t o imply t h a t LISP i s weak i n t h e s e , a r e a s . On t h e c o n t r a r y , LISP systems
have e x t r e m e l y powerful debugging a i d s . T h i s c h a p t e r p r e s e n t s t h e more
common f e a t u r e s a v a i l a b l e on most s y s t e m s , though even h e r e minor d i f f e r e n c e s
i n d e f i n i t i o n s appear a c r o s s v a r i o u s implementations. I n a d d i t i o n , more
r e c e n t systems have c a p i t a l i z e d on new t e c h n o l o g i c a l advances (such a s t i m e -
s h a r i n g and i n t e r a c t i v e o n - l i n e o p e r a t i o n ) , ancl have extended t h e s e f e a t u r e s
i n i n t e r e s t i n g and e x c i t i n g ways, s o t h a t t h e r e a d e r i s encouraged t o e x p l o r e
t h e s e a r e a s w i t h h i s own system.

17.1 PROGRAM DEBUGGING

E r r o r s t h a t f a u l t a LISP program g e n e r a l l y f a l l . i n t o two broad c a t e g o r i e s :


s y n t a x e r r o r s , and s e m a n t i c e r r o r s . O f t e n , t h e symptoms of f a i l u r e f a l l i n
one c a t e g o r y , whereas t h e c a u s e of f a i l u r e stems from e r r o r s i n t h e o t h e r
category. A p e r f e c t example i s t h e p o o r l y formed c o n d i t i o n a l e x p r e s s i o n ,
which--even though p a r e n t h e s e s may b a l a n c e o u t - - i s i n c o r r e c t l y s t r u c t u r e d .

Elaine ~ord'' h a s n o t e d some problem a r e a s i n LISP debugging based on h e r


experience. From t h i s e x p e r i e n c e --and from my own--I have compiled t h e
f o l l o w i n g c h e c k l i s t s of h i g h - p r o b a b i l i t y e r r o r s o u r c e s i n LISP.

S y n t a c t i c E r r o r Sources

1. READ b a l k s b e c a u s e of a n i n s u f f i c i e n t number of r i g h t
parentheses.

2. READ b a l k s because a n S - e x p r e s s i o n b e g i n s w i t h a r i g h t
p a r e n t h e s i s - - a symptom of a n e x c e s s of r i g h t p a r e n t h e s e s
i n t h e p r i o r S-expression read.

3. READ b a l k s because a n S - e x p r e s s i o n w r i t t e n i n d o t - n o t a t i o n
h a s "too many d o t s v - - a symptom of a p o o r l y formed S -
expression.
4. READ b a l k s because of a n i l l e g a l l y s p e l l e d atom. Check t h e
l e g a l s y n t a x of numbers and l i t e r a l atoms allowed by your
system. Watch o u t f o r t h e c l a s s i c e r r o r of i n t e r c h a n g i n g
t h e numbers z e r o and one, and t h e l e t t e r s "0" and 'rI".
Also, i f t h e S - e x p r e s s i o n b e i n g r e a d was o r i g i n a l l y produced
by a LISP p r i n t o u t on c a r d , t a p e , o r d i s c , any u n u s u a l l y
s p e l l e d l i t e r a l atoms p r e v i o u s l y e n t e r e d a s s t r i n g s ( s e e
Paragraph 17.6) have p r o b a b l y been s t r i p p e d of t h e s t r i n g
q u o t i n g a p p a r a t u s and p r i n t e d a s a l i t e r a l s t r i n g . You
cannot re-read such s t r i n g s without r e s t o r i n g t h e s t r i n g
quoting apparatus.

5. A r u n - t i m e e r r o r o c c u r s due t o p o o r l y s t r u c t u r e d forms. Check


your p a r e n t h e s e s a c c o r d i n g t o t h e l e g a l s y n t a x f o r LPiMBDA and
PROG e x p r e s s i o n s , f o r COND and SELECT, f o r DEFINE, and o t h e r
s p e c i a l forms.

6. A r u n - t i m e e r r o r o c c u r s due t o p o o r l y d e l i m i t e d atoms. The


c h a r a c t e r s b l a n k , comma, l e f t and r i g h t p a r e n t h e s i s always
t e r m i n a t e t h e name of a n atom ( o t h e r t e r m i n a t o r s may a l s o e x i t
on given systems). Check t h a t a l l atoms have been p r o p e r l y
delimited. P a r t i c u l a r l y , check t h e l o c a l system c o n v e n t i o n s
f o r atom composition a c r o s s c a r d (or l i n e ) b o u n d a r i e s .

Semantic E r r o r Sources

1. Check s y n t a x e r r o r p o s s i b i l i t i e s .

2. Undefined f u n c t i o n s may n o t have been d e f i n e d t h r o u g h o v e r -


sight. Otherwise, check your s p e l l i n g and t h e proper system
s p e l l i n g f o r t h e f u n c t i o n you want.

3. Watch o u t f o r d u p l i c a t e names. You may have d u p l i c a t e names


f o r dummy v a r i a b l e s ( i f t h e y a r e i n d i f f e r e n t LAMBDA o r PROG
f o r m s ) , b u t n o t f o r CSET v a r i a b l e s o r f u n c t i o n s . Beware
p a r t i c u l a r l y of names used i n t e r n a l l y by t h e system. Check
w i t h your l o c a l LISP e x p e r t f o r a l l system names (both
v a r i a b l e s and f u n c t i o n s ) t h a t may c l a s h w i t h u s e r names.

4. Lambda conversion e r r o r s r e s u l t when a n improper number of


arguments a r e s u p p l i e d t o t h e f u n c t i o n . Furthermore, t h e
o r d e r of t h e arguments may be i n e r r o r .

5. Wrong arguments cause e r r o r s . Check t h a t t h e arguments a r e


of t h e proper t y p e and form. I f t h e f u n c t i o n e x p e c t s numbers,
don 't give i t l i s t s . Also, f u n c t i o n s t h a t use CAAR, CADR,
e t c . , presuppose a l i s t c o n t a i n i n g l i s t s a s elements.

6. F a m i l i a r i z e y o u r s e l f w i t h t h e d i f f e r e n c e s between CONS, LIST,


APPEND, and o t h e r l i s t - m a k i n g f u n c t i o n s . You may have s e l e c t e d
t h e wrong f u n c t i o n and not produced t h e d e s i r e d v a l u e .

7. Watch your p r e d i c a t e s i n c o n d i t i o n a l e x p r e s s i o n s . Are they


t h e n e g a t i o n of what you r e a l l y want? Is the conditional
e x p r e s s i o n checking f o r a l l c a s e s , and i n t h e proper o r d e r ?

8. Terminating c o n d i t i o n s of r e c u r s i v e f u n c t i o n s a r e v e r y
important. Check t h a t you have t h e r i g h t c o n d i t i o n s , t h e
proper number of them, and have s i t u a t e d them i n t h e proper
place i n the recursive function definition.

DIAGNOSTIC TOOLS

LISP p r o v i d e s a number of u s e r - o r i e n t e d t o o l s f o r d i a g n o s i s of run-time e r r o r s ,


b e s i d e s t h e normal system d i a g n o s t i c s . System-detected e r r o r s u s u a l l y y i e l d
an e r r o r message r e l a t e d t o t h e immediate cause of f a i l u r e , followed by a
s h o r t l i s t i n g of t h e program flow l e a d i n g t o t h e immediate t r a p p e d f u n c t i o n .
The l i s t i n g i s c a l l e d a "backtrace", and u s u a l l y c o n s i s t s of t h e f u n c t i o n
names of t h e program h i e r a r c h y involved i n t h e e r r o r . This information i s
g a t h e r e d by t h e system a s i t "unwinds" t h e p r o g r ~ mh i e r a r c h y b u i l t up d u r i n g
evaluation, so a s t o reach the top level again. Some systems may a l s o l i s t
t h e c a l l i n g parameters of each f u n c t i o n a t t h e moment of e r r o r , and may even
a l l o w c o n d i t i o n a l u s e r c o n t r o l of t h e unwinding p r o c e s s .
Some s t a n d a r d LISP d i a g n o s t i c t o o l s a r e l i s t e d below:

TRACE (L) TRACE has one argument, L , a l i s t of f u n c t i o n s t o be


traced. A f t e r TRACE h a s been e v a l u a t e d , t h e arguments,
v a l u e s , and name of each f u n c t i o n a r e p r i n t e d each t i m e
t h e f u n c t i o n i s e n t e r e d and e x i t e d .

UNTRACE (L) UN'EACE h a s one argument, L, a l i s t of f u n c t i o n s c u r r e n t l y


being traced. UNTRACE removes t h e t r a c i n g from a l l
f u n c t i o n s i n l i s t L.

GENSYM ( ) GENSYM h a s no arguments. I t s v a l u e i s a new, d i s t i n c t ,


and f r e s h l y c r e a t e d l i t e r a l atom w i t h a p r i n t name (on
most systems) of t h e form G00001, G00002, ..., G99999.
T h i s f u n c t i o n i s u s e f u l i n debugging and g e n e r a l LISP
programming f o r c r e a t i n g a l i t e r a l atom when one i s
needed; each one i s g u a r a n t e e d t o b e unique. GENSYM
names--if p r i n t e d on c a r d s , t a p e , o r d i s c and s u b s e q u e n t l y
r e a d back i n t o t h e s y s t e m - - w i l l n o t correspond t o a n
i n t e r n a l , e x i s t i n g GENSYM name. This i s necessary t o
guarantee t h e i r uniqueness.

Other n o n - s t a n d a r d d i a g n o s t i c t o o l s , t h a t a l l o w even g r e a t e r c o n t r o l of
program e x e c u t i o n a r e u s u a l l y p r e s e n t i n a given LISP system. These too:Ls
i n c l u d e u s e r a c c e s s t o t h e e r r o r message and unwind c o n t r o l ; c o n d i t i o n a l
t r a p s on CONS u s a g e ; and v a r i o u s b r e a k p o i n t t r a p s and t r a c e s . The l i t e r a t u r e
f o r your system probably e x p l a i n s t h e i r o p e r a t i o n and should b e c o n s u l t e d .

17.3 INPUT -OUTPUT

I n p u t and o u t p u t i n LISP a r e handled p r i n c i p a l l y by t h e two p s e u d o - f u n c t i o n s


READ and PRINT, which r e a d and p r i n t one S - e x p r e s s i o n , r e s p e c t i v e l y . These
f u n c t i o n s a r e , i n t u r n , d e f i n e d i n terms of more p r i m i t i v e machine-dependent
functions. I have p u t t o g e t h e r i n t h i s c h a p t e r a s e t of b a s i c i n p u t - o u t p u t
f u n c t i o n s t h a t a r e r e p r e s e n t a t i v e of t h o s e found on most s y s t e m s , even i f
t h e i r names and o p e r a t i o n s a r e n o t e x a c t l y t h o s e of your system.
READ ( READ i s a pseudo-function of no arguments. I t s evaluation
causes t h e next S-expression t o be r e a d from t h e i n p u t
device and s t r u c t u r e d i n t e r n a l l y a s a LISP S-expression.
A l l l i t e r a l atoms seen by READ, which a r e a l r e a d y s t r u c t u r e d
i n t e r n a l l y , a r e references t o those s t r u c t u r e s . A l l new
atoms (ones not seen b e f o r e ) cause new i n t e r n a l s t r u c t u r e s
t o be c r e a t e d a s t h e i r r e f e r e n c e . READ r e t u r n s a s i t s
v a l u e t h e S-expression r e a d . (The v a l u e , l i k e a l l LISP
v a l u e s , i s a p o i n t e r t o t h e i n t e r n a l r e p r e s e n t a t i o n of
t h e S-expression.)

PRINT (S) PRINT i s a pseudo-function of one argument, an S-expression.


I t s e v a l u a t i o n causes t h a t S-expression t o be p r i n t e d on
t h e output device and t h e argument t o be r e t u r n e d a s i t s
value. Since t h e argument seen by PRINT i s a p o i n t e r t o
t h e i n t e r n a l r e p r e s e n t a t i o n of t h e S-expression, PRINT
computes a " p r i n t name" f o r t h e S-expression based upon
t h e p r i n t names of a l l atoms in t h e S-expression and upon
t h e s t r u c t u r e of t h e S-expression. The p r i n t name of t h e
S-expression i s e x a c t l y t h e l i s t n o t a t i o n used throughout
this text. A d e f i n i t i o n of PRINT i s given below. It
u s e s p r i m i t i v e s d i s c u s s e d i n t h e n e x t paragraph.

(PRINT (LAMBDA (X) (PROG ( )


(PRIN X)
(TERPRI)
(RETURN X) ))I
(PRIN (LAMBDA (X) (PROG (J)
(COND ( (ATOM X) (GO D) ) )
(SETQ J X)
(PRIN1 LPAR)
A (PRIN (CAR J ) )
(COND ( (NULL (CDR J ) ) (GO C)) )
(PRIN1 BLANK)
(COND ( (ATOM (CDR J ) ) (GO B) ))
(SETQ J (CDR J))
(GO A)
B (PRIN1 PERIOD)
(PRIN1 BLANK)
(PRINI (CDR J ) )
C (PRIN1 RPAR)
(RETURN X)
D (PRIN1 X)
(RETURN X) 1)

The d e f i n i t i o n of PRIN above shows t h a t PRIN i s t h e work h o r s e of PRINT. It


does t h e r e c u r s i v e computation f o r p u t t i n g atom p r i n t names, l e f t and r i g h t
p a r e n t h e s e s , and into the print line. PRIN c a l l s upon t h e primiti.ve
PRINl t o do t h e a c t u a l p r i n t l i n e m a n i p u l a t i o n s .

17.4 INPUT -OUTPUT PRIMITIVES

Most LISP systems use b u f f e r e d 110. The p r i m i t i v e s l i s t e d h e r e a r e r e s p o n s i b l e


f o r much of t h e i n t e r n a l 110 b u f f e r management. The r e a d l i n e and p r i n t l i n e
correspond t o t h e i n t e r n a l b u f f e r s .

RATOM ( ) RATOM i s a pseudo-function of no arguments used by READ.


I t s e v a l u a t i o n causes t h e r e a d l i n e t o be examined and
t h e c h a r a c t e r s t h e r e i n t o be parsed i n t o a n atom. If
t h e s t r i n g of c h a r a c t e r s corresponds t o number s y n t a x , a
numerical atom i s formed. I f t h e c h a r a c t e r s t r i n g obeys
l i t e r a l atom s y n t a x , a l i t e r a l atom i s formed. I f neither,
an e r r o r r e s u l t s .

The v a l u e of RATOM i s t h e atom r e a d ( i . e . , the pointer t o


t h e i n t e r n a l atomic s t r u c t u r e ) and t h e i n t e r n a l b u f f e r
c o n t r o l s a r e advanced over t h e atom r e a d . ( I n some systems,
RATOM i s s u f f i c i e n t l y f l e x i b l e t o allow knowledgeable u s e r s
t o s p e c i f y t h e s y n t a x of atoms and t h e r e b y c o n t r o l t h e
parsing.)
READCH ( ) READCH i s a n i n v e n t i o n f o r t h i s t e x t , b u t corresponds t o
a pseudo - f u n c t i o n of no arguments a v a i l a b l e on most systems.
(Some systems a c h i e v e t h e e f f e c t of READCH by f l e x i b l e
c o n t r o l over RATOM.) I t s e v a l u a t i o n causes t h e next
c h a r a c t e r i n t h e r e a d l i n e t o be r e t u r n e d a s a l i t e r a l
atom. No syntax check i s made, hence a l l s i n g l e c h a r a c t e r s
a r e a c c e p t a b l e l i t e r a l atoms.

TEREAD ( ) TEREAD , meaning 'TERminate READ", i s a pseudo -f unct i o n of


no arguments t h a t c l e a r s t h e r e a d l i n e r e g a r d l e s s of i t s
current contents. The v a l u e of TEREAD i s NIL.

PRINl (A) PRINl i s a pseudo-function of one argument, an atom. Its


e v a l u a t i o n causes t h e i n t e r n a l l y s t o r e d p r i n t name of t h e
argument t o be e n t e r e d i n t o t h e p r i n t l i n e . The i n t e r n a l
b u f f e r c o n t r o l s a r e advanced j u s t beyond t h e atom p r i n t e d .
The c o n t e n t s of t h e b u f f e r a r e not normally t r a n s f e r r e d t o
t h e o u t p u t d e v i c e , except when t h e b u f f e r overflows. The
argument A must e v a l u a t e t o an atom which i s t h e v a l u e of
PRINl .
TERPRI ( ) TERPRI, meaning "inate PRInt l i n e " , i s a pseduo-
f u n c t i o n of no arguments used by PRINT t h a t dumps t h e
c u r r e n t p r i n t l i n e on t h e e x t e r n a l o u t p u t d e v i c e and t h e n
clears the print line. A blank l i n e i s printed i f t h e
print line is clear. The v a l u e of TERPRI i s NIL.

17.5 THE OBLIST

RATOM i s a machine-language r o u t i n e t h a t c o n v e r t s c h a r a c t e r s t r i n g s i n t o
i n t e r n a l atomic s t r u c t u r e . When a non-numeric c h a r a c t e r s t r i n g i s r e a d , i t
must be compared w i t h t h e c h a r a c t e r r e p r e s e n t a t i o n of a l l l i t e r a l atoms seen
s o f a r , t o determine whether t h i s s t r i n g i s a new atom o r a r e f e r e n c e t o one
seen b e f o r e .
There must b e r a p i d a c c e s s t o a l l t h e atoms i n the system. There e x i s t s , t h e r e -
f o r e , a l i s t c a l l e d t h e o b j e c t list, o r OBLIST, of a l l l i t e r a l atoms. To speed
up t h e s e a r c h f o r comparisons, t h e OBLIST i s u s u a l l y o r g a n i z e d as a l i s t of a
hundred o r s o s u b l i s t s o r "buckets". The atoms a r e d i s t r i b u t e d among t h e
b u c k e t s by a computation upon t h e i r H o l l e r i t h p r i n t names (hash c o d i n g ) , which
y i e l d s a r e a s o n a b l y uniform and random d i s t r i b u t i o n of atoms among t h e b u c k e t s .

Thus, l i t e r a l atoms i n a l l LISP systems a r e u n i q u e , and have unique l o c a t i o n s


( a d d r e s s e s ) i n t h e system. I n some more r e c e n t systems, s m a l l numbers a r e
u n i q u e l y r e p r e s e n t e d i n t e r n a l l y by t h e a d d r e s s r e s u l t i n g from adding a c o n s t a n t
displacement t o t h e i r numerical values. The u n i q u e n e s s of atoms p e r m i t s r a p i d
t e s t i n g f o r e q u i v a l e n t s t r u c t u r e s by EQ and EQUAL.

UNUSUALLY SPELLED LITERAL ATOMS

The s y n t a x f o r l i t e r a l atoms g i v e n i n Chapter 2 n o t e s t h a t a l i t e r a l atom i s


any sequence of l e t t e r s and d i g i t s , where t h e f i r s t c h a r a c t e r must b e a l e t t e r .
W e speak of such atoms a s b e i n g " p r o p e r l y s p e l l e d " , and now a p p r e c i a t e t h e a t
t h i s s p e l l i n g i s t h e normal s y n t a x a c c e p t e d by RATOM.

O f t e n , however, w i t h extended c h a r a c t e r s e t s we w i s h t o compose l i t e r a l atoms


o u t of o t h e r t h a n c a p i t a l l e t t e r s and d i g i t s . Such atoms a r e s a i d t o b e
"unusually spelled". To u s e them, w i t h o u t modifying RATOM, we must u s e a s t r i n g
q u o t i n g convention. H i s t o r i c a l l y , t h i s convention i s c a l l e d t h e $$-artifact.
We s h a l l u s e and e x p l a i n i t h e r e , even though more s t r e a m l i n e d s t r i n g q u o t i n g
a p p a r a t u s e x i s t s on many c u r r e n t systems.

Any c h a r a c t e r s t r i n g preceded by $$ w i l l a l e r t RATOM t o t h e o c c u r r e n c e of a n


u n u s u a l l y s p e l l e d l i t e r a l atom. RATOM t r e a t s t h e c h a r a c t e r immediately follow-
i n g t h e $$ a s a "bracket" and t h e n b u i l d s a l i t e r a l atom o u t of a l l c h a r a c t e r s
p r e c e d i n g t h e n e x t o c c u r r e n c e of t h e b r a c k e t c h a r a c t e r . The b r a c k e t c h a r a c t e r
may b e any c h a r a c t e r . A l l c h a r a c t e r s between t h e "ad hoc" b r a c k e t s w i l l b e
t a k e n a s t h e p r i n t name f o r a l i t e r a l atom. Note t h a t i f t h e c h a r a c t e r s a r e
d i g i t s , no c o n v e r s i o n t a k e s p l a c e and t h e atom i s l i t e r a l , n o t numeric; i . . e . ,

EQUAL ($$*123* 123) = NIL


Some examples a r e shown below.

$$-Artifact Atom Formed Bracket Character

1. $$*NOW I S THE: TIME* NOW I S THE TIME *


2- $$$123$ 123 ( i n H o l l e r i t h $
not binary)
3. $$*ATOM* ATOM *
4. $$BB space B
5. $ $ . ( ( a (( .
6. $$(.-( .. (

I n example 3 above, $$*ATOM* i s i n t e r n a l l y e q u i v a l e n t t o t h e atom, ATOM. Thusy


b i n d i n g s f o r $$*ATOM* a r e b i n d i n g s f o r ATOM; e . g . ,

CSET ($$*ATOM* 123) = 123


(LAMBDA NIL ATOM) ( ) = 123
EQUAL ($$*ATOM* ATOM) = T

17.7 CHARACTER OBJECTS

It i s assumed h e r e t h a t every s i n g l e c h a r a c t e r h a s a corresponding one-character


l i t e r a l atom a l r e a d y i n t h e system. Thus, c h a r a c t e r "A" corresponds t o t h e
l i t e r a l atom "A"; character "/'I t o t h e l i t e r a l atom "/"; and c h a r a c t e r "7" t o
t h e l i t e r a l atom "7". However, RATOM does n o t always r e a d c h a r a c t e r s a s l i t e r a l
atoms. It obeys t h e s y n t a x of atoms t h a t s a y s l i t e r a l atoms b e g i n w i t h a l e t t e r ,
etc. Thus, t h e c h a r a c t e r "7", i f i t were surrounded by b l a n k s , would be read
by RATOM o r READ a s a number, n o t a s a l i t e r a l atom. READCHy however, would
read "7" a s a l i t e r a l atom. For RATOM o r READ t o read "7" a s a l i t e r a l atom,
t h e $ $ - a r t i f a c t must be employed; e . g . , $$*7*.

An a l t e r n a t i v e t o t h e $ $ - a r t i f a c t is t h e u s e of character objects. A character


o b j e c t i s a l i t e r a l atom, t h e v a l u e of which i s a n o t h e r l i t e r a l atom w i t h a
s p e c i a l p r i n t name, t h a t p r i n t name being some s p e c i a l c h a r a c t e r . The
c h a r a c t e r o b j e c t ' s p r i n t name is t h e E n g l i s h name f o r t h a t c h a r a c t e r . The
c o l l e c t i o n of c h a r a c t e r o b j e c t s a v a i l a b l e depends upon t h e LISP implementation;
however, t h e f o l l o w i n g a r e t y p i c a l examples:
Character Ob-je c t P r i n t Name of Value

LPAR c
WAR 1
BLANK space
PERIOD
SLASH
EQSIGN
UPARROW
DOLLAR
STAR
PLUSS
DASH

The u s e r may d e f i n e any o t h e r s he may choose. For example,

CSET (COMMA s$*,*)~

binds t h e l i t e r a l atom $$*,* a s the value (i.e., t h e z e r o - l e v e l binding) of


t h e l i t e r a l atom COMMA. Then t h e v a l u e of COMMA w i l l p r i n t a s t h e c h a r a c t e r 'I,".

SUPERVISORS

I n Paragraph 9 . 3 we examined t h e a c t i o n of EVALQUOTE without being a b l e a t t h a t


time t o examine i t s d e f i n i t i o n . We have now acquired a l l t h e necessary knowledge
t o d e f i n e EVALQUOTE. This d e f i n i t i o n w i l l work on a l l LISP systems, i f t h e
system can d u p l i c a t e t h e f u n c t i o n EVAL a s d e f i n e d i n t h i s t e x t . In practice,
EVALQUOTE r e q u i r e s more system housekeeping than i s shown h e r e ; t o g i v e a t t e n -
t i o n t o system housekeeping chores, however, would n o t be germane t o our
discussion.

'some LISP systems permit c o n s t a n t s such as t h e s e t o be d e f i n e d by SET


r a t h e r t h a n , o r i n a d d i t i o n t o , CSET.
(EVALQUOTE (LAMBDA ( ) (PROG (S1 S2 ARGS)
A (TEREAD)
(SETQ ARGS NIL)
(SETQ S1 (READ))
(SETQ S2 (READ))
B (COND ((NULLS2) (GOC)))
(SETQ ARGS (CONS (LIST (QUOTE QUOTE) (CAR S2)) ARGS))
(SETQ S2 (CDR S2))
(GO B)
C (PRINT (EVAL (CONS S1 (REVERSE ARGS))))
(GO A) 1))
T h i s f u n c t i o n h a s no arguments and no v a l u e . It p i c k s up i t s parameters and
r e t u r n s i t s v a l u e by doing e x p l i c i t 110 w i t h READ and PRINT. The program v a r i a -
b l e s S1 and S2 a r e bound t o t h e v a l u e s of t h e two READ c a l l s . These v a l u e s a r e
exactly the s and s2 p a i r s f o r EVALQUOTE d e s c r i b e d e a r l i e r i n t h e t e x t . For
1
example,

--
FOO (A B C)

The b i n d i n g of ARGS ( j u s t below s t a t e m e n t B) i s t h e key t o t h e program. It


b u i l d s a l i s t i n which each element of S2 i s quoted. For t h e above example,
it builds t h e l i s t

( (QUOTE C) (QUOTE B) (QUOTE A) )

adding one quoted element f o r each loop i t e r a t i o n . The i t e r a t i o n i s terminated


by t h e COND when t h e r e p e a t e d CDR of S2 r e s u l t s i n S2 being NIL. A t that point
we t r a n s f e r t o s t a t e m e n t C and e v a l u a t e a computed form. The computed form f o r
t h e example above i s

(FOO (QUOTE A) (QUOTE B) (QUOTE C))

Note how t h e form i s computed. The l i s t bound t o ARGS i s r e v e r s e d t o p r o p e r l y


o r d e r t h e quoted arguments. Then t h e v a l u e of S1, t h e f u n c t i o n t o b e a p p l i e d ,
i s i n s e r t e d a t t h e head of t h e r e v e r s e d l i s t by CONS. This computed form i s
e v a l u a t e d by EVAL, t h e v a l u e i s p r i n t e d by PRINT, and c o n t r o l t r a n s f e r s t o
s t a t e m e n t A.
-131-
A t statement A we do a TEREAD t o c l e a r t h e read l i n e and r e - i n i t i a l i z e ARGS.
It i s t h i s i n i t i a l TEREAD t h a t permits u s t o e n t e r a t t h e t o p l e v e l many r:ight
parentheses t o balance t h e EVALQUOTE p a i r without counting. It a l s o accounts
f o r t h e i n a b i l i t y t o e v a l u a t e more than one EVALQUOTE p a i r per l i n e .

The program d e f i n i t i o n appears t o be i n e r r o r because i t w i l l loop continuously.


It is not an e r r o r , a s we wish t h e s u p e r v i s o r t o loop continuously, e v a l u a t i n g
one EVALQUOTE p a i r f o r each i t e r a t i o n . That i s what t h e s u p e r v i s o r y program i s
supposed t o do.

Another kind of s u p e r v i s o r n o t normally found a t t h e t o p l e v e l of most LISP


systems i s one t h a t w i l l e v a l u a t e forms. Using EVAL, SUP3 w i l l do j u s t t h a t .

DEFINE ( (
(SUP3 (LAMBDA ( ) (PROG ( )
TAG1 (TEREAD)
(PRINT (EVAL (READ) ) )
(GO TAGl)))) 1)

We can now comprehend some s t r a i g h t f o r w a r d but unexpected t o p - l e v e l phenomena


caused by t h e o p e r a t i o n of EVALQUOTE, t h e t o p - l e v e l s u p e r v i s o r .

1. Composition of forms cannot be used d i r e c t l y a t t h e top l e v e l except


w i t h i n a lambda expression. For example, i f we w r i t e

(CAR (QUOTE (A B C ) ) )

we do not have a p a i r of S-expressions f o r EVALQUOTE. We could


e v a l u a t e t h i s form w i t h our SUP3 s u p e r v i s o r , however.

2. Round v a r i a b l e s a r e never evaluated a t t h e top l e v e l except w i t h i n a


lambda expression, because a l l arguments a r e quoted, e.g.,

CSET ( P I 3.14159)
CAR ( ( P I ) ) = P I

The r e s u l t i s P I , n o t 3.14159, s i n c e t h e argument of CAR r e a l l y i s


(QUOTE ( P I ) ) . But
(LAMBDA NIL P I ) NIL = 3.14159

s i n c e h e r e P I i s used f r e e and w i l l b e e v a l u a t e d . T h i s i s why top-


l e v e l lambda e x p r e s s i o n s a r e s o i m p o r t a n t .

3. I f more t h a n one p a i r of S - e x p r e s s i o n s is p r e s e n t e d t o EVALQUOTE, o n l y


t h e f i r s t p a i r of S-expressions w i l l b e e v a l u a t e d by some s y s t e m s ; e . g . ,

CAR ((A B C)) CDR ((A B C))

y i e l d s A. The CDR e x p r e s s i o n might n o t be s e e n by EVALQUOTE.

For systems t h a t behave t h i s way, t h e u s e r may end a t o p - l e v e l expres-


s i o n w i t h more r i g h t p a r e n t h e s e s t h a n a r e n e c e s s a r y , as EVALQUOTE s t o p s
r e a d i n g a s soon as t h e p a r e n t h e s e s count o u t c o r r e c t l y i n t h e second
argument. For example,

4. If less t h a n one p a i r of S - e x p r e s s i o n s i s g i v e n t o EVALQUOTE, i t w i l l


demand more i n p u t . T h i s i s a u s e f u l debugging t o o l and u s u a l l y means
one o r more r i g h t p a r e n t h e s e s are m i s s i n g i n t h e e n t e r e d p a i r of
expressions.

5. An e x p r e s s i o n e v a l u a t e d a t t h e t o p l e v e l t h a t e x p l i c i t l y PRINTS i t s
v a l u e may have t h e v a l u e 05 t h e e x p r e s s i o n o u t p u t twice--once by t h e
e x p l i c i t c a l l t o PRINT, and once by EVALQUOTE, which always p r i n t $ t h e
v a l u e of t h e e x p r e s s i o n , e . g . ,

PRINT (ABCD)

yields

ABCD
ABCD
17.9 EXERCISES

Some o f these exercises r e q u i r e an i n t e r a c t i v e L I S P i n w h i c h the u s e r i n o n -


l i n e w i t h the computer.

E v a l u a t e the f o l l o w i n g i n o r d e r :

1. PRINT ( ( L I S T ) )
2. TERPRI N I L
TERPRI NIL
3. (LAMBDA (X Y) (PROG ( )
( P R I N 1 X) ( P R I N 1 BLANK) ( P R I N 1 Y) (TERPRI))) (ATOM1 ATOM2)
4. READ N I L
then enter:
(NOW HEAR THIS)
5. (LAMBDA (J) (CONS (READ) J ) ) ((ANYTHING))
then enter:
(INPUT)
6. (LAMBDA NIL (PROG ( P I R)
(SETQ P I 3 . 1 4 1 5 9 )
TAG (SETQ R (READ))
(COND ((EQUAL (QUOTE END) R) (RETURN R ) ) )
(PRINT (TIMES 2 P I R ) )
(GO TAG))) N I L
E n t e r a n u m b e r f o r R. T h e p r o g r a m r e t u r n s the c o m p u t a t i o n of
2 * PI * R a n d then r e a d s a g a i n .

Y o u can s t o p t h e l o o p by e n t e r i n g

END

7. (LAMBDA ( ) (LIST LPAR RPAR BLANK PERIOD SLASH EQSLGN DOLLAJ3 STAR
(QUOTE $$* NOW HEAR T H I S *) (QUOTE $$+ - 5 3 3 . 1 7 + ) ) ) (
8. CDR ((A B C ) ) CDR ((1 2 3 ) ) entered on one l i n e .
9. 1. CSET(PERCENT $$*%*)
2. (LAMBDA ( ) PERCENT) (. )
3. (LAMBDA (J) J ) (PERCENT)
10. CAR ((A B C ) ) ) ) ) ) ) ) ) ) ) ) ) )
11. D e f i n e EVALQUOTE g i v e n i n the t e x t a s SUP2 t o a v o i d a n a m e clash
w i t h a p o s s i b l e EVALQUOTE i n y o u r s y s t e m . T r y SUP2 w i t h these cases:

sup20 ------ - - - - -to s t a r t SUP2 l o o p i n g


1. CAR ((A B C ) )
2. CDR ((A B C ) )
3. CONS (A B)
4. CSET ( P I 3 . 1 4 1 5 9 )
5. (LAMBDA () P I ) ( )

12. D e f i n e SUP3 as g i v e n e a r l i e r and t r y i t w i t h these cases:

sup30
1. (CAR ( L I S T (QUOTE ( A ) ) ) )
2. (CONS (QUOTE A) (QUOTE B ) )
3. (CSETQ K 3 . 1 4 1 5 9 )
4. (CONS K N I L )
5. (PROG (X) (PRIN1 (QUOTE X))
( P R I N ~ $$* *) (PRINI (QUOTE SQUARE)) (TERPRI) (TERPRI)
(SETQ x O)
TAGl (COND ((EQUAL X 10) (RETURN (QUOTE END))))
( P R I N 1 X) ( P R I N 1 $$* *) (PRIN1 (TIMES X X ) ) (TERPRI)
(SETQ X (ADD1 X ) )
(GO TAGl))

13. D e f i n e SUP4, a s u p e r v i s o r t h a t reads S - e x p r e s s i o n p a i r s i n reverse


order f r o m t h a t a c c e p t e d by EVALQUOTE, i . e . , s f o l l o w e d by sl.
2
T r y these p a i r s :

1. ((A B C ) ) CAR
2. ((A B C ) ) CDR
3. (A B) EQ
4. (1 2 3 4 ) PLUS
5. (K 3.14159) CSET
6. NIL (LAMBDA () K)

14. Define SUP5, a s u p e r v i s o r t h a t e v a l u a t e s p a i r s l i k e SUP2, b u t a l s o :

1. Saves t h e symbolic p a i r s .
2. P r i n t s t h e p a i r f o r i n s p e c t i o n a f t e r i n p u t , l i k e a n echo.
3. Queries your acceptance o r r e j e c t i o n of t h e p r i n t e d p a i r .
4. I f you answer NO, i t loops f o r a n o t h e r p a i r .
5. I f you answer YES, SUP5 p r i n t s t h e p a i r , followed by a n e q u a l
s i g n , followed by t h e v a l u e of t h e p a i r , and t h e n loops f o r
another p a i r .

15. Write a program t h a t p r i n t s a t a b l e of t h e following v a l u e s f o r a


range of X s p e c i f i e d a t program r u n time.

X XSQUARE SQRTX RECIPX FACTORIALX


CHAPTER 18.
FUNCTIONAL ARGUMENTS

Mathematically, it is possible to have functions as arguments of other functions.


For example, in arithmetic one could define a function

OPERATE (op a b)

where op is a functional argument that specifies which arithmetic operation is


to be performed on a and b. Thus,

OPERATE (+ 3 4) = 7
OPERATE (* 3 4) = 12

In LISP, functional arguments are extremely useful and further expand the class
of LISP functions. We call the class of functions that take this type of
argument funct<onaZs.

18.1 FUNCTION

When arguments are transmitted to a function, they are always evaluated, except
when they are transmitted to a special form which controls how its arguments are
evaluated. When we use functions or functional expressions as arguments, we
wish to transmit these arguments unevaluated. The special form FUNCTION is used
for this purpose in LISP. FUNCTION acts very much like QUOTE, and in fact in
some LISP systems, FUNCTION and QUOTE are often interchangeable. FUNCTION is
used with functional arguments to signal that a function is being transmitted as
an argument.

FUNCTION is a special form that takes one argument, a function name or a lambda
expression. It has the form

(FUNCTION fexp)

where fexp is either the name of a function, or is a lambda expression.

We can see the application of functionals by examining a particularly powerful


class of functions prefixed with the name M A P .
These functions are generally
alike, in that they all apply a functional argument to a list.
18.2 MAP

MAP i s a f u n c t i o n of two arguments of t h e form

where t h e f i r s t argument, X, must be a l i s t , and t h e second argument, FN, must


be a f u n c t i o n of one argument. MAP a p p l i e s t h e f u n c t i o n FN t o l i s t X and t o
s u c c e s s i v e CDR segments of t h e l i s t X, u n t i l X i s reduced t o a s i n g l e atom
( u s u a l l y N I L ) which i s r e t u r n e d a s t h e v a l u e of MAP. MAP i s d e f i n e d by

DEFINE ( (
(MAP (LAMBDA (X FN) (PROG ( )
TAG1 (COND ((ATOM X) (RETURN X) ) )
(FN X)
(SETQ X (CDR X))
(GO TAG11 1)) 1)

Example:

(LAMBDA (L) (MAP L (FUNCTION PRINT) ) ) ((THIS I S (A L I S T ) ) )

yields

(THIS IS (A LIST))
( I S (A L I S T ) )
((A L I S T ) )
NIL

I n t h i s example, PRINT i s t h e f u n c t i o n a l argument. Each l i n e of o u t p u t repre-


s e n t s t h e a p p l i c a t i o n of PRINT t o s u c c e s s i v e CDR segments of t h e l i s t (THIS I S
(A L I S T ) ) . The f i n a l N I L i s t h e v a l u e of MAP.

18.3 MAPLIST

MAPLIST i s a f u n c t i o n t h a t performs almost e x a c t l y a s does MAP, except PUPLIST


r e t u r n s a s i t s v a l u e a l i s t of t h e v a l u e s of th.e r e p e a t e d e v a l u a t i o n of FN
applied t o X
MAPLIST is a function of two arguments of the form

MAPLIST (X FN)

where the first argument, X, must be a list, and the second argument, FN, must
be a function of one argument. The value of MAPLIST is a new list of the values
of FN applied to the successive CDR segments of list X. That is, the value of
MAPLIST (X FN) can be expressed as the value of t'he form

A definition for MAPLIST can be given as

DEFINE ( (
(MAPLIST (LAMBDA (X FN)
(COND ((NULL X) NIL)
(T (CONS (FN X) (MAPLIST (CDR X) FN) ) ) ) ) ) ) )

Examples :

DEFINE ( (
(SQUARECAR (LAMBDA (X) (TIMES (CAR X) (CAR X) ) ) ) ) )

(LAMBDA (J) (MAPLIST J (FUNCTION SQUARECAR))) ((1 2 3 4 5)) = (1 4 9 16 25)

(LAMBDA (J) (MAPLIST J (FUNCTION CDR)) ) ((A B C)) = ((B C ) (C) NIL)

In these examples, SQUARECAR and CDR are functional arguments.

The function MAPCAR is a function like MAPLIST, in that it lists the values of
functional argument FN successively applied to each element of list X. It
differs from MAPLIST, in that it applies FN to each element of the list X; i.e.,
the CAR of what FN is applied to in MAPLIST. MAPCAR is defined by
DEFINE ((
( W C A R (LAMBDA (X FN)
(COND ((NULL X) NIL)
(T (CONS (FN (CAR X) ) (MAPCAR (CDR X) FN) ) ) ) ) ) ) )

Examples:

(LAMBDA (J) (MAPCAR J (FUNCTION ADDl))) ((0 1 2 3) = (1 2 3 4 ) [I]

(LAMBDA (J) (MAPCAR J (FUNCTION (LAMBDA (L)


(COND ((NUMBERP L) (TIMES L L))
(T L)))))) ((A 1 B 2 C 3)) = (A 1 B 4 C 9)

In example [I], ADD1 is the functional argument, and the lambda expression adds
one to each element in a list of numbers. In example [ 21 , we have a lambda expres-
sion as the functional argument. The result is the input list with each numerical
element replaced by its square.

W C is a function of two arguments of the form

where X is a list and FN is a function of one argument. MAPC is like MAPCAR


in its effect, but like NAP in its value. MAPC applies FN to each element of X,
and returns as its value the terminal element of the S-expression X (usually NIL).
It is defined by

DEFINE ( (
(MAPC (LAMBDA (X FN) (PROG ( )
A (COND ((ATOM X) (RETURN X)))
(FN (CAR x))
(SETQ X (CDR X))
(GO A) 1)) 1)
Example:

(LAMBDA (L) (MAPC L (FUNCTION PRINT) ) ) ((THIS IS (A LIST) ) )


yields

THIS
IS
(A LIST)
NIL

18.6 CAUTIONS
Most functionals ( . e functions that take functional arguments) cannot be used
at the top level, since the special form FUNCTION must be evaluated. As we
know, EVALQUOTE quotes arguments when transmitting them, thus FUNCTION would not
be evaluated. Therefore, use functionals only in lambda expressions.

18.7 EXERCISES
Evaluate the following:

J' I. (LAMBDA (L) (MAP L (FUNCTION PRINT)) ) ((TRY THIS SIMPLE CASE FIRST))

J 2 (LAMBDA (L) (MAPC L (FUNCTION PRINT)) ) ( (NOW THIS ONE) )

.. 3. (LAMBDA (L) (MAPCAR L (FUNCTION PRINT) ) ) ((AND LASTLY THIS ONE) )


4. (LAMBDA (J) (MAPLIST J (FUNCTION
(LAMBDA (K) (SUBST+(~UOTE ONE) 1 ~1)))) ((1 2 3 1 4 1 5))

' j 5. (LAMBDA (J) (MAPLIST J (FUNCTION


(LAMBDA (K) (MAPCAR K (FUNCTION LENGTH))) ) ) )
(((A) (1 2) (A B c) (1 2 3 4)))
\ 6 . (LAMBDA (L) (MAPLIST L (FUNCTION (LAMBDA (J) (CONS (CAR J)
(CAR J)))))) ((A B C D El)
7. (LAMBDA (L) (MAPCAR L (FUNCTION (LAMBDA (J)
(CONS J (QUOTE X)))))) ((A B C D E))

'SUBST is a built-in system function equivalent to function REPLACE, given as


exercise 20 of Chapter 14.
8. SPECIAL ((Y))
DEFINE ((
(YDOT (LAMBDA (L Y) (MAPCAR L (FUNCTION (LAMBDA (J)
(CONS J Y)))))) ))
UNSPECIAL ((Y) )

Note:

I f we consider t h e f u n c t i o n a l argument h e r e a s a
separate function, i t is evident t h a t i t contains
a bound v a r i a b l e J , and a f r e e v a r i a b l e Y. This
f r e e v a r i a b l e r e q u i r e s a SPECIAL d e c l a r a t i o n , even
though i t i s bound i n YDOT.

i .
\j h \ YDOT ((A B C D E) Z)
YDOT ((AB C D E) ( 1 2 3 4 5))

9. MAPCAR i s a f u n c t i o n of two arguments i n which t h e second argument i s


a f u n c t i o n t h a t t a k e s one argument. Define f u n c t i o n a l MAPCAR2 a s a
f u n c t i o n of t h r e e arguments i n which t h e f i r s t two arguments a r e l i s t s
of equal l e n g t h and t h e l a s t argument i s a f u n c t i o n t h a t t a k e s two
arguments; e.g.,

(LAMBDA (A B) (MAPCAR2 A B (FUNCTION DIFFERENCE))) ((5 6 7 8) (1 2 3 4))


= (4 4 4 4)

(LAMBDA (A B) (MAPCAR2 A B (FUNCTION CONS))) ((ONE TWO THREE) (1 2 3))


= ((ONE .
1 ) (TWO .
2) (THREE 3)) .
10. Define a f u n c t i o n using f u n c t i o n a l s c a l l e d

TYPE (x)

where x i s a l i s t of items. The v a l u e of TYPE i s a list of type-


d e s c r i p t o r s of each t o p - l e v e l element of x according t o t h e following
schedule :
i f f ixed-point number, FIX
i f f l o a t i n g - p o i n t number, FLT
i f l i t e r a l atom, ATOM
i f d o t t e d p a i r of atoms, DOTPAIR
i f none of t h e above, LIST

For example,

TYPE ((1.0 (A . B) (-12 3) A12 4 6 ) 2 = CFLT DOTPAIR LIST ATOM FIX)


CHAPTER 19.
LIST STRUCTURES, PROPERTY LISTS, AND MACROS

P r e v i o u s c h a p t e r s of t h i s t e x t have d i s c u s s e d how computation may be performed


w i t h symbolic e x p r e s s i o n s . T h i s c h a p t e r completes t h e d i s c u s s i o n by f o c u s i n g on
a d d i t i o n a l f e a t u r e s of LISP f o r manipulating and u s i n g l i s t s t r u c t u r e s , p r o p e r t y
l i s t s , and macros.

19.1 GRAPHICAL REPRESENTATION OF LIST STRUCTURE

I n Chapter 3 we noted t h a t t h e graphs of l i s t s were i n t h e form of b i n a r y t r e e s .


L i s t s t r u c t u r e i s a l s o of t h a t form. Rather t h a n r e p r e s e n t i n g t h e graph i n a
v e r t i c a l format, however, i t i s c o n v e n t i o n a l t o r e p r e s e n t t h e graph i n a h o r i -
z o n t a l format t h a t d i s p l a y s more c l e a r l y t h e l e v e l of s u b l i s t s . For example,
the list

was shown a s t h e v e r t i c a l graph

CAR CDR
A
i \

The e n t i r e l y e q u i v a l e n t h o r i z o n t a l graph i s given by

CAR CDR s I

A t B P C
Similarly, the list

i s t h e graph

L i s t s t r u c t u r e i n LISP i s c o m p l e t e l y g e n e r a l and may b e t w i s t e d , k n o t t e d ,


threaded, re-entrant o r c i r c u l a r a s t h e u s e r d e s i r e s . For example,

i s t h e g r a p h of a c i r c u l a r l i s t ( i . e . , the t a i l p o i n t s b a c k t o t h e head) t h a t
can o c c u r i n LISP a s t h e r e s u l t of computation. Of course, i t s printed repre-
s e n t a t i o n i s i n f i n i t e i n l e n g t h , and i s of t h e form

Unless t h e LISP system p r o v i d e s s p e c i a l i n p u t - o u t p u t mechanisms, s u c h s t r u c t u r e s


c a n n o t b e r e a d o r p r i n t e d ; however, t h e y a r e p e r f e c t l y p r o p e r s t r u c t u r e s and may
find useful application.

Another i n t e r e s t i n g l i s t s t r u c t u r e i s one t h a t r e f e r e n c e s a common sub-expression.


For example, t h e S-expression

((JOHN . DOE) MARY (JOHN . DOE))


might r e p r e s e n t t h e g r a p h
I

CMARY
I

-+JOHN DOE
4

19.2 MANIPULATING LIST STRUCTURE

CONS i s t h e p r i n c i p a l f u n c t i o n we have examined t h a t manipulates l i s t s t r u c t u r e ;


b u t CONS does not change e x i s t i n g s t r u c t u r e s : i t c r e a t e s new ones. Other
f u n c t i o n s a l r e a d y t r e a t e d , such a s APPEND and SUBST, do n o t change s t r u c t u r e s ,
b u t make m o d i f i c a t i o n s by copying t h e i r arguments. Copying i s c o s t l y and i n -
convenient. Complete g e n e r a l i t y i n manipulating l i s t s t r u c t u r e i s provided i n
LISP by means of t h e pseudo-functions RPLACA and RPLACD, which change l i s t
s t r u c t u r e permanently. A s such, they must be used w i t h c a u t i o n . They can cause
permanent damage t o t h e system i f used i n c o r r e c t l y . They may a l s o cause i n f i n i t e
s e a r c h o r p r i n t i n g of c i r c u l a r l i s t s c r e a t e d u n i n t e n t i o n a l l y . Functions u s e f u l
f o r mani,pulating l i s t s t r u c t u r e s a r e given below:

RPLACA (X Y ) RPLACA r e p l a c e s t h e CAR of X by Y. I t s v a l u e is X, b u t


X i s now a d i f f e r e n t s t r u c t u r e from what i t was b e f o r e .
The v a l u e of RPLACA can be d e s c r i b e d by t h e v a l u e of t h e
form

(CONS Y (CDR X))

However, t h e e f f e c t i s q u i t e d i f f e r e n t ; t h e r e i s .no CONS


involved, only p o i n t e r s a r e changed.

RPLACD r e p l a c e s t h e CDR of X by Y. I t s v a l u e i s X, b u t
X i s now a d i f f e r e n t s t r u c t u r e from what i t was b e f o r e .
The v a l u e of RPLACD can be d e s c r i b e d by t h e v a l u e of t h e
form

(CONS (CAR X) Y)

-146-
However, t h e e f f e c t i s q u i t e d i f f e r e n t ; t h e r e i s no CONS
involved, only p o i n t e r s a r e changed.

NCONC (X Y) T h i s f u n c t i o n i s s i m i l a r t o APPEND i n e f f e c t . But


NCONC does n o t copy l i s t s t r u c t u r e ; i t a c h i e v e s t h e e f f e c t
by modifying t h e l i s t s t r u c t u r e X, and making t h e l a s t
element of t h e s t r u c t u r e p o i n t t o t h e l i s t Y. The v a l u e
of NCONC i s t h e l i s t X, b u t s i n c e X has been modified,
t h e v a l u e i s t h e concatenated l i s t of X and Y .

19.3 EXAMPLES OF MODIFYING LIST STRUCTURE

Assume t h e v a r i a b l e s X and Y a r e bound t o t h e l i s t s (A B C) and (D E F ) ,


respectively. These l i s t s have t h e graphs

X
CAR CDR,
+ A B
b

E v a l u a t i n g t h e form

y i e l d s t h e graph
X
E v a l u a t i n g t h e form

(RPLACD X Y)

y i e l d s t h e graph

X
CAR CDR

A
Y

+ D - - E
n
F

with o l d CDR (X), i . e . , l i s t (B C), removed from t h e new s t r u c t u r e .

E v a l u a t i n g t h e form

(RPLACA X (CDR X))

y i e l d s t h e graph

CAR CDR
w B C
I t

Evaluating the f o m

(NCONC X Y)

y i e l d s t h e graph
X
E v a l u a t i n g t h e form

(NCONC X XI

yields the circular list

19.4 PROPERTY LISTS

What i s t h e b e s t way t o o r g a n i z e a h i g h l y s t r u c t u r e d , y e t i n d e f i n i t e c o l l e c t i o n
of p r o p e r t i e s f o r a group of mathematical objects--a r e s t r i c t e d d i c t i o n a r y of
E n g l i s h words, f o r example? One s i m p l e s o l u t i o n i s t o have t h e d a t a o r g a n i z e d
l i k e an a - l i s t , i.e., a l i s t of p a i r s . One element of each p a i r c o u l d b e a
l i t e r a l atom t h a t r e p r e s e n t s t h e mathematical o b j e c t ; t h e o t h e r element of t h e
p a i r could be t h e l i s t of p r o p e r t i e s . Then f u n c t i o n s l i k e ASSOC could b e used
t o s e a r c h t h e l i s t f o r d e s i r e d i n f o r m a t i o n , u s i n g t h e l i t e r a l atoms a s i n d i c e s .

An a l t e r n a t i v e s o l u t i o n i s t o d i r e c t l y " a t t a c h " t h e p r o p e r t i e s of i n t e r e s t t o
each atom. The a t t a c h m e n t i s achieved by means of a p r o p e r t y l i s t o r p - l i s t
t h a t i s a s s o c i a t e d w i t h e a c h l i t e r a l atom. For many a p p l i c a t i o n s , t h e u s e of
p r o p e r t y l i s t s o f t e n improves t h e speed and f l e x i b i l i t y of problem s o l u t i o n b y ,
r e p l a c i n g c o s t l y l i s t s e a r c h e s w i t h d i c t i o n a r y - l i k e lookup on t h e p r o p e r t y l i s t .

For most LISP systems, l i t e r a l atoms a r e r e p r e s e n t e d i n t e r n a l l y i n memory a s


list structures. P a r t of t h e s t r u c t u r e i s t h e BCD a r r a y f o r t h e p r i n t name of
t h e atom; a n o t h e r p a r t i s t h e v a l u e c e l l f o r h o l d i n g v a r i a b l e b i n d i n g s . Other
p a r t s may b e used by t h e system. U s u a l l y , t h e "CDW chain" of t h e s t r u c t u r e i s
t h e property list.

The p r o p e r t y l i s t i s provided f o r t h e u s e r and i s i n i t i a l l y empty. (Some LISP


systems u s e t h e p r o p e r t y l i s t f o r system f e a t u r e s and i t may have i n i t i a l p r e s e t
information.) Inasmuch as t h e p r o p e r t y l i s t i s a g e n e r a l l i s t , i t may b e used
i n any manner d e s i r e d . However, a c o l l e c t i o n of system f u n c t i o n s i s a v a i l a b l e
f o r manipulating p r o p e r t y l i s t s .

CAR

v
I

OTHER DATA
CDR
b SEX - - F
I

PARENTS c b AGE
w d 4

CLARK

Before we examine t h e p r o p e r t y l i s t f u n c t i o n s , n o t e the atomic s t r u c t u r e : d i s -


played above f o r t h e l i t e r a l atom ELLIN. The p r o p e r t y l i s t "hangs" from t h e CDR
of t h e s t r u c t u r e . This i s t h e convention adopted h e r e and one t h a t i s s i m i l a r
t o many LISP implementations. The p r o p e r t y l i s t i s s t r u c t u r e d a s a l i s t : of
a l t e r n a t i n g i n d i c a t o r s and p r o p e r t i e s ; e - g . , (SEX F PARENTS (CLARK MARCIA )
AGE 6 ) . indicatorst and p r o p e r t i e s a r e g e n e r a l S-expressions .
S e v e r a l p r o p e r t y l i s t f u n c t i o n s and t h e i r d e f i n i t i o n s a r e g i v e n below:

GET (X Y) GET i s a f u n c t i o n t h a t s e a r c h e s t h e l i s t X f o r a n
i n d i c a t o r E ~ U A L ' t o Y. I f such a n i n d i c a t o r it; found,
i t s property--the n e x t l i s t element ( i . e . , t h e CAR of
t h e r e s t of t h e l i s t ) - - i s r e t u r n e d a s t h e v a l u e of GET.
Otherwise, t h e v a l u e of GET i s NIL.

PUT (X Y 2) T h i s f u n c t i o n p u t s on t h e p r o p e r t y l i s t of t h e l i t e r a l
atom X t h e i n d i c a t o r Y followed by t h e p r o p e r t y Z. Any
previous p r o p e r t y of Y i s r e p l a c e d by 2. The v a l u e of
PUT i s Y.

'some systems u s e EQ r a t h e r t h a n EQUAL i n p r o p e r t y l i s t s e a r c h f u n c t i o n s , t h e r e b y


r e q u i r i n g l i t e r a l atoms a s i n d i c a t o r s .
PROP (X Y FN) The function PROP searches the list X for an indicator
i
EQUAL to Y. If such an indicator is found, the value
of PROP is the rest of the list beginning immediately
after that indicator. Otherwise, the value is FN ( ) ,
where FN is a function of no arguments.

REMPROP (X Y) This function removes all occurrences of the indicator


Y and its property from the property list X. The
value of REMPROP is NIL.

19.5 EXAMPLES OF PROPERTY LIST FUNCTIONS

Assume the property list for ELLIN given in Paragraph 19.4. We may evaluate the
following expressions:

(GET (QUOTE ELLIN) (QUOTE AGE)) = 6


(LAMBDA (X Y) (PROP X Y (FUNCTION (LAMBDA ( ) NIL))) )
(ELLIN PARENTS) = ((CLARK MARCIA) AGE 6)
(LAMBDA (X Y Z) (PUT X Y Z))
(ELLIN SISTERS (HILLARY WENDY) ) = SISTERS

After evaluation of this last example, the property list of ELLIN is augmented
by the indicator SISTERS and the property (HILLARY WENDY).

(REMPROP (QUOTE ELLIN) (QUOTE SEX)) = NIL

The result of this form is the deletion of the indicator SEX and the
property F from the p-list ELLIN.

19.6 MACROS

In a compiler-based LISP system, we must be concerned with both compile-time and


run-time activities of the system. When EVALQUOTE evaluates DEFINE, we are talking
about run-time for the pseudo-function DEFINE. If we are defining a function,
for example, LAST, we are talking about compile-time for LAST. In other words,
one function's run-time is another function's compile-time.

'some systems use EQ rather than EQUAL in property list search functions, thereby
requiring literal atoms as indicators.
As we have already seen, functions can he compiled by DEFINE or by top-level
evaluation of a lambda expression. In the latter case, evaluation means first
compile and then run the compiled code with the supplied arguments. This is
often called "compiling at run-time". This distinction is significant because
it enables compiled code to operate where previously an interpreter was necessary.
In particular, it affects the code that is compiled for a function that enables
that function to retrieve the correct binding for variables at run-time.

A classic problem for compilers is this: How do you define a function of an


indefinite number of arguments, such as PLUS? The key to the answer is that
the arguments are only indefinite when you define the function, not when you run
it. If you could delay compilation until run-time, at which time the number of
arguments is definite, you could resolve this dilemma. In essence, this is what
an interpreter does. To resolve this problem in LISP, we make use of macros via
11
the pseudo-function MACRO.

19.7 MACRO EXPANSION

The function MACRO takes an argument list in the same format as DEFINE, e.g.,

MACRO ( ( (namel (LAMBDA ZJarZist b o d y ) )

(name2 (LAMBDA ZJarl i s t body ) )

As with DEFINE, MACRO compiles each of these definitions. Now watch closely,
for here comes the difference. When a macro function (defined by MACRO)
used in a lambda expression, either at the top level or within a DEFINE, the
macro function is executed before the lambda expression (of which it is part)
is compiled. The argument for the macro is the form in which it is used. In
other words, the macro function is run before compile-time for its effect on
the lambda expression. What does this buy us? That depends on the macro, but
essentially it allows us to expand elements of the lambda expression before it
is compiled, by substituting (for all occurrences of the macro function and
its arguments) other expressions tailored to the particular use of the macro
i n t h e lambda expression. We c a l l t h i s macro expansion. For example, i t permits
us t o d e f i n e a " s p e c i a l form" of a n i n d e f i n i t e number of arguments by c o n v e r t i n g
t h a t s p e c i a l form t o a composition of n e s t e d f u n c t i o n s each having two arguments--
t h e n e s t i n g being determined by examination of t h e p a r t i c u l a r u s e of t h e s p e c i a l
form i n t h e g i v e n lambda expression.

Take, f o r example,

(PLUS Xl x* 0 . . xn)

Here we have a s p e c i a l form of an i n d e f i n i t e number of arguments. But when we


u s e PLUS, we always have a f i x e d number of arguments. Given a f u n c t i o n *PLUS,
which t a k e s t h e sum of i t s two arguments, we can expand

(PLUS Xl x* x3 x4) = (*PLUS XI (*PLUS x2 (*PLUS x3 x 4 ) ) )

Thus, t h e macro d e f i n i t i o n of PLUS i n v o l v e s a body of code t h e s o l e purpose of


which i s t o s u b s t i t u t e *PLUS a n a p p r o p r i a t e number of times i n t h e proper p l a c e s
wherever PLUS appears i n a lambda e x p r e s s i o n being compiled. Then, a f t e r compila-
t i o n , t h e r e i s no t r a c e of PLUS, b u t many occurrences of *PLUS. The o p e r a t i n g
code, however, works e x a c t l y a s d e s i r e d . L e t ' s examine t h e macro d e f i n i t i o n
f o r PLUS t o s e e how t h i s works.

MACRO ( (
(PLUS (LAMBDA (L) (*EXPAND L (QUOTE *PLUS)))) ) )

Here L, t h e argument f o r t h e macro PLUS, i s t h e form

(PLUS Xl x* x3 x4)

*EXPAND i s a system f u n c t i o n used e x c l u s i v e l y f o r expanding macros. It has


t h e form

(*EXPAND form f n )

where form i s t h e e x p r e s s i o n t o be expanded, a s L above, and f n i s t h e system


f u n c t i o n , a f u n c t i o n of two arguments, t o b e used i n t h e expansion. For
form = (PLUS x 1 x 2 x 3 x 4)

we get

(*EXPAND form f n ) = (*PLUS xl (PLUS x2 x3 x 4 ) )

Note t h a t *EXPAND j u s t expands t h e form by one *PLUS when i t i s executed. When


we t h e n a t t e m p t t o compile t h e new form

(*PLUS Xl (PLUS x2 x3 x 4 ) )

*EXPAND i s c a l l e d a g a i n t o expand t h e i n n e r PLUS,

form = (PLUS x2 xl x4)

f n = *PLUS

thereby y i e l d i n g

(*PLUS Xl (*PLUS x2 (PLUS x3 x 4 ) ) )

By r e p e a t e d a p p l i c a t i o n of *EXPAND each time t h e macro PLUS i s encountered, we


e v e n t u a l l y a r r i v e a t t h e complete expanded form f o r PLUS r e g a r d l e s s of t h e
number of arguments, x
n
.
The d e f i n i t i o n f o r *EXPAND i s s t r a i g h t f o r w a r d and noted h e r e f o r r e f e r e n c e .

DEFINE ( (
(*EXPAND (LAMBDA (FORM FN)
(COND ( (NULL (CDDR FORM)) (CADR FORM))
(T (CONS FN
(CONS (CADR FORM)
(CONS(CONS (CAR FORM) (CDDRFORM))NIL) 1))))) 1)

Note how n i c e l y *EXPAND works f o r t h e l a s t term of t h e expansion


When e n t e r e d because macro PLUS was encountered,

form = (PLUS x4)

f n = *PLUS

(CDDR f o m ) = NIL

(CADR form) = x4

Thus, t h e form

(PLUS x4)

g e t s r e p l a c e d by j u s t x 4 , y i e l d i n g t h e f i n a l expanded e x p r e s s i o n

It should now b e c l e a r how e l e g a n t t h i s macro system i s . To s o l v e t h e k n o t t y


problem of s p e c i a l forms of a n i n d e f i n i t e number of arguments, a l l we need a r e
four things:

1. *EXPAND--a s i n g l e LISP f u n c t i o n e a s i l y d.efined i n LISP.

2. A f u n c t i o n l i k e t h e macro t o be d e f i n e d , b u t of j u s t two arguments.


Such a two-argument f u n c t i o n i s e a s i l y d e f i n e d i n LISP.

3. A macro d e f i n i t i o n of t h e s p e c i a l form.

4. MACRO r e c o g n i t i o n by t h e compiler.

Macros must be d e f i n e d b e f o r e t h e y a r e used. Once d e f i n e d , macros may be used


w i t h i n o t h e r macro d e f i n i t i o n s , t h e r e b y p r o v i d i n g complete g e n e r a l i t y of MACRO.
S i n c e a macro must be d e f i n e d b e f o r e i t can b e used, macro d e f i n i t i o n s cannot
be r e c u r s i v e ; i . e . , a macro cannot c a l l i t s e l f .

19.8 MACRO DEFINITIONS OF NEW FUNCTIONS

MACRO h a s u t i l i t y i n a r e a s o t h e r than expansion of s p e c i a l forms. It can b e


used t o d e f i n e f u n c t i o n s n o t a l r e a d y i n t h e system. Take, f o r example, t h e
pseudo-function CSETQ. Assuming we have CSET, we can d e f i n e CSETQ by
MACRO ( (
(CSETQ (LAMBDA (FORM) (LIST (QUOTE CSET)
(LIST (QUOTE QUOTE) (CADR FORM) ) (CADDR FORM))))
>1
Then whenever t h e form

(CSETQ A B)

i s encountered,

(CSET (QUOTE A) B)

w i l l be s u b s t i t u t e d and compiled.

19.9 EXERCISES

For t h e l i s t s

X = ((CAT) (DOG) (FOX) )

and

e v a l u a t e t h e f o l l o w i n g forms; i . e . , show t h e r e s u l t i n g graphs.

1. (NCONC X (NCONC X Y))

2. (RPLACD (RPLACA (CDR Y) (CONS (CADR Y) (CDDR Y)) ) NIL)

3. Define NCONC

4. Define t h e f u n c t i o n REVLIST which r e v e r s e s a l i s t w i t h o u t copying.


The e f f e c t i v e v a l u e i s l i k e t h a t r e t u r n e d by t h e f u n c t i o n REVERSE,

5. A "blam" l i s t i s a l i s t i n which b o t h t h e CAR and CDR p o i n t t o t h e


s u c c e s s o r element of a l i s t . (The l i s t i s u s e f u l f o r debugging t h e
LISP garbage c o l l e c t o r and f o r showing t h e g e n e r a l i t y of LISP l i s t
structure.) A three-element "blam" l i s t h a s t h e graph
D e f i n e t h e BLAM f u n c t i o n of one argument, a l i s t , t h a t m o d i f i e s t h e
l i s t i n t o a "blam" l i s t . Do n o t u s e CONS.

Assume w e a r e b u i l d i n g a program t o p l a y c o n t r a c t b r i d g e . The f o u r p l a y e r s i n


t h e game a r e r e p r e s e n t e d by t h e l i t e r a l atoms NORTH, SOUTH, EAST, and WEST.
The p r o p e r t y l i s t of e a c h p l a y e r h a s t h e i n d i c a t o r HAND and a l i s t of 1 3 l i t e r a l
atoms ( a s t h e c a r d s ) f o r t h e p r o p e r t y . F u r t h e r assume each c a r d h a s two i n d i -
cators--SUIT and RANK--and t h e a p p r o p r i a t e p r o p e r t y f o r each. Use t h e p r o p e r t i e s
SPADE, HEART, DIAMOND, and CLUB f o r t h e i n d i c a t o r SUIT, and t h e i n t e g e r s 1 4 , 1 3 ,
1 2 , e t c . c o r r e s p o n d i n g t o Ace, King, Queen, e t c . as t h e p r o p e r t i e s of t h e
i n d i c a t o r RANK.

6. Define t h e f u n c t i o n

SUITS ( p l a y e r )

that: (1) s e p a r a t e s t h e c a r d s of a g i v e n hand i n t o t h e f o u r s u i t s ;


( 2 ) adds t o t h e p r o p e r t y l i s t f o r t h a t p l a y e r t h e i n d i c a t o r s SPADES,
HEARTS, DIAMONDS, CLUBS and; ( 3 ) a d d s t o t h e p r o p e r t y l i s t f o r t h a t
p l a y e r t h e l i s t of c a r d s i n each s u i t a s t h e p r o p e r t i e s of t h e s e
indicators.

7. Define t h e f u n c t i o n

LONGSUIT ( p l a y e r )

t h a t r e t u r n s a s i t s v a l u e a d o t t e d p a i r whose CAR i s t h e name of t h e


l o n g e s t s u i t , and whose CDR i s t h e l e n g t h of t h a t s u i t . Assume
f u n c t i o n SUITS above h a s a l r e a d y s o r t e d t h e hand. For s u i t s of e q u a l
maximum l e n g t h , SPADES > HEARTS > DIAMONDS > CLUBS.

8. Define t h e f u n c t i o n

POINTS ( p l a y e r )
that returns as its value the point-count of the given hand. Assume
function SUITS has already sorted the hand; assume the following point
system:

Ace (rank 14) 4 points


King (rank 13) and suit length > 1 3 points
Queen (rank 12) and suit length > 2 2 points
Jack (rank 11) and suit length > 3 1 point
Void (suit length = 0) 3 points
Singleton (suit length = 1) 2 points

9. Define the predicate

STOPPERS (player)

that is T if the hand contains one of the following combinations for


each suit: ACE, or KING and length > 1, or QUEEN and length > 2;
and NIL otherwise. Assume function SUITS has already sorted the hand.

10. Define the predicate

BALANCED (player)

that is T if there are at least three cards in each suit of the hand,
and NIL otherwise. Assume function SUITS has already sorted the hand.
Use all your functions from problems 5-10 on an unsorted hand to
define the function

OPENBID (player)

that returns an opening bid in contract bridge according to the follow-


ing schedule (with apologies to Charles Goren).

Condition -
Bid

If points ~ 1 3 ,and the longest suit <7 cards, then: PASS


If 12 <points ~ 2 0 ,and either stoppers = NIL, or
balanced = NIL, where <suit> = longest suit, then: (1 <suit>)
If 15 <points ~ 2 0 ,and stoppers = T, and
balanced = T, then: (1 NO TRUMP)
Condition -
Bid

If points >20, and either stoppers = NIL, or


balanced = NIL, where <suit> = longest suit, then: (2 <suit>)
If points >20, and stoppers = T, and
balanced = T, then: (2 NO TRUMP)
If 6 <points ~ 1 3 ,and longest suit >6 cards,
where <suit> = longest suit, then: (3 <suit>)

11. *TIMES exists as a function of two arguments, the value of which is


the product of its arguments. Define a macro function PROD, using
*TIMES and *EXPAND such that

PROD (xl x2 ... xn) = (*TIMES x1 (*TIMES x2 ... (*TIMES Xn - xn) ))

12. *MAX and *MIN exist as functional counterparts of MAX and MIN, but
having only two arguments. Define the macros MAXIMUM and MINIMUM.

13. In the last chapter, dealing with functional arguments, we saw that
we must always use the special form FUNCTION, when we wish to quote a
functional expression appearing as an argument of another functional
expression; e.g.,

(LAMBDA (L) (MAPLIST L (FUNCTION (LAMBDA (J) (LIST J))) ) ) [l]

Define the macro FLAMBDA, which when used as in form [2],

(LAMBDA (L) (MAPLIST L (FLAMBDA (J) (LIST J) ) ) ) [2]

will expand form [2] to form [I].

14. If you define LIST as a macro and it's wrong, you can wreck the system.
Therefore, define LIST1 as a macro that does exactly what LIST does.

Hint: Remember that

(CONS xn- xn) = ( x ~ - ~


xn)

so the macro must produce

(CONS xn NIL)
as its last expansion. In other words, we want

LIST1 (A B C) = (A B C)

and not

15. When printing multi-word messages in LISP, we always print the


message as a parenthetical expression, i.e., a list; e.g.,

(NOW HEAR THIS)

Define a macro PRINTQ that is a special form of an arbitrary number


of arguments that quotes its arguments and prints them (on one line
if they will fit) without parenthesization; e.g.,

(LAMBDA ( ) (PRINTQ NOW HEAR THIS)) ( ) = NOW HEAR THIS

Hint :

Define PRINTQ as a macro that uses an auxiliary


function PRINTQ1, which in turn uses PRIN on each
argument of PRINTQ.

16. Paragraph 15.6 describes PROG2, a function of two arguments.


Define the macro PROGN as a generalization of PROG2 for an
indefinite number of arguments.
CHAPTER 20.
A COMPLETE LISP PROGRAM

In this concluding chapter we examine a collection of function definitions that


constitute a LISP program which differentiates an algebraic polynomial. Assuming
an on-line, interactive LISP system, the program reads the polynomial as an
"infixttalgebraic expression, and prints the derivative as an infix algebraic
expression. The polynomial is constructed from the four arithmetic operators
and exponentiation (with constant powers).

The treatment for other operators, such as logarithmic and trigonometric opera-
tors would not be significantly more difficult, but would require expanding the
descriptive text without contributing more instructive material to the chapter.
The formal syntax of the algebraic expression is listed below as a series of
syntax equations in Backus Normal Form (BNF). l2 For those not familiar with
this formal language for describing the syntax of formal languages, the English
language interpretation of each equation is given.

20.1 POLYNOMIAL SYNTAX

Syntax Equation English Interpretation

<digit> ::= 011121.~.19 A digit is one of the ten numerals


0 to 9.

A variable is one of the 26 letters


A to Z.

<constant> ::= <digit> <constant>l<digit> A consfiant is an indefinite string of


digits.

Multiplication is explicitly noted


by the character It*", or implied by
its absence.

I
<primary> ::= <variable> <constant>( (<expression>)
A primary is either a variable, or
a cons-bant, or a parenthesized
expression.
-161-
Syntax Equation English Interpretation

<secondary> ::= <primary>f<constant>I <primary>


A secondary is either a primarg raised
to a constant power (explicitly noted
by the character "f ") , or a primary.

<term> ::= <secondary>/<term>( <secondary> <mul> <term>I <secondary>


A tern is either a secondary divided
by a term (explicitly noted by the
character "/"), or a secondarg
(explicitly or implicitly multiplied
by a tern), or a secondary.

<expression> : := <expression> + <term>1 <expression> - <term>1 <term>


An expression is an indefinite sum or
difference of terns.

These syntax equations allow implicit multiplication by limiting variables to


one letter. They allow exponentiation to constant powers, and admit only
integer constants of a magnitude within the computer's capacity. Though unary
minus is excluded and the other operators are restricted to two operands, the
remaining legal constructions allow for a rich set of algebraic expressions,
some examples of which are noted below.

PROGRAM STRATEGY

The problem of symbolic differentiation in LISP is easily solved if the


expression to be differentiated is represented as an S-expression in prefix
notation. Some examples are shown below.
Infix Notation S-Expression

A+B (PLUS A B)
AX+3 + BX1.2 - 3X (PLUS (TIMES A (EXPT X 3))
(DIFFERENCE (TIMES B (EXPT X 2))
(TIMES 3 X)))

With this knowledge, the strategy is to translate the given eqression into the
desired prefix form, differentiate that form, and translate the resulting form
back to infix notation. Though it is not obvious at this point, the solution
also requires a program to perform algebraic simplification of the symbolic
results of differentiation, and a supervisory program to control program
execution and to perform input-output.

This overall strategy results in a complete LISP program structured as a


hierarchy of sub-programs under the control of a supervisor program named DIFF.
There are four principal sub-programs: IN2PRE, DERIV, SIMPLIFY, and PRE2IN.
Each is a supervisor that controls further program hierarchies, and is itself
a complete program. The balance of the chapter is devoted to describing these
programs and their interaction with the supervisor DIFF.

Many algorithms exist for translating from infix to prefix notation,13,14 a


technique well understood by compiler writers. The approach taken here, for
its simplicity of exposition, is to use each of the BNF equations of Paragraph
20.1 as a basic specification for a LISP function. The set of functions defined
satisfy the specifications of the syntax equations, and concurrently parse and
translate the infix expression.

We have then, seven functions--DIGIT, VARIABLE, CONSTANT, PRIMARY, SECONDARY,


and EXPRESSION--each corresponding to its like-named syntax equation. (We
need only seven functions as the specification for mu2 can be subsumed by the
function TERM.) There is also a function, NUMBER, that converts the indefinite
string of digits collected by CONSTANT into a LISP number.
The f u n c t i o n IN2PRE i s a s u p e r v i s o r y program t h a t c o n t r o l s t h e t r a n s l a t i o n and
provides d i a g n o s t i c information. It t a k e s one argument, a l i s t of a l l t h e
non-blank c h a r a c t e r s , i n o r d e r , from t h e i n f i x expression. This l i s t i s
s u p p l i e d by t h e c a l l i n g f u n c t i o n DIFF.

Closer examination of t h e seven p r i n c i p a l s y n t a x e q u a t i o n s shows t h a t they only


s t a t e whether a given s t r i n g of c h a r a c t e r s s a t i s f i e s t h e s p e c i f i c a t i o n . In
LISP, t h e seven corresponding f u n c t i o n s can b e s t accomplish t h i s t a s k i f they
a r e predicates. However, we want them t o do more than j u s t r e c o g n i z e l e g a l
syntax. We want them a l s o t o t r a n s l a t e i n t o p r e f i x form t h a t p a r t of t h e i n p u t
t h a t s a t i s f i e s t h e s p e c i f i c a t i o n s , and t o r e t u r n t h e t r a n s l a t i o n t o g e t h e r w i t h
t h e u n t r a n s l a t e d p o r t i o n of t h e i n p u t . I f t h i s i s n o t enough of a problem, we
a l s o wish t o avoid e x p l i c i t l y having t o "backup" o r r e s t o r e t h e i n p u t s t r i n g t o
i t s i n i t i a l s t a t e when a given s p e c i f i c a t i o n i s not s a t i s f i e d . We encounter
t h i s problem when t h e s p e c i f i c a t i o n r e q u i r e s looking ahead i n t o t h e s t r i n g of
c h a r a c t e r s b e f o r e i t can determine i f t h e syntax e q u a t i o n i s s a t i s f i e d .

The s o l u t i o n t o a l l t h e s e problems i s t o make t h e seven f u n c t i o n s semi-predicates.


Each of t h e seven f u n c t i o n s , t h e n , r e t u r n s t h e v a l u e NIL ( f a l s e ) i f t h e current-
s t a t e of t h e i n p u t does n o t s a t i s f y t h e s p e c i f i c a t i o n s of t h a t f u n c t i o n . If
t h e s p e c i f i c a t i o n i s s a t i s f i e d , t h e c h a r a c t e r s meeting t h e s p e c i f i c a t i o n s a r e
removed from t h e i n p u t l i s t and replaced by t h e i r t r a n s l a t e d p r e f i x form and
t h e r e s u l t i n g l i s t i s r e t u r n e d a s t h e v a l u e of t h e f u n c t i o n ( t r u e ) . For example,
i f t h e fu.nction SECONDARY were c a l l e d w i t h t h e argument

i t would r e t u r n t h e v a l u e

((EXPT A 3 ) + 7)

and s i n c e t h i s v a l u e i s non-NIL, i t s a t i s f i e s t h e conditional expression. The


c a l l i n g function--in t h i s c a s e TERM--knows t h a t t h e CAR of t h e r e t u r n e d l i s t i s
t h e t r a n s l a t e d secondary, and t h a t t h e CDR i s t h e remaining u n t r a n s l a t e d s t r i n g
of c h a r a c t e r s .
By u s i n g s e m i - p r e d i c a t e s we s a t i s f y t h e p r e d i c a t e n a t u r e of t h e s y n t a x e q u a t i o n s
and s i m u l t a n e o u s l y a v o i d e x p l i c i t backup; backup b e i n g s a t i s f i e d by p r o p e r l y
b i n d i n g and t r a n s m i t t i n g arguments between f u n c t i o n s i n t h e program h i e r a r c h y .

The complete l i s t i n g of t h e f u n c t i o n d e f i n i t i o n s f o r IN2PRE a r e g i v e n below.


Note t h a t t h e f r e e v a r i a b l e s DIGITS and ALPHA must: b e d e c l a r e d s p e c i a l b e f o r e
c o m p i l a t i o n of t h e f u n c t i o n s DIGIT and CONSTANT, and t h a t DIGITS and ALPHA must
b e bound b e f o r e e x e c u t i o n of INZPRE. The p r o p e r b i n d i n g s a r e g i v e n by:

CSET ( DIGITS

CSET ( ALPHA (A B C .. . Z ) )
The r e a s o n f o r t h e s e b i n d i n g s w i l l become c l e a r e r a f t e r t h e d i s c u s s i o n of t h e
s u p e r v i s o r program DIFF.

[ I N Z P R E (LAWBOA ( E l
(PROG ( X I
(SETQ X (EXPRESSION € 1 )
(CON0 ( ( N U L L X ) ( P R I N T (QUOTE ( POORLY FORMED EXPRESSION) ) 1 )
( T (RETURN X I ) ) ) ) )

( D I G I T (LAMBDA tE)
(PROG ( X I
(SETQ X (ASSOC (CAR E l D I G I T S ) )
(COND ( ( N U L L X ) (RETURN N I L ) )
( T (RETURN (CONS (CADR X ) (CDR € 1 1 ) ) ) ) ) )

( V A R I A B L E (LAMBDA ( E 1
(CON0 ((MEMBER (CAR E ) ALPHA) E)
(T NIL))) 1

(CONSTANT (LAMBDA ( E 1
(PROG ( X Y )
(CON0 ( (NULL (SETQ X ( D I G I T € 1 ) (RETURN N I L ) ) )
A (SETQ Y (CONS (CAR X I Y ) )
(SETQ E (CDR € 1 )
(CON0 I (OR ( N U L L € 1 (NULL (SETP X ( O I G I T E))))
(RETURN (CONS (NUMBER (REVERSE Y) 1 E ) ) 1 )
(GO A ) ) ) )

(NUHBER (LAMBOA ( € 1
(PROG ( X I
(SETQ X 0 )
A (COND ( ( N U L L € 1 (RETURN X I ) 1
(SETQ X (PLUS ( T I M E S X 10) ( C A R € 1 ) )
(SETQ E (CDR € 1 )
(GO A ) ) ) )

(PRIMARY (LAHBDA ( € 1
(PROG ( X I
(COND ( ( S E T 0 X ( V A R I A B L E E l ) (RETURN X ) )
( (SETQ X (CONSTANT E 1 ) (RETURN X ) 1
( ( N O T (EQ (CAR € 1 LPAR) 1 (RETURN N I L ) )
( ( N O T (SETQ X (EXPRESSION LCOR € 1 ) ) ) (RETURN N I L ) )
( ( N U L L (CDR X I ) (RETURN N I L ) )
( ( E Q (CADR X I R P A R ) (RETURN (CONS (CAR X ) (CDDR X ) ) ) )
(T NIL)))))
(SECONDARY (LAMBDA ( E l
(PROG ( X Y )
(COND ( ( N U L L (SETQ X (PRIMARY E ) I 1 (RETURN N I L )
I ( N U L L (COR X ) (RETURN X I )
( ( N O T (EQ (CADR X ) UPARROW) 1 (RkTURN X I )
( ( S E T Q Y (CONSTANT (CDDR X) 1 )
(RETURN (CONS ( L I S T (QUOTE E X P T ) ( C A R X I ( C A R Y ) I (CDH Y ) ) )
( T (RETURN N I L ) ) ) ) ) I
(TERM (LAMBDA ( € 1
(PROG ( X Y 2 )
(SETQ X (SECONDARY E l 1
(CON0 ( ( O R ( N U L L X I (NULL (CDR X ) ) ) (RETURN X I ) )
(SETQ Z (CDDR X ) 1
(SETQ Y (QUOTE QUOTIENT)
(CON0 (EQ (CADR X I SLASH) (GO A ) 1 )
(SETQ Y (QUOTE T I H E S ) )
(COND ( ( E Q (CADR X ) STAR) (GO A ) ) )
(SETQ Z (CDR X I )
A (CON0 ( ( S E T Q Z (TERM 2 ) )
(RETURN (CONS ( L I S T Y (CAR X I (CAR 2 ) ) (CDR Z ) 1 ) 1
(1 (RETURN X ) ) ) ) ) )

( E X P R E S S I O N (LAMBDA ( € 1
(PROG ( E X P X Y OP)
(COND ( ( N U L L € 1 (RETURN N I L ) )
( ( N U L L (SETQ X (TERM E l ) ) (RETURN N I L ) ) )
(SETQ EXP (CAR X I )
E (CON0 ( ( N U L L ( C D R X ) ) (RETURN E X P I )
( ( € 0 (CADR X I PLUSS) ( S E T 0 OP (QUOTE PLUS) 1 )
( ( E Q (CADR X I DASH) ( S E T 0 OP (QUOTE D I F F E R E N C E ) ) )
( 1 (RETURN (CONS EXP (CDR X ) ) ) ) )
(CON0 ( ( N U L L (SETQ Y (TERH (CDDR X I 1 ) 1 (RETURN N I L ) ) 1
( S E T 0 EXP ( L I S T OP EXP (CAR V ) )
(SET0 X Y )
(GO € ) ) I )

D E R I V is a compact, recursive function that completely differentiates the prefix


e q ~ e s s i o nreturned by I N Z P R E , with respect to a given variable. Listed below
are the seven necessary and sufficient differentiation rules satisfied by DERIV.
The line numbers to the right of each rule show the correspondence between the
rule and its coded form in the function DERIV listed below.
Differentiation Rules Line No.

73

73

74

74

d du dv
5. -
dx
(uv) = v-+u-
dx dx
76

80

7.
d
- (un) = n u
n-1 & . if n = constant
dx dx '

(OERIV (LAMBDA ( E X I
(COND ( ( A T O H € 1 (CON0 ( ( E Q E X I 1 ) ( T 0 1 ) )
((OR (EQ (CAR € 1 (QUOTE P L U S ) ) ( € 0 ( C A R E) ( Q U O T E D I F F E R E N C E ) 1 )
( L I S T (CAH E l ( O E R I V ( C A D R €1 X ) ( D E R I V ( C A U M I E ) X I ) )
( ( E Q (CAR € 1 (QUOTE T I M E S ) )
( L I S T (CUOTE PLUS)
( L I S T ( C A R El (CADDR E l (UErXIV ( C A D 2 E l X I )
( L I S T ( C A R E l (CAUR € 1 ( U t S I V (CAUDK L ) X I ) ) )
( ( E Q (CAR E l (QUOTE Q U O T I E N T ) )
( L I S T (CAR E )
( L I S T (QUOTE DIFFERENCE)
( L I S T ( Q U O T E T I M E S ) ( C A O D R € 1 ( O E R I V (CAW E l X I )
( L I S T ( Q U U T E T I M E S ) ( C A D S E l ( D E K I V (CADDK E ) X I ) )
( L I S T ( O U O T t T I M E S ) ( C A D D K E l (CADDR E ) ) ) )
( (EO (CAR € 1 ( Q U O T E E X P T ) 1
( L I S T (CUOTt TIMES)
( L I S T (QUOTE T I M E S ) (CAUUK E )
(COND ( ( E Q U A L ( C A D D R E) 2 1 ( C A D K E 1 1
( T ( L I S C (CAR € 1 ( C A W € 1 ( S U t 5 1 (CADDi4 € ) I ) ) ) )
( D E R I V (CADR E l X I ) )
(T NIL))))
The s t r u c t u r e of t h e f u n c t i o n DERIV i s simple. It i s one c o n d i t i o n a l expres-
s i o n w i t h s i x c o n d i t i o n a l c l a u s e s ; each c l a u s e s a t i s f i e s one o r two d i f f e r e n t i a -
tion rules. Since t h e r u l e s a r e determined by t h e a r i t h m e t i c o p e r a t i o n , and a
non-atomic p r e f i x e x p r e s s i o n always h a s t h e form

( o p e r a t o r argument argument )
1 2

t h e p r e d i c a t e s i n each c l a u s e determine i f t h e g i v e n e q r e s s i o n s a t i s f i e s i t s
r u l e by examining t h e e;epressionrs o p e r a t o r ( t h e CAR of t h e e q r e s s i o n . ) (For
atomic e x p r e s s i o n s , r u l e s 1 and 2 a r e d e t e c t e d by t h e p r e d i c a t e ATOM.) If the
p r e d i c a t e i s t r u e , t h e c l a u s e a p p l i e s i t s r u l e by simple e v a l u a t i o n o r by u s i n g
r e c u r s i v e c a l l s upon DERIV.

For example, i f t h e o r i g i n a l i n f i x e x p r e s s i o n were

IN2PR.E would r e t u r n t h e p r e f i x e x p r e s s i o n

(PLUS (TIMES 3 (EXPT X 2 ) ) (TIMES 2 X))

f o r DERIV. I f t h i s e x p r e s s i o n i s d i f f e r e n t i a t e d w i t h r e s p e c t t o X, t h e second
c l a u s e ( l i n e 74) would be s a t i s f i e d , and r u l e 3 would be a p p l i e d , y i e l d i n g t h e
expression

(PLUS (DERIV argumentl X) (DERIV argument2 X))

T h i s e x p r e s s i o n i s n o t completely e v a l u a t e d . For

argument = (TIMES 3 (EXPT X 2 ) )


1
argument2 = (TIMES 2 X)

t h e r e c u r s i v e c a l l t o DERIV, f o r each of t h e s e arguments, would invoke r u l e 5


( l i n e 7 6 ) , r e s u l t i n g i n t h e incompletely e v a l u a t e d e x p r e s s i o n s

(PLUS (TIMES (EXPT X 2) (DERIV argument x))


11
(TIMES 3 (DERIV argument X) ) )
12

and
(PLUS (TIMES X (DERIV argument21 X))
(TIMES 2 (DERIV argumentz2 X)))

Again, f o r

argument = 3, argument = (EXPT X 2)


11 12
argument = 2, argument = X
21 22

r e c u r s i v e c a l l s t o DERIV a r e r e q u i r e d , invoking r u l e s 1 and 2 ( l i n e 73) f o r


arguments 11, 21, and 22, and r u l e 7 ( l i n e 86) f o r argument 12. DERIV y i e l d s
a v a l u e of z e r o f o r arguments 11 and 21, one f o r argument 22, and

(TIMES (TIMES 2 X) (DERIV argumentl21 X))

f o r argument 12.

The f i n a l r e c u r s i v e c a l l t o DERIV w i t h

argument = X
121

invokes r u l e 2 and r e t u r n s a v a l u e of one.

Thus, t h e complete symbolic d i f f e r e n t i a t i o n y i e l d s t h e expression

(PLUS (PLUS (TIMES (EXPT X 2) 0)


(TIMES 3 (TIMES (TIMES 2 X) 1 ) ) )
(PLUS (TIMES X 0) (TIMES 2 1 ) ) )

When s i m p l i f i e d , t h i s expression i s reduced t o i t s a l g e b r a i c e q u i v a l e n t

(PLUS (TIMES 6 X) 2)

or i n i n f i x notation

The need f o r a l g e b r a i c s i m p l i f i c a t i o n i s obvious from this simple example. How


we s h a l l achieve s i m p l i f i c a t i o n i s t h e s u b j e c t of t h e next f u n c t i o n t o be
considered--SIMPLIFY.
20.5 SIMPLIFY

Arithmetic s i m p l i f i c a t i o n of symbolic e x p r e s s i o n s i s an a r t , and t h e s u b j e c t of


many l e a r n e d papers i n t h e programming l i t e r a t u r e . l5,l6 I have used a s t r a i g h t -
forward approach, one t h a t y i e l d s a s i m p l e r e x p r e s s i o n , b u t not n e c e s s a r i l y
t h e s i m p l e s t expression. This i s because SIMPLIFY does n o t look f o r common
f a c t o r s among t h e arguments of t h e v a r i o u s sub-expressions. It i s a l s o "'blind"
t o s i m p l i f i c a t i o n s t h a t a r e p o s s i b l e between arguments of a d j a c e n t n e s t e d
operators. However, i t does s a t i s f y a h o s t of r u l e s which s i m p l i f y t h e m a j o r i t y
of e x p r e s s i o n s a l l o w a b l e i n t h i s problem.

The h a r d e s t p a r t of t h i s problem was determining which of a l a r g e number of


i n d i v i d u a l r u l e s should be t r e a t e d by SIMPLIFY. B r e v i t y was my guiding l i g h t ,
and y e t SIMPLIFY i s t h e l a r g e s t of t h e f i v e p a r t s of t h e complete program.

SIMPLIFY i s a s u p e r v i s o r y program t h a t p a r c e l s t h e t a s k of s i m p l i f i c a t i o ~ iamong


f i v e sub-functions--SPLUS, STIMES, SQUOTIENT, SEXPT, and SMINUS--according to
t h e given a r i t h m e t i c o p e r a t o r involved. There i s no f u n c t i o n SDIFFERENCE, s i n c e
SIMPLIFY transforms an e x p r e s s i o n of t h e form

(DIFFERENCE a b)

i n t o an e x p r e s s i o n of t h e form

(PLUS (MINUS b) a )

The f u n c t i o n SMINUS s a t i s f i e s t h e s i m p l i f i c a t i o n requirements of t h e unary


o p e r a t o r MINUS t h a t i s generated by SIMPLIFY. Unary MINUS i s n o t a 1ega.l
o p e r a t o r i n t h e c o n s t r u c t i o n of t h e i n p u t polynomial.

The h i e r a r c h i c a l sub-functions c a l l e d by SIMPLIFY a l l adhere t o a number of


principles :
1. Each function assumes its arguments are in simplest fort11
by earlier evaluation of SIMPLIFY on these arguments
(lines 96 and 99).

2. Eqressions with constant arguments are always simplified


by arithmetic evaluation of the eqression using EVAL.

3. Except for constants, MINUS is always factored out of the


arguments of an expression.

4. SPLUS and STIMES always return their value in a standard


form, i.e., the first argument is always the constant, if
there is one,

5. The input expression is returned unchanged, if all


simplification rules fail.

( S I M P L I F Y (LAMBCA ( E l
(PROG ( A 8 C 0)
( C O N 0 ( ( A T O M E ) ( R E T U R N €1 1 )
(SETQ A ( S I M P L I F Y (CAPP E) 1 )
( C O N 0 ( ( E Q ( S E T O C ( C A R E l ) (QUOTE M I N U S ) )
(RETURN (SMINUS ( L I S T C A ) ) ) ) )
( S E T O B ( S I M P L I F Y (CADDR € 1 ) )
( C O N 0 ( ( E Q C (QUOTE D1FFEAENCE) 1
( R E T U R N ( S P L U S (LIST (QUOTE PLUS)
( S M I N U S ( L I S T (QUOTE MINUS) 0 ) 1 A ) ) ) 1 )
(SETO O ( L I S T C A 8 ) )
( R E T U R N ( S E L E C T C ( ( Q U O T E P L U S ) (SPLUS 0 ) 1
((QUOTE T I M E S ) I S T I M E S D l )
( (QUOTE QUOTIENT) (SOUOTIENT Q))
((QUOTE E X P I ) (SEXPT D l )
0 1))))
20.5.1 SPLUS

For a n e x p r e s s i o n of t h e form

(PLUS a b)

t h e following s i m p l i f i c a t i o n r u l e s a r e used by SPLUS. Higher-numbered r u l e s


assume p r i o r r u l e s f a i l e d .

Rule Value Line No.

1. a and b = c o n s t a n t a + b 111

3. b = O a
4. b = constant, a # constant (PLUS b a ) t
5. a = b (TIMES 2 a I t
6. a = (MINUS al)
b = (MINUS bl)
7. a = (MINUS a l ) , b = a l 0
8. a = (MINUS al) ,b # constant (PLUS b a)'
9. b = (MINUS b l ) , a = bl 0
10. b = (MINUS b l ) , a # c o n s t a n t (PLUS a b)'
11. a l l else (PLUS a b ) +

ISPLUS ( L A M B D A ( € 1
( C O N 0 ( (NUHBERP (CADDR € 1 )
(COND ( (NUMBERP (CADR E ) ) ( E V A L E 1 )
( I Z E R O P (CAODR € 1 ) 1CAOR € 1 )
( T ( C O L L E C T ( L I S T ( C A R € 1 (CAODR € 1 (CADR E ) ) ) ) ) )

( ( A N D (NUWBERP (CADR E)) (ZEROP ( C A D R E l 1 ) (CADOH E l 1


( ( E Q U A L (CAOR E ) (CADDR E l )
( C O L L E C T ( L I S T (QUOTE T I M E S ) 2 ( C A D 2 € ) ) ) I
( ( A N 0 (NOT (ATOM ( C A D H E ) 1 ) ( €0 ( C A A D R E ) (QUOTE EIINUS 1 )
(COND ( ( A N D (NOT ( A T O M (CADOR E 1 ) 1
(EQ (CAADDR E ) (QUOTE M I N U S ) 1 )
( L I S T (QUOTE MINUS)
( C O L L E C T (LIST ( C A R E l
L CADAOR E l
( C A D R (CADDR f Ill l ) I
( ( E U U A L (CADAOR El (CADDR € 1 ) 01
( 7 ( C O L L k C T ( L I S T ( C A R E l (CAUDR E l ( C A D R I ! ) ) ) ) ) )
( ( A N D (NOT ( A T O M (CADUR E 1 ) ) ( E Q (CAAODA € 1 ( Q U O I E M I N U S ) 1 )
( C O N 0 ( ( E Q U A L ( C A D R (CADOH k ) ) ( C A D R E l ) 0)
( T (COLLECT € ) ) ) I
( T (COLLECT E ) ) ) ) )

t ~ h ee x p r e s s i o n i s f u r t h e r s i m p l i f i e d by the f u n c t i o n COLLECT, which i s d e s c r i b e d


later.
20.5.2 STIMES

For an express<on of t h e form

(TIMES a b)

t h e f o l l o w i n g s i m p l i f i c a t i o n r u l e s a r e used by STIMES. Higher-numbered rules


assume p r i o r r u l e s f a i l e d .

Value L i n e No.

1. a and b = c o n s t a n t a * b 133

4. a = constant, b # constant (TIMES a bit


5. b e O 0

7. b = constant, a # constant (TIMES b a ) t


8. a = b (EXPT a 21tt
9. a = (MINUS al)
b = (MINUS bl) (TIMES al bl) t
10. a = (MINUS a l ) , b = al (MINUS (EXPT b 2 ) ) 147
11. a = (MINUS al), b # constant (TIMES b a ) t 149
12. b = (MINUS bl), a = bl (MINUS (EXPT a 2 ) ) 151
13. b = (MINUS bl), a # constant (TIMES a b)' 153
14. a l l else (TIMES a b)' 154

CSTIMES (LAMBDA ( € 1 13 1
(CON0 ( (NUMBERP (CADOH E 1 ) 132
(CON0 ( (NUMBERP (CADR E 1 ) ( € V A L € 1 133
( ( Z E R O P (CADDR € 1 ) 0) 134
( ( O N E P (CADDH E l ) (CAUR € 1 ) 135
( 1 (COLLECT ( L I S T (CAR € 1 (CAODR € 1 (CADR € ) ) ) ) I ) 136
((NUHBERP (CADK E ) 1 137
(COND ( I Z E H O P ( C A D R E l ) 0 1 13 8
( ( O N E P (CADK E l 1 lCADUR € 1 ) 139
( T (COLLECT € 1 ) ) ) 140
( ( E Q U A L (CADR € 1 (CADDR E 1 ) 141
ISEXPT ( L I S T (QUOTE E X P T ) ( C A O K E ) 2 ) ) 1 142
( ( A N D (NOT (ATOM (CADI2 E l ) ) ( € 4 (CAADR E ) (QUOTE M I N U S ) ) ) 143
(CON0 ( ( A N D (NOT (ATOM (CADOR € 1 1 1 144
(EO (CAADUR E ) ( Q U O T E M I N U S ) 1 ) 145
( C O L L E C T ( L 1 S T ( C A R E ) ( C A D A D R E ) (CAUR ( C A D O R E ) ) ) ) ) 146
( ( E Q U A L (CADADR € 1 (CADDH E l ) 147
( L I S T (QUOTE M I N U S ) ( L I S T (QUOTE EXPT) (CADOR € 1 2)1 ) 148
(1 (COLLECT ( L I S T ( C A R E l (CAUDR € 1 (CADR € 1 ) ) ) ) ) 149

t ~ h ee x p r e s s i o n i s f u r t h e r s i m p l i f i e d by t h e f u n c t i o n COLLECT, which i s
described l a t e r .
t t ~ h e e x p r e s s i o n i s f u r t h e r s i m p l i f i e d by t h e f u n c t i o n SEWT, which i s d e s c r i b e d
later.
-173-
( (AND ( N O T ( A T O M (CADDR E 1 ) 1 ( E d (CAADD3 E l ( Q U 3 T E MINUS) 1 ) 1 50
(COND ( ( E Q U A L (CADR (CADDR E l ) (CAUK € 1 1 151
( L I S T ( Q U O T E M I N U S ) ( L I S T ( Q U D T E E K P T ) ( C A D K E l 2))) 152
( T (COLLECT E l ) ) ) 153
( T (COLLECT E ) ) ) ) ) 154

20.5.3 COLLECT

COLLECT i s a f u n c t i o n common t o b o t h SPLUS and STIMES. It p r o v i d e s a d d i t i o n a l


s i m p l i f i c a t i o n r u l e s a p p l i c a b l e t o both functions. COLLECT a t t e m p t s t o s i m p l i f y
a n e q r e s s i o n i f i t matches v a r i o u s p a t t e r n s of n e s t e d PLUS o r TIMES o p e r a t o r s .

Rule L i n e No.

1. expression = atom expression 156


2. eqression = (operator a b)
a = atom, b = atom (operator a b) 158
3. expression = ( o p e r a t o r a b )
a # atom, b = atom (operator b a )
t
4. (PLUS a (PLUS b c ) )
a and b = constant (PLUS a +b c) 161
5. (TIMES a (TIMES b c ) )
a and b = constant (TIMES a * b c) 161
6. (PLUS (PLUS a b ) (PLUS c d ) )
a and c = constant (PLUS a +c (PLUS b d ) ) 166
7. (TIMES (TIMES a b ) (TIMES c d ) )
a and c = constant (TIMES a * c (TIMES b d ) ) 166
8. a l l else expression 1 7 1 and 172

( C O L L E C T ( LAMBDA ( E 1 155
(COND ( ( A T O M E l E l 156
( ( A T O H (CADOR El) 157
ICOND ( ( A T O M (CAUR E l l € 1 158
( 1 ( C O L L E C T ( L I S T ( C A R € 1 (CADDR € 1 ( C A D R € 1 ) ) ) ) ) 159
( ( A N D ( E Q (CAR € 1 (CAADDR k ) ) (NUMBERP ( C A D R ( C A U D H € 1 11 1 160
(CQNO ( (NUMUERP (CAOR E l J 16 1
( L I S T (CAR E ) 162
( € V A L ( L I S T ( C A R € 1 (CADR € 1 (CADR (CADUR € 1 ) ) ) 163
(CADDR (CAOUR E l ) ) ) 164
( ( A T O M (CADR € 1 ) € 1 165
( ( A N D ( E Q (CAR E ) [CAADR E l ) (NJHBERP (CADADH E l 1 ) 166
( L I S T (CAR € 1 167.
( € V A L ( L I S T ( C A R € 1 (CADADR € 1 ( C A D K (CADDR E l ) ) ) 168
( L I S T ( C A R E ) (CAOOR (CADH E J 1 169
(LAUUK [CAODR E l ) ) 1 ) 170
(T El)) 17 1
(1 E ) ) ) ) 172

he expression i s f u r t h e r s i m p l i f i e d by a r e c u r s i v e c a l l t o COLLECT.
-174-
20.5.4 SQUOTIENT

SQUOTIENT attempts some minor simplifications. If they fail, the form

(QUOTIENT a b)

is transformed into the form

(TIMES a (QUOTIENT 1 b ) )

and a call to STIMES is made with the new expression. SQUOTIENT does not make
a zero-divide check, i.e., b is assumed # 0.

Rule Value Line No.


1. a = b
2. a = O
3. a = l (QUOTIENT a b) 176
4. b = l
5. a and b = constant
6. b = constant, a # constant
7. b = (MINUS bl) (TIMES a (MINUS
(QUOTIENT 1 b,)))tt 182
I
8. all else (TIMES a (QUOTIENT 1 b)Itt 187

( S Q U O T I E N T ( L A M B D A (E) 173
(COND ( ( E Q U A L ( C A D R € 1 (CADDR E 1 ) 1 ) 174
( ( A N D (NUWBERP (CADR E l (ZEROP (CADR € 1 1 ) O ) 175
[ ( A N D (NUMBERP (CADR € 1 ) ( O N E P ( C A D R € 1 ) 1 E l 176
( (NUMBEKP (CADDR E 1 ) 17 7
( C O N 0 ( (NUHBERP (CADR E 1 ) ( €VAL E 1 1 178
( ( O N E P (CADDR €11 (CADR E l 1 479
( T (COLLECT ( L I S T (QUUTE T I M E S ) 180
( Q U O T I E N T 1.0 (CADDR E l ) (CADR E l ) ) ) ) ) 181
( ( A N D (NOT ( A T O M (CADDR € ) ) I ( E Q (CAADD3 E ) (QUOTE M I N U S ) 1 ) 182
( S T I M E S ( L I S T (QUOTE T I M E S ) (CADR € 1 183
( L I S T (OUGTE MINUS) 184
( L I S T (QUOTE QUOTIENT) 185
1 ( C A D R (GADDR € 1 ) ) ) 1 ) 1 186
( T ( S T I M E S ( L I S T (QUOTE T I M E S J ( C A D R € 1 187
( L I S T (QUOTE Q U O T I E N T ) 1 (CAODR E 1 ) 1 ) 1 ) l 188

he expression is further simplified by the function COLLECT.


he eqression is further simplified by the function STIMES.
20.5.5 SEXPT

For a n expression of t h e form

(EXPT a b )

t h e f o l l o w i n g s i m p l i f i c a t i o n r u l e s a r e used by SEWT. The argument b i s assumed


t o be constant (i.e., NUMBERP i s n o t used t o g u a r a n t e e t h i s f a c t ) . 0' is
d e f i n e d e q u a l t o one.

Rule Value L i n e No.

2. b - 1
3. a and b = constant
4. a=atom (EXPT a b) 193
5. a = (EXPT a l bl) (EXPT al b * bl) 194
(EWT a l b)
t 198
6. a = (MINUS a l l , b = even
4-
7. a = (MINUS a l l , b = odd (MINUS (EWT al b ) ' ) 200
8. a l l else (EXPT a b ) 197

(SEXPT (LAMBOA ( € 1
(COND ( (ZEROP (CADDR E ) 1 1
( ( O N E P (CADUR E l ) (CAOR E l )
( (NUHBERP (CADR E 1 1 ( € V A L € 1 )
( ( A T O M (CAOR E l ) € 1
(EQ (CAAOR E) (QUOTE E X P T ) )
( L I S T (QUOTE E X P T ) (CADADH El
( T I M E S (CADOR € 1 (CADOR (CAOK E ) ) ) ) )
( ( N O T ( E Q (CAADK E l ( Q U U l t M I N U S ) ) ) E )
( ( E V E N P (CAUDR € 1 )
(SEXPT ( L I S T (QUOTE E X P T ) (CA0AOR € 1 (CADDR E) 1)
( 1 ( L I S T (QUOTE M I N U S )
(SEXYT ( L I S T (QUOTE E X P T ) (CAOADR € 1 (CADUR € 1 ) ) ) ) ) ) )

20.5.6 SMINUS

The r u l e s below a p p l y t o a n expression of t h e form

(MINUS a )

h he expression i s f u r t h e r s i m p l i f i e d by a r e c u r s i v e c a l l t o SEXPT.
Rule Value L i n e No.

1. a = constant
2. a = (MINUS al) a
1
3. a l l else (MINUS a )

fSMINUS (LAMBDA ( E l
(CON0 ( (NUMBERP (CADH E 1 ) l EVAL E 1 )
( (AND (NOT ( A C O M (CADK El 1 )
( € 0 (CAADR E ) (QUOTE M I N U S ) ) ) (CADAOR E l )
(T €))I)

T r a n s l a t i n g from p r e f i x t o i n f i x n o t a t i o n i s c o n s i d e r a b l y simpler t h a n v i c e
v e r s a , and PRE2IN i s a s i m p l e r f u n c t i o n than IN2PRE. There a r e only two
problems: (1) determining when t o p a r e n t h e s i z e a n expression t o remove
ambiguity, and (2) p r i n t i n g t h e expression i n a " p r e t t y " i n f i x form.

The f i r s t problem i s solved by t a k i n g cognizance of t h e precedence o r binding


s t r e n g t h of a d j a c e n t o p e r a t o r s . When p r i n t i n g a n s q r e s s i o n whose o p e r a t o r has
h i g h e r precedence t h a n t h e o p e r a t o r of a n argument, t h e argument i s p a r e n t h e s i z e d .
For example, t h e expression

(TIMES A (PLUS B C))

should p r i n t a s

whereas

(PLUS A (TIMES B C))

should p r i n t a s

s i n c e TIMES has h i g h e r precedence t h a n PLUS. For our problem, o p e r a t o r prece-


dence, from high t o low, i s a s l i s t e d below:

EXPT
TIMES, QUOTIENT
PLUS, MINUS
-177-
where TIMES and QUOTIENT (and PLUS and MINUS) have e q u a l precedence. The
f u n c t i o n WRAP provides t h e p a r e n t h e s e s when c a l l e d .

The second problem of p r i n t i n g t h e expression i n a " p r e t t y " format i s s i d e s t e p p e d


t o some degree by PRE2IN, p a r t i c u l a r l y f o r t h e c a s e

(TIMES constant (QUOTIENT 1 A) )

I f t h e constant were 3, t h e i n f i x eacpression could p r i n t a s

which i s i n c o r r e c t . The s o l u t i o n I ' v e chosen i s t o always i n s e r t a s p a c e


f o l l o w i n g a constant ( l i n e 209). A b e t t e r p r i n t i n g a l g o r i t h m could y i e l d

b u t t h i s i s n o t done h e r e .

The s t r u c t u r e of PRE2IN i n c l u d e s t h e s u p e r v i s o r y program PRE2IN and s i x sub-


functions. They a r e simple enough t o b e l e f t t o t h e r e a d e r t o f o l l o w t h e
code d i r e c t l y . Note, however, t h e u s e of t h e c h a r a c t e r o b j e c t s PLUSS, SLASH,
DASH, UPARROW, LPAR, RPAR, and BLANK, whose v a l u e s p r i n t a s t h e d e s i r e d
characters.

( P R E 2 I N (LAMBDA ( € 1
(PROG ( 1
(CON0 ((NUMBERP € 1 (PHOG2 ( P R I N E ) ( P K I N B L A N K ) ) )
((ATOM € 1 [ P R I N E l )
( T (SELECT (CAK E)
((QUOTE P L U S ) (XPLUS € 1 )
((UUUTE MINUS) (XHINUS E ) )
((QUOTE T I M E S ) ( X T I M E S E l )
( ( Q U O T E O U O T I E N T ) (XQUOTIENT E l
( ( U U u T E E X P T ) (XLXPT E l ) € 1 ) ) 1 ) )

I X P L U S (LAMBDA ( € 1 217
(PROG ( 1 2 18
(CON0 ( (NUMBERP (CADK E 1 1 2119
(RETURN (XPLUS ( L I S T (CAN E l (CADUR E l [CAUK € 1 ) ) ) ) ) 220
( P R E 2 I N (CADR E l 1 221
( P R I N BLANK) 222
(CON0 ( ( A N D (NOT (ATOM (CADDR E l ) ) ( E Q (CAADDR € 1 (QUOTE M I N U S ) ) ) 223
(GO X I ) 224
( ( A N D (NUMBERP (CADDR E l ) ( M I N U S P (CADDR E ) ) ) ( 6 0 X I ) ) 225
( P R I N PLUSS) 226
( P R I N BLANK) 227
X ( P R E Z I N (CADDR € ) ) I ) ) 22 8

( X M I N U S (LAHBDA ( € 1
(PROG ( 1
( P R I N DASH)
( P R I N BLANK)
( P R E 2 I N (CADR E 1 ) 1 ) 1
( X T I H E S (LARBDA ( € 1
(PROG ( X I
(SETO X (QUOTE ( P L U S M I N U S ) 1 )
(CON0 ( ( A T O M (CADR E l ) ( P R I N (CAOH E l ) )
( ( M E M B E R (CAADR E ) X ) (WRAP LCAOR € 1 ) )
( T ( P R E Z I N (CADR E ) ) ) )
(COND ( (ATOH (CADDA E ) LPRIN t CADDR E ) 1 )
((MEMBER (CAADDR € 1 X ) (WRAP (CADOR E l ) )
( T ( P R E 2 I N (CADDR € ) ) I ) ) ) )

(XQUOTIENT (LAMBDA ( E l
(PROG ( X )
( S E T Q X (QUOTE ( P L U S M I N U S ) ) )
(CON0 ( ( A T O M ( C A D R € 1 ) ( P R I N (GADR € 1 ) )
((MEMBER (CAADR E l X I ( W R A P ( C A O R € 1 ) )
( T ( P R E 2 I N (CAOR € 1 ) 1 )
( P R I N SLASH)
(CON0 ( (ATOM (CADOR E ) ) ( P R I N (CAODR € 1 ) 1
( (MEMBER ( CAADDR E 1 X ) ( M ~ ~ A(CADDRP E 1) )
[ T I P R E 2 I N (CADDR E ) ) ) ) ) ) )

(XEXPT (CAHBDA ( E )
(PROG 0
(CON0 ( ( A T O M (CADR E l ) ( P R I N (CADK E l )
( T (WRAP (CADR € ) ) ) I
( P R I N UPARROW)
( P R I N (CADDR € ) ) I ) )

(WRAP (LAMBOA (TERM)


(PROG (
( P R I N LPAR)
( P R E Z I N TERM)
(PRIN RPAR))))

20.7 DIFF

DIFF is the supervisor for the complete program. It controls the evaluation of
the previously discussed functions, and input-output.

MAPCAR is used (line 267) to print the output message

THE DERIVATIVE OF-

whereupon, control is given to the function UADER (line 281) to return a list
of all the non-blank characters of the infix expression to be differentiated.
To satisfy the demands of READER, the infix eqression must be terminated by
a comma (line 286). If the expression ends with a period, READER returns the
value END (line 285) and DIFF exits to EVALQUOTE with the value FINIS (line 272).
If the expression returned by READER can't be translated by INZPRE, INZPRE
prints the message

(POORLY FORMED EXPRESSION)


and DIFF t r i e s a g a i n ( l i n e 273). I f t h e t r a n s l a t i o n i s s u c c e s s f u l , DIFF p r i n t s
t h e message

WITH RESPECT TO-

and READER i s c a l l e d a g a i n ( l i n e 276) t o read t h e variable of d i f f e r e n t i a t i o n ;


i t must be a l i t e r a l atom.

The message

IS-

i s p r i n t e d and DIFF c a l l s DERIV, SIMPLIFY, and PRE2IN i n o r d e r t o perform


d i f f e r e n t i a t i o n , s i m p l i f i c a t i o n , and i n f i x t r a n s l a t i o n , r e s p e c t i v e l y ( l i n e 278).
S i n c e PRE2IN p r i n t s i t s r e s u l t d i r e c t l y , DIFF j u s t r e p e a t s t h e loop f o r a n o t h e r
expression.

Note t h e u s e of t h e $ $ - a r t i f a c t t o p r i n t non-parenthesized o u t p u t messages.


The f r e e v a r i a b l e DIGITS, noted e a r l i e r , i s CSET t o a l i s t of p a i r s of l i t e r a l
atoms and numeric atoms f o r each d i g i t . The f u n c t i o n DIGIT u s e s ASSOC ( l i n e 8)
t o s e a r c h t h i s l i s t f o r e q u a l i t y of t h e l i t e r a l atom r e t u r n e d by READCH, and
t h e CAR of each p a i r , e.g., $$SO$, $$$I$, e t c . When t h e e q u a l i t y i s s a t i s f i e d ,
i.e., ASSOC r e t u r n s a non-NIL v a l u e , t h e CADR of t h e p a i r y i e l d s t h e numeric
v a l u e of t h e l i t e r a l atom read ( l i n e 1 0 ) .

( 0 IFF (LAMBDA ( 1
(PROG ( X Y )
A (TERPRI)
(MAPCAR (QUOTE (THE D t R I V A T I V E * $ $ O F - $ 1 1
( F U N C T I O N (LAMBOA ( J ) ( P R O G Z ( P R I N J ) ( P H I N B L A N K ) 1 ) ))
(TERPRI 1
(TEREAD)
(SET0 X (KEADERIJ
(COND ( (EQUAL X (QUOTE END 1 (RETURN (QUOTE F I N I $1 1
( ( N U L L (SETQ X ( I N 2 P R E X I ) ) (GO A ) ) )
B ( P R I N T (QUOTE $ $ $ WITH RESPECT TO-$))
(BEREAD)
(COND ( (NOT (ATOM ( S E T U Y ( C A R ( R € A D E K ) ) 1 ) (GO 8 ) 1 )
( P R I N T (QUOTE $$$IS-$))
( P R E 2 I N (SIMPLIFY (DEHIV X Y ) ) )
(TERPRI
(GO A ) ) ) )

(READER (LAMBDA ( 1
(PROG ( X Y )
A (SETQ X (HEADCH) 1
(COND ( ( E Q X B L A N K ) ( G O A ) 1
((EQ X P E R I O D ) (RETURN ( O U b T E E N D ) ) )
( ( E Q X COMMA) (&ETURN ( R E V E R S E Y ) ) ) )
(SETQ Y (CONS X Y)
(GO A ) ) ) )
With the program now completely described, we can examine a few t y p i c a l examples
of i t s operation.

DIFF ( )
THE DERIVATIVE OF-

WITH RESPECT TO-

THE DERIVATIVE OF-


x+3,
WITH RESPECT TO-
x,
IS-
1

THE DERIVATIVE OF-


3x43,
WITH RESPECT TO-
x,
IS-
9 X+2

THE DERIVATIVE OF-


3x42-2X-4,
WITH RESPECT TO-
x,
IS-
6 X -2

THE DERIVATIVE OF-


3X+3 + 2X+2+X+3,
WITH RESPECT TO-
x,
IS-
9X+2+4X+1
-181-
TKE DERIVATIVE OF-
3(X+2 + X) + 2X+3,
WITH RESPECT TO-
x,
IS-
3 ( 2 X t - 1 ) +6X+2

THE DERIVATIVE OF-


(X+Y) * (X-Y)
WITH RESPECT TO-
x,
IS-
X - Y + X + Y

Since t h e i n p u t expression i n t h i s example was not i n s i m p l e s t form, t h e answer


i s not i n s i m p l i f i e d form.

THE DERIVATIVE OF-


B+B) * (A-B) ,
(POORLY FORMED EXPRESSION)

THE DERIVATIVE OF-


.
FINIS
APPENDIX A
EXERCISE ANSWERS

CHAPTER 2.

1. Yes
2. No. T h i s i s a n example of two l i t e r a l atoms.
3. Yes
4. Yes
5. No. There a r e no p a r e n t h e s e s i n a n atomic symbol.
6. Yes
7. Yes
8. Yes
9. No. This i s a dotted p a i r .
10. No. F i r s t character not a l e t t e r . (On some systems t h i s may b e a n
a c c e p t a b l e l i t e r a l atom because i t i s obviously n o t a number.)
11. No. P a r e n t h e s e s missing.
12. No. P a r e n t h e s e s missing.
13. No. Too many d o t s w i t h o u t p r o p e r p a r e n t h e s e s .
14. Yes
15. Yes

rfiTWO THREE

Eel
THREE NIL
19. (A. (B . ((C . NIL) . (D . N I L ) ) ) )
20. (((A. B) . ( C . D)) . ( E . ( ( F . G) . H ) ) )

CHAPTER 3 .

1. (ATOM . NIL)
2. ( ( L I S P . NIL) . NIL)
3. (((MORE . (YET . NIL)) . NIL) . NIL)
4. (HOW . (ABOUT . (THIS . N I L ) ) )
5. (DONT . ((GET . ((FOOLED . NIL) . NIL)) . NIL))
6. (XI)
7. (NIL X1)
8. (KNOW THY SELF)
9. ( (BEFORE AND AFTER))
10. (A ((B C ) ) )
11. ((W)) ((W . NIL) . NIL)
12. (NIL NIL NIL) = . (NIL . (NIL . N I L ) ) )
(NIL
(W (B) B) = (W . ( ( B . NIL) . (B . NIL)))

-
13.
14. ((((NEST)))) ((((NEST . NIL) . NIL) . NIL) . NIL)
15. ( ((A) B) (C) D) ( ( ( A . NIL) . (B . NIL)) . ((C . NIL) . (D . NIL)))

CHAPTER 4 .

1. Y e s
2. Y e s (5 E)
3. Y e s (E5 . 5)
4. Yes ( 1 . 0 . 1Q)
3
5. Yes 43 is a literal atom and not 0 X 8
6. Yes 4.4
7. No
8. Yes (B . 9.9)
9. No

Note that the use of blanks to delimit the "CONS dot"


in 7 and 9 would make these legal S-expressions.

10. Yes (1.23 7743 27 2700000 3.213-8 ALPHA. Q . 32)


11. (99.9)
12. (NIL 99.9) .
13. Not a legal S-expression as there are too many dots.
14. (5 5.5 545 5.5 500)
15. ( (13.13) (2542) )

CHAPTER 5.

LEFT
RIGHT
(LEFT . RIGHT)
A
(A)
A
(A B)
(SENTENCE IS A LIST)
((ABOUT THIS))
((DOT . PAIR2))
(CAR . CDR)
NIL E ( )
(CDR)
(CAR)
(A)
.
-
(754 100)
1
(2.0 3.0 774) . (2.0 . (3.0 . 774))
((A B))
((((A;LPHA))))
T h e r e l a t i o n s h i p among CONS, CAR, a n d CDR i s t h a t CONS p u t s t o g e t h e r t h a t
w h i c h CAR and CDR tear a p a r t . M o r e exactly, if t h e a r g u m e n t t o CAR a n d t o
CDR i s t h e s a m e S - e x p r e s s i o n , s , a n d t h e t w o a r g u m e n t s t o CONS are t h . e values
of t h e CAR a n d CDR of t h i s S - e x p r e s s i o n , then t h e value o f CONS i s t h e
o r i g i n a l S-expression, i.e., (CONS (CAR s) (CDR s ) ) = s.
CAR CDR CAR = CADAR
CAR CAR = CAAR

-
CAR CDR = CADR
CDR CDR
CAR CDR CDR = CADDR
CAR CDR CDR CDR = CADDDR
CAR CAR = CAAR
CDR CAR = CDAR
CAR CAR CAR CDR = CAAADR
CAR CAR CDR = CAADR
CAR CDR CAR CDR = CADADR

-
V a r i-
able Binding

X ATOM
Y (LIST)
J (THREE ELEMENT L I S T )
K (THREE ELEMENT L I S T )
U VERY
v GOOD
Y ONE
X (THEN . ANOTHER)
A (A (B 7742))
VARIABLE ((A B))
J NIL
empty empty
U ALPHA
V BETA
Variable B i n d inq

BETA
ALPHA
ALPHA
BETA
ALPHA
u BETA
15. FIRST (FIRST)
SECOND SECOND

CHAPTER 7 .

1. ATOM
2. (LIST)/
3. THREE
4. (ELEMENT L I S T )
5. (VERY . GOOD)
6. (ONE THEN . ANOTHER) (ONE . (THEN . ANOTHER))
7. B,
8. (B) -
9. 3.14159
10. 3.14159
11. ALPHA.
12. BETA
13. BETA
14. ALPHA
15. FIRST

CHAPTER 8.

2. LIST-]
3. NIL
4. 43
5. NUMBER
6 Y
7. (((LIST)))
(A B)
(LAMBDA (J) (CAR (CDR (CDR J ) ) ) ) ( (1 2 3 4) ) = 3
(LAMBDA (X) (CAR (CAR X ) ) ) ( ( (A B C) D) ) = A
(LAMBDA (Y) (CAR (CDR Y ) ) ) ( ( ( A B C) D) ) = D
(LAMBDA (2) (CDR (CAR Z) ) ) ( ((A B C ) D) ) = (B C)
(LAMBDA (VARIABLE) (CAR (CDR (CAR VARIABLE) ) ) ) ( ((A B C) D) ) := B
(A C)
(A C)
(A)
(C)
(0 A) (D C ) )

C W T E R 9.

X
J
(AN S EXPRESSION)
A
(J)
(QUOTE . EXPR)
CAR ( ( A . BETA)) = A
(NOW I S THE TIME FOR ALL GOOD MEN TO COME TO THE AID OF THE PAJXTY)
(A B)
(LAMBDA (X) X)
(ONE TWO THREE)
(ONE TWO THREE)
(NIL F NIL F NIL F )
( F F F F F F )
( ( N I L F F ) (T T T ) (NIL N I L N I L ) (123 1 2 3 123))
X
J
(AN S EXPRESSION)
A
(J)
21. ABLE
22. (ABLE QUOTE ABLE)
23. EVAL ((CAR (QUOTE (ABLE))) ) = ABLE
24. (CONS A B)
25. EVAL ((CDR (QUOTE (A B ) ) ) ) = (B)

CHAPTER 10.

1. DEFINE ( ( (FIRST (LAMBDA (X) (CAR X ) ) ) ))


F I R S T ( (A B C D E) ) = A

2-4. DEFINE ( (
(SECOND (LAMBDA (Y) (CADR Y) ) )
(THIRD (LAMBDA (Z) (CAR (CDDR Z ) ) ) )
(CADDDDR (LAMBDA (J) (CAR (CDDDDR J ) ) ) )
>)
SECOND ( (A B C D E) ) = B
THIRD ( (A B C D E) ) = C
CADDDDR ( (A B C D E) ) = E

5. DEFINE ( ( (REVDOT (LAMBDA (J) (CONS (CDR J ) (CAR J ) ) ) ) ) )


REVDOT ( (A . B) ) = (B . A)
REVDOT ( ((A) (B)) ) = ((B) (A)) = ((B) A)
REVDOT ( ( ( ( F I R S T ) ) . (LAST)) ) = ((LAST) . ((FIRST))) = ((LAST) (FIRST))

C W T E R 11.

1. TRUE
2. 1
3. U n d e f i n e d , as there i s n o non-NIL c o n d i t i o n a l clause.
4. (TI
5. NOP
6. DEFINE ( ( (OR2 (LAMBDA (X Y) (COND (X T) (Y T I (T N I L ) ) ) ) ))
7. DEFINE ( ( (XOR2 (LAMBDA (X Y)
(COND (X (COND (Y NIL) (T T ) ) ) (Y T) (T N I L ) ) ) ) ))
8. DEFINE ( ( (AND2 (LAMBDA (X Y)
(COND (X (COND (Y T ) (T N I L ) ) ) (T NIL) ) ) ) ) )
9. (SELECT T (pl el) ( p 2 e2) ... (pn en) (error))
lo. (COND ((EQUAL p pl) el) ((EQUAL p p 2 ) e2) . ..
((EQUAL p pn) en) (T e ) )

CHAPTER 1 2 .

1. ( T F T F )

4. NIL

6. NIL

8. NIL

10. NIL
11. NIL

14. T
15. N I L , since HEAR i s n o t a t o p - l e v e l m e m b e r of l i s t (NOW (HEAR T H I S ) ) .
R a t h e r , HEAR i s a member of t h e s u b l i s t (HEAR T H I S ) , and MEMBER
tests o n l y f o r e l e m e n t s a t t h e t o p level of a l i s t .
16. T
17. DEFINE ( ( (EQUIV (LAMBDA (X Y) (EQ X Y ) ) ) ) )
18. DEFINE ( ( (IMPLIES (LAMBDA (X Y) (OR (EQ X Y) Y) ) ) ) )
19. T h i s p r o g r a m can be e a s i l y w r i t t e n w i t h c o n d i t i o n a l s and recursion.
H o w e v e r , s i n c e t h e s t u d e n t has n o t learned these techniques, t h e f o l l o w i n g
e x p r e s s i o n i s r e q u i r e d (see exercise 1 9 of C h a p t e r 1 4 f o r recursive s o l u t i o n ) :

DEFINE ((
(IMSEQ (LAMBDA (J)
( (LAMBDA (V W X Y Z ) (AND
(AND (NUMBERP V) (NUMBERP W) (NUMBERP X) (NUMBER2 Y) (NUMBERP Z ) )
(OR (AND (LESSP V W) (LESSP W X) (LESSP X Y) (LESSP Y Z ) )
(AND (GREATERP V W) (GREATERP W X) (GREATER2 X Y) (GREATEFU? Y Z ) ) ) ) )
(CAR J ) (CADR J ) (CADDR J ) (CADDDR J ) (CAR (CDDDDR J ) ) ) ) )
Note t h a t t h e u s e of n e s t e d lambda e x p r e s s i o n s p e r m i t s u s t o bind V t o
t h e f i r s t l i s t element, W t o t h e second, X t o t h e t h i r d , e t c . This
p r a c t i c e c r e a t e s temporary s t o r a g e f o r t h e s e p a r t i a l r e s u l t s and s i m p l i f i e s
t h e t o t a l e x p r e s s i o n a s w e l l a s reduces t h e t o t a l computation, s i n c e we
need compute t h e s e r e p e a t e d l y used arguments o n l y once.

20. DEFINE ( ( (EQN (LAMBDA (X Y) (OR (EQ X Y)


(AND (NUMBEWP X)
(NUMBERP Y)
(ZEROP (DIFFERENCE X Y ) ) ) ) ) ) ) )

CHAPTER 13.

1 i.e., a number-theoretic remainder f o r f i x e d - p o i n t arguments.


9.31322574613-10 i.e., f l o a t i n g - p o i n t r e s i d u e f o r f l o a t i n g - p o i n t arguments.
(18 1 )
(18.333333333 9.31322574613-10)
(18 1 )
123
-123
0
0
5.0
0.333333333
0
lm2345678900E+8
3.14159
25. 777774
26. 7173754
27. 7654354
28. 7153354
29. 123454
30. 20441
31. 1641
32. 34Q
33. DEFINE ( ( (TRIPLE (LAMBDA (X) (PLUS X X X ) ) ) ))
34. DEFINE ( ( (CUBE (LAMBDA (X) (TIMES X X x)) ) ) )
35. DEFINE ( ( (SIMPLEINTEREST (LAHBDA (PRINCIPAL RATE YEARS)
(TIMES PRINCIPAL (ADD1 (TIMES YEARS R A T E ) ) ) ) ) ))
36. DEFINE ( ( (ANNUALCOMPOUND (LAMBDA (P R Y)
(TIMES P (EXPT (ADD1 R) Y ) ) ) ) ))
37. DEPINE ( ( (TIMECOMPOUND (LAMBDA ( P R Y T )
(TIMES P (EXPT (ADD1 (QUOTIENT R T ) ) (TIMES T Y ) ) ) ) ) ))
38. DEFINE ( ( (TWOBY (LAMBDA ( A l l A 1 2 A 2 1 A 2 2 )
(DIFFERENCE (TIMES A l l A22) (TIMES A 1 2 A 2 1 ) ) ) ) ))
39. DEPINE ( ( (THREEBY (LAMBDA ( A l l A 1 2 A 1 3 A 2 1 A 2 2 A 2 3 A 3 1 A 3 2 A33)
(PLUS (TIMES A l l (TWOBY A 2 2 A 2 3 A 3 2 A 3 3 ) )
(MINUS (TIMES A 1 2 (TWOBY A 2 1 A 2 3 A 3 1 A 3 3 ) ) )
(TIMES A 1 3 (TWOBY A 2 1 A 2 2 A 3 1 A 3 2 ) ) ) ) ) ))
40. DEFINE ( ( (SOLVE (LAMBDA ( A l l A 1 2 A 1 3 A 2 1 A 2 2 A 2 3 A 3 1 A 3 2 A 3 3 C 1 C 2 C3)
((LAMBDA ( U 1 U2 U3 D) ( L I S T (CONS (QUOTE U l ) (QUOTIENT U 1 D ) )
(CONS (QUOTE U2) (QUOTIENT U2 D ) )
(CONS (QUOTE U3) (QUOTIENT U3 D ) ) ) )
(THREEBY C 1 A 1 2 A 1 3 C 2 A 2 2 A 2 3 C 3 A 3 2 A 3 3 )
(THREEBY A l l C 1 A 1 3 A 2 1 C2 A 2 3 A 3 1 C 3 A33)
(THREEBY A l l A 1 2 C 1 A 2 1 A 2 2 C 2 A 3 1 A 3 2 C3)
(THREEBY A l l A 1 2 A 1 3 A 2 1 A 2 2 A 2 3 A 3 1 A 3 2 A 3 3 ) ) ) ) ))
1. SOLVE ( 2 1 -2 1 1 1 -1 -2 3 -6 2 1 2 ) = ( ( U l . 1) (U2 . -2) (U3 . 3))
2. SOLVE ( 2 1 - 2 2 1 3 -1 -2 3 5 6 1 2 ) = (U1 . 7) (U2 . -9) .
( ~ 3 0))
3. SOLVE (15 15 15 7 1 -100 -50 1 1 1 5 -100 - 1 6 ) = ((Ul . 0) (U2 . 0) (U3 . 1))
4. SOLVE (1 2 -2 1 1 1 - 2 - 1 3 -12 6 2) = ( ( U l . 8) (U2 . -6) (U3 . 4 ) )
5. SOLVE (-2 2 1 1 1 1 3 -1 -2 - 2 4 49 9) = ( ( U l . 2 2 ) (U2 . -5) (U3 . 3 2 ) )
CHAPTER 1 4 .

NIL
2. X
E
NO
L
3. DEFINE ( ( (TWIST (LAMBDA ( S ) (COND ((ATOM S ) S)
(T (CONS (TWIST (CDR S ) )
(TWIST ( C U S ) ) ) ) ) ) ) 1)
A
(B A)
(C . ( B . A ) ) = (C B . A)
(((NIL . C) . B) . A)
(NIL . (B . A)) = (NIL B . A)
4. DEFINE ( ( (SUM (LAMBDA (X Y) (COND ((ZEROP Y) X) (T (SUM (ADD1 X) (SUB1 Y ) ) ) ) ) ) ))
ARGS OF SUM
1
2
ARGS OF SUM
2
1
ARGS OF SUM
3
0
VALUE OF SUM
3
VALUE OF SUM
3
VALUE OF SUM
3
D E F I N E ( ( (PROD (LAMBDA (X Y) (COND ((ZEROP Y) 0)
(T (SUM X (PROD X (SUB1 Y ) ) ) ) 1)) ))
D E F I N E ( ( (REMXY (LAMBDA (X Y) (COND ((LESSP X Y) X)
((EQUAL X Y) 0)
(T (REMXY (DIFFERENCE X Y) Y ) ) ) ) ) ))
D E F I N E ( ( (COUNT (LAMBDA (E)
(COND ((NULL E ) 0) ((ATOM E ) 1)
( T (PLUS (COUNT (CAR E ) ) (COUNT (CDR E) ) ) I ) 1) ) )
D E F I N E ( ( ( F I B B (LAMBDA (N)
(COND ( (ONEP N) 1) ((EQUAL 2 N) 1)
( T (PLUS ( F I B B ( S U B 1 N ) ) (FIBB (DIFFERENCE N 2) ) ) ) ) ) ) ) )
D E F I N E ( ( (GCD (LAMBDA (X Y) (COND ((GREATERP X Y) (GCD Y X ) )
((ZEROP (REMAINDER Y X) ) X)
( T (GCD X (REMAINDER Y X) ) ) ) ) ) ) )
D E F I N E ( ( (AMONG (LAMBDA (A L ) (COND ((NULL L ) N I L )
( ( E Q A (CAR L ) ) T )
(T (AMONG A (CDR L ) ) ) ) ) ) 1)
D E F I N E ( ( ( I N S I D E (LAMBDA ( A E ) (COND ((ATOM E ) (EQ A E ) )
( ( I N S I D E A (CAR E ) ) T )
( T ( I N S I D E A (CDR E ) ) ) I ) ) 1)
D E F I N E ( ( (COPYN (LAMBDA (X N) (COND ( (ZEROP N) N I L )
(T (CONS X (COPYN X (SUB1 N ) ) ) ) ) ) ) ))
D E F I N E ( ( (LENGTHS (LAMBDA ( L ) (COND ((NULL L ) 0)
( T (ADD1 (LENGTHS (CDR L ) ) ) ) ) ) ) ))
D E F I N E ( ( (UNIONS (LAMBDA (X Y) (COND ((NULL X) Y)
((MEMBER (CAR X) Y) (UNIONS (CDR X) Y ) )
(T (CONS (CAR X) (UNIONS (CDR X) Y) ) ) ) ) ) ) )
DEFINE ( ( (INTERSECT (LAMBDA (X Y) (COND ((NULL X) NIL)
( (MEMBER (CAR X) Y) (CONS (CAR X)
(INTERSECT (CDR X) Y ) ) )
( T (INTERSECT (CDR X) Y ) ) ) ) ) ) )
D E F I N E ( ( (REVERSAL (LAMBDA (L) (COND ((NULL L ) N I L )
(T ( M P E N D (REVERSAL CCDR L ) )
( L I S T (CAR L ) ) ) ) ) ) ) 1)
17. DEFINE ( ( (PAIRS (LAMBDA ( L 1 L 2 ) (COND ((NULL L 1 ) NIL)
(T (CONS (CONS (CAR L 1 ) (CAR L 2 ) )
(PAIRS (CDR L 1 ) (CAR L 2 ) ) ) ) ) ) ) 1)
18. DEFINE ( ( (DELETE (LAMBDA (A L ) (COND ((NULL L ) NIL)
((EQ A (CAR L ) ) (DELETE A (CDR L ) ) )
(T (CONS (CAR L) (DELETE A (CDR L) ) ) ) ) ) ) ) )
19. DEFINE ( (
(INSEQ (LAMBDA (L) (OR (INSEQA L ) (INSEQA (REVERSE L ) ) ) ) )
(INSEQA (LAMBDA (L) (COND ((NULL L ) T)
((NULL (CDR L ) ) T)
((NOT (NUMBERP (CAR L ) ) ) N I L )
((NOT (NUMBERP (CADR L ) ) ) N I L )
((LESSP (CAR L ) (CADR L ) ) (INSEQA (CDR L) ) )
(T N I L ) ) ) )
1)
20. DEFINE ( ( (REPLACE (LAMBDA (A B X) (COND ((ATOM X) (COND ((EQUAL B X) A) ( T X ) ) )
(T (CONS (REPLACE A B (CAR X))
(REPLACE A B (CDR X)) ) ) ) ) ) ) )

CHAPTER 15.

1. DEFINE ( ( (NEGCNT (LAMBDA (L) (PROG (X)


(SETQ X 0)
TAG1 (COND ((NULL L ) (RETURN X))
((MINUSP (CAR L ) ) (SETQ X (ADD1 X ) ) ) )
(SETQ L (CDR L ) )
(GO T A G l ) ) ) ) 1)
2. DEFINE ( ( (CURVE (LAMBDA (A B C) (PROG (X)
(SETQ X (PLUS (TIMES B B) (TIMES -4 A C ) ) )
(COND ((ZEROP X) (RETURN (QUOTE PARABOLA)))
((LESSP X 0) (RETURN (QUOTE E L L I P S E ) ) ) )
(RETURN (QUOTE HYPERBOLA))))) ))
3. DEFINE ( ( (LENGTHS (LAMBDA (M) (PROG (X)
(SETQ X 0)
LOCl (COND ((NULL M) (RETURN X ) ) )
(SETQ X (ADD1 X))
(SETQ M (CDR M ) )
(GO LOC1)))) 1)
4. DEFINE ( ( (LAST (LAMBDA (L) (PROG (U)
TI (COND ((NULL L ) (RETURN U ) ) )
(SETQ U (CAR L ) )
(SETQ L (CDR L ) )
(GO T I ) ) ) ) 1)
5-7. DEFINE ( (
(REVERSAL (LAMBDA (L) (PROG (Y)
T2 (COND ((NULL L) (RETURN Y ) ) )
(SETQ Y (CONS (CAR L ) Y ) )
(SETQ L (CDR L ) )
(GO T 2 ) ) ) )
(PAIRS (LAMBDA ( L 1 L 2 ) (PROG (X)
T3 (COND ((NULL L 1 ) (RETURN (REVERSE X) ) ) )
(SETQ X (CONS (CONS (CAR L 1 ) (CAR L 2 ) ) X) )
(SETQ L 1 (CDR L l ) )
(SETQ L 2 (CDR L 2 ) )
(GO T 3 ) ) ) )
(DELETE (LAMBDA (A L) (PROG (Z)
T4 (COND ((NULL L) (RETURN (REVERSE Z ) ) )
((EQ A (CAR L ) ) (GO T 5 ) ) )
(SETQ Z (CONS (CAR L ) Z ) )
T5 (SETQ L (CDR L ) )
(GO T 4 ) ) ) )
1)
8. DEFINE ( ( (PERMSJT (LAMBDA (N R)
(QUOTIENT (FACTORIAL N) (FACTORIAL (DIFFERENCE N R) ) ) ) ) ) )
DEFINE ( ( (PERMUT (LAMBDA (N R) (PROG ( )
(RETURN (QUOTIENT (FACTORIAL N) (FACTORIAL (DIFFERENCE N R ) ) ) ) ) ) ) ))
9. DEFINE ( ( (COMBIN (LAMBDA (N R)
(QUOTIENT (FACTORIAL N) (TIMES (FACTORIAL R)
(FACTORIAL (DIFFERENCE N R ) ) ) ) ) ) ))
DEFINE ( ( (COMBIN (LAMBDA (N R) (PROG ( )
(RETURN (QUOTIENT (FACTORIAL N)
(TIMES (FACTORIAL R)
(FACTORIAL (DIFFERENCE N R ) ) ) ) ) ) ) ) ) )
lo. DEFINE ( ( (PASCAL (LAMBDA (N) (PROG (X R LINE)
(SETQ X 0 )
OUTLOOP (SETQ R 0)
(COND ( (LESSP N X) (RETURN NIL) ) )
INLOOP (COND ((LESSP X R) (GO BUMPX)))
(SETQ LINE (CONS (COMBIN X R) LINE))
(SETQ R (ADD1 R))
(GO INLOOP)
BWX (PRINT LINE)
(SETQ LINE NIL)
(SETQ X (ADD1 X))
(GO OUTLOOP)))) ))

PASCAL (15)
(1)
(1 1 )
(1 2 1)
(1 3 3 1)
(1 4 6 4 1)
(1 5 1 0 1 0 5 1)
(1 6 15 20 1 5 6 1)
(1 7 2 1 3 5 35 2 1 7 1)
(1 8 2 8 5 6 7 0 5 6 2 8 8 1)
(1 9 3 6 8 4 1 2 6 1 2 6 8 4 3 6 9 1 )
(1 1 0 4 5 1 2 0 2 1 0 252 2 1 0 1 2 0 4 5 1 0 1 )
(1 11 5 5 1 6 5 3 3 0 4 6 2 4 6 2 3 3 0 1 6 5 5 5 11 1)
( 1 1 2 6 6 220 4 9 5 7 9 2 9 2 4 7 9 2 4 9 5 220 6 6 1 2 1.)
(1 13 7 8 2 8 6 7 1 5 1 2 8 7 1 7 1 6 1 7 1 6 1 2 8 7 7 1 5 286 78 1 3 1)
( 1 1 4 9 1 3 6 4 1 0 0 1 2002 3 0 0 3 3 4 3 2 3 0 0 3 2 0 0 2 1.001 3 6 4 9 1 1 4 1)
(1 15 1 0 5 4 5 5 1 3 6 5 3 0 0 3 5 0 0 5 6 4 3 5 6 4 3 5 5 0 0 5 3 0 0 3 1 3 6 5 4 5 5 1 0 5 15 1)
NIL
PASCAL (16) i s t h e l a r g e s t t r i a n g l e p o s s i b l e w i t h t h i s d e f i n i t i o n s i n c e
16! i s maximum f i x e d - p o i n t a c c u r a c y of a 48-bit machine.

CHAPTER 16.

There a r e no e x e r c i s e s f o r t h i s c h a p t e r .

CHAPTER 1 7 .

1. (LIST)
(LIST)
2. blank l i n e
NIL
blank l i n e
NIL
3. ATOM1 ATOM2
NIL
4. (NOW HEAR THIS)
5. ( (INPUT) ANYTHING)
6. For R=5 , 31.4159
For R=50 , 314.159
For R=10 , 62.8318
For END , END
7. (( ) ./ = $ * NOW HEAR THIS -533.17)
8. (B C)
9. 1. % T h i s e x p r e s s i o n b i n d s t h e l i t e r a l atom $$*%* t o t h e name PERCENT.
The $ $ - a r t i f a c t i s t h e o n l y way t o e n t e r i l l e g a l r e a d c h a r a c t e r s .
2. %
3. PERCENT
10. A
11. 1. A
2. (B C)
3. ( A . B)
4. 3.14159
5. 3.14159
12. 1. (A)
2. ( A . B)
3. 3.14159
4. (3.14159)
5. X SQUARE
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
END
13. DEFINE ( (
(SUP4 (LAMBDA ( ) (PROG (S1 S2 ARGS)
A (TEREAD) (SETQ ARGS NIL)
(SETQ S2 (READ))
(SETQ S 1 (READ))
B (COND ( (NULL S2) (GO C) ) )
(SETQ ARGS (CONS (LIST (QUOTE QUOTE) (CAR S2)) ARGS))
(SETQ s 2 (CDR ~ 2 ) )
(GO B)
C (PRINT (EVAL (CONS S 1 (REVERSE ARGS))) )
(GO A) ) ) )
11
1. A
2. (B C)
3. NIL
4. 10
5. 3.14159
6. 3.14159
14. DEFINE ( (
(SUP5 (LAMBDA ( ) (PROG (X Y Z)
A (TEREAD) (SETQ Z NIL)
(SETQ X (READ))
(SETQ Y (READ))
(PRIN X) (PRIN Y) (TERPRI)
(COND ( (EQ (QUOTE NO) (READ)) (GO A) ) )
(PRIN X) (PRIN Y) (PRIN BLANK) ( P R I N ~ EQSIGN) (PRIN BLANK)
B (COND ((NULL Y) (GO C ) ) )
(SETQ Z (CONS ( L I S T (QUOTE QUOTE) (CAR Y ) ) Z))
(SETQ Y (CDR Y) )
(GO B)
C (YRIN (EVBL (CONS X (REVERSE Z ) ) ) )
(TERPRI)
(GO A ) ) ) ) 1)
15. DEFINE ( ( ( P I (LAMBDA (X) (PROG ( H I )
(PRINT (QUOTE (ENTER MAX X) ) )
(SETQ H I (READ))
(SETQ X (TIMES X 1 . 0 ) )
(PRINT (QUOTE $$$
XSQUARE SQRTX RECIPX FACTORI.ALX

(COND ( (LESSP H I X) (RETURN (QUOTE $$$LIMIT REACHED$) ) ) )


( P R I N l X)
(PRIN $$$ $1
( P R I N l (TIMES X X ) )
$$$ $)
( P R I N l (SQRT X))
(PRIN $$$ $1
(PRIN1 (QUOTIENT 1.0 X ) )
(PRIN $$$ $1
( P R I N l (FACTORIAL X) )
(TERPRI)
(SETQ X (ADD1 X))
(GO T A G l ) ) ) ) 1)
CHAPTER 18.

(TRY THIS SIMPLE CASE FIRST)


(THIS SIMPLE CASE FIRST)
(SIMPLE CASE FIRST)
(CASE FIRST)
(FIRST)
NIL
NOW
THIS
ONE
NIL
AND
LASTLY
THIS
ONE
(AND LASTLY THIS ONE)
((ONE 2 3 ONE 4 ONE 5) ( 2 3 ONE 4 ONE 5) (3 ONE 4 ONE 5) (ONE 4 ONE 5)
(4 ONE 5) (ONE 5) (5))
((12 3 4) ( 2 3 4) ( 3 4 ) ( 4 ) )
((A. A) ( B . B) ( C . C) ( D . D) ( E . E ) )
((A. X) ( B . X) (C . X) ( D . X) ( E . X))
((A. Z) ( B . Z) (C . 2) ( D . Z) ( E . Z))
( ( A 1 2 3 4 5 ) ( B 1 2 3 4 5 ) ( C 1 2 3 4 5 ) ( D 1 2 3 4 5 ) ( E 1 2 3 4 5 ) )
DEFINE ( (
(MAPCAR2 (LAMBDA (X Y FN)
(COND ((NULL X) NIL)
(T (CONS (FN (CAR X) (CAR Y)) (MAPCAR2 (CDR X) (CDR Y) FN))) ) ) ) ) )
DEFINE ( (
(TYPE (LAMBDA (J) (MAPCAR J (FUNCTION
(LAMBDA (K) (COND ((NUMBERP K) (RETURN (COND ( ( F I X P K) (QUOTE FIX))
(T (QUOTE F L T ) ) ) ) )
((ATOM K) (QUOTE ATOM))
((EQ (ATOM (CAR K)) (ATOM (CDR K))) (QUOTE DOTPAIR))
(T (QUOTE L I S T ) ) 1) 1) 1) 1)
CHAPTER 1 9 .

1. ((CAT) (DOG) (FOX) 1 2 3.0)

3. (NCONC (LAMBDA (X Y) (PROG (M)


(COND ( (NULL X) (RETURN Y) ) )
(SETQ M X)
A (COND ((NULL (CDR M) ) (GO B ) ) )
(SETQ M (CDR M) )
(GO A)
n (RPLACD M Y)
(RETURN X) 1))
4. (REVLIST (LAMBDA (L) (PROG (C P)
A (SETQ C L )
(COND ( (NULL L) (RETURN P)))
(SETQ L (CDR L ) )
(RPLACD C P )
(SETQ P C)
(GO A) 1)
5. (BLAM (LAMBDA (L) (PROG (M)
(SETQ M L )
A (COND ((NULL M) (RETURN L) ) )
(RPLACA M (CDR M))
(SETQ M (CDR M))
(GO A) 1))
6. (SUITS (LAMBDA (PLAYER) (PROG (S H D C X)
(SETQ X (GET PLAYER (QUOTE HAND) ) )
A (COND ((NULL X) (GO B ) ) )
(SELECT (GET (CAR X) (QUOTE S U I T ) )
((QUOTE SPADE) (SETQ S (CONS (CAR X) S ) ) )
((QUOTE HEART) (SETQ H (CONS (CAR X) H ) ) )
((QUOTE DIAMOND) (SETQ D (CONS (CAR X ) D l ) )
(SETQ C (CONS (CAR X) C ) ) )
(SETQ X (CDR X ) )
(GO A)
B (PUT PLAYER (QUOTE SPADES) S )
(PUT PLAYER (QUOTE HEARTS) H)
(PUT PLAYER (QUOTE DIAMONDS) D)
(PUT PLAYER (QUOTE CLUBS) C)
(RETURN PLAYER) ) ) )
7. (LONGSUIT (LAMBDA (PLAYER) (PROG (S H D C H I 1 H I 2 )
(SETQ s (LENGTH (GET PLAYER (QUOTE SPADES))))
(SETQ H (LENGTH (GET PLAYER (QUOTE HEARTS))))
(SETQ D (LENGTH (GET PLAYER (QUOTE DIAMONDS))))
(SETQ c (LENGTH (GET PLAYER (QUOTE CLUBS))))
(COND ( (GREATERP H S) (SETQ H I 1 (CONS (QUOTE HEARTS) H) ) )
(T (SETQ H I 1 (CONS (QUOTE SPADES) s ) ) ) )
(COND ((GREATERP C D) (SETQ H I 2 (CONS (QUOTE CLUBS) C ) ) )
(T (SETQ H I 2 (CONS (QUOTE DIAMONDS) D ) ) ) )
(COND ((GREATERP (CDR H I 2 ) (CDR H I 1 ) ) (RETURN H I 2 ) )
(T (RETURN 1511) 1) 1)
8. (POINTS (LAMBDA (PLAYER)
(PLUS (POINT1 PLAYER (QUOTE SPADES))
(POINT1 PLAYER (QUOTE HEARTS))
(POINTI PLAYER (QUOTE DIAMONDS))
(POINTI PLAYER (QUOTE CLUBS)))))
(POINT1 (LAMBDA (P S ) (PROG (SUM LN VALUE X)
(SETQ SUM 0)
(SETQ X (GET P S ) )
(SETQ LN (LENGTH X) )
(COND ((ZEROP LN) (RETURN 3 ) ) )
A (COND ((NULL X) (RETURN SUM)))
(SETQ VALUE (GET (CAR X) (QUOTE RANK) )
(SETQ SUM (PLUS SUM
(COND ((LESSP VALUE 11) 0)
((EQUAL VALUE 1 4 ) 4 )
((AND (EQUAL VALUE 13)
(GREATERP LN 1)) 3)
( (AND (EQUAL VALUE 1 2 )
(GREATERP LN 2 ) ) 2)
((AND (EQUAL VALUE 11)
(GREATERP LN 3 ) ) 1)
((ONEP LN) 2)
(T 0))))
(SETQ X (CDR X))
(GO A) 1))
9. (STOPPERS (LAMBDA (PLAYER) (AND (STOP PLAYER (QUOTE SPADES) )
(STOP PLAYER (QUOTE HEARTS))
(STOP PLAYER (QUOTE DIAMONDS))
(STOP PLAYER (QUOTE CLUBS)))))
(STOP (LAMBDA (P S ) (PROG (LN VALUE X)
(SETQ X (GET P S ) )
(SETQ LN (LENGTH X) )
(COND ((ZEROP LN) (RETURN N I L ) ) )
A (COND ((NULL X) (RETURN N I L ) ) )
(SETQ VALUE (GET (CAR X) (QUOTE RANK) ) )
(COND ((OR (EQUAL VALUE 1 4 )
(AND (EQUAL VALUE 13)
(GREATERP LN 1))
(AND (EQUAL VALUE 1 2 )
(GREATERP LN 2 ) ) ) (RETURN T ) ) )
(SETQ X (CDR X))
(GO A) ) ) )
lo. (BALANCED (LAMBDA (PLAYER)
(LESSP 2 (MIN (LENGTH (GET PLAYER (QUOTE SPADES)) )
(LENGTH (GET PLAYER (QUOTE HEARTS) ) )
(LENGTH (GET PLAYER (QUOTE DIAMONDS)))
(LENGTH (GET PLAYER (QUOTE CLUBS))) ))))
(OPENBID (LAMBDA (PLAYER) (PROG (PT LS ST BAL)
(SUITS PLAYER)
(SETQ P T (POINTS PLAYER))
(SETQ L S (LONGSUIT PLAYER))
(SETQ ST (STOPPERS PLAYER) )
( SETQ BAL (BALANCED PLAYER) )
(COND ((LESSP P T 13) (GO PASS))
((LESSP PT 2 0 ) (GO ONEBID))
((AND ST BAL) (RETURN (QUOTE ( 2 NO TRUMP) ) ) )
(T (RETURN ( L I S T 2 (CAR L S ) ) ) ) )
PASS (COND ((AND (GREATERP (CDR LS) 6)
(GREATERP P T 6 ) )
(RETURN ( L I S T 3 (CAR L S ) ) ) )
(T (RETURN (QUOTE PASS))))
ONEBID (COND ((AND ST BAL (GREATERP P T 1 5 ) ) (RETURN (QUOTE (1 NO TRUMP))))
(T (RETURN ( L I S T 1 (CAR LS) ) ) ) ) ) ) )
11. MACRO ( ( (PROD (LAMBDA (J) (*EXPAND J (QUOTE *TIMES)) )) ))
12. MACRO ( (
(MAXIMUM (LAMBDA (J) (*EXPAND J (QUOTE *MAX))))
(MINIMUM (LAMBDA (J) (*EXPAND J (QUOTE *MIN)) ) )
1
13. MACRO ( ( (FLAMBDA (LAMBDA (J)
(LIST (QUOTE FUNCTION)
(CONS (QUOTE LAMBDA) (CDR J ) ) ) ) ) ) )
14. MACRO ( (
( L I S T 2 (LAMBDA (J) (*EXPAND J (QUOTE C O N S ) ) ) )
( L I S T 1 (LAMBDA (J) (APPEND (CONS (QUOTE LIST^)
(CDR J ) )
(QUOTE ( N I L ) ) )
1
We n o t e h e r e t h a t g i v e n a form

t h e macro L I S T 2 expands form [ I ] t o

( L I S T 2 xl x 2 x3 N I L )

and w i t h r e p e a t e d a p p l i c a t i o n t o

Thus t h e s o l e purpose of macro L I S T 1 i s t o i n s e r t N I L a s t h e l a s t argument


of t h e form. I f we used t h e macro d e f i n i t i o n f o r L I S T 2 o n l y , we would g e t
a v a l u e of

r a t h e r than

which i s t h e l i s t we d e s i r e .
A m o r e p r a c t i c a l s o l u t i o n i s t o r e d e f i n e *EXPAND as say *EXPANDS.

DEFINE ( (
(*EXPANDS (LAMBDA (FORM FN)
(COND ((NULL (CDDR FORM))
(CONS F N (CONS (CADR FORM) (QUOTE (NIL) ) ) ) )
( T (CONS F N
(CONS (CADR FORM)
(CONS (CONS (CAR FORM) (CDDR FORM)) N I L ) ) ) ) ) ) ) ))

T h e n define L I S T 1 as

MACRO ( (
( L I S T 1 (LAMBDA (J) (*EXPANDS J (QUOTE C O N S ) ) ) ) ))

I n t h i s approach t h e f o r m

becomes

(CONS xl ( L I S T 1 x2 x3))

and w i t h repeated a p p l i c a t i o n

(CONS xl (CONS x2 ( L I S T 1 x3)))

H e r e ' s t h e difference. *EXPAND w o u l d r e t u r n x3 as t h e l a s t t e r m , b u t


*EXPANDS r e t u r n s (CONS x3 N I L ) and w e achieve the s a m e r e s u l t as above
m u c h m o r e efficiently.

15. DEFINE ((
( P R I N T Q 1 (LAMBDA (J) (PROG ( )
T 1 (COND ((NULL J) (RETURN ( T E R P R I ) ) ) )
( P R I N (CAR J ) ) ( P R I N BLANK)
(SETQ J (CDR J ) )
(GO T 1 ) ) ) ) 1)
MACRO ( (
(PRINTQ (LAMBDA (J) (LIST (QUOTE PRINTQl)
(LIST (QUOTE QUOTE)
(CDR J))))) 1)

The form

(PRINTQ NOW HEAR THIS)

a f t e r t h e macro PRINTQ has been executed, w i l l b e r e p l a c e d by t h e form

The f u n c t i o n PRINTQl e n t e r s each element of t h e l i s t i n t o t h e p r i n t l i n e


w i t h PRIN, and e x e c u t e s a f i n a l TERPRI when t h e l i s t is empty.

16. MACRO ( ( (PROGN (LAMBDA (J) (*EXPAND J (QUOTE P R o G ~ ) ) ) ) ) )


APPENDIX B
GLOSSARY

a-list
A synonym f o r a s s o c i a t i o n l i s t .

association list
A l i s t of d o t t e d p a i r s used by i n t e r p r e t i v e LISP systems t o p a i r bound
v a r i a b l e s with t h e i r values. It i s of t h e form

-
atom
A synonym f o r atomic symbol.

atomic symbol
The elementary form of a n S-expression. There a r e l i t e r a l atoms and
numeric atoms. A l i t e r a l atom c o n s i s t s of a s t r i n g of c a p i t a l l e t t e r s
and decimal d i g i t s of i n d e f i n i t e l e n g t h t h a t b e g i n s w i t h a l e t t e r ; e.g.,
A, BOY22, 2122. Numeric atoms a r e r e a l , i n t e g e r , o r o c t a l numbers d i s -
t i n g u i s h e d by t h e i r syntax. ( c . f . Chapter 4)

binding
The a s s o c i a t i o n of a v a r i a b l e w i t h a v a l u e w i t h i n some e x p r e s s i o n c o n t e x t .
There may b e m u l t i p l e b i n d i n g s f o r a v a r i a b l e during i t s l i f e t i m e a s i t i s
used i n m u l t i p l e c o n t e x t s ; however, t h e r e i s o n l y one binding c u r r e n t a t
a time. Lambda and program e x p r e s s i o n s a r e t h e p r i n c i p a l ways t o c r e a t e
bindings. (See z e r o - l e v e l b i n d i n g s )

bound v a r i a b l e s
A v a r i a b l e named i n t h e l i s t of v a r i a b l e s f o l l o w i n g LAMBDA o r PROG i n
lambda o r program e x p r e s s i o n s i s bound w i t h i n t h e scope of t h a t e x p r e s s i o n .
During e v a l u a t i o n of t h e s e e x p r e s s i o n s , t h e v a l u e of t h e v a r i a b l e i s t h e
v a l u e of t h e corresponding argument i n t h e f u n c t i o n c a l l . For example,
f o r (LAMBDA ( J K) body) ( 1 2 ) , J h a s v a l u e 1 and K h a s v a l u e 2 a t any of
t h e i r o c c u r r e n c e s i n t h e body.
character object
A l i t e r a l atom, t h e v a l u e of which p r i n t s a s a s p e c i a l c h a r a c t e r . For
example,

CSET (LARROW $$*+*)

c r e a t e s t h e c h a r a c t e r o b j e c t LARROW, t h e v a l u e of which p r i n t s a s "+".


clause
A p r e d i c a t e e x p r e s s i o n and i t s corresponding form i n a c o n d i t i o n a l
expression.

common v a r i a b l e
V a r i a b l e s used f o r communication between compiled and i n t e r p r e t e d e!xpressions
f o r LISP systems having both a compiler and a n i n t e r p r e t e r .

composed form
The c o n c a t e n a t i o n of forms such t h a t t h e v a l u e of one form i s used a s t h e
argument f o r a n o t h e r form. This i s w r i t t e n i n a n e s t e d f o r m a t , e . g . ,

(CONS (CAR X) (CDR X ) )

conditional expression
An e x p r e s s i o n c o n t a i n i n g a l i s t of c l a u s e s . The v a l u e of t h e c o n d i t i o n a l
e x p r e s s i o n i s t h e v a l u e of t h e form corresponding t o t h e f i r s t ( l e f t m o s t )
p r e d i c a t e e x p r e s s i o n t h a t e v a l u a t e s t o non-NIL. E v a l u a t i o n proceeds l e f t
t o r i g h t only a s f a r a s t h e f i r s t non-NIL e x p r e s s i o n . No form i s e v a l u a t e d
i f i t s p r e d i c a t e e x p r e s s i o n i s NIL.

constant
A v a r i a b l e , t h e v a l u e of which never changes d u r i n g computation, o r a
quoted expression. The f o l l o w i n g a r e examples of c o n s t a n t s :

(QUOTE (A B C))
3.14159
T
context
The b i n d i n g s f o r v a r i a b l e s d u r i n g a p a r t i c u l a r computation. For r e c u r s i v e
e v a l u a t i o n of a f u n c t i o n o r a n e x p r e s s i o n , t h e c o n t e x t a t any time i s t h e
c u r r e n t s t a t e of i t s v a r i a b l e s . V a r i a b l e s used f r e e i n a n e x p r e s s i o n a r e
s a i d t o be o u t s i d e t h e scope of t h a t e x p r e s s i o n a s t h e e x p r e s s i o n does n o t
\

c o n t r o l t h e b i n d i n g s of t h e f r e e v a r i a b l e s .

dot notation
The fundamental n o t a t i o n of LISP f o r r e p r e s e n t i n g non-atomic S-expressions.
Dot n o t a t i o n c o n t a i n s l e f t p a r e n t h e s e s , r i g h t p a r e n t h e s e s , atoms, and d o t s .
A non-atomic S-expression i s always a d o t t e d p a i r of S-expressions of t h e
form

where s and s a r e any S-expressions.


1 2
dotted pair
A non-atomic S-expression. The v a l u e of t h e e x p r e s s i o n

CONS (s s ) =
1 2 (sl s2)

where s and s a r e a r b i t r a r y S-expressions,


1 2
doublet
A p a i r of S-expressions f o r EVALQUOTE, t h e t o p - l e v e l system s u p e r v i s o r .
The f i r s t S-expression i s a f u n c t i o n name o r a f u n c t i o n a l e x p r e s s i o n . The
second S-expression i s a l i s t of arguments.

dummy v a r i a b l e
I f t h e s y s t e m a t i c s u b s t i t u t i o n of a l i t e r a l atom f o r one used a s a v a r i a b l e
i n a n e x p r e s s i o n does n o t change t h e meaning ( i . e . , t h e v a l u e r e t u r n e d ) of
t h e e x p r e s s i o n , t h e v a r i a b l e i s a dummy v a r i a b l e . A l l lambda and program
v a r i a b l e s a r e dummy v a r i a b l e s .

element
The t o p - l e v e l c o n s t i t u e n t s of a l i s t . These c o n s t i t u e n t s may be atomic o r
non-atomic. I f they a r e l i s t s , t h e y may themselves have elements. Thus,
( 1 has no elements
(A) has one element
(A (B)) has two elements, one of which is a list of
one element.

empty list-
A list having no elements. This list is also equivalent to the literal
atom, NIL.

expression_
A synonym for S-expression in most contexts in this text. In some
instances it may refer to an arithmetic expression.

form
An S-expression that may be a simple constant, a variable, a simple form,
a composed form, or a special form. It may be evaluated when some corres-
pondence has been set up between the variables contained in the S-expression
and a set of arguments. The correspondence is not part of the form.
(See function.)

free variable
A varisble that is used, but not bound within the scope of an express.ion.
A free variable can only be determined free by considering the context in
which it appears. In the expression

K is a free variable in the innermost lambda expression, and a bound


variable in the outermost lambda expression.

function
An expression containing a form and a correspondence between the variables
in the form and the arguments of the function. A lambda expression is a
function, sometimes called a functional expression.

correspondence

(See also lambda notation.)


-212-
functional
A function that can have functions as arguments, e.g., MAPCAR.

functional expression
See function.

function composition
See composed forms.

global bindings
See zero-level bindings.

label notation
A scheme for temporarily naming a lambda expression, so that the expression
may call itself recursively. Recursive functions call themselves by their
names. Lambda expressions are functions without names. Thus, label
notation gives a temporary name to a lambda expression. The notation has
the form

(LABEL name lambda-expression)

labels
See statement labels.

lambda conversion
The process of evaluating lambda expressions. All arguments are paired
with variables in the list of variables foll.owing the LAMBDA. Then the
form inside the lambda expression is evaluated using those values of the
variables.

lambda expression
See lambda notation.

lambda notation
The notation first used by Church for converting forms into functions. In
LISP, lambda notation is used for lambda expressions such that

(LAMBDA (al a2 ... an) fo m )

is a function of n variables. The variables are used in the form whose


arguments are the a.. (See also function.)
1
lambda variable
A variable named in the list of variables following the LAMBDA in a
lambda expression. (See also bound variables.)

list
A shorthand notation for an S-expression of the form

(sl. (s2. ... ("n . NIL) ... ) 1


This represents the list

where the s are any S-expressions.


i
list notation
A method of representing S-expressions in a convenient notation for
reading and writing. (See list.)

list structure
A list of lists.
literal at=
See atomic symbol.

macro expansion
A coniputational process that transforms one form into some other forrr~.
The transformation rule is embedded in the definition of a LISP function.
This function is invoked by the system (usually at compile-time) and given
as its argument the form containing the name of the function. The value
of the function is the new form which replaces the old form in whatever
context the old form appeared. Generally, the transformation involves
expanding the old form into a composed form of primitive function calls;
however, any arbitrary computation is possible. Macros are used to define
special forms in compiler-based LISP systems.
object list
A s p e c i a l system s t r u c t u r e t h a t c o n t a i n s a l l t h e l i t e r a l atoms r e a d by
t h e system. I n most systems, t h e o b j e c t l i s t i s c a l l e d t h e OBLIST and i s
manufactured by d i s t r i b u t i n g t h e l i t e r a l atoms i n t o a s e r i e s of s u b l i s t s ,
c a l l e d b u c k e t s , by a computation (hashing) upon t h e i r H o l l e r i t h p r i n t
names. It p e r m i t s f a s t atom r e c o g n i t i o n d u r i n g r e a d i n g .

ordinary variable
A synonym f o r bound v a r i a b l e .

parameters
An elementary atomic form t h a t i s e i t h e r a c o n s t a n t o r a v a r i a b l e .

p-list
A synonym f o r p r o p e r t y l i s t .

pointer
An i n t e r n a l machine a d d r e s s . It d e s i g n a t e s o r p o i n t s t o a l o c a t i o n of
interest.

predicate
I n mathematics, a f u n c t i o n , t h e v a l u e of which i s t r u e o r f a l s e . I n LISP,
a f u n c t i o n , t h e v a l u e of which i s T ( t r u e ) o r NIL ( f a l s e ) . (See a l s o
semi-predicate.)

p r i n t name
The o r i g i n a l s t r i n g of c h a r a c t e r s r e a d by t h e system, r e p r e s e n t i n g t h e
i n t e r n a l name f o r a l i t e r a l atom. T h i s s t r i n g of c h a r a c t e r s i s p r i n t e d
a s t h e name of t h e l i t e r a l atom by PRINT and o t h e r p r i n t f u n c t i o n s .

prog e x p r e s s i o n
A synonym f o r program e x p r e s s i o n .

prog v a r i a b l e
A synonym f o r program v a r i a b l e .

program e x p r e s s i o n
An e x p r e s s i o n of t h e form

(PROG ( v a r i a b l e s ) s t a t e m e n t s )

t h a t a l l o w s e v a l u a t i o n of s t a t e m e n t s i n s e r i a l f a s h i o n . (See program
feature. )
program f e a t u r e
A f e a t u r e i n LISP t h a t a l l o w s programs, c o n t a i n i n g s t a t e m e n t s , t o b e
executed i n s e r i a l f a s h i o n . It a l s o p e r m i t s i t e r a t i o n and t h e u s e of
temporary v a r i a b l e s .

program v a r i a b l e
A temporary v a r i a b l e t h a t i s d e c l a r e d i n t h e l i s t of v a r i a b l e s f o l l o w i n g
t h e PROG i n a program e x p r e s s i o n . Program v a r i a b l e s a r e i n i t i a l l y a s s i g n e d
t h e , v a l u e NIL; however, t h e y may b e a s s i g n e d a r b i t r a r y v a l u e s by t h e pseudo-
f u n c t i o n s SET and SETQ. They a r e a l s o bound and dummy v a r i a b l e s .

property list
The l i s t s t r u c t u r e a s s o c i a t e d w i t h a l i t e r a l atom t h a t may b e used f o r
s t o r i n g i n f o r m a t i o n t o b e a s s o c i a t e d w i t h t h e l i t e r a l atom. The p r o p e r t y
l i s t is a convenient information r e p o s i t o r y t h a t permits r a p i d , dictionary-
like retrieval.

pseudo-function
An e x p r e s s i o n t h a t i s c a l l e d a s i f i t were a f u n c t i o n , b u t f o r i t s e f f e c t
rather than f o r its value, e.g., READ, PRINT, DEFINE.

push-down l i s t
The l a s t - i n - f i r s t - o u t (LIFO) memory a r e a used by t h e system f o r s a v i n g
p a r t i a l r e s u l t s of r e c u r s i v e f u n c t i o n s . G e n e r a l i z e d LIFO s t o r a g e f o r u s e r s
i s p o s s i b l e by u s i n g CONS (push) o r CDR (pop) on any l i s t .

quo t i n &
The t e c h n i q u e used by LISP t o s u p p r e s s e x p r e s s i o n e v a l u a t i o n . Quoting
creates constant data within functions. The s p e c i a l form QUOTE i s used
f o r quoting.

recursion
R e c u r s i o n i s a t e c h n i q u e f o r d e f i n i n g a computation on a g i v e n datum, The
p r o c e s s u s u a l l y produces a p a r t i a l s o l u t i o n and r e d u c e s t h e datum t o a
s i m p l e r form. The same p r o c e s s i s t h e n r e a p p l i e d t o t h i s s i m p l e r form
of t h e datum. Again a p a r t i a l s o l u t i o n and a s i m p l e r form a r e o b t a i n e d .
The p r o c e s s c o n t i n u e s u n t i l some t e r m i n a l datum o b t a i n s , whereupon a l l
p a r t i a l s o l u t i o n s a r e combined i n some f a s h i o n t o produce t h e f i n a l
solution. To compute r e c u r s i v e l y t h e f a c t o r i a l of N , f o r example, w e have
where N i s t h e p a r t i a l s o l u t i o n and (N-1) i s t h e s i m p l e r form upon which
we r e p e a t t h e f a c t o r i a l computation. This process recurs u n t i l t h e
t e r m i n a l c o n d i t i o n N = 0 i s reached, whereupon t h e p a r t i a l r e s u l t s a r e
combined t o form t h e f i n a l answer; e . g . ,

scope
The domain i n which a v a r i a b l e i s d e f i n e d , i . e . , i t s binding can be
retrieved. The domain i s expressed a s t h e l i m i t s of a g i v e n expression.

semi-predicate
A f u n c t i o n , t h e v a l u e of which i s e i t h e r NIL ( f a l s e ) o r non-NIL ( t r u e ) .
The implementation of COND i n most LISP systems t e s t s only f o r NIL. There-
f o r e , any f u n c t i o n t h a t r e t u r n s a v a l u e of NIL o r non-NIL may b e used i n
t h e p r e d i c a t e p o s i t i o n of a c l a u s e of a c o n d i t i o n a l e x p r e s s i o n . CDR and
SETQ a r e two examples of semi-predicates. (See a l s o p r e d i c a t e . )

S-expression
A symbolic e x p r e s s i o n of a r b i t r a r y l e n g t h t h a t i s e i t h e r atomic o r repre-
s e n t s a s t r u c t u r e having two branches a t each node. The s i m p l e s t form of
a n S-expression i s a n atomic symbol. A non-atomic S-expression i s e i t h e r :

1. A d o t t e d p a i r of atoms, e.g.,

2. A d o t t e d p a i r of S-expressions, e.g.,

special c e l l
See v a l u e c e l l .
s p e c i a l form
A form given s p e c i a l treatment by LISP. It i s a form having a n
' i n d e f i n i t e number of arguments and/or arguments t h a t a r e unevaluated and
given t o t h e s p e c i a l form t o c o n t r o l e v a l u a t i o n .

special variable
V a r i a b l e s t h a t have bindings i n t h e v a l u e c e l l . They a r e used f o r
c o n s t a n t s and/or f r e e v a r i a b l e s . Such v a r i a b l e s have t o be d e c l a r e d
( i n some systems) b e f o r e they a r e used w i t h t h e pseudo-function SPECIAL.
UNSPECIAL removes such v a r i a b l e s from s p e c i a l s t a t u s .

statement l a b e l s
A l i t e r a l atom appearing a t t h e top l e v e l (statement l e v e l ) of a pr~ogram
e x p r e s s i o n i s used a s t h e name f o r t h e form following t h e l a b e l . This
name may be used i n a GO s t a t e m e n t t o t r a n s f e r c o n t r o l t o t h e labelled form.

statements
A s e r i e s of non-atomic forms t h a t c o n s t i t u t e t h e body i n a program
expression. The s t a t e m e n t s a r e e v a l u a t e d i n s e r i e s f o r t h e i r e f f e c t
on v a r i a b l e s r a t h e r t h a n t h e i r v a l u e . A l l LISP forms a r e l e g a l s t a t e m e n t s .
Recursion i s p e r m i t t e d . The GO and RETURN s t a t e m e n t s a l l o w c o n t r o l over
t h e sequence of s t a t e m e n t execution.

value c e l l
A p l a c e used t o s t o r e t h e v a l u e of a s p e c i a l v a r i a b l e . The v a l u e c e l l i s
a s s o c i a t e d w i t h t h e l i t e r a l atom name of t h e s p e c i a l v a r i a b l e s o t h a t t h e
v a l u e may be r e t r i e v e d by a l l f u n c t i o n s , independent of c o n t e x t . (See
zero-level bindings.)

z e r o - l e v e l bindings
A v a r i a b l e t h a t has a v a l u e a t t h e t o p l e v e l (the zero l e v e l ) i s bound a t
t h e top l e v e l . I t has a scope t h a t i s g l o b a l , i . e . , may be used f r e e l y
a t any l e v e l s i n c e i t i s defined f o r a l l l e v e l s . V a r i a b l e s w i t h zero l e v e l
bindings a r e e s t a b l i s h e d by CSET o r CSETQ (and SET and SETQ f o r some
systems) and a r e u s u a l l y system c o n s t a n t s .
APPENDIX C
REFERENCES

John McCarthy, Paul W. Abrahams, Daniel J. Edwards, Timothy P. Hart,


and Michael I. Levin, LISP 1.5 Programmer's Manual (Cambridge, Massa-
chusetts: The MIT Press, 1962).

Timothy P. Hart and Thomas G. Evans, "Notes on Implementing LISP for the
M-460 ~omputer." in Edmund C. Berkeley and Daniel G. Bobrow (eds.),
The Programing Language LISP: I t s Operation and Applications, 2nd ed.
(Cambridge, Massachusetts: The MIT Press, 1966), p. 191.

Robert A. Saunders, h he LISP System for the Q-32 Computer," in Edmund


C. Berkeley and Daniel G. Bobrow (eds.), The Programming Language LISP:
I t s Operation and Applications, 2nd ed. (Cambridge, Massachusetts: The
MIT Press, 1966), p. 220.

L. Peter Deutsch and Edmund C. Berkeley, "The LISP Implementation for the
PDP-1 ~omputer,"in Edmund C. Berkeley and Daniel G. Bobrow (eds.), The
Programming Language LISP: I t s Operation and Applications, 2nd ed.
(Cambridge, Massachusetts: The MIT Press, 1966), p. 326.

Daniel G. Bobrow, D. Lucille Darley, Daniel L. Murphy, Cynthia Solomon,


and Warren Teitelman, The BBN-LISP System (Cambridge, Massachusetts:
Bolt, Beranek and Newman, Inc., 1966), AFCRL-66-180.

L. P. Deutsch and B. W. Lampson, Re-ference Manual, 930 L I S P (University


of California, Berkeley, 1965), Document No. 30.50.40.

Alonzo Church, The Calculi of Lmnbda-ConversCon (Princeton: Princeton


University Press, 1941).

Richard S. Burington, Handbook of Mathematical Tables and F o m l a s


(Sandusky, Ohio: Handbook Publishers, Inc., 1953), p. 45.

Fisher Black, "Styles of Programming in LISP," in Edmund C. Berkeley


and Daniel G. Bobrow (eds.), The Programing Language LISP: I t s Operation
and Applications, 2nd ed. (Cambridge, Massachusetts: The MIT Press, 1966),
p. 96.
10. Elaine Gord, "~oteson the Debugging of LISP Programs," in Edmund C.
Berkeley and Daniel G. Bobrow (eds.), The 'heogrming Language LISP: Its
Operation and AppZications, 2nd ed. (Cambridge, Massachusetts: The MIT
Press, 1966), p. 93.

11. Timothy P. Hart, "MACRO Definitions for LISP" (Artificial Intelligence


Project, RLE and MIT Computation Center, Memo 57, 1963).

12. J. W. Backus, "The Syntax and Semantics of the Proposed International


Algebraic Language of the Zurich ACM-GAMM Conference," Proceedings,
Internat'l. Conf. Information Processing, UNESCO, Paris, June 1959,
pp. 125-132.

13. K. Samelson and F. L. Bauer, "Sequential Formula Translation," Communi-


cations of the Association for Computing Machinery, Vol. 3, 1960,
pp. 76-83.

14. B. W. Arden, G. A. Galler and R. M. Graham, "An Algorithm for Translating


Boolean Expressions," Journal of the Association for Computing Machinery,
Vol. 9, No. 2, April 1962, pp. 222-239.

15. R. G. Toby, R. J. Bobrow and S. N . Zilles, "Automatic Simplification in


FORMAC," Proceedings, 1965 Fall Joint Computer Conference, Vol. 27,
Part 1, November 1965, pp. 37-53.

16. D. Wooldridge, "An Algebraic Simplify Program in LISP," (Artificial


Intelligence Project, Stanford University, Memo 11, December 1963).
APPENDIX D
INDEX TO TECHNICAL TERMS

A RATOM, 126
A-list (see association list) unusual spelling, 128
APVAL, 117 (see also variables) Atoms (see atomic symbol)
Arguments :
arithmetic expressions, 82 B
composed forms , 50-55 Backtrace, 123 (see also error:
COND, 70 diagnostic tools)
debugging, 121-124 Backus Normal Form, 161
DEFINE, 66 Binding variables, 114-115 (see
EVAJiQUOTE, 62, 130-132 also variables)
functional arguments, 137, 141 Black, Fisher, 109
lambda expressions, 38-43 BNF, 161
nature of variables, 114 Breakpoints, 124 (see also error:
QUOTE, 59 diagnostic tools)
rapport with supervisor, 29
simple forms, 48 C
special forms, 58 Character objects, 129-130
Arithmetic: Character strings, 127-128
arithmetic functions, 83-86 .Church,Alonzo, 38
numbers, 25-27 Compile-time, 151 (see also macro)
number conversion, 76, 82 Compiling, 68
Association list, 115 Composition of forms (see form)
Atomic symbol : Composition of functions (see form)
examples, 5 Conditional clause, 70
literal atom, 5 Conditional expressions:
numbers : evaluation, 70-71
floating point, 27 inside program expression, 108
integers, 25-26 syntax, 70, 72
octals, 26 Constants:
object list, 127 elementary form, 47
Constants (Cont ) : . bad forms, 60
numbers, 25-27, 82 CAR, 31
quoted data, 59-60 CDR, 32
Context, 115-117 (see also conditional expressions, 71, 108
variables) unquoted data, 60
EVALQUOTE :
D definition, 130-132
Debugging, 44, 121 (see also mechanics of operation, 60-63
error) supervisor rapport, 29
Document conventions: Evaluation, 61 (see also form)
meta variables, 2
pedagogic variables, 2 F
$$-artifact, 128-129 False, 74
Dotted pair: Form:
decimal point resolution, 27 composed form:
definition, 6 definition, 50
graphical representation, 7-10 evaluation, 51, 54
Dot notation, 17-19 (see also nested lambda expressions, 53-54
S -expression) syntax, 50, 53
Dummy variable, 44-45 (see also contrast with function, 38-39
variables) elementary form:
constants, 47
E evaluation, 46 -48
Error : simple form, 47-48
diagnostic tools, 123-124 syntax, 47
high probability check list, variables, 46
121-123 special form:
number syntax, 25 -26 evaluation, 58
reclefinition, 68 macro requirements, 155
semantic class, 122 nature of, 58
syntax class, 121 value, 46
undefined evaluation: Form vs. function, 38
arithmetic functions, 82 Free variables, 109, 116-117 (see
arithmetic predicates, 76-77 also variables)
Function: List :
name, 66 circular, 145
necessary requirements, 39 construction:
Functional arguments, 137-141 APPEND, 97
Functional expression, 39-40, 53 CONS, 30
Functionals, 137 LIST, 58
Function definition, 39, 66 NCONC, 147
Function notation, 38 R P W A , 146
RPLACD, 146
G elements, 13-14
Garbage collector, 68, 156 empty list, 14
Gord, Elaine, 121 graphical representation, 19-22,
144-145
I knotted, 145
Indicator, 150-152 (see also modifying structures, 146-149
property list) predicate functions, 77
In£ix notation, 163 push-down, 119
Interpreter, 68, 115, 118, 152 re-entrant, 145
threaded, 145
use of NIL, 14
Label notation, 98 List notation, 15-17 (see also
Labels (see statement labels) S -expression)
Lambda conversion, 41-43 List structure, 13, 145 (see also list)
Lambda expression: Literal atoms, 5 (see also atomic
definition, 39-40 symbo1)
evaluation, 41 Logical connectives, 78-79
examples, 40-41
LABEL, 98
syntax, 40 Macro:
Lambda notation, 38-45 macro definitions, 152, 155
Lambda variables, 40, 42 (see macro expansion, 152
also variables) nature of, 151-152
LIFO, 119 Memory management, 68
N GO, 108
NIL : labels, 107
definition of, 14 RETURN, 108
falsity, 74 syntax, 106
result: of computation, 33 Program feature (see program
Number conversion, 76, 82 expression)
Numbers (see atomic symbol) Program variables, 106 (see also
Numeric atoms, 25-27 (see also variables)
atomic symbol) Property list, 117, 149 (see also list)
Propositional connective, 72, 80
0 Pseudo-function, 66
Object list, 127-128 Push-down list, 119
OBLIST, 127-128
On-line, 121, 134, 161
Q
P Quoting:
Parameters, 38, 47 character strings, 128
Parentheses: symbolic constants, 59
dot notation, 6
lambda expressions, 43-44 R
P-list (see property list) Read line, 126
Pointer, 7, 34, 125-126, 145-147 Recursion:
Predicate, 74 examples, 96-98
Prefix notation, 38, 163 heuristics for use, 95
Print line, 126 label notation, 98
Print name, 125-130, 149 macro limitations, 155
Program expression: nature of method, 93
evaluation, 107-108 recursive function calls, 92
program variables, 106 terminal conditions, 95
programming styles, 109 variable bindings, 115
scope of definition, 108-109 Run time, 151 (see also macro)
statements:
conditional expressions, 108 S
flow of control, 107-108 Scale factors (see atomic symbol)
Scope: T
of expression, 43-44 Time-sharing, 121
GO, 108 Top -level:
RETURN, 108 bindings, 117
of variables, 116 function calls, 42, 53
semantic errors, 122 (see also list element, 77
error) nature of, 132-133
Semi-predicate: restrictions, 132-133, 141
application example, 164 supervisor, 29-30, 60-63, 132-133
definition, 74 Trace, 124 (see also error:
S -expression: diagr.~ostictools)
building larger ones, 30-31 Trees (see S-expression)
definition, 6 True, 74
dot notation, 6-10
extracting sub -expressions, u
31 -33 Unwind, 123 (see also error:
graphical representation, 7-10, diagnostic tools)
19-22
list notation, 13-22 v
Special cell, 117-119 (see also Value cell, 117-119, 149 (see also
variables) variables)
Special forms (see form) Variables :
Statement, 106-109 APVAL, 117
Statement labels, 107 binding values:
String, 127-128 lambda conversion, 41, 117
String quoting, 122, 128 global bindings, 118
Sub -expression (see S -expression) on a-list, 115
Supervisors, 29-30, 130-133 (see zero-level bindings, 117-118; 130
a1so EVALQUOTE) bound vari.ables, 114, 132
Symbolic expression (see S- context, 1.15-117
expression) declarations, 119
Syntax errors, 121 (see also dummy vari.ables:
error) definiti.on, 44-45
dummy v a r i a b l e s (Cont .) :
lambda v a r i a b l e s , 40
program v a r i a b l e s , 106
e l e m e n t a r y forms, 46-47
f r e e v a r i a b l e s , 1 0 9 , 116-118,
142
: i n i t i a l i z e d by PROG, 106
n a t u r e o f , 114
o r d i n a r y v a r i a b l e s , 118-119
scope, 116
s e t t i n g values :
CSET, CSETQ, 118
SET, SETQ, 107
s p e c i a l c e l l , 117-119
v a l u e c e l l , 117-119, 149
s p e c i a l v a r i a b l e s , 118-119

z
Z e r o - l e v e l b i n d i n g s , 117-118, 130
FUNCTION DESCRIPTIONS
(Continued from front cover)

Function No. Arguments Type & Page Function


P
No. Arguments Type & Page

*MAX 2 ' function, 159 RATOM 0 pseudo-function, 126

MEMBER 2 predicate, 77 READ 0 pseudo-function, 125

(example) , (98) READCH 0 pseudo-function, 127

MIN indefinite special form, 84 RECIP 1 function, 85

(mairo) , (159) REIUIM)m 2 function, 84

*MIN 2 function, 159 ' R EMPROP 2 function, 151

MINUS 1 function, 83 RETURN 1 pseudo-function, 108

MINUSP 1 predicate, 77 REVERSE 1 example REVERSAL,

NC ONC 2 function, 147, 156 104, 112

NOT - 1 predicate, 78 RPLACA 2 function, 146

(example) , (71) RPLACD 2 function, 146

NULL 1 predicate, 77 SELECT indefinite special form, 72

NUMBERP 1 predicate, 76 SET 2 pseudo-function, 107

ONEP 1 predicate, 77 SETQ 2 pseudo-function, 107

CW. indefinite predicate, special SIN 1 example, 87

form, 78 SPECIAL 1 pseudo-function, 119

PLUS indefinite special form, 83 SQRT 1 function, 85

(macro) , (153) SUB1 1 function, 83

*PLUS 2 function, 153 SUBST 3 example REPIACE, LO5

PRIN 1 pseudo-function, 125 TEREAD 0 pseudo-function, 127

PRINl 1 pseudo-function, 127 TERPRI 0 pseudo -function, 127

PRINT 1 pseudo -function, 125 TIMES indefinite special form, 83

PROG 2 special form, 106 (macro), (159)

PROG2 2 pseudo-function, 109 *TIMES function, 159

PROGN indefinite macro, 160 TRACE pseudo-function, 124

PROP 3 function, 151 UNION example UNIONS, 103

PUT 3 function, 150 UNSPECIAI, pseudo-function, 119

QUmE 1 special fqrm, 59 UNTRACE pseudo-function, 124

QUOTIENT 2 function, 84 ZEROP predicate, 76


DICKENSON PUBLISHING COMPANY, INC.

You might also like