Resurgence of FUNctional Programming
Words of Wisdom
No matter what language you work in, programming in a functional style provides benefits. You should do it whenever it is convenient, and you should think hard about the decision when it isn't convenient.
- John Carmack, ID Software
So
What is FUNctional Programming ?
FUNctional Programming is a list of things you can't do.
Things You can't do
- No Assignments.
- No Varying Your Variables. (Immutable)
- No While/For Loops.
- No Control Over Order of Execution.
- No Side Effects.
- No Mutating or changing state.
Are You Kidding me ?
How Can Anyone Program like this ?
So What is a better definition
FUNctional Programming ?
"Functional Programming is so called because a program consists entirely of functions."
- John Hughes , Why Functional Programming Matters
Functions
f(x) = 3x + 7
- When we evaluate f(4) = 3 * 4 + 7 = 19
- Value of 'x' does not change in function body.
- Same Input. Same Output. Every time.
-
We can replace any occurrence of f(4) with 19. (Referential Integrity)
Words of Wisdom
"The Language that does not affect the way you think about programming, is not worth knowing."
- Alan Perlis
Origin
Alonzo Church
(1903 - 1995)
( Lambda Calculus )
Imperative vs Functional
Imperative:
total = 0
i = 0
while i <= 10
total += i
i = i + 1
end
total
Functional:
total = (1..10).inject(:+)
Words of Wisdom
Functional Programming is unfamiliar territory for most
"If you want everything to be familiar you will never learn anything new"
- Rich Hickey (Author of Clojure)
Functional Programming
Concepts
First Class
&
Higher Order Functions
Higher-order functions are functions that can either take other functions as arguments or return them as results.
Higher Order Functions Enable Currying
Currying
Currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument (partial application).
add = proc { |x, y, z| x + y + z }
add5 = add.curry.call(5)
add5and6 = add5.call(6) # equal to add.curry.call(5).call(6)
add5and6.call(10) # => 21
Words of Wisdom
Functional programming is like describing your problem to a mathematician.
Imperative programming is like giving instructions to an idiot.
Tail Recursion
A recursive function is tail recursive if the final result of the recursive call is the final result of the function itself.
Non Tail Recursive:
def factorial(n)
if n == 0
return 1
else
return n * factorial(n-1)
end
end
Tail Recursive:
def factorial_one(n, total)
if n == 0
return total
else
return factorial_one(n-1, n * total)
end
end
def factorial(n)
return factorial_one(n, 1)
end
Tail Call Optimization
Process by which a smart compiler can make a call to a function taking no additional stack space.
Strict
vs
Non - Strict Evaluations
Strict evaluation always fully evaluates function arguments before invoking the function
print length([2+1, 3*2, 1/0, 5-4])
Strict Evaluation: Fails
Lazy Evaluation: 4
Lazy evaluation does not evaluate function arguments unless their values are required to evaluate the function call itself.
Things you can do
- Lock Free Concurrency.
- Brevity. (Modular Code)
- Lazy Evaluation.
- Composability.
- Parallelism.
- Improved ways of Testing.
- Referential Transparency.
- Lesser Bugs.
Disadvantages
- Steep Learning Curve.
- Cryptic Concepts (Monads)
List of Functional Languages
- Clojure
- Haskell
- Erlang
- Lisp
- Scala
- Scheme
- F#
- OCaml
Thanks