[erlang-questions] how to quelch lc warnings?
Richard Carlsson
richardc@REDACTED
Thu Apr 12 12:48:22 CEST 2007
Ulf Wiger (TN/EAB) wrote:
> Apologies for the silly example, but imagine I want to do this:
>
> Eshell V5.5.3.1 (abort with ^G)
> 1> Nums = [1,2,3], Nouns = [shoe,box,tape], Verbs = [].
> []
> 2> [Nums || Nums =/= []] ++ [Nouns || Nouns =/= []] ++ [Verbs || Verbs
> =/= []].
> [[1,2,3],[shoe,box,tape]]
>
> I have a more complex example, where I'm actually doing something useful,
So what you actually try to do in the longer example, is cause side
effects (write), rather than build lists, but at most once per list
comprehension? I didn't know that there was a meaningful interpretation
of a list comprehension without any generator, but the definition says
"once for each combination of generator list elements for which all
filters are true", and if there are no generators then that is of course
zero or one time. Makes sense. Still, the warning is typically useful.
There is a simple way of suppressing the warning: insert a dummy
generator that controls how many repetitions you want:
[X || _<-[1], ...Tests...]
if Tests fail, you get [], otherwise [X]. This can be used for
duplication as well:
[X || _<-[1,2], ...Tests...]
will yield [] or [X,X], and
[X || _<-lists:seq(1,N), ...Tests...]
yields [] or [X, ..., X] (X repeated N times).
/Richard
More information about the erlang-questions
mailing list