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

Sat Feb 27 00:28:58 CET 2010

```That's right. The second parameter is called Accumulating Parameter. The
accumulating
technique used by Dmitry is to accumulate heads of lists to AP, and then
when base case
is met lists:reverse/1 must be applied to the result. Your method is to
first attach tails of lists
in front of AP, so it's no need to reverse the result.

2010/2/27 Robert Virding <rvirding@REDACTED>

Here are two very similar versions based on the code in lists.erl.
> They are both coded in a very straight forward style (I think) without
> reverse or append, but I haven't measured them.
>
> -module(ft).
>
> -export([fl1/2, fl2/2]).
>
> fl1([[C|_]=H|T], Tail) when is_integer(C) ->
>    [H|fl1(T, Tail)];
> fl1([H|T], Tail) when is_list(H) ->
>    fl1(H, fl1(T, Tail));
>
fl1([H|T], Tail) ->
>    [H|fl1(T, Tail)];
>

The third clause means that the head of H is not an integer and H is not a
list, but what case
does [H|T] match? I think the clause is redundant.

> fl1([], Tail) -> Tail.
>
> fl2([[C|_]=H|T], Tail) when not is_integer(C) ->
>    fl2(H, fl2(T, Tail));
> fl2([[]|T], Tail) -> fl2(T, Tail);
>

If the case below would pick H as a string and glue it in front of the
result of Tail, the above
case is not needed, because H can be a string or an empty string. The above
clause is not
necessary unless any empty string must be strip out from the deep string
list.

fl2([H|T], Tail) ->
>    [H|fl2(T, Tail)];
> fl2([], Tail) -> Tail.
>
> Robert
>
> 2010/2/26 Dmitry Belayev <rumata-estor@REDACTED>:
> > How do you propose to remove lists:reverse? To use A ++ [I] in the second
> > clause? I don't think that would be optimal solution.
> >
> > Robert Virding wrote:
> >>
> >> I personally think this is the best solution, write your own function
> >> that is, as it is such a simple function to start with. Of course, I
> >> wouldn't use reverse but that is just a matter of detail. :-)
> >>
> >> Robert
> >>
> >> On 26 February 2010 16:50, Dmitry Belayev <rumata-estor@REDACTED> wrote:
> >>
> >>>
> >>> You can write your own:
> >>>
> >>> f(List) ->
> >>>  lists:reverse(f(List, [])).
> >>>
> >>> f([], A) ->
> >>>  A;
> >>> f([L|_]=I, A) when is_number(L) ->
> >>>  [I | A];
> >>> f([H|Tail], A) ->
> >>>  f(Tail, f(H, A)).
> >>>
> >>> Bengt Kleberg wrote:
> >>>
> >>>>
> >>>> Greetings,
> >>>>
> >>>> I have the following list: ["asd",[["Flow","kalle"]],["Sub","F"]]
> >>>> I would like to flatten it to: ["asd","Flow","kalle","Sub","F"]
> >>>> lists:flatten/1 is too effective. It gives me: "asdFlowkalleSubF"
> >>>>
> >>>> Is there another flatten somewhere?
> >>>>
> >>>>
> >>>> bengt
> >>>>
> >>>>
> >>>> ________________________________________________________________
> >>>> erlang-questions (at) erlang.org mailing list.
> >>>> See http://www.erlang.org/faq.html
> >>>> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
> >>>>
> >>>>
> >>>>
> >>>>
> >>>
> >>> ________________________________________________________________
> >>> erlang-questions (at) erlang.org mailing list.
> >>> See http://www.erlang.org/faq.html
> >>> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
> >>>
> >>>
> >>>
> >>
> >>
> >
> >
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>
```