<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: PatrickBuhagiar</title>
    <description>The latest articles on DEV Community by PatrickBuhagiar (@patrickbuhagiar).</description>
    <link>https://dev.to/patrickbuhagiar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F219857%2F23792055-00bd-453d-96ef-783e11c18a11.jpg</url>
      <title>DEV Community: PatrickBuhagiar</title>
      <link>https://dev.to/patrickbuhagiar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/patrickbuhagiar"/>
    <language>en</language>
    <item>
      <title>[Discuss] What's your favourite Fibonacci implementation and in which language? Mine is in Haskell</title>
      <dc:creator>PatrickBuhagiar</dc:creator>
      <pubDate>Fri, 30 Aug 2019 00:27:57 +0000</pubDate>
      <link>https://dev.to/patrickbuhagiar/discuss-what-s-your-favourite-fibonacci-implementation-and-in-what-language-mine-is-in-haskell-1kem</link>
      <guid>https://dev.to/patrickbuhagiar/discuss-what-s-your-favourite-fibonacci-implementation-and-in-what-language-mine-is-in-haskell-1kem</guid>
      <description>&lt;p&gt;A few years ago, in my first grad interview, I recall being asked to implement Fibonacci in two different approaches. Back then, I could only think of recursive  and iterative approaches in Java. My naïve, young self could not fathom that other approaches could exist. &lt;/p&gt;

&lt;p&gt;As the years gone by, I've come across many implementations in different languages and paradigms. This however, has got to be my favourite, in good old Haskell:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
When I was first learning Haskell, it took me quite a while to wrap my head around what is going on here. I think it's my favourite implementation because it is a good example of how to use infinite streams and lazy loading. 

&lt;p&gt;&lt;strong&gt;What's your favourite Fibonacci sequence, in what language, and why?&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Don't know Haskell? I'll explain.
&lt;/h2&gt;

&lt;p&gt;The key bit to understand here is what &lt;code&gt;zipWith&lt;/code&gt; does. &lt;code&gt;zipWith&lt;/code&gt;, which returns an array, incrementally takes an element from two supplied arrays, and performs an operation on them. In this case, I am adding them up with the addition operator. Below is an example of what happens if I &lt;code&gt;zipWith&lt;/code&gt; arrays &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BFXD04U5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/68w8f43b37a89rb4psyk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BFXD04U5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/68w8f43b37a89rb4psyk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Haskell, the &lt;code&gt;:&lt;/code&gt; syntax allows you to concatenate elements or arrays into a single array. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;fibs&lt;/code&gt; and &lt;code&gt;fibs'&lt;/code&gt; are two functions that I've defined. &lt;code&gt;fibs&lt;/code&gt; returns an array with &lt;code&gt;1&lt;/code&gt; as its head, and the content of &lt;code&gt;fibs'&lt;/code&gt; as its tail. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;fibs'&lt;/code&gt; also returns an array that starts with &lt;code&gt;1&lt;/code&gt;, but, as a tail, performs &lt;code&gt;zipWith&lt;/code&gt; on &lt;code&gt;fibs&lt;/code&gt; and itself with the addition operator! Confusing? Here's a step by step breakdown of how the whole thing gets evaluated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;fibs&lt;/span&gt; &lt;span class="c1"&gt;-- we're calling "fibs", which expands to...&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fibs'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;-- but fibs' expands to...&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;zipWith&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;fibs&lt;/span&gt; &lt;span class="n"&gt;fibs'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;-- recursively we are going keep expanding&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;zipWith&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fibs'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zipWith&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;fibs&lt;/span&gt; &lt;span class="n"&gt;fibs'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; 
&lt;span class="c1"&gt;-- let's add the first elements in the two arrays supplied for the first zipWith&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;zipWith&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fibs'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;zipWith&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;fibs&lt;/span&gt; &lt;span class="n"&gt;fibs'&lt;/span&gt;&lt;span class="p"&gt;])]&lt;/span&gt;
&lt;span class="c1"&gt;-- and this will go on for infinity!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Another way to think about what is going on here is that we are essentially having two identical infinite Fibonacci arrays, however one is offset by one index. As a result of this offset, &lt;code&gt;zipWith&lt;/code&gt; would achieve the same effect as adding the two last numbers in the sequence. The initial two ones in the sequence are added before performing &lt;code&gt;zipWith&lt;/code&gt;.&lt;/em&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZvJH7lo0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ye09zzw56ihgvamurf21.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZvJH7lo0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ye09zzw56ihgvamurf21.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I know what you're thinking. Sure, this would go on to infinity and blow up memory, however Haskell uses lazy loading which means values are only evaluated when needed. The last part of the this implementation is to use &lt;code&gt;take 10 fibs&lt;/code&gt;, which basically returns the first 10 elements of the fibonacci sequence.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>functional</category>
      <category>todayilearned</category>
      <category>haskell</category>
    </item>
  </channel>
</rss>
