Nested for-loops, there has to be a better way to do this

Yves S. Garret yoursurrogategod@REDACTED
Tue Aug 4 03:51:27 CEST 2009


Cool.  But I do wonder, what are the performance hits compared to one
larger function?  Reversing lists, returning values, etc.  That's one
of the reasons why I made the 'monolithic' function that I did.

On Aug 3, 9:40 am, "Zoltan Lajos Kis" <ki...@REDACTED> wrote:
> for(List) ->
>     for(List, [], []).
>
> for([], [], Result) ->
>     lists:reverse(Result);
>
> for([], [Hd|Stack], Result) ->
>     for(Hd, Stack, Result);
>
> for([Hd|List], Stack, Result) ->
>     NewStack = case Hd > 1 of
>         true -> [[Hd-1|List]|Stack];
>         false -> Stack
>     end,
>     for(List, NewStack, [Hd|Result]).
>
> > for:for([4,2,1,1]).
>
> [4,2,1,1,1,1,1,3,2,1,1,1,1,1,2,2,1,1,1,1,1,1,2,1,1,1,1,1]
>
> Passed all TCs :)
>
> Regards,
> Zoltan.
>
> > Not quite.
>
> > For your input, this should be your output:
> > [4, 2, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2,
> > 1, 1, 1, 1, 1]
>
> ________________________________________________________________
> erlang-questions mailing list. Seehttp://www.erlang.org/faq.html
> erlang-questions (at) erlang.org


More information about the erlang-questions mailing list