Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please Help, trouble with bagof

Status
Not open for further replies.

TerraNova

Technical User
Nov 21, 2002
1
GB
Hi, here is my prolog source. It's supposed to recursively step through a map to find a route to a given destination. I'm trying to implement a breadth first search, and hence using bagof. The logic seems good, but I can't find the error. All of it has been tested apart from the part where bagof appears. Tried using trace, don't think bagof is returning what I'm expecting. Can you help?

connected(middlesbrough, stockton).
connected(middlesbrough, darlington).
connected(stockton, sunderland).
connected(stockton, newcastle).
connected(darlington, thirsk).
connected(newcastle, york).
connected(thirsk, york).
connected(york, leeds).
connected(leeds, huddersfield).
connected(leeds, dewsbury).
connected(huddersfield, manchester).
connected(dewsbury, manchester).

run:-
write('Enter start point:'),
read(Start),nl,
write('Enter destination:'),
read(End),nl,
findroute(Start,End,[Start],[Start]).

myappend(X, [], [X]).

myappend(X, [Head | Tail], [Head | NewList] ):-
myappend(X, Tail, NewList).

writeList([]).

writeList([Head | Tail]):-
write(Head),
nl,
writeList(Tail).

myconcat([], L2, L2).

myconcat([X | TailList1], L2, [X | List3]):-
myconcat(TailList1, L2, List3).

findroute(End,End,L,L1):-
write("Tovist"),
nl,
writeList(L),
nl,write("Visited"),
writeList(L1).


findroute(Start,End,Tovisit,Visited):-
bagof(Start,connected(Start,X),L2),
myconcat(Tovisit,L2,List3),
List3 = [H|T],
myappend(H,Visited,Newvisited),
findroute(H,End,T,Newvisited).


Thanks in advance. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top