-module(second).
-export ([filter_map/5]).
-export ([collect_sequence/3]).
-export ([arithmetic/6]).
-export ([arithmetic_list/4]).
%% collect_sequence(Start_V,End_V,Result)
%% 收集 Start_V~End_V 到结果Result中 区间[Start_V,End_V]
%% Start_V - 左边界
%% End_V - 右边界
collect_sequence(Start_V,End_V,Result) -> if
Start_V>End_V ->
Result;
true -> collect_sequence(Start_V,End_V-1,[End_V|Result])
end.
% filter_map(X,Start_V,End_V,MapFun,FilterFun)
filter_map(X,Start_V,End_V,MapFun,FilterFun) ->
Lst = collect_sequence(Start_V,End_V,[]),
Lst1 = lists:filter(fun(Value) -> FilterFun(X,Value) end,Lst),
lists:map(fun(Value) -> MapFun(X,Value) end,Lst1).
%arithmetic(A1,A2,B1,B2,MapFun,FilterFun)
%根据算法F来计算两组数,比如计算[1~9] 和 [1~9] 的乘法口诀表
%A1_Start 第一组开始值
%A2_End 第一组终止值
%B1_Start 第二组开始值
%B2_Start 第二组终止值
%F为算法 比如 fun(X,Y) -> X*Y end.
%% 例子:arithmetic(1,9,1,9,fun(X,Y) -> X*Y end,fun(X,Y) ->X>=Y end).
arithmetic(A1_Start,A2_End,B1_Start,B2_Start,MapFun,FilterFun) ->
if
A1_Start>A2_End ->
[];
true ->
io:format("~w~n",[filter_map(A1_Start,B1_Start,B2_Start,MapFun,FilterFun)]),
arithmetic(A1_Start+1,A2_End,B1_Start,B2_Start,MapFun,FilterFun)
end.
filter_map_list(X,Lst2,MapFun,FilterFun) ->
TempList = lists:filter(fun(Value) -> FilterFun(X,Value) end,Lst2),
lists:map(fun(Value) -> MapFun(X,Value) end,TempList).
%arithmetic_list - 通过 FilterFun,并且通过MapFun来将两个集合(向量)进行运算,
%得到一个矩阵
%参数:Lst1 - 第一个集合,Lst2 - 第二个集合,MapFun - 两个集合预算的函数,FilterFun 过滤函数
%例子:
%arithmetic_list([1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9],fun(X,Y) ->X*Y end,fun(X,Y)->X>=Y end).
arithmetic_list([],Lst2,MapFun,FilterFun) -> [];
arithmetic_list(Lst1,Lst2,MapFun,FilterFun) ->
[H|T] = Lst1,
TempLst = filter_map_list(H,Lst2,MapFun,FilterFun),
io:format("~w~n",[TempLst]),
arithmetic_list(T,Lst2,MapFun,FilterFun).erlang 通过尾递归实现双层循环,并抽象出两向量的叉积的一般运算式
原文地址:http://blog.csdn.net/justin_bkdrong/article/details/45917709