Predicate in Programing Languages and Naming

By Xah Lee. Date: . Last updated: .

What is Predicate in Computer Science

In programing language, “predicate” just means a function that return true or false.

In first order logic, “predicate” is a unary relation. For example, p(x) means p(x) is true. Note, p(x) is an assertion.

this is important and often confusing. Here's a concrete example. Suppose you want to say that 3 is a integer:

In the above example, “isInteger” is the predicate, but their meaning is critically different.

Naming Convention of Predicate in Programing Languages

Endin in p. (Emacs Lisp, Common Lisp)

in emacs lisp and Common Lisp, by convention, predicate function names end with “p”.

emacs lisp example:

In emacs lisp, it is inconsistent. Some ends in “-p”, some just “p”, and not all functions ending in p is a predicate. E.g. {pop, defgroup, make-sparse-keymap, forward-sexp}

Endin in Question Mark. (Scheme Lisp, Clojure Lisp, Ruby)

in Scheme Lisp and Clojure Lisp, ruby, the convention is to name predicate ending with a question mark “?”.

this is better. Because the question mark is more intuitive. The “p” is incomprehensible, and the term “predicate” came from history of logic.

clojure example:

ruby example:

Ending in Q. Wolfram Language

in Mathematica, predicate ends with “Q”, standing for Question.

Examples:

sigils war, magic chars in variable name