balanced fold for Data.List
Brent Yorgey
byorgey at seas.upenn.edu
Fri Jun 28 23:44:49 CEST 2013
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
More information about the Libraries
mailing list