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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parameter accumulator

Status
Not open for further replies.

trint

Technical User
Aug 23, 2002
4
0
0
GB
I'm making a Londen underground tracking program. I have already created the code that finds and places the stations in a list. Its just that my program builds up the same list of stations twice over, once as a route and once as the avoid list, how can i use the accumulator parameter to tackle this.
 
Would you talk in more detail?
I want you to give an example.
 
well I have the following facts and clauses below, its just that I need to append the lists somehow and I'm a little unsure, an example would be

?- canChange(warrenStreet, charingCross, R).

should return a list of all stations and lines used but not use the same station twice, i.e. loop.

so R= [[warrenStreet, goodgeStreet, northern],[goodgeStreet, tottenhamCourtRoad, northern], etc...

connect(warrenStreet, goodgeStreet, northern).
connect(goodgeStreet, tottenhamCourtRoad, northern).
connect(tottenhamCourtRoad, leicesterSquare, northern).
connect(leicesterSquare, charingCross, northern).
connect(charingCross, embankment, northern).
connect(euston, warrenStreet, victoria).
connect(warrenStreet, oxfordCircus, victoria).
connect(oxfordCircus, greenPark, victoria).
connect(greenPark, victoria, victoria).
connect(victoria, pimlico, victoria).
connect(bank, waterloo, waterlooCity).

connection(X,Y,Line) :-
connect(X,Y,Line)
;
connect(Y,X,Line).

canGo(X,Y,[X,Y,Line],A) :-
connection(X,Y,Line),
not(member(Y,A))
;
connection(X,Z,Line),
not(member(Z,A)),
canGo(Z,Y,[Z,Y,Line],[Z|A]).

canChange(X,Y,[[X,Y,Line]],UsedLines):-
canGo(X,Y,[X,Y,Line],[X]),
not(member(Line,UsedLines)).
canChange(X,Y,[[X,Z,Line]|Lines],UsedLines):-
canGo(X,Z,[X,Z,Line],[X]),
not(member(Line,UsedLines)),
canChange(Z,Y,Lines,R).

canChange(X,Y,R):-
canChange(X,Y,R,[]).

member(H,[H|T]).
member(X,[H|T]):-
member(X,T).
 
How is it now?

connect(warrenStreet, goodgeStreet, northern).
connect(goodgeStreet, tottenhamCourtRoad, northern).
connect(tottenhamCourtRoad, leicesterSquare, northern).
connect(leicesterSquare, charingCross, northern).
connect(charingCross, embankment, northern).
connect(euston, warrenStreet, victoria).
connect(warrenStreet, oxfordCircus, victoria).
connect(oxfordCircus, greenPark, victoria).
connect(greenPark, victoria, victoria).
connect(victoria, pimlico, victoria).
connect(bank, waterloo, waterlooCity).

connection(X, Y, Line) :-
connect(X, Y, Line)
;
connect(Y, X, Line).

go(X, X, []) :- !.
go(X, Y, R) :- go2(Y, X, [], R).

go2(X, Y, Acc, [[Y, X, Line] | Acc]) :-
connection(X, Y, Line).

go2(X, Y, Acc, R) :-
connection(X, Z, Line),
Z \= Y,
not(passed(Z, Acc)),
go2(Z, Y, [[Z, X, Line] | Acc], R).

passed(X, [[X, _, _]|_]) :- !.
passed(X, [[_, X, _]|_]) :- !.
passed(X, [_|L]) :- passed(X, L).


*** example ***


%%% in northern line
?- go(warrenStreet, charingCross, R).
R = [[warrenStreet,goodgeStreet,northern],
[goodgeStreet,tottenhamCourtRoad,northern],
[tottenhamCourtRoad,leicesterSquare,norhern],
[leicesterSquare,charingCross,northern]] ;
no

%%% not necessary to go
?- go(bank, bank, R).
R = '[]' ;
no

%%% no way
?- go(greenPark, waterlooCity, R).
no

%%% in northern and victoria line
?- go(goodgeStreet, greenPark, R).
R = [[goodgeStreet,warrenStreet,northern],
[warrenStreet,oxfordCircus,victoria],
[oxfordCircus,greenPark,victoria]] ;
no

%%% make a loop
?- assert(connection(goodgeStreet, greenPark, newline)).
yes

%%% two answers
?- go(warrenStreet, goodgeStreet, R).
R = [[warrenStreet,goodgeStreet,northern]] ;
R = [[warrenStreet,oxfordCircus,victoria],
[oxfordCircus,greenPark,victoria],
[greenPark,goodgeStreet,newline]] ;
no


 
cheers pimi,

you're a star!! ;),

trint
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top