List-flattening function in erlang feels too wordy I wrote a tail-recursive list-flattening function, but I'm not too happy with it.

a) Is tail-recursion necessary here, or will the function be optimized without it?
b) It's ugly how many clauses there are, and that I have to call out to flatten in the expression of the first clause of flatten_acc

So, how could I do better?

(To be clear, this is a function that completely flattens the list -- e.g. flatten([foo, [[[bar]], [baz]]]) == [foo, bar, baz])

flatten(List) -&gt;
    lists:reverse(flatten_acc(List, [])).

flatten_acc([[[H|T0]|T1]|T2], Acc) -&gt;
    flatten_acc([flatten([[H|T0]|T1])|T2], Acc);
flatten_acc([[H|T0]|T1], Acc) -&gt;
    flatten_acc([T0|T1], [H|Acc]);
flatten_acc([[]|T], Acc) -&gt;
    flatten_acc(T, Acc);
flatten_acc([H|T], Acc) -&gt;
    flatten_acc(T, [H|Acc]);
flatten_acc([], Acc) -&gt;
    Acc.


Thanks!
Little smaller than yours.

% flatten a list
my_flatten([])-&gt;
    [] ;
my_flatten([[]|T])-&gt;
    my_flatten(T);
my_flatten([[H|T]|T2])-&gt;
    my_flatten([H|[T|T2]]);
my_flatten([H|T])-&gt;
    [H|my_flatten(T)].


http://erlang99.wordpress.com/2009/01/19/p07-flatten-a-nested-list-structure/

my_flatten_rec(L)-&gt;
    lists:reverse(my_flatten_rec([],L)).
my_flatten_rec(Acc,[[]|T])-&gt;
    my_flatten_rec(Acc,T);
my_flatten_rec(Acc,[[H|T]|T2])-&gt;
    my_flatten_rec(Acc,[H|[T|T2]]);
my_flatten_rec(Acc,[H|T])-&gt;
    my_flatten_rec([H|Acc],T);
my_flatten_rec(Acc,[])-&gt;
    Acc.