List 如何在Prolog中检查嵌套列表的两个元素是否是相同的索引?

List 如何在Prolog中检查嵌套列表的两个元素是否是相同的索引?,list,prolog,List,Prolog,如果元素X和元素Y在各自的嵌套列表中具有相同的位置,我想创建一个与位置相同的谓词(B,X,Y),该谓词为true 比如说, 相同的位置([[b,c,f],[a,d,g],[h,e]],c,d)将返回true same_position(L,E1,E2):- position(L,E1,N), position(L,E2,N). position(LL,E,N):- member(L,LL), nth0(N,L,E). ?- same_position([[b,

如果元素X和元素Y在各自的嵌套列表中具有相同的位置,我想创建一个与位置相同的谓词(B,X,Y),该谓词为true

比如说,

相同的位置([[b,c,f],[a,d,g],[h,e]],c,d)
将返回true

same_position(L,E1,E2):-
    position(L,E1,N),
    position(L,E2,N).

position(LL,E,N):-
    member(L,LL),
    nth0(N,L,E).

?- same_position([[b,c,f],[a,d,g],[h,e]],c,d).
true

?- same_position([[b,c,f],[a,d,g],[h,e]],b,N).
N = b ;
N = a ;
N = h ;
false.
因此,使用你另一个问题中的谓词,这非常简单。

以下是我的方法:

1。位置谓词检查两个元素的位置

2。然后比较这些值

position([H|T],Element1,Element2):-
    checkElement([H|T],Element1,P1),
    checkElement([H|T],Element2,P2),
    P1=P2.
checkElement([],_,_).
checkElement([H|T],Element,P):-
    (   member(Element,H)->
        nth0(P,H,Element);checkElement(T,Element,P)).
示例:-

?-position([[b,c,f],[a,d,g],[h,e]],b,g).
false
?-position([[b,c,f],[a,d,g],[h,e]],b,a).
true
false

提示:请查看
nth0/2