2. Agenda
•Functional programming (what and why)
•The basic concepts of functional programming:
• Immutability
• Memoization
• Recursion, Tail recursion
• Higher-order functions
• Currying and Partial application
• Pattern matching
2 | Functional Programming | 1 July 2013
3. Functional programming (what and why)
•What:
• no strict definition as opposed to OOP
• programming without mutable
variables, assignments, conditions, loops and other imperative
control structures
• functions should have no side-effects
• “Functions as primary building blocks” (first-class functions)
•Why:
• simpler reasoning principles (data abstraction, concurrency)
• better modularity
• much easier to exploit parallelism for multicore and cloud
computing
3 | Functional Programming | 1 July 2013
4. Immutability
•“No notion of mutation and variables”
• variable as memory cell
• rather variable bindings
•Consequences:
• different programming style with collections (Cons Nil lists)
• usually more complicated collection implementation
• knowledge of implementation details of the underlying collection is
essential
• sometimes even necessary to refactor the code/algorithm when
switching to different collection implementation
•Datomic
• immutable internal database model
4 | Functional Programming | 1 July 2013
5. Memoization
•“Function scope caching”
•Purely mathematical definition of a function
•Implementation details:
• hashmap-like data structure (Scala)
• not changing the value, but rather a reference (Racket, Clojure)
•Consequences:
• can lead to exponentially faster programs for recursive functions
• memory tradeoff
5 | Functional Programming | 1 July 2013
6. Recursion, tail recursion
•“When a function calls even indirectly itself”
•Call stacks and stack frames
•
java.lang.StackOverflowError
•“If a function calls itself as its last action, the function’s stack frame
can be reused”
•Consequences:
• JVM does not directly support tail-call optimizations
• Scala & @tailrec annotation, Clojure & recur form
• algorithms to convert recursive calls to tail recursive calls (e.g.
accumulation)
•Scala example
6 | Functional Programming | 1 July 2013
7. Higher-order functions
•“Passing or returning code/function as argument”
•Functions as first-class values
•OOP comparison:
• pretty much every OOP developer is doing “higher-order”
programming
• design patterns: Strategy, Observer, ...
• practical reasons because of JVM design
•Scala example
7 | Functional Programming | 1 July 2013
8. Currying and Partial application
•“The act of transforming a function of several arguments into a chain
of functions of one argument that will yield the same result when called
in sequence with the same arguments”
•f(x,y,z) = g(x)(y)(z)
•Consequences:
• functions because of currying can be partially applied
• language runtimes can be easier (Haskell)
•Scala example
8 | Functional Programming | 1 July 2013
9. Pattern matching
•“Decomposing data structures and ADTs”
• Abstract Data Types (tuples)
• data structures (Cons Nil lists)
•Nothing to do with switch statement
• constant-time runtime lookup over constants
•Consequences:
• no missing cases
• no exceptions for wrong variants
•SML example
9 | Functional Programming | 1 July 2013
10. Wrap up
•Functional programming is not only, but also about:
• Immutability
• Memoization
• Recursion, Tail recursion
• Higher-order functions
• Currying and Partial application
• Pattern matching
10 | Functional Programming | 1 July 2013
11. Functional programming is much more stuff…
•Influential people:
• Alonso Church
• Haskell Curry
• John McCarthy
•More advanced concepts:
• composition, functors, monads, ...
•Tons of languages:
• Clojure, F#, Lisp, OCaml, Python, Racket, Scala, Scheme, SML
11 | Functional Programming | 1 July 2013
12. Thank you for attention
Functional programming rocks