我需要在 prolog 中实现字典配对。这就是我所做的:
pp((A, B), (B,C), RES) :-
RES = [(A,C)].
pp((A, AA),[B|BS], RES) :-
pp((A, AA), B, RES1),
pp((A, AA), BS, RES2),
append(RES1, RES2, RES).
pp([A|AS], [B|BS], C) :-
pp(A, [B|BS], RES1),
pp(AS, [B|BS], RES2),
append(RES1, RES2, RES),
list_to_set(RES, C).
pp(_, _, C) :-
C = [].
输出:
?- pp([(a,b),(b,c)],[(b,c),(c,z)],C).
C = [ (a, c), (b, z)] ;
C = [ (a, c)] ;
C = [ (a, c)] ;
C = [ (a, c)] ;
C = [ (a, c)] ;
C = [ (a, c), (b, z)] ;
C = [ (a, c)] ;
C = [ (a, c)] ;
C = [ (a, c)] ;
C = [ (a, c)] ;
C = [ (b, z)] ;
C = [] ;
C = [] ;
C = [] ;
C = [] ;
C = [ (b, z)] ;
C = [] ;
C = [] ;
C = [] ;
C = [] ;
C = [ (b, z)] ;
C = [] ;
C = [] ;
C = [] ;
C = [] ;
C = [].
?-
第一个结果是正确的答案。问题是为什么谓词 pp 允许 C 是所有其他值?