3

See for example: https://github.com/vindarel/cl-str#empty-emptyp-s

Or this: https://stackoverflow.com/a/33379360/12400477

I have seen this in several places, and I don't know what it means. Does the p suffix indicate the type the function returns? Does it always mean it returns a bool? Why do there seem to be two alternative symbols for the same function in the first example?

Rainer Joswig
140k11 gold badges230 silver badges357 bronze badges
asked Dec 31, 2021 at 19:06
2

5 Answers 5

4

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.

answered Dec 31, 2021 at 20:47
Sign up to request clarification or add additional context in comments.

Comments

2

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

answered Dec 31, 2021 at 19:41

Comments

2

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
answered Dec 31, 2021 at 20:29

1 Comment

There's also an exception for NULL. If it were following the convention it would be NULLP, but it came from earlier dialects of Lisp and predates the naming convention.
2

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.

answered Jan 3, 2022 at 12:28

Comments

0

The p stands for 'predicate' and the Lisp Hyperspec says:

predicate n. a function that returns a generalized boolean as its first value.

generalized boolean n. an object used as a truth value, where the symbol nil represents false and all other objects represent true. See boolean.

boolean n. an object of type boolean; that is, one of the following objects: the symbol t (representing true), or the symbol nil (representing false). See generalized boolean.

If the function returns nil or T I (and others use ? instead of p).

answered Jan 15, 2024 at 20:44

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.