optimization of list comprehensions

Erik Reitsma (RY/ETM) erik.reitsma@REDACTED
Thu Mar 2 11:31:31 CET 2006


If the return value would be large, you would not want it in the list.
But then you could still do:

1> [ begin io:format("~w~n",[X]), empty end || X <- [1,2,3,4,5] ].
1
2
3
4
5
[empty,empty,empty,empty,empty]
2> 

This way the return value of the function with side effect is not added
to the list. I think it is a bit ugly, though.

*Erik.

Robert wrote:
> I have been thinking about this a bit and I wonder if the 
> constructing of the return list really causes any problems. 
> Space-wise it will only be as large as the size of the input 
> list, so I wouldn't worry.
> 
> Robert
> 
> Ulf Wiger (AL/EAB) skrev:
> > I've seen many examples of how people use list comprehensions as a 
> > form of beautified
> > lists:foreach() - that is, they don't care about the return 
> value of 
> > the comprehension.
> > 
> > I hesitate to say that it's bad practice, even though one 
> will build a 
> > potentially large list unnecessarily, since it's actually 
> looks a lot 
> > nicer than using lists:foreach().
> > 
> > Question: would it be in some way hideous to introduce an 
> optimization 
> > where such list comprehensions do everything except 
> actually build the 
> > list? Then they could execute in constant space.
> > 
> > /Ulf W
> > 
> 



More information about the erlang-questions mailing list