开发者

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

I have this database about TV Shows. It has the following predicates:

person(Person_id,Name).

show(Show_id,Name).

participates(Person_id,Show_id,Activity).

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.

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.

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 wor开发者_如何学JAVAked with someone on the list of people Person2 has worked with.

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

the first step is writing the predicate worked_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; 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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