2

I'm trying to write a rule that goes through a database of facts and adds up the numbers from each fact together and saves it into a list: to be specific, this is the question:

Write a rule based on the answer to Q5 that returns/displays how long a journey will take between two stations.

Answer to question 5:

time(Station1,Station2) :- overground(Station1,Station2,Time),
    overground(Station1,_,Time),
    overground(_,Station1,Time).
time(Station2,Station1) :- overground(Station2,Station1,Time),
    overground(Station2,_,Time),
    overground(_,Station2,Time).
time(Station1,Station2) :- overground(Station1,Station3,Time),
    time(Station3,Station2);
    overground(Station1,Station3,Time),
    time(Station2,Station3).
time(Station1,Station2) :- overground(Station1,_,Time),
    overground(_,Station2,Time).
time(Station1,Station2) :- overground(Station2,_,Time),
    overground(_,Station1,Time).

I have tried adding a list and appending it with 'Time', but no luck.

*yes its a semi-colon.

There's another file that goes with it which has all of the overground stations and times in the format "overground(X,Y,Z)." i.e. overground(kenton,southkenton,2). which shows station1, station2, and the time it takes to get from 1 to 2.

I'm trying to go through the entire database, finding X and Y (which question 5 does) then adding up all of the Z's in-between the two stations and putting them into a list.

daydream
  • 65
  • 8
  • I reformatted the code to try to make it more legible. My apologies if this isn't actually legal Prolog any more, but I can't stand reading code in proportional font and I also detest the horizontal scrollbar on code snippets. So. While re-formatting the code I noticed a `;` near the middle -- is that supposed to be a semicolon or a comma? – sarnold Dec 18 '11 at 22:25

1 Answers1

1

Well, to be honest your question 5 answer needs some refactoring. I'll start by giving you some links to questions about the same subject.

Once you'll have figured out how to handle such a recursion, the answer to your current question should be easy.

Community
  • 1
  • 1
m09
  • 7,490
  • 3
  • 31
  • 58