问题
I had two functions
f1:: String -> Int
f2:: String -> Int
f3:: String -> (Int,Int)
f3 = f1 &&& f2
then they was changed to String -> Maybe Int
f1:: String -> Maybe Int
f2:: String -> Maybe Int
f3:: String -> (Maybe Int,Maybe Int)
Is there pointfree way to get function
f4:: String -> Maybe (Int, Int)
So if both f1 and f2 return Just, f4 will also return Just otherwise Nothing
回答1:
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)
回答2:
You can get pointfree version using liftA2
, but I'm not sure if the point-free version is worth the trouble:
λ> :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)
来源:https://stackoverflow.com/questions/28150825/how-can-i-use-with-a-maybe-a