Why is there no "splitSeperator" function in Data.List

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Why is there no "splitSeperator" function in Data.List

Robert Clausecker
Is there any reason, that one can't find a function that splits a list
at a seperator in the standard library? I imagined something like this:


    splitSeperator :: Eq a => a -> [a] -> [[a]]

    splitSeperator ',' "foo,bar,baz"
      --> ["foo","bar","baz"]

Or something similar? This is needed so often, even if I can implement
it in one line, is there any reason why it's not in the libs?

Yours, Robert Clausecker

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

signature.asc (501 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no "splitSeperator" function in Data.List

Gwern Branwen
On Sat, Feb 12, 2011 at 11:00 AM, Robert Clausecker <[hidden email]> wrote:

> Is there any reason, that one can't find a function that splits a list
> at a seperator in the standard library? I imagined something like this:
>
>
>    splitSeperator :: Eq a => a -> [a] -> [[a]]
>
>    splitSeperator ',' "foo,bar,baz"
>      --> ["foo","bar","baz"]
>
> Or something similar? This is needed so often, even if I can implement
> it in one line, is there any reason why it's not in the libs?

See http://hackage.haskell.org/package/split

The reason it's not in Data.List is because there are a bazillion
different splits one might want (when I was pondering the issue before
Brent released it, I had collected something like 8 different proposed
splits), so no agreement could ever be reached.

--
gwern
http://www.gwern.net

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no "splitSeperator" function in Data.List

Iustin Pop
On Sat, Feb 12, 2011 at 11:21:37AM -0500, Gwern Branwen wrote:

> On Sat, Feb 12, 2011 at 11:00 AM, Robert Clausecker <[hidden email]> wrote:
> > Is there any reason, that one can't find a function that splits a list
> > at a seperator in the standard library? I imagined something like this:
> >
> >
> >    splitSeperator :: Eq a => a -> [a] -> [[a]]
> >
> >    splitSeperator ',' "foo,bar,baz"
> >      --> ["foo","bar","baz"]
> >
> > Or something similar? This is needed so often, even if I can implement
> > it in one line, is there any reason why it's not in the libs?
>
> See http://hackage.haskell.org/package/split
>
> The reason it's not in Data.List is because there are a bazillion
> different splits one might want (when I was pondering the issue before
> Brent released it, I had collected something like 8 different proposed
> splits), so no agreement could ever be reached.

It is curious though that the Python community managed to agree on a
single implementation and include that in the standard library… So it is
possible :)

I also needed a split function and ended up with coding one that behaves
like the Python one for my project.

regards,
iustin

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no "splitSeperator" function in Data.List

Lyndon Maydwell
Does the Python implementation operate on Strings, or all lists?

I think this could be quite important as many split implementations
take regular expressions as arguments. This could be quite challenging
for general lists.

That said, I would like to see some of these features in the split package.

On Sun, Feb 13, 2011 at 5:50 PM, Iustin Pop <[hidden email]> wrote:

> On Sat, Feb 12, 2011 at 11:21:37AM -0500, Gwern Branwen wrote:
>> On Sat, Feb 12, 2011 at 11:00 AM, Robert Clausecker <[hidden email]> wrote:
>> > Is there any reason, that one can't find a function that splits a list
>> > at a seperator in the standard library? I imagined something like this:
>> >
>> >
>> >    splitSeperator :: Eq a => a -> [a] -> [[a]]
>> >
>> >    splitSeperator ',' "foo,bar,baz"
>> >      --> ["foo","bar","baz"]
>> >
>> > Or something similar? This is needed so often, even if I can implement
>> > it in one line, is there any reason why it's not in the libs?
>>
>> See http://hackage.haskell.org/package/split
>>
>> The reason it's not in Data.List is because there are a bazillion
>> different splits one might want (when I was pondering the issue before
>> Brent released it, I had collected something like 8 different proposed
>> splits), so no agreement could ever be reached.
>
> It is curious though that the Python community managed to agree on a
> single implementation and include that in the standard library… So it is
> possible :)
>
> I also needed a split function and ended up with coding one that behaves
> like the Python one for my project.
>
> regards,
> iustin
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no "splitSeperator" function in Data.List

Henning Thielemann
In reply to this post by Iustin Pop

On Sun, 13 Feb 2011, Iustin Pop wrote:

> On Sat, Feb 12, 2011 at 11:21:37AM -0500, Gwern Branwen wrote:
>>
>> See http://hackage.haskell.org/package/split
>>
>> The reason it's not in Data.List is because there are a bazillion
>> different splits one might want (when I was pondering the issue before
>> Brent released it, I had collected something like 8 different proposed
>> splits), so no agreement could ever be reached.
>
> It is curious though that the Python community managed to agree on a
> single implementation and include that in the standard library… So it is
> possible :)
It was not the implementation, that was discussed in length, but it was
the question, what 'split' shall actually do.

