SlideShare a Scribd company logo
Copyright © SELA Software & Education Labs, Ltd. | 14-18 Baruch Hirsch St., Bnei Brak 51202, Israel | www.selagroup.com
Functional Programming
High Level Introduction
elada@sela.co.il
http://il.linkedin.com/in/eladavneri/
http://blogs.microsoft.co.il/elada
Elad Avneri
Architect, ALM and Software
Development
Agenda
The Basics
Principles
FP Languages
Features
Comparison
Demo: Imperative to FP Evolution
3
Software Development Challenges
Modern software becomes larger and more
complex
We’d like to reduce the time and cost of software
development
We’d like to increase the quality of the
developed software
FP to the Rescue
Promotes composition and
modularity
Promotes simpler code
Concise, promotes smaller code
Concurrency is not an issue
Dramatically reduces number of
bugs
The Goal of this Presentation
Is not to convince to abandon OOP
Is to open your mind for additional
paradigm
Learn techniques which you can
utilize in your favorite
programming language
http://theburningmonk.com/2015/04/dont-learn-a-syntax-learn-to-change-the-way-you-think/
A
A
An appetizer:
sorted :: [Integer] -> Bool
sorted [] = True
sorted [_] = True
sorted (x:y:zs) = x < y && sorted (y:zs)
So, What Is It All About?
Functional ProgrammingOther programming styles
Classes, proceduresBuilding Blocks
N/AA mustState changes
Function calls,Loops, conditionals, and
function (method) calls
Main control flow
FP Principles
Immutability
Pure functions
Modularity and composition
Declarative
Immutability
Thread safe
Data flow is explicit
Cacheable objects
"How many Haskell programmers does it take to
change a lightbulb?“ "Haskell programmers don't
"change" lightbulbs, they "replace" them. And you
must also replace the whole house at the same
time.”
Idempotence: Get the same results no
matter how many times a function is called
We gain the ability to cache results for
function calls
Insanity: doing the same thing over and over
again and expecting different results.
Albert Einstein
Pure Functions
A function has no side effect
 We gain clarity
 We gain “laziness”:
int foo()
{
bar();
return 0;
}
Pure Functions
Function calls are independent
Can be parallelized
result = func1(x,y) + func2(x,z)
Pure Functions
Pure Functions
Encapsulation is in the function level
Easier to debug
Easier to test in isolation
Modularity and Composability
A function’s output is only its return value
We gain:
Modularity with light coupling
Composability
Declarative
The focus is on what the computer should do
rather than how it should do it
Think of a spreadsheet:
We don’t specify the order of calculations
We don’t deal with variables
We deal with expressions and not with steps
We tell it what we want not how to achieve it
Think of SQL…
FP is the same but for general purpose
Imperative
List<int> results = new List<int>();
foreach(var num in collection)
{
if (num % 2 != 0)
results.Add(num);
}
Declarative vs Imperative
Declarative
var results = collection.Where( num => num % 2 != 0);
List<int> collection = new List<int> { 1, 2, 3, 4, 5 };
http://stackoverflow.com/a/1784702/871579
Myths and Misconceptions
It’s difficult
It’s not difficult it’s “just” unfamiliar
We can do it also in XXX (= your favorite
language)
Maybe, but “real” FP language is “pushing” you there
It’s not practical for real-world applications
Simply wrong
FP has poor performance
Actually some of the features gives great
optimization opportunities
The quality code vs optimized code tradeoff
FP is here from the ‘50s
At the beginning computers were slow
Not enough power to process abstractions
Now it’s back!
The Retro of FP
Functional Programming/Language
Functional programming is style of
programming
A functional language is one that supports and
encourages the functional style
(Some) Functional Language Features
Separate data from behavior
Functions as First-Class Values
High-Order Functions
Recursion
Pattern matching
Algebraic data types
Lazy evaluation
Separate Data from Behavior
No classes and objects
Data passes between functions as parameters
and return values
Functions as First-Class Values
You can do with functions whatever you can do
with other data-types. E.g.:
Bind identifier to a function
Store a function in a data structure
let squareIt = fun n -> n * n
let doubleIt = fun n -> 2 * n
let funList = [ squareIt; doubleIt ]
Functions as First-Class Values
High order functions
let compose =
fun op1 op2 ->
// Use a let expression to build the function that will
be returned.
let funToReturn = fun n ->
op1 (op2 n)
// Then just return it.
funToReturn
let squareAndDouble = compose doubleIt squareIt
// The following expression squares 3, doubles 9, returns 18, and
// then displays 18.
System.Console.WriteLine(squareAndDouble 3)
Recursion
No loops
factorial n = if n < 2 then 1 else n * factorial (n-1)
Pattern Matching
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)
fib :: Integer -> Integer
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
length' :: (Num b) => [a] -> b
length' [] = 0
length' (_:xs) = 1 + length' xs
Algebraic Data Types
data Bool = False | True
data Point = Point Float Float deriving (Show)
data Shape =
Circle Point Float | Rectangle Point Point deriving (Show)
surface :: Shape -> Float
surface (Circle _ r) = pi * r ^ 2
surface (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 -
x1) * (abs $ y2 - y1)
Lazy Evaluation
Evaluation is deferred until their results are
needed
Allows using infinite listsnats = [0..]
odds = [1,3..]
ones = 1 : ones
-- What will happen when executing the following?
length odds
take 5 odds
Language Comparison
F#ScalaHaskell
CLR (.Net)JVMNativeRuntime
Medium1High (-)HighFP Adherence
Default (but may
be not)
Default (but may
be not)
YesImmutability
YesNot by default (but
can be)
YesLazy Evaluation
YesYesYesAlgebraic Data
Types
1) E.g. functions are not guaranteed to be pure: let tm = DateTime.Now
Demo
Imperative to FP Evolution
What should you Take?
Don’t learn a syntax, learn to change the way
you think
Add new paradigms to your toolbox
Get out of your comfort zone
Questions
Copyright © SELA Software & Education Labs, Ltd. | 14-18 Baruch Hirsch St., Bnei Brak 51202, Israel | www.selagroup.com
Thank You
elada@sela.co.il
http://il.linkedin.com/in/eladavneri/
http://blogs.microsoft.co.il/elada
Elad Avneri
Architect, ALM and Software
Development

