haskellhaskell-prelude

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.


Solution

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

    > import Control.Arrow
    > :t (id &&&)
    (id &&&) :: (a -> c') -> a -> (a, c')
    > (id &&&) succ 4
    (4,5)