I am trying to write a procedure order(List,Result) that has a List as input and returns a list Result of ordered pairs such that:
- the first element of the ordered pair is the position of the pair in the list, and
- the second element of the ordered pair is the element from List n the corresponding position.
Example:
if List = [a,b,c,d], the procedure order(List,Result) outputs the list:
Result = [(1,a), (2,b),(3,c),(4,d)].
I am struggling with the counter for the position of the pair in the list. I have made attempts such as:
increment(Accum,Total):-
Total is Accum + 1.
order([],[]).
order([Head|Tail],Result):-
order(Tail, NewTail),
NewCount is Count + 1,
increment(NewCount,Count),
Result = [(Count,Head)|NewTail].
Please help anyone?
The two clauses:
NewCount is Count + 1andincrement(NewCount,Count)basically have the same meaning. You didn’t make clear thatCountis an input variable and it has a base case of1, so Prolog didn’t know where to start unifying values for it. For example, you should useCountas an input argument as follows (it doesn’t change much if compared with your version):