[newbie] refactor function?

Gaspar Chilingarov <>
Tue Feb 1 10:07:10 CET 2005


Hello All!

what is a correct way of writing following function (initQuota/1) -- 
probably i got problems thinking in a functional way ;) ?

i wish to get rid of last two cases.

following function just adds to list of tuples with range descriptions 
initial zero value, which will be changed later in application.

-cut-
testInitQuota() ->
   [{ single, "AAA", 0 }] = initQuota( [ { single, "AAA" } ] ),
   [{ range, "AAA", "BBB", 0 }] =
	initQuota( [ { range, "AAA", "BBB" } ] ),
   [{ single, "AAA", 0 },{ range, "AAA", "BBB", 0 }] =
	initQuota( [ { single, "AAA" },{ range,    "AAA", "BBB" } ] ).

initQuota([Head | QuotaList]) ->
   lists:reverse(initQuota(Head, QuotaList, [])).
   
initQuota( { range, Value1, Value2 }, [Head | QuotaList], NewList ) ->
   initQuota( Head, QuotaList, [ { range, Value1, Value2, 0 } ] ++ 
NewList );
initQuota( { single, Value1 }, [Head | QuotaList], NewList ) ->
   initQuota( Head, QuotaList, [ { single, Value1, 0 } ] ++ NewList );
initQuota( { range, Value1, Value2 }, [], NewList ) ->
   [ { range, Value1, Value2, 0 } ] ++ NewList;
initQuota( { single, Value1 }, [], NewList ) ->
   [ { single, Value1, 0 } ] ++ NewList.
-cut-

another way -- what is a common way to traverse list, find one element, 
and replace it with new data? are there any functions in lists: module 
or i should write it myself ?

-- 
Gaspar Chilingarov
System Administrator

t +3749 419763
w www.web.am
e 



More information about the erlang-questions mailing list