SlideShare a Scribd company logo
1 of 91
Download to read offline
An Introduction to Functional Programming
Andreas Pauley – @apauley
Pattern Matched Technologies
Lambda Luminaries @lambdaluminary
September 2, 2013
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 1 / 58
Pattern Matched Technologies
Developing financial applications in Erlang.
http://www.patternmatched.com/
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 2 / 58
Lambda Luminaries – @lambdaluminary
Local functional programming user group
We meet once a month, on the second Monday of the month.
http://www.meetup.com/lambda-luminaries/
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 3 / 58
Introduction
Table of Contents
1 Introduction
2 Definition of Functional Programming
3 Function Recap
4 Common FP Idioms
Recursion
Pattern Matching
Higher-order Functions
5 Higher-order Functions and Lists
6 Advantages of Functional Programming
7 Disdvantages of Functional Programming
8 Companies using Functional Programming
9 Where do we go from here?
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 4 / 58
Introduction
A word from the wise
“ 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 [2]
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 5 / 58
Introduction
Quake
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 6 / 58
Introduction
A Few Functional Programming Languages
Haskell Strong focus on functional purity. Lazy evaluation.
Advanced static type system. Native-code compiler.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 7 / 58
Introduction
A Few Functional Programming Languages
Haskell Strong focus on functional purity. Lazy evaluation.
Advanced static type system. Native-code compiler.
Erlang Focused around concurrency and distributed programming.
Strict evaluation. Dynamic typing. Runs on the BEAM VM.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 7 / 58
Introduction
A Few Functional Programming Languages
Haskell Strong focus on functional purity. Lazy evaluation.
Advanced static type system. Native-code compiler.
Erlang Focused around concurrency and distributed programming.
Strict evaluation. Dynamic typing. Runs on the BEAM VM.
Clojure A modern Lisp language. Focus on concurrency. Lazy
evaluation. Dynamic typing. Runs on the JVM.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 7 / 58
Introduction
A Few Functional Programming Languages
Haskell Strong focus on functional purity. Lazy evaluation.
Advanced static type system. Native-code compiler.
Erlang Focused around concurrency and distributed programming.
Strict evaluation. Dynamic typing. Runs on the BEAM VM.
Clojure A modern Lisp language. Focus on concurrency. Lazy
evaluation. Dynamic typing. Runs on the JVM.
Scala FP and OO. Strict evaluation by default, but supports lazy
evaluation. Advanced static type system. Runs on the JVM.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 7 / 58
Introduction
A Few Functional Programming Languages
Haskell Strong focus on functional purity. Lazy evaluation.
Advanced static type system. Native-code compiler.
Erlang Focused around concurrency and distributed programming.
Strict evaluation. Dynamic typing. Runs on the BEAM VM.
Clojure A modern Lisp language. Focus on concurrency. Lazy
evaluation. Dynamic typing. Runs on the JVM.
Scala FP and OO. Strict evaluation by default, but supports lazy
evaluation. Advanced static type system. Runs on the JVM.
OCaml FP and OO. Part of the ML family. Sometimes claimed to
be “faster than C”. Strict evaluation. Advanced static type
system. Native-code compiler.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 7 / 58
Introduction
A Few Functional Programming Languages
Haskell Strong focus on functional purity. Lazy evaluation.
Advanced static type system. Native-code compiler.
Erlang Focused around concurrency and distributed programming.
Strict evaluation. Dynamic typing. Runs on the BEAM VM.
Clojure A modern Lisp language. Focus on concurrency. Lazy
evaluation. Dynamic typing. Runs on the JVM.
Scala FP and OO. Strict evaluation by default, but supports lazy
evaluation. Advanced static type system. Runs on the JVM.
OCaml FP and OO. Part of the ML family. Sometimes claimed to
be “faster than C”. Strict evaluation. Advanced static type
system. Native-code compiler.
F# FP and OO. Based on OCaml. Strict evaluation by default,
but supports lazy evaluation. Advanced static type system.
Runs on the .NET CLR.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 7 / 58
Definition of Functional Programming
Table of Contents
1 Introduction
2 Definition of Functional Programming
3 Function Recap
4 Common FP Idioms
Recursion
Pattern Matching
Higher-order Functions
5 Higher-order Functions and Lists
6 Advantages of Functional Programming
7 Disdvantages of Functional Programming
8 Companies using Functional Programming
9 Where do we go from here?
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 8 / 58
Definition of Functional Programming
So what exactly is Functional Programming?
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 9 / 58
Definition of Functional Programming
Functional Programming, noun:
“ Functional Programming is a list
of things you CAN’T do. ”
[7]
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 10 / 58
Definition of Functional Programming
When programming in a functional style/language:
You can’t vary your variables.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 11 / 58
Definition of Functional Programming
When programming in a functional style/language:
You can’t vary your variables.
You can’t mutate or change your state.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 11 / 58
Definition of Functional Programming
When programming in a functional style/language:
You can’t vary your variables.
You can’t mutate or change your state.
No while/for loops, sorry.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 11 / 58
Definition of Functional Programming
When programming in a functional style/language:
You can’t vary your variables.
You can’t mutate or change your state.
No while/for loops, sorry.
You can’t have side-effects.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 11 / 58
Definition of Functional Programming
When programming in a functional style/language:
You can’t vary your variables.
You can’t mutate or change your state.
No while/for loops, sorry.
You can’t have side-effects.
You can’t control the order of execution (lazy evaluated languages).
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 11 / 58
Definition of Functional Programming
Are you kidding me?
How can anyone program like this???
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 12 / 58
Definition of Functional Programming
GOTO 10
This sounds like
“You can’t have GOTO statements”
See Hughes and Dijkstra [1, 3]
Also framed in the negative (you can’t).
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 13 / 58
Definition of Functional Programming
GOTO 10
This sounds like
“You can’t have GOTO statements”
See Hughes and Dijkstra [1, 3]
Also framed in the negative (you can’t).
In hindsight we don’t really need GOTO’s.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 13 / 58
Definition of Functional Programming
GOTO 10
This sounds like
“You can’t have GOTO statements”
See Hughes and Dijkstra [1, 3]
Also framed in the negative (you can’t).
In hindsight we don’t really need GOTO’s.
In hindsight it is not about what you cannot do.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 13 / 58
Definition of Functional Programming
Structured Programming and Functional Programming
Structured Programming introduced subroutines with fixed entry/exit
points (instead of GOTO’s), resulting in improved
modularity.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 14 / 58
Definition of Functional Programming
Structured Programming and Functional Programming
Structured Programming introduced subroutines with fixed entry/exit
points (instead of GOTO’s), resulting in improved
modularity.
Functional Programming provides similar important benefits – more about
this later.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 14 / 58
Definition of Functional Programming
We need a better definition for Functional Programming.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 15 / 58
Definition of Functional Programming
Functional Programming, noun:
“ Functional programming is so called because a program
consists entirely of functions. ”
— John Hughes, Why Functional Programming Matters [1, p. 1]
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 16 / 58
Definition of Functional Programming
OK... so what exactly is a function?
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 17 / 58
Function Recap
Table of Contents
1 Introduction
2 Definition of Functional Programming
3 Function Recap
4 Common FP Idioms
Recursion
Pattern Matching
Higher-order Functions
5 Higher-order Functions and Lists
6 Advantages of Functional Programming
7 Disdvantages of Functional Programming
8 Companies using Functional Programming
9 Where do we go from here?
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 18 / 58
Function Recap
An example function
f (x) = 2x + 3
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 19 / 58
Function Recap
Variables in functions
f (x) = 2x + 3
When we evaluate the function:
f (4) = 2 ∗ 4 + 3 = 11
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 20 / 58
Function Recap
Variables in functions
f (x) = 2x + 3
When we evaluate the function:
f (4) = 2 ∗ 4 + 3 = 11
The value of x will not change inside the function body.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 20 / 58
Function Recap
Variables in functions
f (x) = 2x + 3
When we evaluate the function:
f (4) = 2 ∗ 4 + 3 = 11
The value of x will not change inside the function body.
No x = 4 and later x = 21 in the same function body.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 20 / 58
Function Recap
Variables in functions
f (x) = 2x + 3
When we evaluate the function:
f (4) = 2 ∗ 4 + 3 = 11
The value of x will not change inside the function body.
No x = 4 and later x = 21 in the same function body.
Same input, same output. Every time.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 20 / 58
Function Recap
Variables in functions
f (x) = 2x + 3
When we evaluate the function:
f (4) = 2 ∗ 4 + 3 = 11
The value of x will not change inside the function body.
No x = 4 and later x = 21 in the same function body.
Same input, same output. Every time.
In other words, we can replace any occurrence of f (4) with 11
(Referential Transparency)
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 20 / 58
Function Recap
Functions can call other functions
g(x) = f (x) + 1
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 21 / 58
Function Recap
Values are functions
Constant values are just functions with no input parameters
k = 42
def k():
return 42
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 22 / 58
Function Recap
Functions can be combined
h(x) = f (g(x))
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 23 / 58
Function Recap
Higher-order Functions
Functions can take functions as input and/or return
functions as the result.
h(p, q, x) = p(x) + q(2)
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 24 / 58
Function Recap
Higher-order Functions
Functions can take functions as input and/or return
functions as the result.
h(p, q, x) = p(x) + q(2)
h(f , g, 3) = f (3) + g(2)
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 24 / 58
Function Recap
Higher-order Functions
The derivative of cos(x) returns another function.
d
dx cos(x) = − sin(x)
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 25 / 58
Function Recap
A functional program consists entirely of functions
def main(args):
result = do_something_with_args(args)
print result
def do_something_with_args(args):
return ’-’.join(args[1:])
./justfunctions.py hello functional programming
hello-functional-programming
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 26 / 58
Function Recap
A functional program consists entirely of functions
main :: IO()
main = do
args <- getArgs
let result = do_something_with_args args
putStrLn result
do_something_with_args :: [String] -> String
do_something_with_args args = intercalate "-" args
./justfunctions hello functional programming
hello-functional-programming
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 27 / 58
Common FP Idioms
Table of Contents
1 Introduction
2 Definition of Functional Programming
3 Function Recap
4 Common FP Idioms
Recursion
Pattern Matching
Higher-order Functions
5 Higher-order Functions and Lists
6 Advantages of Functional Programming
7 Disdvantages of Functional Programming
8 Companies using Functional Programming
9 Where do we go from here?
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 28 / 58
Common FP Idioms Recursion
Recursive function: Haskell
doubleAll :: Num a => [a] -> [a]
doubleAll [] = []
doubleAll (x:xs) = x*2 : doubleAll xs
Example use in the interactive interpreter:
Prelude Main> doubleAll [8,2,3]
[16,4,6]
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 29 / 58
Common FP Idioms Recursion
Recursive function: Python
def doubleAll(numbers):
if numbers == []:
return []
else:
first = numbers[0]
rest = numbers[1:]
return [first * 2] + doubleAll(rest)
Example use in the interactive interpreter:
>>> doubleAll([8,2,3])
[16, 4, 6]
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 30 / 58
Common FP Idioms Recursion
An iterative version in Python
def doubleAll(numbers):
doubled = []
for num in numbers:
doubled.append(num * 2)
return doubled
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 31 / 58
Common FP Idioms Pattern Matching
Pattern Matching: Haskell
doubleAll :: Num a => [a] -> [a]
doubleAll [] = []
doubleAll (x:xs) = x*2 : doubleAll xs
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 32 / 58
Common FP Idioms Higher-order Functions
Higher-order functions: Python
f (x) = 2x + 3
g(x) = f (x) + 1
h(p, q, x) = p(x) + q(2)
def f(x):
return (2*x) + 3
def g(x):
return f(x) + 1
def h(func_a, func_b, x):
return func_a(x) + func_b(2)
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 33 / 58
Common FP Idioms Higher-order Functions
Higher-order functions: Haskell
f (x) = 2x + 3
g(x) = f (x) + 1
h(p, q, x) = p(x) + q(2)
f x = (2*x) + 3
g x = (f x) + 1
h :: (Int->Int) -> (Int->Int) -> Int -> Int
h func_a func_b x = (func_a x) + (func_b 2)
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 34 / 58
Higher-order Functions and Lists
Table of Contents
1 Introduction
2 Definition of Functional Programming
3 Function Recap
4 Common FP Idioms
Recursion
Pattern Matching
Higher-order Functions
5 Higher-order Functions and Lists
6 Advantages of Functional Programming
7 Disdvantages of Functional Programming
8 Companies using Functional Programming
9 Where do we go from here?
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 35 / 58
Higher-order Functions and Lists
3 Basic List Operations
Map Convert each element of a list into some other value.
Example: Convert a list of students to a list of exam scores.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 36 / 58
Higher-order Functions and Lists
3 Basic List Operations
Map Convert each element of a list into some other value.
Example: Convert a list of students to a list of exam scores.
Filter Get a subset of a list based on some condition. Example:
Filter the entire list of students down to only those that
passed the exam.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 36 / 58
Higher-order Functions and Lists
3 Basic List Operations
Map Convert each element of a list into some other value.
Example: Convert a list of students to a list of exam scores.
Filter Get a subset of a list based on some condition. Example:
Filter the entire list of students down to only those that
passed the exam.
Fold Reduce a list of items to a single value. Example: Reduce
the list of students to a single string of all names.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 36 / 58
Higher-order Functions and Lists
Map
doubleAll :: Num a => [a] -> [a]
doubleAll [] = []
doubleAll (x:xs) = x*2 : doubleAll xs
Looks very similar to the builtin map function:
map :: (a -> b) -> [a] -> [b]
map _ [] = []
map f (x:xs) = f x : map f xs
So our doubleAll can actually be simplified as:
doubleAll = map (*2)
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 37 / 58
Higher-order Functions and Lists
Student Data
data Student = Student { firstName :: String
, lastName :: String
, finalExamScore :: Double}
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 38 / 58
Higher-order Functions and Lists
Student Data
[Student {firstName="John",
lastName="Deer",
finalExamScore=60},
Student {firstName="Billy",
lastName="Bob",
finalExamScore=49.1},
Student {firstName="Jane",
lastName="Doe",
finalExamScore=89},
Student {firstName="Jack",
lastName="Johnson",
finalExamScore=29.3}]
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 39 / 58
Higher-order Functions and Lists
Map
Map type signature:
map :: (a -> b) -> [a] -> [b]
Map on student data:
allscores :: [Student] -> [Double]
allscores students = map finalExamScore students
finalExamScore type signature:
finalExamScore :: Student -> Double
Output:
Final Exam Scores: [60.0,49.1,89.0,29.3]
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 40 / 58
Higher-order Functions and Lists
Filter
Filter type signature:
filter :: (a -> Bool) -> [a] -> [a]
Filter on student data:
passed :: [Student] -> [Student]
passed students = filter has_passed students
has_passed :: Student -> Bool
has_passed student = finalExamScore student >= 60
Output:
Students that have passed:
[John Deer (60.0),Jane Doe (89.0)]
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 41 / 58
Higher-order Functions and Lists
Fold
Fold type signature:
foldl :: (a -> b -> a) -> a -> [b] -> a
Fold on student data:
namecat :: [Student] -> String
namecat students = foldl catfun "" students
catfun :: String -> Student -> String
catfun acc student = acc ++ (firstName student) ++ "n"
Output:
"JohnnBillynJanenJackn"
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 42 / 58
Higher-order Functions and Lists
Shortened Map, Filter, Fold
allscores = map finalExamScore
passed = filter has_passed
has_passed student = finalExamScore student >= 60
namecat = foldl catfun ""
where catfun = (acc student ->
acc ++ (firstName student) ++ "n")
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 43 / 58
Higher-order Functions and Lists
Imperative Filter Example
class Student:
def __init__(self, firstname, lastname, finalexamscore):
self.firstname = firstname
self.lastname = lastname
self.finalexamscore = finalexamscore
def has_passed(self):
return self.finalexamscore >= 60
def passed(students):
passed_students = []
for student in students:
if student.has_passed():
passed_students.append(student)
return passed_students
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 44 / 58
Advantages of Functional Programming
Table of Contents
1 Introduction
2 Definition of Functional Programming
3 Function Recap
4 Common FP Idioms
Recursion
Pattern Matching
Higher-order Functions
5 Higher-order Functions and Lists
6 Advantages of Functional Programming
7 Disdvantages of Functional Programming
8 Companies using Functional Programming
9 Where do we go from here?
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 45 / 58
Advantages of Functional Programming
Perlisism #19
“ A language that doesn’t affect the way you think about
programming, is not worth knowing. ”
— Alan Perlis[5]
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 46 / 58
Advantages of Functional Programming
Advantages of Functional Programming
Changes the way you think about programming and problem solving.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 47 / 58
Advantages of Functional Programming
Advantages of Functional Programming
Changes the way you think about programming and problem solving.
Improvements on the types of abstractions we can do with eg.
higher-order functions.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 47 / 58
Advantages of Functional Programming
Advantages of Functional Programming
Changes the way you think about programming and problem solving.
Improvements on the types of abstractions we can do with eg.
higher-order functions.
Lock-free concurrency.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 47 / 58
Advantages of Functional Programming
Advantages of Functional Programming
Changes the way you think about programming and problem solving.
Improvements on the types of abstractions we can do with eg.
higher-order functions.
Lock-free concurrency.
Improved ways of testing - QuickCheck.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 47 / 58
Advantages of Functional Programming
Advantages of Functional Programming
Changes the way you think about programming and problem solving.
Improvements on the types of abstractions we can do with eg.
higher-order functions.
Lock-free concurrency.
Improved ways of testing - QuickCheck.
More effective reasoning about code.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 47 / 58
Advantages of Functional Programming
Advantages of Functional Programming
Changes the way you think about programming and problem solving.
Improvements on the types of abstractions we can do with eg.
higher-order functions.
Lock-free concurrency.
Improved ways of testing - QuickCheck.
More effective reasoning about code.
Declare possible Null values explicitly if you need them. Goodbye
NullPointerException (mostly).
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 47 / 58
Disdvantages of Functional Programming
Table of Contents
1 Introduction
2 Definition of Functional Programming
3 Function Recap
4 Common FP Idioms
Recursion
Pattern Matching
Higher-order Functions
5 Higher-order Functions and Lists
6 Advantages of Functional Programming
7 Disdvantages of Functional Programming
8 Companies using Functional Programming
9 Where do we go from here?
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 48 / 58
Disdvantages of Functional Programming
Disadvantages of Functional Programming
Steep learning curve.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 49 / 58
Disdvantages of Functional Programming
Disadvantages of Functional Programming
Steep learning curve.
There are some very cryptic concepts.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 49 / 58
Disdvantages of Functional Programming
Disadvantages of Functional Programming
Steep learning curve.
There are some very cryptic concepts.
Tools/IDE’s are not as advanced as the mainstream equivalents.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 49 / 58
Disdvantages of Functional Programming
Disadvantages of Functional Programming
Steep learning curve.
There are some very cryptic concepts.
Tools/IDE’s are not as advanced as the mainstream equivalents.
Order of execution may be difficult to reason about in a lazy language.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 49 / 58
Disdvantages of Functional Programming
Unfamiliar Territory
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[6]
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 50 / 58
Companies using Functional Programming
Table of Contents
1 Introduction
2 Definition of Functional Programming
3 Function Recap
4 Common FP Idioms
Recursion
Pattern Matching
Higher-order Functions
5 Higher-order Functions and Lists
6 Advantages of Functional Programming
7 Disdvantages of Functional Programming
8 Companies using Functional Programming
9 Where do we go from here?
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 51 / 58
Companies using Functional Programming
Companies In South Africa
Pattern Matched Technologies, Midrand Using Erlang for all systems, eg.
processing high volumes of financial transactions.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
Companies using Functional Programming
Companies In South Africa
Pattern Matched Technologies, Midrand Using Erlang for all systems, eg.
processing high volumes of financial transactions.
Eldo Energy, Johannesburg Using Clojure for automated meter reading
and intelligent monitoring of consumer energy.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
Companies using Functional Programming
Companies In South Africa
Pattern Matched Technologies, Midrand Using Erlang for all systems, eg.
processing high volumes of financial transactions.
Eldo Energy, Johannesburg Using Clojure for automated meter reading
and intelligent monitoring of consumer energy.
Rheo Systems, Pretoria Using Clojure for supply chain integration.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
Companies using Functional Programming
Companies In South Africa
Pattern Matched Technologies, Midrand Using Erlang for all systems, eg.
processing high volumes of financial transactions.
Eldo Energy, Johannesburg Using Clojure for automated meter reading
and intelligent monitoring of consumer energy.
Rheo Systems, Pretoria Using Clojure for supply chain integration.
Yuppiechef, Cape Town Using Clojure for their Warehouse Management
System.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
Companies using Functional Programming
Companies In South Africa
Pattern Matched Technologies, Midrand Using Erlang for all systems, eg.
processing high volumes of financial transactions.
Eldo Energy, Johannesburg Using Clojure for automated meter reading
and intelligent monitoring of consumer energy.
Rheo Systems, Pretoria Using Clojure for supply chain integration.
Yuppiechef, Cape Town Using Clojure for their Warehouse Management
System.
Effective Control Systems, Kyalami Using Erlang for printer management.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
Companies using Functional Programming
Companies In South Africa
Pattern Matched Technologies, Midrand Using Erlang for all systems, eg.
processing high volumes of financial transactions.
Eldo Energy, Johannesburg Using Clojure for automated meter reading
and intelligent monitoring of consumer energy.
Rheo Systems, Pretoria Using Clojure for supply chain integration.
Yuppiechef, Cape Town Using Clojure for their Warehouse Management
System.
Effective Control Systems, Kyalami Using Erlang for printer management.
Mira Networks, Somerset West Using Erlang for billing administration and
mobile development.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
Companies using Functional Programming
Companies In South Africa
Pattern Matched Technologies, Midrand Using Erlang for all systems, eg.
processing high volumes of financial transactions.
Eldo Energy, Johannesburg Using Clojure for automated meter reading
and intelligent monitoring of consumer energy.
Rheo Systems, Pretoria Using Clojure for supply chain integration.
Yuppiechef, Cape Town Using Clojure for their Warehouse Management
System.
Effective Control Systems, Kyalami Using Erlang for printer management.
Mira Networks, Somerset West Using Erlang for billing administration and
mobile development.
Amazon.com, Cape Town Using Scala in EC2.
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
Where do we go from here?
Table of Contents
1 Introduction
2 Definition of Functional Programming
3 Function Recap
4 Common FP Idioms
Recursion
Pattern Matching
Higher-order Functions
5 Higher-order Functions and Lists
6 Advantages of Functional Programming
7 Disdvantages of Functional Programming
8 Companies using Functional Programming
9 Where do we go from here?
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 53 / 58
Where do we go from here?
Lambda Luminaries – @lambdaluminary
Local functional programming user group
We meet once a month, on the second Monday of the month.
http://www.meetup.com/lambda-luminaries/
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 54 / 58
Where do we go from here?
Online Courses
Functional Programming Principles in Scala
EPFL University
https://www.coursera.org/course/progfun
School of Haskell
FP Complete
https://www.fpcomplete.com/school
Programming Languages
University of Washington
https://www.coursera.org/course/proglang
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 55 / 58
Where do we go from here?
Books
Miran Lipovaˇca
Learn You a Haskell for Great Good!
http://learnyouahaskell.com/
Fred H´ebert
Learn You Some Erlang for Great Good!
http://learnyousomeerlang.com/
Yaron Minski, Anil Madhavapeddy, Jason Hickey
Real World OCaml
https://realworldocaml.org/
Paul Chiusano, R´unar Bjarnason
Functional Programming in Scala
http://www.manning.com/bjarnason/
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 56 / 58
Where do we go from here?
References I
John Hughes
Why Functional Programming Matters
http:
//www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf
John Carmack
Functional Programming in C++
http://www.altdevblogaday.com/2012/04/26/
functional-programming-in-c/
Edsger W. Dijkstra
Go To Statement Considered Harmful
http://www.u.arizona.edu/~rubinson/copyright_violations/
Go_To_Considered_Harmful.html
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 57 / 58
Where do we go from here?
References II
Tweet by Michael Feathers
https://twitter.com/mfeathers/status/29581296216
Alan Jay Perlis
http://www.cs.yale.edu/quotes.html
Rich Hickey
http://www.infoq.com/presentations/Simple-Made-Easy
Andreas Pauley
An Introduction to Functional Programming
https://github.com/apauley/fp_presentation
Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 58 / 58