More Related Content

PDF
Pseudocode & flowchart examples
DOC
Program sba
PPTX
Pseudocode
PPTX
Pseudocode
PPT
pseudo code basics
PPTX
Pseudocode haggis
PPT
Pseudocode basics
DOC
Pseudocode
Pseudocode & flowchart examples
Program sba
Pseudocode
Pseudocode
pseudo code basics
Pseudocode haggis
Pseudocode basics
Pseudocode

What's hot (20)

PPTX
Algorithm and psuedocode
PPTX
Algorithm and pseudo codes
PPT
Algorithms
PDF
02 Control Structures - Loops & Conditions
PPTX
Basic computer-programming-2
DOC
Unit 3
PDF
4 coding from algorithms
PDF
Pseudocode By ZAK
PPTX
pseudocode and Flowchart
PDF
ICP - Lecture 6
PPT
Pseudocode algorithim flowchart
PPTX
C programming enumeration
PPT
Programing Fundamental
PPT
Unit 3 Foc
PDF
Cse115 lecture03problemsolving
PDF
Cse115 lecture04introtoc programming
DOCX
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
PPTX
Pseudocode flowcharts
PPTX
TechDaysNL 2015 - DDD with F#
PDF
Cse115 lecture08repetitionstructures part02
Algorithm and psuedocode
Algorithm and pseudo codes
Algorithms
02 Control Structures - Loops & Conditions
Basic computer-programming-2
Unit 3
4 coding from algorithms
Pseudocode By ZAK
pseudocode and Flowchart
ICP - Lecture 6
Pseudocode algorithim flowchart
C programming enumeration
Programing Fundamental
Unit 3 Foc
Cse115 lecture03problemsolving
Cse115 lecture04introtoc programming
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
Pseudocode flowcharts
TechDaysNL 2015 - DDD with F#
Cse115 lecture08repetitionstructures part02
Ad

Similar to Functional pogramming hl overview (20)

