Re: [Haskell-cafe] identity function

2010-12-20 Thread Edward Amsden
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-Maybe.html#v:maybe

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 asmith9...@gmail.com wrote:
 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

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] identity function

2010-12-20 Thread Sean Leather
 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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe