Created
June 3, 2011 21:31
-
-
Save jvia/1007209 to your computer and use it in GitHub Desktop.
Simple route finding.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% At the moment there are no loops in any route | |
path(a, b). | |
path(a, c). | |
path(c ,d). | |
path(d, e). | |
path(e, f). | |
%%% Calling rule | |
%% This is the interface the user interacts with. This just saves the | |
%% user from explicitly typing in the empty list. That empty list is | |
%% the route accumulator and it starts out empty. Every intermediate | |
%% node will be added to it. | |
route(Start, End, Route):- | |
route(Start, End, [], Route). | |
%%% 1 - terminating | |
%% This clause is true when there is a direct route between two | |
%% nodes. When this occurs, the route accumulator, which is just a | |
%% list of every intermeidate node we've passed through, will unify | |
%% with Route. Up until this point Route has been a free | |
%% variable. Once this unification has taken place, the call stack | |
%% will unwind and Route will come back to the top of the query and | |
%% become the answer. | |
route(Start, End, Route, Route):- | |
path(Start, End). | |
%%% 2 - recursive | |
%% This clause is true when there is an indirect route between the two | |
%% nodes. In this case, Via will be bound to each possible node coming | |
%% from Start. It is then added to the accumulator list and the search | |
%% will continue to see if there is a route between Via and the End | |
%% nodes. | |
%% | |
%% If this search if successful, the route will be passed all the way | |
%% back up to the calling procedure route/3 and back to the | |
%% interpreter. If it is not successful, the search will simply fail. | |
route(Start, End, Route_Accumulator, Route):- | |
path(Start, Via), | |
route(Via, End, [Via|Route_Accumulator], Route). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment