Iteration over lists

Ulf Wiger (AL/EAB) <>
Fri Mar 17 12:04:55 CET 2006


 
 Bjorn Gustavsson wrote:
> 
> Your benchmark is not fair.
> 
> What you call recursion is in fact iteration expressed as 
> tail-recursion.
> Naturally it is faster.
> 
> Real recursion would look this:
> 
> recursion([H | T]) ->
>     [integer_to_list(H) | recursion(T)];
> recursion([]) -> [].
> 
> A list comprehension will in fact be translated to the exact 
> same code a the function above.


Of course one way to view the benchmark is that it
gives a hint about the overhead of atually constructing
the list when using list comprehension instead of 
lists:foreach(). A list comprehension which _didn't_
built the list and avoided using funs could then be
significantly faster, right (that is, also faster than
lists:foreach())?  (:

/Ulf W



More information about the erlang-questions mailing list