[Haskell-cafe] Map unionWith generalization

Hans Aberg haberg at math.su.se
Thu Jan 28 07:25:13 EST 2010

On 28 Jan 2010, at 03:09, Twan van Laarhoven wrote:

>> For example, in Map String Integer (sparse representation of  
>> monomials) compute the minimum value of all associative pairs with  
>> the same key (the gcd); if only one key is present, the absent  
>> should be treated as having value 0. So
>>  unionWith min xs ys
>> will not work, because unionWith will always apply the identity to  
>> the remaining value when one key is missing, whereas it should be  
>> sent to 0.
> If missing keys represent 0, then wouldn't this work?
>    intersectionWith min xs ys

Yes, that works for the gcd function using min. Thank you - this  
function is used a lot, so it is good to have it simple.

For the general problem, one still needs a better interface. For (-),  
though missing keys represent 0, if the first is missing, the second  
should be negated. And if both are present, one should filter out keys  
with 0 value. Right now, this is a combination of negate, (+), and  
filter (/= 0).


More information about the Haskell-Cafe mailing list