What is a predicate?

前端 未结 12 1675
无人及你
无人及你 2020-12-04 12:16

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 ve

相关标签:
12条回答
  • Also somewhat related, there are database-related predicates:

    http://www.tizag.com/sqlTutorial/sqlpredicates.php

    0 讨论(0)
  • 2020-12-04 12:25

    It is probably useful to consider the grammatical meaning of the concept to extrapolate the programming concept.

    From wikipedia:

    In traditional grammar, a predicate is one of the two main parts of a sentence (the other being the subject, which the predicate modifies). For the simple sentence "John [is yellow]," John acts as the subject, and is yellow acts as the predicate, a subsequent description of the subject headed with a verb.

    In current linguistic semantics, a predicate is an expression that can be true of something. Thus, the expressions "is yellow" or "is like broccoli" are true of those things that are yellow or like broccoli, respectively. This notion is closely related to the notion of a predicate in formal logic, which includes more expressions than the former one, like, for example, nouns and some kinds of adjectives.

    In logic terms:

    An operator in logic which returns either true or false.

    from MathWorld

    0 讨论(0)
  • 2020-12-04 12:31

    First let's take a look at a regular dictionary and see what it says about what a predicate is:

    Oxford American Dictionary(1980):

    n. a part of a sentence that says something about the grammatical subject, as "is short" in "life is short"

    Here is another sentence: "John is tall." the predicate is "is tall". As you can see it modifies or describes the subject, another term that is similar to predicate is adjective. In essence it's a modifier.

    IBM's technology glossary provides several definitions but, the one fits best is this one:

    An expression used as part of a filter, consisting of a data item, an operator, and a value

    Here is an example using SQL:

    SELECT name
    FROM tableA
    WHERE name = "john";
    

    The predicate in this code would be name = "john". It has all the components of the IBM definition and also fits with the regular definition of predicate. The subject being name and the predicate being name = "john".

    0 讨论(0)
  • 2020-12-04 12:31

    The best S.O. answer around predicates, that I have found, is on a duplicate question.

    To summarize, in natural languages a predicate is the part of the sentence that describes a subject.

    Jane is tall

    Jane is the subject and is tall is the predicate.

    In computer science we are not interested in asserting a fact about a subject but rather testing if something is true or false.

    jane.isTall();
    

    Here jane is some object with a predicate method that will return either true or false.

    0 讨论(0)
  • I don't know if I'm speaking in the correct context, but there is a Predicate class in C# which is essentially a delegate which, given an item, determines whether or not the object meets a set of criteria.

    For example, the following method, which is of type Predicate<int>, could be used to select all integers greater than 5:

    public bool MyPredicate(int x)
    {
       return x > 5;
    }

    I'm not sure how this translates into the more general case, but it's a start. For more info, click here.

    0 讨论(0)
  • 2020-12-04 12:35

    A function that returns a boolean. Predicates are used a lot in functional and OO programming to select subsets of values from data structures, especially lists and other collections. You'll find plenty of examples in the standard libraries for Haskell and Smalltalk.

    0 讨论(0)
提交回复
热议问题