<p>In my second year of University we were "taught" Haskell, I know almost nothing about it and even less about functional programming.</p>

<p>What is functional programming, why and/xor where would I want to use it instead of non-functional programming and am I correct in thinking that C is a non-functional programming language?</p>

[{'answer_id': 24287, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 3, 'selected': False, 'text': '<p>May be worth checking out this article on <a href="http://www.code-magazine.com/Article.aspx?quickid=0809051" rel="nofollow noreferrer">F# &quot;101&quot;</a> on CoDe Mag recently posted.</p>\n<p>Also, <a href="http://diditwith.net/" rel="nofollow noreferrer">Dustin Campbell has a great blog</a> where he has posted many articles on his adventures on getting up to speed with F#..</p>\n<p>I hope you find these useful :)</p>\n<h3>EDIT:</h3>\n<p>Also, just to add, my understanding of functional programming is that <em>everything</em> is a function, or parameters to a function, rather than instances/stateful objects.. But I could be wrong F# is something I am dying to get in to but just dont have the time! :)</p>\n'}, {'answer_id': 24289, 'author': 'robintw', 'author_id': 1912, 'author_profile': 'https://Stackoverflow.com/users/1912', 'pm_score': 2, 'selected': False, 'text': '<p>Yes you are correct in thinking that C is a non-functional language. C is a procedural language.</p>\n'}, {'answer_id': 24294, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 8, 'selected': True, 'text': "<p>One key feature in a functional language is the concept of first-class functions. The idea is that you can pass functions as parameters to other functions and return them as values.</p>\n\n<p>Functional programming involves writing code that does not change state. The primary reason for doing so is so that successive calls to a function will yield the same result. You can write functional code in any language that supports first-class functions, but there are some languages, like Haskell, which do not allow you to change state. In fact, you're not supposed to make any side effects (like printing out text) at all - which sounds like it could be completely useless.</p>\n\n<p>Haskell instead employs a different approach to IO: monads. These are objects that contain the desired IO operation to be executed by your interpreter's toplevel. At any other level they are simply objects in the system.</p>\n\n<p>What advantages does functional programming provide? Functional programming allows coding with fewer potentials for bugs because each component is completely isolated. Also, using recursion and first-class functions allows for simple proofs of correctness which typically mirror the structure of the code.</p>\n"}, {'answer_id': 24301, 'author': 'John with waffle', 'author_id': 279, 'author_profile': 'https://Stackoverflow.com/users/279', 'pm_score': 2, 'selected': False, 'text': '<p>I prefer to use functional programming to save myself repeated work, by making a more abstract version and then using that instead.  Let me give an example.  In Java, I often find myself creating maps to record structures, and thus writing getOrCreate structures.</p>\n\n<pre><code>SomeKindOfRecord&lt;T&gt; getOrCreate(T thing) { \n    if(localMap.contains(thing)) { return localMap.get(thing); }\n    SomeKindOfRecord&lt;T&gt; record = new SomeKindOfRecord&lt;T&gt;(thing);\n    localMap = localMap.put(thing, record);\n    return record; \n}\n</code></pre>\n\n<p>This happens very often.  Now, in a functional language I could write</p>\n\n<pre><code>RT&lt;T&gt; getOrCreate(T thing, \n                  Function&lt;RT&lt;T&gt;&gt; thingConstructor, \n                  Map&lt;T,RT&lt;T&gt;&gt; localMap) {\n    if(localMap.contains(thing)) { return localMap.get(thing); }\n    RT&lt;T&gt; record = thingConstructor(thing);\n    localMap = localMap.put(thing,record);\n    return record; \n}\n</code></pre>\n\n<p>and I would never have to write a new one of these again, I could inherit it. But I could do one better than inheriting, I could say in the constructor of this thing</p>\n\n<pre><code>getOrCreate = myLib.getOrCreate(*,\n                                SomeKindOfRecord&lt;T&gt;.constructor(&lt;T&gt;), \n                                localMap);\n</code></pre>\n\n<p>(where * is a kind of "leave this parameter open" notation, which is a sort of currying)</p>\n\n<p>and then the local getOrCreate is exactly the same as it would have been if I wrote out the whole thing, in one line, with no inheritance dependencies.</p>\n'}, {'answer_id': 24440, 'author': 'Brian Leahy', 'author_id': 580, 'author_profile': 'https://Stackoverflow.com/users/580', 'pm_score': 2, 'selected': False, 'text': '<p>If you are looking for a good text on F#</p>\n\n<p><a href="http://www.apress.com/book/view/9781590598504" rel="nofollow noreferrer">Expert F#</a> is co-written by Don Syme.  Creator of F#.  He worked on generics in .NET specifically so he could create F#.  </p>\n\n<p>F# is modeled after OCaml so any OCaml text would help you learn F# as well.</p>\n'}, {'answer_id': 559340, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>John the Statistician's example code does not show functional programming, because when you're doing functional programming, the key is that the code does NO ASSIGNMENTS ( <code>record = thingConstructor(t)</code> is an assignment), and it has NO SIDE EFFECTS  (<code>localMap.put(record)</code> is a statement with a side effect). As a result of these two constraints, everything that a <i>function</i> does is fully captured by its arguments and its return value. Rewriting the Statistician's code the way it would have to look, if you wanted to emulate a functional language using C++:</p>\n\n<pre>\nRT getOrCreate(const T thing, \n                  const Function&lt;RT&lt;T&gt;&gt; thingConstructor, \n                  const Map&lt;T,RT&lt;T&gt;&gt; localMap) {\n    return localMap.contains(t) ?\n        localMap.get(t) :\n        localMap.put(t,thingConstructor(t));\n}\n</pre>\n\n<p>As a result of the no side-effects rule, every statement is part of the return value (hence <code>return</code> comes <i>first</i>), and every statement is an expression. In languages that enforce functional programming, the <code>return</code> keyword is implied, and the <i>if</i> statement behaves like C++'s <code>?:</code> operator.</p>\n\n<p>Also, everything is immutable, so <code>localMap.put</code> has to create a new copy of <i>localMap</i> and return it, instead of modifying the original <i>localMap</i>, the way a normal C++ or Java program would. Depending on the structure of localMap, the copy could re-use pointers into the original, reducing the amount of data that has to be copied.</p>\n\n<p>Some of the advantages of functional programming include the fact that functional programs are shorter, and it is easier to modify a functional program (because there are no hidden global effects to take into account), and it is easier to get the program right in the first place.</p>\n\n<p>However, functional programs tend to run slowly (because of all the copying they have to do), and they don't tend to interact well with other programs, operating system processes, or operating systems, which deal in memory addresses, little-endian blocks of bytes, and other machine-specific, non-functional bits. The degree of noninteroperability tends to be inversely correlated with the degree of functional purity, and the strictness of the type system. </p>\n\n<p>The more popular functional languages have really, really strict type systems. In OCAML, you can't even mix integer and floating-point math, or use the same operators (+ is for adding integers, +. is for adding floats). This can be either an advantage or a disadvantage, depending on how highly you value the ability of a type checker to catch certain kinds of bugs.</p>\n\n<p>Functional languages also tend to have really big runtime environments. Haskell is an exception (GHC executables are almost as small as C programs, both at compile-time and runtime), but SML, Common Lisp, and Scheme programs always require tons of memory.</p>\n"}, {'answer_id': 14549335, 'author': 'J D', 'author_id': 13924, 'author_profile': 'https://Stackoverflow.com/users/13924', 'pm_score': 5, 'selected': False, 'text': '<blockquote>\n  <p>What is functional programming</p>\n</blockquote>\n\n<p>There are two different definitions of "functional programming" in common use today:</p>\n\n<p>The older definition (originating from Lisp) is that functional programming is about programming using first-class functions, i.e. where functions are treated like any other value so you can pass functions as arguments to other functions and function can return functions among their return values. This culminates in the use of higher-order functions such as <code>map</code> and <code>reduce</code> (you may have heard of <code>mapReduce</code> as a single operation used heavily by Google and, unsurprisingly, it is a close relative!). The .NET types <code>System.Func</code> and <code>System.Action</code> make higher-order functions available in C#. Although currying is impractical in C#, functions that accept other functions as arguments are common, e.g. the <code>Parallel.For</code> function.</p>\n\n<p>The younger definition (popularized by Haskell) is that functional programming is also about minimizing and controlling side effects including mutation, i.e. writing programs that solve problems by composing expressions. This is more commonly called "purely functional programming". This is made possible by wildly different approaches to data structures called "purely functional data structures". One problem is that translating traditional imperative algorithms to use purely functional data structures typically makes performance 10x worse. Haskell is the only surviving purely functional programming language but the concepts have crept into mainstream programming with libraries like <code>Linq</code> on .NET.</p>\n\n<blockquote>\n  <p>where would I want to use it instead of non-functional programming</p>\n</blockquote>\n\n<p>Everywhere. Lambdas in C# have now demonstrated major benefits. C++11 has lambdas. There\'s no excuse not to use higher-order functions now. If you can use a language like F# you\'ll also benefit from type inference, automatic generalization, currying and partial application (as well as lots of other language features!).</p>\n\n<blockquote>\n  <p>am I correct in thinking that C is a non-functional programming language?</p>\n</blockquote>\n\n<p>Yes. C is a procedural language. However, you can get some of the benefit of functional programming by using function pointers and <code>void *</code> in C.</p>\n'}, {'answer_id': 34650349, 'author': 'onmyway133', 'author_id': 1418457, 'author_profile': 'https://Stackoverflow.com/users/1418457', 'pm_score': 1, 'selected': False, 'text': '<p>I find <a href="http://blog.jenkster.com/2015/12/what-is-functional-programming.html" rel="nofollow">What Is Functional Programming?</a> to be useful</p>\n\n<blockquote>\n  <p>Functional programming is about writing pure functions, about removing\n  hidden inputs and outputs as far as we can, so that as much of our\n  code as possible just describes a relationship between inputs and\n  outputs.</p>\n</blockquote>\n\n<p>Prefer explicit <code>when</code> param</p>\n\n<pre><code>public Program getProgramAt(TVGuide guide, int channel, Date when) {\n  Schedule schedule = guide.getSchedule(channel);\n\n  Program program = schedule.programAt(when);\n\n  return program;\n}\n</code></pre>\n\n<p>over</p>\n\n<pre><code>public Program getCurrentProgram(TVGuide guide, int channel) {\n  Schedule schedule = guide.getSchedule(channel);\n\n  Program current = schedule.programAt(new Date());\n\n  return current;\n}\n</code></pre>\n\n<blockquote>\n  <p>A functional language is actively hostile to side-effects. Side-effects are complexity and complexity is bugs and bugs are the devil. A functional language will help you be hostile to side-effects too.</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/24279', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1384652/']