• Aucun résultat trouvé

13 More Functions

Dans le document 2 The Atom Table and the Number Table (Page 28-31)

Exercise 12.4: Is (LAMBDA NIL 3.1415926) a legal λ-expression?

Solution 12.4: Yes.

The name LAMBDA and the term λ-expression are borrowed from the so-called λ-calculus of Alonzo Church [Kle52]. This is a mathematical invention used to explore the syntactic and semantic nature and power of substitution. It is, in some sense, a theory of macro languages.

13 More Functions

There are many builtin functions in LISP which are not logically required to be builtin. They are there for convenience, and in some cases because they are faster that way. The three functions presented below are builtin functions which have definitions in terms of other more basic functions and special forms.

• APPEND – function Defined by:

(SETQ APPEND (LAMBDA (X Y)

(COND ((EQ X NIL) Y) ((ATOM X) (CONS X Y))

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

This version ofAPPENDis slightly more general than the commonly-found definition which omits the((ATOM X) (CONS X Y))pair. Given twolists(a1 a2 . . . ah)and(b1 b2 . . . bk)as input, the list(a1 a2 ... ah b1 b2 . . . bk)is produced as the output. The input is not damaged in any way. In a sense, this function embodies the essence of LISP. When you understand in detail at the machine level what happens during the application of this function to arguments, then you understand the essence of LISP.

Exercise 13.1: What isv[(APPEND (QUOTE (A . NIL)) T)]? What isv[(APPEND (QUOTE (A B)) NIL)]?

What is v[(APPEND T T)]?

Exercise 13.2: Suppose the ordinary atom X has the value (NIL . (T . NIL)) and that the ordinary atom Y has the value (X . (Y . NIL)), where these S-expressions are stored in the list area as follows.

28 13 MORE FUNCTIONS atom table

name type value

1 NIL 8 1 –

2 T 8 2 –

3 X 0 1 –

4 Y 0 2 –

list area 1 (−1,3) 2 (−3,4) 3 (−2,−1) 4 (−4,−1) 5 first free

Assume new list area nodes are allocated and used in the order 5, 6, . . . , etc. Tabulate the changes which occur in the list area when (APPEND X Y) is executed, and present the resulting contents of the list area. (Actually, the typed-pointer value denoted by −1 isoa(1) = 1000 :: 1.

What exactly is the typed-pointer denoted by −3?)

• REVERSE – function Defined by:

(SETQ REVERSE (LAMBDA (X)

(COND ((ATOM X) X)

(T (APPEND (REVERSE (CDR X)) (CONS (CAR X) NIL))))))

Exercise 13.3: Suppose the functions Aand B are defined by

(SETQ A (LAMBDA (X Y)

(COND ((EQ X NIL) Y)

(T (B (REVERSE X) Y)))))

and

(SETQ B (LAMBDA (X Y)

(COND ((EQ X NIL) Y)

(T (B (CDR X) (CONS (CAR X) Y))))))

What does A do?

Solution 13.3: The function A is another version of APPEND. It behaves the same way APPEND

does on list arguments.

• EQUAL – function Defined by:

(SETQ EQUAL (LAMBDA (X Y)

(COND ((OR (ATOM X) (ATOM Y)) (EQ X Y))

((EQUAL (CAR X) (CAR Y)) (EQUAL (CDR X) (CDR Y))) (T NIL))))

Exercise 13.4: Is the last (T NIL) pair appearing in the definition ofEQUALnecessary?

Solution 13.4: No, as we have definedCOND herein it is not necessary, but it is harmless, and it is required in some dialects of LISP where a CONDdoes not have a NILfinal value by default.

Exercise 13.5: Does theEQUAL function consume list area nodes during its application?

Solution 13.5: No. TheCONS function is not applied directly or indirectly.

Exercise 13.6: Define a LISP function LENGTH which takes a list as input and returns the number of elements in the list as output.

Exercise 13.7: Define a LISP predicate LISTP which returns T if its assignment is a list and which returns NILotherwise.

Solution 13.7: DefineLISTP by

(SETQ LISTP (LAMBDA (S) (COND ((ATOM S) (EQ S NIL)) (T (LISTP (CDR S))))))

Exercise 13.8: Define a LISP predicate MEMBERwhich returns Tif the input S-expression,a, is an element of the input list, s, and which returns NILotherwise.

Solution 13.8: DefineMEMBER by:

(SETQ MEMBER (LAMBDA (A S)

(COND ((EQ S NIL) NIL) ((EQUAL A (CAR S)) T) (T (MEMBER A (CDR S))))))

Exercise 13.9: Define a LISP function PLACE, which is like MEMBER in that an input list s is searched for an S-expressiona, but the remainder of the listsafter the point at whichais found is returned as the result, or the atom NULL is returned as the result if a is not an element of s.

Why isNULL specified as the atom which is returned to indicate a failure?

Exercise 13.10: Define a LISP predicate DEEPMEM which returns T if the input S-expression a occurs as an element of the input list s, or as an element of any sublist of s at any level, and returns NILotherwise. You may assume thatsis a pure list, all of whose elements are atoms or pure lists.

Solution 13.10: Define DEEPMEMby:

(SETQ DEEPMEM (LAMBDA (A S)

(COND ((ATOM S) NIL)

((OR (EQUAL A (CAR S)) (DEEPMEM A (CAR S))) T) (T (DEEPMEM A (CDR S))))))

Exercise 13.11: Define a LISP function TRUNC which takes a non-empty list s as input and returns a list identical to s, but with the last element removed.

Exercise 13.12: Define a LISP function DEPTH which takes an S-expression a as input and returns 0 if a is an atom and returns the number of levels in the binary tree picture of a otherwise. Thus the depth of the S-expression a is the maximum number of parentheses pairs which enclose an atom in a.

30 14 DEFINING SPECIAL FORMS Exercise 13.13: What happens when ((SETQ G (LAMBDA (X) (CONS X (G X)))) 3) is typed-in to the LISP interpreter?

Solution 13.13: First the value of the ordinary atomGwill become the specified function, and then the LISP interpreter will run out of stack space because of the infinite recursion specified inGapplied to 3. Note that the list area will not be exhausted, since no CONSapplication is ever actually consumated.

Exercise 13.14: Define aflatlist to be a list whose elements are atoms. Write a LISP function

FLATP to test whether an S-expression is flat. Write another LISP functionFLAT which returns a flat list containing all the atoms found within the input S-expression x.

Exercise 13.15: Define a LISP predicate PURE which takes an S-expression x as input and returns Tifx is a pure list and returnsNIL otherwise.

Exercise 13.16: Can there be two list area entries Pi and Pj with i6=j such that Pi =Pj? That is, does list-node sharing preclude duplicate list-nodes existing?

Solution 13.16: Yes, duplicate list-nodes can exist. But it is interesting to contemplate ways in which duplicate list-nodes can be avoided.

Dans le document 2 The Atom Table and the Number Table (Page 28-31)

Documents relatifs