MTS 8: LISP and SLIP in MTS
June 1976
function (READ) is called, and returns one S-expression which it reads
from the system input device, SCARDS.
Program Description (in an algebraic-type programming language)
RESTLIST = ((READ-IN-ARGUMENT . 0));
DO WHILE RESTLIST ¬= NIL;
LIST = (CAAR RESTLIST);
DEPTH = (CDAR RESTLIST);
RESTLIST = (CDR RESTLIST);
IF (LIST IS ATOM) THEN DO;
PRINT "ATOM" ":" LIST ", DEPTH" DEPTH;
END;
ELSE DO;
TEMP = (CAR LIST);
LIST = (CDR LIST);
IF LIST ¬= NIL THEN
RESTLIST = (CONS (CONS LIST DEPTH) RESTLIST);
ELSE;
RESTLIST = (CONS (CONS TEMP DEPTH+1) RESTLIST);
END;
END;
Program:
(PROG (LIST DEPTH TEMP RESTLIST)
(SETQ RESTLIST (LIST (CONS (READ) 0)) )
A (COND
((NOT RESTLIST) (RETURN ’DONE))
(T (SETQ LIST (UNCONS (UNCONS RESTLIST
RESTLIST ) DEPTH))
(COND ((ATOM LIST)
(MAPC ’PRIN1 (LIST ’ATOM ’: LIST ’"," ’DEPTH DEPTH))
(TERPRI))
(T (SETQ TEMP (UNCONS LIST LIST))
(COND (LIST
(SETQ RESTLIST (CONS(CONS LIST DEPTH) RESTLIST))))
(SETQ RESTLIST (CONS (CONS TEMP
(ADD1 DEPTH)) RESTLIST))
))))
(GO A))
Recursive Implementation of the Same Program:
(PROG NIL (
(LABEL ATOMPRINT (LAMBDA (RESTLIST)
(COND ((NOT RESTLIST) (RETURN ’DONE))
((ATOM (CAAR RESTLIST)) (MAPC ’PRIN1
(LIST ’ATOM ’: (CAAR RESTLIST)
’"," ’DEPTH (CDAR RESTLIST)))
(TERPRI)
(ATOMPRINT (CDR RESTLIST)))
( T (ATOMPRINT (GRAFT
34 LISP

MTS 8: LISP and SLIP in MTS
June 1976 Page Revised February 1979
(LIST (CONS (CAAAR RESTLIST) (ADD1 (CDAR RESTLIST))))
(AND (CDAAR RESTLIST) (LIST (CONS (CDAAR RESTLIST)
(CDAR RESTLIST))))
(CDR RESTLIST)))))))
(LIST (CONS (READ) 0))))
Output from Program with Input:
(A (B C) (D (E F (G H (I) J K) L))))))
ATOM : A , DEPTH 1
ATOM : B , DEPTH 2
ATOM : C , DEPTH 2
ATOM : D , DEPTH 2
ATOM : E , DEPTH 3
ATOM : F , DEPTH 3
ATOM : G , DEPTH 4
ATOM : H , DEPTH 4
ATOM : I , DEPTH 5
ATOM : J , DEPTH 4
ATOM : K , DEPTH 4
ATOM : L , DEPTH 3
DONE
MORE ABOUT FUNCTIONS ____________________
LAMBDA-Expressions __________________
As we noted earlier, the CAR of a form being EVALed is interpreted as
a function specification and an atomic CAR is interpreted as the name of
a function to be called.
However, the CAR of a form to be EVALed need not be an atom. It can
be an explicit function specification, called a LAMBDA-expression. The
basic form of a LAMBDA-expression is
(LAMBDA (A1...AN) S1...SN)
When a LAMBDA-expression appears as a function specification, it is
treated as a function where A1...AN are the dummy arguments, and S1...SN
is the body of the function. The dummy arguments A1...AN are bound to
the arguments which are passed to the function; in turn, S1...SN are
EVALed. Finally, A1...AN are unbound to their original VALUEs.
The value of the LAMBDA-expression is the value of SN.
A LAMBDA-expression may appear whenever a function specification is
required, for example, as the first argument of APPLY, MAP, MAPLIST,
etc. For example,
LISP 35
Previous Page Next Page