What is a predicate?

前端 未结 12 1716
无人及你
无人及你 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条回答
  •  醉梦人生
    2020-12-04 12:39

    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?

提交回复
热议问题