[erlang-questions] Charset conversion / stylistic question.
Wed Apr 18 16:07:18 CEST 2007
--- "Ulf Wiger (TN/EAB)" <>
> Thomas Lindgren wrote:
> > Actually, plain old map/2 isn't tail recursive
> How right you are. Apologies.
For everyone's amusement, here is a tail-recursive
map(F, Xs) -> map_tr(F, Xs, ).
map_tr(F, [X|Xs], Acc) -> map_tr(F, Xs, [F(X)|Acc]);
map_tr(F, , Acc) -> lists:reverse(Acc).
It won't exhaust the process stack unless F/1 does,
but allocates an extra N list cells and incurs an
extra list traversal (N elements). Which perhaps tells
us that tail recursion is not everything.
(Actually, I haven't measured how well map_tr stacks
up against the regular definition. It might surprise
me; if F/1 is substantial work, the extra cost could
become negligible for instance.)
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
More information about the erlang-questions