[erlang-questions] how to quelch lc warnings?
Richard Carlsson
richardc@REDACTED
Mon Apr 16 10:24:17 CEST 2007
ok wrote:
> Isn't this stuff about list comprehensions with no generator distracting
> us from the original application of the idea? It was something like
>
> [X || X /= []] ++ [Y || Y /= []] ++ [Z || Z /= []]
>
> which can be written easily as a list comprehension WITH a generator:
>
> [T | T <- [X,Y,Z], T /= []]
>
> This way does require the invention of a name (T), but to me it is
> more intention-revealing (I want those of X, Y, Z that are not empty
> lists collected as a list) and avoids the repeated /= [] test.
Ulf's short example was a bit misleading in this way. What he was
really after was a compact conditional: do-this-unless-x-is-empty,
which used side effects instead of building a list. (Substitute a
comma for ++ above.)
I don't necessarily endorse the use of list comprehensions for
side effects, and would probably have used a macro instead:
?UNLESS(X =:= [],
begin
...side effects...
end),
?UNLESS(Y =:= [],
...
or just
if X =/= [] ->
...side effects...;
true ->
ok
end,
...
but Ulf said that he sort of liked the layout he got with LC:s (and I
agree that it gets rid of distracting tokens like if/->/end/true/ok.)
/Richard
More information about the erlang-questions
mailing list