制作比较列表的谓词?

Making a predicate that compares lists?

我对 Prolog 比较陌生,所以请多多包涵。假设我有事实,其中每个人都包含一个人以及他们的爱好是什么(在列表中):

hobbies(mark, [running, video_games, soccer])
hobbies(jasmine, [writing, swimming, music])
hobbies(john, [rugby, rowing, pets, politics])
hobbies(lisa, [gymnastics, dancing, television])

我现在想做的是定义一个谓词(我们称它为 same),其中如果 P1P2 有爱好 same(P1, P2, H) 为真=16=]共同点。我知道我需要使用成员谓词,但到目前为止我只学会了如何在更简单的情况下使用它。会不会是这样的:

same(P1, P2, H) :- member(H, P1), member(H, P2)

不过我当然知道这是不可能的。我如何访问这些事实中的列表?

您需要一个列表才能使用 member 谓词。所以你必须得到 P1P2 的爱好列表。怎么做?使用 hobbies 事实

hobbies(mark, [running, video_games, soccer]).
hobbies(jasmine, [writing, swimming, music]).
hobbies(john, [rugby, rowing, pets, politics]).
hobbies(lisa, [gymnastics, dancing, television, video_games]).

same(P1, P2, H) :-
    % Hobbies1 is a list of hobbies of P1
    hobbies(P1, Hobbies1),
    member(H, Hobbies1),
    % Hobbies2 is a list of hobbies of P2
    hobbies(P2, Hobbies2),
    member(H, Hobbies2),
    % P1 and P2 are not the same. Also eliminates symmetrical redundancy
    P1 @< P2.

查询

?- same(P1, P2, H).
P1 = mark,
P2 = lisa,
H = video_games ;
false.