<?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: thealmarty</title>
    <description>The latest articles on DEV Community by thealmarty (@thealmarty).</description>
    <link>https://dev.to/thealmarty</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%2F58803%2Fe9386aaf-29d4-4e54-9ada-4f5c92a14524.jpg</url>
      <title>DEV Community: thealmarty</title>
      <link>https://dev.to/thealmarty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thealmarty"/>
    <language>en</language>
    <item>
      <title>Recursion: An Indispensable Tool For Every Functional Programmer [Example With Insertion Sort]</title>
      <dc:creator>thealmarty</dc:creator>
      <pubDate>Tue, 13 Apr 2021 15:35:21 +0000</pubDate>
      <link>https://dev.to/thealmarty/recursion-an-indispensable-tool-for-every-functional-programmer-example-with-insertion-sort-50ek</link>
      <guid>https://dev.to/thealmarty/recursion-an-indispensable-tool-for-every-functional-programmer-example-with-insertion-sort-50ek</guid>
      <description>&lt;p&gt;This is the first of a series of articles that illustrates &lt;em&gt;functional programming&lt;/em&gt; (FP) concepts to imperative programmers. As you go through these articles with code examples in Haskell (one of the most popular FP languages), you gain the grounding for picking up any FP languages quickly. Why should you care about FP? See &lt;a href="https://thealmarty.com/2021/04/13/recursion-an-indispensable-tool-for-every-functional-programmer-example-with-insertion-sort/"&gt;this post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One of the most important concepts is the use of &lt;em&gt;recursion&lt;/em&gt; in place of loops. Loops are an important tool for imperative programmer to perform repeated actions. Functional programmers use recursion (calling the function itself) instead. Below, I use the &lt;em&gt;insertion sort algorithm&lt;/em&gt; as an example. I first implement it in imperative style, then I implement it in functional style in Haskell.&lt;/p&gt;

&lt;h2&gt;
  
  
  Insertion Sort
&lt;/h2&gt;

&lt;p&gt;I’ve chosen insertion sort as an example algorithm to illustrate how loops and recursion serve similar purposes. Of course, many languages have a sorting algorithm built-in, so most of the time you don’t need to write your own.&lt;/p&gt;

&lt;p&gt;The insertion sort algorithm can be described as: separate the given list of elements into two list: one sorted, one unsorted. The sorted list initially contains the first element of the given list while the unsorted list initially contains the rest of the elements. In each step, take the first element from the unsorted list, compare it with each element of the sorted list, starting from the right most (or left most) element. Insert the element right after the element it should follow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Imperative style implementation
&lt;/h3&gt;

&lt;p&gt;Below is the pseudocode from &lt;a href="https://en.wikipedia.org/wiki/Insertion_sort"&gt;Wikipedia&lt;/a&gt;. See also the &lt;a href="https://gist.github.com/4401a113108da4d5cbfb8fbee800b63a"&gt;C&lt;/a&gt;, &lt;a href="https://gist.github.com/thealmarty/f88189688e26db735a0aec83ad8e6ce3"&gt;C++&lt;/a&gt;, and &lt;a href="https://github.com/thealmarty/ocaml_algorithms/blob/master/sort/imperative_insertion_sort.ml"&gt;OCaml&lt;/a&gt; implementation. Don’t worry about understanding the code line by line, the main point is that we use two loops for the algorithm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i ← 1
while i &amp;lt; length(A)
    j ← i
    while j &amp;gt; 0 and A[j-1] &amp;gt; A[j]
        swap A[j] and A[j-1]
        j ← j - 1
    end while
    i ← i + 1
end while

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that &lt;code&gt;insertion_sort&lt;/code&gt; is a function that takes an array &lt;code&gt;A&lt;/code&gt; and sorts it &lt;strong&gt;&lt;em&gt;in place&lt;/em&gt;&lt;/strong&gt; , that is, it rearranges the elements in &lt;code&gt;A&lt;/code&gt; as the function runs. When you run &lt;code&gt;insertion_sort&lt;/code&gt; with array &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;A&lt;/code&gt; becomes the sorted array, this is not the case in FP.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recursion
&lt;/h2&gt;

