如何将 &&& 与 -> Maybe a 一起使用

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

我有两个功能

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

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

然后它们被更改为 String -> Maybe Int

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

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

有没有pointfree方式获取函数

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

所以如果f1和f2都return就好了,f4也会return否则没什么

您可以使用liftA2获取免积分版本,但我不确定免积分版本是否值得:

λ> :t \f1 f2 -> uncurry (liftA2 (,)) . (f1 &&& f2)
\f1 f2 -> uncurry (liftA2 (,)) . (f1 &&& f2)
  :: Applicative f => (b1 -> f a) -> (b1 -> f b) -> b1 -> f (a, b)
import Control.Arrow
import Control.Applicative

h :: (Applicative f) => (a -> f b) -> (a -> f c) -> a -> f (b, c)
h = liftA2 (liftA2 (,))

等于h f g x = liftA2 (,) (f x) (g x)