This document provides an introduction to functional programming concepts. It begins by dispelling myths about FP, such as that it requires stopping OO programming or that it lacks classes. It then discusses immutability and why it is useful, defines first-class functions and how they serve like polymorphism in OO. The document also covers built-in regular expressions, and how recursion is preferred over iteration in FP due to immutable defaults. It concludes with a summary of the key topics covered.
1 of 20
Download to read offline
More Related Content
Gentle Introduction To Funcitonal Programming - Detroit.Code
1. A Gentle Introduction To The Basics Of Functional
Programming
Onorio Catenacci,
IT Trainer
United Shore, Troy, MI
2. 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
14. 14
First Class Functions
What is a ¡°First Class¡± Function?
First Class Functions Serve Analogous
Purpose to Polymorphism in OO.
15. 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. 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. 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. 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. 19
Summary/Review
¡ñ FP doesn¡¯t mean ¡°Discard OO¡±
¡ñ FP has default immutability
¡ñ
FP has 1st
class functions
¡ñ FP emphasizes recursion over
iteration.
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
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
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
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
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