PDA

View Full Version : Prolog



YankeePride13
12-12-2005, 01:18 PM
Any prolog fans? I was attempting to go on the codecomments.com prolog forum, but they're down for the moment so I was curious to see if anyone here knew any prolog...Basically I'm trying to write a program that emulates the game 6 Degrees of Kevin Bacon...

how you play is you pick an actor and you try to see how many degrees away from Kevin Bacon he is. I.E. Kevin Bacon was in Apollo 13 and so was Tom Hanks, so Tom Hanks is 1 degree away from Kevin Bacon. Tim Allen was in Toy Story with Tom Hanks, so Tim allen is 2 degrees away from Kevin Bacon. However, I'm very new to prolog, and I'm stuck...here's the code I have worked out thus far...

%% I plan on adding a lot more examples then this
in('Kevin Bacon', 'Appollo 13').
in('Tom Hanks', 'Appollo 13').
in('Tom Hanks', 'Toy Story').
in('Tim Allen', 'Toy Story').
in('Kevin Smith', 'Mallrats').

%%related/2 - sees if the 2 actors are related
related(X, Y) :-
in(X, Z),
in(Y, Z),
not(in(X, X)),
not(in(Y, Y)).

%%degrees/3 - asks if actor x is Z degrees away from actor Y
degrees(X, Y, Z) :-
related(X, Y),
Z is 1.
degrees(X, Y, Z) :-
related(X, Actor),
related(Actor, Y),
Z is 2.

basically my problem is that Kevin Bacon under this set up is both 1 degree and 2 degrees away. Prolog sees that Kevin Bacon is related to himself, also that he's related to Tom Hanks, both editions. Any ideas how to cure this ailment? Thanks buddies in advance.

SMurf
12-13-2005, 03:37 PM
Well I haven't done Prolog in well over a year now, and that was writing one bit of coursework for university, so... I'm not gonna be able to provide a solution as I can't think what the syntax would be. :o

But anyway, you can't have two definitions of degrees(X, Y, Z). There's nothing different in either sets of arguments, so both'll run regardless. What you need to do is work on one definition of degrees which does an "if...then...else" type thing.

Only I can't remember how you'd do that. :rolleyes:

YankeePride13
12-13-2005, 04:06 PM
lol thanks, i have the 2 in there so that to say the difference in degrees...like when the first one fails it goes to the second one...

like its ideally supposed to be like ('Tom Hanks' 'Kevin Bacon' Z) and tell you 2, but it goes both 1 and 2....

SMurf
12-13-2005, 04:37 PM
Yeah, but what if the first one succeeds? Because the two definitions are no different it'll try both anyway.

What you need is something like

degrees(X, Y, Z) :-
(if) related(X, Y), Z is 1,
(else if) related(X, Actor), related(Actor, Y), Z is 2.

There's a way to do this, but I dunno how off the top of my head.

Perspective
12-13-2005, 05:41 PM
p() :- if, !, then.
p() :- else.

YankeePride13
12-15-2005, 03:50 PM
%% I plan on adding a lot more examples then this
in('Kevin Bacon', 'Appollo 13').
in('Tom Hanks', 'Appollo 13').
in('Tom Hanks', 'Toy Story').
in('Tim Allen', 'Toy Story').
in('Kevin Smith', 'Mallrats').

member(Thing, [Thing|_]).
member(Thing, [_|Rest] :-
member(Thing, Rest).

related(X,Y) :-
in(X,Z),
in(Y,Z).

degrees(Actor1, Actor1, _, Dist).
degrees(Actor1, Actor2, People, Dist):-
related(Actor1, Actor3),
not(member(Actor3, People)),
degrees(Actor3, Actor2, [Actor3 | People], Dist2),
Dist = Dist2 + 1.

I edited the code a bit, here's what i am up to now.
My problem now is in the recurssion I believe, when i call degrees it just says no, when I know that the answer is yes. if i could only pursuade eclipse to say yes... =/

any ideas guys?