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

Dieter Schön dieter@REDACTED
Wed May 12 17:53:16 CEST 2021


It seems that Jesper wrote this function a bit later in 2016, if I 
interpret the logs correctly.

Only, the function is named join:

25> X = [1,2,3].
[1,2,3]
26> [1,0,2,0,3] = lists:join(0, X).
[1,0,2,0,3]
27>
27> Y = ["a", "b", "c"].
["a","b","c"]
28> "a, b, c" = lists:flatten(lists:join(", ", Y)).
"a, b, c"


regards,

dieter


On 23.03.21 21:18, Siraaj Khandkar wrote:
> Greetings Erlangers,
>
> Nothing serious, just some fun here. I stumbled on the function
> "add-between" in Racket stdlib, which reminded me of the lively, lolzy 
> debate we had here 5 years ago :-D
>
>     $ racket
>     Welcome to Racket v7.9 [bc].
>     > (add-between '(a b c d) 0)
>     '(a 0 b 0 c 0 d)
>     >
>
> https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Flist..rkt%29._add-between%29%29 
>
>
>
> On 3/2/16 12:02 PM, Siraaj Khandkar wrote:
>> On 3/2/16 9:47 AM, Jesper Louis Andersen wrote:
>>> 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.
>>>
>>> Thoughts?
>>
>> +1
>>
>> Though I prefer the name "interleave" to "intersperse", since its 
>> meaning is more-precise and closer to the intended behavior here.


More information about the erlang-questions mailing list