SlideShare a Scribd company logo
1 of 31
Download to read offline
Why
Functional Programming
Matters
Why ?
Functional Programming
Matters
“Well-structured software is easy to write to and
to debug, and provides a collection of modules
that can reused to reduce future programming
costs.”
John Hughes - The university, Glasgow
What is a function
The basic, and not very enlightening
definition is this:
“in a functional language, functions are firstclass citizens.”
What is a function
The mean is that
the function takes a function as one of its
arguments.
let double x = x * 2 in
List.map double [ 1; 2; 3 ];;
- : int list = [2; 4; 6]
What is a functional program
FP won't make your word processing
program faster or better. But there are
domains where it is highly useful, and in
particular FP looks like the paradigm of
choice for unlocking the power of multicore
processors.
What is a functional program
A functional program contains no
assignment statements, so the variables
once defined never change their value.
The property to change the variable’s value is
called “side effect”...
A functional program has no side effect.
A function call can have no effect other than
to compute its result, so the function can be
evaluated at any time because the no sideeffect.
A function call can have no effect other than
to compute its result, so the function can
be evaluated at any time because the no
side-effect.
A function call can have no effect other than
to compute its result, so the function can
be evaluated at any time because the no
side-effect.

ult
m

ec
or
ic

lus
lcu
a
A function
like the math

val double =
fn x:int =>
x * 2;
This function accepts an integer argument
and yields its double.
A function
like the math

val double =
fn x:int =>
x * 2;
The keyword val binds a value to the
variable, in this case the value is a function.
A function
like the math

val double =
fn x:int =>
x * 2;
The function get a list of arguments and
yields the computed result.
Partial application
fun sum x y = x + y;
sum

-> is a function

sum 1 3 -> is an integer value
Partial application
sum 2 =

??? WTF

the partial application
returns a function that
takes an int and returns an int
fn : int -> int
Partial application

Haskell Curry

fn (x, y, z, ...)
fn x => fn y => fn z => ...
Datatype [ list ]
listof * := Nil | Cons * (listof *)
This line defines a list of * (whatever is *)
that could be either Nil (an empty list)
or a Cons of * and another list.
Example:
[]
means Nil
[1,2]
means Cons 1(Cons 2(Cons Nil))
Datatype [ list ]
*

Cons n (

*

)

Cons n (

*

)

Cons n ( Nil )
Pattern Matching
defining functions by cases:
fun and_operator true true = true
| and_operator x
y
= false;
When both arguments are true the result is
true,
result is false in the other cases.
Sum the elements of list
The elements of the list could be added by a
recursive “sum“ function
fun sum Nil = 0
| sum (Cons n list) =
n + sum (list)
Sum the elements of list
Examining the definition of sum we see that
there are only two specific computation parts
fun sum Nil = 0
| sum (Cons n list) =
n + sum (list)
Modularizing the function
fun sum Nil = 0
| sum (Cons n list) =
n + sum (list)
Modularizing the function
fun sum Nil = 0
| sum (Cons n list) =
n + sum (list)
foldr f x Nil = x
foldr f x (Cons n list) =
f n ((foldr f x) list)
Modularizing the function
fun sum Nil = 0
| sum (Cons n list) =
n + sum (list)
0 is the base value used for the base case (empty list)
foldr f x Nil = x
foldr f x (Cons n list) =
f n ((foldr f x) list)
Modularizing the function
fun sum Nil = 0
| sum (Cons n list) =
n + sum (list)
0 is the base value used for the base case (empty list)
the + operator is a function

foldr f x Nil = x
foldr f x (Cons n list) =
f n ((foldr f x) list)
Modularizing the function
fun sum Nil = 0
| sum (Cons n list) =
n + sum (list)
0 is the base value used for the base case (empty list)
the + operator is a function

foldr f x Nil = x
foldr f x (Cons n list) =
f n ((foldr f x) list)

sum == foldr (+) 0
The “foldr” function
Now we are able to use the “foldr” function
sum

= foldr (+) 0

multiply

= foldr (*) 1

subtract

= foldr (-) 0
The “foldr” function
Test whether any list of boolean is true
anytrue = foldr (or) false
Test all elements are true
alltrue = foldr (and) true

These are a functions
Everything is a function
In a functional world a complete program is a
function, so like in the math we could
combine programs together.
Combining programs | functions
Now combining the first program with the
second one becomes like combining
mathematical functions
Math says
g • f
in Standard ML
fun combine f g x = f(g(x));
Functional and the Real World

http://www.leafpetersen.com/leaf/publications/hs2013/hrc-paper.pdf
Thanks

@toretto460

More Related Content

What's hot (20)

Functions in C
Functions in CFunctions in C
Functions in C
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
 
