group collects equal elements in sublists. Thus, unique can be implemented as:<br>
unique = map head . group<br>
i.e. taking the first of each group of equal elements.<br>
<br>
(Yet) another way of doing this, is a modified quicksort:<br>
<br>
qsort [] = []<br>
qsort (x:xs) = qsort (filter (&lt;x) xs) ++ [x] ++ qsort (filter (&gt;x) xs)<br><br>At each step, this will ignore all elements equal to the pivot value.<br><br><div><span class="gmail_quote">On 18/05/07, <b class="gmail_sendername">
Dan Weston</b> &lt;<a href="mailto:westondan@imageworks.com">westondan@imageworks.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
OK, I looked up &quot;group&quot; and didn&#39;t see any Ord constraint analog. I give<br>up. What is the common idiom? I was stupidly using nub even though I<br>already have an Ord constraint. I have rewritten it to:<br>
<br>unique :: Ord a =&gt; [a] -&gt; [a]<br>unique (x:y:z) = (if x &lt; y then (x:) else id) (unique (y:z))<br>unique xyz&nbsp;&nbsp;&nbsp;&nbsp; = xyz<br><br>uniqueSort = unique . sort<br><br>but I would much rather use the &quot;common idiom&quot; than this dreck.
<br><br>Help a poor guy out? :)<br><br>Dan<br><br>Paul Johnson wrote:<br>&gt; Andrew Coppin wrote:<br>&gt;&gt; It occurs to me that if you want a sorted list of only unique<br>&gt;&gt; elements, it would seem (to me) to be efficient to do the sorting and
<br>&gt;&gt; the uniquing at the same time. Does any library function do this? (I<br>&gt;&gt; imagine it wouldn&#39;t be hard to write it yourself...)<br>&gt; Yes, although it only works on instances of Ord then, because of the
<br>&gt; sorting.<br>&gt;<br>&gt; Its actually quite a common idiom, and worth figuring out for yourself.<br>&gt; Hint: look at &quot;group&quot;.<br>&gt;<br>&gt; Paul.<br>&gt; _______________________________________________
<br>&gt; Haskell-Cafe mailing list<br>&gt; <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>&gt; <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe">http://www.haskell.org/mailman/listinfo/haskell-cafe
</a><br>&gt;<br>&gt;<br><br><br>_______________________________________________<br>Haskell-Cafe mailing list<br><a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe">
http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></blockquote></div><br>