28 Jun 23:44 2013

## balanced fold for Data.List

Brent Yorgey <byorgey <at> seas.upenn.edu>

2013-06-28 21:44:49 GMT

2013-06-28 21:44:49 GMT

How would people feel about adding a function foldb :: (a -> a -> a) -> a -> [a] -> a to Data.List which performs a*balanced*fold of a list? Right now I have this function in the Diagrams.Util module of diagrams-lib but it seems generally useful. Here's a (strawman) implementation: foldb :: (a -> a -> a) -> a -> [a] -> a foldb _ z [] = z foldb f _ as = foldb' as where foldb' [x] = x foldb' xs = foldb' (go xs) go [] = [] go [x] = [x] go (x1:x2:xs) = f x1 x2 : go xs I could also stick it in a separate package but it seems silly to have a whole package for such a small function. Also, does this already exist anywhere? Can it be implemented more simply in terms of existing machinery? etc.? -Brent