Thread: Prolog

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    36

    Prolog

    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.

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    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.

    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.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    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....

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    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.

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    p() :- if, !, then.
    p() :- else.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    %% 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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prolog
    By anirban in forum Tech Board
    Replies: 4
    Last Post: 02-25-2008, 09:40 PM
  2. Prolog Compilers?
    By indigo0086 in forum Tech Board
    Replies: 0
    Last Post: 08-30-2007, 07:04 AM
  3. Should I learn C++ or Prolog?
    By coreyt1111 in forum C++ Programming
    Replies: 10
    Last Post: 11-14-2006, 09:35 AM
  4. more prolog
    By bob20 in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 12-10-2002, 10:33 PM
  5. Prolog
    By bob20 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-03-2002, 07:02 PM