<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vincent Gable's Blog &#187; Quicksort</title>
	<atom:link href="http://vgable.com/blog/tag/quicksort/feed/" rel="self" type="application/rss+xml" />
	<link>http://vgable.com/blog</link>
	<description>my weblog.</description>
	<lastBuildDate>Tue, 29 Nov 2011 22:20:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The Best Quicksort Ever</title>
		<link>http://vgable.com/blog/2009/05/31/the-best-quicksort-ever/</link>
		<comments>http://vgable.com/blog/2009/05/31/the-best-quicksort-ever/#comments</comments>
		<pubDate>Sun, 31 May 2009 06:59:02 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sample Code]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[College]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Quicksort]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2009/05/31/the-best-quicksort-ever/</guid>
		<description><![CDATA[The first time I saw this quicksort in Haskell was an eye opening moment, qsort [] = [] qsort (x:xs) = qsort (filter (&#60; x) xs) ++ [x] ++ qsort (filter (&#62;= x) xs) The first line reads: &#8220;When you sort an empty list ([]), the result is another empty list&#8221;. The second line reads: [...]]]></description>
			<content:encoded><![CDATA[<p>The first time I saw <a href="http://www.haskell.org/haskellwiki/Introduction#Ease_of_understanding">this quicksort in Haskell</a> was an eye opening moment,</p>
<blockquote>
<pre>
qsort []     = []
qsort (x:xs) = qsort (filter (&lt; x) xs) ++ [x] ++ qsort (filter (&gt;= x) xs)
</pre>
<p>The first line reads: &#8220;When you sort an empty list (<code>[]</code>), the result is another empty list&#8221;. The second line reads: &#8220;To sort a list whose first element is named <code>x</code> and the rest of which is named <code>xs</code>, sort the elements of <code>xs</code> that are less than <code>x</code>, sort the elements of <code>xs</code> that are greater than or equal to <code>x</code>, and concatenate (<code>++</code>) the results, with <code>x</code> sandwiched in the middle.&#8221;
</p></blockquote>
<p>The code is so concise, yet clear (even with cryptic variable names like <code>xs</code>). The day my professor wrote it on the whiteboard was the first time I internalized that there might be something good about the <em>alien</em> world of functional programming.</p>
<p>The biggest revelation to me was, <code>filter (&lt; x) xs</code> . It&#8217;s amazing that <code>(&lt; x)</code> builds a temporary, unnamed, function equivalent to C++,</p>
<pre>bool lessThanX(AnyOrderedType y){
    return y &lt; X;
}
//plus the definition of X somewhere...</pre>
<p><em>Building a function!</em> It&#8217;s still profound stuff to me. And <strong>clearly it really uses fewer lines of code</strong>. Amazingly, using <a href="http://en.wikipedia.org/wiki/List_comprehension">list comprehension</a> syntax is not more concise,</p>
<pre>qsort (x:xs) = qsort [i | i &lt;- xs, i &lt; x] ++ [x] ++ qsort [i | i &lt;- xs, i &lt;= x]</pre>
<p>compared to the original,</p>
<pre>qsort (x:xs) = qsort (filter (&lt; x) xs) ++ [x] ++ qsort (filter (&gt;= x) xs)</pre>
<p>I hope this is an <em>ah-ah</em> moment for someone else too. I&#8217;d love to know what made you interested in functional programming; or if you know of a more elegant quicksort implementation.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/05/31/the-best-quicksort-ever/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