PDF
Introduction to programming : flowchart, algorithm
PPTX
ForLoops.pptx
PDF
GSP 215 RANK Education Counseling--gsp215rank.com
PDF
GSP 215 RANK Education Planning--gsp215rank.com
PDF
GSP 215 RANK Introduction Education--gsp215rank.com
PPT
Computer Programming Computer Programming
DOCX
GSP 215 RANK Education Counseling -- gsp215rank.com
PPTX
lecture 2.pptx
PPT
BP206 - Let's Give Your LotusScript a Tune-Up
DOCX
GSP 215 RANK Education Your Life--gsp215rank.com
DOCX
GSP 215 RANK Lessons in Excellence-- gsp215rank.com
DOCX
GSP 215 RANK Inspiring Innovation--gsp215rank.com
PDF
Logical Expressions in C/C++. Mistakes Made by Professionals
PDF
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
PDF
ELAVARASAN.pdf
PDF
structured programming Introduction to c fundamentals
PPT
3 algorithm-and-flowchart
PPTX
Programming Fundamentals
PDF
Dutch PHP Conference 2013: Distilled
PPTX
Programming str_Language of Logic/c.pptx
Introduction to programming : flowchart, algorithm
ForLoops.pptx
GSP 215 RANK Education Counseling--gsp215rank.com
GSP 215 RANK Education Planning--gsp215rank.com
GSP 215 RANK Introduction Education--gsp215rank.com
Computer Programming Computer Programming
GSP 215 RANK Education Counseling -- gsp215rank.com
lecture 2.pptx
BP206 - Let's Give Your LotusScript a Tune-Up
GSP 215 RANK Education Your Life--gsp215rank.com
GSP 215 RANK Lessons in Excellence-- gsp215rank.com
GSP 215 RANK Inspiring Innovation--gsp215rank.com
Logical Expressions in C/C++. Mistakes Made by Professionals
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ELAVARASAN.pdf
structured programming Introduction to c fundamentals
3 algorithm-and-flowchart
Programming Fundamentals
Dutch PHP Conference 2013: Distilled
Programming str_Language of Logic/c.pptx
Ad

Recently uploaded (20)

