What is a predicate?
作为一个业余的编码员,我缺乏一些基础知识。最近几天我一直在读一些东西,"谓词"这个词不断出现。我非常感谢你对这个问题的解释。
谓词的定义可以在各种源(如这里)联机找到,如下所示:
A logical expression which evaluates to TRUE or FALSE, normally to direct the execution path in code.
参考:软件测试。作者:马修·海登
对或错的陈述。在编程中,它通常是一个为某些输入返回布尔值的函数。
最常见的(我猜)是在高阶函数的上下文中使用的。例如,
javascript中的示例:
1 2 | lessThanTen = function(x) { return x < 10; } [1,7,15,22].filter(lessThanTen) --> [1,7] |
函数
谓词不仅仅是一个计算结果为"真"或"假"的表达式,它还有很多优点。"谓词"一词用来指一个决定某事物是真是假的表达式。或者换句话说,它做出一个断言,并基于此返回真或假。
例如(在c)中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /*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; } |
我明白我在这里所说的完全是语义上的不同,但这就是这个问题的意义所在,对吗?语义学?
用非程序术语;问题。通常是一个普通的问题,可以问很多问题。
- 它是红色的吗?
- 它是一只狗吗?
- 它是他们所有的吗?
产生布尔值1的基本评估。它通常指的是表示这种类型的计算的函数或对象。
1:布尔值,不一定指声明为
首先,让我们看一看常规字典,看看它对谓词是什么的描述:
《牛津美国词典》(1980年):
n. a part of a sentence that says something about the grammatical subject, as"is short" in"life is short"
这里还有一句话:"约翰很高。"谓词是"很高"。如你所见,它修改或描述了主题,另一个类似于
IBM的技术术语表提供了几种定义,但最适合的定义是:
An expression used as part of a filter, consisting of a data item, an operator, and a value
下面是使用SQL的示例:
1 2 3 | SELECT name FROM tableA WHERE name ="john"; |
这段代码中的谓词是
考虑到这个概念的语法意义来推断编程概念可能是有用的。
维基百科:
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.
逻辑术语:
An operator in logic which returns
either true or false.
来自MathWorld
返回布尔值的函数。谓词在函数和OO编程中经常被用来从数据结构中选择值的子集,特别是列表和其他集合。您可以在Haskell和Smalltalk的标准库中找到许多示例。
从C++引物第五(第十章3.1节):
A predicate is an expression that can be called and that returns a value that can be used as a condition.
另见章节定义术语部分:
predicate : Function that returns a type that can be converted to
bool .
我不知道我说的上下文是否正确,但C中有一个
例如,以下方法(类型为
1 2 3 4 | public bool MyPredicate(int x) { return x > 5; } |
我不确定这是如何解释为更一般的情况,但这是一个开始。有关详细信息,请单击此处。
还有一些相关的,与数据库相关的谓词:
http://www.tizag.com/sqlttutorial/sqlpredicates.php