
I've been reviewing the library, and have come unstuck with the *id*function. What's its purpose and can someone give me an example of its practical use. -- Andrew

It does precisely what you'd think. It returns the value passed in.
It's mainly used in cases where a higher-order function expects a
function, but you don't want to modify anything. See for instance
http://www.haskell.org/ghc/docs/7.0-latest/html/libraries/base-4.3.0.0/Data-...
If I want to extract a value from a Maybe, but I don't particularly
care to apply a function to it, I can write (for instance)
maybe 0 id ma
Where 'ma' is my maybe value. (There's a library function fromMaybe,
but this should illustrate the idea.)
On Mon, Dec 20, 2010 at 5:14 PM, A Smith
I've been reviewing the library, and have come unstuck with the id function. What's its purpose and can someone give me an example of its practical use. -- Andrew
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- ------ Edward Amsden Undergraduate Computer Science Rochester Institute of Technology

I've been reviewing the library, and have come unstuck with the *id*function. What's its purpose and can someone give me an example of its practical use.
It's purpose is simply to be the identity function. The type says exactly what it does. Prelude> :t id id :: a -> a It's often useful in combination with higher-order functions. Prelude> :t foldr (.) id foldr (.) id :: [a -> a] -> a -> a Here's a slightly complicated way to add a list of numbers: Prelude> foldr (.) id (map (+) [1..5]) 0 15 Sean
participants (3)
-
A Smith
-
Edward Amsden
-
Sean Leather