我有这个嵌套列表:
Xs = [ [Joe, Pilot, 100], [Stan, Co-Pilot, 300], [Steve, Pilot, 150], ].
如何对此嵌套列表进行排序以执行以下操作:(按降序使用第三个元素)
Xs = [ [Stan, Co-Pilot, 300], [Steve, Pilot, 150], [Joe, Pilot, 100] ]. ?
答案 0 :(得分:2)
定义:
List
并使用" predsort / 3"喜欢在:
criteria(R,[_,_,N1],[_,_,N2]) :- compare(R,N2,N1).
如果存在重复的第三个元素,"标准"必须改变。例如:
?- predsort(criteria,[ [Joe, Pilot, 100], [Stan, Co-Pilot, 300], [Steve, Pilot, 150] ], Xs).
Xs = [[Stan, Co-Pilot, 300], [Steve, Pilot, 150], [Joe, Pilot, 100]].
答案 1 :(得分:2)
如果您将数据表示更改为结构(无论如何这是一个好主意),并且您的Prolog系统具有sort/4
(例如ECLiPSe,SWI),您只需执行以下操作:
?- Xs = [emp(joe, pilot, 100), emp(stan, copilot, 300), emp(steve, pilot, 150)],
sort(3, >=, Xs, Ys).
Xs = [emp(joe, pilot, 100), emp(stan, copilot, 300), emp(steve, pilot, 150)]
Ys = [emp(stan, copilot, 300), emp(steve, pilot, 150), emp(joe, pilot, 100)]
Yes (0.00s cpu)
更便携(ISO)的方法是将每个列表元素与排序键配对,然后使用keysort / 2,然后再次删除键:
?- Xs = [emp(joe, pilot, 100), emp(stan, copilot, 300), emp(steve, pilot, 150)],
add_keys(Xs, KXs),
keysort(KXs, KYs),
strip_keys(KYs, Ys).
Xs = [emp(joe, pilot, 100), emp(stan, copilot, 300), emp(steve, pilot, 150)]
KXs = [-100 - emp(joe, pilot, 100), -300 - emp(stan, copilot, 300), -150 - emp(steve, pilot, 150)]
KYs = [-300 - emp(stan, copilot, 300), -150 - emp(steve, pilot, 150), -100 - emp(joe, pilot, 100)]
Ys = [emp(stan, copilot, 300), emp(steve, pilot, 150), emp(joe, pilot, 100)]
Yes (0.00s cpu)
使用辅助谓词
add_keys([], []).
add_keys([Emp|Emps], [Key-Emp|SEmps]) :-
Emp = emp(_Name,_Job,Salary),
Key is -Salary,
add_keys(Emps, SEmps).
strip_keys([], []).
strip_keys([_-V|KVs], [V|Vs]) :-
strip_keys(KVs, Vs).