The problem with your second implementation is that elements which occur more than once will eventually be included, when the part of the list remaining only has one copy. For example:<br><br>unique2 [1,1,2,4,1]<br>= unique2 [1,2,4,1]
<br>= unique2 [2,4,1]<br>= 2 : unique2 [4,1]<br>= 2 : 4 : unique2 [1]<br>= 2 : 4 : 1 : unique2 []&nbsp;&nbsp; -- only a single 1 left, so it gets mistakenly included<br>= [2,4,1]<br><br>When you determine that a certain number should not be included in the output, you need to delete all remaining occurrences of it from the list, so it won&#39;t get included later.
<br><br>unique2 (x:xs)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|elemNum2 x xs == 1 = x:unique2 xs<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|otherwise = unique2 (deleteElt x xs)<br><br>I&#39;ll let you figure out how to implement the deleteElt function.<br><br>hope this is helpful!
<br>-Brent<br><br><div><span class="gmail_quote">On 7/10/07, <b class="gmail_sendername">Alexteslin</b> &lt;<a href="mailto:alexteslin@yahoo.co.uk">alexteslin@yahoo.co.uk</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;">
<br>Hi, i am a beginner to Haskell and i have a beginner&#39;s question to ask.<br><br>An exercise asks to define function unique :: [Int] -&gt; [Int], which outputs<br>a list with only elements that are unique to the input list (that appears no
<br>more than once).&nbsp;&nbsp;I defined a function with list comprehension which works<br>but trying to implement with pattern matching and primitive recursion with<br>lists and doesn&#39;t work.<br><br>unique :: [Int] -&gt; [Int]
<br>unique xs = [x | x &lt;- xs, elemNum2 x xs == 1]<br><br><br>elemNum2 :: Int -&gt; [Int] -&gt; Int<br>elemNum2 el xs = length [x| x &lt;- xs, x == el]<br><br>//This doesn&#39;t work, I know because the list shrinks and produces wrong
<br>result but can not get a right //thinking<br><br>unique2 :: [Int] -&gt; [Int]<br>unique2 [] = []<br>unique2 (x:xs)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|elemNum2 x xs == 1 = x:unique2 xs<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|otherwise = unique2 xs<br><br><br>Any help to a right direction would be very appreciated, thanks.
<br>--<br>View this message in context: <a href="http://www.nabble.com/function-unique-tf4058328.html#a11528933">http://www.nabble.com/function-unique-tf4058328.html#a11528933</a><br>Sent from the Haskell - Haskell-Cafe mailing list archive at 
<a href="http://Nabble.com">Nabble.com</a>.<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>