Short write path (\ (x, y) & # 8594; (fx, gy))

For functions f :: a → b , g :: c → d , how to write lambda

 \(x, y) → (fx, gy) :: (a, b) → (c, d) 

more concise? I tried (f, g) , but - as expected, I think - without success.

+7
haskell tuples
source share
3 answers

The Bifunctor (,) instance is what you are looking for:

 instance Bifunctor (,) where bimap fg (a, b) = (fa, gb) 

bimap applies two functions to a tuple, one for each element.

 > import Data.Bifunctor > bimap (+1) (*5) (1,1) (2, 5) 

You might be wondering what the difference is between bimap and (***) .

 > :t bimap bimap :: Bifunctor p => (a -> b) -> (c -> d) -> pac -> pbd > :t (***) (***) :: Arrow a => abc -> ab' c' -> a (b, b') (c, c') 

With bimap you can restrict the type to tuples, rather than an arbitrary p bifunter, so if p ~ (,) the bimap type becomes

  (a -> b) -> (c -> d) -> (a, c) -> (b, d). 

With (***) you can restrict the type to functions, rather than the arbitrary arrow a , so that with a ~ (->) type (***) becomes

  (b -> c) -> (b' -> c') -> (b, b') -> (c, c') 

A close look reveals that two limited types are equivalent.

+16
source share

You can use (***) from Control.Arrow ie

 f *** g 
+9
source share

Try

 import Control.Arrow answer = f *** g $ (a, c) 

eg.

 import Control.Arrow f :: Int -> Int f = (+1) g :: Double -> String g = show answer = f *** g $ (10, 3.5) 
+7
source share

All Articles