If you are satisfied with a simple Haskell 98 implementation of a 'split'
operation you might like 'chop' in
   http://hackage.haskell.org/packages/archive/utility-ht/0.0.5.1/doc/html/Data-List-HT.html


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no "splitSeperator" function in Data.List

Iustin Pop
In reply to this post by Lyndon Maydwell
On Sun, Feb 13, 2011 at 06:01:01PM +0800, Lyndon Maydwell wrote:
> Does the Python implementation operate on Strings, or all lists?

Of course, just on strings.

> I think this could be quite important as many split implementations
> take regular expressions as arguments. This could be quite challenging
> for general lists.

Agreed. But (in Python at least), split via re and split via (static)
element are two separate functions, and split via element can be nicely
replicated in Haskell.

regards,
iustin

> That said, I would like to see some of these features in the split package.
>
> On Sun, Feb 13, 2011 at 5:50 PM, Iustin Pop <[hidden email]> wrote:
> > On Sat, Feb 12, 2011 at 11:21:37AM -0500, Gwern Branwen wrote:
> >> On Sat, Feb 12, 2011 at 11:00 AM, Robert Clausecker <[hidden email]> wrote:
> >> > Is there any reason, that one can't find a function that splits a list
> >> > at a seperator in the standard library? I imagined something like this:
> >> >
> >> >
> >> >    splitSeperator :: Eq a => a -> [a] -> [[a]]
> >> >
> >> >    splitSeperator ',' "foo,bar,baz"
> >> >      --> ["foo","bar","baz"]
> >> >
> >> > Or something similar? This is needed so often, even if I can implement
> >> > it in one line, is there any reason why it's not in the libs?
> >>
> >> See http://hackage.haskell.org/package/split
> >>
> >> The reason it's not in Data.List is because there are a bazillion
> >> different splits one might want (when I was pondering the issue before
> >> Brent released it, I had collected something like 8 different proposed
> >> splits), so no agreement could ever be reached.
> >
> > It is curious though that the Python community managed to agree on a
> > single implementation and include that in the standard library… So it is
> > possible :)
> >
> > I also needed a split function and ended up with coding one that behaves
> > like the Python one for my project.
> >
> > regards,
> > iustin
> >
> > _______________________________________________
> > Haskell-Cafe mailing list
> > [hidden email]
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no "splitSeperator" function in Data.List

Iustin Pop
In reply to this post by Henning Thielemann
On Sun, Feb 13, 2011 at 11:21:42AM +0100, Henning Thielemann wrote:

>
> On Sun, 13 Feb 2011, Iustin Pop wrote:
>
> >On Sat, Feb 12, 2011 at 11:21:37AM -0500, Gwern Branwen wrote:
> >>
> >>See http://hackage.haskell.org/package/split
> >>
> >>The reason it's not in Data.List is because there are a bazillion
> >>different splits one might want (when I was pondering the issue before
> >>Brent released it, I had collected something like 8 different proposed
> >>splits), so no agreement could ever be reached.
> >
> >It is curious though that the Python community managed to agree on a
> >single implementation and include that in the standard library… So it is
> >possible :)
>
> It was not the implementation, that was discussed in length, but it
> was the question, what 'split' shall actually do.

Doh, of course I meant they managed to agree on a single definition of
what split means. Sorry for bad wording.

> If you are satisfied with a simple Haskell 98 implementation of a
> 'split' operation you might like 'chop' in
>   http://hackage.haskell.org/packages/archive/utility-ht/0.0.5.1/doc/html/Data-List-HT.html

Probably, but when I can have my own version in ~4 lines of Haskell, I'd
rather not have another dependency (that might or might not be packaged
in my distro).

regards,
iustin

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no "splitSeperator" function in Data.List

Evan Laforge
In reply to this post by Iustin Pop
>> The reason it's not in Data.List is because there are a bazillion
>> different splits one might want (when I was pondering the issue before
>> Brent released it, I had collected something like 8 different proposed
>> splits), so no agreement could ever be reached.
>
> It is curious though that the Python community managed to agree on a
> single implementation and include that in the standard library… So it is
> possible :)

This is sometimes cited as the advantage of a benevolent
dictator-for-life.  I remember there was lots of argument when 'join'
was added as a string method (vs. should it be a list method).  In the
end, Guido decided on one and that's what went in.  Fortunately that
particular dilemma is one forced by single-dispatch OO and doesn't
apply to haskell :)

I also wrote simple 'split' and 'join' functions that behave like the
python ones.  I use them all the time.  It doesn't bother me that
there are lots of other possible implementations, the simple 'join ::
String -> [String] -> String' and 'split :: String -> String ->
[String]' versions work in enough cases.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no "splitSeperator" function in Data.List

Max Rabkin-2
On Mon, Feb 14, 2011 at 07:52, Evan Laforge <[hidden email]> wrote:
> the simple 'join ::
> String -> [String] -> String' and 'split :: String -> String ->
> [String]' versions work in enough cases.

BTW, this "join" is Data.List.intercalate.

--Max

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no "splitSeperator" function inData.List

Donn Cave-4
In reply to this post by Evan Laforge
> It is curious though that the Python community managed to agree on a
> single implementation and include that in the standard library

To me, it's more like 2 implementations, overloaded on the same
function name.

Python 2.6.2 (r262:71600, Aug 30 2009, 15:41:32)
[GCC 2.95.3-haiku-090629] on haiku1
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.split(' ho ho ')
['ho', 'ho']
>>> string.split(' ho ho ', ' ')
['', 'ho', 'ho', '']
>>>

I.e., let the separator parameter default (to whitespace), and you
get what we have with Prelude.words, but specify a split character
and you get a reversible split.  It wasn't a new idea, the Bourne
shell for example has a similar dual semantics depending on whether
the separator is white space or not.  Somehow doesn't seem right
for Haskell, though.

        Donn Cave

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no "splitSeperator" function in Data.List

Peter Simons
In reply to this post by Evan Laforge
Hi Evan,

 >>> The reason it's not in Data.List is because there are a bazillion
 >>> different splits one might want (when I was pondering the issue
 >>> before Brent released it, I had collected something like 8
 >>> different proposed splits), so no agreement could ever be reached.
 >>
 >> It is curious though that the Python community managed to agree on a
 >> single implementation and include that in the standard library… So
 >> it is possible :)
 >
 > This is sometimes cited as the advantage of a benevolent
 > dictator-for-life. I remember there was lots of argument when 'join'
 > was added as a string method (vs. should it be a list method). In the
 > end, Guido decided on one and that's what went in.

having a dictator is not a necessary prerequisite for the ability to
make decisions. It's quite possible to decide controversial matters
without a dictator -- say, by letting people vote.

Take care,
Peter


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no "splitSeperator" function inData.List

Iustin Pop-2
In reply to this post by Donn Cave-4
On Sun, Feb 13, 2011 at 11:33:16PM -0800, Donn Cave wrote:

> > It is curious though that the Python community managed to agree on a
> > single implementation and include that in the standard library
>
> To me, it's more like 2 implementations, overloaded on the same
> function name.
>
> Python 2.6.2 (r262:71600, Aug 30 2009, 15:41:32)
> [GCC 2.95.3-haiku-090629] on haiku1
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import string
> >>> string.split(' ho ho ')
> ['ho', 'ho']
> >>> string.split(' ho ho ', ' ')
> ['', 'ho', 'ho', '']
> >>>
>
> I.e., let the separator parameter default (to whitespace), and you
> get what we have with Prelude.words, but specify a split character
> and you get a reversible split.  It wasn't a new idea, the Bourne
> shell for example has a similar dual semantics depending on whether
> the separator is white space or not.  Somehow doesn't seem right
> for Haskell, though.

Agreed, but I don't think that lacking a generic split functionality (as
in reversible split) is also good.

iustin

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no "splitSeperator" function inData.List

Donn Cave-4
In reply to this post by Peter Simons
Quoth Peter Simons <[hidden email]>,
...
> having a dictator is not a necessary prerequisite for the ability to
> make decisions. It's quite possible to decide controversial matters
> without a dictator -- say, by letting people vote.

The problem might be slightly miscast here, as an inability to reach
a decision in the face of controversy, and overall I don't think you
could make much of a case that Python's development is noticeably
more decisive.  If you really have 8 candidates for the semantics
of separator-split, then even if everyone can bring themselves to
agree on one, it's still just one, out of 8 missing functions, and
it's fairly understandable that this might not be very appealing.

Python also
  - is different in that function semantics can be conveniently overloaded
    (I forgot the "count" parameter, I often want that one - split at the
    first (n - 1) locations and leave the remainder intact)
  - is different as a general matter of style and niche
  - doesn't have our division between String and ByteString
  - makes it harder and more expensive to implement string.split()

Anyway, before it gets to the point where the crowds are camping in
the city square and demanding a vote, it might be interesting to see
where the code comes down on the matter, so I looked at the hackage
source I already happen to have at hand, a measly 252 Haskell source
files.

I found one (1) separator-split implementation.  I was surprised
at so few, as I've done it myself several times, so maybe I happen
to have an unrepresentative sample?

        Donn

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe