[Haskell-cafe] Re: Why is there no splitBy in the list module?

Brian Hulley brianh at metamilk.com
Wed Jul 12 18:24:37 EDT 2006

Christian Maeder wrote:
> Donald Bruce Stewart schrieb:
>> Question over whether it should be:
>>     splitBy (=='a') "aabbaca" == ["","","bb","c",""]
>>   or
>>     splitBy (=='a') "aabbaca" == ["bb","c"]
>> I argue the second form is what people usually want.
> Yes, the second form is needed for "words", but the first form is
> needed for "lines", where one final empty element needs to be removed
> from your version!
> Prelude> lines "a\nb\n"
> ["a","b"]
> Prelude> lines "a\n\nb\n\n"
> ["a","","b",""]

Prelude.lines and Prelude.unlines treat '\n' as a terminator instead of a 
separator. I'd argue that this is poor design, since information is lost ie 
lines . unlines === id whereas unlines . lines =/= id whereas if '\n' had 
been properly conceived of as a separator, the identity would hold.

So I vote for the first option ie:

    splitBy (=='a') "aabbaca" == ["","","bb","c",""]

Regards, Brian.
Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.


More information about the Haskell-Cafe mailing list