Skip to content

Instantly share code, notes, and snippets.

@jcomellas
Created March 4, 2013 18:46
Show Gist options
  • Save jcomellas/5084441 to your computer and use it in GitHub Desktop.
Save jcomellas/5084441 to your computer and use it in GitHub Desktop.
Join a list of elements in Erlang, adding a separator between them
%% @doc Join a a list of elements adding a separator between
%% each of them.
-spec join(iolist(), Sep :: term()) -> iolist().
join([Head | Tail], Sep) ->
join_list_sep(Tail, Sep, [Head]);
join([], _Sep) ->
[].
-spec join_list_sep(iolist(), Sep :: term(), Acc :: iolist()) -> iolist().
join_list_sep([Head | Tail], Sep, Acc) ->
join_list_sep(Tail, Sep, [Head, Sep | Acc]);
join_list_sep([], _Sep, Acc) ->
lists:reverse(Acc).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment