# Proposal: add lists:intersperse/2 and lists:intercalate/2

47 messages
123
Open this post in threaded view
|

## Proposal: add lists:intersperse/2 and lists:intercalate/2

 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?-- J. _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 I find myself reimplementing the first one a lot, for use with iolists. The second one is string:join/2, right? > On 02 Mar 2016, at 06:47, Jesper Louis Andersen <[hidden email]> 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? > > -- > J. > _______________________________________________ > erlang-questions mailing list > [hidden email] > http://erlang.org/mailman/listinfo/erlang-questions_______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 In reply to this post by Jesper Louis Andersen-2 On Wed, Mar 02, 2016 at 03:47:41PM +0100, 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, > > Thoughts? > > -- > J. Hi Jesper Please spell Separator consistently and correctly :-) Bob -- Kiss me, Kate, we will be married o' Sunday.                 -- William Shakespeare, "The Taming of the Shrew" _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 In reply to this post by Pierre Fenoll-2 I recently implemented the second as "join" - ala string, but for arbitrary separators and list elements.I think the name "intersperse" is descriptive enough."intercalculate" no so much. I'd go with "join".In the interest of consistency with other lists functions, I'd put the list argument last in each case.On Wed, Mar 2, 2016 at 8:54 AM Pierre Fenoll <[hidden email]> wrote:I find myself reimplementing the first one a lot, for use with iolists. The second one is string:join/2, right? > On 02 Mar 2016, at 06:47, Jesper Louis Andersen <[hidden email]> 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? > > -- > J. > _______________________________________________ > erlang-questions mailing list > [hidden email] > http://erlang.org/mailman/listinfo/erlang-questions _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 In reply to this post by Jesper Louis Andersen-2 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. _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 Hello, “interleave” and “join” are friendly for non-native English programmers.   Otherwise +1 - Dmitry > On Mar 2, 2016, at 7:02 PM, Siraaj Khandkar <[hidden email]> 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. > _______________________________________________ > erlang-questions mailing list > [hidden email] > http://erlang.org/mailman/listinfo/erlang-questions_______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 In reply to this post by Joe Armstrong-2 On Wed, Mar 2, 2016 at 7:28 PM, Joe Armstrong wrote:Years ago I wanted to add a -needs(Vsn) annotation to module For example:      -needs(erl18). Means would mean you need version 18 of erlang. That way if you added a needs annotation to the code that used the updated lists then an earlier version of Erlang could give a meaningfull diagnostic and not just crash when the encountering a function 'from the future'. Actually I had another problem today - a program I wrote in 2003 did not work with the latest and greatest Erlang - not because the language had changed but because the library functions had been renamed and moved around. I guess a lint program would be useful here. It should be fairly doable to download all old erlangs and simple make lists of all the function names in all modules and do a bit of consistency checking.One such tool is geas https://github.com/crownedgrouse/geas. regards,Vlad _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 In reply to this post by Joe Armstrong-2 Hi Joe, I did it in crownedgrouse/geas_devel database either in term and yaml format starting R15. And as Vlad said, my project geas does this work on project and dependancies,  compiled or not. Integrated in rebar and erlang.mk. Enjoy. _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 In reply to this post by Jesper Louis Andersen-2 On 3/03/16 3:47 am, Jesper Louis Andersen wrote: > I'd really like to add two functions to the lists module from Haskell: > > intersperse(List, Seperator) Why have you swapped the arguments?  The Haskellfunction     Data.List.intersperse :: a -> [a] -> [a] puts the separator *first*.  > produces a list where each element is separated by separator, You can't separate one thing.  The elements *are* separated by the separator. > > and it's cousin, intercalate(ListOfLists, Separator) is > append(intersperse(ListOfLists, Seperator)), Once again, why have you switched the arguments? Data.List.intercalate :: [a] -> [[a]] -> [a] puts the separator *first*. I've never really been happy with the name "intercalate". To the extent that it's acceptableto use "intercalate" for anything other than adding chunks to the calendar in order to fix itup, it means what the intersperse function does.  However, the name _is_ established inHaskell, and there's no point in gratuitous difference. Which is why it is important to get the argument order right. intersperse and intercalate are very nearly the same function. intersperse sep xs = interwhatsit (:)  [] (sep:)  xs intercalate sep xs = interwhatsit (++) [] (sep++) xs interwhatsit :: (a -> b -> b) -> b -> (b -> b) -> [a] -> b --              Combine          End Separate Items interwhatsit _ e _ []     = e interwhatsit c e s (x:xs) = loop xs x    where loop []     x = c x e          loop (y:ys) x = c x \$ s \$ loop ys y Do we want to have the special cases without the general case? (There has to be a better name than interwhatsit.  sepfoldr?) > > The rationale for this proposal is that I find myself implementing > this function again and again in every project I write Why would you do that rather than putting it in a utilities module and just reusing that module? > , and it is highly generic. That sounds as though you have some other use in mind for these functions than pasting strings together.  I'd love to know what. I've been using Haskell since before it had intersperse and intercalate, and can't remember ever using them. _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 In reply to this post by Siraaj Khandkar-3 On 3/03/16 6:02 am, Siraaj Khandkar wrote: > Though I prefer the name "interleave" to "intersperse", since its > meaning is more-precise and closer to the intended behavior here. No, interleaving requires *two* sequences.  This is interleave: interleave([], []) -> []; interleave([X|Xs], [Y|Ys]) -> [X,Y|interleave(Xs,Ys)]. interleave, v, OED, sense 3: "Computing and Telecommunications. To interfile (two or more digitized signals or sequences of information) by alternating between them; to alternate (one such signal or sequence) with others. Also, to divide (memory, etc.) between a number of different tasks by allocating successive segments to each in turn." (I must admit I hadn't encountered the word 'interfile' before.) _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 On 3/2/16 7:41 PM, Richard A. O'Keefe wrote: > > > On 3/03/16 6:02 am, Siraaj Khandkar wrote: >> Though I prefer the name "interleave" to "intersperse", since its >> meaning is more-precise and closer to the intended behavior here. > > No, interleaving requires *two* sequences.  This is interleave: > > interleave([], []) -> []; > interleave([X|Xs], [Y|Ys]) -> [X,Y|interleave(Xs,Ys)]. > > interleave, v, OED, sense 3: > "Computing and Telecommunications. To interfile (two or more > digitized signals or sequences of information) by alternating > between them; to alternate (one such signal or sequence) with > others. Also, to divide (memory, etc.) between a number of > different tasks by allocating successive segments to each in turn." > > (I must admit I hadn't encountered the word 'interfile' before.) The way I understand it is: - interleave: put 1 B between 2 As - intersperse: put M Bs between N As i.e. "intersperse" is more-general - interval length is unspecified. Is this wrong? _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 In reply to this post by Siraaj Khandkar-3 On 3/03/16 2:23 pm, Siraaj Khandkar wrote: > >> interleave, v, OED, sense 3: >> "Computing and Telecommunications. To interfile (two or more >> digitized signals or sequences of information) by alternating >> between them; to alternate (one such signal or sequence) with >> others. Also, to divide (memory, etc.) between a number of >> different tasks by allocating successive segments to each in turn." >> >> (I must admit I hadn't encountered the word 'interfile' before.) > > The way I understand it is: > > - interleave: put 1 B between 2 As [A1,B1,A2,B2,A3,B3] will do. [A1,B1,A2,B2,A3,B3,A4] will do. Using the *same* B every time is not interleaving. A simple example of interleaving: put the fingers of one hand between the fingers of the other. Please people, do NOT use the word "interleaving" to refer to what the proposed "intersperse" function does.  We need "interleaving" to refer to interleaving. > - intersperse: put M Bs between N As OED sense 1: "to scatter or sprinkle between or among other things; to place here and there in the course of something; to mingle dispersedly or at intervals." I don't think anyone looking at the OED would expect the Haskell function definition.  The only justification for adopting the names intersperse and intercalate is that those *are* the names other not-entirely-dissimilar programming languages are using. _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 In reply to this post by Michael Truog On 03/03/2016 02:25 AM, Michael Truog wrote: > The alternative > might be to use http://erlang.org/doc/man/xref.html as a command line > tool, if the checking > needs to only consider functions and not types.  I am not aware of a > standalone (escript) > to run xref, but that may be useful for normal Erlang development (aside > from rebar or > other build tool usage). https://github.com/inaka/xref_runner-- Loïc Hoguin http://ninenines.euAuthor of The Erlanger Playbook, A book about software development using Erlang _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions
Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

Open this post in threaded view
|

## Re: Proposal: add lists:intersperse/2 and lists:intercalate/2

 On Sat, Mar 5, 2016 at 4:04 AM Jesper Louis Andersen <[hidden email]> wrote:So to catch up:* 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 :)Yep, iolists is the application I have for this. * 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 naming.In looking over the naming conventions in Erlang, one might assume that the design pattern is for diversity rather than uniformity. That's a nice way to look at it I think.Personally I'd vote to converge on some standards rather than diverge - and taking cues from Erlang, and not the many many other languages, will help there. The clear analog is string:join. It's also short - saves valuable typing cycles! _______________________________________________ erlang-questions mailing list [hidden email] http://erlang.org/mailman/listinfo/erlang-questions