Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 677
ais Avatar asked Dec 24 '22 23:12

ais


2 Answers

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)

like image 144
user3237465 Avatar answered Jan 07 '23 17:01

user3237465


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)
like image 27
phadej Avatar answered Jan 07 '23 17:01

phadej