common-lispnaming-conventionspredicate

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


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?


Solution

  • 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.