1. λ
Intro Definitions Features Example Wrap
Practical Functional Programming
Paul Nathan
Logos Bible Software,
Bellingham, WA
Logos Tech Day, 2013
Paul Nathan Practical Functional Programming
2. λ
Intro Definitions Features Example Wrap
Who
Who am I?
Paul Nathan, M.S.C.S.
Full Time SW Developer
Passionate about programming and its languages
Paul Nathan Practical Functional Programming
3. λ
Intro Definitions Features Example Wrap
Why
All language influences how we think
Programming languages shape our capabilities
Expand our minds beyond yesterday
Because languages are cool!
Paul Nathan Practical Functional Programming
4. λ
Intro Definitions Features Example Wrap
Why
All language influences how we think
Programming languages shape our capabilities
Expand our minds beyond yesterday
Because languages are cool!
Paul Nathan Practical Functional Programming
5. λ
Intro Definitions Features Example Wrap
Why
All language influences how we think
Programming languages shape our capabilities
Expand our minds beyond yesterday
Because languages are cool!
Paul Nathan Practical Functional Programming
6. λ
Intro Definitions Features Example Wrap
Why
All language influences how we think
Programming languages shape our capabilities
Expand our minds beyond yesterday
Because languages are cool!
Paul Nathan Practical Functional Programming
7. λ
Intro Definitions Features Example Wrap
Overview
1 Intro
2 Definitions
3 Features
4 Example
5 Wrap
Paul Nathan Practical Functional Programming
8. λ
Intro Definitions Features Example Wrap
What is functional programming?
Bad Joke
Ask three computer scientists, get three answers and two research
projects
Languages which claim they are
First class functions
“Anti” side-effects
Advanced type system?
Paul Nathan Practical Functional Programming
9. λ
Intro Definitions Features Example Wrap
Claimed FP Langs
Partial list of FP languages...
Agda, Clean, Clojure, Common Lisp, Erlang, Javascript, F# .NET,
Haskell, Kernel, ML, occam, OCaml, Python, Ruby, Rust, Scala,
Scheme... and more.
Targeted Languages
Common Lisp
Haskell
Paul Nathan Practical Functional Programming
10. λ
Intro Definitions Features Example Wrap
Considerations
Functional programming(FP) is where most of programming
language research from 1975 on has lived.
Significant work has been done in the areas of correctness,
control structures, static analysis, limits of computation, and
more.
Academic roots feeding into modern tools: C#, F#, Java,
C++11, and more.
FP is based around the lambda calculus rather than the von
Neumman machine - “Math” vs “Engineering”.
Paradigm oriented around recursion, proofs, correctness, and
simplicity.
Paul Nathan Practical Functional Programming
11. λ
Intro Definitions Features Example Wrap
Common Features
First class functions
“Anti” side-effects
Less/different OO
Declarative
Composable (sometimes)
Advanced type system (sometimes)
Paul Nathan Practical Functional Programming
12. λ
Intro Definitions Features Example Wrap
First Class Functions
Functions are “real” objects in the language. They are:
not compiled out (C)
not syntactic sugar (C# delegates)
passable/assignable
Example in Common Lisp
;;oddp is a regular function
CL-USER> (type-of #’oddp)
FUNCTION
;;passing oddp into mapcar
CL-USER> (mapcar #’oddp ’(1 2 3 4))
(T NIL T NIL)
Paul Nathan Practical Functional Programming
13. λ
Intro Definitions Features Example Wrap
Different OO 01/10
Some ideas...
Types without objects
Usually multiple-dispatch
Usually no functions bound to an object
Instead of:
template<typename T> class CClass {
T function();
};
The usual idiom is similar to:
template<typename T> T function(T thing);
Paul Nathan Practical Functional Programming
14. λ
Intro Definitions Features Example Wrap
Different OO 10/10
... and who “usually” uses what...
Many functions & few types (Clojure) “Everything” is a
dict/list/tuple.
Multimethods & many types (Common Lisp) “Everything”
specializes on incoming types
Many types & many functions (Haskell) “Everything” is
pushed into types for type checking
...the above is a crass generalization...
Paul Nathan Practical Functional Programming
15. λ
Intro Definitions Features Example Wrap
Declarative
Say what you want done (Haskell)
let odds = map odd list_of_ints
Instead of how to do it (C++)
vector<bool> odds = new vector<int>();
for(int i = 0; i < list_of_ints.length(); i++)
odds.push_back(odd(list_of_ints[i]));
Paul Nathan Practical Functional Programming
16. λ
Intro Definitions Features Example Wrap
Anti Side Effects
Side effects work against shared memory
Side effects work against parallelism
Side effects work against predictability.
Therefore
FP languages either deprecate side effects or go “pure”, stripping
them out entirely.
Paul Nathan Practical Functional Programming
17. λ
Intro Definitions Features Example Wrap
Composable
Composable
With fewer objects/types, minimal side effects and a
declarative approach, a “composable” approach becomes
more practical in the architecture.
No “uber” framework, many “pluggable” mini-frameworks
Protocols: define an interface without a hierarchy - anything
that behaves that way is a member.
Clojure a fascinating example.
Paul Nathan Practical Functional Programming
18. λ
Intro Definitions Features Example Wrap
Advanced Type Systems?
Theorem 1 (Curry-Howard - approximately)
Because a program is a proof, a well-typed program is a proof of
correctness.
A sufficiently ’advanced’ type system allows this to be true.
Sufficiently advanced languages include Haskell.
The advantage is a more reliable program - unreliable edge
cases in interactions are prohibited by the type system.
Paul Nathan Practical Functional Programming
19. λ
Intro Definitions Features Example Wrap
Practical Advanced Type Systems
In practice, this means...
... no pointers
... no null pointer exceptions
... union types
... pattern matching
... immutability
... no type cast runtime errors
Paul Nathan Practical Functional Programming
20. λ
Intro Definitions Features Example Wrap
Practical Advanced Type Systems
In practice, this means...
... no pointers
... no null pointer exceptions
... union types
... pattern matching
... immutability
... no type cast runtime errors
Paul Nathan Practical Functional Programming
21. λ
Intro Definitions Features Example Wrap
Practical Advanced Type Systems
In practice, this means...
... no pointers
... no null pointer exceptions
... union types
... pattern matching
... immutability
... no type cast runtime errors
Paul Nathan Practical Functional Programming
22. λ
Intro Definitions Features Example Wrap
Practical Advanced Type Systems
In practice, this means...
... no pointers
... no null pointer exceptions
... union types
... pattern matching
... immutability
... no type cast runtime errors
Paul Nathan Practical Functional Programming
23. λ
Intro Definitions Features Example Wrap
Practical Advanced Type Systems
In practice, this means...
... no pointers
... no null pointer exceptions
... union types
... pattern matching
... immutability
... no type cast runtime errors
Paul Nathan Practical Functional Programming
24. λ
Intro Definitions Features Example Wrap
Practical Advanced Type Systems
In practice, this means...
... no pointers
... no null pointer exceptions
... union types
... pattern matching
... immutability
... no type cast runtime errors
Paul Nathan Practical Functional Programming
25. λ
Intro Definitions Features Example Wrap
Demo problem
Demo problem
Measure similarity between users according to the SKUs they buy.
Similarity between two sets A and B is defined here as the Jaccard
Index:
|A∩B|
|A∪B|
where 1.0 denotes 100% similarity and 0.0 denotes 0.0% similarity
Paul Nathan Practical Functional Programming
26. λ
Intro Definitions Features Example Wrap
Data structures
The data structures needed to handle this are:
sku - ID + description
user - ID + name
purchase (link between user and sku) - sku + id + cost
Paul Nathan Practical Functional Programming
27. λ
Intro Definitions Features Example Wrap
Haskell - datatypes
Haskell
1 data Sku = Sku SkuID String
2 deriving (Show , Eq)
3
4 data User = User UserID String
5 deriving (Show , Eq)
6
7 data Purchase = Purchase UserID SkuID Dollars
8 deriving (Show , Eq)
9
10 -- Datatype to pretty -print simiarities
11 data Similarity = Similarity UserID UserID Double
12 deriving (Show , Eq)
13
14 -- pattern matching
15 instance Ord Similarity where
16 compare (Similarity _ _ valuea) (Similarity _ _ valueb) = compare valueb
valuea
deriving (...) adds in machinery for pretty printing and equality.
instance Ord... adds in machinery for comparisons.
Paul Nathan Practical Functional Programming
28. λ
Intro Definitions Features Example Wrap
Common Lisp - datatypes
Common Lisp
1 (defstruct user id name)
2
3 (defstruct sku sku name)
4
5 (defstruct purchase userid sku cost)
Under the hood: getters/setters, pretty-printing, and a constructor
created. No equality, ordering.
Paul Nathan Practical Functional Programming
29. λ
Intro Definitions Features Example Wrap
Haskell - Jaccard
Haskell
1
2 -- Force integer ->floating point division
3 -- Note the type signature.
4 fdiv :: (Fractional a, Integral a1 , Integral a2) => a1 -> a2 -> a
5 fdiv a b = ( fromIntegral a) / ( fromIntegral b)
6
7 jaccard_index :: (Eq a1 , Fractional a) => [a1] -> [a1] -> a
8 jaccard_index a b =
9 (length (intersect a b)) ‘fdiv ‘ (length (union a b))
Surprise: Does not perform automatic conversions from integer to
float. We can specify the precise behavior of the divide though.
Paul Nathan Practical Functional Programming
30. λ
Intro Definitions Features Example Wrap
Common Lisp - Jaccard
Common Lisp’s Jaccard
1 (defun jaccard-index (a b)
2 (/
3 (length ( intersection a b))
4 (length (union a b))))
Looks good, tastes great - turns out Common Lisp has a ’rational’
type.
What about equality of the members of ’a’ and ’b’? What if a
type error lurks there?
Paul Nathan Practical Functional Programming
31. λ
Intro Definitions Features Example Wrap
Comments
Both programs effectively implement the same approach.
Type errors in Lisp were caught at run-time; in Haskell they
were caught at compile time.
Haskell can deboilerplate using types; Common Lisp can
deboilerplate using macros & run-time reflection
These statements can be generalized to their respective
families.
Paul Nathan Practical Functional Programming
32. λ
Intro Definitions Features Example Wrap
Summary 01/10
Functional programming is...
an alternative approach to program design
rooted in lambda calculus
oriented towards function-based abstractions
avoidant of side effects
Paul Nathan Practical Functional Programming
33. λ
Intro Definitions Features Example Wrap
Summary 10/10
Practically, functional programming involves...
...learning new ways of thinking
...doing more with less code
...fewer bugs in different areas
Paul Nathan Practical Functional Programming
34. λ
Intro Definitions Features Example Wrap
What must I do to be functional?
Which concepts are “the biggest bang for the buck” for you?
Learn a tool that gets you those.
Then make a bigger bang
Paul Nathan Practical Functional Programming
35. λ
Intro Definitions Features Example Wrap
Questions
λ
Questions
?
Paul Nathan Practical Functional Programming
36. λ
Intro Definitions Features Example Wrap
References
http://james-iry.blogspot.com/2009/05/
brief-incomplete-and-mostly-wrong.html
http://fsharpforfunandprofit.com/
http://www.cse.chalmers.se/~rjmh/Papers/whyfp.pdf
http://www.manning.com/fogus2/JoC2e_meap_ch1.pdf
Paul Nathan Practical Functional Programming