Prelude function suggestions

Graham Klyne GK at ninebynine.org
Wed Jul 28 11:11:26 EDT 2004


At 10:44 28/07/04 +0100, Keith Wansbrough wrote:
> > > >> {- | Split the list at the occurrences of a separator into sub-list.
> > > >>      This is a generalization of 'words'. -}
>
>There should be two versions of this, one which treats repeated delimiters 
>the same as single ones and ignores initial and final delimiters, and one 
>which treats repeated delimiters as creating an empty field.  Thus
>
>split  isSpace " foo  bar baz " = ["foo","bar","baz"]
>split' isSpace " foo  bar baz " = ["","foo","","bar","baz",""]

I'm not sure if I support your exact conclusion, but I think this raises an 
important consideration about exactly what split should be.  I just scanned 
through my codebase for similar functions, and found two versions:

[[
splitBy :: (a->Bool) -> [a] -> [[a]]
splitBy _ []  = []
splitBy p (s0:str) = let (s1,sr) = break p str in
     (s0:s1):splitBy p sr

t1 = splitBy (`elem` "/\\") "//foo\\bar/baz"
      == ["/","/foo","\\bar","/baz"]
]]
The separator is *not* removed

and

[[
breakAll :: (a -> Bool) -> [a] -> [[a]]
breakAll _ [] = []
breakAll p s  = let (h,s') = break p s
                     in h : breakAll p (drop 1 s')

t2 = breakAll (==',') "ab,cde,f,g,hij,,kl,,"
      == ["ab","cde","f","g","hij","","kl",""]
]]
Where the separator is removed.

I had requirements for both forms, but if it's not clear that one form is 
superior I'd be less inclined to try and "standardize" it at all.  And all 
this makes me realize that there are several other candidates such as 
additional variations suggested by Keith.

#g


------------
Graham Klyne
For email:
http://www.ninebynine.org/#Contact



More information about the Libraries mailing list