0

I would like to list out every possible path

country(england,france). 
country(france,bulgaria).
country(bulgaria,germany).
country(england,bulgaria).
country(germany,italy).
edit: additional to

country(germany,italy).
country(england,italy).
country(england,greece).
country(greece,france).

connectto(X, Y) :-
country(X, Y).

?-op(150,xfy,to).

X to Y:-get_waypoints(X,Y,Waypoints),write(Waypoints),fail.

get_waypoints(Start, End, [Waypoint|Result]) :-
   country(Start, End),
   !;country(Start, Waypoint),
   get_waypoints(Waypoint, End, Result).

otherwise from the original code the system will give out

   | ?- england to italy.
   []no

from the code you mention.

now the problem comes into

    | ?- england to italy.
    [_31242|_31243][france,bulgaria,germany,_31332|_31333]   
    [bulgaria,germany,_31422|_31423]
    [greece,france,bulgaria,germany,_31602|_31603]no

although it shows out all possible route.

Any solution will be appreciated.

1 Answers1

2

Ask if you need explanation :

country(bulgaria,germany).
country(england,bulgaria).
country(england,france). 
country(england,greece).
country(england,italy).
country(france,bulgaria).
country(greece,france).
country(germany,italy).

:- op(150, xfy, to).

X to Y :-
    findall(Waypoint, get_waypoints(X,Y,Waypoint), Waypoints),
    write(Waypoints).

get_waypoints(Start, End, []) :- 
    country(Start, End).
get_waypoints(Start, End, [Waypoint|Result]) :-
    country(Start, Waypoint),
    get_waypoints(Waypoint, End, Result).

Use is :

?- england to italy.

Here, I updated my code to match your expectations.

m09
  • 7,490
  • 3
  • 31
  • 58
  • yes, but i've mention this only giving 1 solution. I need mutiple different data. I've tried the fail and return but it doesnt work – Frank Jaeger Nov 28 '11 at 11:46
  • 1
    it does not give only 1 solution, you certainly forgot to add the first clause on top of the second one. Do you confirm that when you copy paste my code and test it it returns only one solution ? And which implementation of prolog are you using ? – m09 Nov 28 '11 at 11:48
  • im using lpa win prolog. and it really did give only 1 solution. Also, using fail giving me multiple paths but some aint correct. sorry i forgot to mention that it should be (adam,jim) and list out matt and frank. – Frank Jaeger Nov 28 '11 at 12:01
  • if you don't use a free variable as second parameter, you already bind the result, so calling your predicate will only tell you if jim is an ancestor or not. You need to call your predicate with a free variable as second argument. Like ?- ancestor(adam, X). – m09 Nov 28 '11 at 12:06
  • yes that what i was intent but for it similar to saleman's path, i require to set both parameters and looking for the middle. cause on this part, if i add mother(a,b) inside the ancestor, it require both different paths of answer to the result – Frank Jaeger Nov 28 '11 at 12:11
  • I edited my answer to precise things about calling the predicate. If you need another predicate then directly specify it in the question by editing it please. – m09 Nov 28 '11 at 12:13
  • sorry mog, seems my previous ancestor's having some logical issue. i've made this country path to show out the problem much easier. – Frank Jaeger Nov 28 '11 at 12:33
  • lol, sry for the careless mate, accidently left it over and forgot to change it. Done changing. – Frank Jaeger Nov 28 '11 at 13:04
  • when i added country(germany,italy). country(england,italy). country(england,greece). country(greece,france). ?-op(150,xfy,to). X to Y:-get_waypoints(X,Y,Waypoints),write(Waypoints),fail. funny things that it gave me is | ?- england to italy. [] no so i modified the get_waypoints(Start, End, [Waypoint|Result]) :- country(Start, End), !;country(Start, Waypoint), get_waypoints(Waypoint, End, Result). which lead me to | ?- england to italy. [_31242|_31243][france,bulgaria,germany,_31332|_31333][bulgaria,germany,_31422|_31423][greece,france,bulgaria,germany,_31602|_31603]no – Frank Jaeger Nov 28 '11 at 13:42