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.
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.