More Related Content

What's hot

What's hot (8)

API Docs Made Right / RAML - Swagger rant
API Docs Made Right / RAML - Swagger rantAPI Docs Made Right / RAML - Swagger rant
API Docs Made Right / RAML - Swagger rant
 
Empathic API-Design
Empathic API-DesignEmpathic API-Design
Empathic API-Design
 
REST Coder: Auto Generating Client Stubs and Documentation for REST APIs
REST Coder: Auto Generating Client Stubs and Documentation for REST APIsREST Coder: Auto Generating Client Stubs and Documentation for REST APIs
REST Coder: Auto Generating Client Stubs and Documentation for REST APIs
 
What is Swagger?
What is Swagger?What is Swagger?
What is Swagger?
 
NLP using JavaScript Natural Library
NLP using JavaScript Natural LibraryNLP using JavaScript Natural Library
NLP using JavaScript Natural Library
 
PDQ Programming Languages plus an overview of Alice - Frank Ducrest
PDQ Programming Languages plus an overview of Alice - Frank DucrestPDQ Programming Languages plus an overview of Alice - Frank Ducrest
PDQ Programming Languages plus an overview of Alice - Frank Ducrest
 
Creating multillingual apps for android
Creating multillingual apps for androidCreating multillingual apps for android
Creating multillingual apps for android
 
