<?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>Ecommerce Ninja - The Zachary Fox Blog &#187; Fibonacci Project</title>
	<atom:link href="http://www.zacharyfox.com/blog/category/fibonacci-project/feed" rel="self" type="application/rss+xml" />
	<link>http://www.zacharyfox.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 11 Aug 2011 10:44:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Fibonacci in Erlang</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-erlang</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-erlang#comments</comments>
		<pubDate>Thu, 11 Aug 2011 10:44:32 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/?p=148</guid>
		<description><![CDATA[We&#8217;ve seen the fibonacci example in multiple languages, but none of them purely functional. Erlang is a functional language that is growing in popularity due to the ease in which you can create concurrent programs. It has a number of paradigms that may be foreign to developers that are used to working in scripting or [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve seen the fibonacci example in multiple languages, but none of them purely functional. Erlang is a functional language that is growing in popularity due to the ease in which you can create concurrent programs. It has a number of paradigms that may be foreign to developers that are used to working in scripting or object oriented languages, but rather than a hindrance, things like immutable variables and the lack of loop constructs encourage an elegance in implementation that becomes second nature once you change your mindset.</p>
<p>In this example, we&#8217;re using three of these features together to create a very simple version of our fibonacci function.</p>
<ul>
<li><strong>Multiple function clauses</strong> &#8211; In erlang, you can define a function multiple times &#8211; and depending on the arguments passed to the function the correct version of that function is executed.</li>
<li><strong>Pattern matching</strong> &#8211; To determine which function clause is executed, variables declared in the head can be matched against a pattern. In this example, we&#8217;re using the same variable name in the first clause for the third and fourth variables. Because variables are immutable, they can only ever equal one value when they are set. That means that when we are looking for &#8220;Count&#8221; twice in the function head &#8211; we are really looking for those variables to contain the same value.</li>
<li><strong>Tail recursion</strong> &#8211; Instead of for or while loops, erlang uses a mechanism called tail recursion. By calling itself multiple times with incremented values, our fibonacci function is in effect executing a loop; and because we have multiple function clauses, we can end the loop when a certain condition is met.</li>
</ul>
<pre class="vci_code">
#<span class="Statement">!/</span>usr<span class="Statement">/</span>bin<span class="Statement">/</span>env escript
<span class="Comment">%% -*- erlang -*-</span>
<span class="Comment">%%! -smp enable -sname fibonacci debug verbose</span>

<span class="Comment">%% I've written this as an escript rather than something that we need to compile</span>
<span class="Comment">%% This means it will compile on the fly and execute the function named main</span>
main([]) <span class="Statement">-&gt;</span>
        <span class="Comment">%% Getting the input here is simple a single call to an io function</span>
        Num <span class="Statement">=</span> <span class="Identifier">io</span><span class="Special">:</span><span class="Identifier">get</span><span class="Special">_</span><span class="Identifier">line</span>(<span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">How many numbers of the sequence would you like?</span><span class="Special">\n</span><span class="Constant">&quot;</span>),
        <span class="Comment">%% Erlang does have types - we need to remove the newline and convert the</span>
        <span class="Comment">%% input to an integer here before passing it to the fibonacci function</span>
        fibonacci(<span class="Constant">0</span>, <span class="Constant">1</span>, <span class="Identifier">list_to_integer</span>(<span class="Identifier">string</span><span class="Special">:</span><span class="Identifier">strip</span>(Num, both, $<span class="Special">\</span>n)), <span class="Constant">0</span>);
main([Num]) <span class="Statement">-&gt;</span>
        fibonacci(<span class="Constant">0</span>, <span class="Constant">1</span>, <span class="Identifier">list_to_integer</span>(<span class="Identifier">string</span><span class="Special">:</span><span class="Identifier">strip</span>(Num, both, $<span class="Special">\</span>n)), <span class="Constant">0</span>)<span class="Special">.</span>

<span class="Comment">%% Here we see the elegance of erlang's tail recursion and multiple function</span>
<span class="Comment">%% definitions. Rather than changing variables (which we can't do anyway - </span>
<span class="Comment">%% they are immutable!), we call the function again with new values. When</span>
<span class="Comment">%% the end is reached ( Num == Count ), we exit with ok. Notice that the end</span>
<span class="Comment">%% clause is first so it is exectued when the 3rd and 4th arguments match.</span>
fibonacci( <span class="Special">_</span>, <span class="Special">_</span>, Count, Count ) <span class="Statement">-&gt;</span>
        ok;
fibonacci( A, B, Num, Count ) <span class="Statement">-&gt;</span>
        <span class="Identifier">io</span><span class="Special">:</span><span class="Identifier">format</span>(<span class="Constant">&quot;</span><span class="Special">~w~n</span><span class="Constant">&quot;</span>, [A]),
        fibonacci( B, A <span class="Statement">+</span> B, Num, Count <span class="Statement">+</span> <span class="Constant">1</span>)<span class="Special">.</span>
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.4.0.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.erl">fibonacci.erl</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-erlang/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fibonacci in Java</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-java</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-java#comments</comments>
		<pubDate>Sun, 06 Jan 2008 17:53:59 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
				<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-java</guid>
		<description><![CDATA[So we&#8217;ve seen lots of examples of our Fibonacci program in scripting languages, but other than C, I haven&#8217;t touched on many compiled languages. So here is a version in Java, probably one of the most common and popular languages in use today. Considered by advocates to be the among the best enterprise application development [...]]]></description>
			<content:encoded><![CDATA[<p>So we&#8217;ve seen lots of examples of our Fibonacci program in scripting languages, but other than C, I haven&#8217;t touched on many compiled languages. So here is a version in <a href="http://www.java.com/en/">Java</a>, probably one of the most common and popular languages in use today. Considered by advocates to be the among the best enterprise application development languages, we&#8217;ll see here how to write our simple program.</p>
<pre class="vci_code">
<span class="Comment">/*</span>
<span class="Comment"> * Much like C's stdio.h, the java.io.* classes will let us access stdin and stdout.</span>
<span class="Comment"> */</span>

<span class="PreProc">import</span> java.io.*;

<span class="Comment">/*</span>
<span class="Comment"> * Like Ruby, in java, everything is an object. To write a program, you'll need to</span>
<span class="Comment"> * start by declaring a class. As in C, our program execution starts with the main</span>
<span class="Comment"> * function. Also like C, Java is a compiled language, so you'll need to compile </span>
<span class="Comment"> * the code and then run the class using the java interpreter.</span>
<span class="Comment"> */</span>

<span class="Type">class</span> Fibonacci {

  <span class="Comment">/*</span>
<span class="Comment">   * So here we are defining the main function. Remember that this is supposed to</span>
<span class="Comment">   * actually run this program, so the function needs to be `public`, in addition,</span>
<span class="Comment">   * it's `static`, meaning we can call this method without an object of the Fibonacci</span>
<span class="Comment">   * class being instantiated, and it doesn't need to return anything, as it will</span>
<span class="Comment">   * run by the interpreter, which will handle the exit status. </span>
<span class="Comment">   */</span>

  <span class="Type">public</span> <span class="Type">static</span> <span class="Type">void</span> main(String args[]) {

    <span class="Comment">/* </span>
<span class="Comment">     * We are using System.out.println here, but newer versions of Java have the printf method.</span>
<span class="Comment">     */</span>

    System.out.println(<span class="Constant">&quot;How many numbers of the sequence would you like?&quot;</span>);

    <span class="Comment">/*</span>
<span class="Comment">     * I'm sure there's more than one way to skin a cat, but to read stdin here, we</span>
<span class="Comment">     * are creating a new BufferedReader, which will read one line of input.</span>
<span class="Comment">     */</span>

    InputStreamReader sr = <span class="Statement">new</span> InputStreamReader(System.in);
    BufferedReader br    = <span class="Statement">new</span> BufferedReader(sr);

    <span class="Comment">/*</span>
<span class="Comment">     * Now here is a concept we haven't addressed yet. The java compiler complains if</span>
<span class="Comment">     * you try to call a method that could throw an exception (error), so I've included</span>
<span class="Comment">     * an example here of how to handle the exceptions that could be thrown. Also, like</span>
<span class="Comment">     * in our previous examples, we are casting the input to an integer.</span>
<span class="Comment">     */</span>

    <span class="Statement">try</span> {
      String input = br.readLine();
      <span class="Type">int</span> n = Integer.valueOf(input).intValue();
      fibonacci(n);
    } <span class="Statement">catch</span> (NumberFormatException e){
      System.out.println(<span class="Constant">&quot;That is not an integer. Please enter an integer value&quot;</span>);
    } <span class="Statement">catch</span> (IOException e) {
      System.out.println(<span class="Constant">&quot;I did not recieve an input&quot;</span>);
    }
  }

  <span class="Comment">/*</span>
<span class="Comment">   * So here is our Fibonacci function. like the main function, it is public and can be</span>
<span class="Comment">   * called without creating a Fibonacci object. We've also introduced a new method of </span>
<span class="Comment">   * calculating the sequence without using a temporary variable. In a later post, I will</span>
<span class="Comment">   * examine the different algorithms used to calculate the Fibonacci sequence, and compare</span>
<span class="Comment">   * performance in multiple languages.</span>
<span class="Comment">   */</span>

  <span class="Type">public</span> <span class="Type">static</span> <span class="Type">void</span> fibonacci(<span class="Type">int</span> n){
    <span class="Type">int</span> a=<span class="Constant">0</span>,b=<span class="Constant">1</span>;

    <span class="Statement">for</span> (<span class="Type">int</span> i=<span class="Constant">0</span>;i&lt;n;i++){
      System.out.println(a);
      a=a+b;
      b=a-b;
    }
  }
}
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.4.0.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.java">fibonacci.java</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-java/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Fibonacci in Ruby</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-ruby</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-ruby#comments</comments>
		<pubDate>Sun, 06 Jan 2008 01:01:42 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
				<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-ruby</guid>
		<description><![CDATA[So finally we get to Ruby. Is it a ghetto? I don&#8217;t know about that, but I do know that writing our simple Fibonacci program in Ruby was a piece of cake. Before we get to the good stuff, though, I&#8217;d like to recap where we are, and what we&#8217;ve done so far. Here&#8217;s a [...]]]></description>
			<content:encoded><![CDATA[<p align="left">So finally we get to <a href="http://www.ruby-lang.org/en/">Ruby</a>. Is it a ghetto? I don&#8217;t know about that, but I do know that writing our simple Fibonacci program in Ruby was a piece of cake. Before we get to the good stuff, though, I&#8217;d like to recap where we are, and what we&#8217;ve done so far. Here&#8217;s a list of the Fibonacci project programs so far:</p>
<ul>
<li><a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c">Fibonacci in C</a></li>
<li><a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-php">Fibonacci in PHP</a></li>
<li><a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-bash">Fibonacci in BASH</a></li>
<li><a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl">Fibonacci in Perl</a></li>
<li><a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-python">Fibonacci in Python</a></li>
</ul>
<p>So this is our sixth version of the simple sequence generator, this time in Ruby</p>
<pre class="vci_code">
<span class="PreProc">#!/usr/local/bin/ruby</span>

<span class="Comment"># In Ruby, we define a function with def...end functions can accept parameters,</span>
<span class="Comment"># but don't have to, and you can leave off the parenthesis if your function does</span>
<span class="Comment"># not need them.</span>

<span class="PreProc">def </span><span class="Identifier">main</span>
  printf <span class="Special">&quot;</span><span class="Special">\n</span><span class="Constant">How many numbers of the sequence would you like?</span><span class="Special">\n</span><span class="Special">&quot;</span>

  <span class="Comment"># Here, STDIN is a constant, but like everything in Ruby, it's also a class, so</span>
  <span class="Comment"># we use the readline method to get our input. to_i casts our input to an integer</span>

  n = <span class="Identifier">STDIN</span>.readline.to_i
  fibonacci(n)
<span class="PreProc">end</span>

<span class="Comment"># Here is a good example of something that is cool about Ruby. The times method</span>
<span class="Comment"># works just like it sounds, it will do something n times. As in Perl and Python,</span>
<span class="Comment"># we don't need a temp variable here to swap the values for a and b.</span>

<span class="PreProc">def </span><span class="Identifier">fibonacci</span>(n)
  a,b = <span class="Constant">0</span>,<span class="Constant">1</span>
  n.times <span class="Statement">do</span>
    printf(<span class="Special">&quot;</span><span class="Constant">%d</span><span class="Special">\n</span><span class="Special">&quot;</span>, a)
    a,b = b,a+b
  <span class="Statement">end</span>
<span class="PreProc">end</span>

main
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.4.0.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.rb">fibonacci.rb</a></p>
<p>The next installment will be another popular language. Stay tuned to see <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-java">Fibonacci in Java</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-ruby/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fibonacci in Python</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-python</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-python#comments</comments>
		<pubDate>Sun, 06 Jan 2008 00:34:05 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
				<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-python</guid>
		<description><![CDATA[In our latest installment of the Fibonacci project, we&#8217;ll write our simple program in Python. For those of you who are unfamiliar with what we are doing here, please read the first post here. Python is a newer, but increasingly popular scripting language. I suppose the most interesting difference here is that Python is dependent [...]]]></description>
			<content:encoded><![CDATA[<p>In our latest installment of the Fibonacci project, we&#8217;ll write our simple program in <a href="http://www.python.org/">Python</a>. For those of you who are unfamiliar with what we are doing here, please read the <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c">first post here</a>. Python is a newer, but increasingly popular scripting language. I suppose the most interesting difference here is that Python is dependent on indentation to define blocks, rather than braces or other language constructs.</p>
<pre class="vci_code">
<span class="Comment">#!/usr/bin/python</span>

<span class="Comment"># Import the system library. This allows us to access stdin later.</span>
<span class="PreProc">import</span> sys

<span class="Comment"># Here's our main function. Python is pretty efficient here. You</span>
<span class="Comment"># should notice that there are no braces. Python is dependant on</span>
<span class="Comment"># whitespace to define blocks.</span>

<span class="Statement">def</span> <span class="Identifier">main</span>():
  <span class="Statement">print</span> &quot;<span class="Special">\n</span><span class="Constant">How many numbers of the sequence would you like?</span>&quot;
  n = int(sys.stdin.readline())
  fibonacci(n)

<span class="Comment"># Here's the fibonacci function. Like in Perl, you can assign multiple</span>
<span class="Comment"># variables on a line without using a temporary variable. Also, the for </span>
<span class="Comment"># loop here works more like a foreach loop by setting a range from 0 to n.</span>

<span class="Statement">def</span> <span class="Identifier">fibonacci</span>(n):
  a,b = 0,1
  <span class="Statement">for</span> i <span class="Statement">in</span> range(0,n):
    <span class="Statement">print</span> a
    a,b, = b,a+b

main()
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.4.0.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.py">fibonacci.py</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-python/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fibonacci in Perl</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl#comments</comments>
		<pubDate>Thu, 03 Jan 2008 21:47:11 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
				<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl</guid>
		<description><![CDATA[So this is our fourth post of the Fibonacci project, and in this installment, we are going to take on Perl. For those of you who don&#8217;t know about the Fibonacci project, you should read the first post, Fibonacci in C. Also, you may want to check out the other posts in the series. The [...]]]></description>
			<content:encoded><![CDATA[<p>So this is our fourth post of the Fibonacci project, and in this installment, we are going to take on Perl. For those of you who don&#8217;t know about the Fibonacci project, you should read the first post, <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c">Fibonacci in C</a>. Also, you may want to check out the other posts in the series. The comments in the code will make more sense if you have seen the other examples.</p>
<pre class="vci_code">
<span class="PreProc">#!/usr/bin/perl</span>

<span class="Comment"># Functions in Perl are called subroutines. Like bash, you don't define the </span>
<span class="Comment"># parameters in the function declaration. We'll see how to access the parameters</span>
<span class="Comment"># when we look at the Fibonacci subroutine below.</span>

<span class="Statement">sub</span><span class="Identifier"> </span><span class="Identifier">main</span><span class="Identifier"> </span>{

<span class="Comment"># Again, the ubiquitous printf function. Apparently a staple of programming</span>
<span class="Statement">printf</span> <span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">How many numbers of the sequence would you like?</span><span class="Special">\n</span><span class="Constant">&quot;</span>;

<span class="Comment"># Once again, we don't need to declare variables before using them.</span>
<span class="Comment"># Perl's scalar variables, prefaced with $, can be either strings or numbers</span>
<span class="Comment"># We use &lt;STDIN&gt; to get the data from stdin here</span>

<span class="Identifier">$n</span> = <span class="Identifier">&lt;STDIN&gt;</span>;

<span class="Comment"># As in PHP, we need to remove the newline at the end</span>

<span class="Statement">chop</span> <span class="Identifier">$n</span>;

<span class="Identifier">&amp;fibonacci</span>(<span class="Identifier">$n</span>);

<span class="Statement">exit</span> <span class="Constant">0</span>;
}

<span class="Comment"># Except for the first line declaring the subroutine, and the different way that</span>
<span class="Comment"># parameters passed to the subroutine are passed, this is identical to the PHP version</span>

<span class="Statement">sub</span><span class="Identifier"> </span><span class="Identifier">fibonacci</span><span class="Identifier"> </span>{
  <span class="Identifier">$a</span> = <span class="Constant">0</span>;
  <span class="Identifier">$b</span> = <span class="Constant">1</span>;

  <span class="Comment"># So now we see the parameter being used here. For clarity, I have written the for</span>
  <span class="Comment"># loop using $n like the other examples. To set $n using the first parameter passed</span>
  <span class="Comment"># to the subroutine, I access the scalar variable $_[0], which is the first element</span>
  <span class="Comment"># of the parameter array @_</span>

  <span class="Identifier">$n</span> = <span class="Identifier">$_</span>[<span class="Constant">0</span>];

  <span class="Statement">for</span> (<span class="Identifier">$i</span>=<span class="Constant">0</span>;<span class="Identifier">$i</span>&lt;<span class="Identifier">$n</span>;<span class="Identifier">$i</span>++){
    <span class="Statement">printf</span> <span class="Constant">&quot;</span><span class="Identifier">%d</span><span class="Special">\n</span><span class="Constant">&quot;</span>, <span class="Identifier">$a</span>;
    <span class="Identifier">$sum</span> = <span class="Identifier">$a</span> + <span class="Identifier">$b</span>;
    <span class="Identifier">$a</span> = <span class="Identifier">$b</span>;
    <span class="Identifier">$b</span> = <span class="Identifier">$sum</span>;
  }
}

<span class="Identifier">&amp;main</span>;
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.4.0.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.pl">fibonacci.pl</a></p>
<p>Any Perl gurus want to critique my work here? Like most of the examples, this is a simplified version, but one that works.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-perl/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Fibonacci in BASH</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-bash</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-bash#comments</comments>
		<pubDate>Thu, 03 Jan 2008 19:59:05 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
				<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-bash</guid>
		<description><![CDATA[So here&#8217;s the big surprise. Shell scripting! It&#8217;s not what most people think of when they think of programming, but systems administrators are still doing lots of scripting work to automate tasks. Shell scripting can tackle some pretty serious and complicated problems, so running our Fibonacci program is no sweat. If you haven&#8217;t seen the [...]]]></description>
			<content:encoded><![CDATA[<p>So here&#8217;s the big surprise. Shell scripting! It&#8217;s not what most people think of when they think of programming, but systems administrators are still doing lots of scripting work to automate tasks. Shell scripting can tackle some pretty serious and complicated problems, so running our Fibonacci program is no sweat. If you haven&#8217;t seen the <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c">Fibonacci in C</a> and the <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-php">Fibonacci in PHP</a>, now might be a good time to review them.</p>
<pre class="vci_code">
<span class="Comment">#!/bin/bash</span>

<span class="Comment">#</span>
<span class="Comment"># Here is our main function. Like PHP, I could have simply placed this in the script</span>
<span class="Comment"># rather than having a function. You notice that like C and PHP, the printf function</span>
<span class="Comment"># still exists. Unlike C and PHP function calls do not enclose the parameters with </span>
<span class="Comment"># paraenthsis.</span>

<span class="Identifier">function</span> main <span class="Special">{</span>
  <span class="Statement">printf</span> <span class="Statement">&quot;</span><span class="Special">\n</span><span class="Constant">How many numbers of the sequence would you like?</span><span class="Special">\n</span><span class="Statement">&quot;</span>

 <span class="Comment"> # Bash lets us use a function &quot;read&quot; to get input from stdin</span>
 <span class="Comment"> # Notice that we are not using a dollar sign in front of the variables when</span>
 <span class="Comment"> # we set them, but we need to when we use them.</span>

  <span class="Statement">read</span> n
  fibonacci <span class="PreProc">$n</span>

 <span class="Comment"> # Again, I'm exiting with status 0</span>

  <span class="Statement">exit</span> <span class="Constant">0</span>
<span class="Special">}</span>

<span class="Comment"># Starting to look familiar? This function is actually a little different than the </span>
<span class="Comment"># C style for loop that we've used before. Instead we use while..do..done to accomplish</span>
<span class="Comment"># the same thing. You'll also see that rather than specifying parameters in the </span>
<span class="Comment"># function;s declaration, we simply reference the variable &quot;$1&quot; to get the first parameter</span>
<span class="Comment"># passed when the function was called.</span>

<span class="Identifier">function</span> fibonacci <span class="Special">{</span>
  <span class="Identifier">a</span>=<span class="Constant">0</span>
  <span class="Identifier">b</span>=<span class="Constant">1</span>
  <span class="Identifier">i</span>=<span class="Constant">0</span>

  <span class="Statement">while</span><span class="Statement"> </span><span class="Statement">[</span> <span class="PreProc">$i</span> <span class="Statement">-lt</span> <span class="PreProc">$1</span> <span class="Statement">]</span>
<span class="Statement">  </span><span class="Statement">do</span>
    <span class="Statement">printf</span> <span class="Statement">&quot;</span><span class="Constant">%d</span><span class="Special">\n</span><span class="Statement">&quot;</span> <span class="PreProc">$a</span>
    <span class="Statement">let</span> <span class="Identifier">sum</span>=<span class="PreProc">$a</span>+<span class="PreProc">$b</span>
    <span class="Statement">let</span> <span class="Identifier">a</span>=<span class="PreProc">$b</span>
    <span class="Statement">let</span> <span class="Identifier">b</span>=<span class="PreProc">$sum</span>
    <span class="Statement">let</span> <span class="Identifier">i</span>=<span class="PreProc">$i</span>+<span class="Constant">1</span>
  <span class="Statement">done</span>
<span class="Special">}</span>

main
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.4.0.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.sh">fibonacci.sh</a></p>
<p>So what are the <a href="http://tldp.org/LDP/abs/html/gotchas.html">gotchas for scripting in BASH</a>? What killed me is whitespace! (a=1 is not the same as a = 1). Once you get the hang of it, it&#8217;s not a big deal, but if you are used to working in PHP or another language that ignores whitespace, you&#8217;ll run through a few syntax errors before figuring it out. Using an editor like Vim that has syntax highlighting will help a lot there.So there is the third version of our Fibonacci project. Coming soon, I&#8217;ll take on the popular scripting languages perl, python, and ruby.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-bash/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Fibonacci in PHP</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-php</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-php#comments</comments>
		<pubDate>Thu, 03 Jan 2008 19:49:11 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
				<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-php</guid>
		<description><![CDATA[So here is my second post in the Fibonacci project. For those of you that have not seen the first post here, the Fibonacci project is a learning experiment designed to highlight the similarities and differences between programming languages. In our C Fibonacci example, we learned that we need to declare variables and types, even [...]]]></description>
			<content:encoded><![CDATA[<p>So here is my second post in the Fibonacci project. For those of you that have not seen the <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c">first post here</a>, the Fibonacci project is a learning experiment designed to highlight the similarities and differences between programming languages. In our <a href="http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c">C Fibonacci example</a>, we learned that we need to declare variables and types, even the return type of a function. In this PHP example, we&#8217;ll see that PHP, while different, is very similar to C in syntax.</p>
<pre class="vci_code">
#!/usr/bin/php -q

<span class="Special">&lt;?php</span>
<span class="Comment">/**</span>
<span class="Comment"> * This is our main function. In PHP, there isn't really a need for this to be</span>
<span class="Comment"> * a function, but to keep consistant, I've placed it there anyway, and called</span>
<span class="Comment"> * it from the script. In keeping with good programming practices, this script</span>
<span class="Comment"> * exits with a status 0. Any other status would indicate an error condition.</span>
<span class="Comment"> */</span>

<span class="PreProc">function</span> <span class="Identifier">main</span><span class="Special">(){</span>

  <span class="Identifier">printf</span><span class="Special">(</span>&quot;<span class="Constant">How many numbers of the sequence would you like?</span><span class="Special">\n</span>&quot;<span class="Special">)</span>;

  <span class="Comment">/*</span>
<span class="Comment">   * To read from stdin in php, you open it as a file handle, and use</span>
<span class="Comment">   * the standard file reading functions</span>
<span class="Comment">   */</span>

  <span class="Statement">$</span><span class="Identifier">fr</span><span class="Statement">=</span><span class="Identifier">fopen</span><span class="Special">(</span>&quot;<span class="Constant">php://stdin</span>&quot;,&quot;<span class="Constant">r</span>&quot;<span class="Special">)</span>;
  <span class="Statement">$</span><span class="Identifier">n</span> <span class="Statement">=</span> <span class="Identifier">rtrim</span><span class="Special">(</span><span class="Identifier">fgets</span><span class="Special">(</span><span class="Statement">$</span><span class="Identifier">fr</span>,<span class="Constant">128</span><span class="Special">))</span>;
  <span class="Identifier">fclose</span> <span class="Special">(</span><span class="Statement">$</span><span class="Identifier">fr</span><span class="Special">)</span>;

  <span class="Comment">/* Then we call the fibonacci function */</span>
  fibonacci<span class="Special">(</span><span class="Statement">$</span><span class="Identifier">n</span><span class="Special">)</span>;
  <span class="Statement">exit</span><span class="Special">(</span><span class="Constant">0</span><span class="Special">)</span>;
<span class="Special">}</span>

<span class="Comment">/**</span>
<span class="Comment"> * The fibonacci function should look familiar.</span>
<span class="Comment"> */</span>

<span class="PreProc">function</span> fibonacci<span class="Special">(</span><span class="Statement">$</span><span class="Identifier">n</span><span class="Special">){</span>
  <span class="Comment">/* </span>
<span class="Comment">   * Notice that in PHP, we do not have to declare our variables, nor must we</span>
<span class="Comment">   * declare their types. Also see how variables in PHP are prefaced with the </span>
<span class="Comment">   * dollar sign. Other than that, this is identical to the C version of this function.</span>
<span class="Comment">   */</span>
  <span class="Statement">$</span><span class="Identifier">a</span> <span class="Statement">=</span> <span class="Constant">0</span>;
  <span class="Statement">$</span><span class="Identifier">b</span> <span class="Statement">=</span> <span class="Constant">1</span>;
  <span class="Statement">for</span> <span class="Special">(</span><span class="Statement">$</span><span class="Identifier">i</span> <span class="Statement">=</span> <span class="Constant">0</span>; <span class="Statement">$</span><span class="Identifier">i</span> <span class="Statement">&lt;</span> <span class="Statement">$</span><span class="Identifier">n</span>; <span class="Statement">$</span><span class="Identifier">i</span><span class="Statement">++</span><span class="Special">){</span>
    <span class="Identifier">printf</span><span class="Special">(</span>&quot;<span class="Constant">%d</span><span class="Special">\n</span>&quot;,<span class="Statement">$</span><span class="Identifier">a</span><span class="Special">)</span>;
    <span class="Statement">$</span><span class="Identifier">sum</span> <span class="Statement">=</span> <span class="Statement">$</span><span class="Identifier">a</span><span class="Statement">+</span><span class="Statement">$</span><span class="Identifier">b</span>;
    <span class="Statement">$</span><span class="Identifier">a</span> <span class="Statement">=</span> <span class="Statement">$</span><span class="Identifier">b</span>;
    <span class="Statement">$</span><span class="Identifier">b</span> <span class="Statement">=</span> <span class="Statement">$</span><span class="Identifier">sum</span>;
  <span class="Special">}</span>
<span class="Special">}</span>

<span class="Comment">/**</span>
<span class="Comment"> * In PHP, program execution starts here. It isn't neccessary to place functions </span>
<span class="Comment"> * ahead of the program, but it's a good practice. In a larger application, these</span>
<span class="Comment"> * functions would be in seperate files. Again, since PHP is a procedural language,</span>
<span class="Comment"> * I could have simply included the contents of the main function here, rather than</span>
<span class="Comment"> * defining a function and calling it.</span>
<span class="Comment"> */</span>

<span class="Identifier">main</span><span class="Special">()</span>;

<span class="Special">?&gt;</span>
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.4.0.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.php">fibonacci.php</a></p>
<p>You&#8217;ll notice a couple of things about this version of the Fibonacci program. Because it&#8217;s intended to be run on the command line, the first line tells the shell which interpretor to use. In addition, I have been careful here to return an exit code so that the shell knows that the program completed successfully.Stay tuned for our next installment, I&#8217;ll keep it a surprise, and even though everyone thinks it will be Ruby, I&#8217;m going to save that one for later.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-php/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Fibonacci in C</title>
		<link>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c</link>
		<comments>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c#comments</comments>
		<pubDate>Thu, 03 Jan 2008 18:20:45 +0000</pubDate>
		<dc:creator>Zachary Fox</dc:creator>
				<category><![CDATA[Fibonacci Project]]></category>

		<guid isPermaLink="false">http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c</guid>
		<description><![CDATA[Welcome to the first post of the Fibonacci project. In the following weeks, I am going to write this simple program in as many languages as possible, to highlight the similarities and differences between them. Today I will start with C, a classic language that is still in use today, and a good foundation for [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the first post of the Fibonacci project. In the following weeks, I am going to write this simple program in as many languages as possible, to highlight the similarities and differences between them. Today I will start with C, a classic language that is still in use today, and a good foundation for everything else that we will see. Rather than writing a lengthy post with each language, I&#8217;ll try to comment the code to point out the important things. Hopefully, this learning experiment will teach me something, and hopefully it will provide a handy resource for people to compare languages, even if it&#8217;s in a limited capacity.</p>
<p>So before I started, I gave myself the following &#8220;spec sheet&#8221; for writing the code:</p>
<blockquote><p>The purpose of the Fibonacci project is to create a simple programming example in as many languages as possible, to provide a clear demonstration of the differences and similarities between them.</p>
<p>The sample code should:</p>
<ul>
<li>Accept an input &#8220;x&#8221; (A value to progress up to)</li>
<li>Starting at 0 and 1, output the Fibonacci sequence up to &#8220;x&#8221; permutations.</li>
<li>There should be two functions, for clarity.
<ul>
<li>(main) Accepts the input and calls the fibonacci function.</li>
<li>(fibonacci) One that accepts the value of x and outputs the sequence.</li>
</ul>
</li>
</ul>
<p>The code should accept input from the console (stdin), with a prompt, and output to the console (stdout). This is so we keep everything simple. All of the code should be thoroughly commented.</p></blockquote>
<pre class="vci_code">
<span class="Comment">/*</span><span class="Comment">*</span>
<span class="Comment"> * You'll notice that we need to include a header file that</span>
<span class="Comment"> * contains functions we need to use. Being a compiled language,</span>
<span class="Comment"> * it's inefficient to include functions that aren't needed.</span>
<span class="Comment"> * stdio.h contains functions for reading from and writing to the console</span>
<span class="Comment"> </span><span class="Comment">*/</span>

<span class="PreProc">#include </span><span class="Constant">&lt;stdio.h&gt;</span>

<span class="Comment">/*</span><span class="Comment">*</span>
<span class="Comment"> * In C, the program executes the main function. You should also take note</span>
<span class="Comment"> * that we must declare a return type for the function. In this case, it's</span>
<span class="Comment"> * an integer, and we return 0 to indicate successful completion of the </span>
<span class="Comment"> * program.</span>
<span class="Comment"> </span><span class="Comment">*/</span>

<span class="Type">int</span> main ()
{
  <span class="Comment">/*</span><span class="Comment"> Notice that we need to declare our variables, and their type </span><span class="Comment">*/</span>

  <span class="Type">int</span> n;

  <span class="Comment">/*</span><span class="Comment"> printf prints a formated string to the stdout </span><span class="Comment">*/</span>

  printf(<span class="Constant">&quot;</span><span class="Special">\n</span><span class="Constant">How many numbers of the sequence would you like?</span><span class="Special">\n</span><span class="Constant">&quot;</span>);

  <span class="Comment">/*</span><span class="Comment"> scanf reads a formated string from the stdin. We are expecting an integer here. </span><span class="Comment">*/</span>

  scanf(<span class="Constant">&quot;</span><span class="Special">%d</span><span class="Constant">&quot;</span>,&amp;n);

  <span class="Comment">/*</span><span class="Comment"> Here we call the fibonacci function </span><span class="Comment">*/</span>

  fibonacci(n);

  <span class="Comment">/*</span><span class="Comment"> Finally, return 0 </span><span class="Comment">*/</span>

  <span class="Statement">return</span> <span class="Constant">0</span>;
}

<span class="Comment">/*</span><span class="Comment">*</span>
<span class="Comment"> * This is the simple fibonacci sequence generator. Notice also, we</span>
<span class="Comment"> * declare the type of variable we expect to be passed to the function.</span>
<span class="Comment"> </span><span class="Comment">*/</span>

<span class="Type">int</span> fibonacci(<span class="Type">int</span> n)
{
  <span class="Comment">/*</span><span class="Comment">*</span>
<span class="Comment">   * Here we declare and set our variables.</span>
<span class="Comment">   </span><span class="Comment">*/</span>
  <span class="Type">int</span> a = <span class="Constant">0</span>;
  <span class="Type">int</span> b = <span class="Constant">1</span>;
  <span class="Type">int</span> sum;
  <span class="Type">int</span> i;

  <span class="Comment">/*</span><span class="Comment">*</span>
<span class="Comment">   * Here is the standard for loop. This will step through, performing the code</span>
<span class="Comment">   * inside the braces until i is equal to n.</span>
<span class="Comment">   </span><span class="Comment">*/</span>
  <span class="Statement">for</span> (i=<span class="Constant">0</span>;i&lt;n;i++)
  {
    printf(<span class="Constant">&quot;</span><span class="Special">%d</span><span class="Special">\n</span><span class="Constant">&quot;</span>,a);
    sum = a + b;
    a = b;
    b = sum;
  }
  <span class="Statement">return</span> <span class="Constant">0</span>;
}
</pre>
<p class="vci_info">HTML code generated by <a href="http://www.zacharyfox.com/blog/free-tools/vim-color-improved">vim-color-improved v.0.4.0.</a><strong>Download this code:</strong> <a href="http://www.zacharyfox.com/fibonacci/fibonacci.c">fibonacci.c</a></p>
<p>So there&#8217;s my version of the Fibonacci sequence generator in C. I&#8217;m sure there are some problems with it, but the above code works. What would you change to make it better? Stay tuned for the next installment &#8230; PHP.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

