Improving my code

Vlad Dumitrescu XX (LN/EAB) vlad.xx.dumitrescu@REDACTED
Thu Aug 11 15:16:19 CEST 2005


Hi,

> In the interests of improving my code (I understand the functional 
> paradigm but am not yet fluent in it) would someone kindly 
> suggest how can I improve upon the current snippets?

They look good, really. There are just a few observations to make:
 
> update_history([], _Name, _Value, Acc) ->
>    Acc;
***  lists:reverse(Acc);

> update_history([{Name, Max, Values}|Rest], Name, Value, Acc) ->
>    NewVs = trunclist([Value] ++ Values, Max),
***  NewVs = trunclist([Value | Values], Max),

>    update_history(Rest, Name, Value, Acc ++ [{Name, Num, NewVs}]);
***  update_history(Rest, Name, Value, [{Name, Num, NewVs} | Acc]);

> update_history([H|Rest], Name, Value, Acc) ->
>    update_history(Rest, Name, Value, Acc ++ [H]).
***  update_history(Rest, Name, Value, [H | Acc]).
 
This idiom of constructing a reversed list and "straighten it up" before returning is worth remembering - the application to trunclist is left as an exercise :-)

best regards,
Vlad



More information about the erlang-questions mailing list