Function composition in Haskell with tuple arguments [duplicate]

南楼画角 提交于 2019-12-05 01:13:25

What you're looking to do is to take a function that operates on curried arguments, h, and apply it to the result of f, which is a tuple. This process, turning a function of two arguments into a function that takes one argument that is a tuple, is called uncurrying. We have, from Data.Tuple:

curry :: ((a, b) -> c) -> a -> b -> c 
   -- curry converts an uncurried function to a curried function.

uncurry :: (a -> b -> c) -> (a, b) -> c
   -- uncurry converts a curried function to a function on pairs.

So now we can write:

f :: a -> (b,c)
f = undefined

h :: b -> c -> d
h = undefined

k :: a -> d
k = uncurry h . f

Another tricky way to think of this is via an applicative functor,

k = (h <$> fst <*> snd) . f

Idea from Conor McBride, who'd write it as: (|f fst snd|) . f I think.

What you want to do is uncurry h. This function takes a -> b -> c and converts it into (a, b) -> c.

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