<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Replace in Haskell</title>
	<atom:link href="http://bluebones.net/2007/01/replace-in-haskell/feed/" rel="self" type="application/rss+xml" />
	<link>http://bluebones.net/2007/01/replace-in-haskell/</link>
	<description>Adventures in Computer Programming</description>
	<pubDate>Fri, 21 Nov 2008 21:10:09 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: Anonymous</title>
		<link>http://bluebones.net/2007/01/replace-in-haskell/#comment-24262</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 05 Aug 2008 06:37:37 +0000</pubDate>
		<guid isPermaLink="false">http://bluebones.net/2007/01/replace-in-haskell/#comment-24262</guid>
		<description>legend!</description>
		<content:encoded><![CDATA[<p>legend!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joseph</title>
		<link>http://bluebones.net/2007/01/replace-in-haskell/#comment-13721</link>
		<dc:creator>Joseph</dc:creator>
		<pubDate>Sat, 24 Nov 2007 16:13:50 +0000</pubDate>
		<guid isPermaLink="false">http://bluebones.net/2007/01/replace-in-haskell/#comment-13721</guid>
		<description>The first function here is missing a check that can cause infinite recursion if an empty `find` list is given

&lt;pre&gt;
 replace "abc" "" "A"
&lt;/pre&gt;

it needs something like...

&lt;pre&gt;
replace _ [] list = list
&lt;/pre&gt;

The second function given only replaces the first instance of the string, and if the `find` list is empty then it is equivalent to 

&lt;pre&gt;
&#62; to:xs
&lt;/pre&gt;

the fourth line could be changed to...

&lt;pre&gt;
then to ++ subst from to (drop (length from) xs)
&lt;/pre&gt;

but then it has the same problem as the first function, and needs another check

&lt;pre&gt;
substr [] _ list = list -- or something like that
&lt;/pre&gt;

I chose to write my replace as follows

&lt;pre&gt;
replace::(Eq a) =&#62; [a] -&#62; [a] -&#62; [a] -&#62; [a]
replace [] newSub list = join newSub list
replace oldSub newSub list = _replace list where
	_replace list@(h:ts) = if isPrefixOf oldSub list
		then newSub ++ _replace (drop len list)
		else h : _replace ts
	_replace [] = []
	len = length oldSub
&lt;/pre&gt;

 I decided that an empty match list would mean inserting the new element between all of the elements in the list, but maybe the new element should be inserted on the beginning of the list as well.

&lt;pre&gt;
join::[a] -&#62; [a] -&#62; [a]
join glue [h] = [h]
join glue (h:ts) = h : glue ++ join glue ts
join _ [] = []
&lt;/pre&gt;

another function that works nicely with replace

&lt;pre&gt;
strip::(Eq a) =&#62; [a] -&#62; [a] -&#62; [a]
strip old = replace old []
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>The first function here is missing a check that can cause infinite recursion if an empty `find` list is given</p>
<pre>
 replace "abc" "" "A"
</pre>
<p>it needs something like&#8230;</p>
<pre>
replace _ [] list = list
</pre>
<p>The second function given only replaces the first instance of the string, and if the `find` list is empty then it is equivalent to </p>
<pre>
&gt; to:xs
</pre>
<p>the fourth line could be changed to&#8230;</p>
<pre>
then to ++ subst from to (drop (length from) xs)
</pre>
<p>but then it has the same problem as the first function, and needs another check</p>
<pre>
substr [] _ list = list -- or something like that
</pre>
<p>I chose to write my replace as follows</p>
<pre>
replace::(Eq a) =&gt; [a] -&gt; [a] -&gt; [a] -&gt; [a]
replace [] newSub list = join newSub list
replace oldSub newSub list = _replace list where
	_replace list@(h:ts) = if isPrefixOf oldSub list
		then newSub ++ _replace (drop len list)
		else h : _replace ts
	_replace [] = []
	len = length oldSub
</pre>
<p> I decided that an empty match list would mean inserting the new element between all of the elements in the list, but maybe the new element should be inserted on the beginning of the list as well.</p>
<pre>
join::[a] -&gt; [a] -&gt; [a]
join glue [h] = [h]
join glue (h:ts) = h : glue ++ join glue ts
join _ [] = []
</pre>
<p>another function that works nicely with replace</p>
<pre>
strip::(Eq a) =&gt; [a] -&gt; [a] -&gt; [a]
strip old = replace old []
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cale Gibbard</title>
		<link>http://bluebones.net/2007/01/replace-in-haskell/#comment-5241</link>
		<dc:creator>Cale Gibbard</dc:creator>
		<pubDate>Sat, 20 Jan 2007 22:52:24 +0000</pubDate>
		<guid isPermaLink="false">http://bluebones.net/2007/01/replace-in-haskell/#comment-5241</guid>
		<description>Yeah, the general rule is to always order parameters to functions in order of increasing likelihood of variation. In this case, for instance, it seems more likely that the same substitutions will be used on many different lists, rather than many different substitutions on the same initial list.</description>
		<content:encoded><![CDATA[<p>Yeah, the general rule is to always order parameters to functions in order of increasing likelihood of variation. In this case, for instance, it seems more likely that the same substitutions will be used on many different lists, rather than many different substitutions on the same initial list.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thomas David Baker</title>
		<link>http://bluebones.net/2007/01/replace-in-haskell/#comment-5110</link>
		<dc:creator>Thomas David Baker</dc:creator>
		<pubDate>Wed, 17 Jan 2007 20:19:59 +0000</pubDate>
		<guid isPermaLink="false">http://bluebones.net/2007/01/replace-in-haskell/#comment-5110</guid>
		<description>Thanks jethr0.  I see you've moved the list to act on to the last of the arguments.  Someone else on #haskell suggested that too, to improve composability.</description>
		<content:encoded><![CDATA[<p>Thanks jethr0.  I see you&#8217;ve moved the list to act on to the last of the arguments.  Someone else on #haskell suggested that too, to improve composability.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jethr0</title>
		<link>http://bluebones.net/2007/01/replace-in-haskell/#comment-5047</link>
		<dc:creator>jethr0</dc:creator>
		<pubDate>Tue, 16 Jan 2007 14:09:51 +0000</pubDate>
		<guid isPermaLink="false">http://bluebones.net/2007/01/replace-in-haskell/#comment-5047</guid>
		<description>sorry, too smart (sic!) for my own good. my "isPrefixOf" is broken, instead use "Data.List.isPrefixOf"</description>
		<content:encoded><![CDATA[<p>sorry, too smart (sic!) for my own good. my &#8220;isPrefixOf&#8221; is broken, instead use &#8220;Data.List.isPrefixOf&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jethr0</title>
		<link>http://bluebones.net/2007/01/replace-in-haskell/#comment-5046</link>
		<dc:creator>jethr0</dc:creator>
		<pubDate>Tue, 16 Jan 2007 13:51:27 +0000</pubDate>
		<guid isPermaLink="false">http://bluebones.net/2007/01/replace-in-haskell/#comment-5046</guid>
		<description>"head" and "tail" should be used sparingly, because "head" is a partially defined function (see "head []").

&lt;pre&gt;
subst _    _  [       ] = []
subst from to xs@(a:as) = 
    if isPrefixOf from xs 
        then to ++ drop (length from) xs
        else a : subst from to as
    where isPrefixOf as bs = and $ zipWith (==) as bs
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>&#8220;head&#8221; and &#8220;tail&#8221; should be used sparingly, because &#8220;head&#8221; is a partially defined function (see &#8220;head []&#8220;).</p>
<pre>
subst _    _  [       ] = []
subst from to xs@(a:as) =
    if isPrefixOf from xs
        then to ++ drop (length from) xs
        else a : subst from to as
    where isPrefixOf as bs = and $ zipWith (==) as bs
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
