I keep reusing lambda expressions such as
\x -> (f x, g x)
where I apply the same input to two functions and encapsulate the result in a pair. I can write a function capturing this
combine :: (a -> b) -> (a -> c) -> a -> (b,c)
combine f g x = (f x, g x)
Now the above lambda expression is just combine f g. I have two questions.
- I’m interested to know if there is a standard library function that does this that I just can’t find.
- Out of curiosity, I’d like to rewrite this function in point-free style, but I’m having a lot of trouble with it.
Control.Arrowhas the function(&&&)for this. It has a “more general” type, which unfortunately means that Hoogle doesn’t find it (maybe this should be considered a bug in Hoogle?).You can usually figure this sort of thing automatically with
pointfree, whichlambdabotin#haskellhas as a plugin.For example:
Where
liftM2with the(r ->)instance ofMonadhas type(a -> b -> c) -> (r -> a) -> (r -> b) -> r -> c. Of course, there are many other ways of writing this point-free, depending on what primitives you allow.