What does suffix "p" mean in common lisp functions?

Viewed 249
4 Answers

For Common Lisp there are a bunch of naming conventions. Adding a p or -p is for functions which return a truth value.

This does not mean that the function will return only t or nil. Other values of true are also possible:

Example of a predicate which will return a different value from t for true.

CL-USER 1 > (digit-char-p #\a)
NIL

CL-USER 2 > (digit-char-p #\1)
1                              ; any object other than nil
                               ; also means *true*

The convention to use ? instead is coming from the Scheme programming language.

It stands for predicate, i.e. functions with a yes or no answer. Example: integerp (is it an integer?), realp, rationalp, etc..

The extension 'p' stands for 'predicate'. A predicate is a test which returns a Boolean result of true or false. The idea of a predicate applies to most if not all languages.

An example is 'zero predicate' of a parameter, x, which returns true if x is zero, or false if it is not zero.

The style varies from language to language, and also it depends on the predicate name. Clojure's style is to add a question mark. Common Lisp's style is to add a 'p' to the name, usually. Common Lisp has an exception for predicate names containing a hyphen, when '-p' is added. The first example seems to want to do both styles, '?' and 'p', depending on what style the user is used to.

Coding of a predicate involves making a test and returning the result instead of acting on it.

Python:

def zerop (x):
   return x == 0

# zerop (0) => True
# zerop (1) => False

Common Lisp:

(defun my-zero-p (x)
   (= x 0))

; (my-zero-p 0) => t
; (my-zero-p 1) => nil

Because nobody mentioned it clearly yet, I wanted to add: you use -p, if the symbol to which you want to attach p contains already one or more -. If not, you just attach p wihout the hyphen.

So one-word would become one-word-p, but blue would become bluep.

Related