Prolog:比较 2 个列表并找出第一个列表中的至少一个成员是否存在于另一个列表中 [英] Prolog: Compare 2 Lists and find out if at least one member of the first list exists in the other one

查看:38
本文介绍了Prolog:比较 2 个列表并找出第一个列表中的至少一个成员是否存在于另一个列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了进一步了解 prolog(以及为了解决我的作业),我遇到了一种情况,我需要比较 2 个列表并找出至少一个元素匹配...
这是我想要做的一个例子:

In the quest of learning more about prolog (and in the interest of solving my assignment), I have come across a situation where I need to compare 2 lists and find out if AT LEAST ONE element match ...
Here is an example what I want to do:

?-match([a,b,c],[x,y,z]).
no.

?-match([a,b,c],[x,y,b]).
yes.

我目前的解决方案:

compare_list([],[]).
compare_list([],_).
compare_list([L1Head|L1Tail],List2):-
    member(L1Head,List2),
    compare_list(L1Tail,List2).

但是当 List1 的所有成员都存在于 List2 中时,此解决方案给出了 true

but this solution gives a true when all the members of List1 are present in List2!

请大家,不要认为我在作业中作弊,问题要复杂得多,我只是被困在这一点上,需要帮助才能摆脱这个棘手的角落……否则我已经完成了整个作业我自己!

Please people, don't think that I am cheating on an assignment, the problem is much much more complex, I am just stuck at this point and need help to get out of this sticky corner... otherwise I have done the entire assignment myself!

推荐答案

我将评论您所做的尝试,这很接近,但还不够:

I'll comment on the attempt you've made, which is close, but not quite there:

compare_list([],[]).
compare_list([],_).

compare_list/2 的第一个子句表示空列表在空列表中至少有一个元素.第二个表示空列表在任何其他列表中至少有一个元素.所以第一个子句是多余的(它已经被第二个子句覆盖了).您是否希望这是真的(空列表在任何其他列表中有一个元素)取决于您.由于空列表没有成员,人们可能会认为这是一个失败案例(因此,不会被声明为真),但如果您愿意,您可以根据定义将其称为真.但是,一旦正确定义了谓词,它将在递归情况下引起一些问题,因为减少到 [] 将变为真,最终它可能会发现任何列表在任何列表中都有元素(哎呀!).我会忽略这两个条款,并认为这种情况是失败的.

The first clause for compare_list/2 says that the empty list has at least one element in the empty list. The second says the empty list has at least one element in any other list. So the first clause is redundant (it's already covered by the second). Whether you want this to be true (that an empty list has an element in any other list) is up to you. Since an empty list has no members, one might consider this a failure case (and, thus, not be declared as true) but you can call it true by definition if you wish. However, it will cause some issues in the recursive case once you have your predicate correctly defined since reducing down to [] will become true and ultimately it might find any list has elements in any list (oops!). I'd leave these two clauses out and consider this case a fail.

compare_list([L1Head|L1Tail], List2):-
    member(L1Head, List2),
    compare_list(L1Tail, List2).

这表示第一个列表在第二个列表中有一个元素,如果:(1)列表的头部是第二个列表的成员,并且(2)第一个列表的尾部在第二个列表中有一个元素列表.这听起来在逻辑上正确吗?如果您仔细考虑一下,假设没有额外的 compare_list/2 子句,那么只有当第一个列表的每个元素都是第二个列表的成员时,这才是正确的,正如您所观察到的那样.

This says that the first list has an element in the second list if: (1) the head of the list is a member of the second list, AND (2) the tail of the first list has an element in the second list. Does this sound logically correct? If you think this through, given there are no additional compare_list/2 clauses, this is true only if EVERY element of the first list is a member of the second list, as you have observed.

最后,您错过了第一个列表的头部不是第二个列表的成员的情况.这不一定是失败的,因为第一个列表的尾部可能有第二个列表中的成员,即使第一个元素(头部)不是成员.

Finally, you're missing the case where the first list has a head that is not a member of the second list. This shouldn't necessarily be a failure since the tail of the first list may have a member in the second list, even if the first element (head) is not a member.

这篇关于Prolog:比较 2 个列表并找出第一个列表中的至少一个成员是否存在于另一个列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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