&lt;p&gt;Instead of using loops for repeated actions, FPers use recursion. &lt;em&gt;A recursive function calls itself as part of its own function definition!&lt;/em&gt; It’s common to recurse on the &lt;em&gt;tail&lt;/em&gt; of a list. The tail is the whole list without the first element (the &lt;em&gt;head&lt;/em&gt;). Like loops, recursion requires a terminating condition. Because the tail is guaranteed to have one fewer element than the original input, we know that as it recurses its input is smaller and thus it can terminate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functional style implementation in Haskell
&lt;/h3&gt;

&lt;p&gt;The Haskell implementation includes &lt;strong&gt;recursive functions &lt;code&gt;sort&lt;/code&gt; and &lt;code&gt;insert&lt;/code&gt;&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sort :: Ord a =&amp;gt; [a] -&amp;gt; [a]
sort l = sortInto [] l -- initially all elements are unsorted
  where
    sortInto :: Ord a =&amp;gt; [a] -&amp;gt; [a] -&amp;gt; [a]
    sortInto sorted [] = sorted
    sortInto sorted (l:ls) = sortInto (insert sorted l) ls

    insert :: Ord a =&amp;gt; [a] -&amp;gt; a -&amp;gt; [a]
    insert [] elem = [elem]
    insert sorted@(s:ss) elem | elem &amp;gt; s = s : insert ss elem
                              | otherwise = elem : sorted

egL :: [Int]
egL = [23,2,5]
main = putStrLn $ show (sort egL) ++ show egL

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;sort&lt;/code&gt; is declared with its types first. &lt;code&gt;sort&lt;/code&gt; is a function that concerns a type &lt;code&gt;a&lt;/code&gt; that can be ordered (hence &lt;code&gt;Ord a&lt;/code&gt; before &lt;code&gt;=&amp;gt;&lt;/code&gt;). &lt;code&gt;sort&lt;/code&gt; takes a list with elements of type &lt;code&gt;a&lt;/code&gt; and returns a list with elements of type &lt;code&gt;a&lt;/code&gt;. That is, instead of sorting the list in place, &lt;strong&gt;&lt;code&gt;sort&lt;/code&gt; returns a new list!&lt;/strong&gt; &lt;code&gt;sort&lt;/code&gt; calls &lt;code&gt;sortInto&lt;/code&gt; to perform the function of in the outer loop in the imperative implementation.&lt;/p&gt;

&lt;p&gt;Inside &lt;code&gt;sort&lt;/code&gt; we also define &lt;code&gt;insert&lt;/code&gt;, which performs the function of the inner loop in the imperative implementation. But instead of calling each element by position like the &lt;code&gt;while&lt;/code&gt; loop, &lt;code&gt;insert&lt;/code&gt; is a function that takes two arguments, a sorted list, &lt;code&gt;sorted&lt;/code&gt; and an element that is to be inserted in the correct position, &lt;code&gt;elem&lt;/code&gt;. The function compares the element with the sorted list head, &lt;code&gt;s&lt;/code&gt;, and the rest of the sorted list, &lt;code&gt;ss&lt;/code&gt;, is passed to the recursive call to continue with the next comparison. The recursion terminates when either&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;elem&lt;/code&gt; is not greater than the next element of the sorted list, and is inserted in front of that element. Or – &lt;code&gt;elem&lt;/code&gt; is being inserted at the end of the list. The first pattern match case. The resulting list of this call contains the single element &lt;code&gt;elem&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;sortInto&lt;/code&gt; is also a recursive function defined inside &lt;code&gt;sort&lt;/code&gt;. It takes as arguments a sorted list, and an unsorted list. Since &lt;code&gt;sorted&lt;/code&gt; is a sorted list, we can put it in as an input to &lt;code&gt;insert&lt;/code&gt; so that &lt;code&gt;insert&lt;/code&gt; can insert the first element &lt;code&gt;l&lt;/code&gt; of the unsorted list properly. &lt;code&gt;sortInto&lt;/code&gt; then recursively calls itself to insert the rest of the elements of the unsorted list in to the sorted list returned by &lt;code&gt;insert&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sort&lt;/code&gt; calls &lt;code&gt;sortInto&lt;/code&gt; with an empty sorted list, and the whole unsorted list as the unsorted list.&lt;/p&gt;

