<?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/"
		>
<channel>
	<title>Comments on: Tail recursion in Python</title>
	<atom:link href="http://paulbutler.org/archives/tail-recursion-in-python/feed/" rel="self" type="application/rss+xml" />
	<link>http://paulbutler.org/archives/tail-recursion-in-python/</link>
	<description></description>
	<lastBuildDate>Thu, 09 Feb 2012 16:01:42 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Akhmed</title>
		<link>http://paulbutler.org/archives/tail-recursion-in-python/#comment-53748</link>
		<dc:creator>Akhmed</dc:creator>
		<pubDate>Thu, 12 Jan 2012 20:05:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/tail-recursion-in-python/#comment-53748</guid>
		<description>Paul, very interesting post and great solution!

I see one problem with your code though: your tail_rec() function has it hardcoded that recursive functions must have just 1 argument. I rewrote your code as follows. Seems to be a more general solution.
&lt;code&gt;
# tail_rec receives fun(.) and returns digger(fun(.))
def tail_rec(fun):
	
    # digger(f) keeps digging recursively into f 
    # until it encounters some value 
	def digger(f):
		retval = f;
		
		# keep digging into the recursion 
		# while returned value is a function
		while (callable( retval )):
			retval = retval();

		return retval;

	# return the digger(fun(.)) 
	return (digger(fun));

# Recursive functions
def even(x):
	if x == 0:
		return True;
	else:
		return (lambda: odd(x-1));

def odd(x):
	if x == 0:
		return False;
	else:
		return (lambda: even(x-1));

is_even = tail_rec( even(2000001) );
is_odd = tail_rec( odd(2000001) );

print is_even;
print is_odd;
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Paul, very interesting post and great solution!</p>
<p>I see one problem with your code though: your tail_rec() function has it hardcoded that recursive functions must have just 1 argument. I rewrote your code as follows. Seems to be a more general solution.<br />
<code><br />
# tail_rec receives fun(.) and returns digger(fun(.))<br />
def tail_rec(fun):</p>
<p>    # digger(f) keeps digging recursively into f<br />
    # until it encounters some value<br />
	def digger(f):<br />
		retval = f;</p>
<p>		# keep digging into the recursion<br />
		# while returned value is a function<br />
		while (callable( retval )):<br />
			retval = retval();</p>
<p>		return retval;</p>
<p>	# return the digger(fun(.))<br />
	return (digger(fun));</p>
<p># Recursive functions<br />
def even(x):<br />
	if x == 0:<br />
		return True;<br />
	else:<br />
		return (lambda: odd(x-1));</p>
<p>def odd(x):<br />
	if x == 0:<br />
		return False;<br />
	else:<br />
		return (lambda: even(x-1));</p>
<p>is_even = tail_rec( even(2000001) );<br />
is_odd = tail_rec( odd(2000001) );</p>
<p>print is_even;<br />
print is_odd;<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rich Katz</title>
		<link>http://paulbutler.org/archives/tail-recursion-in-python/#comment-192</link>
		<dc:creator>Rich Katz</dc:creator>
		<pubDate>Tue, 31 Mar 2009 18:32:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/tail-recursion-in-python/#comment-192</guid>
		<description>See Python Tail Recursion Decorators.

For instance:

http://code.activestate.com/recipes/496691/

Regards,

