What is the benefit of being a surgeon?

What is functional programming and what is the benefit of learning it?

  • My questions are: 1. What is functional programming? How is it different from Object oriented programming (examples will be an added excellence )? 2. If I learn functional programming, what skill(s) will I gain? 3. What are the resources for the same? I heard 'The Little Schemer ' is a good book, any review on that? Thanks.

  • Answer:

    To a zeroth approximation, functional programming is programming without side-effects. You don't set variables.You just return values. There are two basic models of computation, the Turing Machine by Alan Turing, and the Lambda Calculus by Alonso Church. They both describe computation very well and are equivalent. The Turing Machine caught on. It's about as far from functional programming as you can get. The Lambda Calculus is pure functional programming. Typically, some sort of hybrid is used, because naive use of functional programming can do some things over and over again. Sometimes things like proper tail recursion (required by Scheme) do things behind the scenes, which let you pretend you are doing recursion, but you aren't. This improves efficiency. There are a number of reasons to learn it, apart from its being so basic in Computer Science. One it to show yourself that it's possible at all. Another thing is that it makes you magically good at a large class of parallelizable problems. Without side-effects and variable changing, most of the problems you normally would need locks for just go away. I don't know of any introductory books, but you can really do it in most languages. Just don't set member or global variables. Practice makes perfect. ETA: One more interesting thing is that languages designed for functional programming usually have many more basic ideas as first-class constructs, such as closures, anonymous functions, etc. Still, you can do anything you really want with vanilla ANSI C. It gets ugly, but you can do it.

Eric Pepke at Quora Visit the source

Was this solution helpful to you?

Other answers

Functional programming is an approach that emphasizes taking some data-thingy (structure, variable, or whatever) and passing it through well-defined black-boxes. A well rounded programmer must be able to do both. Not just understand both, they must be able to think in both: Functional programming Object oriented programming Having a robust thought perspective enables a larger range of solutions. Some solutions do well in OOP, some do better in functional, and some are best in a combined OOP/Functional architecture. When designing under a functional programming paradigm, you can think of data as a blank paper. You can then hand this "paper" to a function. This function is very specific and only takes "paper" as input and will provide very specific output, like "paper with a smiley face". A few key points: For sure, the function takes "paper" as input. For sure, the output will add "with a smiley face" to it. The function will do nothing else. This kind of approach makes a lot of sense for abstraction. You have well-defined behavior and you don't really care what happens inside of this function, our black-box. The approach makes modularity easy by making it seem like a real life workbench. You can design (and implement) any number of black-boxes that have specific input/outputs, your tools. You simply string the tools together in series or parallel, or series-that-parallel. Hammer, screw-driver, etc... It's like having "+" "-" in your workbench of modular tools. You know exactly how they will behave and your high-level code just needs to choose between all the available toys at your disposal. When designing under an object oriented approach, you think of data as a stupid robot. "Stupid robots" are told to take actions by the high-level code, if the "stupid robot" (through inheritance) knows how to do that action, then it will do it. If it doesn't, you're out of luck. The tricky thing here is the inheritance of this specific "stupid robot". If it was a robot born from "stupid robot parents" that understand the action requested as "draw a smiley face", then your in luck! If the "stupid robot" had inherited its understanding of the world from "stupid robot parents" that understood the action requested as "kill yourself", then you're out of luck. Inheritance is tricky because your "stupid robot" can also be taught to treat actions requested different than how their "stupid robot parents" may have done it. (Its called a virtual method, or sub-class overloaded method, actually... There's LOTs of names for this. You may have picked up on my bias by now.) A few key points: Objects can perform complicated tasks by adopting their parent-object's logic. Objects defined to work in a specific way can be sub-classed very easily without duplicating code (inherit from parent). This type of approach makes more intuitive sense to those learning programming for the first time. It's easy to say "mammals" breath air and eat, therefore we might sub-class a "human" based on a parent-class "mammals". It makes code reuse easy because you can write the common code once, then have multiple sub-classes just further refine what they do differently. It also makes object-to-object interactions easier to understand. You can intuitively design something like "this 'stupid robot' will act on that 'stupid robot'". Overall, it's important to master both paradigms (or any other ones you may find, or consider a paradigm). As a game developer, we use both in our architecture. Also, C/C++/C#/obj-C are designed to enable both as a mixed-paradigm language. Best way to learn? Read lots and lots of code. Draw lots and lots of architecture diagrams. Good luck.