Java Week10 Notepad
Java Week10   NotepadJava Week10   Notepad
Java Week10 Notepad
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
Swift the implicit parts
Swift the implicit partsSwift the implicit parts
Swift the implicit parts
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
 
Python list
Python listPython list
Python list
 
functions in C
functions in Cfunctions in C
functions in C
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
 
Arrays
ArraysArrays
Arrays
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
Chap 9(functions)
Chap 9(functions)Chap 9(functions)
Chap 9(functions)
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
List in Python
List in PythonList in Python
List in Python
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 

Viewers also liked

Research Inventy : International Journal of Engineering and Science is publis...
Research Inventy : International Journal of Engineering and Science is publis...Research Inventy : International Journal of Engineering and Science is publis...
Research Inventy : International Journal of Engineering and Science is publis...researchinventy
 
Modeling enterprise architecture using timed colored petri net single process...
Modeling enterprise architecture using timed colored petri net single process...Modeling enterprise architecture using timed colored petri net single process...
Modeling enterprise architecture using timed colored petri net single process...ijmpict
 
ML Tutorial Introduction
ML Tutorial IntroductionML Tutorial Introduction
ML Tutorial Introductionelbop
 
Modelling of walking humanoid robot with capability of floor detection and dy...
Modelling of walking humanoid robot with capability of floor detection and dy...Modelling of walking humanoid robot with capability of floor detection and dy...
Modelling of walking humanoid robot with capability of floor detection and dy...ijfcstjournal
 
Colored petri nets theory and applications
Colored petri nets theory and applicationsColored petri nets theory and applications
Colored petri nets theory and applicationsAbu Hussein
 
言語処理系入門1
言語処理系入門1言語処理系入門1
言語処理系入門1Kenta Hattori
 

Viewers also liked (8)

Research Inventy : International Journal of Engineering and Science is publis...
Research Inventy : International Journal of Engineering and Science is publis...Research Inventy : International Journal of Engineering and Science is publis...
Research Inventy : International Journal of Engineering and Science is publis...
 
Modeling enterprise architecture using timed colored petri net single process...
Modeling enterprise architecture using timed colored petri net single process...Modeling enterprise architecture using timed colored petri net single process...
Modeling enterprise architecture using timed colored petri net single process...
 
ML Tutorial Introduction
ML Tutorial IntroductionML Tutorial Introduction
ML Tutorial Introduction
 
Modelling of walking humanoid robot with capability of floor detection and dy...
Modelling of walking humanoid robot with capability of floor detection and dy...Modelling of walking humanoid robot with capability of floor detection and dy...
Modelling of walking humanoid robot with capability of floor detection and dy...
 
E045026031
E045026031E045026031
E045026031
 
Standard ML / CPN ML
Standard ML / CPN MLStandard ML / CPN ML
Standard ML / CPN ML
 
Colored petri nets theory and applications
Colored petri nets theory and applicationsColored petri nets theory and applications
Colored petri nets theory and applications
 
言語処理系入門1
言語処理系入門1言語処理系入門1
言語処理系入門1
 

Similar to On fuctional programming, high order functions, ML

TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsEelco Visser
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming iiPrashant Kalkar
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Mattersromanandreg
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskellfaradjpour
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsKirill Kozlov
 
Lesson 1
Lesson 1Lesson 1
Lesson 1urenaa
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programmingVisnuDharsini
 
Lesson 1
Lesson 1Lesson 1
Lesson 1urenaa
 
How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1Taisuke Oe
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskellLuca Molteni
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskellgoncharenko
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Sheik Uduman Ali
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query LanguageJulian Hyde
 
Edsc 304 lesson 1
Edsc 304 lesson 1Edsc 304 lesson 1
Edsc 304 lesson 1urenaa
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scalaehsoon
 

Similar to On fuctional programming, high order functions, ML (20)

TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Functional programming java
Functional programming javaFunctional programming java
Functional programming java
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
Lesson 1
Lesson 1Lesson 1
Lesson 1
 
Frp2016 3
Frp2016 3Frp2016 3
Frp2016 3
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
functions
functionsfunctions
functions
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
 
Lesson 1
Lesson 1Lesson 1
Lesson 1
 
How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
 
Map, Reduce and Filter in Swift
Map, Reduce and Filter in SwiftMap, Reduce and Filter in Swift
Map, Reduce and Filter in Swift
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskell
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query Language
 
Edsc 304 lesson 1
Edsc 304 lesson 1Edsc 304 lesson 1
Edsc 304 lesson 1
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
 

More from Simone Di Maulo

More from Simone Di Maulo (6)

Orm hero
Orm heroOrm hero
Orm hero
 
PHP Generators
PHP GeneratorsPHP Generators
PHP Generators
 
Docker cqrs react
Docker cqrs reactDocker cqrs react
Docker cqrs react
 
The dark side of the app
The dark side of the appThe dark side of the app
The dark side of the app
 
