The typical use of this concept will first sort the input list so that elements that can be combined are consecutive in the list.
:-mode combine(+,-). combine([],[]). combine([A,B|R],Res):- can_combine(A,B,C), !, combine([C|R],Res). combine([A|A1],[A|Res]):- combine(A1,Res).
The cut in the second clause ensures that elements that can be combined are always combined, and that we do not leave a choice point in the execution.
The most simple use of the concept is the removal of duplicate entries in a sorted list.
:-mode combine_traffic(+,-). combine_traffic([],[]). combine_traffic([A,B|R],L):- try_to_combine(A,B,C), !, combine_traffic([C|R],L). combine_traffic([A|R],[A|S]):- combine_traffic(R,S). try_to_combine(interface_traffic_sample(Time,Router,Interface, X1,X2,X3,X4,X5, X6,X7,X8,X9,X10), interface_traffic_sample(Time,Router,Interface, Y1,Y2,Y3,Y4,Y5, Y6,Y7,Y8,Y9,Y10), interface_traffic_sample(Time,Router,Interface, Z1,Z2,Z3,Z4,Z5, Z6,Z7,Z8,Z9,Z10)):- Z1 is X1+Y1, Z2 is X2+Y2, Z3 is X3+Y3, Z4 is X4+Y4, Z5 is X5+Y5, Z6 is X6+Y6, Z7 is X7+Y7, Z8 is X8+Y8, Z9 is X9+Y9, Z10 is X10+Y10.Here we combine traffic samples for the same interface and time point by adding the sample values X1 ... X10 and Y1 ... Y10. The predicate try_to_combine will only succeed if the two input arguments have the same time stamp, router and interface, but it will fail if the arguments differ on these fields.
Also note that we do not use named structures in this example. This is justified as any extension of the structure would probably entail a change of the program anyway.