Hello,
I am trying to find a solution for the following predicate:
Predicate: line_predicate(I, List_Integers, List_Next_To)
Where:
- I is an Integer (>=1),
- List_Integers is a List of Integers that does not include I, and are sorted
- List_Next_To is a List that is obtained from List_Integers, that are the values next to I
examples:
- line_predicate(2, [4,5,10,12], [4])
- line_predicate(14, [4,5,10,12], [12])
- line_predicate(7, [4,5,10,12], [5,10])
I have already written some code, but it does not work:
line_predicate(_, [], []) :- !.
line_predicate(_, [H | []], [H | []]) :- !.
line_predicate(I, [P | R], [H | T]) :-
P > I, !,
H = P,
line_predicate(I, R, T).
line_predicate(I, [P,S | R], [H,J | T]) :-
between(P, S, I), !,
H = P,
J = S,
line_predicate(I, R, T).
line_predicate(_, [_ | R], T) :-
line_predicate(_, R, T).
I would appreciate all your help on finding a solution to this problem, without using any external library.
Any explanation why the above code is not working would be very important in order to be able to improve my skills.
Thanks,
Jay