Prolog 中的复合布尔表达式
在 Prolog 中,如何实现复合逻辑谓词(A 和 B)或(C 和 D)?
这似乎是一个简单的问题,但许多平易近人的在线教程对布尔表达式的描述不够详细。我认为你不能只写:
test(A, B, C, D) :- cond(A), cond(B); cond(C), cond(D).
那么你会怎么做呢?
In Prolog, how do you implement compound logical predicates like (A and B) or (C and D)?
This may seem like a simple question but many approachable online tutorials are not detailed enough on boolean expressions. I assume you can't just write:
test(A, B, C, D) :- cond(A), cond(B); cond(C), cond(D).
so how would you do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所指出的,您的原始示例
完全有效(假设解析符合您的预期)。你没有尝试过吗?
入门
逻辑与
逻辑或
组合
上面的解析为:
使用括号表示不同的所需绑定:
偶数更好的是,避免使用
;
OR 运算符并将替代项分解为单独的子句。序列比分支树结构更容易让人理解。将交替分解为多个子句可以简化测试/调试并提高理解力。因此,更
喜欢和
更
喜欢可能最重要的是,像这样将事情分成多个子句使得以后的维护更容易。结构如下:
添加另一个案例时你会做什么?当它扩展到 50 个替代方案时会怎样?
每个额外的替代方案都会增加通过该子句的代码路径的数量,从而使测试和理解变得更加困难。为了在测试中获得完整的代码覆盖率,必须单独测试许多替代路径。
使用等效结构,
添加替代项只需添加一个或多个附加子句,每个子句都可以单独测试。
专业程序员首先为几年后需要理解、更改和修复代码的人编写代码(提示:那个人可能就是你自己)。
As noted by others, your original example
is perfectly valid (assuming the parse is as you intended it). Didn't you try it?
A Primer
Logical AND
Logical OR
Combined
The above parses as:
Use parentheses to indicate a different desired binding:
Even better, eschew the
;
OR operator and break alternatives up into separate clauses. Sequence is much easier for people to comprehend than branching tree structures. Breaking alternation up into multiple clauses simplifies testing/debugging and improves comprehension. Therefore, preferover
and prefer
over
Possibly most importantly, breaking things up into multiple clauses like this makes later maintenance easier. With a structure like:
what do you do when you add another case? What about when it expands to 50 alternatives?
Each additional alternative increases the number of code paths through the clause, thus making testing and comprehension more difficult. To get complete code coverage in testing, a lot of alternative paths must be individually tested.
With the equivalent structure
Adding alternatives is simply a matter of adding additional clause or clauses, each of which can be tested in isolation.
A professional programmer writes first for the people who will, several years down the line, need to comprehend, change and fix that code (hint: that person might be yourself).
连接词的一个简单替代方法是使用子语句。对于析取,请使用多行。你的例子:
(A或B)和(C或D)怎么样?
请注意,这只是作为 joel76 答案的替代方案而提到的。
A simple alternative for conjunctions is to use substatements. For disjunctions, use multiple lines. Your example:
What about (A or B) and (C or D)?
Note that this is only mentioned as an alternative to joel76's answer.
测试(A,B,C,D):-(条件(A),条件(B)); (条件(C),条件(D))。
test(A, B, C, D) :- (cond(A), cond(B)); (cond(C), cond(D)).