一些替代方案:
sort1(L , S) :-
predsort([R,(A,B),(C,D)]>>compare(R,(B,A),(D,C)), L, S).
sort2(L, S) :-
findall((A,B), order_by([asc(B),asc(A)], member((A,B),L)), S).
sort3(L, S) :-
maplist([(X,Y),(Y,X)]>>true, L, M),
msort(M, M1),
maplist([(X,Y),(Y,X)]>>true, M1, S).
一些例子:
?- A=[(a,1), (b,3), (d,2), (a,0), (c,2), (b,3)], time(sort1(A,S)).
% 53 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
A = [(a, 1), (b, 3), (d, 2), (a, 0), (c, 2), (b, 3)],
S = [(a, 0), (a, 1), (c, 2), (d, 2), (b, 3)].
?- A=[(a,1), (b,3), (d,2), (a,0), (c,2), (b,3)], time(sort2(A,S)).
% 100 inferences, 0.000 CPU in 0.001 seconds (0% CPU, Infinite Lips)
A = [(a, 1), (b, 3), (d, 2), (a, 0), (c, 2), (b, 3)],
S = [(a, 0), (a, 1), (c, 2), (d, 2), (b, 3), (b, 3)].
?- A=[(a,1), (b,3), (d,2), (a,0), (c,2), (b,3)], time(sort3(A,S)).
% 29 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
A = [(a, 1), (b, 3), (d, 2), (a, 0), (c, 2), (b, 3)],
S = [(a, 0), (a, 1), (c, 2), (d, 2), (b, 3), (b, 3)].
庞大列表的执行时间:
?- N is 10**6,
length(L, N),
maplist([(X,Y)]>>(random(1,N,X), random(1,N,Y)), L),
time(sort1(L,A)),
time(sort2(L,B)),
time(sort3(L,C)),
fail.
% 76,846,271 inferences, 13.063 CPU in 13.171 seconds (99% CPU, 5882968 Lips)
% 4,000,076 inferences, 3.063 CPU in 3.140 seconds (98% CPU, 1306147 Lips)
% 4,000,005 inferences, 2.375 CPU in 2.375 seconds (100% CPU, 1684213 Lips)
false.
似乎最好将maplist/3与msort/2结合使用。