daydream
daydream

Reputation: 65

Prolog - adding to a list recursively

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.

Upvotes: 2

Views: 1134

Answers (1)

m09
m09

Reputation: 7493

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.

Upvotes: 1

Related Questions