首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

初次想到record转换成 list步骤

2012-12-24 
初次想到record转换成 list方法??把:rd(test,{a1,b2}).成:[{a,1},{b,2}]方法1通用?1 -module(test).? 2

初次想到record转换成 list方法

?

?把:rd(test,{a=1,b=2}).

成:[{a,1},{b,2}]


方法1通用

?1 -module(test).

? 2 -export([test/0,testif/2,testcase/2]).

? 3 -record(langxw,{name=name,sex=sex,age=age}).

? 4 test()->

? 5 ? ? A=#langxw{},

? 6 ? ? B=A#langxw{name=lang,sex=male,age=26},

? 7 ? ? AL= tuple_to_list(A),

? 8 ? ? io:format("~p~n",[AL]),

? 9 ? ? BL= tuple_to_list(B),

?10 ? ? io:format("~p~n",[BL]),

?11 ? ? F =fun (X,Y)-> {X,Y} end,

?12 ? ? [_|T]=lists:zipwith(F,AL,BL), ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

?13 ? ? T.

比较好

[name,sex,age]=?record_info(fields,langxw).?

?

方法2 不通用fun(Rdef,Rd)->[{Rdef#test.a,Rd#test.a},{Rdef#test.b,Rdef#test.b}].
方法3?17 test1()->?18 ? ? B=#langxw{name=lang,sex=male,age=26}, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??19 ? ? ZL=record_info(fields,langxw),?20 ? ? ZF = fun(Zl,{T,Index}) ->?21 ? ? ? ? ? ? ?{[{Zl,element(Index,B)}|T],Index+1}?22 ? ? ? ? ?end,?23 ? ? lists:foldl(ZF,{[],1},ZL).?24?

热点排行