haskelloption-typearrow-abstraction

How can I use &&& with a -> Maybe a


I had two String -> Int functions:

f1 :: String -> Int
f2 :: String -> Int

f3 :: String -> (Int,Int)    
f3 = f1 &&& f2

Which were then changed to String -> Maybe Int

f1 :: String -> Maybe Int
f2 :: String -> Maybe Int

f3 :: String -> (Maybe Int,Maybe Int)    

Is there a pointfree way to get a function:

f4:: String -> Maybe (Int, Int)

Such that if both f1 and f2 return Just, f4 will also return Just, and otherwise Nothing.


Solution

  • import Control.Arrow
    import Control.Applicative
    
    h :: (Applicative f) => (a -> f b) -> (a -> f c) -> a -> f (b, c)
    h = liftA2 (liftA2 (,))
    

    which is equal to h f g x = liftA2 (,) (f x) (g x)