Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's useful as an argument to <a href="http://www.haskell.org/haskellwiki/Higher_order_function#Definition" rel="noreferrer">higher order functions</a> (functions which take functions as arguments), where you want some particular value left unchanged.</p> <p><strong>Example 1</strong>: Leave a value alone if it is in a Just, otherwise, return a default of 7.</p> <pre><code>Prelude Data.Maybe&gt; :t maybe maybe :: b -&gt; (a -&gt; b) -&gt; Maybe a -&gt; b Prelude Data.Maybe&gt; maybe 7 id (Just 2) 2 </code></pre> <p><strong>Example 2</strong>: building up a function via a fold:</p> <pre><code>Prelude Data.Maybe&gt; :t foldr (.) id [(+2), (*7)] :: (Num a) =&gt; a -&gt; a Prelude Data.Maybe&gt; let f = foldr (.) id [(+2), (*7)] Prelude Data.Maybe&gt; f 7 51 </code></pre> <p>We built a new function <code>f</code> by folding a list of functions together with <code>(.)</code>, using <code>id</code> as the base case.</p> <p><strong>Example 3</strong>: the base case for functions as monoids (simplified).</p> <pre><code>instance Monoid (a -&gt; a) where mempty = id f `mappend` g = (f . g) </code></pre> <p>Similar to our example with fold, functions can be treated as concatenable values, with <code>id</code> serving for the empty case, and <code>(.)</code> as append.</p> <p><strong>Example 4</strong>: a trivial hash function.</p> <pre><code>Data.HashTable&gt; h &lt;- new (==) id :: IO (HashTable Data.Int.Int32 Int) Data.HashTable&gt; insert h 7 2 Data.HashTable&gt; Data.HashTable.lookup h 7 Just 2 </code></pre> <p>Hashtables require a hashing function. But what if your key is already hashed? Then pass the id function, to fill in as your hashing method, with zero performance overhead.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload