Improving my code

Tim Bates tim@REDACTED
Thu Aug 11 15:02:03 CEST 2005


Hi folks,
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?

update_history takes a list of {Name, Max, [Values]} tuples. It finds 
the element(s) of the list with matching Name, and adds Value to the 
front of the list. It then makes sure the Values list has no more than 
Max values in it. Other elements of the list are unchanged.

eg
update_history([{foo, 2, [1,2]}, {bar, 5, [2,3]}], foo, 0) ->
   [{foo, 2, [0,1]}, {bar, 5, [2,3]}].




update_history(History, Name, Value) ->
   update_history(History, Name, Value, []).


update_history([], _Name, _Value, Acc) ->
   Acc;

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

update_history([H|Rest], Name, Value, Acc) ->
   update_history(Rest, Name, Value, Acc ++ [H]).


trunclist(List, Num) ->
   trunclist(List, Num, []).


trunclist(_List, 0, Acc) ->
   Acc;

trunclist([], _N, Acc) ->
   Acc;

trunclist([L|Rest], N, Acc) ->
   trunclist(Rest, N - 1, Acc ++ [L]).



Thanks,
Tim.

-- 
Tim Bates
tim@REDACTED



More information about the erlang-questions mailing list