<br><br><div class="gmail_quote">On Wed, Apr 1, 2009 at 9:50 PM, Zachary Turner <span dir="ltr">&lt;<a href="mailto:divisortheory@gmail.com">divisortheory@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im"><br><br><div class="gmail_quote">On Wed, Apr 1, 2009 at 7:45 PM, Michael P Mossey <span dir="ltr">&lt;<a href="mailto:mpm@alumni.caltech.edu" target="_blank">mpm@alumni.caltech.edu</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
What if I have a list xs, and I want to remove one item and replace it with another? The item to remove can be detected by a predicate function. The order of the items doesn&#39;t matter. How about this:<br>
<br>
replaceItem :: (a -&gt; Bool) -&gt; a -&gt; [a] -&gt; [a]<br>
replaceItem p new xs = new : snd (partition p xs)<br>
<br>
This will actually replace all items that match the predicate with one copy of &#39;new&#39;. It will also prepend &#39;new&#39; even if no items match the predicate.<br>
<br>
For another challenge, can someone explain to me how to write this in point-free style?<br>
</blockquote></div><br></div>I used an intermediate helper function, but the replaceItem function is point free<br><br>choose :: (a -&gt; Bool) -&gt; (a -&gt; b) -&gt; (a -&gt; b) -&gt; a -&gt; b<br>choose pred yes _ val | (pred val) = yes val<br>

choose _ _ no val = no val<div class="im"><br><br>replaceItem :: (a -&gt; Bool) -&gt; a -&gt; [a] -&gt; [a]<br></div>replaceItem pred rep = map (choose pred id (const rep))<br>
</blockquote></div><br>Ok I looked at the OP again and apparently I didn&#39;t understand the wording of the question :P  I just thought it said to replace each element in the list that matched the predicate with the new item.<br>
<br>How about this?<br><br>replaceItem :: [a] -&gt; (a -&gt; Bool) -&gt; a -&gt; [a]<br>let replaceItem xs pred = (: filter (not.pred) xs)<br><br>Note that I changed the order of the arguments, the value now comes last.  But I think this still should be ok?<br>