apidays LIVE London 2021 - Consumer-first APIs in Open Banking by Chris Dudle...
apidays LIVE London 2021 - Consumer-first APIs in Open Banking by Chris Dudle...apidays LIVE London 2021 - Consumer-first APIs in Open Banking by Chris Dudle...
apidays LIVE London 2021 - Consumer-first APIs in Open Banking by Chris Dudle...
 

Viewers also liked

From Imperative to Functional Programming (for Absolute Beginners)
From Imperative to Functional Programming (for Absolute Beginners)From Imperative to Functional Programming (for Absolute Beginners)
From Imperative to Functional Programming (for Absolute Beginners)
Alex Bunardzic
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
pramode_ce
 
Functional programming
Functional programmingFunctional programming
Functional programming
edusmildo
 
Csc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigmCsc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigm
IIUM
 

Viewers also liked (20)

Introduction To Functional Programming
Introduction To Functional ProgrammingIntroduction To Functional Programming
Introduction To Functional Programming
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
From Imperative to Functional Programming (for Absolute Beginners)
From Imperative to Functional Programming (for Absolute Beginners)From Imperative to Functional Programming (for Absolute Beginners)
From Imperative to Functional Programming (for Absolute Beginners)
 
Functional programming for optimization problems in Big Data
Functional programming for optimization problems in Big DataFunctional programming for optimization problems in Big Data
Functional programming for optimization problems in Big Data
 
