[erlang-questions] Nested for-loops, there has to be a better way to do this

Fred Hebert (MononcQc) mononcqc@REDACTED
Thu Jul 30 13:57:07 CEST 2009


On Thu, Jul 30, 2009 at 5:46 AM, Robert Raschke<rtrlists@REDACTED> wrote:
> On Wed, Jul 29, 2009 at 10:08 PM, Fred Hebert (MononcQc) <mononcqc@REDACTED
>> wrote:
>
>> I find nested lists comprehension to be the easiest to deal with when
>> representing nested loops:
>>
>> [[ Fn(I,J) || J <- GenExp2 ] || I <- GenEXp1 ].
>>
>> or indented in a different way:
>>
>>  [[[ Fn(I, J, K) || K <- GenExp3 ]
>>                 || J <- GenExp2 ]
>>                 || I <- GenExp1 ].
>>
>> This gives a nested list. If that's not what you want, you could use
>> lists:flatten/1 to change that, but overall I find this form to be the
>> cleanest (visually speaking).
>>
>
> I may be missing something obvious, but ...
>
> 1> [ {A,B,C,A+B+C} || A<-lists:seq(1,5), B<-lists:seq(1,5),
> C<-lists:seq(1,10) ].
> [{1,1,1,3},
>  {1,1,2,4},
>  {1,1,3,5},
>  {1,1,4,6},
>  {1,1,5,7},
>  {1,1,6,8},
>  {1,1,7,9},
>  {1,1,8,10},
>  {1,1,9,11},
>  {1,1,10,12},
>  {1,2,1,4},
>  {1,2,2,5},
>  {1,2,3,6},
>  {1,2,4,7},
>  {1,2,5,8},
>  {1,2,6,9},
>  {1,2,7,10},
>  {1,2,8,11},
>  {1,2,9,12},
>  {1,2,10,13},
>  {1,3,1,5},
>  {1,3,2,6},
>  {1,3,3,7},
>  {1,3,4,8},
>  {1,3,5,9},
>  {1,3,6,...},
>  {1,3,...},
>  {1,...},
>  {...}|...]
> 2>
>
> Robby
>

Haha, yes. Total brain fart on my end. That's what I should have posted instead.


More information about the erlang-questions mailing list