[erlang-questions] how: Another library flatten function?

黃耀賢 (Yau-Hsien Huang) g9414002.pccu.edu.tw@REDACTED
Sat Feb 27 15:00:52 CET 2010


Right, accumulating parameter and tail recursion.
Thank you.

2010/2/27 Dmitry Belayev <rumata-estor@REDACTED>

> I'm not sure but:
>
> f(List) ->
>   f(List, [], []).
>
> f([], [], A) ->
>   lists:reverse(A);
> f([], [H|T], A) ->
>   f(H, T, A);
> f([L|_]=I, T, A) when is_number(L) ->
>   f(T, [], [I | A]);
> f([H|Tail], T, A) ->
>   f(H, [Tail | T], A).
>
>
>
> 黃耀賢 (Yau-Hsien Huang) wrote:
>
>> The basic concept of flattening a list, similar to process a binary tree,
>> is
>> to flatten the head
>> elements which may be a list or not, to flatten the tail which is a list,
>> and to concatenate
>> these two parts. How to make a flattening function tail-recursive?
>>
>>
>> 2010/2/27 Dmitry Belayev <rumata-estor@REDACTED>
>>
>>
>>
>>> Shame on me, my own version uses f(_, f(_)) so it is not-tail recursive
>>> too.
>>>
>>>
>>> Dmitry Belayev wrote:
>>>
>>>
>>>
>>>> It would be best solution in non-strict language like haskell, but in
>>>> erlang it is not tail-recursive, so it will use more memory.
>>>>
>>>>
>>>>
>>>>
>>>
>>
>>
>
>


More information about the erlang-questions mailing list