Why don&#39;t you just swap the pattern match order?<div><br></div><div><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;; ">   remdups               :: (Eq a) =&gt; [a] -&gt; [a]<br>   remdups (x : xx : xs) =  if x == xx then remdups (x : xs) else x : remdups (xx : xs)<br>
</span><div class="gmail_quote"><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;; ">   remdups xs            = xs</span></div><div class="gmail_quote"><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;;"><br>
</span></div><div class="gmail_quote"><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;;">This should cover all cases no?</span></div><div class="gmail_quote"><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;;"><br>
</span></div><div class="gmail_quote"><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;;">Also I prefer guards, but I guess that is personal</span></div><div class="gmail_quote"><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;;"><br>
</span></div><div class="gmail_quote"><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;;"><span class="Apple-style-span" style="font-family: arial; "><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;; ">   remdups (x1:x2:xs) </span></span></span></div>
<div class="gmail_quote"><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;;"><span class="Apple-style-span" style="font-family: arial; "><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;; ">   | x1 == x2  =      remdups (x2 : xs) </span></span></span></div>
<div class="gmail_quote"><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;;"><span class="Apple-style-span" style="font-family: arial; "><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;; ">   | otherwise = x1 : remdups (x2 : xs)<br>
</span><div class="gmail_quote"><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;; ">   remdups xs  = xs</span></div><div><span class="Apple-style-span" style="font-family: &#39;Courier New&#39;;"><br>
</span></div></span></span></div><div class="gmail_quote">2009/3/15 R J <span dir="ltr">&lt;<a href="mailto:rj248842@hotmail.com">rj248842@hotmail.com</a>&gt;</span><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">




<div>
<font face="Courier New">I need to write an implementation using foldl, and a separate implementation using foldr, of a function, &quot;remdups xs&quot;, that removes adjacent duplicate items from the list xs.  For example, remdups [1,2,2,3,3,3,1,1]= [1,2,3,1].<br>
<br>My approach is first to write a direct recursion, as follows:<br><br>   remdups               :: (Eq a) =&gt; [a] -&gt; [a]<br>   remdups []            =  []<br>   remdups (x : [])      =  [x]<br>   remdups (x : xx : xs) =  if x == xx then remdups (x : xs) else x : remdups (xx : xs)<br>
<br>This code works, but it has three cases, not usual two, namely [] and (x : xs).<br><br>What, if any, is the implementation using only two cases?<br><br>Also, if three cases are required, then how can it be implemented using foldr, and how using foldl?<br>
<br>Thanks.<br></font><div class="hm"><br><hr>Express your personality in color! Preview and select themes for HotmailŽ.  <a href="http://www.windowslive-hotmail.com/LearnMore/personalize.aspx?ocid=TXT_MSGTX_WL_HM_express_032009#colortheme" target="_blank">See how.</a></div>
</div>
<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" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div>