有没有比制定新规则来更改 maplist 的变量顺序更好的方法? [英] Is there a better method than making a new rule to change variable order for maplist?

查看:46
本文介绍了有没有比制定新规则来更改 maplist 的变量顺序更好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written a program that defines a relation between a 'full' data table a set of sets of column headers (features), an aggregate function and the aggregate table.

示例查询:

 ?- 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]].

在我的代码中,我有这两行只是重新排列参数,以便我可以将这些规则传递到地图列表中.

In my code I have these two lines which just reorder arguments so that I can pass these rules into a maplist.

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

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

有没有更好的方法来做到这一点?在这种情况下如何使用 maplist 以便不必定义这些规则?

Is there a better way to do this? How can I use maplist in this case so as not to have to define these rules?

推荐答案

在 SWI-Prolog 中有 library(yall)和库(lambda).

in SWI-Prolog there are library(yall) and library(lambda).

例如使用 library(yall)

Using library(yall), for instance

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 需要捕获"Features.图书馆(lambda)在

Libraries have similar functionalities. but note that yall requires a 'capture' of Features. library(lambda) is available after

?- pack_install(lambda).

同时库(yall)是自动加载的

while library(yall) is autoloaded

这篇关于有没有比制定新规则来更改 maplist 的变量顺序更好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