98 lines
3.0 KiB
Plaintext
98 lines
3.0 KiB
Plaintext
;; Function To Be Tested: AND
|
|
;;
|
|
;; Source: Guy L Steele's CLTL
|
|
;; Section: 6.4 Logical Operators
|
|
;; Page: 82
|
|
;;
|
|
;; Created By: Kelly Roach
|
|
;;
|
|
;; Creation Date: 25-Jul-86
|
|
;;
|
|
;; Last Update: 25-Jul-86
|
|
;;
|
|
;; Filed As: {ERIS}<LISPCORE>CML>TEST>6-4-AND.TEST
|
|
;;
|
|
;;
|
|
;; Syntax: (AND &REST FORMS)
|
|
;;
|
|
;; Function Description:
|
|
;;
|
|
;; (AND FORM1 FORM2 ... ) evaluates each FORM, one at a time,
|
|
;; from left to right. If any FORM evaluates to NIL, the value NIL
|
|
;; is immediately returned without evaluating the remaining
|
|
;; FORMS. If every FORM but the last evaluates to a non-NIL value,
|
|
;; AND returns whatever the last FORM returns.
|
|
;; Therefore in general AND can be used both for logical operations,
|
|
;; where NIL stands for FALSE and non-NIL values stand for TRUE,
|
|
;; and as a conditional expression.
|
|
;; For example:
|
|
;;
|
|
;; (IF (AND (>= N 0)
|
|
;; (< N (LENGTH A-SIMPLE-VECTOR))
|
|
;; (EQ (ELT A-SIMPLE-VECTOR N) 'FOO))
|
|
;; (PRINC "FOO!"))
|
|
;;
|
|
;; The above expression prints FOO! if element N of A-SIMPLE-VECTOR
|
|
;; is the symbol FOO, provided also that N is indeed a valid index
|
|
;; for A-SIMPLE-VECTOR. Because AND guarantees left-to-right testing
|
|
;; of its parts, ELT is not called if N is out of range.
|
|
;;
|
|
;; To put it another way,
|
|
;; the AND special form does SHORT-CIRCUIT Boolean evaluation,
|
|
;; like the and then operator in Ada
|
|
;; and what in some Pascal-like languages is called cand (for ``conditional
|
|
;; and''); the Lisp AND special form is
|
|
;; unlike the Pascal or Ada and operator,
|
|
;; which always evaluates both arguments.
|
|
;;
|
|
;; In the previous example writing
|
|
;;
|
|
;; (AND (>= N 0)
|
|
;; (< N (LENGTH A-SIMPLE-VECTOR))
|
|
;; (EQ (ELT A-SIMPLE-VECTOR N) 'FOO)
|
|
;; (PRINC "FOO!"))
|
|
;;
|
|
;; would accomplish the same thing. The difference is purely stylistic.
|
|
;; Some programmers never use expressions containing side effects
|
|
;; within AND, preferring to use IF or WHEN for that purpose.
|
|
;;
|
|
;; From the general definition, one can deduce that
|
|
;; (AND X) = X. Also,
|
|
;; (AND) evaluates to T, which is an identity for this operation.
|
|
;;
|
|
;; One can define AND in terms of macro COND in this way:
|
|
;;
|
|
;; (AND X Y Z ... W) = (COND ((NOT X) NIL)
|
|
;; ((NOT Y) NIL)
|
|
;;
|
|
;; ((NOT Z) NIL)
|
|
;; ...
|
|
;; (T W))
|
|
;;
|
|
;;
|
|
;; See IF and macro WHEN, which are sometimes stylistically
|
|
;; more appropriate than AND for conditional purposes.
|
|
;; If it is necessary to test whether a predicate is true
|
|
;; of all elements of a list or vector (element 0 AND element 1 AND
|
|
;; element 2 AND...), then the function function EVERY may be useful.
|
|
;;
|
|
;; Argument(s): Any number of Lisp objects.
|
|
;;
|
|
;; Returns: A Lisp object.
|
|
;;
|
|
|
|
(DO-TEST "TEST AND 1"
|
|
(AND (EQ (AND) T)
|
|
(EQ (AND T) T)
|
|
(EQ (AND NIL) NIL)
|
|
(EQ (AND 123) 123)
|
|
(EQ (AND 'ATOM) 'ATOM)
|
|
(EQ (AND T T) T)
|
|
(EQ (AND T NIL) NIL)
|
|
(EQ (AND T 23 100) 100)
|
|
(EQ (AND 100 T 23) 23)
|
|
(EQ (AND T 1 T 2 T 3) 3)
|
|
(EQ (AND T T 10 20 T) T)))
|
|
|
|
STOP
|