predsort / 3的可能行为

``````?- predsort(compare, List, Sorted).
``````

``````mcompare(Delta, A, B) :-
compare(Delta0, A, B),
(   Delta0 == (=)
->  Delta = (<)
;   Delta = Delta0
).

?- predsort(mcompare, List, Sorted).
``````

``````sort_argn(N, List, Sorted) :-
map_list_to_pairs(arg(N), List, Pairs),
keysort(Pairs, Sorted_pairs),
pairs_values(Sorted_pairs, Sorted).
``````

``````compare_argn(N, Delta, A, B) :-
arg(N, A, AN),
arg(N, B, BN),
compare(Delta, AN-A, BN-B).
``````

``````?- predsort(compare_argn(2), List, Sorted).
``````

``````?- predsort(compare_argn(2), [f(b,2), f(a,1), f(a,1), f(a,2)], Sorted).
Sorted = [f(a, 1), f(a, 2), f(b, 2)].

?- sort_argn(2, [f(b,2), f(a,1), f(a,1), f(a,2)], Sorted).
Sorted = [f(a, 1), f(a, 1), f(b, 2), f(a, 2)].
``````

``````compare_argn_stable(N, Delta, A, B) :-
arg(N, A, AN),
arg(N, B, BN),
compare(Delta0, AN, BN),
(   Delta0 == (=)
->  Delta = (<)
;   Delta = Delta0
).
``````

``````?- predsort(compare_argn_stable(N), List, Sorted).
``````

• “稳定性”取决于实施`predsort/3`（请参阅问题）
• `predsort/3`本身不是任何标准的一部分（据我可以告诉）
• 可能是，您的Prolog实施提供了`msort/2``keysort/2`效率比`predsort/3`

``````list_to_keyval_pairs(List, Pairs), % defined by the user as necessary
keysort(Pairs, Sorted_pairs),
pairs_values(Sorted_pairs, Sorted)
``````

0条评论