Star Sapphire Common LISP Home

Download Star Saphire
Index

6. PREDICATES

This chapter contains the following sections:

6.1 Predicate Conventions

6.2 Logical Values

6.3 Data Type Predicates

6.4 Logical Operators

A predicate is a function which tests a condition. A predicate returns nil if the condition is not met, and a non-nil value otherwise.

Predicates are often said to produce a Boolean value (true or false), where nil stands for false and non-nil stands for true nil can also sometimes be used for 'success' and non-nil for 'failure'.

The non-nil value which prediciates return is often t in Star Sapphire LISP and entries will often state this explicitly; however, code which tests for a true condition should test for (not nil) to be entirely conformant.

Examples of control structures which require such a predicate as part of their syntax include cond, if, when and unless.

6.1 Predicate conventions

The names of LISP predicates conventionally end in the letter p which stands for predicate. Common LISP also uses the following convention to hypenate names of predicates. If the name of the predicate is formed by adding a 'p' to an existing name, such as the name of a data type, a hyphen is placed before the final p only if there is a hyphen in the existing name.

For example, a predicate which tests for number-ness is numberp but the equivalent for standard-char is standard-char-p.

Additionally, if a predicate is named by prefixing a qualifier to an existing qualifier, the base predicate is kept intact and a hyphen is used to join the two halves.

For example, the predicate char-lessp has no hypen before its name because it is the character version of lessp (a MacLisp predicate which is called < in Common LISP). The name char-less-p is clumsy; it looks like it is a predicate which tests for the the lack of a character.

6.2 Logical Values

The names nil and are predefined constants in Common LISP.

nil and t are symbols like any other symbol. They appear to be treated as variables when evaluated. However, it is not permitted to modify their values, as if they were defined by defconstant.

6.3 Data Type Predicates

Perhaps the most important predicates in LISP are those that deal with data types; that is, given a data object one can determine whether or not it belongs to a given type, or one can compare two type specifiers.

6.3.1. General Type Predicates

If a data type is viewed as the set of all objects belonging to the type, then the typep function is a set membership test.

6.3.2. Specific Data Type Predicates

The following predicates test for individual data types.

null

symbolp

atom

consp

listp

numberp

integerp

rationalp

floatp

complexp

characterp

stringp

bit-vector-p

vectorp

simple-vector-p

simple-string-p

simple-bit-vector-p

arrayp

packagep

functionp

compiled-function-p

commonp

standard-char-p

string-char-p

streamp

random-state-p

readtablep

hash-table-p

pathnamep

6.3. Equality Predicates

Common LISP provides four predicates which test for equality of two objects. They form a hierarchy of increasingly more general tests for equality:

eq (least general)

eql

equal

equalp (most general)

The eq predicate is the most specific test; in Star Sapphire eq is true if and only if two objects have the same virtual address, or the same immediate representation (such as two instances of the number 3 or the character #\X).

The equalp predicate is the most general test; for instance, two arrays are equalp if and only if they have the same number of dimensions, the dimensions match, and the corresponding components are all themselves equalp.

If two objects satisfy any one of these equality predicates, then they also satisfy all those that are more general. That is, the two instances of the number 3, besides being eq are also eql, equal and equalp.

6.4 Logical Operators

Common LISP provides three operators on Boolean values:

and

or

not

Of these, and and or are also considered control structures because their arguments are evaluated conditionally; they are implemented as built-in macros. The function not necessarily examines its single argument, and so is a simple function.