Why you should be learning functional programming today


Published on

My slide deck from my talk at 1DevDay on November 5, 2011

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • We might divide most programming languages into families of languages by various properties of the language—dynamic vs. static typing, strong vs. weak typing, early vs. late binding of data etc. One major division in programming languages is imperative vs. functional. As you might guess from the name, “imperative” is basically telling the computer what to do. Imperative languages usually have both expressions and statements. A statement is an instruction to the computer which alters the state of the program but does not return a value. A good example of a statement is the “{“ in C based languages. It delimits a scope for variables and in C++/Java/C# it also invokes constructor code for various classes. It’s hard to give a strict definition of an “imperative” language—almost need to define it anecdotally.On the other hand functional programming is rooted in the notion of applying functions to transform data to get to the desired answer. A “function” in functional is much closer to the mathematical sense of a function. As with imperative languages, it’s difficult to give a rigorous definition of functional programming. However, there are certain properties that most functional languages share:1.) There are few or no “statements” in the language. Almost all programming constructs are expressions which return values.2.) Functions are “first class citizens”. That is, functions are on the same footing as data. Functions can be passed to other functions and returned from other functions.3.) Data is immutable by default.4.) There are no side-effects to a function or the side-effects are severely limited.5.) There are anonymous functions—often called “lambdas”.6.) The language or its library will often have very powerful list processing routines built-in. Likewise the language will usually have good pattern matching primitives.7.) The language will often support the notion of lazy evaluation—that is, deferring the evaluation of expressions until it’s necessary.Within the family of functional languages there are also pure and impure functional languages. “Purity” in this sense refers to the difficulty of mutating a value. In a pure functional language—for example, Haskell—one can only mutate values in a very special construct called a monad. In an impure functional language there’s usually a way to declare a value as being mutable.
  • Why you should be learning functional programming today

    1. 1. Why You Should Be LearningFunctional Programming Today Onorio Catenacci Leader, MIGANG F# SIG
    2. 2. What We’ll Cover• Some important preliminaries• Benefits of functional programming• Drawbacks functional programming• Why start learning functional now• Additional resources
    3. 3. Preliminaries• Dimensions of Languages – “There are 10 kinds of people in the world. Those who understand binary and those who don’t.”• State• Ceremony
    4. 4. More Preliminaries• What is Functional Programming? – Attributes of functional • First class functions • Immutable by default • Lambda functions • Pattern Matching • Currying and Partial Application – Pure vs. Impure
    5. 5. Benefits of Functional Approach• Easier to reason about code• Easier to reuse code• Stateless by default• Less ceremony – Orthogonal to OO• DRY• Easier Parallel/Concurrent code
    6. 6. Drawbacks Of Functional Approach• Very foreign to most developers• Performance of certain algorithms – A bit of impurity• Harder to find skilled FP devs – Often the case with new technologies• Stateful code can be harder to write• Data structs may need to be rethought
    7. 7. Why Functional Now?• Multicore CPUs – Locking memory vs. immutable memory – Simplest thing that can possibly work is != easiest thing to do • Immutability can be simpler and less error-prone than locking.• Cloud Computing – Want to make as stateless as possible
    8. 8. Additional Resources• Can Your Programming Language Do This? – http://www.joelonsoftware.com/items/2006/08/01.html• Thinking Functionally – http://www.ibm.com/developerworks/java/library/j-ft1/• Functional Java – http://functionaljava.org/• Simple Made Easy – http://www.infoq.com/presentations/Simple-Made-Easy
    9. 9. More Resources• Functional Languages on Major Platforms: – JVM: Scala, Clojure – .Net: F#, Nemerle – Native: Haskell (pure), OCaml
    10. 10. The Takeaway• Functional can add tools to your development toolkit• You don’t need to abandon OO to use FP• With multi-core machines becoming more prevalent parallel/concurrent development will become more important• Cloud computing will also require stateless development
    11. 11. Questions? Contact Info:Onorio Catenacci, Senior Consultant, Sogeti Catenacci@ieee.org @OnorioCatenacci