Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Partial answer to the first part of your question:</p> <p>Passing in <code>intCMP</code> to <code>sort3</code> lets you control the way the sorting is done. Presumably, <code>sort3 intCMP [7,3,6]</code> will return <code>[3,6,7]</code>, whereas <code>sort3 intCMPRev [7,3,6]</code> will return <code>[7,6,3]</code>. You could even make your own weird sorting functions like first all the even numbers and then all the odd ones in descending order, like so:</p> <pre><code>intCMPWeird :: Int -&gt; Int -&gt; Ordering intCMPWeird a b | even a &amp;&amp; even b = intCMP a b -- even numbers compare as usual | odd a &amp;&amp; odd b = intCMPRev a b -- odd numbers compare in reverse order | even a &amp;&amp; odd b = LT -- evens are all 'smaller' than odds | odd a &amp;&amp; even b = GT -- odds are all 'greater' than evens </code></pre> <p>Using this, <code>sort3 intCMPWeird [7,3,6]</code> should give <code>[6,7,3]</code>.</p> <p>What happens during typechecking when you pass one of the <code>intCMP...</code> functions into <code>sort3</code> is (in a very small nutshell) that the compiler tries to match the type of <code>sort3</code>'s first argument <code>(a -&gt; a -&gt; Ordering)</code> with the type of the supplied value <code>(Int -&gt; Int -&gt; Ordering)</code> and succeeds in doing that, by making <code>a</code> equal to <code>Int</code>. Then it needs to check whether the constraint <code>Ord a</code> is satisfied for <code>Int</code>, which works! Finally the compiler can figure out that the type of <code>sort3 intCMP</code> is <code>[Int] -&gt; [Int]</code>. Similarly, <code>sort3 floatCMP</code> has type <code>[Float] -&gt; [Float]</code>.</p> <p>I'm not 100% sure why the <code>Ord</code> constraint is on the type of <code>sort3</code>, since you can get the needed information from the passed-in comparison function - are you sure you've typed that correctly?</p> <p>EDIT: Hint on how to use a where clause to get a readable definition, you still have to fill in some bits and add some more clauses:</p> <pre><code>sort3 cmp [x,y,z] | x &lt;= y &amp;&amp; somethingElse1 = listWithXyzInSomeOrder1 | x &lt;= z &amp;&amp; somethingElse2 = listWithXyzInSomeOrder2 where a &lt;= b = cmp a b /= GT </code></pre>
    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