Common Lisp the Language, 2nd Edition


next up previous contents index
Next: Logical Values Up: Common Lisp the Language Previous: Control of Time

6. Predicates

A predicate is a function that tests for some condition involving its arguments and returns nil if the condition is false, or some non-nil value if the condition is true. One may think of a predicate as producing a Boolean value, where nil stands for false and anything else stands for true. Conditional control structures such as cond, if, when, and unless test such Boolean values. We say that a predicate is true when it returns a non-nil value, and is false when it returns nil; that is, it is true or false according to whether the condition being tested is true or false.

By convention, the names of predicates usually end in the letter p (which stands for ``predicate''). Common Lisp uses a uniform convention in hyphenating 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 if and only if there is a hyphen in the existing name. For example, number begets numberp but standard-char begets standard-char-p. On the other hand, if the name of a predicate is formed by adding a prefixing qualifier to the front of an existing predicate name, the two names are joined with a hyphen and the presence or absence of a hyphen before the final p is not changed. For example, the predicate string-lessp has no hyphen before the p because it is the string version of lessp (a MacLisp function that has been renamed < in Common Lisp). The name string-less-p would incorrectly imply that it is a predicate that tests for a kind of object called a string-less, and the name stringlessp would connote a predicate that tests whether something has no strings (is ``stringless'')!

The control structures that test Boolean values only test for whether or not the value is nil, which is considered to be false. Any other value is considered to be true. Often a predicate will return nil if it ``fails'' and some useful value if it ``succeeds''; such a function can be used not only as a test but also for the useful value provided in case of success. An example is member.

If no better non-nil value is available for the purpose of indicating success, by convention the symbol t is used as the ``standard'' true value.




next up previous contents index
Next: Logical Values Up: Common Lisp the Language Previous: Control of Time


AI.Repository@cs.cmu.edu