[erlang-questions] Proposal: add lists:intersperse/2 and lists:intercalate/2

Siraaj Khandkar <>
Mon Mar 7 17:59:55 CET 2016

On 3/6/16 10:18 PM, Richard A. O'Keefe wrote:
> On 5/03/16 4:33 am, Siraaj Khandkar wrote:
>> The notion of sameness here is problematic, since you're now requiring
>> that the above-quoted "sequences of information" are sets, which is
>> unlikely the intended meaning.
> You just lost me.  I was requiring that the sequences be SEQUENCES.
> How does anything I wrote require them to be sets?
> Here's what I was saying:
>    going from [a,b,c] and [1,2,3] to [a,1,b,2,c,3] *IS* interleaving
>    going from [a,b,c] and 0 to [a,0,b,0,c,0] is *NOT* interleaving.

Both of the above results are functions of 2 sequences, the difference 
is that 1st works with a pre-computed list, while the 2nd works with a 
an infinite stream of 0s.

So the difference is in the sources of the sequences, not in how they're 

This clearly calls for some distinction in API (i.e. how those sequences 
are passed), but not a different description of the meaning of the 
outcome (i.e. name).

One (quick and rough) example of an API distinction could be:

-spec interleave([A], {exactly, [A]} | {repeat, A}) -> [A].

Which can actually be made even more general, since repeating the same 
element is just a special case of cycling a single element list:

-spec interleave([A], {exactly, [A]} | {cycle, [A]}) -> [A].

... but I digress ...

> The question is not whether a sequence contains repeated elements
> but a question as to whether it *IS* a sequence in the first place,
> I am happy to call [a,b,c] [0,0,0] -> [a,0,b,0,c,0] interleaving.

Right, the main difference in our points of view is that I see this 0 as 
just an argument for a process which makes an infinite stream of 0s 
(i.e. another sequence), while the rule for combining the sequences 
remains exactly the same.

Since, if you truly only have one 0, then the best you can do is :

   [1, 0, 2]

or maybe

   [1, 0, 2, 3, 4, 5]

to insert more 0s - you need more 0s, hence you need a sufficiently-long 
sequence of them; how that sufficiently-long sequence is computed is 
unrelated to how it is combined with the 1st sequence.

What I'm naming is the rule for the combination.

More information about the erlang-questions mailing list