Anda di halaman 1dari 2

A MICRO-MANUAL FOR LISP - NOT THE WHOLE T R U T H

John McCarthy
Artificial Intelligence Laboratory
Stanford University

L I S P data are symbolic expressions that can be either atoms 10. Here's the hard one. value ((LABEL f (LAMBDA (o I ... v,)
ol lists. Atoms are s~tittgs of letters and digits and other characters e)) e I . . . en) is the same as value ((LAMBDA (v I ... vn) e) e I ...
not otherwise used in LISP. A list consists of a left parenthesis e n) with the additional rule that whenever ~¢al an) must be
followed by zero or more atoms or lists separated by spaces and
evaluated, f is replaced by (LABEL/" (LAMBDA (v I ... vn) e)).
e n d i n g with a right parenthesis. Examples: A, ONION, 0, (A), (A
O N I O N A), ( P L U S B (TIMES X PI) i), (CAR ( Q U O T E CA B))). Lists b e g i n n i n g with L A B E L define functions recursively.

T h e LISP programming language is defined by rules T h i s is the core of LISP, and here are more examples:
w h e r e b y certain LISP expressions have other LISP expressions as
values. T h e function called value that we will use in giving these value ( C A R X) = (A B) if value X ~ ((A B) C), and value
rules is not part of the LISP language but rather part of the ( ( L A B E L FF ( L A M B D A (X) (GOND ((ATOM X) X) ((QUOTE
informal m3tbematical language u;ed to define LISP. Likewise, T ) (FF (CAR X)))))) ( Q U O T E ((A B) C))) - A. Thus ((LABEL
the italic letters • and a (sometimes with subscripts) denote LISP FF ( L A M B D A (X) ( C O N D ((ATOM X) X) ((QUOTE T) (FF
expressions, the letter v (usually subscripted) denotes an atom ( C A R X)))))), is the LISP name of a function f f such that f f e is
s e r v i n g as a variable, and the letter f stands for a LISP expression the first atom in the written form of e. Note that the list f f is
serving as a function name. substituted for the atom FF twice.
1. value ( Q U O T E e) = e. T h u s the value of ( Q U O T E A) is A. Difficult mathematical type exercise: Find a list e such that
value e = e.
2. value (CAR e), where value e is a non-empty list, is the first
element of value e. T h u s value (CAR ( Q U O T E (A B C))) - A. Abbreviations

3. value (CDR e), where value e is a non-empty list, is the the list T h e above LISP needs some abbreviations for practical use.
that remains when the first element of value e is deleted. T h u s
value (CDR ( Q U O T E (A B C))) = (B C). 1. T h e variables T and NIL are permanently assigned the values T
and NIL, and NIL is the name of the null list 0.
4. value ( C O N S el e2), is the list that results from prefixing
value el onto the list value e2. Thus 2. So as not to describe a LISP function each time it is used, we
value ( C O N S ( Q U O T E A) ( Q U O T E (B C))) = (A B C). d e f i n e it permanently by typing (DEFUN f CaI ... vn) e).
T h e r e a f t e r ~f e I . . . e n) is evaluated by evaluating e with the
5. value ( E Q U A L el e2) is T if value el = value e2. Otherwise, its
variables v I . . . . , v n taking the values value el, . . . , v a l u e en
value is NIL. Thus
respectively. Thus, after we define (DEFUN FF (X) (COND
value ( E Q U A L (CAR ( Q U O T E (A B))) ( Q U O T E A)) = T,
( ( A T O M X) X) (T (FF (CAR X))))), typing (FF ( Q U O T E (CA B)
C))), gets A from LISP.
6. value CATOM e) - T if value e is an atom; otherwise its value Is
NIL.
3. W e have the permanent function definitions
7. value ( C O N D ( p t e I) ... (PB en)) = value ei, where Pi is the the
( D E F U N N U L L (X) ( E Q U A L X NIL)) and
first of the p's whose value is not NIL. Thus
( D E F U N C A D R (X) (CAR (CDR X))),
value ( C O N D ( ( A T O M ( Q U O T E A)) ( Q U O T E B)) ((QUOTE T)
( Q U O T E C))) = B. and similarly for arbitrary combinations of A and D.
8. A n atom v, regarded as a variable, may have a value. 4. ( L I S T e I . . . en) is defined for each n to be (CONS e I (CONS ...
( C O N S e n NIL))).
9. value ( ( L A M B D A (v I ... v,) e) e I ... ea) is the same as value e
but in an environment in which the variables v I ... vn take the
5. ( A N D p q) abbreviates (COND (p q) (T NIL)). ANDs with
values of the expressions e I ... en in the original environment.
more terms are defined similarly, and the propositional connectives
Thus
O R and N O T are used in abbreviating corresponding conditional
expressions.
value ( ( L A M B D A (X Y) (CONS (CAR X) Y)) (OJJOTE (A B))
(CDR ( Q U O T E (C D)))) = (A D).
H e r e are m o r e examples o f L I S P function definitions:

¢1978 Association for Computing Machinery, Inc. '2..1.5 ACM SIGPLAN Notices, Vol. 13, No. 8, August 1978
(DEFUN A L T (X) (COND ((OR (NULL X) (NULL (CDR X))) Here is EVAL.
X) (T (CONS (CAR X) (ALT (CDDR X))))))
(LRBEL EVBL (LRnBDB (E R)
(CONO ((ATOM E)
defines a f u n c t i o n t h a t gives alternate elements o f a list starting (CONO ((EQ E Nil.) NIL)
with the first element. Thus (ALT (QUOTE (A B C D E))) = (A ((EQ E T) T)
(T (CDR ((LRBEL
c E) RSSOC
(LflMBDfl (E fl)
(DEFUN SUBST (X Y Z) (COND ((ATOM Z) (COND ((EQUAL (COND ((NULL R) NIL)
((EQ E (CflgR R)) (CflR R))
Z Y) X) (T Z))) (T (CONS (SUBST X Y (CAR Z)) (SUBST X Y (T (RSSOC E (CDR f l ) ) ) ) ) )
( C D R Z)))))), E
R)))))
((ATOM (CAR E))
where Y is an atom, gives the result of substituting X for Y in Z. (COND ((EQ (CRR E) (QUOTEQUOTE)) (CflgR E))
Thus ((EQ (CRR E) (QUOTECRR))
(CflR (EVRL (CRDR E) R)))
((EQ (CAR E) (QUOTECUR))
(SUBST ( q U O T E (PLUS X Y)) ( q U O T E V) ( q U O T E (TIMES (CDR (EVRL (CRDR E) R)))
X V))) = (TIMES X (PLUS X Y)). ((EQ (CAR E) (QUOTECAOR))
(CflDR (EVRL (CAOR E) fl)))
((EQ (CRR E) (QUOTECRODR))
You may now program in LISP. Call LISP on a time- (CRDDR (EVRL (CRDR E) R)))
sharing computer, define some functions, type in a LISP ((EQ (CRR E) (QUOTECRflR))
(CRflR (EVflL (CADR E) A)))
expression, and LISP will output its value on your terminal. ((EQ (CRR E) (QUOTECADflR))
(CRDRR (EVRL (CROR E) A)))
((EQ (CflR E) (QUOTECADOAR))
(CRDDRR (EVRL (CflDR E) R)))
((EQ (CAR E) (QUOTEATOfl))
(RTOM (EVRL (CRDR E) A)))
T H E LISP INTERPRETER WRITTEN IN LISP ((EQ (CAR E) (QUOTENULL))
(NULL (EVRL (CADR E) A)))
((EQ (CflR E) (QUOTECONS))
The r u l e s we h a v e g i v e n f o r e v a l u a t i n g L I S P expressions (CONS (EVRL (CRORE) R) (EVRL (CflDDR E) R)))
can themselves be expressed as a LISP function (EVAL e a), ((EQ (CRR E) (QUOTEEO))
(EQ (EVflL (CROR E) R) (EVRL (CROONE) R)))
where e is an expression to be evaluated, and a is a list of ((EQ (CRR E) (QUOTECOND))
variable-value pairs, a is used in the recursion and is often ((LRBEL EVCUNO
(LRMBDA (U A) (COND ((EVRL (CflAR U) R)
initially NIL. The long LISP expression that follows is just such (EVAL (CflORR U)
an evaluator. It is presented as a single LABEL expressions with A))
all auxiliary functions also defined by LABEL expressions (T (EVCONO (CDR U)
internally, so that it references only the basic function of LISP and A)))))
some of abbreviations like CADR and friends. It knows about all (CDR E)
the functions that are used in its own definition so that it can A))
(T (EVRL (CONS (CDR ((LRBEL
e v a l u a t e i t s e l f e v a l u a t i n g some o t h e r expression. I t does n o t k n o w RSSOC
about DEFUNs or any features of LISP not explained in this (LflMBOR (E fl)
(COND
micro-manual such as functional arguments, property list functions, ((NULL R) NIL)
input-output, or sequential programs. ((EQ E (CRRR fl))
(CRR R))
(T (RSSOC E
T h e function EVAL can serve as an interpreter f o r LISP, (CUR R))))))
and LISP interpreters are actually made by hand-compiling EVAL (CflR E)
into machine language or by cross-compiling it on a machine for R))
(CDR E))
which a LISP system already exists. R))))
((EQ (CRRR E) (QUOTELRMBDfl)
(EVRL (CRDORRE)
T h e definition would have been easier to follow had we ((LABEL FFflPPEND
defined auxiliary functions separately rather than include them (LAflBDA (U V)
using LABEL. However, we would then have needed property list (COND ((NULL U) V)
(T (CONS (CRR U)
functions in order to make the EVAL self-applicable. These (FFflPPEND (COR U)
auxiliary functions are EVLIS which evaluates lists of expressions, V))))))
((LRBEL
E V C O N D which evaluates conditional expressions, ASSOC which PRIRUP
finds the value associated with a variable in the environment, and (LflMBUA (U V)
P A I R U P which pairs up the corresponding elements of two lists. (COND ((NULL U) NIL)
iT (CONS (CONS (CRR U) (CAN V))
'-(PR]RUP (CDR U)
(CON V ) ) ) ) ) ) )
(CflDflR E)
((LRBEL
EVLIS
(LRflBDR (U A)
(COND ((NULL U) NIL)
(T (CONS (EVflL (CAR U) R)
(EVL]S (CDR O)
A))))))
(CON E)
R))
A)))
((EQ (CRAR E) (QUOTELABEL))
(EVAL (CONS (CADOARE) (CDR E))
(CONS (CONS (CRORRE) (CRR E)) fl)))))

216

Anda mungkin juga menyukai