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

Mazen Harake mazen.harake@REDACTED
Tue Aug 4 08:09:53 CEST 2009


Cool! I did the same thing! Look:

f([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 TC beautifully! ^-^

/M

Zoltan Lajos Kis 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. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>   



More information about the erlang-questions mailing list