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 ->