Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function in Prelude to pair a value with that value applied to a function?

I am searching for a function which looks something similar to this:

withSelf :: (a -> b) -> a -> (a, b) withSelf f x = (x, f x)

I have searched with Hoogle for such a function; I searched for (a -> b) -> a -> (a, b) and a -> (a -> b) -> (a, b), neither of which were conclusive. The Hackage page on Data.Tuple doesn't have what I'm looking for either.

I'm aware that it's trivial to write, but I want to write idiomatic Haskell where possible, and avoid re-inventing the wheel.

like image 549
Tom Galvin Avatar asked Mar 29 '15 17:03

Tom Galvin


1 Answers

The section (id &&&) does what you want:

> import Control.Arrow
> :t (id &&&)
(id &&&) :: (a -> c') -> a -> (a, c')
> (id &&&) succ 4
(4,5)
like image 119
chi Avatar answered Sep 19 '22 15:09

chi