SlideShare a Scribd company logo
1 of 36
Download to read offline
λ
Intro Definitions Features Example Wrap
Practical Functional Programming
Paul Nathan
Logos Bible Software,
Bellingham, WA
Logos Tech Day, 2013
Paul Nathan Practical Functional Programming
λ
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
λ
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
λ
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
λ
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
λ
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
λ
Intro Definitions Features Example Wrap
Overview
1 Intro
2 Definitions
3 Features
4 Example
5 Wrap
Paul Nathan Practical Functional Programming
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
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
λ
Intro Definitions Features Example Wrap
Questions
λ
Questions
?
Paul Nathan Practical Functional Programming
λ
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

More Related Content

What's hot

Tools for the Toolmakers
Tools for the ToolmakersTools for the Toolmakers
Tools for the ToolmakersCaleb Callaway
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - RecursionAdan Hubahib
 
Architecting Domain-Specific Languages
Architecting Domain-Specific LanguagesArchitecting Domain-Specific Languages
Architecting Domain-Specific LanguagesMarkus Voelter
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05Terry Yoast
 
9781111530532 ppt ch02
9781111530532 ppt ch029781111530532 ppt ch02
9781111530532 ppt ch02Terry Yoast
 
Chapter1pp
Chapter1ppChapter1pp
Chapter1ppJ. C.
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03Terry Yoast
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP ImplementationFridz Felisco
 
From Programming to Modeling And Back Again
From Programming to Modeling And Back AgainFrom Programming to Modeling And Back Again
From Programming to Modeling And Back AgainMarkus Voelter
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaAdan Hubahib
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13Terry Yoast
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04Terry Yoast
 
Agile development with Ruby
Agile development with RubyAgile development with Ruby
Agile development with Rubykhelll
 

What's hot (19)

Programming paradigms
Programming paradigmsProgramming paradigms
Programming paradigms
 
pebble - Building apps on pebble
pebble - Building apps on pebblepebble - Building apps on pebble
pebble - Building apps on pebble
 
Design attern in php
Design attern in phpDesign attern in php
Design attern in php
 
Tools for the Toolmakers
Tools for the ToolmakersTools for the Toolmakers
Tools for the Toolmakers
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - Recursion
 
Architecting Domain-Specific Languages
Architecting Domain-Specific LanguagesArchitecting Domain-Specific Languages
Architecting Domain-Specific Languages
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
9781111530532 ppt ch02
9781111530532 ppt ch029781111530532 ppt ch02
9781111530532 ppt ch02
 
Chapter1pp
Chapter1ppChapter1pp
Chapter1pp
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03
 
Dart programming language
Dart programming languageDart programming language
Dart programming language
 
Prgramming paradigms
Prgramming paradigmsPrgramming paradigms
Prgramming paradigms
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP Implementation
 
From Programming to Modeling And Back Again
From Programming to Modeling And Back AgainFrom Programming to Modeling And Back Again
From Programming to Modeling And Back Again
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
 
Agile development with Ruby
Agile development with RubyAgile development with Ruby
Agile development with Ruby
 

Similar to Presentation

Programming in Scala - Lecture One
Programming in Scala - Lecture OneProgramming in Scala - Lecture One
Programming in Scala - Lecture OneAngelo Corsaro
 
Designing function families and bundles with java's behaviors parameterisatio...
Designing function families and bundles with java's behaviors parameterisatio...Designing function families and bundles with java's behaviors parameterisatio...
Designing function families and bundles with java's behaviors parameterisatio...Alain Lompo
 
Fp for the oo programmer
Fp for the oo programmerFp for the oo programmer
Fp for the oo programmerShawn Button
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesDominic Graefen
 
PL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesPL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesSchwannden Kuo
 
Functional programming
Functional programmingFunctional programming
Functional programmingPiumiPerera7
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
Introduction to scala for a c programmer
Introduction to scala for a c programmerIntroduction to scala for a c programmer
Introduction to scala for a c programmerGirish Kumar A L
 
Clojure and The Robot Apocalypse
Clojure and The Robot ApocalypseClojure and The Robot Apocalypse
Clojure and The Robot Apocalypseelliando dias
 
PHP Reactive Programming at Medan Tech Day 2018
PHP Reactive Programming at Medan Tech Day 2018PHP Reactive Programming at Medan Tech Day 2018
PHP Reactive Programming at Medan Tech Day 2018Dolly Aswin Harahap
 
Functional Programming in R
Functional Programming in RFunctional Programming in R
Functional Programming in RDavid Springate
 
Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Ralf Laemmel
 
Envisioning the Future of Language Workbenches
Envisioning the Future of Language WorkbenchesEnvisioning the Future of Language Workbenches
Envisioning the Future of Language WorkbenchesMarkus Voelter
 
Creativity vs Best Practices
Creativity vs Best PracticesCreativity vs Best Practices
Creativity vs Best PracticesSupun Dissanayake
 
Functional programming is the most extreme programming
Functional programming is the most extreme programmingFunctional programming is the most extreme programming
Functional programming is the most extreme programmingsamthemonad
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8 Bansilal Haudakari
 

Similar to Presentation (20)

Programming in Scala - Lecture One
Programming in Scala - Lecture OneProgramming in Scala - Lecture One
Programming in Scala - Lecture One
 
PARADIGM IT.pptx
PARADIGM IT.pptxPARADIGM IT.pptx
PARADIGM IT.pptx
 
Designing function families and bundles with java's behaviors parameterisatio...
Designing function families and bundles with java's behaviors parameterisatio...Designing function families and bundles with java's behaviors parameterisatio...
Designing function families and bundles with java's behaviors parameterisatio...
 
Fp for the oo programmer
Fp for the oo programmerFp for the oo programmer
Fp for the oo programmer
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love Parantheses
 
PL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesPL Lecture 01 - preliminaries
PL Lecture 01 - preliminaries
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Introduction to scala for a c programmer
Introduction to scala for a c programmerIntroduction to scala for a c programmer
Introduction to scala for a c programmer
 
Clojure and The Robot Apocalypse
Clojure and The Robot ApocalypseClojure and The Robot Apocalypse
Clojure and The Robot Apocalypse
 
PHP Reactive Programming at Medan Tech Day 2018
PHP Reactive Programming at Medan Tech Day 2018PHP Reactive Programming at Medan Tech Day 2018
PHP Reactive Programming at Medan Tech Day 2018
 
Functional Programming in R
Functional Programming in RFunctional Programming in R
Functional Programming in R
 
PYTHON PPT.pptx
PYTHON PPT.pptxPYTHON PPT.pptx
PYTHON PPT.pptx
 
Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)
 
Javascript
JavascriptJavascript
Javascript
 
Envisioning the Future of Language Workbenches
Envisioning the Future of Language WorkbenchesEnvisioning the Future of Language Workbenches
Envisioning the Future of Language Workbenches
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
Creativity vs Best Practices
Creativity vs Best PracticesCreativity vs Best Practices
Creativity vs Best Practices
 
Functional programming is the most extreme programming
Functional programming is the most extreme programmingFunctional programming is the most extreme programming
Functional programming is the most extreme programming
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
 

Recently uploaded

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
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...Neo4j
 
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 slidevu2urc
 
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 RobisonAnna Loughnan Colquhoun
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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.pdfEnterprise Knowledge
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
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 2024The Digital Insurer
 
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 WorkerThousandEyes
 
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)wesley chun
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
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 MenDelhi Call girls
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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 organizationRadu Cotescu
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
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...
 
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
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
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
 
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
 
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)
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

Presentation

  • 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