<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: fast string appending/concatenation in haskell</title>
	<atom:link href="http://tehgeekmeister.wordpress.com/2008/12/22/fast-string-appendingconcatenation-in-haskell/feed/" rel="self" type="application/rss+xml" />
	<link>http://tehgeekmeister.wordpress.com/2008/12/22/fast-string-appendingconcatenation-in-haskell/</link>
	<description>ramblings of a zen buddhist geek.</description>
	<lastBuildDate>Fri, 14 Aug 2009 03:11:53 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: josh</title>
		<link>http://tehgeekmeister.wordpress.com/2008/12/22/fast-string-appendingconcatenation-in-haskell/#comment-54</link>
		<dc:creator>josh</dc:creator>
		<pubDate>Tue, 23 Dec 2008 23:26:25 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/?p=22#comment-54</guid>
		<description>Note that showString *is* ++.  http://haskell.org/onlinereport/standard-prelude.html#$vshowString

Your second definition is &quot;newContent = (pageContent page) . (str ++)&quot;, which is better for the reasons that Eugene Kirpichov notes.</description>
		<content:encoded><![CDATA[<p>Note that showString *is* ++.  <a href="http://haskell.org/onlinereport/standard-prelude.html#$vshowString" rel="nofollow">http://haskell.org/onlinereport/standard-prelude.html#$vshowString</a></p>
<p>Your second definition is &#8220;newContent = (pageContent page) . (str ++)&#8221;, which is better for the reasons that Eugene Kirpichov notes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eugene Kirpichov</title>
		<link>http://tehgeekmeister.wordpress.com/2008/12/22/fast-string-appendingconcatenation-in-haskell/#comment-53</link>
		<dc:creator>Eugene Kirpichov</dc:creator>
		<pubDate>Tue, 23 Dec 2008 19:37:06 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/?p=22#comment-53</guid>
		<description>You should avoid intensive string concatenation where possible in *all* languages. Read the article http://en.wikipedia.org/wiki/Schlemiel_the_painter%27s_Algorithm .

By the way, in Haskell string concatenation is less of a performance hog because of its laziness: for example, completely evaluating s1 ++ s2 will only take as much time and memory as needed to scan s1 and then scan s2.

Consider the implementation of (++):

[] ++ bs = bs
(a:as) ++ bs = a:(as++bs)

So, to know the next element of (as++bs), you have to check whether &#039;as&#039; is empty.

However, for ((s1 ++ s2) ++ s3) ++ s4, to know its very first element, you have to check whether (s1++s2)++s3 is empty, for that you have to check whether (s1++s2) is empty, and for that you have to check whether s1 is empty. And these 4 operations continue to happen until s1 ends; after that you have 3 operations until s2 ends etc.

So, fully evaluating the contatenation of N strings of lengths L1,L2,..,LN in a *LEFT*-fold fashion takes N*L1 + (N-1)*L2 + ... steps.

Note that if you concatenate from the *RIGHT*, you will have absolutely no problems: completely evaluating s1 ++ (s2 ++ (s3 ++ s4)) takes only as much time as needed to scan s1, then scan s2, then s3, then s4.</description>
		<content:encoded><![CDATA[<p>You should avoid intensive string concatenation where possible in *all* languages. Read the article <a href="http://en.wikipedia.org/wiki/Schlemiel_the_painter%27s_Algorithm" rel="nofollow">http://en.wikipedia.org/wiki/Schlemiel_the_painter%27s_Algorithm</a> .</p>
<p>By the way, in Haskell string concatenation is less of a performance hog because of its laziness: for example, completely evaluating s1 ++ s2 will only take as much time and memory as needed to scan s1 and then scan s2.</p>
<p>Consider the implementation of (++):</p>
<p>[] ++ bs = bs<br />
(a:as) ++ bs = a:(as++bs)</p>
<p>So, to know the next element of (as++bs), you have to check whether &#8216;as&#8217; is empty.</p>
<p>However, for ((s1 ++ s2) ++ s3) ++ s4, to know its very first element, you have to check whether (s1++s2)++s3 is empty, for that you have to check whether (s1++s2) is empty, and for that you have to check whether s1 is empty. And these 4 operations continue to happen until s1 ends; after that you have 3 operations until s2 ends etc.</p>
<p>So, fully evaluating the contatenation of N strings of lengths L1,L2,..,LN in a *LEFT*-fold fashion takes N*L1 + (N-1)*L2 + &#8230; steps.</p>
<p>Note that if you concatenate from the *RIGHT*, you will have absolutely no problems: completely evaluating s1 ++ (s2 ++ (s3 ++ s4)) takes only as much time as needed to scan s1, then scan s2, then s3, then s4.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josef Svenningsson</title>
		<link>http://tehgeekmeister.wordpress.com/2008/12/22/fast-string-appendingconcatenation-in-haskell/#comment-52</link>
		<dc:creator>Josef Svenningsson</dc:creator>
		<pubDate>Tue, 23 Dec 2008 12:12:33 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/?p=22#comment-52</guid>
		<description>Another option would be to use Data.Sequence. But maybe that wasn&#039;t an option for you if you used libraries which used lists?</description>
		<content:encoded><![CDATA[<p>Another option would be to use Data.Sequence. But maybe that wasn&#8217;t an option for you if you used libraries which used lists?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Felipe</title>
		<link>http://tehgeekmeister.wordpress.com/2008/12/22/fast-string-appendingconcatenation-in-haskell/#comment-51</link>
		<dc:creator>Felipe</dc:creator>
		<pubDate>Tue, 23 Dec 2008 12:10:10 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/?p=22#comment-51</guid>
		<description>Nasty? No. All languages have the same problem, c&#039;mon. Never saw

    &quot;&quot;.join(strings)

in Python code, for example?</description>
		<content:encoded><![CDATA[<p>Nasty? No. All languages have the same problem, c&#8217;mon. Never saw</p>
<p>    &#8220;&#8221;.join(strings)</p>
<p>in Python code, for example?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tehgeekmeister</title>
		<link>http://tehgeekmeister.wordpress.com/2008/12/22/fast-string-appendingconcatenation-in-haskell/#comment-50</link>
		<dc:creator>tehgeekmeister</dc:creator>
		<pubDate>Tue, 23 Dec 2008 07:40:26 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/?p=22#comment-50</guid>
		<description>James: yeah, it is kinda nasty.  but we get lots of other cool stuff from the nature of our list type, and i think it&#039;s a good trade.  this is really why i made the post, so others could see how to solve the problem if they run into it; when i was trying to solve it i had to talk to people on #haskell, which, while a great resource, shouldn&#039;t be necessary for something that&#039;s so common as this.</description>
		<content:encoded><![CDATA[<p>James: yeah, it is kinda nasty.  but we get lots of other cool stuff from the nature of our list type, and i think it&#8217;s a good trade.  this is really why i made the post, so others could see how to solve the problem if they run into it; when i was trying to solve it i had to talk to people on #haskell, which, while a great resource, shouldn&#8217;t be necessary for something that&#8217;s so common as this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James William Pye</title>
		<link>http://tehgeekmeister.wordpress.com/2008/12/22/fast-string-appendingconcatenation-in-haskell/#comment-49</link>
		<dc:creator>James William Pye</dc:creator>
		<pubDate>Tue, 23 Dec 2008 07:22:59 +0000</pubDate>
		<guid isPermaLink="false">http://tehgeekmeister.wordpress.com/?p=22#comment-49</guid>
		<description>This seems like a fairly nasty gotcha to me. The kind that could impress a naive or simply impatient user with a negative view of haskell&#039;s performance. =\</description>
		<content:encoded><![CDATA[<p>This seems like a fairly nasty gotcha to me. The kind that could impress a naive or simply impatient user with a negative view of haskell&#8217;s performance. =\</p>
]]></content:encoded>
	</item>
</channel>
</rss>
