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

Zoltan Lajos Kis kiszl@REDACTED
Mon Aug 3 15:40:49 CEST 2009


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]
>




More information about the erlang-questions mailing list