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 (<x) xs) ++ [x] ++ qsort (filter (>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> <<a href="mailto:westondan@imageworks.com">westondan@imageworks.com</a>> 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 "group" and didn'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 => [a] -> [a]<br>unique (x:y:z) = (if x < y then (x:) else id) (unique (y:z))<br>unique xyz = xyz<br><br>uniqueSort = unique . sort<br><br>but I would much rather use the "common idiom" than this dreck.
<br><br>Help a poor guy out? :)<br><br>Dan<br><br>Paul Johnson wrote:<br>> Andrew Coppin wrote:<br>>> It occurs to me that if you want a sorted list of only unique<br>>> elements, it would seem (to me) to be efficient to do the sorting and
<br>>> the uniquing at the same time. Does any library function do this? (I<br>>> imagine it wouldn't be hard to write it yourself...)<br>> Yes, although it only works on instances of Ord then, because of the
<br>> sorting.<br>><br>> Its actually quite a common idiom, and worth figuring out for yourself.<br>> Hint: look at "group".<br>><br>> Paul.<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>><br>><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>