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

旧街凉风 提交于 2019-12-10 17:34:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!