{"id":5140,"date":"2017-01-23T06:07:35","date_gmt":"2017-01-23T06:07:35","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/the-definition-of-functional-programming\/"},"modified":"2024-07-22T16:26:52","modified_gmt":"2024-07-22T16:26:52","slug":"the-definition-of-functional-programming","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/the-definition-of-functional-programming\/","title":{"rendered":"The Definition of Functional Programming"},"content":{"rendered":"<p>The definition of functional programming is relatively easy. Functional programming is programming with mathematical functions. Is that all? Of course, not!<\/p>\n<p><!--more--><\/p>\n<h2>Mathematical functions<\/h2>\n<p><strong>Functional programming is programming with mathematical functions.<\/strong>\u00a0 I think you already guess it. The key to this definition is the mathematical expression function. Mathematical functions are functions that return every time the same result when given the same arguments. They behave like an infinite big lookup table.<\/p>\n<h3>Referential transparency<\/h3>\n<p>Referential transparency is the property that a function (expression) always returns the same result when given the same arguments. Referential transparency has far-reaching consequences:<\/p>\n<ul>\n<li>Mathematical functions can not have a side effect and can, therefore, not change the state outside the function body.<\/li>\n<li>The function call can be replaced with its result but reordered or put on a different thread.<\/li>\n<li>The program flow is defined by the data dependencies and not by the sequence of instructions.<\/li>\n<li>Mathematical functions are much easier to refactor and test because you can reason about the function in isolation.<\/li>\n<\/ul>\n<p>That sounds very promising. But with so many advantages, there is a massive restriction. Mathematical functions can not talk to the outside world. Examples?<\/p>\n<p>Mathematical functions can&#8217;t<\/p>\n<ul>\n<li>get user input or read from files.<\/li>\n<li>write to the console or into a file.<\/li>\n<li>return random numbers or time points because the return values are different.<\/li>\n<li>build a state.<\/li>\n<\/ul>\n<p>Thanks to mathematical functions, the definition of functional is concise but does not help so much. The critical question remains. How can you program something useful with functional programming? Mathematical functions are like islands that have no communication with the outside world. Or to say it in the words of\u00a0 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Simon_Peyton_Jones\">Simon Peyton Jones<\/a>, one of the fathers of Haskell. The only effect that mathematical functions can have is to warm up your room.<\/p>\n<p>Now, I will be a little bit more elaborated. What are the characteristics of functional programming languages?<\/p>\n<h2>Characteristics of functional programming languages<\/h2>\n<p>Haskell will significantly help me on my tour through the characteristics of functional programming.<\/p>\n<h3>Haskell<\/h3>\n<p>There are two reasons for using Haskell.<\/p>\n<ol>\n<li>Haskell is a purely functional programming language, and therefore you can study very well the characteristics of functional programming by using Haskell.<\/li>\n<li>Haskell may be the most influential programming language of the last 10 &#8211; 15 years.<\/li>\n<\/ol>\n<p>My second statement needs proof. I will provide them in the next post for Python and C++. Therefore, here are a few words about Java, Scala, and C#.<\/p>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Philip_Wadler\">Philip Wadler<\/a>, another father of\u00a0 Haskell, was one of the implementors of generics in Java.<\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Martin_Odersky\">Martin Odersky<\/a>, the father of Scala, who adapted a lot from Haskell, was also involved in implementing generics in Java.<\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Erik_Meijer_(computer_scientist)\">Erik Meijer <\/a>is a passionate admirer and researcher around Haskell. He used the Haskell concepts of monads and created the well-known C# library <a href=\"https:\/\/en.wikipedia.org\/wiki\/Language_Integrated_Query\">LINQ.<\/a><\/li>\n<\/ul>\n<p>I will even go one step further. Who knows functional programming, and in particular Haskell, knows how the mainstream programming languages will develop in the following years? Even a <em>pure <\/em>object-oriented language like Java can not withstand the pressure of functional ideas. Java now has generics and lambda expressions.<\/p>\n<p>But now, back to my subject. What are the characteristics of functional programming languages?<\/p>\n<h3>Characteristics<\/h3>\n<p>In my search for functional characteristics, I identified seven typical properties. These must not be all characteristics, and each functional programming language has not supported them. However, the characteristics help to bring meat to the abstract definition of functional programming.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5139\" style=\"margin: 15px;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/CharakteristikenFunktionaleProgrammierungEng.png\" alt=\"CharakteristikenFunktionaleProgrammierungEng\" width=\"441\" height=\"451\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/CharakteristikenFunktionaleProgrammierungEng.png 441w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/CharakteristikenFunktionaleProgrammierungEng-293x300.png 293w\" sizes=\"auto, (max-width: 441px) 100vw, 441px\" \/><\/p>\n<p>The graphic outlines the characteristics of functional programming and my next posts. I will provide many examples in Haskell, C++, and Python. But what do the seven characteristics mean?<\/p>\n<p><strong>First-class Functions <\/strong>are typical for functional programming languages. These functions can accept functions as argument or return functions. Therefore, the functions have to be <strong>higher-order functions<\/strong>. That means they behave like data.\u00a0 <strong>Pure functions<\/strong> always return the same result when given the same arguments and can not have a side effect. They are the reason that Haskell is called a pure functional language. A purely functional language has only <strong>immutable data<\/strong>. That means they can not have a <span style=\"font-family: courier new,courier;\">while<\/span> or <span style=\"font-family: courier new,courier;\">for<\/span> loop based on a counter. Instead of the loops, they use <strong>recursion<\/strong>. The essential characteristic of functional programming is that you can easily compose functions. This is because of their bread-and-butter data structure <strong>list<\/strong>. If an expression evaluates its arguments immediately, it&#8217;s called greedy or eager evaluation. If the expression evaluates the arguments only if needed, it&#8217;s called <strong>lazy evaluation<\/strong>. Lazy evaluation will reduce time and memory if the evaluated expression is unnecessary. I think you already guessed it. Classical programming languages are greedy. They evaluate their expressions immediately.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>In my next post, I will start with first-class functions, which we have had since the beginning of C++.<\/p>\n<div id=\"s3gt_translate_tooltip\" class=\"s3gt_translate_tooltip\" style=\"position: absolute; left: 10px; top: 272px; opacity: 0.55;\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The definition of functional programming is relatively easy. Functional programming is programming with mathematical functions. Is that all? Of course, not!<\/p>\n","protected":false},"author":21,"featured_media":5139,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[365],"tags":[],"class_list":["post-5140","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-functional"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5140"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5140\/revisions"}],"predecessor-version":[{"id":9769,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5140\/revisions\/9769"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5139"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}