2017-02-24 72 views
1

所以我必须制作这个小程序。 这个想法是,有几个派对,其中有人被邀请(我们知道这些人是谁,谁能告诉谁)。事情是,我需要这个能够告诉我,如果派对是暴力的,一个暴力派对就是这样,每个人都可以找到一个在那里的人,但我似乎无法想象出来,我认为我刚刚拥有了它,当我能够统一那些参加派对的人无法忍受的时候有人,所以如果每个参加聚会的人都感到受到另一位嘉宾的厌恶,那么该聚会就是violentParty/1。 有帮助吗?我不知道为什么这不会给我正确的输出,当我咨询violentParty(Date)时,它只是说错误。 代码:序言 - 无法创建相关元素的有用列表

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). 

cantStand(leo, flor). 
cantStand(pablo, fer). 
cantStand(fer, leo). 
cantStand(flor, fer). 

violentParty(Date):- 
    cameToThePartyThatDay(People,Date), 
    findall(X,member(X,People),Guests), 
    cantStandSomeone(Guests,ThoseWhoCantStandSomeone), 
    forall(member(Guest,Guests),member(Guest,ThoseWhoCantStandSomeone)). 

cantStandSomeone(Guests,List):- 
    member(Guest,Guests), 
    findall(FeelsAnnoyed,cantStand(FeelsAnnoyed,Guest),List). 



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

'的findall(X,会员(X,人),客人)'是一个什么都不做从句 - 它只是复制人。您可以使用'cameToThePartyThatDay(Guests,Date)',或使用已使用客人的人员。没有解决问题,但我第一眼就注意到了。 –

+0

你是对的,在我找到答案(并在下面发布)后,我可以明确地失去这一行,这只是一个新手的错误,将统一概念与我不想做的返回值概念相混淆。 – newbie

+0

令人讨厌的是,我通过发布我的答案中途召集了一次会议,但不值得浪费编写它的努力,以防有人想要一个稍微不同的方法:) –

回答

1

这不是转型A -> B(B的列表),我需要的东西,因为我将不得不再次找到A和B之间的关系,这只是没有任何意义,我需要A的列表,关系CantStand(A,B),然后每Guestmember(Guest,Guests)也应该是member of member(Guest,ThoseWhoCantStandSomeone),这是完美的完成所有。

comida(achura(chori, 200)). % ya sabemos que el chori no es achura 
comida(achura(chinchu, 150)). 
comida(ensalada(waldorf, [manzana, apio, nuez, mayo])). 
comida(ensalada(mixta, [lechuga, tomate, cebolla])). 
comida(morfi(vacio)). 
comida(morfi(mondiola)). 
comida(morfi(asado)). 

leGusta(mati, chori). 
leGusta(fer, mondiola). 
leGusta(pablo, asado). 
leGusta(mati, vacio). 
leGusta(fer, vacio). 
leGusta(mati, waldorf). 
leGusta(flor, mixta). 


leGusta(ezequiel,X):-leGusta(mati,X);leGusta(fer,X). 
leGusta(marina,X):-leGusta(flor,X);comida(morfi(mondiola)). 
leGusta(leo,X):-comida(X), X\=ensalada(waldorf,_). 

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). 

cantStand(leo, flor). 
cantStand(pablo, fer). 
cantStand(fer, leo). 
cantStand(flor, fer). 

violentParty(Date):- 
    cameToThePartyThatDay(People,Date), 
    cantStandSomeone(People,ThoseWhoCantStandSomeone), 
    forall(member(Guest,People),member(Guest,ThoseWhoCantStandSomeone)). 

cantStandSomeone(Guests,List):- 
    findall(FeelsAnnoyed,(member(Guest,Guests),cantStand(FeelsAnnoyed,Guest)),List). 



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

因此,暴力派对是每个客人至少有一个其他客人无法忍受的派对。 violentParty/1因此找到客人,并检查他们都无法忍受某人。

我们如何检查所有人都无法忍受某人?我们检查一下,对于每一位客人,都有一些他们无法忍受的客人。

violentParty(Date):- 
    cameToThePartyThatDay(Guests,Date), 
    allCantStandSomeone(Guests). 

allCantStandSomeone(Guests) :- 
    forall(member(Guest,Guests),cantStandSomeone(Guest,Guests)). 

cantStandSomeone(Guest,Guests) :- 
    cantStand(Guest,X), 
    member(X,Guests). 

,所有的将是非常有用的,如果OP早已不是已经解决了自己的问题......

+0

谢谢。这对我很有用,给了我另一种解决方法,无需使用另一个列表。 – newbie

+0

@newbie很高兴能帮到你! –