James Liu

Here are some relevant answers I and others have written: And for a more high-brow and academic comparison of functional vs imperative programming:

Costya Perepelitsa

Functional programming is literally what it sounds like : programming with mathematical functions. In mathematics, functions are purely an input-output relationship which is constant over all time. Every time an argument is presented to the function, it will produce the same answers for equal arguments. This means that functions are not really able to change the world (i.e., no variable may be modified and no state may be updated). All programs are broken up into functions, and defined recursively with no assignment operations. Simple example: factorial 0 = 1 factorial (n+1) = (n+1)*factorial n All data structures are correspondingly defined recursively, and functions access these data structures recursively. Functional programming is a very powerful paradigm because it relies on modern mathematical thinking. It forces a clarity of thought that is generally lacking in other forms of coding, and it leads to more compact, clear and provably correct code. Developers who can adopt this clarity of thinking are much more productive in what they produce, but, frankly it is a very binary classification. People either get it or they don't. Because it is mathematical in nature, functional programming can be used to model not just every day concepts, but also very abstract mathematical concepts. It is a very liberating paradigm to work with. However, it does require a mathematical discipline over and above what most programmers are used to. Functional programming has a close relative (ancestor?) in "Functional Style" programming which does not exclude side-effects (assignments etc.) but encourages the functional style of thinking. Scheme and Lisp are in this category of languages.  Object oriented programming is a programming paradigm based on how objects should behave when they receive messages. While it is a general purpose paradigm, it rose to stardom in the world of UI's. E.g. it allows you to very easily express things like "How should this button behave when it  is clicked."  In general, it requires a concept of "object state" and updates to the object state upon receiving these messages. (e.g. Change the background of the button to red when the mouse is clicked if the button is active.). The paradigm has many different languages with their own twist on the concept, but this is what lies at the heart of it. Object oriented programming is a relatively easy way to think about the world. There are direct correlations to real and abstract constructs from the real world onto objects in the programming language (such as "customer", "button", "window") and their behavior can be defined succinctly. Developers can quickly adopt this style of thinking, although the peculiarities of individual languages can take years to master. Because of its rather restricted way of thinking about the world (objects), OOP is very good for modeling everyday concepts but it gets immensely painful to work with outside of its typical use-cases. Very few languages (of Scheme and Lisp class, and to its credit, C++)  that allow OOP as an "option." where you can choose stay out of the paradigm if so required. Which way is better? They are both powerful tools. Languages that pick one and exclude the other limit developers' creativity and effectiveness. I would highly recommend you to learn both without exception. My recommendation to everyone is to use Scheme or Common Lisp for this exact reason. It allows freedom to think and develop that is unavailable elsewhere. Start with Racket (http://www.racket-lang.org) which is a Scheme that comes with lots of libraries for all modern programming applications. There are good tutorials for you to get up to speed on it. The Little Schemer is a fantastic way to learn Scheme and the functional way of thinking in a quick question and answer style. But, make sure you learn the language with substantially sized projects. It will give you a flavor for it much better. Also by the same authors (Dan Friedman et al.) is a similar book called A Little Java, A Few Patterns, which will give you a quick introduction to OOP. A perspective on Pure Functional Programming. Once you have developed a taste for functional style thinking, you should try out Haskell. It is definitely something all modern developers must learn. Pure functional programs have an interesting attribute: they are very easily parallelizable. This is because computation not having side-effects makes partitioning and synchronization much much easier to do automatically.  We have reached a point where for the last decade or so where the speed of processors is plateauing. This means the only way to get more powerful processors is through multicore architectures. The ability of pure functional languages to easily adapt to parallel architectures is going to make them the mainstay of complex programming in the future. As a testament to this, Warp, a Haskell based web-server actually is one of the  fastest and most  scalable webservers around (see the comparison with nginx below. GHC stands for Glasgow Haskell Compiler. Source: http://aosabook.org/en/posa/warp.html).

Anurag Mendhekar

I agree with all the above but just bear in mind the opposite of functional programming is imperative programming, not object-oriented programming. Examples of imperative programming are for-next loops, mutable arrays, and printf statements. Examples of functional programming are recursion, lists, functors and IO monads. Functional programming can and is combined with object orientation in 'impure' functional languages like OCaml. Pure functional languages like Haskell force you to do things the functional way which can lose performance in some algorithms such as TCP/IP stacks where much copying is done.

Jonathan Kimmitt

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.