Prolog 有没有比制定新规则更好的方法来更改maplist的变量顺序?

Prolog 有没有比制定新规则更好的方法来更改maplist的变量顺序?,prolog,maplist,Prolog,Maplist,我编写了一个程序,定义了“完整”数据表、一组列标题功能、聚合函数和聚合表之间的关系 一个示例查询: ?- data(D), fulltable_aggfunction_sets_aggtable(D,mean,[[a,b,c],[d,e,f]],AggTable), print_data(D),print_data(AggTable). [a,b,c,d,e,f,g,class] [1,1,1,1,1,1,1,0] [2,3,4,5,4,2,1,0] [3,1,3,4,6,7,8,1

我编写了一个程序,定义了“完整”数据表、一组列标题功能、聚合函数和聚合表之间的关系

一个示例查询:

 ?- data(D), fulltable_aggfunction_sets_aggtable(D,mean,[[a,b,c],[d,e,f]],AggTable), 
print_data(D),print_data(AggTable).

[a,b,c,d,e,f,g,class] 
[1,1,1,1,1,1,1,0] 
[2,3,4,5,4,2,1,0] 
[3,1,3,4,6,7,8,1] 
[1,2,3,6,1,1,2,1] 

[feature(1,mean,[a,b,c]),feature(2,mean,[d,e,f])] 
[1,1] 
[3,3.6666666666666665] 
[2.3333333333333335,5.666666666666667] 
[2,2.6666666666666665] 

D = [[a, b, c, d, e, f, g, class], [1, 1, 1, 1, 1, 1, 1|...], [2, 3, 4, 5, 4, 2|...], [3, 1, 3, 4, 6|...], [1, 2, 3, 6|...]],
AggTable = [[feature(1, mean, [a, b, c]), feature(2, mean, [d, e, f])], [1, 1], [3, 3.6666666666666665], [2.3333333333333335, 5.666666666666667], [2, 2.6666666666666665]] 
以下是我的代码:

fulltable_aggfunction_sets_aggtable(Full,Func,Sets,Aggtable):-
 Full =[Features|Data],
 flist_sets_indexs(Features,Sets,Indexs),
 maplist(indexs_flist_sets(Indexs),Data,Datasplits),
 maplist(aggfun_listoflists_values(Func),Datasplits,AggData),
 list_indexes(Sets,SetIndex),
 maplist(name_set_id_feature(Func),Sets,SetIndex,FeatureNames),
 append([FeatureNames],AggData,Aggtable).

name_set_id_feature(Func,Set,Id,feature(Id,Func,Set)).

list_indexes(List,Indexes):-
 findall(I,nth1(I,List,_),Indexes).

aggfunc_list_value(sum,List,Value):-
 sumlist(List,Value).

aggfunc_list_value(mean,List,Value):-
 sumlist(List,Sum),
 length(List,L),
 Value is Sum/L.

aggfun_listoflists_values(Fun,ListsofLists,Values):-
 maplist(aggfunc_list_value(Fun),ListsofLists,Values).

my_nth0(List,Elem,I):- nth0(I,List,Elem).

indexs_flist_sets(I,F,S):-flist_sets_indexs(F,S,I).

flist_sets_indexs(Features,Sets,Indexs):-
 maplist(flist_set_indexes(Features),Sets,Indexs).

flist_set_indexes(Features,Set,Indexs):-
 maplist(my_nth0(Features),Set,Indexs).

%aux
print_data(Data_set):-
 maplist(print_line,Data_set).

print_line(Data_line):-
 format("~w ~n",[Data_line]).

data(Data):-
 Data =[[a,b,c,d,e,f,g,class],
       [1,1,1,1,1,1,1,0],
       [2,3,4,5,4,2,1,0],
       [3,1,3,4,6,7,8,1],
       [1,2,3,6,1,1,2,1]].
在我的代码中有两行代码,它们只是对参数重新排序,这样我就可以将这些规则传递到映射列表中

my_nth0(List,Elem,I):- nth0(I,List,Elem).

indexs_flist_sets(I,F,S):-flist_sets_indexs(F,S,I).

有更好的方法吗?在这种情况下,我如何使用maplist,以便不必定义这些规则

在SWI-Prolog中,有库和库

例如,使用libraryyall

flist_set_indexes(Features,Set,Indexs):-
 maplist({Features}/[Elem,I]>>nth0(I,Features,Elem),Set,Indexs).

print_data(Data_set):-
 maplist([Data_line]>>format("~w ~n",[Data_line]),Data_set).
当使用第二个

:- use_module(library(lambda)).

flist_set_indexes(Features,Set,Indexs):-
 maplist(\Elem^I^nth0(I,Features,Elem),Set,Indexs).

%aux
print_data(Data_set):-
 maplist(\Data_line^format("~w ~n",[Data_line]),Data_set).
图书馆也有类似的功能。但请注意,yall需要“捕获”功能。librarylambda可在

?- pack_install(lambda).

虽然libraryyall是自动加载的

补充了Carlo的答案,但如果您没有使用SWI Prolog,您仍然可以使用Ulrich的lambda实现使用lambda表达式:

或者使用支持大多数Prolog系统的Logtalk:


在实践中,您是喜欢琐碎的辅助谓词还是lambda表达式,这实际上是一个品味和判断的问题。请看这两个答案。辅助谓词很好,因为选择好的名称可以作为文档。Lambda表达式很好,因为对于简单的事情,你真的觉得额外的努力是不值得的。我个人仍然无法决定哪一个是最佳实践。