Distributing monadic(?) functions across dyadic functions

Is there a common way (standard libs, higher order) to express the lambda part below? It's not particulary complicated but I think it is not higher-order enough
unionBy (\x y -> fst x == fst y) listOfPairs1 listOfPairs2
Something like "distribute fst (==)" where
distribute f op x y = f x `op` f y
would leave
unionBy (distribute fst (==)) listOfPairs1 listOfPairs2
I imagine something involving Arrows and/or zip/curry/uncurry but I just can't see it. Is this a case of trying to make something more complicated than it is? Jared. -- http://www.updike.org/~jared/ reverse ")-:"

Jared Updike writes:
Is there a common way (standard libs, higher order) to express the lambda part below? It's not particulary complicated but I think it is not higher-order enough
unionBy (\x y -> fst x == fst y) listOfPairs1 listOfPairs2
Something like "distribute fst (==)" where
distribute f op x y = f x `op` f y
would leave
unionBy (distribute fst (==)) listOfPairs1 listOfPairs2
I imagine something involving Arrows and/or zip/curry/uncurry but I just can't see it. Is this a case of trying to make something more complicated than it is?
If you look at it in terms of folds over pairs,
cata (&) (x,y) = x & y -- corresponds to uncurry
ana f g x = (f x, g x) -- corresponds to (&&&)
Then you can de-forest:
hylo (&) f g x = f x & g x
-- hylo (&) f g == cata (&) . ana f g
-- == uncurry (&) . f &&& g
--
-- cata (&) == hylo (&) fst snd
-- ana f g == hylo (,) f g
This seems remeniscent of pull-backs (or push-outs) in category theory,
but I don't know enough to say for certain.
--
David Menendez

On Monday 03 April 2006 08:09, David Menendez wrote:
If you look at it in terms of folds over pairs,
cata (&) (x,y) = x & y -- corresponds to uncurry ana f g x = (f x, g x) -- corresponds to (&&&)
Then you can de-forest:
hylo (&) f g x = f x & g x
-- hylo (&) f g == cata (&) . ana f g -- == uncurry (&) . f &&& g -- -- cata (&) == hylo (&) fst snd -- ana f g == hylo (,) f g
This seems remeniscent of pull-backs (or push-outs) in category theory, but I don't know enough to say for certain.
I especially like the above code after it has been automatically transformed to use the puppy operator by my email client. Actually, the more I look at it... maybe the new crop of haskell editors should define some 'haskicon' replacements. :) Daniel

At 11:58 AM -0700 4/2/06, Jared Updike wrote:
Is there a common way (standard libs, higher order) to express the lambda part below? It's not particulary complicated but I think it is not higher-order enough
unionBy (\x y -> fst x == fst y) listOfPairs1 listOfPairs2
Something like "distribute fst (==)" where
distribute f op x y = f x `op` f y
would leave
unionBy (distribute fst (==)) listOfPairs1 listOfPairs2
I imagine something involving Arrows and/or zip/curry/uncurry but I just can't see it. Is this a case of trying to make something more complicated than it is?
Jared.
I think you've reached sufficient higher-order-ness. Others on the list have offered your function in a slightly different form:
f `on` g = \x y -> g x `f` g y unionBy ((==) `on` fst) listOfPairs1 listOfPairs2

On Sun, 02 Apr 2006, "Jared Updike"
Something like "distribute fst (==)" where
distribute f op x y = f x `op` f y
A function like this has been suggested for the standard libraries a couple of times before. Someone suggested the name "on", which I quite like: (*) `on` f = \x y -> f x * f y
unionBy (distribute fst (==)) listOfPairs1 listOfPairs2
unionBy ((==) `on` fst) xs ys I think on makes the code rather readable: union by equality on first. -- /NAD
participants (5)
-
Daniel McAllansmith
-
David Menendez
-
Dean Herington
-
Jared Updike
-
Nils Anders Danielsson