<?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/"
	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>As I understand it</title>
	<atom:link href="http://priyeshnarayanan.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://priyeshnarayanan.wordpress.com</link>
	<description>&#34;Until you stalk and overrun, you can&#039;t devour anyone.&#34; - Hobbes</description>
	<lastBuildDate>Fri, 02 Dec 2011 06:32:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='priyeshnarayanan.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>As I understand it</title>
		<link>http://priyeshnarayanan.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://priyeshnarayanan.wordpress.com/osd.xml" title="As I understand it" />
	<atom:link rel='hub' href='http://priyeshnarayanan.wordpress.com/?pushpress=hub'/>
		<item>
		<title>C++ Notes: Pointer Members vs. Default Constructor</title>
		<link>http://priyeshnarayanan.wordpress.com/2010/02/09/c-notes-pointer-members-vs-default-constructors/</link>
		<comments>http://priyeshnarayanan.wordpress.com/2010/02/09/c-notes-pointer-members-vs-default-constructors/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 08:38:15 +0000</pubDate>
		<dc:creator>Priyesh Narayanan</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ notes]]></category>
		<category><![CDATA[Constructors]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Smart Pointers]]></category>

		<guid isPermaLink="false">http://priyeshnarayanan.wordpress.com/?p=234</guid>
		<description><![CDATA[It appears that sometimes it is a good idea to keep pointer-to-object members rather than object members in a class. Three good occasions when we would want to do that are: When working with incomplete types. Classic example is when implementing the Pimpl idiom. When we want our class to be copyable and the member [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=234&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It appears that sometimes it is a good idea to keep <em>pointer-to-object</em> members rather than <em>object</em> members in a class. Three good occasions when we would want to do that are:</p>
<ol>
<li>When working with incomplete types. Classic example is when implementing the <a title="Gotw #24" href="http://gotw.ca/gotw/024.htm" target="_blank">Pimpl idiom</a>.</li>
<li>When we want our class to be copyable  and the member object is of a non-copyable class.</li>
<li>When making a member out of a class that does not have a default constructor.</li>
</ol>
<p>To elaborate on the second point, the copy construction and copy assignment of a class usually involves copying/assigning the parent classes and then the members. This is what the compiler would do with the implicit copy constructor and copy assignment operator. But even if we implement it ourselves, it should almost always be constructing/assigning parents and members, and in that order. If the members are of a non-copyable class, then this means that the class that we define also becomes non-copyable.</p>
<p>To elaborate on the third point, if we make use of an object member out of a class that does not have a default constructor, then, while constructing our class, we have to make sure that we call the appropriate constructor for the member with appropriate arguments. This can get a bit restrictive especially with templates:</p>
<p><pre class="brush: cpp;">
template &lt;typename T&gt;
class CustomClass1 {
private:
    T data_;
...
};
</pre></p>
<p><em>CustomClass1 </em>can only be specialized with types that have default constructors. Instead, <em>CustomClass2 </em>below can be specialized for any type:</p>
<p><pre class="brush: cpp;">
template &lt;typename T&gt;
class CustomClass2 {
private:
    T* pData_;
...
};
</pre></p>
<p>As an aside, two things to keep in mind when defining classes with pointer-to-object members:</p>
<ul>
<li> It is probably a good idea to make use of <em>boost::scoped_ptr</em> or <em>boost::shared_ptr</em> instead of raw pointers, so that deletion is handled automatically.</li>
<li>Always remember to provide one&#8217;s own declarations (with or without definitions) of the copy constructor and copy assignment operator for the class that has pointer-to-object members, as, the implicitly generated ones almost certainly will not be what we want (and can very likely be dangerous). Note that if we used a <em>scoped_ptr</em>, this is automatically taken care of, because, as indicated in point 2 above, since <em>scoped_ptr</em> is a non-copyable class, the enclosing class itself becomes non-copyable (unless you intentionally make it copyable and deal with non-copyable members somehow).</li>
</ul>
<p>Now, I guess the other way to look at point 3 is to always provide a default constructor for a class unless it absolutely does not make sense. It helps in many ways:</p>
<ol>
<li> It alleviates the restriction of having to keep pointer members (if this were the only reason, like in point 3 above).</li>
<li>You can create dynamic arrays as follows, only if there is a default constructor:</li>
<p><pre class="brush: cpp;">

CustomClass* dynamic_array = new CustomClass[20]; //only possible if CustomClass has a default constructor.

//For static arrays you could invoke parametrized constructors using the initialization syntax, provided there is an accessible copy constructor:
CustomClass static_array[20]; //only possible with default constructors.
CustomClass static_array_param[2] = { CustomClass(...), CustomClass(...) }; //parametrized constructors invoked.

//Similarly for container classes. Following vector works with parametrized constructors provided there is an accessible copy constructor (which is a must for elements in a vector anyway):
std::vector&lt;CustomClass&gt; v(10, CustomClass(...));

// But notice that, strictly speaking, you are creating temporary object(s) with the parametrized constructor and then copy-constructing the elements in the static array or vector using the temporaries.
</pre></ol>
<p>Interestingly, many standard library classes provide default constructors although it might appear to not make much sense.</p>
<p><pre class="brush: cpp;">
#include &lt;fstream&gt;

ofstream file1(&quot;/tmp/tempfile&quot;); //parametrized constructor makes sense.

//However, we can achieve the same effect with the following:
ofstream file2; //default constructor.
//Nothing constructive you can do with file2 until you call the open method:
file2.open(&quot;/tmp/tempfile&quot;);
...
</pre></p>
<p><strong>Updates:<br />
</strong>1. Corrections in the section about default constructors, with regard to static arrays and vectors.</p>
<p>2. Reference: <a href="http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.5">http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.5</a></p>
<p>3. Reference: <a href="http://www.acm.org/crossroads/xrds1-4/ovp.html">http://www.acm.org/crossroads/xrds1-4/ovp.html</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/priyeshnarayanan.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/priyeshnarayanan.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/priyeshnarayanan.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/priyeshnarayanan.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/priyeshnarayanan.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/priyeshnarayanan.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/priyeshnarayanan.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/priyeshnarayanan.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/priyeshnarayanan.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/priyeshnarayanan.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/priyeshnarayanan.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/priyeshnarayanan.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/priyeshnarayanan.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/priyeshnarayanan.wordpress.com/234/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=234&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://priyeshnarayanan.wordpress.com/2010/02/09/c-notes-pointer-members-vs-default-constructors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3e7c60801f473e3dd195ed0bdce66f7e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sifaka</media:title>
		</media:content>
	</item>
		<item>
		<title>A slip in swap()</title>
		<link>http://priyeshnarayanan.wordpress.com/2009/08/20/a-slip-in-swap/</link>
		<comments>http://priyeshnarayanan.wordpress.com/2009/08/20/a-slip-in-swap/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 13:46:42 +0000</pubDate>
		<dc:creator>Priyesh Narayanan</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://priyeshnarayanan.wordpress.com/?p=214</guid>
		<description><![CDATA[What&#8217;s the bug in the C++ function swap1() below, a function that intends to swap two integers&#8230; &#8230; that is not there in swap2() below? Learned it the hard way. My Quicksort program was misbehaving and it was a bit puzzling until I figured out that the bug was in my swap function. The bug [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=214&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>What&#8217;s the bug in the C++ function <strong>swap1()</strong> below, a function that intends to swap two integers&#8230;</p>
<p><pre class="brush: cpp;">
void swap1(int&amp; a, int&amp; b)
{
    a = a^b;
    b = a^b;
    a = a^b;
}
</pre></p>
<p>&#8230; that is not there in <strong>swap2()</strong> below?</p>
<p><pre class="brush: cpp;">
void swap2(int&amp; a, int&amp; b)
{
    int t = a;
    a = b;
    b = t;
}
</pre></p>
<p>Learned it the hard way. My Quicksort program was misbehaving and it was a bit puzzling until I figured out that the bug was in my swap function. The bug surfaces when you try to swap a variable with itself &#8211; you&#8217;d want it to remain unchanged, but swap1() will zero it out.</p>
<p>In a related note, Bjarne Stroustrup wrote (and I quote from <a href="http://www.artima.com/cppsource/top_cpp_books.html">here</a>):</p>
<p><pre class="brush: cpp;">
if (this == &amp;a) return;  		// beware of s=s;
</pre></p>
<p>:)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/priyeshnarayanan.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/priyeshnarayanan.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/priyeshnarayanan.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/priyeshnarayanan.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/priyeshnarayanan.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/priyeshnarayanan.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/priyeshnarayanan.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/priyeshnarayanan.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/priyeshnarayanan.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/priyeshnarayanan.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/priyeshnarayanan.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/priyeshnarayanan.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/priyeshnarayanan.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/priyeshnarayanan.wordpress.com/214/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=214&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://priyeshnarayanan.wordpress.com/2009/08/20/a-slip-in-swap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3e7c60801f473e3dd195ed0bdce66f7e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sifaka</media:title>
		</media:content>
	</item>
		<item>
		<title>No matter how well you think you know something&#8230;</title>
		<link>http://priyeshnarayanan.wordpress.com/2009/08/11/no-matter-how-well-you-think-you-know-something/</link>
		<comments>http://priyeshnarayanan.wordpress.com/2009/08/11/no-matter-how-well-you-think-you-know-something/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 13:56:57 +0000</pubDate>
		<dc:creator>Priyesh Narayanan</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[Computational Complexity]]></category>
		<category><![CDATA[dynamic programming]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://priyeshnarayanan.wordpress.com/?p=183</guid>
		<description><![CDATA[&#8230; nothing like getting it wrong to start wondering and appreciating the point. Recently, faced with a decision problem, I started off with an approach that used Dynamic Programming (DP, here onwards) &#8211; because it appeared deceivingly similar to those classic DP problems, you know, matrices and all, and I was a bit over-enthusiastic. The [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=183&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>&#8230; nothing like getting it wrong to start wondering and appreciating the point.</p>
<p>Recently, faced with a <a href="http://en.wikipedia.org/wiki/Decision_problem" target="_blank">decision problem</a>, I started off with an approach that used Dynamic Programming (DP, here onwards) &#8211; because it appeared deceivingly similar to those classic DP problems, you know, matrices and all, and I was a bit over-enthusiastic. The problem basically turned out to be the decision version of the <a href="http://en.wikipedia.org/wiki/Pathfinding" target="_blank">Pathfinding problem</a> &#8211; to determine whether there exists a path between given two nodes <span style="font-family:courier new;"> u,v </span>.</p>
<p>Although I switched to a &#8216;better&#8217; algorithm, which still solved the <a href="http://en.wikipedia.org/wiki/Optimization_problem" target="_blank">optimization problem</a> (<em>Dijkstra&#8217;s shortest path algorithm</em>) and therefore did feel like an overkill, I realized later that all I needed to do was determine whether <span style="font-family:courier new;">u</span> and <span style="font-family:courier new;">v</span> belonged to the same connected component of the graph, and thus a simple Depth First Search would have sufficed.</p>
<p>In retrospect, my first thought was that I had made the mistake of trying to solve a decision problem using dynamic programming, that I had overlooked the fact that DP should be applied only to optimization problems. But then it got me wondering &#8211; how come finding the n<sup>th</sup> Fibonacci number, which is not an optimization problem, is something you can solve effectively using DP? (On a side note, this also brought my attention to the type of <a href="http://en.wikipedia.org/wiki/Computational_problem" target="_blank">computational problems</a> &#8211; decision, search, optimization, counting, promise. I guess Fibonacci would be a search problem?)</p>
<p>So the whole thing got me asking myself two questions:</p>
<ol>
<li><strong>Do the problems for which DP is applicable, necessarily be</strong><strong> optimization problems?</strong><strong> </strong>The requirements for DP are that the problem exhibit optimal substructure and subproblem overlap. Fibonacci is big on overlap, and exhibits optimal substructure trivially (later Fibonacci numbers are based on previous ones). But does that make Fibonacci an optimization problem in some twisted way?</li>
<li><strong>Can a decision problem be converted to an optimization problem of equal hardness?</strong> Every optimization problem has an associated decision problem, which is usually easier, so I&#8217;m talking about whether there is one of equal hardness &#8211; meaning solving one is equivalent to solving the other). Or does it even not make sense? In my first approach of trying to solve the problem using DP, I was in some way trying to convert the decision problem into an optimization problem (marking distances as ∞ if no edge existed between two nodes and thus trying to see if my dynamic programming &#8216;equation&#8217; that tries to optimize something, would trivially avoid those edges and so on). Question is, could I have succeeded by taking that route at all?</li>
</ol>
<p>&nbsp;</p>
<div id="_mcePaste" style="overflow:hidden;position:absolute;left:-10000px;top:580px;width:1px;height:1px;">&#8220;&#8221;"Finds the sublist in a given list of integers that has the max sum<br />
among all such sublists. Uses dynamic programming to find the solution<br />
in O(n) time thus:<br />
v(i) = max( v(i-1) + a[i], a[i] ),<br />
where v(i) denotes the max sublist ending at i. So the max sublist&#8217;s<br />
sum would be the max of all v(i)s and the corresponding sublist will be<br />
returned.<br />
&#8220;&#8221;"<br />
def maxsublist(a):<br />
&#8220;&#8221;"Finds the sublist of maximum sum, in a given list,<br />
and returns a tuple (max_sum, max_sublist)<br />
&#8220;&#8221;"<br />
try:<br />
if len(a) == 0: raise Exception<br />
except Exception:<br />
print &#8220;List is empty!\n&#8221;<br />
else:<br />
l = 0 #left index.<br />
r = 0 #right index.<br />
v = a[0] #v stores the optimal for i, from i = 0 to n-1<br />
max = v<br />
for i in range(1, len(a)):<br />
v,lt,rt = v + a[i] &gt; a[i] and (v + a[i], l, i) or (a[i], i, i)<br />
if v &gt; max:<br />
max, l, r = (v, lt, rt)<br />
return (max, a[l:r+1]) #[l, r)#Unit tests:<br />
if __name__ == "__main__":<br />
li = [-1,4,-3,3,-2,4,-1]<br />
print li<br />
y = maxsublist(li)<br />
print y<br />
if y == (6, [4,-3,3,-2,4]):<br />
print &#8220;Pass&#8221;<br />
else:<br />
print &#8220;Fail&#8221;</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/priyeshnarayanan.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/priyeshnarayanan.wordpress.com/183/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/priyeshnarayanan.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/priyeshnarayanan.wordpress.com/183/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/priyeshnarayanan.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/priyeshnarayanan.wordpress.com/183/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/priyeshnarayanan.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/priyeshnarayanan.wordpress.com/183/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/priyeshnarayanan.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/priyeshnarayanan.wordpress.com/183/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/priyeshnarayanan.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/priyeshnarayanan.wordpress.com/183/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/priyeshnarayanan.wordpress.com/183/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/priyeshnarayanan.wordpress.com/183/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=183&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://priyeshnarayanan.wordpress.com/2009/08/11/no-matter-how-well-you-think-you-know-something/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3e7c60801f473e3dd195ed0bdce66f7e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sifaka</media:title>
		</media:content>
	</item>
		<item>
		<title>C++ Notes: Why have a method const?</title>
		<link>http://priyeshnarayanan.wordpress.com/2009/08/09/c-notes-why-const-methods/</link>
		<comments>http://priyeshnarayanan.wordpress.com/2009/08/09/c-notes-why-const-methods/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 07:33:46 +0000</pubDate>
		<dc:creator>Priyesh Narayanan</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ notes]]></category>
		<category><![CDATA[const]]></category>

		<guid isPermaLink="false">http://priyeshnarayanan.wordpress.com/?p=78</guid>
		<description><![CDATA[While reading Effective C++, one thing I figured out for good, although it&#8217;s never the point of any of Meyers&#8217; items, is the real purpose of the const keyword when applied to member methods. The way I had learned it (and I&#8217;m sure there are many others out there) is that you qualify a method [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=78&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While reading <a href="http://www.aristeia.com/books.html" target="_blank"><em>Effective C++</em></a>, one thing I figured out for good, although it&#8217;s never the point of any of Meyers&#8217; items, is the real purpose of the <span style="font-family:courier new;">const</span> keyword when applied to member methods.</p>
<p>The way I had learned it (and I&#8217;m sure there are many others out there) is that</p>
<blockquote><p>you qualify a method as <span style="font-family:courier new;"> const </span> if you don&#8217;t want the function to modify any members.</p></blockquote>
<p>And then they tell you about <span style="font-family:courier new;">mutable</span> member variables and how <span style="font-family:courier new;">const</span> methods can modify them.</p>
<p>For a language feature, this was not quite convincing or at least compelling enough for me. I mean, the use case was pretty much similar to declaring a local variable that is never passed around in function calls, a <span style="font-family:courier new;">const</span>. It just safeguards you from accidentally modifying the variable yourself and is therefore a <strong>may-have</strong>. Don&#8217;t get me wrong, I totally agree that this aspect is definitely good programming practice and even sometimes lets the compiler generate better optimized code. But come on, this is perhaps not why <span style="font-family:courier new;">const</span> was introduced in the first place (remember that <span style="font-family:courier new;">const</span> keyword is one of those things that originated in C++ and then went onto C).</p>
<p><span style="font-family:courier new;">const</span> really comes in handy when variables are passed around in functions and when APIs are defined. Like when you make the &#8216;source&#8217; argument of strncpy() a <span style="font-family:courier new;">const</span>,</p>
<ul>
<li>so the caller can pass in a source string that is <span style="font-family:courier new;">const</span>, and</li>
<li>so the caller is given a guarantee that the call is not going to modify its source.</li>
</ul>
<p>These, I believe, are the &#8216;real&#8217; uses of <span style="font-family:courier new;">const</span> (in the current context).</p>
<p>Which is why it made my day when I finally came to understand that</p>
<blockquote><p>you make a method <span style="font-family:courier new;">const</span> so it can be invoked on <span style="font-family:courier new;">const</span> objects.</p></blockquote>
<p>and</p>
<blockquote><p>you make a method <span style="font-family:courier new;">const</span> to guarantee the client of your class that calling that method will not change the &#8216;state&#8217; of the object.</p></blockquote>
<p>And that made complete sense! And that is when you really appreciate why the <span style="font-family:courier new;">mutable</span> keyword is there. Because, even for constant objects, you might want certain methods to modify members that don&#8217;t really affect the &#8216;state&#8217; of the object (<span style="font-family:courier new;">mutable</span> keyword was in fact added later when this need quickly became obvious). In fact, wouldn&#8217;t you say that the very presence of the <span style="font-family:courier new;">mutable</span> keyword validates the point of this post?</p>
<p>And then on, it all made sense &#8211; why you <strong>must</strong> make methods <span style="font-family:courier new;">const</span> whenever you can, why you can have overloaded methods based on just them being <span style="font-family:courier new;">const</span> or not and so on.</p>
<p>Although nothing original from my side, let me wrap this post up with a typical example scenario where you absolutely need a <span style="font-family:courier new;">const</span> method (this is straight out of <em>Effective C++</em>): Let&#8217;s say we are designing a class <span style="font-family:courier new;">Array</span> that simulates a regular array but also does bounds checking, and can be used like this:</p>
<p><pre class="brush: cpp;">
Array&lt;int&gt; a(4, 7); //creates an Array of 4 ints,
                    //all initialized to 7.
const Array&lt;int&gt; ca(4, 8); //creates a const Array of 4 ints,
                           //all initialized to 8.
...
a[2] = ca[3]; //OK.
ca[2] = a[3]; //compile error!
...
</pre></p>
<p>And the way this can be achieved is by overloading the array operator [] twice &#8211; once as a non-<span style="font-family:courier new;">const</span> method which will get invoked for non-<span style="font-family:courier new;">const</span> objects, and as a <span style="font-family:courier new;">const</span> method which will get invoked for <span style="font-family:courier new;">const</span> objects, as follows:</p>
<p><pre class="brush: cpp;">
template&lt;typename T&gt;
class Array
{
public:

    ...

    //For non-const objects:
    T&amp; operator[](const size_t i)
    {
        // If i is out of bounds, handle error - throw.

        return v_[i];
    }

    //For const objects:
    const T&amp; operator[](const size_t i) const
    {
        // If is out of bounds, handle error - throw.

        return v_[i];
    }

    ...

private:
    vector&lt;T&gt; v_;
}
</pre></p>
<p>I wish I could learn everything right the first time&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/priyeshnarayanan.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/priyeshnarayanan.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/priyeshnarayanan.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/priyeshnarayanan.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/priyeshnarayanan.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/priyeshnarayanan.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/priyeshnarayanan.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/priyeshnarayanan.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/priyeshnarayanan.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/priyeshnarayanan.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/priyeshnarayanan.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/priyeshnarayanan.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/priyeshnarayanan.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/priyeshnarayanan.wordpress.com/78/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=78&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://priyeshnarayanan.wordpress.com/2009/08/09/c-notes-why-const-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3e7c60801f473e3dd195ed0bdce66f7e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sifaka</media:title>
		</media:content>
	</item>
		<item>
		<title>C++ Notes: Myths about Constructors</title>
		<link>http://priyeshnarayanan.wordpress.com/2009/08/01/c-notes-part-1-myths-about-constructors/</link>
		<comments>http://priyeshnarayanan.wordpress.com/2009/08/01/c-notes-part-1-myths-about-constructors/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 16:02:00 +0000</pubDate>
		<dc:creator>Priyesh Narayanan</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ notes]]></category>
		<category><![CDATA[Constructors]]></category>
		<category><![CDATA[Smart Pointers]]></category>

		<guid isPermaLink="false">http://priyeshnarayanan.wordpress.com/2009/08/01/c-notes-part-1-myths-about-constructors/</guid>
		<description><![CDATA[Some facts about Constructors in C++ that I came to understand only eventually: 1. The &#8216;Default&#8217; in a &#8216;Default Constructor&#8217;, refers to the parameter list, and that too in the call. Basically, a default constructor is a constructor that can be called with no arguments. So a constructor is a default constructor if it does [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=25&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Some facts about Constructors in C++ that I came to understand only eventually:</p>
<p><strong>1. </strong>The &#8216;Default&#8217; in a &#8216;Default Constructor&#8217;, refers to the parameter list, and that too in the call. Basically, <span style="font-weight:bold;">a default constructor is a constructor that can be called with no arguments</span>. So a constructor is a default constructor if it does not take any arguments or it takes default values for all its parameters &#8211; irrespective of whether you&#8217;ve explicitly defined one or the compiler generated one for you.</p>
<p><strong>2. </strong>&#8230; which is why <span style="font-weight:bold;">there is no such thing as a Default Copy Constructor.</span> I believe there are mainly two kinds of C++ folks out there &#8211; those that use the term &#8216;default copy constructor&#8217;, and those that used to use this term and now refrain from it. The whole problem is one tends to think of &#8216;Default&#8217; in this context to refer to &#8216;one that compiler generates by default if you don&#8217;t provide one&#8217;. It naturally follows from point #1 above that one can&#8217;t have the copy constructor called without any arguments, so there is no such thing. Unfortunately, this has become sort of a standard usage, even in some books (just do a google search and you&#8217;ll know). But you wouldn&#8217;t see Stroustrup or Meyers use this term, so that should be enough reason to refrain from it. I prefer the term &#8216;compiler-generated&#8217; instead &#8211; &#8216;Compiler-generated copy constructors&#8217; and &#8216;Compiler-generated default constructors&#8217;.</p>
<p><strong>3. </strong>Another mistake we all make at some point or the other, and we hear others make almost all the time, is the following innocuous looking statement: &#8216;the compiler-generated copy constructor does a bit-wise copy of its members&#8217;. Correct? No! And it&#8217;s one of those things that you quickly realize is wrong when you are made to think of it, but you learned it wrong because it was passed on to you and you never paused to think about it. The compiler-generated copy constructor does the following:</p>
<ul>
<li>Calls the copy constructor of any and all base classes with its input argument, the RHS object reference (so that the base parts are copy-constructed by the base itself).</li>
<li>Constructs the non-static members by &#8216;copying&#8217; from the respective members of the RHS object &#8211; meaning, for those members that have a copy constructor, the copy constructor is called, and for others, a &#8216;regular&#8217; copy assignment is done.</li>
</ul>
<p>	<span style="font-weight:bold;">4. A copy constructor does not necessarily have to take a const RHS</span>. The signature one usually sees for a copy constructor is the following: <pre class="brush: cpp;">
T::T(const T&amp; rhs);
</pre></p>
<p>But the following is perfectly valid as well:</p>
<p><pre class="brush: cpp;">
T::T(T&amp; rhs);
</pre></p>
<p>Of course, it makes sense once you think about it. A classic use of this is in the implementation of <span style="font-family:courier new;">std::auto_ptr</span><span style="font-style:italic;font-weight:bold;">. </span>The smart pointer philosophy for <span style="font-family:courier new;">auto_ptr</span> is to transfer ownership and nullify the RHS in a copy. That is, if you do the following:</p>
<p><pre class="brush: cpp;">
...
std::auto_ptr&lt;int&gt; ptr1 = new int;
std::auto_ptr&lt;int&gt; ptr2 = ptr1;
...
</pre></p>
<p>the statement at line 03 results in ptr1&#8242;s member primitive pointer to int getting set to 0. In order to facilitate this, the copy constructor has to take a non-const reference to the RHS.</li>
<li><span style="font-weight:bold;">5. Copy Constructors need not always be present. </span>This might be a compiler specific thing, but one thing I noticed while experimenting with gcc is that the compiler does not always generate a copy constructor for your class, even when you don&#8217;t provide one. I think the compiler &#8216;optimizes&#8217; it away if it figures that it can do without it &#8211; after all, structs could be copied in C without any &#8216;functions&#8217;, and the same is built into C++ as well, so I guess you fall back to it.</li>
<p>&#8211;</p>
<p><strong>Updates:</strong></p>
<p>(7th Aug 09): Reg point# 3, check out <a href="http://www.gotw.ca/gotw/080.htm" target="_blank">this</a> entry on Herb Sutter&#8217;s GOTW about the order in which subobjects are constructed.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/priyeshnarayanan.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/priyeshnarayanan.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/priyeshnarayanan.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/priyeshnarayanan.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/priyeshnarayanan.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/priyeshnarayanan.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/priyeshnarayanan.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/priyeshnarayanan.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/priyeshnarayanan.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/priyeshnarayanan.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/priyeshnarayanan.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/priyeshnarayanan.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/priyeshnarayanan.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/priyeshnarayanan.wordpress.com/25/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=25&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://priyeshnarayanan.wordpress.com/2009/08/01/c-notes-part-1-myths-about-constructors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3e7c60801f473e3dd195ed0bdce66f7e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sifaka</media:title>
		</media:content>
	</item>
		<item>
		<title>Heap&#8217;s the man</title>
		<link>http://priyeshnarayanan.wordpress.com/2009/07/30/heaps-the-man/</link>
		<comments>http://priyeshnarayanan.wordpress.com/2009/07/30/heaps-the-man/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 06:14:00 +0000</pubDate>
		<dc:creator>Priyesh Narayanan</dc:creator>
				<category><![CDATA[algorithm analysis]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[selection]]></category>
		<category><![CDATA[tree traversal]]></category>

		<guid isPermaLink="false">http://priyeshnarayanan.wordpress.com/2009/07/30/heaps-the-man/</guid>
		<description><![CDATA[I&#8217;d always wondered one thing about the heap data structure. Wherever I&#8217;d seen it being used in an algorithm, all it ever did was give the min or max in O(log n) time &#8211; in other words, it was always used to implement a priority queue. But I always always had the feeling that a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=24&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d always wondered one thing about the <span style="font-weight:bold;">heap</span> data structure. Wherever I&#8217;d seen it being used in an algorithm, all it ever did was give the min or max in O(log n) time &#8211; in other words, it was always used to implement a priority queue. But I always always had the feeling that a heap is capable of a little more, you know. I mean, agreed that the heap is much more relaxed in its relational property than a BST, but surely, it kept more information than what was required to find the min? Surely, there is more likelihood of finding the smaller elements at the top than at the bottom? Surely, there is more use of the property that every element <span style="font-style:italic;"></span>is the smallest in the subtree rooted at it?</p>
<p>
And so it was that, when I read about a certain use of heap in <a href="http://en.wikipedia.org/wiki/Selection_algorithm">this</a> article on wikipedia, I was enlightened. In particular, it talked about doing a <span style="font-weight:bold;">traversal of a heap</span>.</p>
<p>
Now, heap traversal is not something you see everyday. How is it interesting? How does it even make sense? Well, imagine the following problem: Given a heap containing <span style="font-style:italic;">n </span>elements, return the <span style="font-style:italic;">k </span>smallest elements from it (not in any specific order).</p>
<p>
One way to do it, the first thing that usually comes to mind is to do a <span style="font-style:italic;">removeMin()</span> on the heap <span style="font-style:italic;">k </span>times, which would return the <span style="font-style:italic;">k</span> smallest elements (and in order), in O(<span style="font-style:italic;">k </span>log <span style="font-style:italic;">n</span>) time.</p>
<p>
But imagine instead, and herein lies the rub, using the Breadth First Traversal on the heap &#8211; but with the exception that, instead of picking an element from the front of the queue that stores the children of elements that we visit, pick the min element and recursively visit that child. In other words, instead of using a queue to store the children of a node when we visit it, use a <span style="font-style:italic;">priority queue</span>.</p>
<p>
One can see that using this approach, the BFS traversal turns into a traversal that visits the elements in order of their &#8216;weights&#8217;. Then, coming back to our problem, to get the <span style="font-style:italic;">k </span>smallest elements, we will have to &#8216;visit&#8217; <span style="font-style:italic;">k</span> times in the traversal, and the priority queue would therefore contain at most <span style="font-style:italic;">k </span>elements (a visit results in 1 element being deleted from and 2 children being added to the priority queue).</p>
<p>
If we implement this priority queue, in turn, using another heap, we can then find the <span style="font-style:italic;">k </span>smallest elements in O(<span style="font-style:italic;"><span style="font-style:italic;">k </span></span>log <span style="font-style:italic;">k</span>) time, as the second heap would contain at most <span style="font-style:italic;">k </span>elements at any time. With an additional space requirement of O(<span style="font-style:italic;">k</span>), of course.</p>
<p>
So, for instance, to find the top <span style="font-style:italic;"><span style="font-style:italic;"><span style="font-style:italic;"><span style="font-style:italic;"><span style="font-style:italic;"></span></span></span></span></span>log<span style="font-style:italic;"> n </span>elements from a heap containing <span style="font-style:italic;">n</span> elements, one could do it in O(log<sup>2</sup><em>n</em>) time using the first approach, but in O(log<span style="font-style:italic;"> n </span>log log <span style="font-style:italic;">n</span>) time using the second.</p>
<p>
Now, is that awesome or what? :)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/priyeshnarayanan.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/priyeshnarayanan.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/priyeshnarayanan.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/priyeshnarayanan.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/priyeshnarayanan.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/priyeshnarayanan.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/priyeshnarayanan.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/priyeshnarayanan.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/priyeshnarayanan.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/priyeshnarayanan.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/priyeshnarayanan.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/priyeshnarayanan.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/priyeshnarayanan.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/priyeshnarayanan.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=24&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://priyeshnarayanan.wordpress.com/2009/07/30/heaps-the-man/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3e7c60801f473e3dd195ed0bdce66f7e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sifaka</media:title>
		</media:content>
	</item>
		<item>
		<title>Algorithms and Haskell</title>
		<link>http://priyeshnarayanan.wordpress.com/2009/05/26/algorithms-and-haskell/</link>
		<comments>http://priyeshnarayanan.wordpress.com/2009/05/26/algorithms-and-haskell/#comments</comments>
		<pubDate>Tue, 26 May 2009 07:25:00 +0000</pubDate>
		<dc:creator>Priyesh Narayanan</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://priyeshnarayanan.wordpress.com/2009/05/26/algorithms-and-haskell/</guid>
		<description><![CDATA[Okay, I am a Haskell newbie. Also an FP newbie. What, so far, I find amazing about Haskell more than its simplicity and elegance is how much an algorithm is conveyed by its Haskell representation. The classic example here is of course quick sort, the &#8216;poster child&#8217; of Haskell as one enthusiast puts it: In [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=19&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Okay, I am a Haskell newbie. Also an FP newbie. What, so far, I find amazing about Haskell more than its simplicity and elegance is how much an algorithm is conveyed by its Haskell representation. The classic example here is of course quick sort, the &#8216;poster child&#8217; of Haskell as one enthusiast puts it:</p>
<p><pre class="brush: vb;">
qsort' :: (Ord a) =&gt; [a] -&gt; [a]
qsort' [] = []
qsort' (x:xs) = qsort' lt ++ [x] ++ qsort' ge
    where lt = [y | y &lt;- xs, y &lt; x]
          ge = [y | y &lt;- xs, y &gt;= x]
</pre><br />
In case you are new to Haskell, yes it is short! And yes, we&#8217;ve used only built-in constructs of the language. One day into a Haskell tutorial and one should be fairly proficient in understanding the syntax of the above program.</p>
<p><span style="font-style:italic;">Note</span>: Quicksort, definitely Hoare&#8217;s version, is inherently imperative to begin with, in my opinion. In fact, most algorithms are stated in an imperative way and so naturally find their way more easily into C rather than to a language like Haskell (because imperative is the way the machine executes it). And most of the time, that imperative nature so much contributes to their efficiency. Since we have to break free from this imperative nature when representing them in a functional language, we might not be effectively doing the exact thing. The above Quicksort implementation, for instance, is indeed different from Hoare&#8217;s version, but not at a conceptual level. And personally, I find it okay as long as they are not inefficient by an order of magnitude (the classic argument here is that we switched from Assembly to C and Java, although nothing can beat the former&#8217;s efficiency really). Also to note here is that Haskell, in particular, makes use of lazy evaluation which probably makes up in part for the &#8216;inefficiency&#8217;. I guess.</p>
<p>Coming back, although quick sort is the much touted example for Haskell&#8217;s elegance, it is not a one-off case. In fact, look at the following sorting algorithms &#8211; try to figure what is what. It should be fairly easy to tell because the functions &#8216;explain&#8217; what the algorithm is in its natural form.</p>
<p><pre class="brush: vb;">
sort2 :: (Ord a) =&gt; [a] -&gt; [a]
sort2 [] = []
sort2 xs = min' ++ sort2 rest
    where min' = [z | z &lt;- xs, z == minimum xs]
          rest = [z | z &lt;- xs, z /= minimum xs]
</pre><br />
<pre class="brush: vb;">
sort3 :: (Ord a) =&gt; [a] -&gt; [a]
sort3 [] = []
sort3 (x:xs) = (filter (&lt;=x) sort3 xs)++[x]++(filter (&gt;x) sort3 xs)
</pre></p>
<p>And the following, which probably can be written in an even better way (hey, I&#8217;ve just begun!)</p>
<p><pre class="brush: vb;">
sort4 :: (Ord a) =&gt; [a] -&gt; [a]
sort4 []  = []
sort4 [x] = [x]
sort4 xs  = fun (sort4 (take l xs)) (sort4 (drop l xs))
    where fun xs [] = xs
          fun [] ys = ys
          fun xall@(x:xs) yall@(y:ys)
             | x &lt;= y    = x  : merge xs yall
             | otherwise = y : merge xall ys
          l = length xs `div` 2 
</pre></p>
<p><pre class="brush: vb;">
sort5 :: (Ord a) =&gt; [a] -&gt; [a]
sort5 [] = []
sort5 xs = sort5 (init l) ++ [last l]
where pass []  = []
      pass [x] = [x]
      pass (x:y:xs)
          | x &lt;= y    = x: pass (y:xs)
          | otherwise = y: pass (x:xs)
      l = pass xs
</pre></p>
<p>Like they say, a function in an FP language describes what it &#8216;is&#8217;, not how it &#8216;does&#8217; something. I wonder how effective it would be to have an Algorithms course taught in Haskell. I guess algorithms could be conveyed clearly, albeit a bit unconventionally. On the downside, it is probably difficult to judge (read analyze) an algorithm by looking at its implementation in Haskell. In fact, a Haskell implementation might not be the most efficient for an algorithm. And I also wonder about data structures. An FP language like Haskell, from the looks of it, is perhaps less about getting things done via data structures and more about getting them done via functions and recursions and list comprehensions and higher order functions, etc. Anyway, let me see how deep the rabbit hole goes.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/priyeshnarayanan.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/priyeshnarayanan.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/priyeshnarayanan.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/priyeshnarayanan.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/priyeshnarayanan.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/priyeshnarayanan.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/priyeshnarayanan.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/priyeshnarayanan.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/priyeshnarayanan.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/priyeshnarayanan.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/priyeshnarayanan.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/priyeshnarayanan.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/priyeshnarayanan.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/priyeshnarayanan.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=19&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://priyeshnarayanan.wordpress.com/2009/05/26/algorithms-and-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3e7c60801f473e3dd195ed0bdce66f7e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sifaka</media:title>
		</media:content>
	</item>
		<item>
		<title>Functional Programming, Vroom Vroom!</title>
		<link>http://priyeshnarayanan.wordpress.com/2009/05/26/functional-programming-vroom-vroom/</link>
		<comments>http://priyeshnarayanan.wordpress.com/2009/05/26/functional-programming-vroom-vroom/#comments</comments>
		<pubDate>Tue, 26 May 2009 06:46:00 +0000</pubDate>
		<dc:creator>Priyesh Narayanan</dc:creator>
				<category><![CDATA[dynamic programming]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://priyeshnarayanan.wordpress.com/2009/05/26/functional-programming-vroom-vroom/</guid>
		<description><![CDATA[So much to read and so little time. Well, not exactly short on time, but just can&#8217;t wait. You know how you are sometimes overwhelmed with just the prospect of doing something? It has sort of become perennial in my case. Anyway, been learning Haskell, primarily to get a real feel of the functional programming [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=18&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So much to read and so little time. Well, not exactly short on time, but just can&#8217;t wait. You know how you are sometimes overwhelmed with just the prospect of doing something? It has sort of become perennial in my case.</p>
<p>
Anyway, been learning Haskell, primarily to get a real feel of the functional programming (FP) paradigm. Oh, by the way, if you are, like I used to be, one of those who have always been hearing about functional programming but never really dived into it, then here is the first thing you should know: The &#8216;functional&#8217; in FP refers to the functions as we once knew &#8211; as we studied in mathematics &#8211; the <span style="font-style:italic;">f(x)</span>s and <span style="font-style:italic;">g(x)</span>s. It has got nothing to do with the conventional &#8216;functions&#8217; in programming. Yeah, it doesn&#8217;t look as revealing a statement until you&#8217;ve actually thought about it and wondered why a C program that is completely comprised of &#8216;functions&#8217; is not &#8216;functional&#8217;. The last time I recall being through such an Aha! moment was on finding out that the &#8216;programming&#8217; in dynamic programming has zilch to do with the &#8216;programming&#8217; as we know it. And from then on it just makes sense.
</p>
<p>
(They keep tricking us with such things though, don&#8217;t they?)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/priyeshnarayanan.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/priyeshnarayanan.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/priyeshnarayanan.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/priyeshnarayanan.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/priyeshnarayanan.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/priyeshnarayanan.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/priyeshnarayanan.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/priyeshnarayanan.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/priyeshnarayanan.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/priyeshnarayanan.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/priyeshnarayanan.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/priyeshnarayanan.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/priyeshnarayanan.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/priyeshnarayanan.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=18&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://priyeshnarayanan.wordpress.com/2009/05/26/functional-programming-vroom-vroom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3e7c60801f473e3dd195ed0bdce66f7e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sifaka</media:title>
		</media:content>
	</item>
		<item>
		<title>Why isn&#8217;t there an asymptotic notation for a tight bound?</title>
		<link>http://priyeshnarayanan.wordpress.com/2009/01/18/why-isnt-there-an-asymptotic-notation-for-a-tight-bound/</link>
		<comments>http://priyeshnarayanan.wordpress.com/2009/01/18/why-isnt-there-an-asymptotic-notation-for-a-tight-bound/#comments</comments>
		<pubDate>Sun, 18 Jan 2009 13:12:00 +0000</pubDate>
		<dc:creator>Priyesh Narayanan</dc:creator>
				<category><![CDATA[algorithm analysis]]></category>
		<category><![CDATA[asymptotic analysis]]></category>
		<category><![CDATA[Computational Complexity]]></category>

		<guid isPermaLink="false">http://priyeshnarayanan.wordpress.com/2009/01/18/why-isnt-there-an-asymptotic-notation-for-a-tight-bound/</guid>
		<description><![CDATA[For instance, we know that comparison based sorting takes Ω(n) time definitely, for the trivial reason that any algorithm will need to &#8216;consider&#8217; the entire input. But we know that Ω(n) is not a tight lower bound. Comparison based sorting will take, in the worst case, Ω(n log n) time (to decide on one of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=15&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>For instance, we know that comparison based sorting takes <span style="font-family:times new roman;">Ω(</span><i>n</i><span style="font-family:times new roman;">)</span> time definitely, for the trivial reason that any algorithm will need to &#8216;consider&#8217; the entire input. But we know that <span style="font-family:times new roman;">Ω(</span><i>n</i><span style="font-family:times new roman;">)</span> is not a tight lower bound. Comparison based sorting will take, in the worst case, <span style="font-family:times new roman;">Ω(</span><i>n</i><span style="font-family:times new roman;"> log </span><i>n</i><span style="font-family:times new roman;">)</span> time (to decide on one of the <span style="font-style:italic;font-family:times new roman;">n</span><span style="font-family:times new roman;">!</span> permutations of the input). This bound, we know is  tight.
</p>
<p></span>
<p>
Was just thinking it would have been convenient if there was a way to express the &#8216;tightness&#8217; using another one of those characters from the Greek alphabet, instead of having to say that so and so bound is tight every time. None of the existing asymptotic notations I know of (<i>O, o</i>, Ω, ω, <i>Θ</i>) help achieve this.
</p>
<p>
Perhaps just as well, we probably have enough notations already&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/priyeshnarayanan.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/priyeshnarayanan.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/priyeshnarayanan.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/priyeshnarayanan.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/priyeshnarayanan.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/priyeshnarayanan.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/priyeshnarayanan.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/priyeshnarayanan.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/priyeshnarayanan.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/priyeshnarayanan.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/priyeshnarayanan.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/priyeshnarayanan.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/priyeshnarayanan.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/priyeshnarayanan.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=15&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://priyeshnarayanan.wordpress.com/2009/01/18/why-isnt-there-an-asymptotic-notation-for-a-tight-bound/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3e7c60801f473e3dd195ed0bdce66f7e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sifaka</media:title>
		</media:content>
	</item>
		<item>
		<title>PRIMES and NONPRIMES</title>
		<link>http://priyeshnarayanan.wordpress.com/2009/01/12/primes-and-nonprimes/</link>
		<comments>http://priyeshnarayanan.wordpress.com/2009/01/12/primes-and-nonprimes/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 11:58:00 +0000</pubDate>
		<dc:creator>Priyesh Narayanan</dc:creator>
				<category><![CDATA[Computational Complexity]]></category>

		<guid isPermaLink="false">http://priyeshnarayanan.wordpress.com/2009/01/12/primes-and-nonprimes/</guid>
		<description><![CDATA[The other day, I happened to go through a simple but excellent lecture on the background of the P vs. NP problem. I really recommend it for brushing up, if it has been a while. Among other things, I found a particular remark interesting. In 1972, when Richard Karp, in his famous paper, showed that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=14&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The other day, I happened to go through a simple but excellent <i><a href="http://www.claymath.org/millennium/P_vs_NP/" rel="nofollow">lecture</a></i> on the background of the P vs. NP problem. I really recommend it for brushing up, if it has been a while.</p>
<p>
Among other things, I found a particular remark interesting. In 1972, when Richard Karp, in his famous paper, showed that many naturally occurring problems for which we didn&#8217;t know of a polynomial time algorithm, are in fact NP-complete, he also listed a few other such problems that couldn&#8217;t be proven to be NP-complete. The latter included NONPRIMES, which is the following problem:
</p>
<p>
&#8220;Given a number, is it composite?&#8221;
</p>
<p>
Interestingly, he listed this problem instead of PRIMES itself, which is:
</p>
<p>
&#8220;Given a number, is it prime?&#8221;
</p>
<p>
What got me wondering was why he chose to list NONPRIMES instead of PRIMES itself. (Not that it makes much of a difference, but, surely, PRIMES is much more impressive and prettier?)
</p>
<p>
So why did he? And what&#8217;s the difference at all, aren&#8217;t they complements of each other?
</p>
<p>
To understand that, let&#8217;s reformulate the questions as follows:
</p>
<p>
NONPRIMES: Are there two numbers less than <span style="font-style:italic;">n</span> whose product is <span style="font-style:italic;">n</span>?
</p>
<p>
PRIMES: Is the product of every possible pair of numbers less than <span style="font-style:italic;">n</span> not equal to <span style="font-style:italic;">n</span>?
</p>
<p>
First, remember that the input size is not the <span style="font-weight:bold;">value</span> of <span style="font-style:italic;">n</span> but the length of its <span style="font-weight:bold;">encoding</span> <span style="font-style:italic;"></span><span style="font-weight:bold;"></span> which would be the number of bits in <span style="font-style:italic;">n</span> which is log<span style="font-style:italic;">n</span>. (For more info on this, visit <a href="http://en.wikipedia.org/wiki/Pseudo-polynomial_time">this</a> wikipedia link on pseudo-polynomial algorithms).
</p>
<p>
So here&#8217;s the thing: According to the lecture, the reason PRIMES wasn&#8217;t listed is that, at that time, it wasn&#8217;t clear whether PRIMES is in NP at all. On the other hand, it was easy to show that NONPRIMES is in NP: Given a &#8220;yes&#8221; instance of the problem (which would be a pair of numbers whose product is <span style="font-style:italic;">n</span>), one could easily verify that it is indeed right by just multiplying and checking. Multiplication will take O<i>(log</i><sup>2</sup>n) time.
</p>
<p>
Interesting history, wouldn&#8217;t you say?
</p>
<p>
But let&#8217;s talk about the other question: What&#8217;s the difference really between the two problems? Aren&#8217;t they complements of each other?
</p>
<p>
Well, the answer to that gives us some insight into complexity classes. If a problem is in NP, then it&#8217;s complement need not be in NP. The problems which are complements of the problems in NP form the class co-NP. NP and co-NP are thought to be not equal.
</p>
<p>
But, aren&#8217;t they effectively the same questions? Can&#8217;t I just say &#8220;yes&#8221; for PRIMES if I get a &#8220;no&#8221; for NONPRIMES and vice versa?
</p>
<p>
Yes, you can! But that doesn&#8217;t necessarily mean that the running time of &#8216;verifying that a &#8220;yes&#8221; instance is indeed right&#8217;, is the same for both NONPRIMES and PRIMES &#8211; and therein lies the rub. For NONPRIMES, as we saw above, the verification can be done in polynomial time. But for PRIMES, if the question we consider is the above (&#8220;whether the product of every possible pair of numbers less than <span style="font-style:italic;">n</span> is not equal to <span style="font-style:italic;">n</span>&#8220;), then a &#8220;yes&#8221; instance itself would be the set of all pairs of numbers less than <span style="font-style:italic;">n</span>. That&#8217;s <sup>n</sup>C<sub>2</sub> pairs. To verify that that they all multiply to a different number will take <i>O(</i>n<sup>2</sup> <i>log </i><sup>2</sup>n) time which is exponential to the input size, the number of bits.
</p>
<p>
How about that!
</p>
<p>
Of course, PRIMES need not be formulated as &#8220;whether the product of every pair is less than <span style="font-style:italic;">n</span>&#8220;. There could be a better way to formulate the problem so that the &#8220;yes&#8221; instance is small enough and its verification also takes polynomial time. In 1972 nobody knew of such a way. Indeed, a few years later, someone did and it was proven that PRIMES is in NP.
</p>
<p>
And in 2002, it was proven that <a href="http://www.cse.iitk.ac.in/users/manindra/algebra/primality_v6.pdf" rel="nofollow" title="http://www.cse.iitk.ac.in/users/manindra/algebra/primality_v6.pdf"><i>PRIMES and therefore NONPRIMES are in fact in P</i></a>.
</p>
<p>
Understanding what the classes really stand for is therefore fundamental. I kinda wonder now if this whole thing should be thought of as a limitation of the way the complexity classes are defined.
</p>
<p>
p.s: Another interesting fact is that for all problems in P, the complements are also in P. Also, if a problem and its complement are both in NP (which is equivalent to saying that the problem is in both NP and co-NP), then I think it is believed that the problem is likely not NP-complete.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/priyeshnarayanan.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/priyeshnarayanan.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/priyeshnarayanan.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/priyeshnarayanan.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/priyeshnarayanan.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/priyeshnarayanan.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/priyeshnarayanan.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/priyeshnarayanan.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/priyeshnarayanan.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/priyeshnarayanan.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/priyeshnarayanan.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/priyeshnarayanan.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/priyeshnarayanan.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/priyeshnarayanan.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=priyeshnarayanan.wordpress.com&amp;blog=8878040&amp;post=14&amp;subd=priyeshnarayanan&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://priyeshnarayanan.wordpress.com/2009/01/12/primes-and-nonprimes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3e7c60801f473e3dd195ed0bdce66f7e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sifaka</media:title>
		</media:content>
	</item>
	</channel>
</rss>
