optimization of list comprehensions

Serge Aleynikov serge@REDACTED
Tue Mar 7 00:07:10 CET 2006


Richard A. O'Keefe wrote:
> Well, we can improve that:
> 
> 	lists:foldl(fun(I, Acc) -> Acc+I end, 0,
> 	  [I || I <- lists:seq(1, 20), I > 10])
> 
> and indeed we can improve it to
> 
> 	lists:sum([I || I <- lists:seq(1, 20), I > 10])
> 
> which is pretty hard to improve on, no?  The Haskell version is
> more concise:
> 
> 	sum [x | x <- [1..20], x > 10]

Well, you could as well say that one could similarly do:

         -import(lists, [sum/1, seq/2]).

         sum([I || I <- seq(1,20), I > 10]).

> but that's because Haskell has .. notation and doesn't use parentheses
> or commas nearly as much as Erlang does.

I agree with your statement in the previous email that it's important 
not to clutter the language with rediculous notations, yet if list 
comprehensions are already a part of the language with implementation of 
map and filter patterns, why not allowing for folding as well, which is 
probably as frequently used operation as map?

Considering more complicated folding examples, I tend to believe that 
the more complicated/lengthy the function implementation under the fold 
is, the less it matters whether lists:fold vs. comprehension is used, as 
most of the mental reading effort is focussed on the folding function 
itself.

-- 
Serge Aleynikov
R&D Telecom, IDT Corp.
Tel: (973) 438-3436
Fax: (973) 438-1464
serge@REDACTED



More information about the erlang-questions mailing list