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?
-
2cs.cmu.edu/Groups/AI/html/cltl/clm/node69.html "By convention, the names of predicates usually end in the letter p (which stands for ``predicate'')."Raymond Chen– Raymond Chen2021年12月31日 19:11:05 +00:00Commented Dec 31, 2021 at 19:11
-
catb.org/jargon/html/p-convention.htmlkirjosieppo– kirjosieppo2021年12月31日 20:52:16 +00:00Commented Dec 31, 2021 at 20:52
5 Answers 5
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.
Comments
It stands for predicate, i.e. functions with a yes or no answer. Example: integerp (is it an integer?), realp, rationalp, etc..
Comments
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
1 Comment
NULL. If it were following the convention it would be NULLP, but it came from earlier dialects of Lisp and predates the naming convention.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.
Comments
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).
1 Comment
Explore related questions
See similar questions with these tags.