Rich</description>
		<content:encoded><![CDATA[<p>See Python Tail Recursion Decorators.</p>
<p>For instance:</p>
<p><a href="http://code.activestate.com/recipes/496691/" rel="nofollow">http://code.activestate.com/recipes/496691/</a></p>
<p>Regards,</p>
<p>Rich</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Butler</title>
		<link>http://paulbutler.org/archives/tail-recursion-in-python/#comment-189</link>
		<dc:creator>Paul Butler</dc:creator>
		<pubDate>Mon, 30 Mar 2009 21:45:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/tail-recursion-in-python/#comment-189</guid>
		<description>ythefoe, that works, but only if you have the memory and you know in advance an upper bound on the size of the stack. Neither of these are always the case.</description>
		<content:encoded><![CDATA[<p>ythefoe, that works, but only if you have the memory and you know in advance an upper bound on the size of the stack. Neither of these are always the case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Butler</title>
		<link>http://paulbutler.org/archives/tail-recursion-in-python/#comment-188</link>
		<dc:creator>Paul Butler</dc:creator>
		<pubDate>Mon, 30 Mar 2009 21:42:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/tail-recursion-in-python/#comment-188</guid>
		<description>Brian, thanks. Stackless looks pretty cool.</description>
		<content:encoded><![CDATA[<p>Brian, thanks. Stackless looks pretty cool.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg Stoll</title>
		<link>http://paulbutler.org/archives/tail-recursion-in-python/#comment-187</link>
		<dc:creator>Greg Stoll</dc:creator>
		<pubDate>Mon, 30 Mar 2009 21:31:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/tail-recursion-in-python/#comment-187</guid>
		<description>Interesting technique - thanks!</description>
		<content:encoded><![CDATA[<p>Interesting technique &#8211; thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ythefoe</title>
		<link>http://paulbutler.org/archives/tail-recursion-in-python/#comment-186</link>
		<dc:creator>ythefoe</dc:creator>
		<pubDate>Mon, 30 Mar 2009 20:54:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/tail-recursion-in-python/#comment-186</guid>
		<description>Why not setting your recursion limit higher?
From the python doc:

sys.setrecursionlimit(limit)

    Set the maximum depth of the Python interpreter stack to limit. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python.

    The highest possible limit is platform-dependent. A user may need to set the limit higher when she has a program that requires deep recursion and a platform that supports a higher limit. This should be done with care, because a too-high limit can lead to a crash.</description>
		<content:encoded><![CDATA[<p>Why not setting your recursion limit higher?<br />
From the python doc:</p>
<p>sys.setrecursionlimit(limit)</p>
<p>    Set the maximum depth of the Python interpreter stack to limit. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python.</p>
<p>    The highest possible limit is platform-dependent. A user may need to set the limit higher when she has a program that requires deep recursion and a platform that supports a higher limit. This should be done with care, because a too-high limit can lead to a crash.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pádraig Brady</title>
		<link>http://paulbutler.org/archives/tail-recursion-in-python/#comment-185</link>
		<dc:creator>Pádraig Brady</dc:creator>
		<pubDate>Mon, 30 Mar 2009 09:46:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/tail-recursion-in-python/#comment-185</guid>
		<description>So you explicitly store state rather than implicitly on the stack. This is better as I noted here:
http://www.pixelbeat.org/programming/functional_python_state.html</description>
		<content:encoded><![CDATA[<p>So you explicitly store state rather than implicitly on the stack. This is better as I noted here:<br />
<a href="http://www.pixelbeat.org/programming/functional_python_state.html" rel="nofollow">http://www.pixelbeat.org/programming/functional_python_state.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: popurls.com // popular today</title>
		<link>http://paulbutler.org/archives/tail-recursion-in-python/#comment-184</link>
		<dc:creator>popurls.com // popular today</dc:creator>
		<pubDate>Mon, 30 Mar 2009 09:40:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/tail-recursion-in-python/#comment-184</guid>
		<description>&lt;strong&gt;popurls.com // popular today...&lt;/strong&gt;

story has entered the popular today section on popurls.com...</description>
		<content:encoded><![CDATA[<p><strong>popurls.com // popular today&#8230;</strong></p>
<p>story has entered the popular today section on popurls.com&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: username223</title>
		<link>http://paulbutler.org/archives/tail-recursion-in-python/#comment-183</link>
		<dc:creator>username223</dc:creator>
		<pubDate>Mon, 30 Mar 2009 09:09:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/tail-recursion-in-python/#comment-183</guid>
		<description>&quot;a = a()&quot;?  Objectivist? ;-)</description>
		<content:encoded><![CDATA[<p>&#8220;a = a()&#8221;?  Objectivist? ;-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: praxis' me2DAY</title>
		<link>http://paulbutler.org/archives/tail-recursion-in-python/#comment-182</link>
		<dc:creator>praxis' me2DAY</dc:creator>
		<pubDate>Mon, 30 Mar 2009 08:43:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.paulbutler.org/archives/tail-recursion-in-python/#comment-182</guid>
		<description>&lt;strong&gt;praxis? ??...&lt;/strong&gt;

tail recursion in python, ??? ?? ???....</description>
		<content:encoded><![CDATA[<p><strong>praxis? ??&#8230;</strong></p>
<p>tail recursion in python, ??? ?? ???&#8230;.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

