Prolog检查列表中的第一个元素是否不相等,列表中的第二个元素是否相等 [英] Prolog check if first element in lists are not equal and second item in list is equal

查看:85
本文介绍了Prolog检查列表中的第一个元素是否不相等,列表中的第二个元素是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较2个列表,第一个元素不应该相等,第二个元素应该相等.

I want to compare 2 lists, the first element should not be equal, the second one should be equal.

示例数据库:

likes(josh,muse).
likes(sam,muse).
likes(josh,gnr).
likes(sam, radiohead).

因此对于相同的内容([josh,muse],[sam,muse]),它应该返回true.

so it should return true for same([josh,muse], [sam,muse]).

这是我到目前为止尝试过的:

This is what I tried so far:

same([H1|R1], [H2|R2]):-
    H1 \= H2,
    same(R1,R2).

每个组合都返回false.

This returned false for every combination.

推荐答案

在阅读您的问题时,您首先提供了一个事实数据库

In reading your question you first gave a database of facts

likes(josh,muse).
likes(sam,muse).
likes(josh,gnr).
likes(sam, radiohead).

但随后将列表用作谓词

same([josh,muse], [sam,muse]).

Paulo从列表开始回答,我将从事实开始回答.

As Paulo answered starting with list, I will answer starting with facts.

第一件事是创建一个谓词,以读取事实,执行一些逻辑并返回结果.

The first thing is to create a predicate that reads the facts, does some logic and returns results.

same_1(P1,P2,Item) :-
    likes(P1,Item),
    likes(P2,Item).

给出

?- same_1(P1,P2,Item).
P1 = P2, P2 = josh,
Item = muse ;
P1 = josh,
P2 = sam,
Item = muse ;
P1 = sam,
P2 = josh,
Item = muse ;
P1 = P2, P2 = sam,
Item = muse ;
P1 = P2, P2 = josh,
Item = gnr ;
P1 = P2, P2 = sam,
Item = radiohead.

因此,需要确保P1与P2不同.

So this needs to make sure P1 is not the same as P2.

same_2(P1,P2,Item) :-
    likes(P1,Item),
    likes(P2,Item),
    P1 \= P2.

给出

?- same_2(P1,P2,Item).
P1 = josh,
P2 = sam,
Item = muse ;
P1 = sam,
P2 = josh,
Item = muse ;
false.

仍然有两个有效的答案,但本质上是重复的.要删除这些重复项,需要存储所有结果,以便可以对照现有结果检查每个新结果,而不将其添加到当前结果中.同样,在存储结果之前,还需要对它们进行规范化处理,以便无论在最初创建时以哪种方式对名称进行排序,在保存之前进行比较时,它们的顺序都相同.

Still two answers that are valid but essentially a duplicate. To remove these duplicates requires storing all of the results so that each new result can be checked against existing results and not added to the current results. Also before storing the results they need to be normalized so that no matter which way the names are ordered when initially created they are in the same order when comparing them before saving them.

修改代码以创建规范化的条目.

Modifying the code to create normalized entries.

same_3(P1,P2,Item) :-
    likes(T1,Item),
    likes(T2,Item),
    T1 \= T2,
    normalize(T1,T2,P1,P2).

normalize(P1,P2,P1,P2) :- P1 @> P2.
normalize(P1,P2,P2,P1) :- P1 @=< P2.

返回

?- same_3(P1,P2,Item).
P1 = sam,
P2 = josh,
Item = muse ;
P1 = sam,
P2 = josh,
Item = muse ;
false.

请注意,此结果的名称顺序相同.

Notice that this result has the names in the same order.

现在仅保存结果生成时的结果,并仅向结果中添加唯一项.使用 setof/3 完成.

Now to just save the results as they are generated and only add unique items to the result. This is done using setof/3.

?- setof((P1,P2,Item),(same_3(P1,P2,Item)),Bag).
Bag = [(sam, josh, muse)].

这篇关于Prolog检查列表中的第一个元素是否不相等,列表中的第二个元素是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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