在 SWI-Prolog 中,它們是排序串列的謂詞sort/2 。他們是通過某個索引對具有固定長度的串列串列進行排序的好方法嗎?我的意思是在示例中,如果我有以下元素串列
[[1,2], [3,1], [2, 5]]
它們是 SWI-Prolog 中的一個函式,用于按第一個或第二個索引對其進行排序。通過第一個索引,結果將是:
[[1,2], [2,5], [3, 1]]
按第二個索引:
[[3,1], [1,2], [2, 5]]
uj5u.com熱心網友回復:
如果可以洗掉重復項,可以使用https://www.swi-prolog.org/pldoc/doc_for?object=predsort/3 :
test(Nth1, S) :-
L = [[1,2], [3,1], [2, 5], [1,2]],
predsort(nth1_compare(Nth1), L, S).
nth1_compare(Nth1, Comp, L1, L2) :-
nth1(Nth1, L1, V1),
nth1(Nth1, L2, V2),
compare(Comp, V1, V2).
結果:
?- test(1, S).
S = [[1,2],[2,5],[3,1]].
?- test(2, S).
S = [[3,1],[1,2],[2,5]].
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/514786.html
