依赖规则顺序 [英] Relying on rule order

查看:53
本文介绍了依赖规则顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了计算相同长度的两个列表之间的汉明距离,我使用 foldl(hamm, A, B, 0, R).hamm/4 的定义代码>:

To calculate the hamming distance between two lists of the same length, I use foldl(hamm, A, B, 0, R). with this definition of hamm/4:

hamm(A, A, V, V) :- !.
hamm(A, B, V0, V1) :- A \= B, V1 is V0 + 1.

第一条规则中的削减防止了不必要的回溯.但是,第二条规则可以写成不同的:

The cut in the first rule prevents the unnecessary backtracking. The second rule, however, could have been written differently:

hamm2(A, A, V, V) :- !.
hamm2(_, _, V0, V1) :- V1 is V0 + 1.

hamm2/4foldl/5 或 A 和 B 都接地的查询仍然是正确的.

and hamm2/4 will still be correct together with foldl/5 or for queries where both A and B are ground.

那么,是否有充分的理由更喜欢一个而不是另一个?或者是否有理由将规则保持在该顺序或改变它们?

So is there a really good reason to prefer the one over the other? Or is there a reason to keep the rules in that order or switch them around?

我知道查询

hamm(a, B, 0, 1).

是假的,而

hamm2(a, B, 0, 1).

是真的,但我不能完全确定哪个更有意义...

is true, but I can't quite decide which one makes more sense . . .

推荐答案

您已经发现了这些定义之间的差异:除了效率之外,您应该决定您的要求.你打算在你的数据结构中接受变量吗?这种编程风格引入了一些高级 Prolog 特性(不完整的数据结构).

You already spotted the differences between those definitions: efficiency apart, you should decide about your requirements. Are you going to accept variables in your data structures? Such programming style introduces some of advanced Prolog features (incomplete data structures).

无论如何,我认为第一种形式更准确(不太确定,我会说坚定不移的 4° 论证)

Anyway, I think the first form is more accurate (not really sure about, I would say steadfast on 4° argument)

?- hamm(a, B, 0, 1).
false.

?- hamm(a, B, 0, 0).
B = a.

而 hamm2 是

?- hamm2(a, B, 0, 1).
true.

?- hamm2(a, B, 0, 0).
B = a.

这篇关于依赖规则顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