Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One of the benefits I get out of making my functions as general as possible is that it often allows for use cases I didn't think of before. When you start making things general, you start coming up with weird uses for your functions.</p> <p>You go, "But what if this value is a function?" Or "What if I allow this for any functor – what would be the result for other functors? Is it useful?" As it turns out, it <em>is</em> in a lot of cases! And this is one of the reasons Haskell is so great, in my opinion. It is easy to "accidentally" create very reusable functions.</p> <p>In other languages, you design functions for specific purposes and use them that way. In Haskell, you design functions for specific purposes and give them a general type signature, and suddenly they work for a ton of cases you didn't design them for.</p> <hr> <p>@jozefg made a great point about the generality restricting the possible implementations. I just want to give that more of a spotlight, because it is actually a very powerful concept. With a few general function, you can actually be completely sure what the function does only based on the type signature, because there's only one possible implementation for that general signature.</p> <p>I recently encountered the signature</p> <pre><code>mystery :: (a -&gt; b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c </code></pre> <p>which is interesting because we can actually figure out which function it is based on what it does. We have</p> <pre><code>mystery :: (a -&gt; b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c mystery f g x = ... </code></pre> <p>and we need it to return a <code>c</code> value of some kind. The only way we can get a value of type <code>c</code> is by applying the function <code>f</code> to an <code>a</code> value and a <code>b</code> value. We already have a single <code>a</code> value – this is the <code>x</code> argument. So we can partially apply <code>f x :: b -&gt; c</code> but we still need a <code>b</code> value to get the <code>c</code> value we want.</p> <p>The solution, of course, is to apply <code>g x</code> to get a <code>b</code> value, which we then can stick into <code>f</code>, thus finally returning a <code>c</code> value. The description of this is a little complicated to follow, but if you work it out in your head you arrive at</p> <pre><code>mystery f g x = f x (g x) </code></pre> <p>which performs the same thing as the library function <code>ap</code>. (From <code>Control.Applicative</code>.)</p> <p>It is <em>very</em> cool that you can figure out what a function does solely based on its type signature!</p>
    singulars
    1. This table or related slice is empty.
    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.
    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