Sample database:
any(a,b,2).
any(b,c,2).
any(c,d,3).
any(d,e,1).
any(e,f,3).
Key: (Station1,Station2,Time).
The question:
enter (station1,time) into prolog and have it return all of the stations within reach.
For example:
If you enter: (b,2).
The output should be: ‘a’ and ‘c’
This is because from station ‘b’ in ‘2’ minutes, you will be able to get to stations ‘a’ and ‘c’ as they are within reach of the time entered.
I have tired using lists and recursion but no luck, any help/suggestions?
reachable(Station1, Limit, Result) :-
reachable(Station1, Limit, 0, 0, Result).
reachable(Station1, Visited, TimeSpent, Limit, Result) :-
overground(Station1,Station2,Time),
Visited is Limit - Time,
Limit =< TimeSpent,
Result = [Station2];
overground(Station1, Waypoint, Time),
NewVisited is Visited - Limit,
NewTimeSpent is TimeSpent - NewVisited,
reachable(Waypoint,Station2, NewTimeSpent, NewVisited,Result).
<<< something I tried but doesnt seem to work X_X
So, let’s look at the problem :
any(MyStation, Y)orany(Y, MyStation), so you’ll need a disjunction when usingany/3.General look of the solution :
First step, calling the working predicate :
Where
...should be a call to reachable/5 (this one + the 2 accumulators).Second step, the working predicate :
Where
...should satisfy those guidelines :If you want more precise advice, I’d advise you to publish the work you’ve already done so that we can work from there !