Join and split
Jun 21, 2007
It always amazes me when I try out a new programming language and find that the standard library doesn't include join and split functions. join is used to combine a list of strings into a single string with each item separated by a delimiter. split takes a delimited string and divides it into sub-strings. Python, thankfully, does provide these functions:
['a', 'b', 'c']
Erlang provides regexp:split but it looks like I have to add my own join function. Not sure this is the most efficient implementation but it will do the job:
intersperse(_, ) -> ;
intersperse(Element, List) ->
tl(lists:reverse(lists:foldl(fun(X, A) -> [X,Element|A] end, , List))).
join(List, ListOfLists) -> lists:append(intersperse(List, ListOfLists)).
> intersperse(":", ["a","b","c"]).
> join(":", ["a","b","c"]).
And since strings in Erlang are just lists of integers this can be applied to other lists as well.