340 lines
113 KiB
Plaintext
340 lines
113 KiB
Plaintext
Copyright (c) 1986 Xerox Corporation. All rights reserved.
|
||
|
||
12.8.8 Examples
|
||
1
|
||
|
||
Example: (match X with (-- 'A --))
|
||
-- matches any arbitrary segment. 'A matches only an A, and the second -- again matches an arbitrary segment; thus this translates to (MEMB 'A X).
|
||
Example: (match X with (-- 'A))
|
||
Again, -- matches an arbitrary segment; however, since there is no -- after the 'A, A must be the last element of X. Thus this translates to: (EQ (CAR (LAST X)) 'A).
|
||
Example: (match X with ('A 'B -- 'C $3 --))
|
||
CAR of X must be A, and CADR must be B, and there must be at least three elements after the first C, so the translation is:
|
||
(AND (EQ (CAR X) 'A)
|
||
(EQ (CADR X) 'B)
|
||
(CDDDR (MEMB 'C (CDDR X))))
|
||
Example: (match X with (('A 'B) 'C Y_$1 $))
|
||
Since ('A 'B) does not end in $ or --, (CDDAR X) must be NIL. The translation is:
|
||
(COND
|
||
((AND (EQ (CAAR X) 'A)
|
||
(EQ (CADAR X) 'B)
|
||
(NULL (CDDAR X))
|
||
(EQ (CADR X) 'C)
|
||
(CDDR X))
|
||
(SETQ Y (CADDR X))
|
||
T))
|
||
Example: (match X with (#1 'A $ 'B 'C #1 $))
|
||
#1 is implicitly assigned to the first element in the list. The $ searches for the first B following A. This B must be followed by a C, and the C by an expression equal to the first element. The translation is:
|
||
[PROG ($$2)
|
||
(RETURN
|
||
(AND (EQ (CADR X) 'A)
|
||
(EQ [CADR (SETQ $$2 (MEMB 'B (CDDR X] 'C)
|
||
(CDDR $$2)
|
||
(EQUAL (CADDR $$2) (CAR X]
|
||
Example: (match X with (#1 'A -- 'B 'C #1 $))
|
||
Similar to the pattern above, except that -- specifies a search for any B followed by a C followed by the first element, so the translation is:
|
||
[AND (EQ (CADR X) 'A)
|
||
(SOME (CDDR X)
|
||
(FUNCTION (LAMBDA ($$2 $$1)
|
||
(AND (EQ $$2 'B)
|
||
(EQ (CADR $$1) 'C)
|
||
(CDDR $$1)
|
||
(EQUAL (CADDR $$1) (CAR X]
|
||
|