1
  domains
  list=symbol*

  predicates
     path(symbol,symbol)
     solve(symbol,symbol,list)
     insert(symbol,list,list)

  clauses
  path(a,b). 
  path(b,c).
  path(c,d).
  path(d,e). 
  path(a,d).
  path(c,e).

solve(X, Z, P):-
    path(X,Z),
insert(Z,Temp,P),
P=Temp.

solve(X,Z,P):-
path(X,Y),
insert(Y,Temp,P),
P=Temp,
    solve(Y,Z,P).

insert(X,[X|Tail],Tail).
insert(X,[Y|Tail],[Y|Tail1]):-
insert(X,Tail,Tail1).

Want to print the path which i have followed in going from one point to another.But getting errors.For examples, i want:

goal
solve(a,c,P).

Answer:P=[a,b,c]

false
  • 10,264
  • 13
  • 101
  • 209
Zohaib
  • 363
  • 2
  • 4
  • 15
  • Please describe what kind of error you're getting, and which non-standard Prolog dialect this is. – Fred Foo Nov 28 '11 at 14:23
  • iam using turbo prolog and therefore i don't know what's the error is, but the cursor is pointing to the tail in the first insert predicate. – Zohaib Nov 28 '11 at 14:30
  • 1
    The first thing to learn about Prolog is that unification is not assignment. Your construct `insert(Z,Temp,P),P=Temp` in conjunction with the first clause of `insert/3` and the goal `solve(a,c,P)` leads to a circular data structure (i.e., `P=[b|P]`). What error you get depends on the Prolog system. – twinterer Nov 28 '11 at 14:41

2 Answers2

1

I don't get what your solve predicate does. Here is my guess about how it should be :

solve(Start, End, [Start|PathMinusStart]) :-
    solve_(Start, End, PathMinusStart).
solve_(Start, End, [End]) :-
    path(Start, End).
solve_(Start, End, [Waypoint|Path]) :-
    path(Start, Waypoint),
    solve_(Waypoint, End, Path).

solve will give you the paths one after another using the ; to browse them (well, I guess at least since I don't know anything about the prolog implementation you're using).

m09
  • 7,490
  • 3
  • 31
  • 58
1
search_path(Node, End, Temp, Path) :- path(Node,End),reverse([End | [Node | Temp]], Path).
search_path(Node, End, Temp, Path) :- 
  path(Node, Next),
  not(member(Node, Temp)),
  search_path(Next, End, [Node | Temp], Path).

solve(Start, End, Path) :- search_path(Start, End, [], Path).
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70