[erlang-questions] list:join() for erlang?
Fri Sep 14 11:29:58 CEST 2007
I haven't profiled it, but the code seems fine to me.
"Bob Ippolito" <> writes:
> > Looks like you're right. Here's my tail-recursive version of the same:
> > join(, _) -> ;
> > join([S1 | S_Rest], Sep) ->
> > S1 ++ reverse_join(lists:reverse(S_Rest), Sep, ).
This call is body-recursive, which means that that '++' is evaluated from
right to left. This use of '++' is fine.
> > reverse_join(, _, Joined) -> Joined;
> > reverse_join([S1 | S_Rest], Sep, Joined) ->
> > reverse_join(S_Rest, Sep, Sep ++ S1 ++ Joined).
This usage of '++' is also OK, since the growing result is on the right
> Using ++ all over the place is probably a bad idea, at least according
> to the performance guide and my intuition. I haven't profiled it.
The Efficiency Guide could be a little bit clearer about '++'. We will probably
update it in the R12B release.
'++' is not always bad. It is bad if you build a result on the left-hand side of '++',
because the result that is growing will be copied again and again. It is fine if the
result is on the right-hand side, because the right-hand side is not copied.
Björn Gustavsson, Erlang/OTP, Ericsson AB
More information about the erlang-questions