erlang基础练习题
1、将列表中的integer,float,atom转成字符串并合并成一个字个字符串:[1,a,4.9,“sdfds”] 结果:“1a4.9sdfds”(禁用++ – append concat实现)
/ / An highlighted block
spl ( [ ] ) - > [ ] ;
spl ( [ H | T ] ) - > X = if is_integer ( H ) - > [ integer_to_list ( H ) | spl ( T ) ] ; is_atom ( H ) - > [ atom_to_list ( H ) | spl ( T ) ] ; is_float ( H ) - > [ float_to_list ( H , [ { decimals , 1 } ] ) | spl ( T ) ] ; is_list ( H ) - > [ H | spl ( T ) ] ; true - > erlang : error ( "data error" ) end , binary_to_list ( list_to_binary ( X ) ) .
spl2 ( L ) - > spl2 ( L , [ ] ) . spl2 ( [ ] , ACC ) - > ACC ;
spl2 ( [ H | T ] , ACC ) - > X = if is_integer ( H ) - > spl2 ( T , [ ACC | integer_to_list ( H ) ] ) ; is_atom ( H ) - > spl2 ( T , [ ACC | atom_to_list ( H ) ] ) ; is_float ( H ) - > spl2 ( T , [ ACC | float_to_list ( H , [ { decimals , 1 } ] ) ] ) ; is_list ( H ) - > spl2 ( T , [ ACC | H ] ) ; true - > erlang : error ( "data error" ) end , binary_to_list ( list_to_binary ( X ) ) .
2、得到列表或元组中的指定位的元素
/ / An highlighted block
pop ( [ ] , _ ) - > [ ] ;
pop ( { } , _ ) - > [ ] ; pop ( L , K ) - > case is_list ( L ) of true - > pop ( L , K , [ ] ) ; false - > pop ( tuple_to_list ( L ) , K , [ ] ) end . pop ( [ H | T ] , K , _ ) - > if K =:= 1 - > H ; K = < 0 - > exit ( K ) ; true - > pop ( T , K - 1 ) end .
3、根据偶数奇数过滤列表或元组中的元素(禁用API实现)
/ / An highlighted block
odds_and_evens ( [ ] ) - > [ ] ;
odds_and_evens ( { } ) - > [ ] ;
odds_and_evens ( L ) - > case is_list ( L ) of false - > odds_and_evens1 ( tuple_to_list ( L ) , [ ] , [ ] ) ; true - > odds_and_evens1 ( L , [ ] , [ ] ) end . odds_and_evens1 ( [ H | T ] , Odds , Evens ) - > case ( H rem 2 ) of 1 - > odds_and_evens1 ( T , [ H | Odds ] , Evens ) ; 0 - > odds_and_evens1 ( T , Odds , [ H | Evens ] ) end ;
odds_and_evens1 ( [ ] , Odds , Evens ) - > { lists : reverse ( Odds ) , lists : reverse ( Evens ) } .
4、便用匿名函数对列表奇数或偶数过滤
/ / An highlighted block
filter_odd ( [ ] ) - > [ ] ;
filter_odd ( { } ) - > { } ;
filter_odd ( L ) - > case is_list ( L ) of false - > list_to_tuple ( [ X | | X < - L , X rem 2 =:= 1 ] ) ; true - > [ X | | X < - L , X rem 2 =:= 1 ] end . filter_evens ( L ) - > case is_list ( L ) of false - > list_to_tuple ( [ X | | X < - L , X rem 2 =:= 0 ] ) ; true - > [ X | | X < - L , X rem 2 =:= 0 ] end .
5、计算数字列表[1,2,3,4,5,6,7,8,9]索引N到M的和
/ / An highlighted block
sum ( [ ] , _ , _ ) - > [ ] ;
sum ( L , N , M ) - > for ( L , 1 , N , M ) . for ( [ ] , _ , _ , _ ) - > 0 ;
for ( [ H | T ] , I , N , M ) - > if I >= N , I = < m - > H + for ( T , I + 1 , N , M ) ; true - > for ( T , I + 1 , N , M ) end .
6、查询List1是为List2的前缀(禁用string API实现)
/ / An highlighted block
test ( [ ] , [ ] ) - > true ;
test ( _ , [ ] ) - > false ;
test ( [ ] , _ ) - > true ;
test ( [ H1 | T1 ] , [ H2 | T2 ] ) - > case H1 =:= H2 of true - > test ( T1 , T2 ) ; false - > false end .
7、逆转列表或元组(禁用lists API实现)
/ / An highlighted block
reverse ( [ ] ) - > [ ] ;
reverse ( { } ) - > { } ;
reverse ( L ) - > if is_list ( L ) - > reverse_list ( L , [ ] ) ; is_tuple ( L ) - > reverse_tuple ( tuple_to_list ( L ) , [ ] ) ; true - > throw ( "no a tuple or list" ) end . reverse_list ( [ ] , L ) - > L ;
reverse_list ( [ H | T ] , L ) - > reverse_list ( T , [ H | L ] ) . reverse_tuple ( [ ] , L ) - > list_to_tuple ( L ) ;
reverse_tuple ( [ H | T ] , L ) - > reverse_tuple ( T , [ H | L ] ) .
8、对列表进行排序
/ / An highlighted block
sort_list ( [ ] ) - > [ ] ;
sort_list ( [ T | H ] ) - > sort_list ( [ X | | X < - H , X < T ] ) ++ [ T ] ++ sort_list ( [ X | | X < - H , X >= T ] ) .
sort_list ( [ ] ) - > [ ] ;
sort_list ( L ) - > sort_list ( L , len ( L ) ) . sort_list ( L , 1 ) - > L ;
sort_list ( [ H | T ] , Len ) - > Result = get_list ( H , T ) , sort_list ( Result , Len - 1 ) . get_list ( H , [ ] ) - > [ H ] ;
get_list ( X , [ H | T ] ) - > case ( X > H ) of true - > [ H | get_list ( X , T ) ] ; false - > [ X | get_list ( H , T ) ] end .
len ( L ) - > len ( L , 0 ) . len ( [ ] , ACC ) - > ACC ;
len ( [ _H | T ] , ACC ) - > len ( T , 1 + ACC ) .
9、对数字列表进行求和再除以指定的参数,得到商余
/ / An highlighted block
sum_rem ( [ ] , _ ) - > [ ] ;
sum_rem ( L , N ) - > { sum ( L ) / N , sum ( L ) rem N } . sum ( L ) - > sum ( L , 0 ) . sum ( [ ] , ACC ) - > ACC ;
sum ( [ H | T ] , ACC ) - > sum ( T , ACC + H ) .
10、获得当前的堆栈
erlang:get_stacktrace().
11、获得列表或元组中的最大最小值(禁用API实现)
/ / An highlighted block
输入A(元子列表),B(数值列表)两个相同长度的参数进行随机匹配(要求字母为Key,数值为Value),支持指定的Key和Value在一组
/ / An highlighted block
r_match ( A , B ) - > case ( length ( A ) =:= length ( B ) ) and is_list ( A ) and is_list ( B ) of true - > L1 = random ( A ) , L2 = random ( B ) , match ( L1 , L2 , [ ] ) ; false - > error ( "data error" ) end .
match ( [ ] , [ ] , ACC ) - > ACC ;
match ( [ L1H | L1T ] , [ L2H | L2T ] , ACC ) - > case is_atom ( L1H ) of true - > case is_integer ( L2H ) of true - > match ( L1T , L2T , [ { L1H , L2H } | ACC ] ) ; false - > error ( "bad data" ) end ; false - > error ( "bad data" ) end .
r_match ( A , B , C ) - > case ( length ( A ) =:= length ( B ) ) and is_list ( A ) and is_list ( B ) of true - > para_match ( A , B , C , [ ] , [ ] , false , false ) ; false - > error ( "data error" ) end .
para_match ( [ ] , [ ] , { Key , Value } , L1 , L2 , true , true ) - > match ( random ( L1 ) , random ( L2 ) , [ { Key , Value } ] ) ;
para_match ( [ ] , _ , _ , _ , _ , false , true ) - > error ( "bad data" ) ;
para_match ( [ ] , [ ] , _ , _ , _ , true , false ) - > error ( "bad data" ) ;
para_match ( [ H1 | T1 ] , B , { Key , Value } , L1 , L2 , F1 , F2 ) - > case ( H1 =:= Key ) and ( F1 =:= false ) of true - > para_match ( T1 , B , { Key , Value } , L1 , L2 , true , F2 ) ; false - > para_match ( T1 , B , { Key , Value } , [ H1 | L1 ] , L2 , F1 , F2 ) end ; para_match ( [ ] , [ H1 | T1 ] , { Key , Value } , L1 , L2 , F1 , F2 ) - > case ( H1 =:= Value ) and ( F2 =:= false ) of true - > para_match ( [ ] , T1 , { Key , Value } , L1 , L2 , F1 , true ) ; false - > para_match ( [ ] , T1 , { Key , Value } , L1 , [ H1 | L2 ] , F1 , F2 ) end . random ( L ) - > F = lists : map ( fun ( X ) - > { rand : uniform ( ) , X } end , L ) , S = lists : sort ( F ) , [ V | | { _ , V } < - S ] .