optimization of list comprehensions
Richard A. O'Keefe
Thu Mar 2 23:11:37 CET 2006
Serge Aleynikov <serge@REDACTED> wrote:
I do want to throw a vote for Mats' suggestion on the alternative syntax:
(I || I <- List) -> ok
What I also find limiting is that it's not possible to have an
accumulator when using list comprehension. Perhaps something like this
could also be considered (unless someone can suggest a better syntax):
[Acc+1, I || Acc, I <- List](0) -> Acc1
Initial Acc's value
EEEK! I started watching this thread with the belief that the occasional
small abuse of list comprehension syntax was OK. This suggestion has
finally convinced me otherwise.
I am not at all happy about (Expr || Pat <- List) because
(A) it looks like a syntax error; it's quite likely to be 'corrected'
to use square brackets
(B) it reduces the parser's ability to diagnose syntax errors
(C) nothing about it visually suggests the most important piece of
information, which is that the expression is executed for side
effects, not for its value.
The idea of explicitly writing "_ = [Expr || Pat <- List]" has none
of those defects. The only compiler change that's called for is to
emit a warning when the value of a list comprehension *isn't* used in
some way (including _= as a use).
As for combining accumulation with list comprehension,
one can already write
lists:foldl(fun (X,Y) -> X+Y end, 0,
[some list comprehension goes here])
which isn't *that* bad. Any more direct syntax (such as something
based on the Lisp 'do' construct, for example) would have to involve
some way of presenting two bindings for the same names in the same
construct (as 'do' does), which is not a very Erlangish thing to do.
More information about the erlang-questions