Functional Programming Principles & Patterns
Functional Programming Principles & PatternsFunctional Programming Principles & Patterns
Functional Programming Principles & Patterns
 
Practical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan HodorogPractical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan Hodorog
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programming
 
那些 Functional Programming 教我的事
那些 Functional Programming 教我的事那些 Functional Programming 教我的事
那些 Functional Programming 教我的事
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculusComputability, turing machines and lambda calculus
Computability, turing machines and lambda calculus
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programming
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojure
 
Csc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigmCsc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigm
 
Core FP Concepts
Core FP ConceptsCore FP Concepts
Core FP Concepts
 
Uses of Cupcakes For Any Occasion in Hyderabad!
 Uses of Cupcakes For Any Occasion in Hyderabad! Uses of Cupcakes For Any Occasion in Hyderabad!
Uses of Cupcakes For Any Occasion in Hyderabad!
 
Oop project briefing sem 1 2015 2016
Oop project briefing  sem 1 2015 2016Oop project briefing  sem 1 2015 2016
Oop project briefing sem 1 2015 2016
 
Functional Programming and Big Data
Functional Programming and Big DataFunctional Programming and Big Data
Functional Programming and Big Data
 

Similar to Introduction to Functional Programming

API Description Languages: Which is the Right One for Me?
API Description Languages: Which is the Right One for Me?API Description Languages: Which is the Right One for Me?
API Description Languages: Which is the Right One for Me?
Akana
 
