Computer Language: Predicate Function, Terminology, and Naming Convention

,

Meaning of “Predicate” in Logic and Computer Science

in math and programing, there's the term “predicate”. In first order logic, “predicate” is a unary relation. For example, p(x) means p(x) is true. In pseudo-code, you are trying to assert eval(f(x)) == True.

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

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 Computer Languages

in emacs lisp and Common Lisp, by convention, predicate function names end with “p”. For example, in emacs lisp you have:

in emacs lisp, not all functions ending in p is a predicate. For example: {pop, defgroup, make-sparse-keymap, forward-sexp}

in Scheme Lisp and Clojure Lisp, 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.

in Mathematica, they end with “Q”. Mathematica uses Q because the character set of identifiers are English letters A to Z only. (and that's because all symbols/punctuations are used for operators in the syntax layer what lispers calls mexp.) Since only letters can be used, and “p” is not intuitive, thus “Q”, standing for Question.

examples of Mathematica predicate:

〔➤ Mathematica vs Lisp Syntax

in Ruby, it picked up from Scheme Lisp, and ends with “?”. For example, here's predicate methods for integer:

〔➤ Ruby Tutorial by Example

blog comments powered by Disqus