Fast api
Fast apiFast api
Fast api
 
Processing asyncrono dei dati - Symfony2 ❤ Message Queuing
Processing asyncrono dei dati - Symfony2 ❤ Message QueuingProcessing asyncrono dei dati - Symfony2 ❤ Message Queuing
Processing asyncrono dei dati - Symfony2 ❤ Message Queuing
 

Recently uploaded

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 

Recently uploaded (20)

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 

On fuctional programming, high order functions, ML

  • 2. Why ? Functional Programming Matters “Well-structured software is easy to write to and to debug, and provides a collection of modules that can reused to reduce future programming costs.” John Hughes - The university, Glasgow
  • 3. What is a function The basic, and not very enlightening definition is this: “in a functional language, functions are firstclass citizens.”
  • 4. What is a function The mean is that the function takes a function as one of its arguments. let double x = x * 2 in List.map double [ 1; 2; 3 ];; - : int list = [2; 4; 6]
  • 5. What is a functional program FP won't make your word processing program faster or better. But there are domains where it is highly useful, and in particular FP looks like the paradigm of choice for unlocking the power of multicore processors.
  • 6. What is a functional program A functional program contains no assignment statements, so the variables once defined never change their value. The property to change the variable’s value is called “side effect”... A functional program has no side effect.
  • 7. A function call can have no effect other than to compute its result, so the function can be evaluated at any time because the no sideeffect.
  • 8. A function call can have no effect other than to compute its result, so the function can be evaluated at any time because the no side-effect.
  • 9. A function call can have no effect other than to compute its result, so the function can be evaluated at any time because the no side-effect. ult m ec or ic lus lcu a
  • 10. A function like the math val double = fn x:int => x * 2; This function accepts an integer argument and yields its double.
  • 11. A function like the math val double = fn x:int => x * 2; The keyword val binds a value to the variable, in this case the value is a function.
  • 12. A function like the math val double = fn x:int => x * 2; The function get a list of arguments and yields the computed result.
  • 13. Partial application fun sum x y = x + y; sum -> is a function sum 1 3 -> is an integer value
  • 14. Partial application sum 2 = ??? WTF the partial application returns a function that takes an int and returns an int fn : int -> int
  • 15. Partial application Haskell Curry fn (x, y, z, ...) fn x => fn y => fn z => ...
  • 16. Datatype [ list ] listof * := Nil | Cons * (listof *) This line defines a list of * (whatever is *) that could be either Nil (an empty list) or a Cons of * and another list. Example: [] means Nil [1,2] means Cons 1(Cons 2(Cons Nil))
  • 17. Datatype [ list ] * Cons n ( * ) Cons n ( * ) Cons n ( Nil )
  • 18. Pattern Matching defining functions by cases: fun and_operator true true = true | and_operator x y = false; When both arguments are true the result is true, result is false in the other cases.
  • 19. Sum the elements of list The elements of the list could be added by a recursive “sum“ function fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
  • 20. Sum the elements of list Examining the definition of sum we see that there are only two specific computation parts fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
  • 21. Modularizing the function fun sum Nil = 0 | sum (Cons n list) = n + sum (list)
  • 22. Modularizing the function fun sum Nil = 0 | sum (Cons n list) = n + sum (list) foldr f x Nil = x foldr f x (Cons n list) = f n ((foldr f x) list)
  • 23. Modularizing the function fun sum Nil = 0 | sum (Cons n list) = n + sum (list) 0 is the base value used for the base case (empty list) foldr f x Nil = x foldr f x (Cons n list) = f n ((foldr f x) list)
  • 24. Modularizing the function fun sum Nil = 0 | sum (Cons n list) = n + sum (list) 0 is the base value used for the base case (empty list) the + operator is a function foldr f x Nil = x foldr f x (Cons n list) = f n ((foldr f x) list)
  • 25. Modularizing the function fun sum Nil = 0 | sum (Cons n list) = n + sum (list) 0 is the base value used for the base case (empty list) the + operator is a function foldr f x Nil = x foldr f x (Cons n list) = f n ((foldr f x) list) sum == foldr (+) 0
  • 26. The “foldr” function Now we are able to use the “foldr” function sum = foldr (+) 0 multiply = foldr (*) 1 subtract = foldr (-) 0
  • 27. The “foldr” function Test whether any list of boolean is true anytrue = foldr (or) false Test all elements are true alltrue = foldr (and) true These are a functions
  • 28. Everything is a function In a functional world a complete program is a function, so like in the math we could combine programs together.
  • 29. Combining programs | functions Now combining the first program with the second one becomes like combining mathematical functions Math says g • f in Standard ML fun combine f g x = f(g(x));
  • 30. Functional and the Real World http://www.leafpetersen.com/leaf/publications/hs2013/hrc-paper.pdf