Created
December 22, 2018 08:44
-
-
Save DonaldKellett/8b15f6ea1ec234c758d862d5a93d1f3f to your computer and use it in GitHub Desktop.
Learn Prolog Now! - Chapter 3 - Practical Session - Programming Exercise 3 - Travel routes
This file contains hidden or 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
% Knowledge base of travel routes (provided) | |
% Assumption: these routes are unidirectional | |
byCar(auckland,hamilton). | |
byCar(hamilton,raglan). | |
byCar(valmont,saarbruecken). | |
byCar(valmont,metz). | |
byTrain(metz,frankfurt). | |
byTrain(saarbruecken,frankfurt). | |
byTrain(metz,paris). | |
byTrain(saarbruecken,paris). | |
byPlane(frankfurt,bangkok). | |
byPlane(frankfurt,singapore). | |
byPlane(paris,losAngeles). | |
byPlane(bangkok,auckland). | |
byPlane(singapore,auckland). | |
byPlane(losAngeles,auckland). | |
% travel/3 - In order to travel from X to Y, what route(s) Z should we take? | |
travel(X, Y, go(X, Y)) :- byCar(X, Y); byTrain(X, Y); byPlane(X, Y). | |
travel(X, Y, go(X, Z, go(Z, Y))) :- | |
byCar(X, Z), travel(Z, Y, go(Z, Y)); | |
byTrain(X, Z), travel(Z, Y, go(Z, Y)); | |
byPlane(X, Z), travel(Z, Y, go(Z, Y)). | |
travel(X, Y, go(X, Z, go(Z, W, P))) :- | |
byCar(X, Z), travel(Z, Y, go(Z, W, P)); | |
byTrain(X, Z), travel(Z, Y, go(Z, W, P)); | |
byPlane(X, Z), travel(Z, Y, go(Z, W, P)). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment