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

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Thu Jul 13 05:16:43 EDT 2006


On 2006-07-12 at 23:24BST "Brian Hulley" wrote:
> 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.

Hooray!  I've been waiting to ask "Why aren't we asking what
laws hold for these operations?" but now you've saved me the
effort. I've been bitten by unlines . lines /= id already;
it's something we could gainfully change without wrecking
too much code, methinks.

> So I vote for the first option ie:
> 
>     splitBy (=='a') "aabbaca" == ["","","bb","c",""]

Seconded.  

As far as naming is concerned, since this is a declarative
language, surely we shouldn't be using active verbs like
this? (OK I lost that argument way back in the mists of
Haskell 0.0 with take. Before then I called "take" "first":
"first n some_list" reads perfectly well).

 Jón

-- 
Jón Fairbairn                              Jon.Fairbairn at cl.cam.ac.uk




More information about the Libraries mailing list