Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

haskell

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

like image 884
גלעד ברקן Avatar asked Oct 08 '14 21:10

גלעד ברקן


3 Answers

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

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

so you can write

f >>= (,)
like image 98
dfeuer Avatar answered Oct 25 '22 15:10

dfeuer


import Control.Arrow
(g &&& f) v = (g v, f v)
-- ergo,
(id &&& f) v = (v, f v)
(f &&& id) v = (f v, v)
like image 23
chi Avatar answered Oct 25 '22 15:10

chi


How about using the Applicative instance for (->)

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

For example

liftA2 (,) id succ 5

>>> (5,6)
like image 43
danidiaz Avatar answered Oct 25 '22 16:10

danidiaz