<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body><div>This is the wrong list. You probably meant to email haskell-cafe or perhaps libraries@haskell.org.</div><div><br></div><div><br></div><div><br></div><div id="composer_signature"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><div style="font-size:85%;color:#575757">David Feuer</div><div style="font-size:85%;color:#575757">Well-Typed, LLP</div></div><div><br></div><div style="font-size:100%;color:#000000"><!-- originalMessage --><div>-------- Original message --------</div><div>From: Harendra Kumar <harendra.kumar@gmail.com> </div><div>Date: 2/4/18 10:50 PM (GMT-05:00) </div><div>To: ghc-devs@haskell.org </div><div>Subject: rolling span and groupBy for lists </div><div><br></div></div>Hi,<br><br>For a small problem, I was looking for a groupBy like function that groups<br>based on a predicate on successive elements but I could not find one. I<br>wrote these little functions for that purpose:<br><br>-- | Like span, but with a predicate that compares two successive elements.<br>The<br>-- span ends when the two successive elements do not satisfy the predicate.<br>rollingSpan :: (a -> a -> Bool) -> [a] -> ([a], [a])<br>rollingSpan _ xs@[] = (xs, xs)<br>rollingSpan _ xs@[_] = (xs, [])<br>rollingSpan p (x1:xs@(x2:_))<br> | p x1 x2 =<br> let (ys, zs) = rollingSpan p xs<br> in (x1 : ys, zs)<br> | otherwise = ([x1], xs)<br><br>-- | Like 'groupBy' but with a predicate that compares two successive<br>elements.<br>-- A group ends when two successive elements do not satisfy the predicate.<br>rollingGroupBy :: (a -> a -> Bool) -> [a] -> [[a]]<br>rollingGroupBy _ [] = []<br>rollingGroupBy cmp xs =<br> let (ys, zs) = rollingSpan cmp xs<br> in ys : rollingGroupBy cmp zs<br><br>Are there any existing functions that serve this purpose or is there any<br>simpler way to achieve such functionality? If not, where is the right place<br>for these, if any. Can they be included in Data.List in base?<br><br>Thanks,<br>Harendra<br></body></html>