Maintenance of Dynamically vs. Statically typed Languages
Maintenance of Dynamically vs. Statically typed LanguagesMaintenance of Dynamically vs. Statically typed Languages
Maintenance of Dynamically vs. Statically typed Languages
Amin Bandeali
 

Similar to Introduction to Functional Programming (20)

Reverse engineering and instrumentation of android apps
Reverse engineering and instrumentation of android appsReverse engineering and instrumentation of android apps
Reverse engineering and instrumentation of android apps
 
Hadoop at Lookout
Hadoop at LookoutHadoop at Lookout
Hadoop at Lookout
 
Functional Groovy - Confess
Functional Groovy - ConfessFunctional Groovy - Confess
Functional Groovy - Confess
 
API Description Languages: Which is the Right One for Me?
API Description Languages: Which is the Right One for Me?API Description Languages: Which is the Right One for Me?
API Description Languages: Which is the Right One for Me?
 
API Description Languages: Which is the Right One for Me?
API Description Languages: Which is the Right One for Me?API Description Languages: Which is the Right One for Me?
API Description Languages: Which is the Right One for Me?
 
Get Functional Programming with Clojure
Get Functional Programming with ClojureGet Functional Programming with Clojure
Get Functional Programming with Clojure
 
Open event (show&tell april 2016)
Open event (show&tell april 2016)Open event (show&tell april 2016)
Open event (show&tell april 2016)
 
Scala: An OO Surprise
Scala: An OO SurpriseScala: An OO Surprise
Scala: An OO Surprise
 
Introduction to Clojure
Introduction to ClojureIntroduction to Clojure
Introduction to Clojure
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
 
Maintenance of Dynamically vs. Statically typed Languages
Maintenance of Dynamically vs. Statically typed LanguagesMaintenance of Dynamically vs. Statically typed Languages
Maintenance of Dynamically vs. Statically typed Languages
 
Preparing for Scala 3
Preparing for Scala 3Preparing for Scala 3
Preparing for Scala 3
 
Apache Zeppelin Helium and Beyond
Apache Zeppelin Helium and BeyondApache Zeppelin Helium and Beyond
Apache Zeppelin Helium and Beyond
 
Csharp tp
Csharp tpCsharp tp
Csharp tp
 
Functional conf 2014_schedule
Functional conf 2014_scheduleFunctional conf 2014_schedule
Functional conf 2014_schedule
 
Pyspark vs Spark Let's Unravel the Bond!
Pyspark vs Spark Let's Unravel the Bond!Pyspark vs Spark Let's Unravel the Bond!
Pyspark vs Spark Let's Unravel the Bond!
 
Presentation
PresentationPresentation
Presentation
 
java traning report_Summer.docx
java traning report_Summer.docxjava traning report_Summer.docx
java traning report_Summer.docx
 
Functional programming in scala coursera
Functional programming in scala  courseraFunctional programming in scala  coursera
Functional programming in scala coursera
 
