Prolog - 为什么 member/2 在这里不起作用?

Prolog - Why is member/2 not working here?

我不知道为什么这不起作用...这是代码。

cameToTheParty(date(15,9,2011), flor).
cameToTheParty(date(22,9,2011), marina).
cameToTheParty(date(15,9,2011), pablo).
cameToTheParty(date(22,9,2011), pablo).
cameToTheParty(date(15,9,2011), leo).
cameToTheParty(date(22,9,2011), flor).
cameToTheParty(date(15,9,2011), fer).
cameToTheParty(date(22,9,2011), mati).

cameToThePartyThatDay(Peoples, Date):-
    bagof(X,cameToTheParty(Date,X),Peoples).

当我尝试时

?- cameToThePartyThatDay(People,Day).

它说

 People = [flor, pablo, leo, fer],
 Day = date(15, 9, 2011) ; 
 People = [marina, pablo, flor, mati], 
 Day = date(22, 9, 2011).

但是,当我尝试使用可变日期字段或实际日期执行以下操作时,例如...

member(X,cameToThePartyThatDay(People,date(15,9,2011))).

它只是说

false.

问题是成员正试图从列表 cameToThePartyThatDay(People,date(15,9,2011)) 中查找一个元素,实际上它不是列表。

您想做的是:

cameToThePartyThatDay(People,date(15,9,2011)),
member(X,People).

...这样People就和当天来聚会的人列表统一了,然后member就可以从People列表中拉取元素了。

member(X,cameToThePartyThatDay(People,date(15,9,2011)))

是使用 member/2 的错误方法,因为

cameToThePartyThatDay(People,date(15,9,2011))

不是列表。

正确的方法可能是

cameToThePartyThatDay(People, date(15, 9, 2011)),
member(X, People)

对于 Prolog,以下表达式中的粗体部分:

member(X,<b>cameToThePartyThatDay(People,date(15,9,2011))</b>).

不是来电。事实上谓词是不是函数:它们不是return任何东西。根据 Prolog,粗体部分是 functor.

为了让它工作,你首先调用 cameToThePartyThatDay 然后你在 member/2 谓词中使用 People,如:

<b>cameToThePartyThatDay(People,date(15,9,2011)),</b>
member(X,<b>People</b>).