Apply function and wrap tuple in Haskell
I'm looking for a way to write a function that applies the provided function and wraps the argument and result of the function inside a tuple. For example:
applyZip :: (a -> b) -> a -> (a,b)
applyZip f x = (x, f x)
Is there a more idiomatic way to write this in Haskell, preferably with library code?
Edit : I was just sincerely interested in other approaches to solving this. If you find yourself in the same problem, it is preferable to implement a function with a descriptive name.
+3
source to share
1 answer
You can use:
import Control.Monad(ap)
applyZip :: (a -> b) -> a -> (a,b)
applyZip = ap (,)
Here we are working with a function ap :: Monad m => m (a -> b) -> m a -> m b
.
+7
source to share