How to get two values from one, point free, in Haskell?

杀马特。学长 韩版系。学妹 提交于 2020-01-03 10:44:55

问题


For example, given a value, v, and a function f, is there a way to get (f v,v) point free?


回答1:


Alternatively, note that for functions h and f with appropriate types,

h >>= f = \w -> f (h w) w

so you can write

f >>= (,)



回答2:


import Control.Arrow
(g &&& f) v = (g v, f v)
-- ergo,
(id &&& f) v = (v, f v)
(f &&& id) v = (f v, v)



回答3:


How about using the Applicative instance for (->)

liftA2 (,) id :: (a -> b) -> a -> (a, b)

For example

liftA2 (,) id succ 5

>>> (5,6)


来源:https://stackoverflow.com/questions/26266825/how-to-get-two-values-from-one-point-free-in-haskell

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