&lt;p&gt;Save the above code in a file named &lt;code&gt;main.hs&lt;/code&gt;. After you install GHC, you can run it in a terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; runhaskell main.hs
[2,5,23][23,2,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that after running &lt;code&gt;sort egL&lt;/code&gt;, &lt;code&gt;egL&lt;/code&gt; stays unchanged. That is, FP preserves the inputs.&lt;/p&gt;

&lt;p&gt;In this post, you’ve learned quite a few important concepts! We’re more aware of &lt;strong&gt;types&lt;/strong&gt; , we learned that &lt;strong&gt;a function takes an input and gives an output&lt;/strong&gt; (instead of performing actions &lt;em&gt;in place&lt;/em&gt;), and we learned &lt;strong&gt;recursion&lt;/strong&gt;. In the next post, I will show a glimpse of the power of FP with higher order functions, which gives the FPer tremendous expressive powers and enables many awesome and concise ways to describe algorithms. For example, &lt;strong&gt;recursion schemes&lt;/strong&gt; , which let you describe recursion in a single line of code.&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://thealmarty.com/2021/04/13/recursion-an-indispensable-tool-for-every-functional-programmer-example-with-insertion-sort/"&gt;Recursion: An Indispensable Tool For Every Functional Programmer [Example With Insertion Sort]&lt;/a&gt; appeared first on &lt;a href="https://thealmarty.com"&gt;The Always Learning Marty&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>fpseries</category>
      <category>functionalprogrammin</category>
      <category>haskell</category>
    </item>
    <item>
      <title>Why you should learn functional programming</title>
      <dc:creator>thealmarty</dc:creator>
      <pubDate>Wed, 10 Mar 2021 22:06:52 +0000</pubDate>
      <link>https://dev.to/thealmarty/why-you-should-learn-functional-programming-1gp0</link>
      <guid>https://dev.to/thealmarty/why-you-should-learn-functional-programming-1gp0</guid>
      <description>&lt;p&gt;Many of the widely used languages (including C++, Java, and Javascript) are imperative.  In imperative programming, computations are structured as sequences of instructions that operate by making modifications to the state of the program.  Functional languages operate by declaring functions. The output value of a function depends only on the arguments that are passed to the function.  Because of this, functional languages have a few advantages that minimize mistakes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Sometimes, the elegant implementation is a function.&lt;br&gt;&lt;br&gt;
Not a method. Not a class. Not a framework. Just a function.”&lt;br&gt;&lt;br&gt;
–John Carmack&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Until recently, functional languages were considered “academic” and for research only. But the industry has since discovered their advantages and many leaders are adopting functional programming to gain competitiveness.&lt;/p&gt;

&lt;p&gt;In this post, I’ll list a few reasons why you should learn functional programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  It makes you a better programmer
&lt;/h2&gt;

&lt;p&gt;Functional programming, especially &lt;em&gt;typed&lt;/em&gt; functional programming, offers a very different mental toolbox than the traditional programmer usually uses. For example:&lt;/p&gt;

&lt;h3&gt;
  
  
  Immutability
&lt;/h3&gt;

&lt;p&gt;Unlike in imperative languages, in functional languages, variables are immutable by default, and do not depend on the state of the program.  Along with referencial transparency (given the same inputs, a function always return the same results), this increases consistency. As a result, we have reduced errors, increased stability, and increased effectiveness of tests. For example, tools like &lt;a href="https://hackage.haskell.org/package/QuickCheck"&gt;QuickCheck&lt;/a&gt; is very effective.&lt;/p&gt;

&lt;h3&gt;
  
  
  Type safety
&lt;/h3&gt;

&lt;p&gt;Modern functional languages are usually statically typed, which means that they type check (verify all functions have the correct input types passed to them and return the correct type) at compile-time as opposed to run-time.  This ensures that all program in production are type safe.  Even though some imperative languages are also statically typed, they don’t offer as many safety guarantees as modern functional languages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Memory safety
&lt;/h3&gt;

&lt;p&gt;Functional programming languages handle allocation and de-allocation for you. The compiler avoids many common memory leaks that imperative programmers see and completely removes the risk of null dereferences.&lt;/p&gt;

&lt;h3&gt;
  
  
  Purity
&lt;/h3&gt;

&lt;p&gt;In pure functional languages like Haskell, you can prove the absence of unwanted side-effects.  Because pure functional languages operates with functions that are type safe, you can control whether a function in the code has any interaction with the outside world more easily.&lt;/p&gt;

&lt;p&gt;And many more! See &lt;a href="https://www.foxhound.systems/blog/why-haskell-for-production/"&gt;here&lt;/a&gt; and &lt;a href="http://book.realworldhaskell.org/read/why-functional-programming-why-haskell.html"&gt;here&lt;/a&gt; for more details.&lt;/p&gt;

&lt;p&gt;These new tools and perspectives empower you to write better programs even when you write in traditional languages. In fact, many modern languages/extensions/frameworks have functional flavours added. See for example &lt;a href="https://www.rust-lang.org/"&gt;Rust&lt;/a&gt;, &lt;a href="https://reasonml.github.io/"&gt;ReasonML&lt;/a&gt; and &lt;a href="https://www.typescriptlang.org/"&gt;typescript&lt;/a&gt;. Learning functional programming will give you the necessary building blocks to pick up these framework quickly and correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  It’s enjoyable
&lt;/h2&gt;

&lt;p&gt;A far smaller cognitive load is required when writing functional rather than imperative code. As mentioned above, in functional programming the compiler type checks and ensures memory safety. In additional, functions have no side effects. Therefore, many concerns present in imperative languages can be completely offloaded to the compiler. With this tedious cognitive load freed up, functional programmer can focus on the more fun part of programming: designing the implementation! When we develop in functional languages we often write much less code, in substantially less time, and with fewer bugs.&lt;/p&gt;

&lt;h2&gt;
  
  
  It gives you great career prospects
&lt;/h2&gt;

&lt;p&gt;Many companies have adopted functional programming and there are many opportunities for functional programmers. Companies using Haskell include Facebook, &lt;a href="https://hasura.io/"&gt;Hasura&lt;/a&gt;, etc. Companies using OCaml include &lt;a href="https://ahrefs.com/"&gt;Ahrefs&lt;/a&gt;, &lt;a href="https://www.janestreet.com/"&gt;Jane Street&lt;/a&gt;, etc. Check out &lt;a href="https://blockchain.works-hub.com/"&gt;Functional Works&lt;/a&gt; for many more companies that are using functional languages!&lt;/p&gt;

&lt;p&gt;Functional languages are prominent in blockchain especially. Blockchain is an increasing popular technology with applications in many areas.  The most common uses of the technology include cryptocurrencies, banking/FinTech, and smart contracts. They all involve financial transactions that are time sensitive and mistakes can be very costly.  Functional languages can minimize these mistakes and therefore many blockchain and related applications are written in functional languages! For example, &lt;a href="https://minaprotocol.com/"&gt;Mina&lt;/a&gt; and &lt;a href="https://tezos.com/"&gt;Tezos&lt;/a&gt; are written in OCaml. &lt;a href="https://cardano.org/"&gt;Cardano&lt;/a&gt;, &lt;a href="https://www.kadena.io/"&gt;Kadena&lt;/a&gt; and &lt;a href="https://blockapps.net/"&gt;BlockApps&lt;/a&gt; are written in Haskell.&lt;/p&gt;

&lt;p&gt;Check out &lt;a href="https://blockchain.works-hub.com/"&gt;Blockchain Works&lt;/a&gt; for many more blockchain related companies that are using functional languages!&lt;/p&gt;

&lt;p&gt;In conclusion, functional languages have advantages over imperative languages. They are already well-used in the blockchain world, and are really catching on in other areas as well. Whether you want to stay up-to-date, expand your career choices, or broaden your programming knowledge, learning functional programming is the ideal choice!&lt;/p&gt;

&lt;p&gt;If I’ve got you convinced, I’ve got more good news for you! I’m going to publish a series of posts introducing functional programming concepts. You don’t need to have background in functional programming or even programming. I’ll go through practical concepts from beginner to advanced levels with many code examples. Stay tuned!&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://thealmarty.com/2021/03/10/why-you-should-learn-functional-programming/"&gt;Why you should learn functional programming&lt;/a&gt; appeared first on &lt;a href="https://thealmarty.com"&gt;The Always Learning Marty&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>fpseries</category>
    </item>
  </channel>
</rss>
