简体   繁体   English

Prolog的新功能-列表挑战

[英]New to Prolog - challenge with lists

First I apologize for any mistakes I may make since English is not my first language. 首先,对于英语可能不是我的母语,我可能会犯的任何错误我深表歉意。

So I decided to learn Prolog all by myself and I came across this "challenge." 因此,我决定自己一个人学习Prolog,并且遇到了这种“挑战”。

I have this database about TV Shows. 我有这个关于电视节目的数据库。 It has the following predicates: 它具有以下谓词:

person(Person_id,Name). 人(PERSON_ID,名称)。

show(Show_id,Name). 显示(Show_id,名称)。

participates(Person_id,Show_id,Activity). 参与(PERSON_ID,Show_id,活动)。

What I have to find out is the relation between 2 people... I have to write an objective like this: network(Person1,Person2), that given the names of 2 people (Person1,Person2) gives back the name of 2 other people, Person3 and Person4 - Person1 has worked with Person3 on any show, Person2 has worked with Person4, and Person3 and Person4 have both worked together. 我要找出的是2个人之间的关系...我必须这样写一个目标:network(Person1,Person2),给定2个人的名字(Person1,Person2)还会返回另外2个人的名字people,Person3和Person4-Person1在任何表演中都与Person3合作,Person2与Person4合作,而Person3和Person4都一起合作。

I made a list of all the shows Person1 has worked in and then made a list of all the shows Person2 has worked in. 我列出了Person1参加过的所有演出的清单,然后列出了Person2参加过的所有演出的清单。

My problem is how to continue from here. 我的问题是如何从这里继续。 I thought about making a list of all the people who worked in the shows Person1 has worked in, and another list with all the people who worked in the shows Person2 has worked in, and then try to find out if, of all the people Person1 has worked with, if someone has worked with someone on the list of people Person2 has worked with. 我考虑过列出一个清单,其中列出了在Person1中参加过演出的所有人员,并在另一个列表中列出了所有在Person2中进行过演出的人员,然后尝试找出是否所有Person1中的人员与某人一起工作,如果某人已经与Person2一起工作的人列表中的某人一起工作。

Can anyone give me some lights on how to work this out? 谁能给我一些解决方法的建议? Thanks!! 谢谢!!

in prolog there is no such thing as "returning value" 在序言中没有“回报价值”之类的东西

therefore, you actually have to write a predicate like 因此,您实际上必须写一个谓词,例如

network(Person1,Person2,Person3,Person4). 网络(PERSON1,PERSON2,Person3可能,Person4)。

the first step is writing the predicate worked_with(Person1,Person2) 第一步是编写谓词work_with(Person1,Person2)

something like: 就像是:

worked_with(Person1,Person2):-
    participates(Person1,X,_),
    participates(Person2,X,_),
    Person1 \= Person2.

after that the network predicate would be something like 之后,网络谓词将类似于

network(P1,P2,P3,P4):-
    worked_with(P1,P3),
    worked_with(P2,P4),
    worked_with(P3,P4).

however, this predicate uses as input the ID's instead of the names; 但是,该谓词使用ID代替名称作为输入; you simply need to write a wrapper that will do the decoding. 您只需要编写一个包装即可进行解码。 i think that you could try to write it yourself as an exercise:b 我认为您可以尝试自己将其写为练习:b

by the way, if you are just starting to learn prolog, i dont really think that there is a reason to try something complex like that; 顺便说一句,如果您才刚刚开始学习序言,我真的不认为有理由尝试类似的事情。 try something simpler first to grasp the way prolog behaves 首先尝试更简单的方法来掌握序言的行为方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM