<?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>Huddled Masses &#187; Computation</title>
	<atom:link href="http://huddledmasses.org/tag/computation/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Fibonacci Sequence &#8230; in PowerShell</title>
		<link>http://huddledmasses.org/fibbonaci-sequence-in-powershell/</link>
		<comments>http://huddledmasses.org/fibbonaci-sequence-in-powershell/#comments</comments>
		<pubDate>Thu, 31 Jan 2008 05:52:41 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Computation]]></category>
		<category><![CDATA[Fibonacci]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/fibbonaci-sequence-in-powershell/</guid>
		<description><![CDATA[Scott Hanselman has been posting weekly snippets of code in various languages &#8230; today he did Fibonacci sequences &#8230; by the way, the point of his posts is to get you to read code in other languages, not necessarily showing the best way to do things in any of the languages represented &#8212; you only [...]]]></description>
			<content:encoded><![CDATA[	<p><a href="http://www.hanselman.com/blog/TheWeeklySourceCode13FibonacciEdition.aspx">Scott Hanselman</a> has been posting weekly snippets of code in various languages &#8230;  today he did Fibonacci sequences &#8230;  by the way, the point of his posts is to get you to read code in other languages, not necessarily showing the <strong>best</strong> way to do things in any of the languages represented &#8212; you only solve fibonacci sequences recursively in demos, not in real life.  Well, except perhaps in tightly optimized tail-recursion languages, maybe in Haskell &#8230;</p>

	<h4>In C#</h4>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #FF0000;">int</span> fib<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> x<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>x<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">:</span>fib<span style="color: #000000;">&#40;</span>x<span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> fib<span style="color: #000000;">&#40;</span>x<span style="color: #008000;">-</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span><br />
fib<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div>

	<h4>In PowerShell</h4>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">filter</span> fib<span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #66cc66;">-</span>lt2<span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#125;</span><span style="color: #666699; font-weight: bold;">else</span><span style="color: #333;">&#123;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">|</span>fib<span style="color: #333;">&#41;</span><span style="color: #66cc66;">+</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">|</span>fib<span style="color: #333;">&#41;</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
<span style="color: #cc66cc;">20</span> <span style="color: #66cc66;">|</span> fib</div>

	<p>That&#8217;s just beautiful  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' />   ... incidentally, by default, PowerShell will only recurse up to 100 levels in the stack, so it kind-of sucks at recursive calls.  You could also write that as a traditional function in powershell, but the way powershell passes arguments to functions makes it really awkward.  I&#8217;ll put it on multiple lines so you can read it easier.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> fib<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span> <br />
&nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #000066;">-lt</span> <span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #cc66cc;">1</span> <br />
&nbsp; <span style="color: #333;">&#125;</span><span style="color: #666699; font-weight: bold;">else</span><span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># yes, every one of these parentheses is required</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#40;</span>fib <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #333;">&#40;</span>fib <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <br />
&nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
fib <span style="color: #cc66cc;">20</span></div>

	<p>As a side note, that function actually <a href="http://blogs.msdn.com/powershell/archive/2008/01/28/lightweight-performance-testing-with-powershell.aspx">runs measurably slower</a> than the pipeline method &#8230; but speeds up noticeably if you stick a few <code>[int]</code> type specifiers in the right places so PowerShell doesn&#8217;t have to infer types &#8212; something to think about.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/fibbonaci-sequence-in-powershell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

