[erlang-questions] Proposal: add lists:intersperse/2 and lists:intercalate/2
Jesper Louis Andersen
Sat Mar 5 11:03:17 CET 2016
So to catch up:
* I need to learn how to spell separator :)
* Indeed, the argument order should be intersperse(Sep, Xs), not the other
way around. This looks consistent with most List functions.
* On Joe's comments of why not simply a utility library:
This would have been fine, were it not for the fact that Erlang is
miserably bad at handling ad-hoc utility libraries for other modules. There
is no way I can hide joe_utils.erl since it is a globally known name. This
creates lots of versioning problems down the road. In e.g., Standard ML or
OCaml, I could just package the modules such that 'module JoeUtils' is not
exported outside the scope of the individual libraries and applications.
But Erlang is not that language.
I also think intersperse/intercalate are so often used they shouldn't be in
everyones utils library but should be put in the stdlib base.
* On Richard's comments of other uses than for a string:
The obvious use in Erlang is to produce iolist() types for binary data:
intersperse($, [<<"a">>, <<"b">>, <<"c">>]) is relatively common in my
code. The slightly less obvious use is to interleave a simplifier step in
between every optimization step of a compiler, but I've only done that once
in my life and I'm not sure it is that useful :)
One could argue we should just have tooling such as Hughes-combinators
for output rather than the singular intersperse function, but it is by the
far the missing tool in the toolbox when trying to use standard Erlang as
were it a pretty-printing combinator library.
* On the name choice of 'intersperse/intercalate':
I think the semantics I'm aiming for is so close to the OCaml(Core)/Haskell
implementations I'm willing to reuse that name. Another valid name is
'join' which is common in many other languages. I don't think one is better
than the other, but the FP roots in me says we should adopt FP-language
I'd like to produce the necessary PR for 'intersperse' first, which gives
me an idea of where to put changes. This builds a small unit of work which
should be easy to make concrete. We can always augment this with additional
functions later.' We need at least:
* The implementation in lists.erl (I don't think we need to make it fast
right away, so plain Erlang it is)
* Documentation of the function, with examples
* Tests with good coverage of the function.
Once this is done, adding 'intercalate' is easy, but the question is if it
is needed since once can just 'append' after having interspersed. The power
of having a special function is that it is amenable to certain
optimizations if they are kept as a larger block.
 The thing I have in mind here is John Hughes "The Design of a
On Wed, Mar 2, 2016 at 3:47 PM, Jesper Louis Andersen <
> Hi Erlangers,
> I'd really like to add two functions to the lists module from Haskell:
> intersperse(List, Seperator) produces a list where each element is
> separated by separator, i.e.
> X = [1,2,3]
> [1, x, 2, x, 3] = lists:intersperse(X, x),
> and it's cousin, intercalate(ListOfLists, Separator) is
> append(intersperse(ListOfLists, Seperator)), i.e,
> Y = ["a", "b", "c"]
> "a, b, c" = lists:intercalate(Y, ", "),
> The implementations are straightforward and easy to write tests for, even
> property based tests if needed.
> The rationale for this proposal is that I find myself implementing this
> function again and again in every project I write, and it is highly
> generic. It belongs in a typical list module. OCaml libraries add it.
> Haskell's Data.List has it. I believe Erlang, being a practical language,
> should have it as well.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions