序言两个列表完全相同 [英] prolog two lists are exactly the same

查看:69
本文介绍了序言两个列表完全相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个函数,如果两个列表完全相同(元素顺序很重要),则返回true.

I want to write a function that returns true if two lists are exactly the same(order of elements matters).

我这样尝试过:

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

当两个列表相同时,它返回true,如果有,我也希望

It returns true while two lists are the same, also I expect if I have

?- same(X, [1, 2, 3]).

我希望它退回

X = [1, 2, 3].

但是,如果输入是这样,它将不起作用.这是我得到的一些示例输出:

But it doesn't work if input is like this. Here are some sample outputs I got:

?- same([1, 2], [1, 2]).

true.

?- same([2, 1], [1, 2]).

false.

?- same(X, [1, 2, 3]).

false.

?- same([1, 2, 3], [1, 2, X]).

false.

如何解决?

推荐答案

问题是您使用的是==/2(检查两个项目是否实例化相同)而不是=/2(检查两个项目是否统一)或无法更改).只需更改为统一:

The problem is that you're using ==/2 (checking whether two items are instantiated the same) rather than =/2 (checks if two items are unified or unifiable). Just change to unification:

same([], []).

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

这将具有您想要的行为:

Then this will have the behavior you're looking for:

| ?- same(X, [1, 2, 3]).

X = [1,2,3] ? a

no
| ?- same([1, 2], [1, 2]).

(1 ms) yes
| ?- same([2, 1], [1, 2]).

no
| ?- same([1, 2, 3], [1, 2, X]).

X = 3

(1 ms) yes
| ?- same([A,B,C], L).

L = [A,B,C]

yes
% In this last example, A, B, and C are variables. So it says L is [A,B,C],
% whatever A, B, and C are.

如果在Prolog中查询X == 3,并且X未绑定到值3,或者只是未绑定,则它将失败.如果X是未绑定的,并且您查询X = 3,则Prolog将X3统一(绑定),它将成功.

If you query X == 3 in Prolog, and X is not bound to the value 3, or it is just unbound, it will fail. If X is unbound and you query, X = 3, then Prolog will unify X (bind it) with 3 and it will succeed.

有关=/2==/2之间差异的更多信息,请参见

For more regarding the difference between =/2 and ==/2, see What is the difference between == and = in Prolog?

您也可以使用maplist作为一个不错的紧凑型解决方案. maplist对于遍历列表非常方便:

You can also use maplist for a nice, compact solution. maplist is very handy for iterating through a list:

same(L1, L2) :- maplist(=, L1, L2).

此处,出于与上述完全相同的原因,仍使用统一(=/2).


最后,正如@Boris指出的那样,在Prolog中,统一谓词将在整个列表中起作用.在这种情况下,就足够了:

Here, unification (=/2) is still used for exactly the same reason as above.


Finally, as @Boris points out, in Prolog, the unification predicate will work on entire lists. In this case, this would suffice:

same(L1, L2) :- L1 = L2.

或等效地:

same(L, L).  % Would unify L1 and L2 queried as same(L1, L2)

如果列表相同,将成功,或者尝试通过依次统一每个元素来统一它们.

This will succeed if the lists are the same, or will attempt to unify them by unifying each element in turn.

| ?- same([1,2,X], [1,2,3]).    % Or just [1,2,X] = [1,2,3]

X = 3

yes
| ?- same([1,2,X], [1,2,3,4]).  % Or just [1,2,X] = [1,2,3,4]

no

在列表处理中,为了说明起见,以前更详细的方法被认为是一种练习.但是,用于比较和/或统一列表的最简单,最正确的方法是L1 = L2.

The prior more elaborate approaches are considered an exercise in list processing for illustration. But the simplest and most correct method for comparison and/or unification of lists would be L1 = L2.

这篇关于序言两个列表完全相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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