PDF
System and Network Administraation Chapter 3
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
ai tools demonstartion for schools and inter college
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
top salesforce developer skills in 2025.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
System and Network Administraation Chapter 3
CHAPTER 2 - PM Management and IT Context
Upgrade and Innovation Strategies for SAP ERP Customers
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
How to Migrate SBCGlobal Email to Yahoo Easily
Wondershare Filmora 15 Crack With Activation Key [2025
How to Choose the Right IT Partner for Your Business in Malaysia
Navsoft: AI-Powered Business Solutions & Custom Software Development
VVF-Customer-Presentation2025-Ver1.9.pptx
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
ai tools demonstartion for schools and inter college
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
top salesforce developer skills in 2025.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Softaken Excel to vCard Converter Software.pdf
Odoo POS Development Services by CandidRoot Solutions

Functional pogramming hl overview

  • 1. Copyright © SELA Software & Education Labs, Ltd. | 14-18 Baruch Hirsch St., Bnei Brak 51202, Israel | www.selagroup.com Functional Programming High Level Introduction elada@sela.co.il http://il.linkedin.com/in/eladavneri/ http://blogs.microsoft.co.il/elada Elad Avneri Architect, ALM and Software Development
  • 3. 3 Software Development Challenges Modern software becomes larger and more complex We’d like to reduce the time and cost of software development We’d like to increase the quality of the developed software
  • 4. FP to the Rescue Promotes composition and modularity Promotes simpler code Concise, promotes smaller code Concurrency is not an issue Dramatically reduces number of bugs
  • 5. The Goal of this Presentation Is not to convince to abandon OOP Is to open your mind for additional paradigm Learn techniques which you can utilize in your favorite programming language http://theburningmonk.com/2015/04/dont-learn-a-syntax-learn-to-change-the-way-you-think/
  • 6. A A An appetizer: sorted :: [Integer] -> Bool sorted [] = True sorted [_] = True sorted (x:y:zs) = x < y && sorted (y:zs) So, What Is It All About? Functional ProgrammingOther programming styles Classes, proceduresBuilding Blocks N/AA mustState changes Function calls,Loops, conditionals, and function (method) calls Main control flow
  • 8. Immutability Thread safe Data flow is explicit Cacheable objects "How many Haskell programmers does it take to change a lightbulb?“ "Haskell programmers don't "change" lightbulbs, they "replace" them. And you must also replace the whole house at the same time.”
  • 9. Idempotence: Get the same results no matter how many times a function is called We gain the ability to cache results for function calls Insanity: doing the same thing over and over again and expecting different results. Albert Einstein Pure Functions
  • 10. A function has no side effect  We gain clarity  We gain “laziness”: int foo() { bar(); return 0; } Pure Functions
  • 11. Function calls are independent Can be parallelized result = func1(x,y) + func2(x,z) Pure Functions
  • 12. Pure Functions Encapsulation is in the function level Easier to debug Easier to test in isolation
  • 13. Modularity and Composability A function’s output is only its return value We gain: Modularity with light coupling Composability
  • 14. Declarative The focus is on what the computer should do rather than how it should do it Think of a spreadsheet: We don’t specify the order of calculations We don’t deal with variables We deal with expressions and not with steps We tell it what we want not how to achieve it Think of SQL… FP is the same but for general purpose
  • 15. Imperative List<int> results = new List<int>(); foreach(var num in collection) { if (num % 2 != 0) results.Add(num); } Declarative vs Imperative Declarative var results = collection.Where( num => num % 2 != 0); List<int> collection = new List<int> { 1, 2, 3, 4, 5 }; http://stackoverflow.com/a/1784702/871579
  • 16. Myths and Misconceptions It’s difficult It’s not difficult it’s “just” unfamiliar We can do it also in XXX (= your favorite language) Maybe, but “real” FP language is “pushing” you there It’s not practical for real-world applications Simply wrong FP has poor performance Actually some of the features gives great optimization opportunities The quality code vs optimized code tradeoff
  • 17. FP is here from the ‘50s At the beginning computers were slow Not enough power to process abstractions Now it’s back! The Retro of FP
  • 18. Functional Programming/Language Functional programming is style of programming A functional language is one that supports and encourages the functional style
  • 19. (Some) Functional Language Features Separate data from behavior Functions as First-Class Values High-Order Functions Recursion Pattern matching Algebraic data types Lazy evaluation
  • 20. Separate Data from Behavior No classes and objects Data passes between functions as parameters and return values
  • 21. Functions as First-Class Values You can do with functions whatever you can do with other data-types. E.g.: Bind identifier to a function Store a function in a data structure let squareIt = fun n -> n * n let doubleIt = fun n -> 2 * n let funList = [ squareIt; doubleIt ]
  • 22. Functions as First-Class Values High order functions let compose = fun op1 op2 -> // Use a let expression to build the function that will be returned. let funToReturn = fun n -> op1 (op2 n) // Then just return it. funToReturn let squareAndDouble = compose doubleIt squareIt // The following expression squares 3, doubles 9, returns 18, and // then displays 18. System.Console.WriteLine(squareAndDouble 3)
  • 23. Recursion No loops factorial n = if n < 2 then 1 else n * factorial (n-1)
  • 24. Pattern Matching factorial :: Integer -> Integer factorial 0 = 1 factorial n = n * factorial (n - 1) fib :: Integer -> Integer fib 0 = 1 fib 1 = 1 fib n = fib (n-1) + fib (n-2) length' :: (Num b) => [a] -> b length' [] = 0 length' (_:xs) = 1 + length' xs
  • 25. Algebraic Data Types data Bool = False | True data Point = Point Float Float deriving (Show) data Shape = Circle Point Float | Rectangle Point Point deriving (Show) surface :: Shape -> Float surface (Circle _ r) = pi * r ^ 2 surface (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 - x1) * (abs $ y2 - y1)
  • 26. Lazy Evaluation Evaluation is deferred until their results are needed Allows using infinite listsnats = [0..] odds = [1,3..] ones = 1 : ones -- What will happen when executing the following? length odds take 5 odds
  • 27. Language Comparison F#ScalaHaskell CLR (.Net)JVMNativeRuntime Medium1High (-)HighFP Adherence Default (but may be not) Default (but may be not) YesImmutability YesNot by default (but can be) YesLazy Evaluation YesYesYesAlgebraic Data Types 1) E.g. functions are not guaranteed to be pure: let tm = DateTime.Now
  • 29. What should you Take? Don’t learn a syntax, learn to change the way you think Add new paradigms to your toolbox Get out of your comfort zone
  • 31. Copyright © SELA Software & Education Labs, Ltd. | 14-18 Baruch Hirsch St., Bnei Brak 51202, Israel | www.selagroup.com Thank You elada@sela.co.il http://il.linkedin.com/in/eladavneri/ http://blogs.microsoft.co.il/elada Elad Avneri Architect, ALM and Software Development