Languages used by web app development services remotestac x
Languages used by web app development services  remotestac xLanguages used by web app development services  remotestac x
Languages used by web app development services remotestac x
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Introduction to Functional Programming

  • 1. An Introduction to Functional Programming Andreas Pauley – @apauley Pattern Matched Technologies Lambda Luminaries @lambdaluminary September 2, 2013 Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 1 / 58
  • 2. Pattern Matched Technologies Developing financial applications in Erlang. http://www.patternmatched.com/ Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 2 / 58
  • 3. Lambda Luminaries – @lambdaluminary Local functional programming user group We meet once a month, on the second Monday of the month. http://www.meetup.com/lambda-luminaries/ Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 3 / 58
  • 4. Introduction Table of Contents 1 Introduction 2 Definition of Functional Programming 3 Function Recap 4 Common FP Idioms Recursion Pattern Matching Higher-order Functions 5 Higher-order Functions and Lists 6 Advantages of Functional Programming 7 Disdvantages of Functional Programming 8 Companies using Functional Programming 9 Where do we go from here? Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 4 / 58
  • 5. Introduction A word from the wise “ 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 [2] Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 5 / 58
  • 6. Introduction Quake Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 6 / 58
  • 7. Introduction A Few Functional Programming Languages Haskell Strong focus on functional purity. Lazy evaluation. Advanced static type system. Native-code compiler. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 7 / 58
  • 8. Introduction A Few Functional Programming Languages Haskell Strong focus on functional purity. Lazy evaluation. Advanced static type system. Native-code compiler. Erlang Focused around concurrency and distributed programming. Strict evaluation. Dynamic typing. Runs on the BEAM VM. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 7 / 58
  • 9. Introduction A Few Functional Programming Languages Haskell Strong focus on functional purity. Lazy evaluation. Advanced static type system. Native-code compiler. Erlang Focused around concurrency and distributed programming. Strict evaluation. Dynamic typing. Runs on the BEAM VM. Clojure A modern Lisp language. Focus on concurrency. Lazy evaluation. Dynamic typing. Runs on the JVM. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 7 / 58
  • 10. Introduction A Few Functional Programming Languages Haskell Strong focus on functional purity. Lazy evaluation. Advanced static type system. Native-code compiler. Erlang Focused around concurrency and distributed programming. Strict evaluation. Dynamic typing. Runs on the BEAM VM. Clojure A modern Lisp language. Focus on concurrency. Lazy evaluation. Dynamic typing. Runs on the JVM. Scala FP and OO. Strict evaluation by default, but supports lazy evaluation. Advanced static type system. Runs on the JVM. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 7 / 58
  • 11. Introduction A Few Functional Programming Languages Haskell Strong focus on functional purity. Lazy evaluation. Advanced static type system. Native-code compiler. Erlang Focused around concurrency and distributed programming. Strict evaluation. Dynamic typing. Runs on the BEAM VM. Clojure A modern Lisp language. Focus on concurrency. Lazy evaluation. Dynamic typing. Runs on the JVM. Scala FP and OO. Strict evaluation by default, but supports lazy evaluation. Advanced static type system. Runs on the JVM. OCaml FP and OO. Part of the ML family. Sometimes claimed to be “faster than C”. Strict evaluation. Advanced static type system. Native-code compiler. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 7 / 58
  • 12. Introduction A Few Functional Programming Languages Haskell Strong focus on functional purity. Lazy evaluation. Advanced static type system. Native-code compiler. Erlang Focused around concurrency and distributed programming. Strict evaluation. Dynamic typing. Runs on the BEAM VM. Clojure A modern Lisp language. Focus on concurrency. Lazy evaluation. Dynamic typing. Runs on the JVM. Scala FP and OO. Strict evaluation by default, but supports lazy evaluation. Advanced static type system. Runs on the JVM. OCaml FP and OO. Part of the ML family. Sometimes claimed to be “faster than C”. Strict evaluation. Advanced static type system. Native-code compiler. F# FP and OO. Based on OCaml. Strict evaluation by default, but supports lazy evaluation. Advanced static type system. Runs on the .NET CLR. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 7 / 58
  • 13. Definition of Functional Programming Table of Contents 1 Introduction 2 Definition of Functional Programming 3 Function Recap 4 Common FP Idioms Recursion Pattern Matching Higher-order Functions 5 Higher-order Functions and Lists 6 Advantages of Functional Programming 7 Disdvantages of Functional Programming 8 Companies using Functional Programming 9 Where do we go from here? Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 8 / 58
  • 14. Definition of Functional Programming So what exactly is Functional Programming? Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 9 / 58
  • 15. Definition of Functional Programming Functional Programming, noun: “ Functional Programming is a list of things you CAN’T do. ” [7] Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 10 / 58
  • 16. Definition of Functional Programming When programming in a functional style/language: You can’t vary your variables. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 11 / 58
  • 17. Definition of Functional Programming When programming in a functional style/language: You can’t vary your variables. You can’t mutate or change your state. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 11 / 58
  • 18. Definition of Functional Programming When programming in a functional style/language: You can’t vary your variables. You can’t mutate or change your state. No while/for loops, sorry. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 11 / 58
  • 19. Definition of Functional Programming When programming in a functional style/language: You can’t vary your variables. You can’t mutate or change your state. No while/for loops, sorry. You can’t have side-effects. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 11 / 58
  • 20. Definition of Functional Programming When programming in a functional style/language: You can’t vary your variables. You can’t mutate or change your state. No while/for loops, sorry. You can’t have side-effects. You can’t control the order of execution (lazy evaluated languages). Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 11 / 58
  • 21. Definition of Functional Programming Are you kidding me? How can anyone program like this??? Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 12 / 58
  • 22. Definition of Functional Programming GOTO 10 This sounds like “You can’t have GOTO statements” See Hughes and Dijkstra [1, 3] Also framed in the negative (you can’t). Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 13 / 58
  • 23. Definition of Functional Programming GOTO 10 This sounds like “You can’t have GOTO statements” See Hughes and Dijkstra [1, 3] Also framed in the negative (you can’t). In hindsight we don’t really need GOTO’s. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 13 / 58
  • 24. Definition of Functional Programming GOTO 10 This sounds like “You can’t have GOTO statements” See Hughes and Dijkstra [1, 3] Also framed in the negative (you can’t). In hindsight we don’t really need GOTO’s. In hindsight it is not about what you cannot do. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 13 / 58
  • 25. Definition of Functional Programming Structured Programming and Functional Programming Structured Programming introduced subroutines with fixed entry/exit points (instead of GOTO’s), resulting in improved modularity. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 14 / 58
  • 26. Definition of Functional Programming Structured Programming and Functional Programming Structured Programming introduced subroutines with fixed entry/exit points (instead of GOTO’s), resulting in improved modularity. Functional Programming provides similar important benefits – more about this later. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 14 / 58
  • 27. Definition of Functional Programming We need a better definition for Functional Programming. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 15 / 58
  • 28. Definition of Functional Programming Functional Programming, noun: “ Functional programming is so called because a program consists entirely of functions. ” — John Hughes, Why Functional Programming Matters [1, p. 1] Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 16 / 58
  • 29. Definition of Functional Programming OK... so what exactly is a function? Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 17 / 58
  • 30. Function Recap Table of Contents 1 Introduction 2 Definition of Functional Programming 3 Function Recap 4 Common FP Idioms Recursion Pattern Matching Higher-order Functions 5 Higher-order Functions and Lists 6 Advantages of Functional Programming 7 Disdvantages of Functional Programming 8 Companies using Functional Programming 9 Where do we go from here? Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 18 / 58
  • 31. Function Recap An example function f (x) = 2x + 3 Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 19 / 58
  • 32. Function Recap Variables in functions f (x) = 2x + 3 When we evaluate the function: f (4) = 2 ∗ 4 + 3 = 11 Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 20 / 58
  • 33. Function Recap Variables in functions f (x) = 2x + 3 When we evaluate the function: f (4) = 2 ∗ 4 + 3 = 11 The value of x will not change inside the function body. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 20 / 58
  • 34. Function Recap Variables in functions f (x) = 2x + 3 When we evaluate the function: f (4) = 2 ∗ 4 + 3 = 11 The value of x will not change inside the function body. No x = 4 and later x = 21 in the same function body. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 20 / 58
  • 35. Function Recap Variables in functions f (x) = 2x + 3 When we evaluate the function: f (4) = 2 ∗ 4 + 3 = 11 The value of x will not change inside the function body. No x = 4 and later x = 21 in the same function body. Same input, same output. Every time. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 20 / 58
  • 36. Function Recap Variables in functions f (x) = 2x + 3 When we evaluate the function: f (4) = 2 ∗ 4 + 3 = 11 The value of x will not change inside the function body. No x = 4 and later x = 21 in the same function body. Same input, same output. Every time. In other words, we can replace any occurrence of f (4) with 11 (Referential Transparency) Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 20 / 58
  • 37. Function Recap Functions can call other functions g(x) = f (x) + 1 Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 21 / 58
  • 38. Function Recap Values are functions Constant values are just functions with no input parameters k = 42 def k(): return 42 Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 22 / 58
  • 39. Function Recap Functions can be combined h(x) = f (g(x)) Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 23 / 58
  • 40. Function Recap Higher-order Functions Functions can take functions as input and/or return functions as the result. h(p, q, x) = p(x) + q(2) Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 24 / 58
  • 41. Function Recap Higher-order Functions Functions can take functions as input and/or return functions as the result. h(p, q, x) = p(x) + q(2) h(f , g, 3) = f (3) + g(2) Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 24 / 58
  • 42. Function Recap Higher-order Functions The derivative of cos(x) returns another function. d dx cos(x) = − sin(x) Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 25 / 58
  • 43. Function Recap A functional program consists entirely of functions def main(args): result = do_something_with_args(args) print result def do_something_with_args(args): return ’-’.join(args[1:]) ./justfunctions.py hello functional programming hello-functional-programming Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 26 / 58
  • 44. Function Recap A functional program consists entirely of functions main :: IO() main = do args <- getArgs let result = do_something_with_args args putStrLn result do_something_with_args :: [String] -> String do_something_with_args args = intercalate "-" args ./justfunctions hello functional programming hello-functional-programming Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 27 / 58
  • 45. Common FP Idioms Table of Contents 1 Introduction 2 Definition of Functional Programming 3 Function Recap 4 Common FP Idioms Recursion Pattern Matching Higher-order Functions 5 Higher-order Functions and Lists 6 Advantages of Functional Programming 7 Disdvantages of Functional Programming 8 Companies using Functional Programming 9 Where do we go from here? Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 28 / 58
  • 46. Common FP Idioms Recursion Recursive function: Haskell doubleAll :: Num a => [a] -> [a] doubleAll [] = [] doubleAll (x:xs) = x*2 : doubleAll xs Example use in the interactive interpreter: Prelude Main> doubleAll [8,2,3] [16,4,6] Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 29 / 58
  • 47. Common FP Idioms Recursion Recursive function: Python def doubleAll(numbers): if numbers == []: return [] else: first = numbers[0] rest = numbers[1:] return [first * 2] + doubleAll(rest) Example use in the interactive interpreter: >>> doubleAll([8,2,3]) [16, 4, 6] Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 30 / 58
  • 48. Common FP Idioms Recursion An iterative version in Python def doubleAll(numbers): doubled = [] for num in numbers: doubled.append(num * 2) return doubled Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 31 / 58
  • 49. Common FP Idioms Pattern Matching Pattern Matching: Haskell doubleAll :: Num a => [a] -> [a] doubleAll [] = [] doubleAll (x:xs) = x*2 : doubleAll xs Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 32 / 58
  • 50. Common FP Idioms Higher-order Functions Higher-order functions: Python f (x) = 2x + 3 g(x) = f (x) + 1 h(p, q, x) = p(x) + q(2) def f(x): return (2*x) + 3 def g(x): return f(x) + 1 def h(func_a, func_b, x): return func_a(x) + func_b(2) Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 33 / 58
  • 51. Common FP Idioms Higher-order Functions Higher-order functions: Haskell f (x) = 2x + 3 g(x) = f (x) + 1 h(p, q, x) = p(x) + q(2) f x = (2*x) + 3 g x = (f x) + 1 h :: (Int->Int) -> (Int->Int) -> Int -> Int h func_a func_b x = (func_a x) + (func_b 2) Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 34 / 58
  • 52. Higher-order Functions and Lists Table of Contents 1 Introduction 2 Definition of Functional Programming 3 Function Recap 4 Common FP Idioms Recursion Pattern Matching Higher-order Functions 5 Higher-order Functions and Lists 6 Advantages of Functional Programming 7 Disdvantages of Functional Programming 8 Companies using Functional Programming 9 Where do we go from here? Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 35 / 58
  • 53. Higher-order Functions and Lists 3 Basic List Operations Map Convert each element of a list into some other value. Example: Convert a list of students to a list of exam scores. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 36 / 58
  • 54. Higher-order Functions and Lists 3 Basic List Operations Map Convert each element of a list into some other value. Example: Convert a list of students to a list of exam scores. Filter Get a subset of a list based on some condition. Example: Filter the entire list of students down to only those that passed the exam. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 36 / 58
  • 55. Higher-order Functions and Lists 3 Basic List Operations Map Convert each element of a list into some other value. Example: Convert a list of students to a list of exam scores. Filter Get a subset of a list based on some condition. Example: Filter the entire list of students down to only those that passed the exam. Fold Reduce a list of items to a single value. Example: Reduce the list of students to a single string of all names. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 36 / 58
  • 56. Higher-order Functions and Lists Map doubleAll :: Num a => [a] -> [a] doubleAll [] = [] doubleAll (x:xs) = x*2 : doubleAll xs Looks very similar to the builtin map function: map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs So our doubleAll can actually be simplified as: doubleAll = map (*2) Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 37 / 58
  • 57. Higher-order Functions and Lists Student Data data Student = Student { firstName :: String , lastName :: String , finalExamScore :: Double} Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 38 / 58
  • 58. Higher-order Functions and Lists Student Data [Student {firstName="John", lastName="Deer", finalExamScore=60}, Student {firstName="Billy", lastName="Bob", finalExamScore=49.1}, Student {firstName="Jane", lastName="Doe", finalExamScore=89}, Student {firstName="Jack", lastName="Johnson", finalExamScore=29.3}] Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 39 / 58
  • 59. Higher-order Functions and Lists Map Map type signature: map :: (a -> b) -> [a] -> [b] Map on student data: allscores :: [Student] -> [Double] allscores students = map finalExamScore students finalExamScore type signature: finalExamScore :: Student -> Double Output: Final Exam Scores: [60.0,49.1,89.0,29.3] Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 40 / 58
  • 60. Higher-order Functions and Lists Filter Filter type signature: filter :: (a -> Bool) -> [a] -> [a] Filter on student data: passed :: [Student] -> [Student] passed students = filter has_passed students has_passed :: Student -> Bool has_passed student = finalExamScore student >= 60 Output: Students that have passed: [John Deer (60.0),Jane Doe (89.0)] Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 41 / 58
  • 61. Higher-order Functions and Lists Fold Fold type signature: foldl :: (a -> b -> a) -> a -> [b] -> a Fold on student data: namecat :: [Student] -> String namecat students = foldl catfun "" students catfun :: String -> Student -> String catfun acc student = acc ++ (firstName student) ++ "n" Output: "JohnnBillynJanenJackn" Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 42 / 58
  • 62. Higher-order Functions and Lists Shortened Map, Filter, Fold allscores = map finalExamScore passed = filter has_passed has_passed student = finalExamScore student >= 60 namecat = foldl catfun "" where catfun = (acc student -> acc ++ (firstName student) ++ "n") Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 43 / 58
  • 63. Higher-order Functions and Lists Imperative Filter Example class Student: def __init__(self, firstname, lastname, finalexamscore): self.firstname = firstname self.lastname = lastname self.finalexamscore = finalexamscore def has_passed(self): return self.finalexamscore >= 60 def passed(students): passed_students = [] for student in students: if student.has_passed(): passed_students.append(student) return passed_students Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 44 / 58
  • 64. Advantages of Functional Programming Table of Contents 1 Introduction 2 Definition of Functional Programming 3 Function Recap 4 Common FP Idioms Recursion Pattern Matching Higher-order Functions 5 Higher-order Functions and Lists 6 Advantages of Functional Programming 7 Disdvantages of Functional Programming 8 Companies using Functional Programming 9 Where do we go from here? Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 45 / 58
  • 65. Advantages of Functional Programming Perlisism #19 “ A language that doesn’t affect the way you think about programming, is not worth knowing. ” — Alan Perlis[5] Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 46 / 58
  • 66. Advantages of Functional Programming Advantages of Functional Programming Changes the way you think about programming and problem solving. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 47 / 58
  • 67. Advantages of Functional Programming Advantages of Functional Programming Changes the way you think about programming and problem solving. Improvements on the types of abstractions we can do with eg. higher-order functions. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 47 / 58
  • 68. Advantages of Functional Programming Advantages of Functional Programming Changes the way you think about programming and problem solving. Improvements on the types of abstractions we can do with eg. higher-order functions. Lock-free concurrency. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 47 / 58
  • 69. Advantages of Functional Programming Advantages of Functional Programming Changes the way you think about programming and problem solving. Improvements on the types of abstractions we can do with eg. higher-order functions. Lock-free concurrency. Improved ways of testing - QuickCheck. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 47 / 58
  • 70. Advantages of Functional Programming Advantages of Functional Programming Changes the way you think about programming and problem solving. Improvements on the types of abstractions we can do with eg. higher-order functions. Lock-free concurrency. Improved ways of testing - QuickCheck. More effective reasoning about code. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 47 / 58
  • 71. Advantages of Functional Programming Advantages of Functional Programming Changes the way you think about programming and problem solving. Improvements on the types of abstractions we can do with eg. higher-order functions. Lock-free concurrency. Improved ways of testing - QuickCheck. More effective reasoning about code. Declare possible Null values explicitly if you need them. Goodbye NullPointerException (mostly). Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 47 / 58
  • 72. Disdvantages of Functional Programming Table of Contents 1 Introduction 2 Definition of Functional Programming 3 Function Recap 4 Common FP Idioms Recursion Pattern Matching Higher-order Functions 5 Higher-order Functions and Lists 6 Advantages of Functional Programming 7 Disdvantages of Functional Programming 8 Companies using Functional Programming 9 Where do we go from here? Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 48 / 58
  • 73. Disdvantages of Functional Programming Disadvantages of Functional Programming Steep learning curve. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 49 / 58
  • 74. Disdvantages of Functional Programming Disadvantages of Functional Programming Steep learning curve. There are some very cryptic concepts. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 49 / 58
  • 75. Disdvantages of Functional Programming Disadvantages of Functional Programming Steep learning curve. There are some very cryptic concepts. Tools/IDE’s are not as advanced as the mainstream equivalents. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 49 / 58
  • 76. Disdvantages of Functional Programming Disadvantages of Functional Programming Steep learning curve. There are some very cryptic concepts. Tools/IDE’s are not as advanced as the mainstream equivalents. Order of execution may be difficult to reason about in a lazy language. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 49 / 58
  • 77. Disdvantages of Functional Programming Unfamiliar Territory 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[6] Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 50 / 58
  • 78. Companies using Functional Programming Table of Contents 1 Introduction 2 Definition of Functional Programming 3 Function Recap 4 Common FP Idioms Recursion Pattern Matching Higher-order Functions 5 Higher-order Functions and Lists 6 Advantages of Functional Programming 7 Disdvantages of Functional Programming 8 Companies using Functional Programming 9 Where do we go from here? Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 51 / 58
  • 79. Companies using Functional Programming Companies In South Africa Pattern Matched Technologies, Midrand Using Erlang for all systems, eg. processing high volumes of financial transactions. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
  • 80. Companies using Functional Programming Companies In South Africa Pattern Matched Technologies, Midrand Using Erlang for all systems, eg. processing high volumes of financial transactions. Eldo Energy, Johannesburg Using Clojure for automated meter reading and intelligent monitoring of consumer energy. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
  • 81. Companies using Functional Programming Companies In South Africa Pattern Matched Technologies, Midrand Using Erlang for all systems, eg. processing high volumes of financial transactions. Eldo Energy, Johannesburg Using Clojure for automated meter reading and intelligent monitoring of consumer energy. Rheo Systems, Pretoria Using Clojure for supply chain integration. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
  • 82. Companies using Functional Programming Companies In South Africa Pattern Matched Technologies, Midrand Using Erlang for all systems, eg. processing high volumes of financial transactions. Eldo Energy, Johannesburg Using Clojure for automated meter reading and intelligent monitoring of consumer energy. Rheo Systems, Pretoria Using Clojure for supply chain integration. Yuppiechef, Cape Town Using Clojure for their Warehouse Management System. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
  • 83. Companies using Functional Programming Companies In South Africa Pattern Matched Technologies, Midrand Using Erlang for all systems, eg. processing high volumes of financial transactions. Eldo Energy, Johannesburg Using Clojure for automated meter reading and intelligent monitoring of consumer energy. Rheo Systems, Pretoria Using Clojure for supply chain integration. Yuppiechef, Cape Town Using Clojure for their Warehouse Management System. Effective Control Systems, Kyalami Using Erlang for printer management. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
  • 84. Companies using Functional Programming Companies In South Africa Pattern Matched Technologies, Midrand Using Erlang for all systems, eg. processing high volumes of financial transactions. Eldo Energy, Johannesburg Using Clojure for automated meter reading and intelligent monitoring of consumer energy. Rheo Systems, Pretoria Using Clojure for supply chain integration. Yuppiechef, Cape Town Using Clojure for their Warehouse Management System. Effective Control Systems, Kyalami Using Erlang for printer management. Mira Networks, Somerset West Using Erlang for billing administration and mobile development. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
  • 85. Companies using Functional Programming Companies In South Africa Pattern Matched Technologies, Midrand Using Erlang for all systems, eg. processing high volumes of financial transactions. Eldo Energy, Johannesburg Using Clojure for automated meter reading and intelligent monitoring of consumer energy. Rheo Systems, Pretoria Using Clojure for supply chain integration. Yuppiechef, Cape Town Using Clojure for their Warehouse Management System. Effective Control Systems, Kyalami Using Erlang for printer management. Mira Networks, Somerset West Using Erlang for billing administration and mobile development. Amazon.com, Cape Town Using Scala in EC2. Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 52 / 58
  • 86. Where do we go from here? Table of Contents 1 Introduction 2 Definition of Functional Programming 3 Function Recap 4 Common FP Idioms Recursion Pattern Matching Higher-order Functions 5 Higher-order Functions and Lists 6 Advantages of Functional Programming 7 Disdvantages of Functional Programming 8 Companies using Functional Programming 9 Where do we go from here? Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 53 / 58
  • 87. Where do we go from here? Lambda Luminaries – @lambdaluminary Local functional programming user group We meet once a month, on the second Monday of the month. http://www.meetup.com/lambda-luminaries/ Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 54 / 58
  • 88. Where do we go from here? Online Courses Functional Programming Principles in Scala EPFL University https://www.coursera.org/course/progfun School of Haskell FP Complete https://www.fpcomplete.com/school Programming Languages University of Washington https://www.coursera.org/course/proglang Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 55 / 58
  • 89. Where do we go from here? Books Miran Lipovaˇca Learn You a Haskell for Great Good! http://learnyouahaskell.com/ Fred H´ebert Learn You Some Erlang for Great Good! http://learnyousomeerlang.com/ Yaron Minski, Anil Madhavapeddy, Jason Hickey Real World OCaml https://realworldocaml.org/ Paul Chiusano, R´unar Bjarnason Functional Programming in Scala http://www.manning.com/bjarnason/ Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 56 / 58
  • 90. Where do we go from here? References I John Hughes Why Functional Programming Matters http: //www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf John Carmack Functional Programming in C++ http://www.altdevblogaday.com/2012/04/26/ functional-programming-in-c/ Edsger W. Dijkstra Go To Statement Considered Harmful http://www.u.arizona.edu/~rubinson/copyright_violations/ Go_To_Considered_Harmful.html Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 57 / 58
  • 91. Where do we go from here? References II Tweet by Michael Feathers https://twitter.com/mfeathers/status/29581296216 Alan Jay Perlis http://www.cs.yale.edu/quotes.html Rich Hickey http://www.infoq.com/presentations/Simple-Made-Easy Andreas Pauley An Introduction to Functional Programming https://github.com/apauley/fp_presentation Andreas Pauley – @apauley (Pattern Matched TechnologiesLambda Luminaries @lambdaluminary)Functional Programming September 2, 2013 58 / 58