<?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>My Other Pants &#187; functional</title>
	<atom:link href="http://myotherpants.com/tag/functional/feed/" rel="self" type="application/rss+xml" />
	<link>http://myotherpants.com</link>
	<description>I left it in my other pants</description>
	<lastBuildDate>Sun, 28 Feb 2010 21:27:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Just passing through</title>
		<link>http://myotherpants.com/2009/11/just-passing-through/</link>
		<comments>http://myotherpants.com/2009/11/just-passing-through/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 23:30:57 +0000</pubDate>
		<dc:creator>Ball</dc:creator>
				<category><![CDATA[work safe]]></category>
		<category><![CDATA[functional]]></category>

		<guid isPermaLink="false">http://myotherpants.com/?p=182</guid>
		<description><![CDATA[Continuation Passing Style is a functional pattern where all work that can&#8217;t be passed into a tail recursive function call is passed in as an additional argument via a closure or anonymous function, delaying execution.
What got me confused for years on this was the explanation, almost like a mantra, &#8220;A continuation is like a callback.&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>Continuation Passing Style is a functional pattern where all work that can&#8217;t be passed into a tail recursive function call is passed in as an additional argument via a closure or anonymous function, delaying execution.</p>
<p>What got me confused for years on this was the explanation, almost like a mantra, &#8220;A continuation is like a callback.&#8221; I don&#8217;t use callbacks much and I believe that phrase is actually expressing a different pattern, simple first-class functions.  So what do I think a continuation is?  It is a promise to get to it later, after a tail call.  I do agree with the literature that it turns the function inside out.  <a href="http://my.safaribooksonline.com/9781590598504/stack_as_a_resource_colon_tail_calls_and">And Don Syme&#8217;s reasoning that you&#8217;re trading heap to avoid spending stack.</a></p>
<p>I know you hate <a href="http://en.wikipedia.org/wiki/Fibonacci_number">fibonacci</a>, but it&#8217;s simple to explain.  Here it is in all it&#8217;s glory.</p>
<p><script src="http://gist.github.com/235454.js"></script></p>
<p>You note, however, that since the results of two recursive calls need to be stored, we can&#8217;t dump the stackframe and tail optimize the call, right?  We could pass around two accumulators, essentially mimicking the imperative for-loop solution to the problem</p>
<p><script src="http://gist.github.com/235456.js"></script></p>
<p>This works because the Fibonacci sequence is just that, a sequence.  It can also be represented by a tree, and that&#8217;s what&#8217;s causing it to be a problem for recursion in the first place.  We are still left with, &#8220;How do I process a tree without risking a blown stack?&#8221;  That&#8217;s what Continuation Passing hopes to solve by using a continuation (read: anonymous function) that wraps our core functionality in a heap object so we don&#8217;t call down one leve.  I&#8217;m not making this clear, so I&#8217;ll show this in pictures.</p>
<div id="attachment_185" class="wp-caption aligncenter" style="width: 310px"><a href="http://myotherpants.com/wp-content/uploads/2009/11/cps_one.png"><img src="http://myotherpants.com/wp-content/uploads/2009/11/cps_one-300x69.png" alt="Continutation Passing Style, step one." title="cps_one" width="300" height="69" class="size-medium wp-image-185" /></a><p class="wp-caption-text">Step One: We're abstracting the problem to it's simplest form</p></div>
<div id="attachment_187" class="wp-caption aligncenter" style="width: 147px"><a href="http://myotherpants.com/wp-content/uploads/2009/11/cps_two.png"><img src="http://myotherpants.com/wp-content/uploads/2009/11/cps_two.png" alt="Step Two: We&#039;re making a promise to do something later" title="cps_two" width="137" height="96" class="size-full wp-image-187" /></a><p class="wp-caption-text">Step Two: We're making a promise to do something later</p></div>
<div id="attachment_186" class="wp-caption aligncenter" style="width: 310px"><a href="http://myotherpants.com/wp-content/uploads/2009/11/cps_three.png"><img src="http://myotherpants.com/wp-content/uploads/2009/11/cps_three-300x79.png" alt="Step Three: We&#039;re expanding the first abstraction" title="cps_three" width="300" height="79" class="size-medium wp-image-186" /></a><p class="wp-caption-text">Step Three: We're expanding the first abstraction</p></div>
<div id="attachment_184" class="wp-caption aligncenter" style="width: 310px"><a href="http://myotherpants.com/wp-content/uploads/2009/11/cps__four.png"><img src="http://myotherpants.com/wp-content/uploads/2009/11/cps__four-300x101.png" alt="Step Four: We&#039;re expanding the second abstraction" title="cps__four" width="300" height="101" class="size-medium wp-image-184" /></a><p class="wp-caption-text">Step Four: We're expanding the second abstraction</p></div>
<p>Step Five:  Fill in the base-cases for both our promise, and the function.<br />
<script src="http://gist.github.com/235497.js"></script></p>
<p>And now for the punchline.  Why do I care?  This goes back to the  <a href="http://xp123.com/xplor/xp0201/">spreadsheet challenge</a> <a href="http://myotherpants.com/2009/10/state-makes-it-easy/">I talked about earlier</a>.  I was evaluating my abstract tree and noticed a lot of code that looked like: <code>| Sum(a,b) -> eval a + eval b</code> and I knew it wasn&#8217;t going to scale past <a href="http://github.com/Ball/Spreadsheet-Challenge">my toy problem</a>.  I had a few tests, so I branched it with Git and tried replacing it with a tail recursive version.  I know a spreadsheet doesn&#8217;t need that, but it really didn&#8217;t add any complexity once I figured out what it was actually doing.  Just thought I&#8217;d share.</p>
]]></content:encoded>
			<wfw:commentRss>http://myotherpants.com/2009/11/just-passing-through/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
