
On Thu, 2009-11-19 at 13:28 +0200, Yitzchak Gale wrote:
Philip K.F. wrote:
runs :: (a -> a -> Bool) -> [a] -> [[a]] runs p xs = ...
which produces a list of runs, i.e. the first result is that prefix of xs, such that for all consecutive elements e_i, e_{i+1}, the property holds, i.e. p e_i e_{i+1} -->> True.
We already have something like that:
groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
The groupBy function compares the first element e_1 to all consecutive elements e_i until it finds one for which the predicate doesn't hold. The runs function compares *consecutive* elements e_i and e_{i+1}. By example:
runs (<) [1,2,3,4,3,4,5] [[1,2,3,4],[3,4,5]]
In fact, instead of spans and breaks, why not just use:
runs :: (a -> Bool) -> [a] -> [[a]] runs = groupBy . on (==)
groupBy . on (==) :: (a -> ()) -> [a] -> [[a]]
Then we have:
breaks p = runs p . dropWhile p spans p = runs p . dropWhile (not . p)
For spans and breaks there might be a composition of functions already in Data.List, but because of the above type check failure, it's not this. Also, runs is more general than spans and breaks and runs is actually more sorely missed. I may still be mistaken, but there are no functions in Data.List that allow predicates on *consecutive* list elements. Regards, Philip