What is a predicate?

Being a hobbyist coder, I'm lacking some fundamental knowledge. For the last couple days I've been reading some stuff and the word "predicate" keeps reappearing. I'd very much appreciate an explanation on the subject.


Solution 1:

The definition of a predicate, which can be found online in various sources such as here, is:

A logical expression which evaluates to TRUE or FALSE, normally to direct the execution path in code.

Referencing: Software Testing. By Mathew Hayden

Solution 2:

In programming, a predicate is a function which returns either true or false for some input.

Most commonly (I guess) used in the context of higher-order function. E.g. filter is a function in many languages which takes a predicate and a list as arguments, and returns the items in the list for which the predicate is true.

Example in javascript:

function lessThanTen(x) { return x < 10; }
[1,7,15,22].filter(lessThanTen) --> [1,7]

The function lessThanTen is the predicate here, which is applied to each item in the list.

Solution 3:

A predicate isn't simply an expression that evaluates to true or false, there's more to it. The term "predicate" is used to refer to an expression that determines whether something is true or false. Or in other words, it makes an assertion and returns true or false based on that.

For example (in C#):

/*this is a predicate, as it's sole purpose is to make some 
 assertion about something.*/
bool IsNameBob(string name)
{
   return name == "Bob";
}

/*Whereas this is not a predicate, as it's performing an action
 then evaluating to true if it succeeds. */
bool DoSomethingCool() {
   try 
   {
       ImDoingSomethingCool();
   }
   catch
   {
      return false;
   }
   return true;
}

I understand what I've put here is purely a difference in semantics, but that's what this question was about right? Semantics?

Solution 4:

In non programing terms; a question. Typically a general question with place holders (like it and them) that can be asked of many things.

  • Is it red?
  • Is it a dog?
  • Is it owned by them?

Solution 5:

A basic evaluation that results in a boolean1 value. It often refers to a function or object that represents an evaluation of this type.

1: boolean used loosely, not necessarily referring to variables declared bool or boolean.