Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Christian Maeder <maeder <at> tzi.de>
Subject: Re: Why is there no splitBy in the list module?
Newsgroups: gmane.comp.lang.haskell.libraries
Date: Wednesday 12th July 2006 10:56:09 UTC (over 11 years ago)
Simon Marlow schrieb:
> Would someone like to make a concrete proposal (with code!) for 2-3
> functions we could reasonably add to Data.List?

Here is my proposal that is consistent with Data.PackedString and
"lines" (i.e a final delimiter is ignored -- by extra code)

{- | The 'splitWith' function takes a predicate and splits the input
list at each element which satisfies the predicate. -}
splitWith :: (a -> Bool) -> [a] -> [[a]]
splitWith p s =
    case s of
      [] -> []
      _ -> let (l, r) = break p s in
           case r of
             _ : [email protected](_ : _) -> l : splitWith p t
             _ -> [l]

{- | The 'split' function splits the input list on each occurrence of
the given element. -}
split :: Eq a => a -> [a] -> [[a]]
split c = splitWith (== c)
 
CD: 3ms