
On Saturday 06 December 2008 22:07:51 Paul Johnson wrote:
So we could have
fromListWithZero :: Ord k => (a -> b -> b) -> b -> [(k, a)] -> Map k b fromListWithZero combiner zero pairs = ...
The first time a key is seen the combining function is called with "zero" as its second argument. E.g.
fromListWithZero (:) [] xs
Or is that too much trouble?
It could be made a bit more general and efficient: Every entry x that did not have to be combined with another will end up as an application (combiner zero x). Thus, you could also write a function fromListWithUnit :: Ord k => (a -> b -> b) -> (a -> b) -> [(k, a)] -> Map k b fromListWithUnit combiner unit pairs = ... and define fromListWithZero via fromListWithZero c z ps = fromListWithUnit c (c z) ps But there is fromListWithUnit c u = fromListWith c . map (\p ->(fst p, u (snd p))) So, you already have what you want. Regards, Holger