Proposal (Trac ticket #3671): Add takeRec, genericTakeRec and spanRec to Data.List

Philip K.F. Hölzenspies p.k.f.holzenspies at utwente.nl
Tue Nov 17 10:11:21 EST 2009


On Tue, 2009-11-17 at 14:35 +0000, Ian Lynagh wrote:
> > http://hackage.haskell.org/trac/ghc/ticket/3671
> 
> I don't think spanRec does what you want:
> 
>     take 10 $ spanRec (< 3) [1,2,3,4,5,6,1,2,3,4,5,6]
>     [[1,2],[],[],[],[],[],[],[],[],[]]
> 
> I have also defined your "takeRec" a number of times in the past, but I
> have called it something like "splitAts". I've also defined functions
> called "breaks". I think the 's' suffix is more consistent with "tails",
> "inits" etc.

Dear Ian, et al.

I posted the proposal too quickly. Two corrections and *still* missed
it. I should have dug up my PreludeEx from somewhere. You are right
about the mistake, though. Also, I also usually use the 's' suffix, but
thought it could possibly be considered too invasive in the namespace.
If no one objects to +s names, then I actually prefer it.

When also including spans antonym 'breaks', they can actually be defined
in mutually recursive fashion.

Thus, the new proposal:

splitAts :: Int -> [a] -> [[a]]
splitAts = genericSplitAts

genericSplitAts :: (Integral a) => a -> [b] -> [[b]]
genericSplitAts n _  | n <= 0 = []
genericSplitAts _ []          = []
genericSplitAts i xs = let (hs,ts) = genericSplitAt i xs in hs : genericSplitAts i ts

spans :: (a -> Bool) -> [a] -> [[a]]
spans _ [] = []
spans p xs = let (hs,ts) = span p xs in hs : breaks p ts

breaks :: (a -> Bool) -> [a] -> [[a]]
breaks _ [] = []
breaks p xs = let (hs,ts) = break p xs in hs : spans p ts


Am I chalking you up as a +1?

Regrads,
Philip





More information about the Libraries mailing list