ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
A Gentle Introduction To The Basics Of Functional
Programming
Onorio Catenacci,
IT Trainer
United Shore, Troy, MI
2
Agenda
Dispelling Myths About FP
What is Immutability and Why Should You Care?
What Are First-Class Functions?
Built-in Regular Expressions
Recursion vs. Iteration
Summary/Review
3
Myths About FP
Adopting FP Ideas Means You Must
Stop Writing OO Code
4
Myths About FP
Adopting FP Ideas Means You Must
Stop Writing OO Code
Learning a New Approach Means
Discarding The Old One.
5
Myths About FP
Adopting FP Ideas Means You Must
Stop Writing OO Code
FP Doesn¡¯t Have Classes.
6
Myths About FP
Adopting FP Ideas Means You Must
Stop Writing OO Code
You Can¡¯t Write FP in Java or C#.
7
Myths About FP
FP is a New Idea
8
Myths About FP
You Can Only Write FP in an FP
Language
9
Myths About FP
OO is More ¡°Natural¡±
10
Immutability
What is Immutability and Why Should
You Care
i++;
11
Immutability
Languages Mostly Help Or Hinder Us In
Terms Of The Defaults!
Developers Won¡¯t Fight Language
Defaults!
12
First Class Functions
What Do We Mean When We Say
¡°Function¡± In FP?
let square n = n * n
13
First Class Functions
What is a ¡°First Class¡± Function?
14
First Class Functions
What is a ¡°First Class¡± Function?
First Class Functions Serve Analogous
Purpose to Polymorphism in OO.
15
First Class Functions
let iseven n = n % 2 == 0
let isodd n = not iseven n
let EvenIsDifferent fnForEven fnForOdd n =
if iseven n then
fnForEven n
else
fnForOdd n
16
Built-In Regular Expressions
let regexDemo fn value =
match (fn value) with
| true -> "It's true"
| false -> "Nope it's false";;
regexDemo iseven 3;;
// val it : string = "Nope it's false"
17
Recursion vs. Iteration
By Default Values Are Immutable In FP
Cannot mutate index into list
for i = 0; i++; i < length(list) //Not allowed!
18
Recursion vs. Iteration
Can recursively call function on list:
let rec sumList lst =
match lst with
| [] -> 0 //Matches empty list
| hd :: tl -> hd + sumList tl ;;
let l = [1..100];;
sumList l;; // 5050
19
Summary/Review
¡ñ FP doesn¡¯t mean ¡°Discard OO¡±
¡ñ FP has default immutability
¡ñ
FP has 1st
class functions
¡ñ FP emphasizes recursion over
iteration.
20
Feedback
Twitter: @OldDutchCap
E-mail: catenacci@ieee.org
Also please fill out survey:
https://tinyurl.com/dcode-fp
Survey is anonymous.

More Related Content

Gentle Introduction To Funcitonal Programming - Detroit.Code

Editor's Notes

  1. Dispelling myths about FP What¡¯s immutability and why does it matter? What¡¯s a first class function? Built-in regular expressions Recursion vs. iteration Summary/review
  2. Someone seems to have gotten the mistaken notion that adopting FP means dropping OO; nothing could be further from the truth. Let¡¯s look at a few of the assumptions underlying that: 1.) Learning a new approach means abandoning an old approach. 2.) FP doesn¡¯t have classes
  3. Someone seems to have gotten the mistaken notion that adopting FP means dropping OO; nothing could be further from the truth. Let¡¯s look at a few of the assumptions underlying that: 1.) Learning a new approach means abandoning an old approach. 2.) FP doesn¡¯t have classes
  4. Someone seems to have gotten the mistaken notion that adopting FP means dropping OO; nothing could be further from the truth. Let¡¯s look at a few of the assumptions underlying that: 1.) Learning a new approach means abandoning an old approach. 2.) FP doesn¡¯t have classes
  5. Someone seems to have gotten the mistaken notion that adopting FP means dropping OO; nothing could be further from the truth. Let¡¯s look at a few of the assumptions underlying that: 1.) Learning a new approach means abandoning an old approach. 2.) FP doesn¡¯t have classes