Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are two problems I think might be troubling you.</p> <p>First of all, loading a file with your definitions into ghci, I try</p> <pre><code>ghci&gt; csort (&gt;= x) [2,1,3] &lt;interactive&gt;:1:10: Not in scope: 'x' </code></pre> <p>The way you wrote it, <code>f</code> takes two parameters. And indeed <code>x</code> is not in scope here. So the custom filter function should be simply <code>(&gt;=)</code>.</p> <pre><code>ghci&gt; csort (&gt;=) [2,1,3] ***Exception: blahblah: Non-exhaustive patterns in function csort </code></pre> <p>Now the real problem: non-exhaustive patterns. What does this mean? You've written a definition of how to sort a list with at least one element. But how do you sort a list with no elements? Simple, you ignore the custom filter function, and simply produce an empty list. Since an empty list has no elements, it is already "sorted".</p> <pre><code>csort _ [] = [] </code></pre> <p>Once I added that line to the source file, it suddenly worked. The pattern <code>[]</code> compliments the pattern <code>(x:xs)</code>, and those two patterns, together, are exhaustive (a list is either empty, or it has at least one element).</p> <pre><code>ghci&gt; csort (&gt;=) [2,1,3] [1,2,3] ghci&gt; csort (&lt;) [2,1,3] [3,2,1] ghci&gt; quickCheck (\xs -&gt; csort (&lt;) xs == (reverse $ csort (&gt;) xs)) +++ OK, passed 100 tests. </code></pre> <hr> <p>Here's my sort.hs file:</p> <pre><code>csort _ [] = [] csort f (x:xs) = csort f lesser ++ [x] ++ csort f greater where lesser = [e | e &lt;- xs, f x e] greater = [e | e &lt;- xs, not $ f x e] </code></pre> <p>I have no idea why you would still have errors; this works perfectly well for me.</p>
 

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